2020-11-05 18:58:23 +02:00
|
|
|
// Provides spell checking feature via the native Electron built-in spell checker
|
|
|
|
|
2020-11-07 17:59:37 +02:00
|
|
|
import SpellCheckerServiceDriverBase from '@joplin/lib/services/spellChecker/SpellCheckerServiceDriverBase';
|
2020-11-05 18:58:23 +02:00
|
|
|
import bridge from '../bridge';
|
2020-11-27 13:12:28 +02:00
|
|
|
import { languageCodeOnly, localesFromLanguageCode } from '@joplin/lib/locale';
|
|
|
|
import Logger from '@joplin/lib/Logger';
|
|
|
|
|
|
|
|
const logger = Logger.create('SpellCheckerServiceDriverNative');
|
2020-11-05 18:58:23 +02:00
|
|
|
|
|
|
|
export default class SpellCheckerServiceDriverNative extends SpellCheckerServiceDriverBase {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private session(): any {
|
2020-11-05 18:58:23 +02:00
|
|
|
return bridge().window().webContents.session;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public get availableLanguages(): string[] {
|
2020-11-05 18:58:23 +02:00
|
|
|
return this.session().availableSpellCheckerLanguages;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Language can be set to '' to disable spell-checking
|
2020-11-12 21:13:28 +02:00
|
|
|
public setLanguage(v: string) {
|
2020-11-05 18:58:23 +02:00
|
|
|
// If we pass an empty array, it disables spell checking
|
|
|
|
// https://github.com/electron/electron/issues/25228
|
2020-11-27 13:12:28 +02:00
|
|
|
if (!v) {
|
|
|
|
this.session().setSpellCheckerLanguages('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The below function will throw an error if the provided language is
|
|
|
|
// not supported, so we provide fallbacks.
|
|
|
|
// https://github.com/laurent22/joplin/issues/4146
|
|
|
|
const languagesToTry = [
|
|
|
|
v,
|
|
|
|
languageCodeOnly(v),
|
|
|
|
].concat(localesFromLanguageCode(languageCodeOnly(v), this.availableLanguages));
|
|
|
|
|
|
|
|
for (const toTry of languagesToTry) {
|
|
|
|
try {
|
|
|
|
this.session().setSpellCheckerLanguages([toTry]);
|
2020-11-27 14:03:32 +02:00
|
|
|
logger.info(`Set effective language from "${v}" to "${toTry}"`);
|
2020-11-27 13:12:28 +02:00
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn(`Failed to set language to "${toTry}". Will try the next one in this list: ${JSON.stringify(languagesToTry)}`);
|
|
|
|
logger.warn('Error was:', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.error(`Could not set language to: ${v}`);
|
2020-11-05 18:58:23 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public get language(): string {
|
2020-11-05 18:58:23 +02:00
|
|
|
const languages = this.session().getSpellCheckerLanguages();
|
|
|
|
return languages.length ? languages[0] : '';
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public addWordToSpellCheckerDictionary(_language: string, word: string) {
|
2020-11-05 18:58:23 +02:00
|
|
|
// Actually on Electron all languages share the same dictionary, or
|
|
|
|
// perhaps it's added to the currently active language.
|
|
|
|
this.session().addWordToSpellCheckerDictionary(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|