Finished addressing review.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import LocalBibleRefPlugin from 'main';
|
||||
import { App, Notice, PluginSettingTab, Setting } from 'obsidian';
|
||||
import { App, normalizePath, Notice, PluginSettingTab, Setting, TextComponent } from 'obsidian';
|
||||
import { PassageFormat } from './passage-reference';
|
||||
import { PathSuggest } from './path-suggest';
|
||||
import { VersionSuggest } from './version-suggest';
|
||||
|
||||
export default class LocalBibleRefSettingTab extends PluginSettingTab {
|
||||
private plugin: LocalBibleRefPlugin;
|
||||
@@ -23,13 +24,24 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
|
||||
text.setPlaceholder('e.g. Data/Bibles')
|
||||
.setValue(this.plugin.settings.biblesPath)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.biblesPath = value;
|
||||
// toggle visibility of default version setting
|
||||
if (value) {
|
||||
defaultVersionSetting.settingEl.style.display = 'flex';
|
||||
} else {
|
||||
defaultVersionSetting.settingEl.style.display = 'none';
|
||||
(defaultVersionSetting.components[0] as TextComponent).inputEl.value = '';
|
||||
this.plugin.settings.defaultVersionShorthand = '';
|
||||
}
|
||||
|
||||
const path = value ? normalizePath(value) : '';
|
||||
this.plugin.settings.biblesPath = path;
|
||||
await this.plugin.saveSettings();
|
||||
|
||||
clearTimeout(biblesPathTimeout);
|
||||
biblesPathTimeout = window.setTimeout(async () => {
|
||||
const exists = await this.app.vault.adapter.exists(value);
|
||||
if (!exists) new Notice(`Bibles folder doesn't exist at path: ${value}.`);
|
||||
if (!path) return;
|
||||
const exists = this.app.vault.getFolderByPath(path);
|
||||
if (!exists) new Notice(`Folder doesn't exist at path: ${path}.`);
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
@@ -37,11 +49,11 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
|
||||
});
|
||||
|
||||
let defaultVersionTimeout: number;
|
||||
new Setting(containerEl)
|
||||
const defaultVersionSetting = new Setting(containerEl)
|
||||
.setName('Default version shorthand')
|
||||
.setDesc('The version to use by default - shorthand.')
|
||||
.addText(text => text
|
||||
.setPlaceholder('e.g. NIV')
|
||||
.setDesc('The version to use by default - shorthand. This should correspond to a folder in the bibles folder selected above.')
|
||||
.addText(text => {
|
||||
text.setPlaceholder('e.g. NIV')
|
||||
.setValue(this.plugin.settings.defaultVersionShorthand)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.defaultVersionShorthand = value;
|
||||
@@ -50,10 +62,16 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
|
||||
clearTimeout(defaultVersionTimeout);
|
||||
defaultVersionTimeout = window.setTimeout(async () => {
|
||||
const path = `${this.plugin.settings.biblesPath}/${value}`;
|
||||
const exists = await this.app.vault.adapter.exists(path);
|
||||
if (!exists) new Notice(`Version folder doesn't exist at path: ${path}.`);
|
||||
const exists = this.app.vault.getFolderByPath(normalizePath(path));
|
||||
if (!exists) new Notice(`Folder doesn't exist at path: ${path}.`);
|
||||
}, 1000);
|
||||
}));
|
||||
});
|
||||
|
||||
new VersionSuggest(this.app, text.inputEl, this.plugin.settings);
|
||||
});
|
||||
|
||||
defaultVersionSetting.settingEl.style.display =
|
||||
this.plugin.settings.biblesPath ? 'flex' : 'none';
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Default passage format')
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
EditorSuggest,
|
||||
EditorSuggestContext,
|
||||
EditorSuggestTriggerInfo,
|
||||
normalizePath,
|
||||
Notice,
|
||||
TFile,
|
||||
TFolder,
|
||||
@@ -132,10 +133,11 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
let basePath = "";
|
||||
for (const alias of [ref.book.name, ...ref.book.aliases]) {
|
||||
basePath = [this.settings.biblesPath, ref.version, alias].join("/");
|
||||
basePath = normalizePath(basePath);
|
||||
|
||||
// if the book exists at this alias, use the alias instead
|
||||
// and add the previous book name to the aliases
|
||||
if (await this.app.vault.adapter.exists(basePath)) {
|
||||
if (this.app.vault.getFolderByPath(basePath)) {
|
||||
ref.book.aliases.push(ref.book.name);
|
||||
ref.book.aliases.remove(alias);
|
||||
ref.book.name = alias;
|
||||
@@ -148,7 +150,7 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
// collect chapter texts
|
||||
for (let ch = ref.startChapter; ch <= ref.endChapter; ch++) {
|
||||
const path = basePath + `/${ref.book.name} ${ch}.md`;
|
||||
const file = this.app.vault.getFileByPath(path);
|
||||
const file = this.app.vault.getFileByPath(normalizePath(path));
|
||||
if (!file) return null;
|
||||
|
||||
texts.push(await this.app.vault.cachedRead(file));
|
||||
@@ -301,8 +303,8 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
context: EditorSuggestContext
|
||||
): string {
|
||||
const { version, book, startChapter } = ref;
|
||||
const filePath = `${this.settings.biblesPath}/${version}/${book.name}/${book.name} ${startChapter}.md`;
|
||||
const file = this.app.vault.getFileByPath(filePath);
|
||||
const path = `${this.settings.biblesPath}/${version}/${book.name}/${book.name} ${startChapter}.md`;
|
||||
const file = this.app.vault.getFileByPath(normalizePath(path));
|
||||
if (!file) return ref.stringify();
|
||||
|
||||
return this.app.fileManager.generateMarkdownLink(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AbstractInputSuggest, App } from "obsidian";
|
||||
import { AbstractInputSuggest, App, TFolder } from "obsidian";
|
||||
|
||||
export class PathSuggest extends AbstractInputSuggest<string> {
|
||||
private textInputEl: HTMLInputElement | HTMLDivElement;
|
||||
@@ -9,11 +9,12 @@ export class PathSuggest extends AbstractInputSuggest<string> {
|
||||
}
|
||||
|
||||
async getSuggestions(query: string): Promise<string[]> {
|
||||
let searchPath = '';
|
||||
if (await this.app.vault.adapter.exists(query)) searchPath = query;
|
||||
let folders = (await this.app.vault.adapter.list(searchPath)).folders;
|
||||
folders = folders.filter(folder => !folder.startsWith('.') && folder.startsWith(query));
|
||||
return folders;
|
||||
query ||= '/';
|
||||
const folder = this.app.vault.getFolderByPath(query);
|
||||
if (!folder) return [];
|
||||
return folder.children
|
||||
.filter(c => c instanceof TFolder)
|
||||
.map(f => f.path);
|
||||
}
|
||||
|
||||
renderSuggestion(item: string, el: HTMLElement): void {
|
||||
|
||||
30
src/version-suggest.ts
Normal file
30
src/version-suggest.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { AbstractInputSuggest, App, TFolder } from "obsidian";
|
||||
import { LocalBibleRefSettings } from "./settings";
|
||||
|
||||
export class VersionSuggest extends AbstractInputSuggest<string> {
|
||||
private settings: LocalBibleRefSettings;
|
||||
private textInputEl: HTMLInputElement | HTMLDivElement;
|
||||
|
||||
constructor(app: App, textInputEl: HTMLInputElement | HTMLDivElement, settings: LocalBibleRefSettings) {
|
||||
super(app, textInputEl);
|
||||
this.textInputEl = textInputEl;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
async getSuggestions(): Promise<string[]> {
|
||||
const folder = this.app.vault.getFolderByPath(this.settings.biblesPath);
|
||||
if (!folder) return [];
|
||||
return folder.children
|
||||
.filter(c => c instanceof TFolder)
|
||||
.map(f => f.name);
|
||||
}
|
||||
|
||||
renderSuggestion(item: string, el: HTMLElement): void {
|
||||
el.setText(item);
|
||||
}
|
||||
|
||||
selectSuggestion(item: string, _: MouseEvent | KeyboardEvent): void {
|
||||
this.setValue(item);
|
||||
this.textInputEl.dispatchEvent(new Event('input'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user