From d093d5cc06669eeca48c5c28afdbedbe552a2bce Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 21 Nov 2013 14:21:06 -0800 Subject: [PATCH 1/4] Check selectors using webkitMatchesSelector --- src/menu-manager.coffee | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/menu-manager.coffee b/src/menu-manager.coffee index 0ae3f26d8..51bc745a3 100644 --- a/src/menu-manager.coffee +++ b/src/menu-manager.coffee @@ -27,11 +27,27 @@ class MenuManager @merge(@template, item) for item in items @update() + # Private: Should the binding for the given selector be included in the menu + # commands. + # + # * selector: A String selector to check. + # + # Returns true to include the selector, false otherwise. + includeSelector: (selector) -> + unless @testEditor? + @testEditor = document.createElement('div') + @testEditor.classList.add('editor') + testBody = document.createElement('body') + testBody.classList.add(document.body.classList.toString().split(' ')...) + testBody.appendChild(@testEditor) + + document.body.webkitMatchesSelector(selector) or @testEditor.webkitMatchesSelector(selector) + # Public: Refreshes the currently visible menu. update: -> keystrokesByCommand = {} - selectors = ['body', ".platform-#{process.platform}", '.editor', '.editor:not(.mini)'] - for binding in atom.keymap.getKeyBindings() when binding.selector in selectors + for binding in atom.keymap.getKeyBindings() when @includeSelector(binding.selector) + console.log binding.selector keystrokesByCommand[binding.command] ?= [] keystrokesByCommand[binding.command].push binding.keystroke @sendToBrowserProcess(@template, keystrokesByCommand) @@ -40,8 +56,8 @@ class MenuManager loadPlatformItems: -> menusDirPath = path.join(@resourcePath, 'menus') platformMenuPath = fs.resolve(menusDirPath, process.platform, ['cson', 'json']) - data = CSON.readFileSync(platformMenuPath) - @add(data.menu) + {menu} = CSON.readFileSync(platformMenuPath) + @add(menu) # Private: Merges an item in a submenu aware way such that new items are always # appended to the bottom of existing menus where possible. From c4dec72dcd8d618ee60ec39e9b047903c9835a7f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 21 Nov 2013 14:32:28 -0800 Subject: [PATCH 2/4] Return early when document.body matches selector --- src/menu-manager.coffee | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/menu-manager.coffee b/src/menu-manager.coffee index 51bc745a3..5b5fd868d 100644 --- a/src/menu-manager.coffee +++ b/src/menu-manager.coffee @@ -34,6 +34,8 @@ class MenuManager # # Returns true to include the selector, false otherwise. includeSelector: (selector) -> + return true if document.body.webkitMatchesSelector(selector) + unless @testEditor? @testEditor = document.createElement('div') @testEditor.classList.add('editor') @@ -41,7 +43,7 @@ class MenuManager testBody.classList.add(document.body.classList.toString().split(' ')...) testBody.appendChild(@testEditor) - document.body.webkitMatchesSelector(selector) or @testEditor.webkitMatchesSelector(selector) + @testEditor.webkitMatchesSelector(selector) # Public: Refreshes the currently visible menu. update: -> From a9887b500733901073e503405d599121f9a2a1f5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 21 Nov 2013 14:32:45 -0800 Subject: [PATCH 3/4] Remove logging --- src/menu-manager.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/src/menu-manager.coffee b/src/menu-manager.coffee index 5b5fd868d..6a918f1ec 100644 --- a/src/menu-manager.coffee +++ b/src/menu-manager.coffee @@ -49,7 +49,6 @@ class MenuManager update: -> keystrokesByCommand = {} for binding in atom.keymap.getKeyBindings() when @includeSelector(binding.selector) - console.log binding.selector keystrokesByCommand[binding.command] ?= [] keystrokesByCommand[binding.command].push binding.keystroke @sendToBrowserProcess(@template, keystrokesByCommand) From 7d9d0c715c5affdf1c34bbe0bb047e8cb1639b9e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 21 Nov 2013 14:33:58 -0800 Subject: [PATCH 4/4] :memo: Mention what testEditor simulates --- src/menu-manager.coffee | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/menu-manager.coffee b/src/menu-manager.coffee index 6a918f1ec..4850e040b 100644 --- a/src/menu-manager.coffee +++ b/src/menu-manager.coffee @@ -36,6 +36,8 @@ class MenuManager includeSelector: (selector) -> return true if document.body.webkitMatchesSelector(selector) + # Simulate an .editor element attached to a body element that has the same + # classes as the current body element. unless @testEditor? @testEditor = document.createElement('div') @testEditor.classList.add('editor')