From 846846abe80ea00a6e019e28f7e47a238d464af7 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 3 Apr 2012 15:51:53 -0600 Subject: [PATCH] Keymap normalizes key patterns so modifier keys can be listed in a random order. --- spec/app/keymap-spec.coffee | 11 ++++++++++- src/app/binding-set.coffee | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/spec/app/keymap-spec.coffee b/spec/app/keymap-spec.coffee index df3d3a946..d0c26265b 100644 --- a/spec/app/keymap-spec.coffee +++ b/spec/app/keymap-spec.coffee @@ -125,7 +125,16 @@ describe "Keymap", -> expect(bazHandler).toHaveBeenCalled() describe ".bindKeys(selector, fnOrMap)", -> - describe "when called with a function", -> + describe "when called with a selector and a hash", -> + it "normalizes the key patterns in the hash to put the modifiers in alphabetical order", -> + fooHandler = jasmine.createSpy('fooHandler') + fragment.on 'foo', fooHandler + keymap.bindKeys '*', 'ctrl-alt-delete': 'foo' + result = keymap.handleKeyEvent(keydownEvent('delete', ctrlKey: true, altKey: true, target: fragment[0])) + expect(result).toBe(false) + expect(fooHandler).toHaveBeenCalled() + + describe "when called with a selector and a function", -> it "calls the given function when selector matches", -> handler = jasmine.createSpy 'handler' keymap.bindKeys '.child-node', handler diff --git a/src/app/binding-set.coffee b/src/app/binding-set.coffee index 425f4e5f8..5aa6f54d3 100644 --- a/src/app/binding-set.coffee +++ b/src/app/binding-set.coffee @@ -15,6 +15,7 @@ class BindingSet if _.isFunction(mapOrFunction) mapOrFunction else + mapOrFunction = @normalizeKeystrokePatterns(mapOrFunction) (event) => for pattern, command of mapOrFunction return command if @eventMatchesPattern(event, pattern) @@ -23,3 +24,16 @@ class BindingSet eventMatchesPattern: (event, pattern) -> pattern = pattern.replace(/^<|>$/g, '') event.keystroke == pattern + + normalizeKeystrokePatterns: (map) -> + normalizedMap = {} + for pattern, event of map + normalizedMap[@normalizeKeystrokePattern(pattern)] = event + normalizedMap + + normalizeKeystrokePattern: (pattern) -> + keys = pattern.split('-') + modifiers = keys[0...-1] + modifiers.sort() + [modifiers..., _.last(keys)].join('-') +