hotkeySettingsTab.component.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  2. import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'
  3. import { Component, NgZone } from '@angular/core'
  4. import {
  5. ConfigService,
  6. Hotkey,
  7. HotkeyDescription,
  8. HotkeysService,
  9. HostAppService,
  10. } from 'tabby-core'
  11. _('Search hotkeys')
  12. /** @hidden */
  13. @Component({
  14. selector: 'hotkey-settings-tab',
  15. templateUrl:'./hotkeySettingsTab.component.pug',
  16. })
  17. export class HotkeySettingsTabComponent {
  18. hotkeyFilter = ''
  19. hotkeyDescriptions: HotkeyDescription[]
  20. allDuplicateHotkeys = this.getAllDuplicateHotkeys()
  21. constructor (
  22. public config: ConfigService,
  23. public hostApp: HostAppService,
  24. public zone: NgZone,
  25. hotkeys: HotkeysService,
  26. ) {
  27. hotkeys.getHotkeyDescriptions().then(descriptions => {
  28. this.hotkeyDescriptions = descriptions
  29. })
  30. }
  31. getHotkeys (id: string): Hotkey[] {
  32. let ptr = this.config.store.hotkeys
  33. for (const token of id.split(/\./g)) {
  34. ptr = ptr[token]
  35. }
  36. return (ptr || []).map(hotkey => this.detectDuplicates(hotkey))
  37. }
  38. setHotkeys (id: string, hotkeys: Hotkey[]) {
  39. let ptr = this.config.store
  40. let prop = 'hotkeys'
  41. for (const token of id.split(/\./g)) {
  42. ptr = ptr[prop]
  43. prop = token
  44. }
  45. ptr[prop] = hotkeys.map(hotkey =>
  46. hotkey.strokes.length === 1 && Array.isArray(hotkey.strokes)
  47. ? hotkey.strokes[0]
  48. : hotkey.strokes,
  49. )
  50. this.config.save()
  51. this.allDuplicateHotkeys = this.getAllDuplicateHotkeys()
  52. }
  53. hotkeyFilterFn (hotkey: HotkeyDescription, query: string): boolean {
  54. const s = hotkey.name + hotkey.id + this.getHotkeys(hotkey.id).map(h => h.strokes).toString()
  55. return s.toLowerCase().includes(query.toLowerCase())
  56. }
  57. private getAllDuplicateHotkeys (): string[] {
  58. const allHotkeys = Object
  59. .values(this.config.store.hotkeys)
  60. .filter((value: unknown) => Array.isArray(value))
  61. .flat()
  62. .map((hotkey: string | string[]) => this.toHotkeyIdentifier(hotkey))
  63. return allHotkeys.filter(hotkey => allHotkeys.indexOf(hotkey) !== allHotkeys.lastIndexOf(hotkey))
  64. }
  65. private detectDuplicates (strokes: string[] | string): Hotkey {
  66. const hotkeyIdentifier = this.toHotkeyIdentifier(strokes)
  67. const isDuplicate = this.allDuplicateHotkeys.includes(hotkeyIdentifier)
  68. return { strokes, isDuplicate }
  69. }
  70. private toHotkeyIdentifier (hotkey: string[] | string): string {
  71. return Array.isArray(hotkey) ? hotkey.join('$#!') : hotkey
  72. }
  73. }