A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
desktop: add setting to choose spell checker languages
Abdullah Attaa67599ce8308
diff --git a/apps/web/desktop/electron.js b/apps/web/desktop/electron.jsindex 251fbc60d..537c1096a 100644--- a/apps/web/desktop/electron.js+++ b/apps/web/desktop/electron.js@@ -79,7 +79,7 @@ async function createWindow() {mainWindow.setAutoHideMenuBar(true);mainWindowState.manage(mainWindow);globalThis.window = mainWindow;- setupMenu(mainWindow);+ setupMenu();if (isDevelopment())mainWindow.webContents.openDevTools({ mode: "right", activate: true });diff --git a/apps/web/desktop/ipc/actions/index.js b/apps/web/desktop/ipc/actions/index.jsindex 5544f3e1c..40bb0d4a4 100644--- a/apps/web/desktop/ipc/actions/index.js+++ b/apps/web/desktop/ipc/actions/index.js@@ -27,6 +27,7 @@ import setZoomFactor from "./setZoomFactor";import setPrivacyMode from "./setPrivacyMode";import showNotification from "./showNotification";import bringToFront from "./bringToFront";+import setSpellCheckerLanguages from "./setSpellCheckerLanguages";import toggleSpellChecker from "./toggleSpellChecker";const actions = {@@ -40,6 +41,7 @@ const actions = {setPrivacyMode,showNotification,bringToFront,+ setSpellCheckerLanguages,toggleSpellChecker};diff --git a/apps/web/desktop/ipc/actions/setSpellCheckerLanguages.js b/apps/web/desktop/ipc/actions/setSpellCheckerLanguages.jsnew file mode 100644index 000000000..281eb1d98--- /dev/null+++ b/apps/web/desktop/ipc/actions/setSpellCheckerLanguages.js@@ -0,0 +1,25 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++export default (args) => {+ if (!globalThis.window) return;+ const { languages } = args;++ globalThis.window.webContents.session.setSpellCheckerLanguages(languages);+};diff --git a/apps/web/desktop/ipc/calls/getSpellChecker.js b/apps/web/desktop/ipc/calls/getSpellChecker.jsindex 23ad2c526..d1d6cb046 100644--- a/apps/web/desktop/ipc/calls/getSpellChecker.js+++ b/apps/web/desktop/ipc/calls/getSpellChecker.js@@ -19,6 +19,64 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { getIsSpellCheckerEnabled } from "../../config/spellChecker";+const LANGUAGES = {+ af: "Afrikaans",+ bg: "Bulgarian",+ ca: "Catalan",+ cs: "Czech",+ cy: "Welsh",+ da: "Danish",+ de: "German",+ "de-DE": "German (Germany)",+ el: "Greek",+ en: "English",+ "en-AU": "English (Australia)",+ "en-CA": "English (Canada)",+ "en-GB": "English (UK)",+ "en-GB-oxendict": "English (UK Oxford)",+ "en-US": "English (US)",+ es: "Spanish",+ "es-419": "Spanish (Latin America)",+ "es-AR": "Spanish (Argentina)",+ "es-ES": "Spanish (Spain)",+ "es-MX": "Spanish (Mexico)",+ "es-US": "Spanish (US)",+ et: "Estonian",+ fa: "Persian",+ fo: "Faroese",+ fr: "French",+ "fr-FR": "French (France)",+ he: "Hebrew",+ hi: "Hindi",+ hr: "Croatian",+ hu: "Hungarian",+ hy: "Armenian",+ id: "Indonesian",+ it: "Italian",+ "it-IT": "Italian (Italy)",+ ko: "Korean",+ lt: "Lithuanian",+ lv: "Latvian",+ nb: "Norwegian Bokmål",+ nl: "Dutch",+ pl: "Polish",+ pt: "Portuguese",+ "pt-BR": "Portuguese (Brazil)",+ "pt-PT": "Portuguese (Portugal)",+ ro: "Romanian",+ ru: "Russian",+ sh: "Serbo-Croatian",+ sk: "Slovak",+ sl: "Slovenian",+ sq: "Albanian",+ sr: "Serbian",+ sv: "Swedish",+ ta: "Tamil",+ tg: "Tajik",+ tr: "Turkish",+ uk: "Ukrainian",+ vi: "Vietnamese"+};/***@@ -31,5 +89,17 @@ export default function (_args, win) {return {enabled: getIsSpellCheckerEnabled(),+ languages: win.webContents.session.availableSpellCheckerLanguages.map(+ (code) => ({+ code,+ name: LANGUAGES[code]+ })+ ),+ enabledLanguages: win.webContents.session+ .getSpellCheckerLanguages()+ .map((code) => ({+ code,+ name: LANGUAGES[code]+ }))};}diff --git a/apps/web/desktop/menu.js b/apps/web/desktop/menu.jsindex 39c560b6e..53291b962 100644--- a/apps/web/desktop/menu.js+++ b/apps/web/desktop/menu.js@@ -19,13 +19,41 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { Menu, MenuItem, clipboard } from "electron";-/**- *- * @param {import("@playwright/test").BrowserWindow} mainWindow- */-function setupMenu(mainWindow) {- mainWindow.webContents.on("context-menu", (event, params) => {+function setupMenu() {+ globalThis.window.webContents.on("context-menu", (_event, params) => {const menu = new Menu();++ // Add each spelling suggestion+ for (const suggestion of params.dictionarySuggestions) {+ menu.append(+ new MenuItem({+ label: suggestion,+ click: () =>+ globalThis.window.webContents.replaceMisspelling(suggestion)+ })+ );+ }++ // Allow users to add the misspelled word to the dictionary+ if (params.misspelledWord) {+ menu.append(+ new MenuItem({+ label: "Add to dictionary",+ click: () =>+ globalThis.window.webContents.session.addWordToSpellCheckerDictionary(+ params.misspelledWord+ )+ })+ );+ }++ if (menu.items.length > 0)+ menu.append(+ new MenuItem({+ type: "separator"+ })+ );+if (params.isEditable) {menu.append(new MenuItem({@@ -81,28 +109,6 @@ function setupMenu(mainWindow) {}));- // Add each spelling suggestion- // for (const suggestion of params.dictionarySuggestions) {- // menu.append(- // new MenuItem({- // label: suggestion,- // click: () => mainWindow.webContents.replaceMisspelling(suggestion),- // })- // );- // }-- // Allow users to add the misspelled word to the dictionary- // if (params.misspelledWord) {- // menu.append(- // new MenuItem({- // label: "Add to dictionary",- // click: () =>- // mainWindow.webContents.session.addWordToSpellCheckerDictionary(- // params.misspelledWord- // ),- // })- // );- // }menu.popup();});}diff --git a/apps/web/desktop/tsconfig.json b/apps/web/desktop/tsconfig.jsonindex 9a18f3665..0f71e5e0d 100644--- a/apps/web/desktop/tsconfig.json+++ b/apps/web/desktop/tsconfig.json@@ -1,7 +1,7 @@{"compilerOptions": {"target": "ES6",- "lib": ["dom", "dom.iterable", "esnext"],+ "lib": ["esnext"],"allowJs": true,"skipLibCheck": true,"esModuleInterop": true,diff --git a/apps/web/src/commands/set-spell-checker-languages.js b/apps/web/src/commands/set-spell-checker-languages.jsnew file mode 100644index 000000000..8717c5cea--- /dev/null+++ b/apps/web/src/commands/set-spell-checker-languages.js@@ -0,0 +1,24 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++import { invokeCommand } from "./index";++export default function setSpellCheckerLanguages(languages) {+ invokeCommand("setSpellCheckerLanguages", { languages });+}diff --git a/apps/web/src/common/dialog-controller.tsx b/apps/web/src/common/dialog-controller.tsxindex 087a3d729..a8c863167 100644--- a/apps/web/src/common/dialog-controller.tsx+++ b/apps/web/src/common/dialog-controller.tsx@@ -199,6 +199,13 @@ export function showEmailChangeDialog() {);}+export function showLanguageSelectorDialog() {+ return showDialog<"LanguageSelectorDialog", string | null>(+ "LanguageSelectorDialog",+ (Dialog, perform) => <Dialog onClose={() => perform(null)} />+ );+}+export function showToolbarConfigDialog() {return showDialog<"ToolbarConfigDialog", string | null>("ToolbarConfigDialog",diff --git a/apps/web/src/components/dialogs/index.ts b/apps/web/src/components/dialogs/index.tsindex 750b9b418..f9f5cb3b7 100644--- a/apps/web/src/components/dialogs/index.ts+++ b/apps/web/src/components/dialogs/index.ts@@ -54,6 +54,9 @@ const Prompt = React.lazy(() => import("./prompt"));const ToolbarConfigDialog = React.lazy(() => import("./toolbar-config-dialog"));const MigrationDialog = React.lazy(() => import("./migration-dialog"));const EmailChangeDialog = React.lazy(() => import("./email-change-dialog"));+const LanguageSelectorDialog = React.lazy(+ () => import("./language-selector-dialog")+);export const Dialogs = {AddNotebookDialog,@@ -81,5 +84,6 @@ export const Dialogs = {MigrationDialog,AddReminderDialog,ReminderPreviewDialog,- EmailChangeDialog+ EmailChangeDialog,+ LanguageSelectorDialog};diff --git a/apps/web/src/components/dialogs/language-selector-dialog.tsx b/apps/web/src/components/dialogs/language-selector-dialog.tsxnew file mode 100644index 000000000..772b51efb--- /dev/null+++ b/apps/web/src/components/dialogs/language-selector-dialog.tsx@@ -0,0 +1,100 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++import { deleteItem } from "@notesnook/core/utils/array";+import { Flex, Input, Label } from "@theme-ui/components";+import { useCallback, useEffect, useState } from "react";+import { Perform } from "../../common/dialog-controller";+import useSpellChecker from "../../hooks/use-spell-checker";+import Dialog from "./dialog";++export type LanguageSelectorDialogProps = {+ onClose: Perform;+};++export default function LanguageSelectorDialog(+ props: LanguageSelectorDialogProps+) {+ const spellChecker = useSpellChecker();+ const [enabledLanguages, setEnabledLanguages] = useState<string[]>([]);+ const [languages, setLanguages] = useState<Language[]>([]);++ useEffect(() => {+ if (!spellChecker.enabledLanguages || !spellChecker.languages) return;++ setEnabledLanguages(spellChecker.enabledLanguages.map((a) => a.code));+ setLanguages(spellChecker.languages.slice());+ }, [spellChecker.enabledLanguages, spellChecker.languages]);++ const filter = useCallback(+ async (query) => {+ if (!spellChecker.languages) return;+ setLanguages(+ spellChecker.languages.filter(+ (a) =>+ a.name.toLowerCase().includes(query) ||+ a.code.toLowerCase().includes(query)+ )+ );+ },+ [spellChecker]+ );++ return (+ <Dialog+ isOpen={true}+ title={"Spellchecker languages"}+ description={+ "Choose the languages you want to spell check while editing."+ }+ onClose={props.onClose}+ positiveButton={{+ text: "Done",+ onClick: props.onClose+ }}+ negativeButton={{ text: "Cancel", onClick: props.onClose }}+ >+ <Input+ placeholder="Filter languages"+ sx={{ mt: 1, mb: 2 }}+ onChange={(e) => filter(e.currentTarget.value)}+ />+ <Flex style={{ maxHeight: 200, flexDirection: "column" }}>+ {languages.map((lang) => (+ <Label key={lang.code}>+ <input+ type="checkbox"+ checked={enabledLanguages.includes(lang.code)}+ onChange={async (e) => {+ const { checked } = e.currentTarget;+ const copiedLanguages = enabledLanguages.slice();++ if (checked) copiedLanguages.push(lang.code);+ else deleteItem(copiedLanguages, lang.code);++ await spellChecker.setLanguages(copiedLanguages);+ }}+ />+ {lang.name}+ </Label>+ ))}+ </Flex>+ </Dialog>+ );+}diff --git a/apps/web/src/global.d.ts b/apps/web/src/global.d.tsindex 580672864..b76744c37 100644--- a/apps/web/src/global.d.ts+++ b/apps/web/src/global.d.ts@@ -17,7 +17,10 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/+type Language = { code: string; name: string };type SpellCheckerOptions = {+ languages: Language[];+ enabledLanguages: Language[];enabled: boolean;};declare interface Window {diff --git a/apps/web/src/hooks/use-spell-checker.ts b/apps/web/src/hooks/use-spell-checker.tsindex ad2c6adfe..88241e6ad 100644--- a/apps/web/src/hooks/use-spell-checker.ts+++ b/apps/web/src/hooks/use-spell-checker.ts@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.*/import { useCallback, useEffect, useState } from "react";+import setSpellCheckerLanguages from "../commands/set-spell-checker-languages";import toggleSpellChecker from "../commands/toggle-spell-checker";export default function useSpellChecker() {@@ -44,10 +45,18 @@ export default function useSpellChecker() {[loadSpellChecker]);+ const setLanguages = useCallback(+ async (languages: string[]) => {+ setSpellCheckerLanguages(languages);+ await loadSpellChecker();+ },+ [loadSpellChecker]+ );return {...spellChecker,toggle,+ setLanguages,loadSpellChecker};}diff --git a/apps/web/src/views/settings.js b/apps/web/src/views/settings.jsindex 23208bd5f..98e32f7ed 100644--- a/apps/web/src/views/settings.js+++ b/apps/web/src/views/settings.js@@ -42,7 +42,8 @@ import {show2FARecoveryCodesDialog,showToolbarConfigDialog,showPromptDialog,- showEmailChangeDialog+ showEmailChangeDialog,+ showLanguageSelectorDialog} from "../common/dialog-controller";import { SUBSCRIPTION_STATUS } from "../common/constants";import { createBackup, importBackup, verifyAccount } from "../common";@@ -572,6 +573,20 @@ function Settings() {onToggled={() => spellChecker.toggle(!spellChecker.enabled)}isToggled={spellChecker.enabled}/>+ {isDesktop() && getPlatform() !== "darwin" && (+ <Button+ variant="list"+ onClick={async () => {+ await showLanguageSelectorDialog();+ }}+ >+ <Tip+ text="Spellchecker languages"+ tip="Choose languages for the spellchecker"+ sx={{ py: 2 }}+ />+ </Button>+ )}</>)}