Load hunspell.wasm from the CDN (#20856)

GitOrigin-RevId: 0fcbc2102e280222ab4b9279f02c820a3b904496
This commit is contained in:
Alf Eaton 2024-10-08 09:24:44 +01:00 committed by Copybot
parent 50c74c3168
commit a1ea50ded3
3 changed files with 35 additions and 11 deletions

View file

@ -30,6 +30,7 @@ type Message =
} }
export class HunspellManager { export class HunspellManager {
baseAssetPath: string
dictionariesRoot: string dictionariesRoot: string
hunspellWorker!: Worker hunspellWorker!: Worker
abortController: AbortController | undefined abortController: AbortController | undefined
@ -42,16 +43,13 @@ export class HunspellManager {
private readonly language: string, private readonly language: string,
private readonly learnedWords: string[] private readonly learnedWords: string[]
) { ) {
const baseAssetPath = new URL( this.baseAssetPath = new URL(
getMeta('ol-baseAssetPath'), getMeta('ol-baseAssetPath'),
window.location.href window.location.href
)
this.dictionariesRoot = new URL(
getMeta('ol-dictionariesRoot'),
baseAssetPath
).toString() ).toString()
this.dictionariesRoot = getMeta('ol-dictionariesRoot')
createWorker(() => { createWorker(() => {
this.hunspellWorker = new Worker( this.hunspellWorker = new Worker(
new URL('./hunspell.worker.ts', import.meta.url), new URL('./hunspell.worker.ts', import.meta.url),
@ -97,6 +95,7 @@ export class HunspellManager {
type: 'init', type: 'init',
lang: this.language, lang: this.language,
learnedWords: this.learnedWords, // TODO: add words learnedWords: this.learnedWords, // TODO: add words
baseAssetPath: this.baseAssetPath,
dictionariesRoot: this.dictionariesRoot, dictionariesRoot: this.dictionariesRoot,
}) })
for (const message of this.pendingMessages) { for (const message of this.pendingMessages) {

View file

@ -1,4 +1,5 @@
import Hunspell from './wasm/hunspell' import Hunspell from './wasm/hunspell'
import hunspellWasmPath from './wasm/hunspell.wasm'
type SpellChecker = { type SpellChecker = {
spell(words: string[]): { index: number }[] spell(words: string[]): { index: number }[]
@ -11,13 +12,23 @@ type SpellChecker = {
const createSpellChecker = async ({ const createSpellChecker = async ({
lang, lang,
learnedWords, learnedWords,
baseAssetPath,
dictionariesRoot, dictionariesRoot,
}: { }: {
lang: string lang: string
learnedWords: string[] learnedWords: string[]
baseAssetPath: string
dictionariesRoot: string dictionariesRoot: string
}) => { }) => {
const hunspell = await Hunspell() const fileLocations: Record<string, string> = {
'hunspell.wasm': new URL(hunspellWasmPath, baseAssetPath).toString(),
}
const hunspell = await Hunspell({
locateFile(file: string) {
return fileLocations[file]
},
})
const { const {
cwrap, cwrap,
@ -51,11 +62,13 @@ const createSpellChecker = async ({
FS.mkdir('/dictionaries') FS.mkdir('/dictionaries')
const dictionariesRootURL = new URL(dictionariesRoot, baseAssetPath)
const [dic, aff] = await Promise.all([ const [dic, aff] = await Promise.all([
fetch(new URL(`./${lang}.dic`, dictionariesRoot)).then(response => fetch(new URL(`./${lang}.dic`, dictionariesRootURL)).then(response =>
response.blob() response.blob()
), ),
fetch(new URL(`./${lang}.aff`, dictionariesRoot)).then(response => fetch(new URL(`./${lang}.aff`, dictionariesRootURL)).then(response =>
response.blob() response.blob()
), ),
]) ])

View file

@ -1,2 +1,14 @@
declare module '*.svg' declare module '*.svg' {
declare module '*.png' const src: string
export default src
}
declare module '*.png' {
const src: string
export default src
}
declare module '*.wasm' {
const src: string
export default src
}