🤖 Merge PR #49968 update(mousetrap): parameterless ctor support and other changes by @peterblazejewicz

- just like creation function constructor comes with optional target
  element support
https://github.com/ccampbell/mousetrap/blob/master/mousetrap.js#L438
- DT support updated and unified with default settings
- DT header cleared out
- default formatting applied after DT linting change
- maintainer added

Thanks!
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz)
2020-12-08 07:20:00 +01:00
committed by GitHub
parent 94e34ea125
commit ee963d0c05
6 changed files with 143 additions and 58 deletions

View File

@@ -1,8 +1,11 @@
// Type definitions for Mousetrap 1.6.x
// Type definitions for Mousetrap 1.6
// Project: http://craig.is/killing/mice
// Definitions by: Dániel Tar <https://github.com/qcz>, Alan Choi <https://github.com/alanhchoi>, Nic Barker <https://github.com/nicbarker>, Mitsuka Hanakura a.k.a ragg <https://github.com/ra-gg>
// Definitions by: Dániel Tar <https://github.com/qcz>
// Alan Choi <https://github.com/alanhchoi>
// Nic Barker <https://github.com/nicbarker>
// Mitsuka Hanakura a.k.a ragg <https://github.com/ra-gg>
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
declare namespace Mousetrap {
interface ExtendedKeyboardEvent extends KeyboardEvent {
@@ -10,30 +13,41 @@ declare namespace Mousetrap {
}
interface MousetrapStatic {
(el: Element): MousetrapInstance;
(el?: Element): MousetrapInstance;
new (el?: Element): MousetrapInstance;
addKeycodes(keycodes: { [key: number]: string }): void;
stopCallback: (e: ExtendedKeyboardEvent, element: Element, combo: string) => boolean;
bind(keys: string|string[], callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): MousetrapInstance;
unbind(keys: string|string[], action?: string): MousetrapInstance;
bind(
keys: string | string[],
callback: (e: ExtendedKeyboardEvent, combo: string) => any,
action?: string,
): MousetrapInstance;
unbind(keys: string | string[], action?: string): MousetrapInstance;
trigger(keys: string, action?: string): MousetrapInstance;
reset(): MousetrapInstance;
/** https://craig.is/killing/mice#extensions.global */
bindGlobal(keyArray: string|string[], callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): void;
bindGlobal(
keyArray: string | string[],
callback: (e: ExtendedKeyboardEvent, combo: string) => any,
action?: string,
): void;
}
interface MousetrapInstance {
stopCallback: (e: ExtendedKeyboardEvent, element: Element, combo: string) => boolean;
bind(keys: string|string[], callback: (e: ExtendedKeyboardEvent, combo: string) => any, action?: string): this;
unbind(keys: string|string[], action?: string): this;
bind(
keys: string | string[],
callback: (e: ExtendedKeyboardEvent, combo: string) => any,
action?: string,
): this;
unbind(keys: string | string[], action?: string): this;
trigger(keys: string, action?: string): this;
handleKey(character: string, modifiers: string[], e: ExtendedKeyboardEvent): void;
reset(): this;
}
}
declare var Mousetrap: Mousetrap.MousetrapStatic;
export = Mousetrap;

View File

@@ -1,12 +1,24 @@
Mousetrap.bindGlobal('4', function() { console.log('4'); });
Mousetrap.bindGlobal("?", function() { console.log('show shortcuts!'); });
Mousetrap.bindGlobal('esc', function() { console.log('escape'); }, 'keyup');
Mousetrap.bindGlobal('4', () => {
console.log('4');
});
Mousetrap.bindGlobal('?', () => {
console.log('show shortcuts!');
});
Mousetrap.bindGlobal(
'esc',
() => {
console.log('escape');
},
'keyup',
);
// combinations
Mousetrap.bindGlobal('command+shift+K', function() { console.log('command shift k'); });
Mousetrap.bindGlobal('command+shift+K', () => {
console.log('command shift k');
});
// map multiple combinations to the same callback
Mousetrap.bindGlobal(['command+k', 'ctrl+k'], function() {
Mousetrap.bindGlobal(['command+k', 'ctrl+k'], () => {
console.log('command k or control k');
// return false to prevent default browser behavior
@@ -15,11 +27,15 @@ Mousetrap.bindGlobal(['command+k', 'ctrl+k'], function() {
});
// gmail style sequences
Mousetrap.bindGlobal('g i', function() { console.log('go to inbox'); });
Mousetrap.bindGlobal('* a', function() { console.log('select all'); });
Mousetrap.bindGlobal('g i', () => {
console.log('go to inbox');
});
Mousetrap.bindGlobal('* a', () => {
console.log('select all');
});
// konami code!
Mousetrap.bindGlobal('up up down down left right left right b a enter', function() {
Mousetrap.bindGlobal('up up down down left right left right b a enter', () => {
console.log('konami code');
});
@@ -27,10 +43,11 @@ Mousetrap.bindGlobal('up up down down left right left right b a enter', function
{
// Test that union types are accepted.
const unionTypeKeys: string | string[] = ['a', 'b', 'c'];
Mousetrap.bindGlobal(unionTypeKeys, function() { console.log('Union type test') });
Mousetrap.bindGlobal(unionTypeKeys, () => {
console.log('Union type test');
});
}
Mousetrap.bindGlobal(['ctrl+s', 'meta+s'], (e, combo) => {
if (e.preventDefault) {
e.preventDefault();

View File

@@ -1,10 +1,49 @@
// 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 Mousetrap = require('mousetrap');
import { ExtendedKeyboardEvent } from 'mousetrap';
import importedMousetrap = require('mousetrap');
// $ExpectType MousetrapStatic
var mousetrapModuleReference = importedMousetrap;
Mousetrap;
// Can import event
type Event = importedMousetrap.ExtendedKeyboardEvent;
type Event = ExtendedKeyboardEvent;
Mousetrap.bind('4', () => {
console.log('4');
});
Mousetrap.bind('?', () => {
console.log('show shortcuts!');
});
Mousetrap.bind(
'esc',
() => {
console.log('escape');
},
'keyup',
);
// combinations
Mousetrap.bind('command+shift+k', () => {
console.log('command shift k');
});
// map multiple combinations to the same callback
Mousetrap.bind(['command+k', 'ctrl+k'], () => {
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.bind('g i', () => {
console.log('go to inbox');
});
Mousetrap.bind('* a', () => {
console.log('select all');
});
// konami code!
Mousetrap.bind('up up down down left right left right b a enter', () => {
console.log('konami code');
});

View File

@@ -1,15 +1,24 @@
/// <reference types="jquery"/>
Mousetrap
.bind('4', function() { console.log('4'); })
.bind("?", function() { console.log('show shortcuts!'); })
.bind('esc', function() { console.log('escape'); }, 'keyup');
Mousetrap.bind('4', () => {
console.log('4');
})
.bind('?', () => {
console.log('show shortcuts!');
})
.bind(
'esc',
() => {
console.log('escape');
},
'keyup',
);
// combinations
Mousetrap.bind('command+shift+K', function() { console.log('command shift k'); });
Mousetrap.bind('command+shift+K', () => {
console.log('command shift k');
});
// map multiple combinations to the same callback
Mousetrap.bind(['command+k', 'ctrl+k'], function() {
Mousetrap.bind(['command+k', 'ctrl+k'], () => {
console.log('command k or control k');
// return false to prevent default browser behavior
@@ -18,11 +27,15 @@ Mousetrap.bind(['command+k', 'ctrl+k'], function() {
});
// gmail style sequences
Mousetrap.bind('g i', function() { console.log('go to inbox'); });
Mousetrap.bind('* a', function() { console.log('select all'); });
Mousetrap.bind('g i', () => {
console.log('go to inbox');
});
Mousetrap.bind('* a', () => {
console.log('select all');
});
// konami code!
Mousetrap.bind('up up down down left right left right b a enter', function() {
Mousetrap.bind('up up down down left right left right b a enter', () => {
console.log('konami code');
});
@@ -42,25 +55,37 @@ Mousetrap.trigger('esc', 'keyup');
// Test that custom keycodes can be added.
Mousetrap.addKeycodes({ 144: 'numlock' });
Mousetrap.bind('numlock', function() { console.log('numlock'); });
Mousetrap.bind('numlock', () => {
console.log('numlock');
});
Mousetrap.reset();
// Test that we can create an instance of mousetrap and attach the
// event handler to the form element only, instead of the entire document.
var element = document.querySelector('form');
var instance = new Mousetrap(element);
instance.bind('mod+s', function () { console.log('Instance Saved'); });
let element = document.querySelector('form')!;
let instance = new Mousetrap(element);
instance.bind('mod+s', () => {
console.log('Instance Saved');
});
// Test that we can create an instance of mousetrap without passing element to the constructor.
var documentInstance = new Mousetrap();
documentInstance.bind('mod+s', function () { console.log('documentInstance Saved'); });
let documentInstance = new Mousetrap();
documentInstance.bind('mod+s', () => {
console.log('documentInstance Saved');
});
// Test that the factory method works as well.
Mousetrap(element).bind('mod+s', function(){ console.log('Factory Saved'); });
Mousetrap(element).bind('mod+s', () => {
console.log('Factory Saved');
});
// Test that union types are accepted.
const unionTypeKeys: string | string[] = ['a', 'b', 'c'];
Mousetrap(element).bind(unionTypeKeys, function() { console.log('Union type test') });
Mousetrap(element).bind(unionTypeKeys, () => {
console.log('Union type test');
});
Mousetrap(element).handleKey = (character: string, modifiers: string[], e: KeyboardEvent) => { console.log('Override handleKey test') };
Mousetrap(element).handleKey = (character: string, modifiers: string[], e: KeyboardEvent) => {
console.log('Override handleKey test');
};

View File

@@ -7,7 +7,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [

View File

@@ -1,13 +1,3 @@
{
"extends": "dtslint/dt.json",
"rules": {
"dt-header": false,
"no-consecutive-blank-lines": false,
"no-var-keyword": false,
"one-line": false,
"only-arrow-functions": false,
"prefer-const": false,
"semicolon": false,
"space-before-function-paren": false
}
"extends": "dtslint/dt.json"
}