Added autocomplete suggestions to the bibles path setting field. Cleaned up a few things.
This commit is contained in:
2
package-lock.json
generated
2
package-lock.json
generated
@@ -6,7 +6,7 @@
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "local-bible-ref",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.6",
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import LocalBibleRefPlugin from 'main';
|
||||
import { App, Notice, PluginSettingTab, Setting } from 'obsidian';
|
||||
import { PassageFormat } from './passage-reference';
|
||||
import { PathSuggest } from './path-suggest';
|
||||
|
||||
export default class LocalBibleRefSettingTab extends PluginSettingTab {
|
||||
plugin: LocalBibleRefPlugin;
|
||||
private plugin: LocalBibleRefPlugin;
|
||||
|
||||
constructor(app: App, plugin: LocalBibleRefPlugin) {
|
||||
super(app, plugin);
|
||||
@@ -18,19 +19,22 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
|
||||
new Setting(containerEl)
|
||||
.setName('Bibles Path')
|
||||
.setDesc('The path to the folder containing your bibles.')
|
||||
.addText(text => text
|
||||
.setPlaceholder('e.g. Data/Bibles')
|
||||
.setValue(this.plugin.settings.biblesPath)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.biblesPath = value;
|
||||
await this.plugin.saveSettings();
|
||||
.addText(text => {
|
||||
text.setPlaceholder('e.g. Data/Bibles')
|
||||
.setValue(this.plugin.settings.biblesPath)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.biblesPath = value;
|
||||
await this.plugin.saveSettings();
|
||||
|
||||
clearTimeout(biblesPathTimeout);
|
||||
biblesPathTimeout = setTimeout(async () => {
|
||||
const exists = await this.app.vault.adapter.exists(value);
|
||||
if (!exists) new Notice(`Bibles folder doesn't exist at path: ${value}.`);
|
||||
}, 1000);
|
||||
}));
|
||||
clearTimeout(biblesPathTimeout);
|
||||
biblesPathTimeout = setTimeout(async () => {
|
||||
const exists = await this.app.vault.adapter.exists(value);
|
||||
if (!exists) new Notice(`Bibles folder doesn't exist at path: ${value}.`);
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
new PathSuggest(this.app, text.inputEl);
|
||||
});
|
||||
|
||||
let defaultVersionTimeout: NodeJS.Timeout;
|
||||
new Setting(containerEl)
|
||||
|
||||
@@ -68,7 +68,7 @@ export class PassageReference implements ChapterReference, PassageOptions {
|
||||
if (this.startVerse === 1 && this.endVerse === -1) {
|
||||
if (this.startChapter === this.endChapter)
|
||||
return (
|
||||
this.book.name + `${this.startChapter} - ${this.version}`
|
||||
this.book.name + ` ${this.startChapter} - ${this.version}`
|
||||
);
|
||||
return (
|
||||
`${this.book.name} ${this.startChapter}-` +
|
||||
@@ -81,7 +81,7 @@ export class PassageReference implements ChapterReference, PassageOptions {
|
||||
if (this.startVerse === this.endVerse)
|
||||
return (
|
||||
this.book.name +
|
||||
`${this.startChapter}:${this.startVerse} - ${this.version}`
|
||||
` ${this.startChapter}:${this.startVerse} - ${this.version}`
|
||||
);
|
||||
return (
|
||||
`${this.book.name} ${this.startChapter}:` +
|
||||
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
EditorSuggest,
|
||||
EditorSuggestContext,
|
||||
EditorSuggestTriggerInfo,
|
||||
Notice,
|
||||
TFile,
|
||||
TFolder,
|
||||
} from "obsidian";
|
||||
import { PassageFormat, PassageReference } from "./passage-reference";
|
||||
@@ -12,6 +14,7 @@ import { LocalBibleRefSettings } from "./settings";
|
||||
|
||||
export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
private settings: LocalBibleRefSettings;
|
||||
private noSettingsNotice: Notice;
|
||||
|
||||
constructor(app: App, settings: LocalBibleRefSettings) {
|
||||
super(app);
|
||||
@@ -21,15 +24,27 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
onTrigger(
|
||||
cursor: EditorPosition,
|
||||
editor: Editor,
|
||||
_: any
|
||||
_: TFile | null
|
||||
): EditorSuggestTriggerInfo | null {
|
||||
// min ref length is 6 ('--gen1')
|
||||
if (cursor.ch < 6) return null;
|
||||
|
||||
// line must start with '--'
|
||||
const line = editor.getLine(cursor.line);
|
||||
if (!line.startsWith("--")) return null;
|
||||
|
||||
// if no settings, alert user
|
||||
if (!this.settings.biblesPath) {
|
||||
if (!this.noSettingsNotice?.noticeEl.isShown()) {
|
||||
const noticeText = "Local Bible Ref settings are not " +
|
||||
"configured. Please set the bibles path before " +
|
||||
"attempting to reference passages.";
|
||||
this.noSettingsNotice = new Notice(noticeText);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// min ref length is 6 ('--gen1')
|
||||
if (cursor.ch < 6) return null;
|
||||
|
||||
// must be a passage ref
|
||||
const isPassage = PassageReference.regExp.test(line);
|
||||
if (!isPassage) return null;
|
||||
@@ -48,9 +63,14 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
async getSuggestions(
|
||||
context: EditorSuggestContext
|
||||
): Promise<PassageSuggestion[]> {
|
||||
let version = this.settings.defaultVersionShorthand;
|
||||
if (!version) {
|
||||
const folder = this.app.vault.getFolderByPath(this.settings.biblesPath);
|
||||
version = folder?.children?.filter(c => c instanceof TFolder)?.first()?.name ?? '';
|
||||
}
|
||||
const passageRef = PassageReference.parse(
|
||||
context.query,
|
||||
this.settings.defaultVersionShorthand,
|
||||
version,
|
||||
this.settings.defaultPassageFormat,
|
||||
);
|
||||
if (!passageRef) return [];
|
||||
@@ -90,7 +110,7 @@ export class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
}
|
||||
|
||||
renderSuggestion(item: PassageSuggestion, el: HTMLElement): void {
|
||||
el.innerText = item.excerpt;
|
||||
el.setText(item.excerpt);
|
||||
}
|
||||
|
||||
selectSuggestion(
|
||||
|
||||
27
src/path-suggest.ts
Normal file
27
src/path-suggest.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { AbstractInputSuggest, App } from "obsidian";
|
||||
|
||||
export class PathSuggest extends AbstractInputSuggest<string> {
|
||||
private textInputEl: HTMLInputElement | HTMLDivElement;
|
||||
|
||||
constructor(app: App, textInputEl: HTMLInputElement | HTMLDivElement) {
|
||||
super(app, textInputEl);
|
||||
this.textInputEl = textInputEl;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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