Replace binding-set with key-binding

This commit is contained in:
probablycorey
2013-11-15 13:32:09 -08:00
parent 529c829438
commit 8ed4923e58
3 changed files with 39 additions and 39 deletions

View File

@@ -1,18 +0,0 @@
{$} = require './space-pen-extensions'
_ = require 'underscore-plus'
fs = require 'fs-plus'
{specificity} = require 'clear-cut'
PEG = require 'pegjs'
### Internal ###
module.exports =
class BindingSet
@nextBindingSetIndex: 0
constructor: (selector, commandsByKeystroke, @name) ->
@index = BindingSet.nextBindingSetIndex++
keystrokePattern = fs.readFileSync(require.resolve('./keystroke-pattern.pegjs'), 'utf8')
BindingSet.parser ?= PEG.buildParser(keystrokePattern)
@specificity = specificity(selector)
@selector = selector.replace(/!important/g, '')

35
src/key-binding.coffee Normal file
View File

@@ -0,0 +1,35 @@
{$} = require './space-pen-extensions'
_ = require 'underscore-plus'
fs = require 'fs-plus'
{specificity} = require 'clear-cut'
PEG = require 'pegjs'
### Internal ###
module.exports =
class KeyBinding
@parser: null
@currentIndex: 1
@normalizeKeystroke: (keystroke) ->
normalizedKeystroke = keystroke.split(/\s+/).map (keystroke) =>
keys = @getParser().parse(keystroke)
modifiers = keys[0...-1]
modifiers.sort()
[modifiers..., _.last(keys)].join('-')
normalizedKeystroke.join(' ')
@getParser: ->
if not KeyBinding.parser
keystrokePattern = fs.readFileSync(require.resolve('./keystroke-pattern.pegjs'), 'utf8')
KeyBinding.parser = PEG.buildParser(keystrokePattern)
KeyBinding.parser
constructor: (source, command, keystroke, selector) ->
@source = source
@command = command
@keystroke = KeyBinding.normalizeKeystroke(keystroke)
@selector = selector.replace(/!important/g, '')
@specificity = specificity(selector)
@index = KeyBinding.currentIndex++

View File

@@ -3,7 +3,7 @@ _ = require 'underscore-plus'
fs = require 'fs-plus'
path = require 'path'
CSON = require 'season'
BindingSet = require './binding-set'
KeyBinding = require './key-binding'
{Emitter} = require 'emissary'
Modifiers = ['alt', 'control', 'ctrl', 'shift', 'meta']
@@ -49,18 +49,9 @@ class Keymap
remove: (name) ->
@keyBindings = @keyBindings.filter (keyBinding) -> keyBinding.name is name
bindKeys: (name, selector, keyMappings) ->
bindingSet = new BindingSet(selector, keyMappings, name)
bindKeys: (source, selector, keyMappings) ->
for keystroke, command of keyMappings
@keyBindings.push @buildBinding(bindingSet, command, keystroke)
buildBinding: (bindingSet, command, keystroke) ->
keystroke = @normalizeKeystroke(keystroke)
selector = bindingSet.selector
specificity = bindingSet.specificity
index = bindingSet.index
source = bindingSet.name
{command, keystroke, selector, specificity, source, index}
@keyBindings.push new KeyBinding(source, command, keystroke, selector)
handleKeyEvent: (event) ->
element = event.target
@@ -100,7 +91,7 @@ class Keymap
@bindingsMatchingElement(element, keyBindings)
bindingsForKeystroke: (keystroke) ->
keystroke = @normalizeKeystroke(keystroke)
keystroke = KeyBinding.normalizeKeystroke(keystroke)
keyBindings = @allBindings().filter (keyBinding) ->
multiKeystroke = /\s/.test keystroke
@@ -168,11 +159,3 @@ class Keymap
when 32 then 'space'
when 127 then 'delete'
else String.fromCharCode(charCode)
normalizeKeystroke: (keystroke) ->
normalizedKeystroke = keystroke.split(/\s+/).map (keystroke) =>
keys = BindingSet.parser.parse(keystroke)
modifiers = keys[0...-1]
modifiers.sort()
[modifiers..., _.last(keys)].join('-')
normalizedKeystroke.join(' ')