Load translation notes by visible chapter
This commit is contained in:
+81
-10
@@ -118,6 +118,8 @@ const browserLibraryCache = new Map<string, Promise<Verse[]>>()
|
||||
const browserStrongDataCache = new Map<string, Promise<BrowserStrongData>>()
|
||||
const browserFootnotesCache = new Map<string, Promise<Map<string, Footnote[]>>>()
|
||||
const browserStudyNotesCache = new Map<string, Promise<Map<string, Footnote[]>>>()
|
||||
const browserChapterFootnotesCache = new Map<string, Promise<Map<string, Footnote[]>>>()
|
||||
const browserChapterStudyNotesCache = new Map<string, Promise<Map<string, Footnote[]>>>()
|
||||
let browserCommentaryCache: Promise<Map<string, CommentaryEntry[]>> | null = null
|
||||
let browserDictionaryCache: Promise<Map<string, DictionaryEntry>> | null = null
|
||||
const browserVerseSourceIds = new Map<number, string>()
|
||||
@@ -273,20 +275,12 @@ async function loadBrowserVersesForTranslation(translationId = defaultTranslatio
|
||||
console.warn('Unable to load browser Strong data.', error)
|
||||
return null
|
||||
})
|
||||
const footnotes = await loadBrowserFootnotes(translationId).catch((error) => {
|
||||
console.warn('Unable to load browser footnotes.', error)
|
||||
return new Map<string, Footnote[]>()
|
||||
})
|
||||
const studyNotes = await loadBrowserStudyNotes(translationId).catch((error) => {
|
||||
console.warn('Unable to load browser study notes.', error)
|
||||
return new Map<string, Footnote[]>()
|
||||
})
|
||||
const tagsByVerse = readBrowserTags()
|
||||
return verses.map((verse) => ({
|
||||
...verse,
|
||||
strongs: strongData?.linksByVerse.get(browserVerseKey(verse)) ?? verse.strongs,
|
||||
footnotes: footnotes.get(browserVerseKey(verse)) ?? verse.footnotes ?? [],
|
||||
studyNotes: studyNotes.get(browserVerseKey(verse)) ?? verse.studyNotes ?? [],
|
||||
footnotes: verse.footnotes ?? [],
|
||||
studyNotes: verse.studyNotes ?? [],
|
||||
tags: tagsByVerse[verse.id] ?? [],
|
||||
}))
|
||||
}
|
||||
@@ -329,6 +323,37 @@ export async function getLibraryState(translationId = defaultTranslationId): Pro
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTranslationNotesForVerses(
|
||||
translationId: string,
|
||||
verses: Verse[],
|
||||
): Promise<Map<string, { footnotes: Footnote[]; studyNotes: Footnote[] }>> {
|
||||
if (inTauri() || verses.length === 0) return new Map()
|
||||
|
||||
const chapterKeys = [
|
||||
...new Set(
|
||||
verses
|
||||
.map((verse) => `${bookIdFromVerse(verse)}.${verse.chapter}`)
|
||||
.filter((key) => !key.startsWith('unknown.')),
|
||||
),
|
||||
]
|
||||
|
||||
const [footnoteMaps, studyNoteMaps] = await Promise.all([
|
||||
Promise.all(chapterKeys.map((key) => loadBrowserChapterNotes(translationId, key, 'footnotes'))),
|
||||
Promise.all(chapterKeys.map((key) => loadBrowserChapterNotes(translationId, key, 'study-notes'))),
|
||||
])
|
||||
|
||||
const notesByVerse = new Map<string, { footnotes: Footnote[]; studyNotes: Footnote[] }>()
|
||||
for (const verse of verses) {
|
||||
const verseKey = browserVerseKey(verse)
|
||||
notesByVerse.set(String(verse.id), {
|
||||
footnotes: footnoteMaps.flatMap((map) => map.get(verseKey) ?? []),
|
||||
studyNotes: studyNoteMaps.flatMap((map) => map.get(verseKey) ?? []),
|
||||
})
|
||||
}
|
||||
|
||||
return notesByVerse
|
||||
}
|
||||
|
||||
export async function searchVerses(query: string, translationId = defaultTranslationId): Promise<Verse[]> {
|
||||
if (inTauri()) {
|
||||
return invoke<Verse[]>('search_verses', { query })
|
||||
@@ -539,6 +564,47 @@ async function loadLibreBibleDevStudyNotes(translationId = defaultTranslationId)
|
||||
return mapImportedNotes(importedFootnotes)
|
||||
}
|
||||
|
||||
async function loadBrowserChapterNotes(
|
||||
translationId: string,
|
||||
chapterKey: string,
|
||||
kind: 'footnotes' | 'study-notes',
|
||||
): Promise<Map<string, Footnote[]>> {
|
||||
const cacheKey = `${translationId}:${chapterKey}:${kind}`
|
||||
const cache =
|
||||
kind === 'footnotes' ? browserChapterFootnotesCache : browserChapterStudyNotesCache
|
||||
|
||||
if (!cache.has(cacheKey)) {
|
||||
cache.set(
|
||||
cacheKey,
|
||||
loadLibreBibleDevChapterNotes(translationId, chapterKey, kind).catch(async () => {
|
||||
const fullNotes =
|
||||
kind === 'footnotes'
|
||||
? await loadBrowserFootnotes(translationId)
|
||||
: await loadBrowserStudyNotes(translationId)
|
||||
const filtered = new Map<string, Footnote[]>()
|
||||
for (const [verseId, notes] of fullNotes) {
|
||||
if (verseId.startsWith(`${chapterKey}.`)) filtered.set(verseId, notes)
|
||||
}
|
||||
return filtered
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
return cache.get(cacheKey)!
|
||||
}
|
||||
|
||||
async function loadLibreBibleDevChapterNotes(
|
||||
translationId: string,
|
||||
chapterKey: string,
|
||||
kind: 'footnotes' | 'study-notes',
|
||||
): Promise<Map<string, Footnote[]>> {
|
||||
const importedNotes = await fetchJsonl<ImportedFootnote>(
|
||||
`${libreBibleDevRoot}/${translationId}/notes-by-chapter/${chapterKey}.${kind}.jsonl`,
|
||||
)
|
||||
|
||||
return mapImportedNotes(importedNotes)
|
||||
}
|
||||
|
||||
function mapImportedNotes(importedFootnotes: ImportedFootnote[]): Map<string, Footnote[]> {
|
||||
const footnotes = new Map<string, Footnote[]>()
|
||||
|
||||
@@ -619,6 +685,11 @@ function strongLanguage(strong: string): 'Hebrew' | 'Greek' {
|
||||
return strong.startsWith('H') ? 'Hebrew' : 'Greek'
|
||||
}
|
||||
|
||||
function bookIdFromVerse(verse: Verse): string {
|
||||
const sourceId = browserVerseSourceIds.get(verse.id)
|
||||
return sourceId?.split('.')[0] ?? 'unknown'
|
||||
}
|
||||
|
||||
function browserVerseKey(verse: Verse) {
|
||||
return browserVerseSourceIds.get(verse.id) ?? ''
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user