Merge pull request #20606 from cyrilgandon/angular-hotkeys-improvement

angular-hotkeys: add linter, docs and tests
This commit is contained in:
Nathan Shively-Sanders
2017-11-14 10:44:12 -08:00
committed by GitHub
5 changed files with 166 additions and 187 deletions

View File

@@ -1,55 +0,0 @@
## What is it?
This is a typescript interface to be used with [angular-hotkeys](https://github.com/chieffancypants/angular-hotkeys/).
## What are declaration files?
See the [TypeScript handbook](http://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html).
## How do I get them?
### npm
This is the preferred method. This is only available for TypeScript 2.0+ users. For these typings:
```sh
npm install --save-dev @types/angular-hotkeys
```
The types should then be automatically included by the compiler.
See more in the [handbook](http://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html).
### Other methods
These can be used by TypeScript 1.0.
* [Typings](https://github.com/typings/typings)
* ~~[NuGet](http://nuget.org/Tpackages?q=DefinitelyTyped)~~ (use preferred alternatives, nuget DT type publishing has been turned off)
* Manually download from the `master` branch of this repository
You may need to add manual [references](http://www.typescriptlang.org/docs/handbook/triple-slash-directives.html).
## How to use
After installing a package you can import the definition file and begin using it as so:
```ts
import * as hotkeys from '../../node_modules/@types/angular-hotkeys'
class FooController {
static $inject = [
'hotkeys'
];
constructor(
private hotkeys: ng.hotkeys.HotkeysProvider
) { }
}
```
for a detailed explanation of the behavior of angular-hotkeys please refer to [its documentation](https://github.com/chieffancypants/angular-hotkeys/)

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,10 @@
// 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" />
import * as ng from 'angular';
@@ -15,46 +13,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

@@ -1,79 +1 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
}
{ "extends": "dtslint/dt.json" }