1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/services/spellChecker/SpellCheckerService.ts

224 lines
6.4 KiB
TypeScript
Raw Normal View History

2020-11-05 18:58:23 +02:00
import Setting from '../../models/Setting';
import CommandService from '../CommandService';
import SpellCheckerServiceDriverBase from './SpellCheckerServiceDriverBase';
import { _, countryDisplayName } from '../../locale';
import KvStore from '../KvStore';
2020-11-05 18:58:23 +02:00
export default class SpellCheckerService {
private driver_: SpellCheckerServiceDriverBase;
private latestSelectedLanguages_: string[] = [];
2020-11-05 18:58:23 +02:00
private static instance_: SpellCheckerService;
2020-11-05 18:58:23 +02:00
public static instance(): SpellCheckerService {
2020-11-05 18:58:23 +02:00
if (this.instance_) return this.instance_;
this.instance_ = new SpellCheckerService();
return this.instance_;
}
public async initialize(driver: SpellCheckerServiceDriverBase) {
2020-11-05 18:58:23 +02:00
this.driver_ = driver;
this.latestSelectedLanguages_ = await this.loadLatestSelectedLanguages();
2020-11-05 18:58:23 +02:00
this.setupDefaultLanguage();
this.applyStateToDriver();
}
private get defaultLanguage(): string {
2020-11-05 18:58:23 +02:00
return 'en-US';
}
private async loadLatestSelectedLanguages(): Promise<string[]> {
const result = await KvStore.instance().value<string>('spellCheckerService.latestSelectedLanguages');
if (!result) return [];
return JSON.parse(result);
}
private async addLatestSelectedLanguage(language: string) {
// This function will add selected languages to the history. History size will be capped at languagesHistorySizeMax,
// but it can be bigger. Enabled languages will always be in the history, even if it count greater then
// languagesHistorySizeMax, in such case if one of the languages will be disabled it will disappear from history.
const languagesHistorySizeMax = 5;
const languages = this.latestSelectedLanguages_.slice();
if (!languages.includes(language)) {
languages.push(language);
}
if (languages.length > languagesHistorySizeMax) {
2023-06-30 10:39:21 +02:00
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
this.latestSelectedLanguages_.forEach(l => {
if (!this.languages.includes(l) && languages.length > languagesHistorySizeMax) languages.splice(languages.indexOf(l), 1);
});
}
this.latestSelectedLanguages_ = languages;
await KvStore.instance().setValue('spellCheckerService.latestSelectedLanguages', JSON.stringify(this.latestSelectedLanguages_));
}
2020-11-05 18:58:23 +02:00
public setupDefaultLanguage() {
if (Setting.value('spellChecker.languages').length === 0) {
2020-11-05 18:58:23 +02:00
const l = this.driver_.language;
if (this.availableLanguages.includes(l)) {
this.setLanguage(l);
} else {
this.setLanguage(this.defaultLanguage);
}
2020-11-05 18:58:23 +02:00
}
}
public get availableLanguages(): string[] {
2020-11-05 18:58:23 +02:00
return this.driver_.availableLanguages;
}
private applyStateToDriver() {
this.driver_.setLanguages(this.enabled ? this.languages : []);
2020-11-05 18:58:23 +02:00
}
public setLanguage(language: string) {
let enabledLanguages: string[] = [...this.languages];
if (enabledLanguages.includes(language)) {
enabledLanguages = enabledLanguages.filter(obj => obj !== language);
} else {
enabledLanguages.push(language);
}
Setting.setValue('spellChecker.languages', enabledLanguages);
2020-11-05 18:58:23 +02:00
this.applyStateToDriver();
2020-11-25 16:40:25 +02:00
void this.addLatestSelectedLanguage(language);
2020-11-05 18:58:23 +02:00
}
public get languages(): string[] {
return Setting.value('spellChecker.languages');
2020-11-05 18:58:23 +02:00
}
public get enabled(): boolean {
2020-11-05 18:58:23 +02:00
return Setting.value('spellChecker.enabled');
}
public toggleEnabled() {
Setting.toggle('spellChecker.enabled');
this.applyStateToDriver();
}
private async addToDictionary(language: string, word: string) {
2020-11-05 18:58:23 +02:00
this.driver_.addWordToSpellCheckerDictionary(language, word);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public contextMenuItems(misspelledWord: string, dictionarySuggestions: string[]): any[] {
2020-11-05 18:58:23 +02:00
if (!misspelledWord) return [];
const output = [];
output.push({ type: 'separator' });
2020-11-05 18:58:23 +02:00
if (dictionarySuggestions.length) {
for (const suggestion of dictionarySuggestions) {
output.push({
2020-11-05 18:58:23 +02:00
label: suggestion,
click: () => {
void CommandService.instance().execute('replaceMisspelling', suggestion);
2020-11-05 18:58:23 +02:00
},
});
2020-11-05 18:58:23 +02:00
}
} else {
output.push({
2020-11-05 18:58:23 +02:00
label: `(${_('No suggestions')})`,
enabled: false,
click: () => {},
});
2020-11-05 18:58:23 +02:00
}
output.push({ type: 'separator' });
2020-11-05 18:58:23 +02:00
output.push({
2020-11-05 18:58:23 +02:00
label: _('Add to dictionary'),
click: () => {
void this.addToDictionary(this.languages[0], misspelledWord);
2020-11-05 18:58:23 +02:00
},
});
2020-11-05 18:58:23 +02:00
return output;
}
private changeLanguageMenuItem(language: string, enabled: boolean, checked: boolean) {
return {
label: countryDisplayName(language),
type: 'checkbox',
checked: checked,
enabled: enabled,
click: () => {
this.setLanguage(language);
},
};
}
private changeLanguageMenuItems(selectedLanguages: string[], enabled: boolean) {
2020-11-05 18:58:23 +02:00
const languageMenuItems = [];
for (const locale of this.driver_.availableLanguages) {
languageMenuItems.push(this.changeLanguageMenuItem(locale, enabled, selectedLanguages.includes(locale)));
2020-11-05 18:58:23 +02:00
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
languageMenuItems.sort((a: any, b: any) => {
2020-11-05 18:58:23 +02:00
return a.label < b.label ? -1 : +1;
});
return languageMenuItems;
}
public spellCheckerConfigMenuItems(selectedLanguages: string[], useSpellChecker: boolean) {
const latestLanguageItems = this.latestSelectedLanguages_.map((language: string) => {
return this.changeLanguageMenuItem(language, true, selectedLanguages.includes(language));
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
if (latestLanguageItems.length) latestLanguageItems.splice(0, 0, { type: 'separator' } as any);
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
latestLanguageItems.sort((a: any, b: any) => {
return a.label < b.label ? -1 : +1;
});
return [
{
label: _('Use spell checker'),
type: 'checkbox',
checked: useSpellChecker,
click: () => {
this.toggleEnabled();
},
},
...latestLanguageItems,
{
type: 'separator',
},
// Can be removed once it does work
// {
// label: '⚠ Spell checker doesn\'t work in Markdown editor ⚠',
// enabled: false,
// },
{
type: 'separator',
},
{
label: _('Change language'),
submenu: this.changeLanguageMenuItems(selectedLanguages, useSpellChecker),
},
];
2020-11-05 18:58:23 +02:00
}
public spellCheckerConfigMenuItem(selectedLanguages: string[], useSpellChecker: boolean) {
return {
2020-11-05 18:58:23 +02:00
label: _('Spell checker'),
submenu: this.spellCheckerConfigMenuItems(selectedLanguages, useSpellChecker),
};
2020-11-05 18:58:23 +02:00
}
}