import { Menu, type WebContents, type MenuItemConstructorOptions } from 'electron' /** * Give editable fields (and any selected text) the standard Windows Cut / Copy / * Paste / Select All right-click menu that Electron does not provide by default * (W4) — plus spellcheck suggestions for text areas. The menu items use built-in * roles, so the editing works directly in the sandboxed, context-isolated * renderer without any extra IPC. Wired onto the main window only; the untrusted * cookie sign-in window deliberately keeps its minimal chrome. */ export function attachEditContextMenu(wc: WebContents): void { wc.on('context-menu', (_e, params) => { const { isEditable, editFlags, selectionText, dictionarySuggestions } = params // Only worth a menu over an editable field or a real text selection. if (!isEditable && !selectionText.trim()) return const template: MenuItemConstructorOptions[] = [] // Spellcheck suggestions first, when the caret sits on a misspelling. if (isEditable && dictionarySuggestions.length > 0) { for (const suggestion of dictionarySuggestions) { template.push({ label: suggestion, click: () => wc.replaceMisspelling(suggestion) }) } template.push({ type: 'separator' }) } if (isEditable) { template.push( { role: 'cut', enabled: editFlags.canCut }, { role: 'copy', enabled: editFlags.canCopy }, { role: 'paste', enabled: editFlags.canPaste }, { type: 'separator' }, { role: 'selectAll', enabled: editFlags.canSelectAll } ) } else { // Read-only text with a selection: offer Copy (and Select All). template.push( { role: 'copy', enabled: editFlags.canCopy }, { role: 'selectAll', enabled: editFlags.canSelectAll } ) } Menu.buildFromTemplate(template).popup() }) }