mirror of
https://github.com/atom/atom.git
synced 2026-02-15 09:05:58 -05:00
Add ability to add/remove keymaps by name
This commit is contained in:
committed by
Nathan Sobo
parent
772787121c
commit
5410e9368f
@@ -14,8 +14,9 @@ class BindingSet
|
||||
commandsByKeystrokes: null
|
||||
commandForEvent: null
|
||||
parser: null
|
||||
name: null
|
||||
|
||||
constructor: (@selector, commandsByKeystrokes, @index) ->
|
||||
constructor: (@selector, commandsByKeystrokes, @index, @name) ->
|
||||
BindingSet.parser ?= PEG.buildParser(fs.read(require.resolve 'keystroke-pattern.pegjs'))
|
||||
@specificity = Specificity(@selector)
|
||||
@commandsByKeystrokes = @normalizeCommandsByKeystrokes(commandsByKeystrokes)
|
||||
|
||||
@@ -40,20 +40,39 @@ class Keymap
|
||||
@load(filePath) for filePath in fs.list(directoryPath, ['.cson', '.json']) ? []
|
||||
|
||||
load: (path) ->
|
||||
@add(CSON.readObject(path))
|
||||
@add(path, CSON.readObject(path))
|
||||
|
||||
add: (keymap) ->
|
||||
add: (args...) ->
|
||||
name = args.shift() if args.length > 1
|
||||
keymap = args.shift()
|
||||
for selector, bindings of keymap
|
||||
@bindKeys(selector, bindings)
|
||||
@bindKeys(name, selector, bindings)
|
||||
|
||||
bindKeys: (selector, bindings) ->
|
||||
bindingSet = new BindingSet(selector, bindings, @bindingSets.length)
|
||||
remove: (name) ->
|
||||
for bindingSet in @bindingSets.filter((bindingSet) -> bindingSet.name is name)
|
||||
_.remove(@bindingSets, bindingSet)
|
||||
for keystrokes of bindingSet.commandsByKeystrokes
|
||||
keystroke = keystrokes.split(' ')[0]
|
||||
_.remove(@bindingSetsByFirstKeystroke[keystroke], bindingSet)
|
||||
|
||||
bindKeys: (args...) ->
|
||||
name = args.shift() if args.length > 2
|
||||
[selector, bindings] = args
|
||||
bindingSet = new BindingSet(selector, bindings, @bindingSets.length, name)
|
||||
@bindingSets.unshift(bindingSet)
|
||||
for keystrokes of bindingSet.commandsByKeystrokes
|
||||
keystroke = keystrokes.split(' ')[0] # only index by first keystroke
|
||||
@bindingSetsByFirstKeystroke[keystroke] ?= []
|
||||
@bindingSetsByFirstKeystroke[keystroke].push(bindingSet)
|
||||
|
||||
unbindKeys: (selector, bindings) ->
|
||||
bindingSet = _.detect @bindingSets, (bindingSet) ->
|
||||
bindingSet.selector is selector and bindingSet.bindings is bindings
|
||||
|
||||
if bindingSet
|
||||
console.log "binding set", bindingSet
|
||||
_.remove(@bindingSets, bindingSet)
|
||||
|
||||
bindingsForElement: (element) ->
|
||||
keystrokeMap = {}
|
||||
currentNode = $(element)
|
||||
|
||||
Reference in New Issue
Block a user