overleaf/services/web/frontend/js/features/source-editor/hooks/use-hunspell.ts
Alf Eaton ea5b521882 Remove spell check split tests (#21382)
GitOrigin-RevId: 819fc94f55dc1d73e4f58e36dd594a5019c68439
2024-10-31 09:04:40 +00:00

33 lines
1 KiB
TypeScript

import { useEffect, useState } from 'react'
import getMeta from '@/utils/meta'
import { globalLearnedWords } from '@/features/dictionary/ignored-words'
import { HunspellManager } from '@/features/source-editor/hunspell/HunspellManager'
import { debugConsole } from '@/utils/debugging'
export const useHunspell = (spellCheckLanguage: string | null) => {
const [hunspellManager, setHunspellManager] = useState<HunspellManager>()
useEffect(() => {
if (spellCheckLanguage) {
const lang = (getMeta('ol-languages') ?? []).find(
item => item.code === spellCheckLanguage
)
if (lang?.dic) {
const hunspellManager = new HunspellManager(lang.dic, [
...globalLearnedWords,
...getMeta('ol-learnedWords'),
])
setHunspellManager(hunspellManager)
debugConsole.log(spellCheckLanguage, hunspellManager)
return () => {
hunspellManager.destroy()
}
} else {
setHunspellManager(undefined)
}
}
}, [spellCheckLanguage])
return hunspellManager
}