-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-kit): add
NgDocHotkeyDirective
for binding shortcuts
- Loading branch information
1 parent
2ed2f0d
commit e931994
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './hotkey.directive'; | ||
export * from './hotkey.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "index.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters