2020-11-08 03:08:33 +02:00
|
|
|
import { CommandContext, CommandDeclaration, CommandRuntime } from '@joplin/lib/services/CommandService';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import bridge from '../../../services/bridge';
|
|
|
|
import SpellCheckerService from '@joplin/lib/services/spellChecker/SpellCheckerService';
|
2021-09-04 19:11:29 +02:00
|
|
|
import { AppState } from '../../../app.reducer';
|
2020-11-08 03:08:33 +02:00
|
|
|
|
|
|
|
const Menu = bridge().Menu;
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const declaration: CommandDeclaration = {
|
2020-11-08 03:08:33 +02:00
|
|
|
name: 'showSpellCheckerMenu',
|
|
|
|
label: () => _('Spell checker'),
|
|
|
|
iconName: 'fas fa-globe',
|
|
|
|
};
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const runtime = (): CommandRuntime => {
|
2020-11-08 03:08:33 +02:00
|
|
|
return {
|
2022-08-27 13:05:44 +02:00
|
|
|
execute: async (context: CommandContext, selectedLanguages: string[] = null, useSpellChecker: boolean = null) => {
|
|
|
|
selectedLanguages = selectedLanguages === null ? context.state.settings['spellChecker.languages'] : selectedLanguages;
|
2020-11-08 03:08:33 +02:00
|
|
|
useSpellChecker = useSpellChecker === null ? context.state.settings['spellChecker.enabled'] : useSpellChecker;
|
|
|
|
|
2022-08-27 13:05:44 +02:00
|
|
|
const menuItems = SpellCheckerService.instance().spellCheckerConfigMenuItems(selectedLanguages, useSpellChecker);
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-12-28 15:17:59 +02:00
|
|
|
const menu = Menu.buildFromTemplate(menuItems as any);
|
2024-11-08 17:32:05 +02:00
|
|
|
menu.popup({ window: bridge().activeWindow() });
|
2020-11-08 03:08:33 +02:00
|
|
|
},
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
mapStateToTitle(state: AppState): string {
|
2020-11-08 03:08:33 +02:00
|
|
|
if (!state.settings['spellChecker.enabled']) return null;
|
2022-08-27 13:05:44 +02:00
|
|
|
const languages = state.settings['spellChecker.languages'];
|
|
|
|
if (languages.length === 0) return null;
|
|
|
|
const s: string[] = [];
|
2023-06-30 10:39:21 +02:00
|
|
|
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
|
2022-08-27 13:05:44 +02:00
|
|
|
languages.forEach((language: string) => {
|
2024-02-26 12:27:34 +02:00
|
|
|
const onlyLanguage = language.split('-')[0];
|
|
|
|
if (!s.includes(onlyLanguage)) { s.push(onlyLanguage); }
|
2022-08-27 13:05:44 +02:00
|
|
|
});
|
2024-02-26 12:27:34 +02:00
|
|
|
|
2022-08-27 13:05:44 +02:00
|
|
|
return s.join(', ');
|
2020-11-08 03:08:33 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|