hotkeyInputModal.component.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Component, Input } from '@angular/core'
  2. import { trigger, transition, style, animate } from '@angular/animations'
  3. import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
  4. import { Subscription } from 'rxjs'
  5. import { HotkeysService } from 'terminus-core'
  6. const INPUT_TIMEOUT = 1000
  7. /** @hidden */
  8. @Component({
  9. selector: 'hotkey-input-modal',
  10. template: require('./hotkeyInputModal.component.pug'),
  11. styles: [require('./hotkeyInputModal.component.scss')],
  12. animations: [
  13. trigger('animateKey', [
  14. transition(':enter', [
  15. style({
  16. transform: 'translateX(25px)',
  17. opacity: '0',
  18. }),
  19. animate('250ms ease-out', style({
  20. transform: 'translateX(0)',
  21. opacity: '1',
  22. }))
  23. ]),
  24. transition(':leave', [
  25. style({
  26. transform: 'translateX(0)',
  27. opacity: '1',
  28. }),
  29. animate('250ms ease-in', style({
  30. transform: 'translateX(25px)',
  31. opacity: '0',
  32. }))
  33. ])
  34. ])
  35. ]
  36. })
  37. export class HotkeyInputModalComponent {
  38. @Input() value: string[] = []
  39. @Input() timeoutProgress = 0
  40. private keySubscription: Subscription
  41. private lastKeyEvent: number
  42. private keyTimeoutInterval: number = null
  43. constructor (
  44. private modalInstance: NgbActiveModal,
  45. public hotkeys: HotkeysService,
  46. ) {
  47. this.hotkeys.clearCurrentKeystrokes()
  48. this.keySubscription = hotkeys.key.subscribe((event) => {
  49. this.lastKeyEvent = performance.now()
  50. this.value = this.hotkeys.getCurrentKeystrokes()
  51. event.preventDefault()
  52. event.stopPropagation()
  53. })
  54. }
  55. splitKeys (keys: string): string[] {
  56. return keys.split('+').map((x) => x.trim())
  57. }
  58. ngOnInit () {
  59. this.keyTimeoutInterval = window.setInterval(() => {
  60. if (!this.lastKeyEvent) {
  61. return
  62. }
  63. this.timeoutProgress = Math.min(100, (performance.now() - this.lastKeyEvent) * 100 / INPUT_TIMEOUT)
  64. if (this.timeoutProgress === 100) {
  65. this.modalInstance.close(this.value)
  66. }
  67. }, 25)
  68. this.hotkeys.disable()
  69. }
  70. ngOnDestroy () {
  71. this.keySubscription.unsubscribe()
  72. this.hotkeys.clearCurrentKeystrokes()
  73. this.hotkeys.enable()
  74. clearInterval(this.keyTimeoutInterval)
  75. }
  76. close () {
  77. this.modalInstance.dismiss()
  78. }
  79. }