Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
GsLogiMaker committed Nov 19, 2024
1 parent 79f923b commit 93a06fb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
36 changes: 19 additions & 17 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ 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)
this.name = "Scripture Reference Error"
}
}

/** 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)
Expand Down Expand Up @@ -282,15 +288,10 @@ export module mb {
* @see
* - {@link scripture}
*/
async text(
withVerseNumbers?:boolean,
separator?:string,
):Promise<string> {
async text(options?:ScriptureOptions):Promise<string> {
return await scripture(
this,
withVerseNumbers,
separator,
)
options, )
}

/** Converts this reference to a `string`.
Expand Down Expand Up @@ -349,15 +350,16 @@ export module mb {
*/
export async function scripture(
ref:Reference|string,
withVerseNumbers?:boolean,
separator?:string,
options?:ScriptureOptions,
):Promise<string> {
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
Expand Down Expand Up @@ -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 += "<sup>" + j + "</sup> " + verses[j];
if (j != ref.verseEnd) {
text += separator
text += options.separator
}
} else {
text += verses[j] + separator
text += verses[j] + options.separator
}
j += 1;
}
Expand Down
48 changes: 31 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
;
Expand All @@ -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<void> {
Expand All @@ -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({
Expand Down Expand Up @@ -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() {
Expand All @@ -589,6 +605,7 @@ export default class MyBible extends Plugin {
delete saving[key]
}
}
delete saving["_plugin"]

await this.saveData(saving)
}
Expand Down Expand Up @@ -1232,7 +1249,7 @@ class BibleAPI {
throw new Error("unimplemented")
}

async get_default_translation(): Promise<string> {
async _get_default_translation(): Promise<string> {
throw new Error("unimplemented")
}

Expand Down Expand Up @@ -1465,7 +1482,7 @@ class BibleAPI {
)
}

async _get_default_translation(): Promise<string> {
async get_default_translation(): Promise<string> {
return await this.sync_cache.sync(
"get_default_translation",
() => this._get_default_translation(),
Expand Down Expand Up @@ -1644,9 +1661,6 @@ class BibleAPI {
}
}

type BookKey = string;
type ChapterKey = string;

type Translations = Record<string, Translation>;
interface Translation {
display_name: string,
Expand Down

0 comments on commit 93a06fb

Please sign in to comment.