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
Unified · Split
| @@ -1114,6 +1114,34 @@ function createWindow(): BrowserWindow { · 1113 unmodified lines | ||
| 1114 | 1114 | }, |
| 1115 | 1115 | }); |
| 1116 | 1116 | |
| 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 | + | |
| 1117 | 1145 | window.webContents.setWindowOpenHandler(() => ({ action: "deny" })); |
| 1118 | 1146 | window.on("page-title-updated", (event) => { |
| 1119 | 1147 | event.preventDefault(); |