From a0e56c27e72009602b8587b696ee42888f8fe5a5 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Thu, 6 Nov 2014 01:17:54 -0500 Subject: [PATCH 1/3] Adds definitions and tests for the Mousetrap global-bind extension --- .../mousetrap-global-bind-tests.ts | 42 +++++++++++++++++++ .../mousetrap-global-bind.d.ts | 11 +++++ 2 files changed, 53 insertions(+) create mode 100644 mousetrap-global-bind/mousetrap-global-bind-tests.ts create mode 100644 mousetrap-global-bind/mousetrap-global-bind.d.ts diff --git a/mousetrap-global-bind/mousetrap-global-bind-tests.ts b/mousetrap-global-bind/mousetrap-global-bind-tests.ts new file mode 100644 index 000000000..92374d862 --- /dev/null +++ b/mousetrap-global-bind/mousetrap-global-bind-tests.ts @@ -0,0 +1,42 @@ +/// + +Mousetrap.globalBind('4', function() { console.log('4'); }); +Mousetrap.globalBind("?", function() { console.log('show shortcuts!'); }); +Mousetrap.globalBind('esc', function() { console.log('escape'); }, 'keyup'); + +// combinations +Mousetrap.globalBind('command+shift+K', function() { console.log('command shift k'); }); + +// map multiple combinations to the same callback +Mousetrap.globalBind(['command+k', 'ctrl+k'], function() { + console.log('command k or control k'); + + // return false to prevent default browser behavior + // and stop event from bubbling + return false; +}); + +// gmail style sequences +Mousetrap.globalBind('g i', function() { console.log('go to inbox'); }); +Mousetrap.globalBind('* a', function() { console.log('select all'); }); + +// konami code! +Mousetrap.globalBind('up up down down left right left right b a enter', function() { + console.log('konami code'); +}); + +Mousetrap.globalBind(['ctrl+s', 'meta+s'], (e, combo) => { + if (e.preventDefault) { + e.preventDefault(); + } else { + // internet explorer + e.returnValue = false; + } +}); + +Mousetrap.unbind('?'); + +Mousetrap.trigger('esc'); +Mousetrap.trigger('esc', 'keyup'); + +Mousetrap.reset(); diff --git a/mousetrap-global-bind/mousetrap-global-bind.d.ts b/mousetrap-global-bind/mousetrap-global-bind.d.ts new file mode 100644 index 000000000..b22d5e338 --- /dev/null +++ b/mousetrap-global-bind/mousetrap-global-bind.d.ts @@ -0,0 +1,11 @@ +// Type definitions for Mousetrap 1.4.6's global-bind extension +// Project: http://craig.is/killing/mice#extensions.global +// Definitions by: Andrew Bradley +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +interface MousetrapStatic { + globalBind(keys: string, callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void; + globalBind(keyArray: string[], callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void; +} From 1d556e1d7c7d0c0a32ca48daaa4582ed165cd01b Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Thu, 6 Nov 2014 01:31:01 -0500 Subject: [PATCH 2/3] Modified Mousetrap definition to allow Mousetrap to be loaded as an external module. - tests are also updated to test loading as an external module - mousetrap exports itself as an AMD module when an AMD define function is present --- mousetrap/mousetrap-tests.ts | 8 ++++++++ mousetrap/mousetrap.d.ts | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/mousetrap/mousetrap-tests.ts b/mousetrap/mousetrap-tests.ts index 3eac1eef1..d60d8278d 100644 --- a/mousetrap/mousetrap-tests.ts +++ b/mousetrap/mousetrap-tests.ts @@ -41,3 +41,11 @@ Mousetrap.trigger('esc'); Mousetrap.trigger('esc', 'keyup'); Mousetrap.reset(); + +// Test that Mousetrap can be loaded as an external module. +// Assume that if the externally-loaded module can be assigned to a variable with the type of global Mousetrap, +// then everything is working correctly. + +import importedMousetrap = require('mousetrap'); +var mousetrapModuleReference: typeof Mousetrap = importedMousetrap; + diff --git a/mousetrap/mousetrap.d.ts b/mousetrap/mousetrap.d.ts index dee352b42..846e1f3ce 100644 --- a/mousetrap/mousetrap.d.ts +++ b/mousetrap/mousetrap.d.ts @@ -19,3 +19,7 @@ interface MousetrapStatic { } declare var Mousetrap: MousetrapStatic; + +declare module "mousetrap" { + export = Mousetrap; +} From 0607d042b41fd53aef05afca5a0679c0e211fbc9 Mon Sep 17 00:00:00 2001 From: vvakame Date: Sun, 9 Nov 2014 00:14:20 +0900 Subject: [PATCH 3/3] mv mousetrap-global-bind/mousetrap-global-bind.d.ts -> mousetrap/mousetrap-global-bind.d.ts --- .../mousetrap-global-bind-tests.ts | 0 {mousetrap-global-bind => mousetrap}/mousetrap-global-bind.d.ts | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename {mousetrap-global-bind => mousetrap}/mousetrap-global-bind-tests.ts (100%) rename {mousetrap-global-bind => mousetrap}/mousetrap-global-bind.d.ts (88%) diff --git a/mousetrap-global-bind/mousetrap-global-bind-tests.ts b/mousetrap/mousetrap-global-bind-tests.ts similarity index 100% rename from mousetrap-global-bind/mousetrap-global-bind-tests.ts rename to mousetrap/mousetrap-global-bind-tests.ts diff --git a/mousetrap-global-bind/mousetrap-global-bind.d.ts b/mousetrap/mousetrap-global-bind.d.ts similarity index 88% rename from mousetrap-global-bind/mousetrap-global-bind.d.ts rename to mousetrap/mousetrap-global-bind.d.ts index b22d5e338..ecfdd2ff2 100644 --- a/mousetrap-global-bind/mousetrap-global-bind.d.ts +++ b/mousetrap/mousetrap-global-bind.d.ts @@ -3,7 +3,7 @@ // Definitions by: Andrew Bradley // Definitions: https://github.com/borisyankov/DefinitelyTyped -/// +/// interface MousetrapStatic { globalBind(keys: string, callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void;