diff --git a/spec/context-menu-manager-spec.coffee b/spec/context-menu-manager-spec.coffee index 821cb115d..29d2d8fa9 100644 --- a/spec/context-menu-manager-spec.coffee +++ b/spec/context-menu-manager-spec.coffee @@ -150,3 +150,25 @@ describe "ContextMenuManager", -> shouldDisplay = false expect(contextMenu.templateForEvent(dispatchedEvent)).toEqual [] + + it "allows items to be specified in the legacy format for now", -> + contextMenu.add '.parent': + 'A': 'a' + 'Separator 1': '-' + 'B': + 'C': 'c' + 'Separator 2': '-' + 'D': 'd' + + expect(contextMenu.templateForElement(parent)).toEqual [ + {label: 'A', command: 'a'} + {type: 'separator'} + { + label: 'B' + submenu: [ + {label: 'C', command: 'c'} + {type: 'separator'} + {label: 'D', command: 'd'} + ] + } + ] diff --git a/src/context-menu-manager.coffee b/src/context-menu-manager.coffee index 5514cbb65..2386bfef5 100644 --- a/src/context-menu-manager.coffee +++ b/src/context-menu-manager.coffee @@ -85,7 +85,7 @@ class ContextMenuManager for key, value of itemsBySelector unless _.isArray(value) Grim.deprecate("The format for declaring context menu items has changed. Please consult the documentation.") - itemsBySelector = @convertLegacyItems(itemsBySelector, devMode) + itemsBySelector = @convertLegacyItemsBySelector(itemsBySelector, devMode) addedItemSets = [] @@ -126,22 +126,27 @@ class ContextMenuManager template - convertLegacyItems: (legacyItems, devMode) -> + convertLegacyItemsBySelector: (legacyItemsBySelector, devMode) -> itemsBySelector = {} - for selector, commandsByLabel of legacyItems - itemsBySelector[selector] = items = [] - - for label, commandOrSubmenu of commandsByLabel - if typeof commandOrSubmenu is 'object' - items.push({label, submenu: @convertLegacyItems(commandOrSubmenu, devMode), devMode}) - else if commandOrSubmenu is '-' - items.push({type: 'separator'}) - else - items.push({label, command: commandOrSubmenu, devMode}) + for selector, commandsByLabel of legacyItemsBySelector + itemsBySelector[selector] = @convertLegacyItems(commandsByLabel, devMode) itemsBySelector + convertLegacyItems: (legacyItems, devMode) -> + items = [] + + for label, commandOrSubmenu of legacyItems + if typeof commandOrSubmenu is 'object' + items.push({label, submenu: @convertLegacyItems(commandOrSubmenu, devMode), devMode}) + else if commandOrSubmenu is '-' + items.push({type: 'separator'}) + else + items.push({label, command: commandOrSubmenu, devMode}) + + items + # Public: Request a context menu to be displayed. # # * `event` A DOM event. diff --git a/src/menu-helpers.coffee b/src/menu-helpers.coffee index c47e9ae8b..3341ff16f 100644 --- a/src/menu-helpers.coffee +++ b/src/menu-helpers.coffee @@ -42,7 +42,6 @@ normalizeLabel = (label) -> else label.replace(/\&/g, '') - cloneMenuItem = (item) -> item = _.pick(item, 'type', 'label', 'command', 'submenu', 'commandDetail') if item.submenu?