Finished addressing review.

This commit is contained in:
Caleb Campbell
2025-10-09 12:32:46 +11:00
parent 9911e0ce8a
commit 6c83e97297
4 changed files with 80 additions and 29 deletions

View File

@@ -1,7 +1,8 @@
import LocalBibleRefPlugin from 'main'; 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 { PassageFormat } from './passage-reference';
import { PathSuggest } from './path-suggest'; import { PathSuggest } from './path-suggest';
import { VersionSuggest } from './version-suggest';
export default class LocalBibleRefSettingTab extends PluginSettingTab { export default class LocalBibleRefSettingTab extends PluginSettingTab {
private plugin: LocalBibleRefPlugin; private plugin: LocalBibleRefPlugin;
@@ -23,13 +24,24 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
text.setPlaceholder('e.g. Data/Bibles') text.setPlaceholder('e.g. Data/Bibles')
.setValue(this.plugin.settings.biblesPath) .setValue(this.plugin.settings.biblesPath)
.onChange(async (value) => { .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(); await this.plugin.saveSettings();
clearTimeout(biblesPathTimeout); clearTimeout(biblesPathTimeout);
biblesPathTimeout = window.setTimeout(async () => { biblesPathTimeout = window.setTimeout(async () => {
const exists = await this.app.vault.adapter.exists(value); if (!path) return;
if (!exists) new Notice(`Bibles folder doesn't exist at path: ${value}.`); const exists = this.app.vault.getFolderByPath(path);
if (!exists) new Notice(`Folder doesn't exist at path: ${path}.`);
}, 1000); }, 1000);
}); });
@@ -37,11 +49,11 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
}); });
let defaultVersionTimeout: number; let defaultVersionTimeout: number;
new Setting(containerEl) const defaultVersionSetting = new Setting(containerEl)
.setName('Default version shorthand') .setName('Default version shorthand')
.setDesc('The version to use by default - shorthand.') .setDesc('The version to use by default - shorthand. This should correspond to a folder in the bibles folder selected above.')
.addText(text => text .addText(text => {
.setPlaceholder('e.g. NIV') text.setPlaceholder('e.g. NIV')
.setValue(this.plugin.settings.defaultVersionShorthand) .setValue(this.plugin.settings.defaultVersionShorthand)
.onChange(async (value) => { .onChange(async (value) => {
this.plugin.settings.defaultVersionShorthand = value; this.plugin.settings.defaultVersionShorthand = value;
@@ -50,10 +62,16 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
clearTimeout(defaultVersionTimeout); clearTimeout(defaultVersionTimeout);
defaultVersionTimeout = window.setTimeout(async () => { defaultVersionTimeout = window.setTimeout(async () => {
const path = `${this.plugin.settings.biblesPath}/${value}`; const path = `${this.plugin.settings.biblesPath}/${value}`;
const exists = await this.app.vault.adapter.exists(path); const exists = this.app.vault.getFolderByPath(normalizePath(path));
if (!exists) new Notice(`Version folder doesn't exist at path: ${path}.`); if (!exists) new Notice(`Folder doesn't exist at path: ${path}.`);
}, 1000); }, 1000);
})); });
new VersionSuggest(this.app, text.inputEl, this.plugin.settings);
});
defaultVersionSetting.settingEl.style.display =
this.plugin.settings.biblesPath ? 'flex' : 'none';
new Setting(containerEl) new Setting(containerEl)
.setName('Default passage format') .setName('Default passage format')

View File

@@ -5,6 +5,7 @@ import {
EditorSuggest, EditorSuggest,
EditorSuggestContext, EditorSuggestContext,
EditorSuggestTriggerInfo, EditorSuggestTriggerInfo,
normalizePath,
Notice, Notice,
TFile, TFile,
TFolder, TFolder,
@@ -132,10 +133,11 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
let basePath = ""; let basePath = "";
for (const alias of [ref.book.name, ...ref.book.aliases]) { for (const alias of [ref.book.name, ...ref.book.aliases]) {
basePath = [this.settings.biblesPath, ref.version, alias].join("/"); basePath = [this.settings.biblesPath, ref.version, alias].join("/");
basePath = normalizePath(basePath);
// if the book exists at this alias, use the alias instead // if the book exists at this alias, use the alias instead
// and add the previous book name to the aliases // 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.push(ref.book.name);
ref.book.aliases.remove(alias); ref.book.aliases.remove(alias);
ref.book.name = alias; ref.book.name = alias;
@@ -148,7 +150,7 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
// collect chapter texts // collect chapter texts
for (let ch = ref.startChapter; ch <= ref.endChapter; ch++) { for (let ch = ref.startChapter; ch <= ref.endChapter; ch++) {
const path = basePath + `/${ref.book.name} ${ch}.md`; 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; if (!file) return null;
texts.push(await this.app.vault.cachedRead(file)); texts.push(await this.app.vault.cachedRead(file));
@@ -301,8 +303,8 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
context: EditorSuggestContext context: EditorSuggestContext
): string { ): string {
const { version, book, startChapter } = ref; const { version, book, startChapter } = ref;
const filePath = `${this.settings.biblesPath}/${version}/${book.name}/${book.name} ${startChapter}.md`; const path = `${this.settings.biblesPath}/${version}/${book.name}/${book.name} ${startChapter}.md`;
const file = this.app.vault.getFileByPath(filePath); const file = this.app.vault.getFileByPath(normalizePath(path));
if (!file) return ref.stringify(); if (!file) return ref.stringify();
return this.app.fileManager.generateMarkdownLink( return this.app.fileManager.generateMarkdownLink(

View File

@@ -1,4 +1,4 @@
import { AbstractInputSuggest, App } from "obsidian"; import { AbstractInputSuggest, App, TFolder } from "obsidian";
export class PathSuggest extends AbstractInputSuggest<string> { export class PathSuggest extends AbstractInputSuggest<string> {
private textInputEl: HTMLInputElement | HTMLDivElement; private textInputEl: HTMLInputElement | HTMLDivElement;
@@ -9,11 +9,12 @@ export class PathSuggest extends AbstractInputSuggest<string> {
} }
async getSuggestions(query: string): Promise<string[]> { async getSuggestions(query: string): Promise<string[]> {
let searchPath = ''; query ||= '/';
if (await this.app.vault.adapter.exists(query)) searchPath = query; const folder = this.app.vault.getFolderByPath(query);
let folders = (await this.app.vault.adapter.list(searchPath)).folders; if (!folder) return [];
folders = folders.filter(folder => !folder.startsWith('.') && folder.startsWith(query)); return folder.children
return folders; .filter(c => c instanceof TFolder)
.map(f => f.path);
} }
renderSuggestion(item: string, el: HTMLElement): void { renderSuggestion(item: string, el: HTMLElement): void {

30
src/version-suggest.ts Normal file
View 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'));
}
}