angular-hotkeys: add linter, docs and tests

This commit is contained in:
Cyril Gandon
2017-10-16 10:35:26 +02:00
parent 9df54d39a0
commit ee8134843a
5 changed files with 169 additions and 55 deletions

View File

@@ -37,7 +37,7 @@ After installing a package you can import the definition file and begin using it
```ts
import * as hotkeys from '../../node_modules/@types/angular-hotkeys'
import { HotkeysProvider } from 'angular-hotkeys';
class FooController {
static $inject = [
@@ -45,7 +45,7 @@ class FooController {
];
constructor(
private hotkeys: ng.hotkeys.HotkeysProvider
private hotkeys: HotkeysProvider
) { }
}

View File

@@ -1,30 +1,67 @@
var scope: ng.IScope;
var hotkeyProvider: ng.hotkeys.HotkeysProvider;
var hotkeyObj: ng.hotkeys.Hotkey;
import { HotkeysProvider, Hotkey } from 'angular-hotkeys';
import { module } from 'angular';
hotkeyProvider.add("mod+s", "saves a file", (event: Event, hotkey: ng.hotkeys.Hotkey) => {} );
hotkeyProvider.add(["mod+s"], "saves a file", (event: Event, hotkey: ng.hotkeys.Hotkey) => {} );
hotkeyProvider.add(hotkeyObj);
hotkeyProvider.bindTo(scope);
hotkeyProvider.del("mod+s");
hotkeyProvider.del(["mod+s"]);
hotkeyProvider.get("mod+s");
hotkeyProvider.get(["mod+s"]);
hotkeyProvider.toggleCheatSheet();
hotkeyProvider.add(hotkeyObj.combo, hotkeyObj.description ,hotkeyObj.callback);
hotkeyProvider.bindTo(scope)
.add(hotkeyObj)
.add(hotkeyObj)
.add({
combo: 'w',
description: 'blah blah',
callback: function() {}
})
.add({
combo: ['w', 'mod+w'],
description: 'blah blah',
callback: function() {}
module('myApp', ['cfp.hotkeys'])
.config((hotkeysProvider: HotkeysProvider) => {
hotkeysProvider.includeCheatSheet = false;
const somehotKeyObj: Hotkey = {
combo: '',
callback: () => { }
};
});
function someInjectionService(
scope: ng.IScope,
hotkeyProvider: ng.hotkeys.HotkeysProvider,
hotkeyObj: ng.hotkeys.Hotkey
) {
hotkeyProvider.add("mod+s", "saves a file", (event: Event, hotkey: ng.hotkeys.Hotkey) => { });
hotkeyProvider.add(["mod+s"], "saves a file", (event: Event, hotkey: ng.hotkeys.Hotkey) => { });
hotkeyProvider.add(hotkeyObj);
hotkeyProvider.bindTo(scope);
hotkeyProvider.del("mod+s");
hotkeyProvider.del(["mod+s"]);
hotkeyProvider.get("mod+s");
hotkeyProvider.get(["mod+s"]);
hotkeyProvider.toggleCheatSheet();
hotkeyProvider.add(hotkeyObj.combo, hotkeyObj.description, hotkeyObj.callback);
hotkeyProvider.bindTo(scope)
.add(hotkeyObj)
.add(hotkeyObj)
.add({
combo: 'w',
description: 'blah blah',
callback: () => { }
})
.add({
combo: ['w', 'mod+w'],
description: 'blah blah',
callback: () => { }
});
hotkeyProvider.add({
combo: 'ctrl+w',
description: 'Description goes here',
callback: (event, hotkey) => {
event.preventDefault();
}
});
hotkeyProvider.add({
combo: 'ctrl+x',
callback: (event, hotkey) => {
//
}
});
hotkeyProvider.add({
combo: 'ctrl+w',
description: 'Description goes here',
allowIn: ['INPUT', 'SELECT', 'TEXTAREA'],
callback(event, hotkey) {
event.preventDefault();
}
});
}

View File

@@ -1,12 +1,11 @@
// Type definitions for angular-hotkeys
// Type definitions for angular-hotkeys 1.7
// Project: https://github.com/chieffancypants/angular-hotkeys
// Definitions by: Jason Zhao <https://github.com/jlz27>, Stefan Steinhart <https://github.com/reppners>
// Definitions by: Jason Zhao <https://github.com/jlz27>
// Stefan Steinhart <https://github.com/reppners>
// Cyril Gandon <https://github.com/cyrilgandon>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
//readme written by David Valentine <https://github.com/dvalenti314>
/// <reference types="angular" />
// Readme written by David Valentine <https://github.com/dvalenti314>
import * as ng from 'angular';
@@ -15,46 +14,123 @@ export type HotkeysProviderChained = ng.hotkeys.HotkeysProviderChained;
export type Hotkey = ng.hotkeys.Hotkey;
declare module 'angular' {
export namespace hotkeys {
namespace hotkeys {
interface HotkeysProvider {
template: string;
templateTitle: string;
/**
* Configurable setting to disable the cheatsheet entirely.
* @default true
*/
includeCheatSheet: boolean;
/**
* Configurable setting to disable ngRoute hooks.
*/
useNgRoute: boolean;
/**
* Configurable setting for the cheat sheet title
* @default 'Keyboard Shortcuts'
*/
templateTitle: string;
/**
* Configurable settings for the cheat sheet header in HTML.
* This overrides the normal title if specified.
* @default null
*/
templateHeader: string | null;
/**
* Configurable settings for the cheat sheet footer in HTML.
* @default null
*/
templateFooter: string | null;
/**
* Cheat sheet template in the event you want to totally customize it.
*/
template: string;
/**
* Configurable setting for the cheat sheet hotkey.
* @default '?'
*/
cheatSheetHotkey: string;
/**
* Configurable setting for the cheat sheet description.
* @default 'Show / hide this help menu'
*/
cheatSheetDescription: string;
add(combo: string | string[], callback: (event: Event, hotkey?: Hotkey) => void, action?: string, allowIn?: Array<string>, persistent?: boolean): ng.hotkeys.Hotkey;
/**
* Creates a new Hotkey and creates the Mousetrap binding.
*/
add(combo: string | string[], description?: string, callback?: (event: Event, hotkey: Hotkey) => void, action?: string, allowIn?: string[], persistent?: boolean): Hotkey;
add(combo: string | string[], description: string, callback: (event: Event, hotkey?: Hotkey) => void, action?: string, allowIn?: Array<string>, persistent?: boolean): ng.hotkeys.Hotkey;
/**
* Creates a new Hotkey and creates the Mousetrap binding.
*/
add(hotkeyObj: Hotkey): Hotkey;
add(hotkeyObj: ng.hotkeys.Hotkey): ng.hotkeys.Hotkey;
/**
* Binds the hotkey to a particular scope.
* Useful if the scope is destroyed, we can automatically destroy the hotkey binding.
* @param scope The scope to bind to
*/
bindTo(scope: IScope): HotkeysProviderChained;
bindTo(scope: ng.IScope): ng.hotkeys.HotkeysProviderChained;
/**
* Removes and unbinds a hotkey
* @param combo The keyboard combo (shortcut) or the HotKey object
*/
del(combo: string | string[] | Hotkey): void;
del(combo: string | string[]): void;
del(hotkeyObj: ng.hotkeys.Hotkey): void;
get(combo: string | string[]): ng.hotkeys.Hotkey;
/**
* Returns the Hotkey object
* @param combo The keyboard combo (shortcut)
*/
get(combo: string | string[]): Hotkey;
/**
* Toggles the help menu element's visiblity
*/
toggleCheatSheet(): void;
/**
* Purges all non-persistent hotkeys (such as those defined in routes)
*
* Without this, the same hotkey would get recreated everytime
* the route is accessed.
*/
purgeHotkeys(): void;
}
interface HotkeysProviderChained {
add(combo: string | string[], description: string, callback: (event: Event, hotkeys: ng.hotkeys.Hotkey) => void): HotkeysProviderChained;
add(combo: string | string[], description: string, callback: (event: Event, hotkeys: Hotkey) => void): HotkeysProviderChained;
add(hotkeyObj: ng.hotkeys.Hotkey): HotkeysProviderChained;
add(hotkeyObj: Hotkey): HotkeysProviderChained;
}
interface Hotkey {
/**
* They keyboard combo (shortcut) you want to bind to.
*/
combo: string | string[];
/**
* The description for what the combo does and is only used for the Cheat Sheet.
* If it is not supplied, it will not show up, and in effect, allows you to have unlisted hotkeys.
*/
description?: string;
callback: (event: Event, hotkey: ng.hotkeys.Hotkey) => void;
/**
* The function to execute when the key(s) are pressed. Passes along two arguments, event and hotkey
*/
callback(event: Event, hotkey: Hotkey): void;
/**
* The type of event to listen for, such as keypress, keydown or keyup.
* Usage of this parameter is discouraged as the underlying library will pick the most suitable option automatically.
* This should only be necessary in advanced situations.
*/
action?: string;
allowIn?: Array<string>;
/**
* An array of tag names to allow this combo in ('INPUT', 'SELECT', and/or 'TEXTAREA')
*/
allowIn?: Array<'INPUT' | 'SELECT' | 'TEXTAREA'>;
/**
* Whether the hotkey persists navigation events
*/
persistent?: boolean;
}
}

View File

@@ -7,7 +7,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
@@ -21,4 +21,4 @@
"index.d.ts",
"angular-hotkeys-tests.ts"
]
}
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }