Skip to content

Commit 1e6e78e

Browse files
committed
feat(desktop): add spellcheck suggestions to right-click context menu
Wire up Electron's context-menu event on webContents to surface dictionary suggestions for misspelled words. Standard edit actions (Cut, Copy, Paste, Select All) appear in all context menus. Related to #473
1 parent c97c6b7 commit 1e6e78e

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

apps/desktop/src/main.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,34 @@ function createWindow(): BrowserWindow {
11141114
},
11151115
});
11161116

1117+
window.webContents.on("context-menu", (event, params) => {
1118+
event.preventDefault();
1119+
1120+
const menuTemplate: MenuItemConstructorOptions[] = [];
1121+
1122+
if (params.misspelledWord) {
1123+
for (const suggestion of params.dictionarySuggestions.slice(0, 5)) {
1124+
menuTemplate.push({
1125+
label: suggestion,
1126+
click: () => window.webContents.replaceMisspelling(suggestion),
1127+
});
1128+
}
1129+
if (params.dictionarySuggestions.length === 0) {
1130+
menuTemplate.push({ label: "No suggestions", enabled: false });
1131+
}
1132+
menuTemplate.push({ type: "separator" });
1133+
}
1134+
1135+
menuTemplate.push(
1136+
{ role: "cut", enabled: params.editFlags.canCut },
1137+
{ role: "copy", enabled: params.editFlags.canCopy },
1138+
{ role: "paste", enabled: params.editFlags.canPaste },
1139+
{ role: "selectAll", enabled: params.editFlags.canSelectAll },
1140+
);
1141+
1142+
Menu.buildFromTemplate(menuTemplate).popup({ window });
1143+
});
1144+
11171145
window.webContents.setWindowOpenHandler(() => ({ action: "deny" }));
11181146
window.on("page-title-updated", (event) => {
11191147
event.preventDefault();

0 commit comments

Comments
 (0)