From 5759026cf97cc310a24db181eaa5e7dae08549bf Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Thu, 7 Nov 2024 12:01:44 +0800 Subject: [PATCH] feat(electron): spellcheck setting --- .../src/main/windows-manager/tab-views.ts | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/frontend/apps/electron/src/main/windows-manager/tab-views.ts b/packages/frontend/apps/electron/src/main/windows-manager/tab-views.ts index 7c5b02f7ada28..60f56f527111a 100644 --- a/packages/frontend/apps/electron/src/main/windows-manager/tab-views.ts +++ b/packages/frontend/apps/electron/src/main/windows-manager/tab-views.ts @@ -2,6 +2,8 @@ import { join } from 'node:path'; import { app, + Menu, + MenuItem, session, type View, type WebContents, @@ -816,13 +818,42 @@ export class WebContentViewsManager { transparent: true, contextIsolation: true, sandbox: false, - spellcheck: false, // TODO(@pengx17): enable? + spellcheck: true, // TODO(@pengx17): enable? preload: join(__dirname, './preload.js'), // this points to the bundled preload module // serialize exposed meta that to be used in preload additionalArguments: additionalArguments, }, }); + view.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: () => view.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: () => + view.webContents.session.addWordToSpellCheckerDictionary( + params.misspelledWord + ), + }) + ); + } + + menu.popup(); + }); + this.webViewsMap$.next(this.tabViewsMap.set(viewId, view)); let unsub = () => {};