diff --git a/src/local-bible-ref-setting-tab.ts b/src/local-bible-ref-setting-tab.ts index c259287..8c3044a 100644 --- a/src/local-bible-ref-setting-tab.ts +++ b/src/local-bible-ref-setting-tab.ts @@ -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,23 +49,29 @@ 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') - .setValue(this.plugin.settings.defaultVersionShorthand) - .onChange(async (value) => { - this.plugin.settings.defaultVersionShorthand = value; - await this.plugin.saveSettings(); + .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; + await this.plugin.saveSettings(); - 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}.`); - }, 1000); - })); + clearTimeout(defaultVersionTimeout); + defaultVersionTimeout = window.setTimeout(async () => { + const path = `${this.plugin.settings.biblesPath}/${value}`; + 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') diff --git a/src/passage-suggest.ts b/src/passage-suggest.ts index 6c0f975..bc7be6e 100644 --- a/src/passage-suggest.ts +++ b/src/passage-suggest.ts @@ -5,6 +5,7 @@ import { EditorSuggest, EditorSuggestContext, EditorSuggestTriggerInfo, + normalizePath, Notice, TFile, TFolder, @@ -132,10 +133,11 @@ export class PassageSuggest extends EditorSuggest { 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 { // 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 { 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( diff --git a/src/path-suggest.ts b/src/path-suggest.ts index f7e498e..365bbc0 100644 --- a/src/path-suggest.ts +++ b/src/path-suggest.ts @@ -1,4 +1,4 @@ -import { AbstractInputSuggest, App } from "obsidian"; +import { AbstractInputSuggest, App, TFolder } from "obsidian"; export class PathSuggest extends AbstractInputSuggest { private textInputEl: HTMLInputElement | HTMLDivElement; @@ -9,11 +9,12 @@ export class PathSuggest extends AbstractInputSuggest { } async getSuggestions(query: string): Promise { - 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 { diff --git a/src/version-suggest.ts b/src/version-suggest.ts new file mode 100644 index 0000000..efa67b5 --- /dev/null +++ b/src/version-suggest.ts @@ -0,0 +1,30 @@ +import { AbstractInputSuggest, App, TFolder } from "obsidian"; +import { LocalBibleRefSettings } from "./settings"; + +export class VersionSuggest extends AbstractInputSuggest { + 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 { + 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')); + } +} \ No newline at end of file