Can match key patterns with the '-' character

Add a parser to parse keystroke patterns instead of splitting on '-' with a regex
This commit is contained in:
Nathan Sobo
2012-05-09 10:56:31 -06:00
parent 8b1ac28b89
commit f2f44b9ab6
6 changed files with 25 additions and 48 deletions

View File

@@ -1,13 +1,18 @@
$ = require 'jquery'
_ = require 'underscore'
Specificity = require 'specificity'
fs = require 'fs'
PEG = require 'pegjs'
module.exports =
class BindingSet
selector: null
commandForEvent: null
keystrokePatternParser: null
constructor: (@selector, mapOrFunction) ->
@parser = PEG.buildParser(fs.read(require.resolve 'keystroke-pattern.pegjs'))
@specificity = Specificity(@selector)
@commandForEvent = @buildEventHandler(mapOrFunction)
@@ -18,13 +23,9 @@ class BindingSet
mapOrFunction = @normalizeKeystrokePatterns(mapOrFunction)
(event) =>
for pattern, command of mapOrFunction
return command if @eventMatchesPattern(event, pattern)
return command if event.keystroke == pattern
null
eventMatchesPattern: (event, pattern) ->
pattern = pattern.replace(/^<|>$/g, '')
event.keystroke == pattern
normalizeKeystrokePatterns: (map) ->
normalizedMap = {}
for pattern, event of map
@@ -32,7 +33,7 @@ class BindingSet
normalizedMap
normalizeKeystrokePattern: (pattern) ->
keys = pattern.split('-')
keys = @parser.parse(pattern)
modifiers = keys[0...-1]
modifiers.sort()
[modifiers..., _.last(keys)].join('-')

View File

@@ -0,0 +1,3 @@
keystrokePattern = key:key additionalKeys:additionalKey* { return [key].concat(additionalKeys); }
additionalKey = '-' key:key { return key; }
key = '-' / chars:[^-]+ { return chars.join('') }