diff --git a/api.ts b/api.ts index f605ae3..136ac9c 100644 --- a/api.ts +++ b/api.ts @@ -14,7 +14,7 @@ export module mb { export type VerseID = number export type TranslationID = string - /** An error when constructing a {@link Reference} */ + /** An error when constructing a {@link Reference}. */ export class ReferenceError extends Error { constructor(message?:string) { super(message) @@ -22,7 +22,13 @@ export module mb { } } - /** A reference to a scripture chapter, verse, or verses + /** Options for fetching scripture text. */ + export interface ScriptureOptions { + withVerseNumbers?:boolean + separator?:string + } + + /** A reference to a scripture chapter, verse, or verses. * @example * ```ts * new Reference("Genesis", 1, 1) @@ -282,15 +288,10 @@ export module mb { * @see * - {@link scripture} */ - async text( - withVerseNumbers?:boolean, - separator?:string, - ):Promise { + async text(options?:ScriptureOptions):Promise { return await scripture( this, - withVerseNumbers, - separator, - ) + options, ) } /** Converts this reference to a `string`. @@ -349,15 +350,16 @@ export module mb { */ export async function scripture( ref:Reference|string, - withVerseNumbers?:boolean, - separator?:string, + options?:ScriptureOptions, ):Promise { - withVerseNumbers = withVerseNumbers ?? false - separator = separator ?? " " + if (options === undefined) { + options = {} + } + options.withVerseNumbers = options.withVerseNumbers ?? false + options.separator = options.separator ?? " " if (!(ref instanceof Reference)) { ref = Reference.fromString(ref) } - let version = ref.translation ?? getPlugin().settings.translation let translation = ref.translation ?? getPlugin().settings.reading_translation @@ -415,13 +417,13 @@ export module mb { ); let j = ref.verseStart; while (j < ref.verseEnd + 1 && j < Object.keys(verses).length) { - if (withVerseNumbers) { + if (options.withVerseNumbers) { text += "" + j + " " + verses[j]; if (j != ref.verseEnd) { - text += separator + text += options.separator } } else { - text += verses[j] + separator + text += verses[j] + options.separator } j += 1; } diff --git a/main.ts b/main.ts index d59367a..f345df7 100644 --- a/main.ts +++ b/main.ts @@ -80,21 +80,29 @@ class MyBibleSettings { _built_translation: string; _last_opened_version: Version|undefined + _plugin:MyBible - async set_translation(val: string, plugin: MyBible) { + constructor() {} + + async set_translation(val: string) { if (val === SELECTED_TRANSLATION_OPTION_KEY) { this.translation = val return } let has_translation = false; - let translations = (await plugin.bible_api.get_translations()); + let translations = (await this._plugin.bible_api.get_translations()); if (!(val in translations)) { - this.translation = await plugin.bible_api.get_default_translation(); + this.translation = await this._plugin.bible_api.get_default_translation(); } else { this.translation = val; } } + + async setLastOpenedVersion(version:Version) { + this._last_opened_version = version + this._plugin.saveSettings() + } } const DEFAULT_SETTINGS: MyBibleSettings = { @@ -226,6 +234,9 @@ export async function save_file(path: string, content: string) { } export function translation_to_display_name(translation:Translation):string { + if (translation.language === "") { + return translation.display_name + } return "{0} - {1} - {2}" .format(translation.language, translation.abbreviated_name, translation.display_name) ; @@ -246,7 +257,7 @@ export function cyrb128(str:string): number { h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213); h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179); h1 ^= (h2 ^ h3 ^ h4), h2 ^= h1, h3 ^= h1, h4 ^= h1; - return h1 + return h1 >>> 0 } export function wait(seconds:number):Promise { @@ -265,22 +276,23 @@ export default class MyBible extends Plugin { static plugin:MyBible async onload() { - this.settings._last_opened_version = Version - .fromString(this.manifest.version) - MyBible.plugin = this // @ts-ignore globalThis["mb"] = mb this.legacyParser = new legacy.VerseParser() - - await this.loadSettings(); - this.bible_api = new BollsLifeBibleAPI(); this.bible_api.plugin = this; - await this.settings.set_translation(this.settings.translation, this); + await this.loadSettings() + this.settings._plugin = this + + this.settings.setLastOpenedVersion( + Version.fromString(this.manifest.version) + ) + + await this.settings.set_translation(this.settings.translation) // This adds a simple command that can be triggered anywhere this.addCommand({ @@ -577,7 +589,11 @@ export default class MyBible extends Plugin { } async loadSettings() { - this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); + this.settings = Object.assign( + new MyBibleSettings, + DEFAULT_SETTINGS, + await this.loadData(), + ) } async saveSettings() { @@ -589,6 +605,7 @@ export default class MyBible extends Plugin { delete saving[key] } } + delete saving["_plugin"] await this.saveData(saving) } @@ -1232,7 +1249,7 @@ class BibleAPI { throw new Error("unimplemented") } - async get_default_translation(): Promise { + async _get_default_translation(): Promise { throw new Error("unimplemented") } @@ -1465,7 +1482,7 @@ class BibleAPI { ) } - async _get_default_translation(): Promise { + async get_default_translation(): Promise { return await this.sync_cache.sync( "get_default_translation", () => this._get_default_translation(), @@ -1644,9 +1661,6 @@ class BibleAPI { } } -type BookKey = string; -type ChapterKey = string; - type Translations = Record; interface Translation { display_name: string,