diff --git a/packages/core/src/electron-browser/menu/electron-context-menu-renderer.ts b/packages/core/src/electron-browser/menu/electron-context-menu-renderer.ts index b93ae2e67b887..31266324c89cc 100644 --- a/packages/core/src/electron-browser/menu/electron-context-menu-renderer.ts +++ b/packages/core/src/electron-browser/menu/electron-context-menu-renderer.ts @@ -18,20 +18,60 @@ import * as electron from 'electron'; import { inject, injectable } from 'inversify'; -import { ContextMenuRenderer, RenderContextMenuOptions, ContextMenuAccess } from '../../browser'; +import { ContextMenuRenderer, RenderContextMenuOptions, ContextMenuAccess, FrontendApplicationContribution, CommonCommands } from '../../browser'; import { ElectronMainMenuFactory } from './electron-main-menu-factory'; import { ContextMenuContext } from '../../browser/menu/context-menu-context'; +import { MenuPath, MenuContribution, MenuModelRegistry } from '../../common'; export class ElectronContextMenuAccess extends ContextMenuAccess { - constructor( - public readonly menu: electron.Menu - ) { + constructor(readonly menu: electron.Menu) { super({ dispose: () => menu.closePopup() }); } } +export namespace ElectronTextInputContextMenu { + export const MENU_PATH: MenuPath = ['electron_text_input']; + export const UNDO_REDO_EDIT_GROUP = [...MENU_PATH, '0_undo_redo_group']; + export const EDIT_GROUP = [...MENU_PATH, '1_edit_group']; + export const SELECT_GROUP = [...MENU_PATH, '2_select_group']; +} + +@injectable() +export class ElectronTextInputContextMenuContribution implements FrontendApplicationContribution, MenuContribution { + + @inject(ContextMenuRenderer) + protected readonly contextMenuRenderer: ContextMenuRenderer; + + onStart(): void { + window.document.addEventListener('contextmenu', event => { + if (event.target instanceof HTMLElement) { + const target = event.target; + if (target.nodeName && (target.nodeName.toLowerCase() === 'input' || target.nodeName.toLowerCase() === 'textarea')) { + event.preventDefault(); + event.stopPropagation(); + this.contextMenuRenderer.render({ + anchor: event, + menuPath: ElectronTextInputContextMenu.MENU_PATH, + onHide: () => target.focus() + }); + } + } + }); + } + + registerMenus(registry: MenuModelRegistry): void { + registry.registerMenuAction(ElectronTextInputContextMenu.UNDO_REDO_EDIT_GROUP, { commandId: CommonCommands.UNDO.id }); + registry.registerMenuAction(ElectronTextInputContextMenu.UNDO_REDO_EDIT_GROUP, { commandId: CommonCommands.REDO.id }); + registry.registerMenuAction(ElectronTextInputContextMenu.EDIT_GROUP, { commandId: CommonCommands.CUT.id }); + registry.registerMenuAction(ElectronTextInputContextMenu.EDIT_GROUP, { commandId: CommonCommands.COPY.id }); + registry.registerMenuAction(ElectronTextInputContextMenu.EDIT_GROUP, { commandId: CommonCommands.PASTE.id }); + registry.registerMenuAction(ElectronTextInputContextMenu.SELECT_GROUP, { commandId: CommonCommands.SELECT_ALL.id }); + } + +} + @injectable() export class ElectronContextMenuRenderer extends ContextMenuRenderer { diff --git a/packages/core/src/electron-browser/menu/electron-menu-module.ts b/packages/core/src/electron-browser/menu/electron-menu-module.ts index d610e469273db..4d30fc02752a0 100644 --- a/packages/core/src/electron-browser/menu/electron-menu-module.ts +++ b/packages/core/src/electron-browser/menu/electron-menu-module.ts @@ -18,7 +18,7 @@ import { ContainerModule } from 'inversify'; import { CommandContribution, MenuContribution } from '../../common'; import { FrontendApplicationContribution, ContextMenuRenderer, KeybindingContribution, KeybindingContext } from '../../browser'; import { ElectronMainMenuFactory } from './electron-main-menu-factory'; -import { ElectronContextMenuRenderer } from './electron-context-menu-renderer'; +import { ElectronContextMenuRenderer, ElectronTextInputContextMenuContribution } from './electron-context-menu-renderer'; import { ElectronMenuContribution } from './electron-menu-contribution'; export default new ContainerModule(bind => { @@ -33,4 +33,6 @@ export default new ContainerModule(bind => { for (const serviceIdentifier of [FrontendApplicationContribution, KeybindingContribution, CommandContribution, MenuContribution]) { bind(serviceIdentifier).toService(ElectronMenuContribution); } + bind(FrontendApplicationContribution).to(ElectronTextInputContextMenuContribution).inSingletonScope(); + bind(MenuContribution).to(ElectronTextInputContextMenuContribution).inSingletonScope(); });