mirror of
https://github.com/atom/atom.git
synced 2026-02-10 14:45:11 -05:00
Add Keymap.bindingsForElement method
This commit is contained in:
@@ -8,6 +8,7 @@ PEG = require 'pegjs'
|
||||
module.exports =
|
||||
class BindingSet
|
||||
selector: null
|
||||
keystrokeMap: null
|
||||
commandForEvent: null
|
||||
keystrokePatternParser: null
|
||||
|
||||
@@ -15,6 +16,7 @@ class BindingSet
|
||||
@parser = PEG.buildParser(fs.read(require.resolve 'keystroke-pattern.pegjs'))
|
||||
@specificity = Specificity(@selector)
|
||||
@commandForEvent = @buildEventHandler(mapOrFunction)
|
||||
@keystrokeMap = if not _.isFunction(mapOrFunction) then mapOrFunction else {}
|
||||
|
||||
buildEventHandler: (mapOrFunction) ->
|
||||
if _.isFunction(mapOrFunction)
|
||||
|
||||
@@ -6,7 +6,7 @@ $ = require 'jquery'
|
||||
|
||||
module.exports =
|
||||
class Keymap
|
||||
bindingSetsBySelector: null
|
||||
bindingSets: null
|
||||
|
||||
constructor: ->
|
||||
@bindingSets = []
|
||||
@@ -26,10 +26,22 @@ class Keymap
|
||||
bindKeys: (selector, bindings) ->
|
||||
@bindingSets.unshift(new BindingSet(selector, bindings))
|
||||
|
||||
bindKey: (selector, pattern, eventName) ->
|
||||
bindings = {}
|
||||
bindings[pattern] = eventName
|
||||
@bindKeys(selector, bindings)
|
||||
bindingsForElement: (element) ->
|
||||
currentNode = $(element)
|
||||
keystrokeMap = {}
|
||||
|
||||
while currentNode.length
|
||||
bindingSets = @bindingSets.filter (set) -> currentNode.is(set.selector)
|
||||
console.log @bindingSets, currentNode
|
||||
|
||||
bindingSets.sort (a, b) -> b.specificity - a.specificity
|
||||
for bindingSet in bindingSets
|
||||
for keystroke, command of bindingSet.keystrokeMap
|
||||
keystrokeMap[keystroke] ?= command
|
||||
|
||||
currentNode = currentNode.parent()
|
||||
|
||||
keystrokeMap
|
||||
|
||||
handleKeyEvent: (event) ->
|
||||
event.keystroke = @keystrokeStringForEvent(event)
|
||||
|
||||
Reference in New Issue
Block a user