Skip to content

Commit

Permalink
GH-22: Initial approach to register Monaco commands to the application.
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <kittaakos@gmail.com>
  • Loading branch information
kittaakos committed Apr 5, 2017
1 parent 6b6d989 commit 0bb0986
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
23 changes: 22 additions & 1 deletion src/editor/browser/editor-contextmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,28 @@ export class BrowserContextMenuService implements EditorContextMenuService {
console.log(ids.length);

const menuItems = MenuRegistry.getMenuItems(MenuId.EditorContext);
console.log(JSON.stringify(menuItems));
menuItems.forEach(item => {
if (!item.command) {
console.log(`Menu item does not have a corresponding command: ${JSON.stringify(item)}.`);
} else {
const menuCommand = item.command;
console.log(`Processing menu item with command: ${menuCommand.id}.`);
const command = CommandsRegistry.getCommand(menuCommand.id);
if (!command) {
console.log(`Cannot find command in registry with ID: ${menuCommand.id}.`);
} else {
const handler = command.handler;
if (!handler) {
console.log(`Command '${menuCommand.id}' does not have a handler.`)
} else {
const desc = command.description;
console.log(`Menu item: ${menuCommand.id} with handler: ${handler} with when: ${item.when}.`);
console.log(`Command description was: ${JSON.stringify(desc)}.`);
}
}
}
console.log();
})
}

}
54 changes: 46 additions & 8 deletions src/editor/browser/editor-module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IOpenerService, TheiaPlugin } from '../../application/browser';
import { SelectionService } from '../../application/common/selection-service';
import { CommandContribution, CommandRegistry } from '../../application/common/command';
import { CommandContribution, CommandRegistry, CommandHandler } from '../../application/common/command';
import { CommonCommands } from '../../application/common/commands-common';
import { MenuContribution, MenuModelRegistry } from '../../application/common/menu';
import { EditorCommandHandler } from './editor-command';
Expand All @@ -10,46 +10,79 @@ import { EditorService } from './editor-service';
import { TextModelResolverService } from './model-resolver-service';
import { ContainerModule, inject, injectable } from 'inversify';
import { BrowserContextMenuService, EditorContextMenuService } from './editor-contextmenu';
import CommandsRegistry = monaco.commands.CommandsRegistry;
import MenuRegistry = monaco.actions.MenuRegistry;
import MenuId = monaco.actions.MenuId;
import ICommand = monaco.commands.ICommand;
import IMenuItem = monaco.actions.IMenuItem;

export const EDITOR_CONTEXT = 'editor_context_menu';

@injectable()
export class EditorCommandHandlers implements CommandContribution {
constructor(@inject(IEditorManager) private editorService: IEditorManager,
@inject(SelectionService) private selectionService: SelectionService) {}

constructor( @inject(IEditorManager) private editorService: IEditorManager,
@inject(SelectionService) private selectionService: SelectionService) { }

contribute(registry: CommandRegistry) {
registry.registerHandler(
CommonCommands.EDIT_CUT,
new EditorCommandHandler(this.editorService, this.selectionService, {
this.newHandler({
id: CommonCommands.EDIT_CUT,
actionId: 'editor.action.clipboardCutAction'
}));
registry.registerHandler(
CommonCommands.EDIT_COPY,
new EditorCommandHandler(this.editorService, this.selectionService, {
this.newHandler({
id: CommonCommands.EDIT_COPY,
actionId: 'editor.action.clipboardCopyAction'
}));
registry.registerHandler(
CommonCommands.EDIT_PASTE,
new EditorCommandHandler(this.editorService, this.selectionService, {
this.newHandler({
id: CommonCommands.EDIT_PASTE,
actionId: 'editor.action.clipboardPasteAction'
}));
registry.registerHandler(
CommonCommands.EDIT_UNDO,
new EditorCommandHandler(this.editorService, this.selectionService, {
this.newHandler({
id: CommonCommands.EDIT_UNDO,
actionId: 'undo'
}));
registry.registerHandler(
CommonCommands.EDIT_REDO,
new EditorCommandHandler(this.editorService, this.selectionService, {
this.newHandler({
id: CommonCommands.EDIT_REDO,
actionId: 'redo'
}));

MenuRegistry.getMenuItems(MenuId.EditorContext).map(item => item.command).forEach(command => {
registry.registerCommand({
id: command.id,
label: command.title,
iconClass: command.iconClass
});
});

const findCommand: (item: IMenuItem) => ICommand = (item) => CommandsRegistry.getCommand(item.command.id);
const wrap: (item: IMenuItem, command: ICommand) => { item: IMenuItem, command: ICommand } = (item, command) => {
return { item, command }
}

MenuRegistry.getMenuItems(MenuId.EditorContext).map(item => wrap(item, findCommand(item))).forEach(props => {
const id = props.item.command.id;
this.newHandler({
id,
actionId: id
})
});

}

private newHandler(options: EditorCommandHandler.Options): CommandHandler {
return new EditorCommandHandler(this.editorService, this.selectionService, options);
}

}

@injectable()
Expand All @@ -71,6 +104,11 @@ export class EditorMenuContribution implements MenuContribution {
registry.registerMenuAction([EDITOR_CONTEXT, "2_copy"], {
commandId: CommonCommands.EDIT_PASTE
});

MenuRegistry.getMenuItems(MenuId.EditorContext)
.map(item => [(item.group || ""), item.command.id])
.forEach(props => registry.registerMenuAction([EDITOR_CONTEXT, props[0]], { commandId: props[1] }));

}
}

Expand Down
26 changes: 14 additions & 12 deletions typings/monaco/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ declare module monaco.commands {

export interface ICommand {
handler: ICommandHandler;
// TODO as described below, it is undefined in our case.
description?: ICommandHandlerDescription;
}

// TODO shalll we get rid of this. Currently non of the commands have a handler.
export interface ICommandHandlerDescription {
description: string;
args: { name: string; description?: string; constraint?: string | Function; }[];
Expand Down Expand Up @@ -158,35 +160,35 @@ declare module monaco.commands {
declare module monaco.actions {

export class MenuId {

/**
* The unique ID of the editor's context menu.
*/
public static readonly EditorContext: MenuId;
}

export interface ILocalizedString {
value: string;
original: string;
}

export interface ICommandAction {
id: string;
title: string | ILocalizedString;
category?: string | ILocalizedString;
title: string
category?: string;
iconClass?: string;
}

export interface IMenuItem {
command: ICommandAction;
alt?: ICommandAction;
when?: any;
group?: 'navigation' | string;
order?: number;
}

export interface IMenuRegistry {
getCommand(id: string): ICommandAction;
getMenuItems(loc: MenuId): IMenuItem[];
/**
* Retrieves all the registered menu items for the given menu.
*/
getMenuItems(menuId: MenuId | { id: string }): IMenuItem[];
}

/**
* The shared menu registry singleton.
*/
export const MenuRegistry: IMenuRegistry;

}

0 comments on commit 0bb0986

Please sign in to comment.