]> git.za3k.com Git - flowy.git/commitdiff
Allow registering the same shortcut multiple times
authorZachary Vance <vanceza@gmail.com>
Wed, 20 May 2015 01:56:46 +0000 (18:56 -0700)
committerZachary Vance <vanceza@gmail.com>
Wed, 20 May 2015 01:56:46 +0000 (18:56 -0700)
src/library/shortcut.js

index 75f46ee79147009d8295c65411d6524932dcee8e..29f9d9fc44224e8b5a3ce21f4cb03fdc7725e156 100644 (file)
@@ -8,13 +8,22 @@
 // o.remove() // Once we lose textbox focus, stop keeping track of it, for example
 
 var Shortcut = (function(document, _) {
+    _.mixin({
+        guid : function(){
+            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
+                var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
+                return v.toString(16);
+            });
+        }
+    });
+
     var globalMousetrap = new Mousetrap();
     var globalObject = {element: document, type: "global", isGlobal: true, mousetrap: globalMousetrap};
     var Shortcut = {
         _globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked.
         globalMousetrap: globalMousetrap,
         boundObjects: [globalObject],
-        shortcuts: [],
+        shortcuts: {},
         registerShortcut: function(shortcut) {
             if (arguments.length > 1) {
                 shortcut = {
@@ -36,18 +45,21 @@ var Shortcut = (function(document, _) {
                 _keybinding: shortcut.keybinding || "",
                 action: function() {},
                 boundObjects: [],
-                ownedByUs: true
+                ownedByUs: true,
+                id: _.guid(),
             });
             Object.defineProperty(shortcut, "keybinding", {
                 set: function (newKeybinding) { manager.rebindShortcut(shortcut, newKeybinding); },
                 get: function () { return shortcut._keybinding; },
             });
-            this.shortcuts.push(shortcut);
+            if (this.shortcuts[shortcut.id]) return this.shortcuts[shortcut.id];
+            this.shortcuts[shortcut.id] = shortcut;
             _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value();
             return shortcut;
         },
         unregisterShortcut: function(shortcut) {
             _.each(_.clone(shortcut.boundObjects), _.partial(this.unbindShortcut, shortcut, _), this);
+            delete this.shortcuts[shortcut.id];
         },
         bindShortcut: function(shortcut, object) { // Do not call directly
             if (!_.contains(shortcut.boundObjects, object)) {
@@ -107,7 +119,7 @@ var Shortcut = (function(document, _) {
             object = {
                 element:element,
                 type:type,
-                mousetrap: new Mousetrap(element),
+                mousetrap: Mousetrap(element),
                 remove: function() { manager.removeObject(this); },
                 add: function() { manager.addObject(this.element, this.type); },
             };