Add offline spell-checking
All checks were successful
Build on Push and create Release on Tag / build (push) Successful in 4m2s

This commit is contained in:
MassiveBox 2025-10-04 16:22:54 +02:00
parent a13ac05afb
commit 032e7f0b8c
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
11 changed files with 252 additions and 64 deletions

View file

@ -6,6 +6,10 @@ import {SettingUtils} from "@/libs/setting-utils";
import {Analytics} from "@/analytics";
import {SuggestionEngine} from "@/suggestions";
import {Menus} from "@/menus";
import {ESpellChecker} from "@/espells";
import {LanguageTool, LanguageToolSettings} from "@/languagetool";
import {HunspellDictManager} from "@/hunspellDictManager";
import {Language} from "@/spellChecker";
export default class SpellCheckPlugin extends Plugin {
@ -17,6 +21,9 @@ export default class SpellCheckPlugin extends Plugin {
public analytics: Analytics
public i18nx: any; // This object is just a copy of i18n, but with type "any" to not trigger type errors
public offlineSpellChecker: ESpellChecker
public onlineSpellChecker: LanguageTool
public static ENABLED_ATTR = 'custom-spellcheck-enable'
public static LANGUAGE_ATTR = 'custom-spellcheck-language'
@ -24,11 +31,14 @@ export default class SpellCheckPlugin extends Plugin {
this.i18nx = this.i18n
new Icons(this);
this.settingsUtil = await Settings.init(this)
this.analytics = new Analytics(this.settingsUtil.get('analytics'));
this.suggestions = new SuggestionEngine(this)
this.menus = new Menus(this)
await this.prepareSpellCheckers()
void this.analytics.sendEvent('load')
const style = document.createElement('style');
@ -83,7 +93,6 @@ export default class SpellCheckPlugin extends Plugin {
})
this.eventBus.on('open-menu-doctree', async (event) => {
console.log(event)
const docID = ProtyleHelpers.getNodeId(event.detail.elements[0]) // @TODO this is ugly, why does the event not carry the docID?
void this.menus.addSettingsToDocMenu(docID, event.detail.menu)
})
@ -119,4 +128,24 @@ export default class SpellCheckPlugin extends Plugin {
void this.analytics.sendEvent('uninstall');
}
private async prepareSpellCheckers() {
this.onlineSpellChecker = new LanguageTool(<LanguageToolSettings>this.settingsUtil.dump())
const offlineLanguages = this.settingsUtil.get('offlineDicts').split(',')
let langs: {aff: string, dic: string, language: Language}[] = []
try {
for(const lang of offlineLanguages) {
const { aff, dic } = await HunspellDictManager.loadDictionary(lang, true)
langs.push({aff: aff, dic: dic, language: {name: lang, code: lang, longCode: lang}})
}
this.offlineSpellChecker = new ESpellChecker(langs)
}catch (e){
console.error(e)
showMessage(this.i18nx.errors.hunspellLoadError + e, -1, 'error')
}
}
}