Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-7767: Added context menu for input and textArea #7943

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <HTMLElement>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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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();
});