KeyCommand.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { keyMap } from './utils/keyMap'
  2. // 快捷按键、命令处理类
  3. export default class KeyCommand {
  4. constructor(app) {
  5. this.app = app
  6. this.keyMap = keyMap
  7. this.shortcutMap = {
  8. //Enter: [fn]
  9. }
  10. this.bindEvent()
  11. }
  12. // 绑定事件
  13. bindEvent() {
  14. this.app.event.on('keydown', this.onKeydown, this)
  15. }
  16. // 解绑事件
  17. unBindEvent() {
  18. this.app.event.off('keydown', this.onKeydown)
  19. }
  20. // 按键事件
  21. onKeydown(e) {
  22. Object.keys(this.shortcutMap).forEach(key => {
  23. if (this.checkKey(e, key)) {
  24. e.stopPropagation()
  25. e.preventDefault()
  26. this.shortcutMap[key].forEach(f => {
  27. f.fn.call(f.ctx)
  28. })
  29. }
  30. })
  31. }
  32. // 检查键值是否符合
  33. checkKey(e, key) {
  34. let o = this.getOriginEventCodeArr(e)
  35. let k = this.getKeyCodeArr(key)
  36. if (o.length !== k.length) {
  37. return false
  38. }
  39. for (let i = 0; i < o.length; i++) {
  40. let index = k.findIndex(item => {
  41. return item === o[i]
  42. })
  43. if (index === -1) {
  44. return false
  45. } else {
  46. k.splice(index, 1)
  47. }
  48. }
  49. return true
  50. }
  51. // 获取事件对象里的键值数组
  52. getOriginEventCodeArr(e) {
  53. let arr = []
  54. if (e.ctrlKey || e.metaKey) {
  55. arr.push(keyMap['Control'])
  56. }
  57. if (e.altKey) {
  58. arr.push(keyMap['Alt'])
  59. }
  60. if (e.shiftKey) {
  61. arr.push(keyMap['Shift'])
  62. }
  63. if (!arr.includes(e.keyCode)) {
  64. arr.push(e.keyCode)
  65. }
  66. return arr
  67. }
  68. // 获取快捷键对应的键值数组
  69. getKeyCodeArr(key) {
  70. // 对xxx++情况特殊处理
  71. key = key.replace(/\+\+/, '+add')
  72. let keyArr = key.split(/\s*\+\s*/).map(item => {
  73. return item === 'add' ? '+' : item
  74. })
  75. let arr = []
  76. keyArr.forEach(item => {
  77. arr.push(keyMap[item])
  78. })
  79. return arr
  80. }
  81. /**
  82. * 添加快捷键命令
  83. * Enter
  84. * Tab | Insert
  85. * Shift + a
  86. */
  87. addShortcut(key, fn, ctx) {
  88. key.split(/\s*\|\s*/).forEach(item => {
  89. if (this.shortcutMap[item]) {
  90. this.shortcutMap[item].push({
  91. fn,
  92. ctx
  93. })
  94. } else {
  95. this.shortcutMap[item] = [
  96. {
  97. fn,
  98. ctx
  99. }
  100. ]
  101. }
  102. })
  103. }
  104. // 移除快捷键命令
  105. removeShortcut(key, fn) {
  106. key.split(/\s*\|\s*/).forEach(item => {
  107. if (this.shortcutMap[item]) {
  108. if (fn) {
  109. let index = this.shortcutMap[item].findIndex(f => {
  110. return f.fn === fn
  111. })
  112. if (index !== -1) {
  113. this.shortcutMap[item].splice(index, 1)
  114. }
  115. } else {
  116. this.shortcutMap[item] = []
  117. delete this.shortcutMap[item]
  118. }
  119. }
  120. })
  121. }
  122. }