Skip to content

Commit

Permalink
Various lint-fixes and code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
wittlock committed Mar 2, 2020
1 parent e573e9d commit 2b84a6d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 58 deletions.
29 changes: 15 additions & 14 deletions src/lib/hotkey.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ export interface ExtendedKeyboardEvent extends KeyboardEvent {
}

export class Hotkey {
_formatted: string[];
private formattedHotkey: string[];

static symbolize(combo: string): string {
let map: any = {
const map: any = {
command: '\u2318', // ⌘
shift: '\u21E7', // ⇧
left: '\u2190', // ←
right: '\u2192', // →
up: '\u2191', // ↑
down: '\u2193', // ↓
// tslint:disable-next-line:object-literal-key-quotes
'return': '\u23CE', // ⏎
backspace: '\u232B' // ⌫
};
let comboSplit: string[] = combo.split('+');
const comboSplit: string[] = combo.split('+');

for (let i = 0; i < comboSplit.length; i++) {
// try to resolve command / ctrl based on OS:
Expand All @@ -37,30 +38,30 @@ export class Hotkey {
/**
* Creates a new Hotkey for Mousetrap binding
*
* @param {string} combo mousetrap key binding
* @param {string} description description for the help menu
* @param {Function} callback method to call when key is pressed
* @param {string} action the type of event to listen for (for mousetrap)
* @param {array} allowIn an array of tag names to allow this combo in ('INPUT', 'SELECT', and/or 'TEXTAREA')
* @param {boolean} persistent if true, the binding is preserved upon route changes
* @param combo mousetrap key binding
* @param description description for the help menu
* @param callback method to call when key is pressed
* @param action the type of event to listen for (for mousetrap)
* @param allowIn an array of tag names to allow this combo in ('INPUT', 'SELECT', and/or 'TEXTAREA')
* @param persistent if true, the binding is preserved upon route changes
*/
constructor(public combo: string | string[], public callback: (event: KeyboardEvent, combo: string) => ExtendedKeyboardEvent | boolean,
public allowIn?: string[], public description?: string | Function, public action?: string,
public persistent?: boolean) {
this.combo = (Array.isArray(combo) ? combo : [<string>combo]);
this.combo = (Array.isArray(combo) ? combo : [combo as string]);
this.allowIn = allowIn || [];
this.description = description || '';
}

get formatted(): string[] {
if (!this._formatted) {
if (!this.formattedHotkey) {

let sequence: string[] = this.combo as Array<string>;
const sequence: string[] = this.combo as Array<string>;
for (let i = 0; i < sequence.length; i++) {
sequence[i] = Hotkey.symbolize(sequence[i]);
}
this._formatted = sequence;
this.formattedHotkey = sequence;
}
return this._formatted;
return this.formattedHotkey;
}
}
1 change: 1 addition & 0 deletions src/lib/hotkey.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { HotkeysService } from './hotkeys.service';
exports: [HotkeysDirective, HotkeysCheatsheetComponent]
})
export class HotkeyModule {
// noinspection JSUnusedGlobalSymbols
static forRoot(options: IHotkeyOptions = {}): ModuleWithProviders<HotkeyModule> {
return {
ngModule : HotkeyModule,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/hotkeys-cheatsheet/hotkeys-cheatsheet.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Subscription } from 'rxjs';
})
export class HotkeysCheatsheetComponent implements OnInit, OnDestroy {
helpVisible = false;
@Input() title: string = 'Keyboard Shortcuts:';
@Input() title = 'Keyboard Shortcuts:';
subscription: Subscription;

hotkeys: Hotkey[];
Expand Down
18 changes: 9 additions & 9 deletions src/lib/hotkeys.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ export class HotkeysDirective implements OnInit, OnDestroy {
private hotkeysList: Hotkey[] = [];
private oldHotkeys: Hotkey[] = [];

constructor(private _hotkeysService: HotkeysService, private _elementRef: ElementRef) {
this.mousetrap = new Mousetrap(this._elementRef.nativeElement); // Bind hotkeys to the current element (and any children)
constructor(private hotkeysService: HotkeysService, private elementRef: ElementRef) {
this.mousetrap = new Mousetrap(this.elementRef.nativeElement); // Bind hotkeys to the current element (and any children)
}

ngOnInit() {
for (let hotkey of this.hotkeys) {
let combo = Object.keys(hotkey)[0];
let hotkeyObj: Hotkey = new Hotkey(combo, hotkey[combo]);
let oldHotkey: Hotkey = <Hotkey>this._hotkeysService.get(combo);
for (const hotkey of this.hotkeys) {
const combo = Object.keys(hotkey)[0];
const hotkeyObj: Hotkey = new Hotkey(combo, hotkey[combo]);
const oldHotkey: Hotkey = this.hotkeysService.get(combo) as Hotkey;
if (oldHotkey !== null) { // We let the user overwrite callbacks temporarily if you specify it in HTML
this.oldHotkeys.push(oldHotkey);
this._hotkeysService.remove(oldHotkey);
this.hotkeysService.remove(oldHotkey);
}
this.hotkeysList.push(hotkeyObj);
this.mousetrap.bind(hotkeyObj.combo, hotkeyObj.callback);
}
}

ngOnDestroy() {
for (let hotkey of this.hotkeysList) {
for (const hotkey of this.hotkeysList) {
this.mousetrap.unbind(hotkey.combo);
}
this._hotkeysService.add(this.oldHotkeys);
this.hotkeysService.add(this.oldHotkeys);
}

}
73 changes: 39 additions & 34 deletions src/lib/hotkeys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ export class HotkeysService {
mousetrap: MousetrapInstance;
cheatSheetToggle: Subject<any> = new Subject();

private _preventIn = ['INPUT', 'SELECT', 'TEXTAREA'];
private preventIn = ['INPUT', 'SELECT', 'TEXTAREA'];

constructor(@Inject(HotkeyOptions) private options: IHotkeyOptions) {
// noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols
Mousetrap.prototype.stopCallback = (event: KeyboardEvent, element: HTMLElement, combo: string, callback: Function) => {
// if the element has the class "mousetrap" then no need to stop
if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
return false;
}
return (element.contentEditable && element.contentEditable === 'true');
};
this.mousetrap = new (<any>Mousetrap)();
this.mousetrap = new (Mousetrap as any)();
if (!this.options.disableCheatSheet) {
this.add(new Hotkey(
this.options.cheatSheetHotkey || '?',
function (event: KeyboardEvent) {
function(_: KeyboardEvent) {
this.cheatSheetToggle.next();
}.bind(this),
[],
Expand All @@ -38,7 +39,7 @@ export class HotkeysService {
if (this.options.cheatSheetCloseEsc) {
this.add(new Hotkey(
'esc',
function (event: KeyboardEvent) {
function(_: KeyboardEvent) {
this.cheatSheetToggle.next(false);
}.bind(this),
['HOTKEYS-CHEATSHEET'],
Expand All @@ -50,57 +51,58 @@ export class HotkeysService {

add(hotkey: Hotkey | Hotkey[], specificEvent?: string): Hotkey | Hotkey[] {
if (Array.isArray(hotkey)) {
let temp: Hotkey[] = [];
for (let key of hotkey) {
temp.push(<Hotkey>this.add(key, specificEvent));
const temp: Hotkey[] = [];
for (const key of hotkey) {
temp.push(this.add(key, specificEvent) as Hotkey);
}
return temp;
}
this.remove(hotkey);
this.hotkeys.push(<Hotkey>hotkey);
this.mousetrap.bind((<Hotkey>hotkey).combo, (event: KeyboardEvent, combo: string) => {
this.hotkeys.push(hotkey as Hotkey);
this.mousetrap.bind((hotkey as Hotkey).combo, (event: KeyboardEvent, combo: string) => {
let shouldExecute = true;

// if the callback is executed directly `hotkey.get('w').callback()`
// there will be no event, so just execute the callback.
if (event) {
let target: HTMLElement = <HTMLElement>(event.target || event.srcElement); // srcElement is IE only
let nodeName: string = target.nodeName.toUpperCase();
const target: HTMLElement = (event.target || event.srcElement) as HTMLElement; // srcElement is IE only
const nodeName: string = target.nodeName.toUpperCase();

// check if the input has a mousetrap class, and skip checking preventIn if so
if ((' ' + target.className + ' ').indexOf(' mousetrap ') > -1) {
shouldExecute = true;
} else if (this._preventIn.indexOf(nodeName) > -1 && (<Hotkey>hotkey).allowIn.map(allow => allow.toUpperCase()).indexOf(nodeName) === -1) {
} else if (this.preventIn.indexOf(nodeName) > -1 &&
(hotkey as Hotkey).allowIn.map(allow => allow.toUpperCase()).indexOf(nodeName) === -1) {
// don't execute callback if the event was fired from inside an element listed in preventIn but not in allowIn
shouldExecute = false;
}
}

if (shouldExecute) {
return (<Hotkey>hotkey).callback.apply(this, [event, combo]);
return (hotkey as Hotkey).callback.apply(this, [event, combo]);
}
}, specificEvent);
return hotkey;
}

remove(hotkey?: Hotkey | Hotkey[]): Hotkey | Hotkey[] {
let temp: Hotkey[] = [];
const temp: Hotkey[] = [];
if (!hotkey) {
for (let key of this.hotkeys) {
temp.push(<Hotkey>this.remove(key));
for (const key of this.hotkeys) {
temp.push(this.remove(key) as Hotkey);
}
return temp;
}
if (Array.isArray(hotkey)) {
for (let key of hotkey) {
temp.push(<Hotkey>this.remove(key));
for (const key of hotkey) {
temp.push(this.remove(key) as Hotkey);
}
return temp;
}
let index = this.findHotkey(<Hotkey>hotkey);
const index = this.findHotkey(hotkey as Hotkey);
if (index > -1) {
this.hotkeys.splice(index, 1);
this.mousetrap.unbind((<Hotkey>hotkey).combo);
this.mousetrap.unbind((hotkey as Hotkey).combo);
return hotkey;
}
return null;
Expand All @@ -111,55 +113,58 @@ export class HotkeysService {
return this.hotkeys;
}
if (Array.isArray(combo)) {
let temp: Hotkey[] = [];
for (let key of combo) {
temp.push(<Hotkey>this.get(key));
const temp: Hotkey[] = [];
for (const key of combo) {
temp.push(this.get(key) as Hotkey);
}
return temp;
}
for (let i = 0; i < this.hotkeys.length; i++) {
if (this.hotkeys[i].combo.indexOf(<string>combo) > -1) {
return this.hotkeys[i];
for (const hotkey of this.hotkeys) {
if (hotkey.combo.indexOf(combo as string) > -1) {
return hotkey;
}
}
return null;
}

// noinspection JSUnusedGlobalSymbols
pause(hotkey?: Hotkey | Hotkey[]): Hotkey | Hotkey[] {
if (!hotkey) {
return this.pause(this.hotkeys);
}
if (Array.isArray(hotkey)) {
let temp: Hotkey[] = [];
for (let key of hotkey) {
temp.push(<Hotkey>this.pause(key));
const temp: Hotkey[] = [];
for (const key of hotkey) {
temp.push(this.pause(key) as Hotkey);
}
return temp;
}
this.remove(hotkey);
this.pausedHotkeys.push(<Hotkey>hotkey);
this.pausedHotkeys.push(hotkey as Hotkey);
return hotkey;
}

// noinspection JSUnusedGlobalSymbols
unpause(hotkey?: Hotkey | Hotkey[]): Hotkey | Hotkey[] {
if (!hotkey) {
return this.unpause(this.pausedHotkeys);
}
if (Array.isArray(hotkey)) {
let temp: Hotkey[] = [];
for (let key of hotkey) {
temp.push(<Hotkey>this.unpause(key));
const temp: Hotkey[] = [];
for (const key of hotkey) {
temp.push(this.unpause(key) as Hotkey);
}
return temp;
}
let index: number = this.pausedHotkeys.indexOf(<Hotkey>hotkey);
const index: number = this.pausedHotkeys.indexOf(hotkey as Hotkey);
if (index > -1) {
this.add(hotkey);
return this.pausedHotkeys.splice(index, 1);
}
return null;
}

// noinspection JSUnusedGlobalSymbols
reset() {
this.mousetrap.reset();
}
Expand Down

0 comments on commit 2b84a6d

Please sign in to comment.