提交 1052cb5f 编写于 作者: A Alex Dima

Fixes Microsoft/monaco-editor#175: bring back context menu functionality for `editor.addAction`

上级 90aa6e0f
......@@ -10,7 +10,7 @@ import {Disposable, IDisposable, dispose} from 'vs/base/common/lifecycle';
import {TPromise} from 'vs/base/common/winjs.base';
import {ServicesAccessor, IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
import {IContextKey, IContextKeyServiceTarget, IContextKeyService} from 'vs/platform/contextkey/common/contextkey';
import {ContextKeyExpr, IContextKey, IContextKeyServiceTarget, IContextKeyService} from 'vs/platform/contextkey/common/contextkey';
import {CommonEditorConfiguration} from 'vs/editor/common/config/commonEditorConfig';
import {DefaultConfig} from 'vs/editor/common/config/defaultConfig';
import {Cursor} from 'vs/editor/common/controller/cursor';
......@@ -27,6 +27,8 @@ import {SplitLinesCollection} from 'vs/editor/common/viewModel/splitLinesCollect
import {ViewModel} from 'vs/editor/common/viewModel/viewModelImpl';
import {hash} from 'vs/base/common/hash';
import {EditorModeContext} from 'vs/editor/common/modes/editorModeContext';
import {MenuId, MenuRegistry, IMenuItem} from 'vs/platform/actions/common/actions';
import {CommandsRegistry} from 'vs/platform/commands/common/commands';
import EditorContextKeys = editorCommon.EditorContextKeys;
......@@ -511,7 +513,30 @@ export abstract class CommonCodeEditor extends EventEmitter implements editorCom
) {
throw new Error('Invalid action descriptor, `id`, `label` and `run` are required properties!');
}
// Generate a unique id to allow the same descriptor.id across multiple editor instances
let uniqueId = this.getId() + ':' + descriptor.id;
let action = new DynamicEditorAction(descriptor, this);
// Register the command
CommandsRegistry.registerCommand(uniqueId, () => action.run());
if (descriptor.contextMenuGroupId) {
let menuItem: IMenuItem = {
command: {
id: uniqueId,
title: descriptor.label
},
when: ContextKeyExpr.equals('editorId', this.getId()),
group: descriptor.contextMenuGroupId,
order: descriptor.contextMenuOrder || 0
};
// Register the menu item
MenuRegistry.appendMenuItem(MenuId.EditorContext, menuItem);
}
this._actions[action.id] = action;
}
......
......@@ -3421,6 +3421,20 @@ export interface IActionDescriptor {
* An array of keybindings for the action.
*/
keybindings?: number[];
/**
* Control if the action should show up in the context menu and where.
* The context menu of the editor has these default:
* navigation - The navigation group comes first in all cases.
* 1_modification - This group comes next and contains commands that modify your code.
* 9_cutcopypaste - The last default group with the basic editing commands.
* You can also create your own group.
* Defaults to null (don't show in context menu).
*/
contextMenuGroupId?: string;
/**
* Control the order in the context menu group.
*/
contextMenuOrder?: number;
/**
* The keybinding rule.
*/
......
......@@ -16,7 +16,7 @@ import {ActionItem, Separator} from 'vs/base/browser/ui/actionbar/actionbar';
import {IContextMenuService, IContextViewService} from 'vs/platform/contextview/browser/contextView';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {IContextKeyService} from 'vs/platform/contextkey/common/contextkey';
import {IMenuService, IMenu, MenuId} from 'vs/platform/actions/common/actions';
import {IMenuService, MenuId} from 'vs/platform/actions/common/actions';
import {ICommonCodeEditor, IEditorContribution, MouseTargetType, EditorContextKeys, IScrollEvent} from 'vs/editor/common/editorCommon';
import {editorAction, ServicesAccessor, EditorAction} from 'vs/editor/common/editorCommonExtensions';
import {ICodeEditor, IEditorMouseEvent} from 'vs/editor/browser/editorBrowser';
......@@ -39,7 +39,6 @@ export class ContextMenuController implements IEditorContribution {
private _toDispose: IDisposable[] = [];
private _contextMenuIsBeingShownCount: number = 0;
private _editor: ICodeEditor;
private _contextMenu: IMenu;
constructor(
editor: ICodeEditor,
......@@ -51,9 +50,6 @@ export class ContextMenuController implements IEditorContribution {
) {
this._editor = editor;
this._contextMenu = this._menuService.createMenu(MenuId.EditorContext, this._contextKeyService);
this._toDispose.push(this._contextMenu);
this._toDispose.push(this._editor.onContextMenu((e: IEditorMouseEvent) => this._onContextMenu(e)));
this._toDispose.push(this._editor.onDidScrollChange((e: IScrollEvent) => {
if (this._contextMenuIsBeingShownCount > 0) {
......@@ -129,7 +125,10 @@ export class ContextMenuController implements IEditorContribution {
private _getMenuActions(): IAction[] {
const result: IAction[] = [];
const groups = this._contextMenu.getActions();
let contextMenu = this._menuService.createMenu(MenuId.EditorContext, this._contextKeyService);
const groups = contextMenu.getActions();
contextMenu.dispose();
for (let group of groups) {
const [, actions] = group;
......
......@@ -2776,6 +2776,20 @@ declare module monaco.editor {
* An array of keybindings for the action.
*/
keybindings?: number[];
/**
* Control if the action should show up in the context menu and where.
* The context menu of the editor has these default:
* navigation - The navigation group comes first in all cases.
* 1_modification - This group comes next and contains commands that modify your code.
* 9_cutcopypaste - The last default group with the basic editing commands.
* You can also create your own group.
* Defaults to null (don't show in context menu).
*/
contextMenuGroupId?: string;
/**
* Control the order in the context menu group.
*/
contextMenuOrder?: number;
/**
* The keybinding rule.
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册