Skip to content

Build your personal knowledge base with Trilium Notes

chore(spellcheck): address requested changes

Elian Doran22149b94a1cf
diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.ts
index eece558aec..44ab3b2281 100644
--- a/apps/server/src/services/window.ts
+++ b/apps/server/src/services/window.ts
@@ -1,4 +1,4 @@
-import { type App, type BrowserWindow, type BrowserWindowConstructorOptions, default as electron, ipcMain, type IpcMainEvent, type WebContents } from "electron";
+import { type App, type BrowserWindow, type BrowserWindowConstructorOptions, default as electron, ipcMain, type IpcMainEvent, type Session, type WebContents } from "electron";
import fs from "fs/promises";
import { t } from "i18next";
import path from "path";
@@ -19,6 +19,7 @@ import { formatDownloadTitle, isMac, isWindows } from "./utils.js";
let mainWindow: BrowserWindow | null;
let setupWindow: BrowserWindow | null;
let allWindows: BrowserWindow[] = []; // // Used to store all windows, sorted by the order of focus.
+const loadedSpellcheckSessions = new WeakSet<Session>();
function trackWindowFocus(win: BrowserWindow) {
// We need to get the last focused window from allWindows. If the last window is closed, we return the previous window.
@@ -70,6 +71,11 @@ electron.ipcMain.on("create-extra-window", (event, arg) => {
createExtraWindow(arg.extraWindowHash);
});
+electron.ipcMain.on("add-word-to-dictionary", (event, word: string) => {
+ event.sender.session.addWordToSpellCheckerDictionary(word);
+ customDictionary.addWord(word);
+});
+
interface PrintOpts {
notePath: string;
printToPdf: boolean;
@@ -376,18 +382,22 @@ async function configureWebContents(webContents: WebContents, spellcheckEnabled:
});
if (spellcheckEnabled) {
+ setupSpellcheckForSession(webContents.session);
+ }
+}
+
+function setupSpellcheckForSession(session: Session) {
+ if (!loadedSpellcheckSessions.has(session)) {
+ loadedSpellcheckSessions.add(session);
+
const languageCodes = optionService
.getOption("spellCheckLanguageCode")
.split(",")
.map((code) => code.trim());
- webContents.session.setSpellCheckerLanguages(languageCodes);
- customDictionary.loadForSession(webContents.session);
-
- ipcMain.on("add-word-to-dictionary", (_event, word: string) => {
- webContents.session.addWordToSpellCheckerDictionary(word);
- customDictionary.addWord(word);
- });
+ session.setSpellCheckerLanguages(languageCodes);
+ customDictionary.loadForSession(session)
+ .catch(err => log.error(`Failed to load custom dictionary for spellcheck: ${err}`));
}
}