Skip to content

Commit

Permalink
Fixed submenu ordering.
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
Akos Kitta authored and kittaakos committed Aug 14, 2020
1 parent 51e4c70 commit af98a8e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const SampleCommand: Command = {
id: 'sample-command',
label: 'Sample Command'
};
const SampleCommand2: Command = {
id: 'sample-command2',
label: 'Sample Command2'
};

@injectable()
export class SampleCommandContribution implements CommandContribution {
Expand All @@ -30,6 +34,11 @@ export class SampleCommandContribution implements CommandContribution {
alert('This is a sample command!');
}
});
commands.registerCommand(SampleCommand2, {
execute: () => {
alert('This is sample command2!');
}
});
}

}
Expand All @@ -42,7 +51,22 @@ export class SampleMenuContribution implements MenuContribution {
order: '2' // that should put the menu right next to the File menu
});
menus.registerMenuAction(subMenuPath, {
commandId: SampleCommand.id
commandId: SampleCommand.id,
order: '0'
});
menus.registerMenuAction(subMenuPath, {
commandId: SampleCommand2.id,
order: '2'
});
const subSubMenuPath = [...subMenuPath, 'sample-sub-menu'];
menus.registerSubmenu(subSubMenuPath, 'Sample sub menu', { order: '1' });
menus.registerMenuAction(subSubMenuPath, {
commandId: SampleCommand.id,
order: '0'
});
menus.registerMenuAction(subSubMenuPath, {
commandId: SampleCommand2.id,
order: '2'
});
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/common/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ export class MenuModelRegistry {
const index = menuPath.length - 1;
const menuId = menuPath[index];
const groupPath = index === 0 ? [] : menuPath.slice(0, index);
const parent = this.findGroup(groupPath);
let groupNode = this.findSubMenu(parent, menuId);
const parent = this.findGroup(groupPath, options);
let groupNode = this.findSubMenu(parent, menuId, options);
if (!groupNode) {
groupNode = new CompositeMenuNode(menuId, label, options);
return parent.addNode(groupNode);
Expand Down Expand Up @@ -155,23 +155,23 @@ export class MenuModelRegistry {
recurse(this.root);
}

protected findGroup(menuPath: MenuPath): CompositeMenuNode {
protected findGroup(menuPath: MenuPath, options?: SubMenuOptions): CompositeMenuNode {
let currentMenu = this.root;
for (const segment of menuPath) {
currentMenu = this.findSubMenu(currentMenu, segment);
currentMenu = this.findSubMenu(currentMenu, segment, options);
}
return currentMenu;
}

protected findSubMenu(current: CompositeMenuNode, menuId: string): CompositeMenuNode {
protected findSubMenu(current: CompositeMenuNode, menuId: string, options?: SubMenuOptions): CompositeMenuNode {
const sub = current.children.find(e => e.id === menuId);
if (sub instanceof CompositeMenuNode) {
return sub;
}
if (sub) {
throw new Error(`'${menuId}' is not a menu group.`);
}
const newSub = new CompositeMenuNode(menuId);
const newSub = new CompositeMenuNode(menuId, undefined, options);
current.addNode(newSub);
return newSub;
}
Expand Down

0 comments on commit af98a8e

Please sign in to comment.