Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

48 lines
1.2 KiB

/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { Directive, ElementRef, Input, OnDestroy, OnInit } from '@angular/core';
import { ShortcutService } from '@app/framework/internal';
@Directive({
selector: '[shortcut]',
})
export class ShortcutDirective implements OnDestroy, OnInit {
private subscription: Function;
@Input()
public shortcut: string;
@Input()
public shortcutAction: 'focus' | 'click' | 'none' = 'click';
constructor(
private readonly element: ElementRef,
private readonly shortcutService: ShortcutService,
) {
}
public ngOnDestroy() {
this.subscription?.();
}
public ngOnInit() {
if (this.shortcut && this.shortcutAction !== 'none') {
this.subscription = this.shortcutService.listen(this.shortcut, () => {
const target = this.element.nativeElement;
if (target && !target.disabled) {
try {
target[this.shortcutAction]();
} catch {
// NOOP
}
}
});
}
}
}