| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import { Component, Input } from '@angular/core'
- import { trigger, transition, style, animate } from '@angular/animations'
- import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
- import { Subscription } from 'rxjs'
- import { HotkeysService } from 'terminus-core'
- const INPUT_TIMEOUT = 1000
- /** @hidden */
- @Component({
- selector: 'hotkey-input-modal',
- template: require('./hotkeyInputModal.component.pug'),
- styles: [require('./hotkeyInputModal.component.scss')],
- animations: [
- trigger('animateKey', [
- transition(':enter', [
- style({
- transform: 'translateX(25px)',
- opacity: '0',
- }),
- animate('250ms ease-out', style({
- transform: 'translateX(0)',
- opacity: '1',
- }))
- ]),
- transition(':leave', [
- style({
- transform: 'translateX(0)',
- opacity: '1',
- }),
- animate('250ms ease-in', style({
- transform: 'translateX(25px)',
- opacity: '0',
- }))
- ])
- ])
- ]
- })
- export class HotkeyInputModalComponent {
- @Input() value: string[] = []
- @Input() timeoutProgress = 0
- private keySubscription: Subscription
- private lastKeyEvent: number
- private keyTimeoutInterval: number = null
- constructor (
- private modalInstance: NgbActiveModal,
- public hotkeys: HotkeysService,
- ) {
- this.hotkeys.clearCurrentKeystrokes()
- this.keySubscription = hotkeys.key.subscribe((event) => {
- this.lastKeyEvent = performance.now()
- this.value = this.hotkeys.getCurrentKeystrokes()
- event.preventDefault()
- event.stopPropagation()
- })
- }
- splitKeys (keys: string): string[] {
- return keys.split('+').map((x) => x.trim())
- }
- ngOnInit () {
- this.keyTimeoutInterval = window.setInterval(() => {
- if (!this.lastKeyEvent) {
- return
- }
- this.timeoutProgress = Math.min(100, (performance.now() - this.lastKeyEvent) * 100 / INPUT_TIMEOUT)
- if (this.timeoutProgress === 100) {
- this.modalInstance.close(this.value)
- }
- }, 25)
- this.hotkeys.disable()
- }
- ngOnDestroy () {
- this.keySubscription.unsubscribe()
- this.hotkeys.clearCurrentKeystrokes()
- this.hotkeys.enable()
- clearInterval(this.keyTimeoutInterval)
- }
- close () {
- this.modalInstance.dismiss()
- }
- }
|