chore(spellcheck): address requested changes

This commit is contained in:
Elian Doran
2026-04-07 14:26:55 +03:00
parent 21f6cc00eb
commit 22149b94a1

View File

@@ -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}`));
}
}