Skip to content

Commit

Permalink
feat(ui-kit): add NgDocHotkeyDirective for binding shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
skoropadas committed Jan 31, 2023
1 parent 2ed2f0d commit e931994
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
48 changes: 48 additions & 0 deletions libs/ui-kit/directives/hotkey/hotkey.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {DOCUMENT} from '@angular/common';
import {Directive, EventEmitter, Inject, Input, NgZone, Output} from '@angular/core';
import {isKeyboardEvent, objectKeys} from '@ng-doc/core';
import {ngDocZoneOptimize} from '@ng-doc/ui-kit/observables';
import {UntilDestroy, untilDestroyed} from '@ngneat/until-destroy';
import {Document} from 'postcss';
import {fromEvent, Observable} from 'rxjs';
import {filter, share} from 'rxjs/operators';

const KEYUP_EVENT: Observable<Event> = fromEvent(document, 'keyup').pipe(share());

@Directive({
selector: '[ngDocHotkey]',
})
@UntilDestroy()
export class NgDocHotkeyDirective {
@Input('ngDocHotkey')
hotkey?: Partial<KeyboardEvent>;

@Output('ngDocHotkey')
callback: EventEmitter<void> = new EventEmitter<void>();

constructor(
@Inject(DOCUMENT)
private readonly document: Document,
private readonly ngZone: NgZone,
) {
KEYUP_EVENT.pipe(
filter(isKeyboardEvent),
filter((event: KeyboardEvent) =>
objectKeys(this.hotkey ?? {}).every(
(key: keyof KeyboardEvent) => this.hotkey && this.hotkey[key] === event[key],
),
),
filter((event: KeyboardEvent) => {
if (event.target instanceof HTMLElement) {
return !['input', 'textarea', 'select'].includes(event.target.tagName.toLowerCase());
}
return true;
}),
ngDocZoneOptimize(this.ngZone),
untilDestroyed(this),
).subscribe((event: KeyboardEvent) => {
event.preventDefault();
this.callback.emit();
});
}
}
9 changes: 9 additions & 0 deletions libs/ui-kit/directives/hotkey/hotkey.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {NgModule} from '@angular/core';

import {NgDocHotkeyDirective} from './hotkey.directive';

@NgModule({
declarations: [NgDocHotkeyDirective],
exports: [NgDocHotkeyDirective],
})
export class NgDocHotkeyModule {}
2 changes: 2 additions & 0 deletions libs/ui-kit/directives/hotkey/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './hotkey.directive';
export * from './hotkey.module';
5 changes: 5 additions & 0 deletions libs/ui-kit/directives/hotkey/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "index.ts"
}
}
1 change: 1 addition & 0 deletions libs/ui-kit/directives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from '@ng-doc/ui-kit/directives/dropdown-origin';
export * from '@ng-doc/ui-kit/directives/event-switcher';
export * from '@ng-doc/ui-kit/directives/focus-catcher';
export * from '@ng-doc/ui-kit/directives/focusable';
export * from '@ng-doc/ui-kit/directives/hotkey';
export * from '@ng-doc/ui-kit/directives/input-number';
export * from '@ng-doc/ui-kit/directives/input-string';
export * from '@ng-doc/ui-kit/directives/let';
Expand Down

0 comments on commit e931994

Please sign in to comment.