index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import hotkeys from "hotkeys-js";
  2. import { ModuleControl } from "queenjs";
  3. import { EditorModule } from "../../module";
  4. type IHotKeyItem = {
  5. hotKey: string;
  6. action: (this: EditorModule, key: string) => void;
  7. };
  8. export class HotKeyCtrl extends ModuleControl<EditorModule> {
  9. // 热键配置
  10. hotKeys = this.defineHotKeys([
  11. // 取消选中
  12. {
  13. hotKey: "esc",
  14. action() {
  15. this.controls.editorCtrl.clickPickComp("");
  16. },
  17. },
  18. // 切换到父组件
  19. {
  20. hotKey: "ctrl+up",
  21. action() {
  22. this.actions.pickParentComp(this.store.currCompId);
  23. },
  24. },{
  25. hotKey: "ctrl+q",
  26. action() {
  27. this.actions.ctrlq();
  28. },
  29. },
  30. {
  31. hotKey: "ctrl+c",
  32. action() {
  33. this.actions.ctrlc();
  34. },
  35. },{
  36. hotKey: "ctrl+g",
  37. action() {
  38. this.actions.toogleGroup();
  39. },
  40. },
  41. {
  42. hotKey: "ctrl+v",
  43. action() {
  44. this.actions.ctrlv();
  45. },
  46. },
  47. {
  48. hotKey: "ctrl+x",
  49. action() {
  50. this.actions.ctrlx();
  51. },
  52. },
  53. // 删除当前组件
  54. {
  55. hotKey: "Backspace,del",
  56. action() {
  57. if (this.controls.selectCtrl.gizmo.selected.length > 1) {
  58. this.actions.removeSelectComps();
  59. return;
  60. }
  61. this.actions.removeComp(this.store.currCompId);
  62. },
  63. },
  64. {
  65. hotKey: "q,w,a,s,d,e",
  66. action(key) {
  67. // this.actions.removeComp(this.store.currCompId);
  68. // console.log("image hot key down", key);
  69. this.actions.handleImageHotKey(key);
  70. },
  71. },
  72. // 回退
  73. {
  74. hotKey: "ctrl+z",
  75. action() {
  76. // if (!this.controls.historyCtrl.state.enable) return;
  77. // this.controls.historyCtrl.history.undo();
  78. this.controls.selectCtrl.gizmo.history.undo();
  79. },
  80. },
  81. // 重做
  82. {
  83. hotKey: "ctrl+shift+z",
  84. action() {
  85. if (!this.controls.historyCtrl.state.enable) return;
  86. this.controls.historyCtrl.history.redo();
  87. },
  88. }, //移动
  89. {
  90. hotKey: "up,down,left,right",
  91. action(key) {
  92. console.log("move key down", key);
  93. this.actions.handleSelectMoving(key);
  94. },
  95. },
  96. ]);
  97. init() {
  98. const { module, hotKeys } = this;
  99. document.addEventListener('keydown',(event) =>{
  100. // 判断按下的键是否是 Ctrl 键和 A 键
  101. if (event.ctrlKey && event.key === 'a') {
  102. event.preventDefault();
  103. event.stopPropagation();
  104. // 文字编辑状态,不做操作
  105. if(this.controls.textEditorCtrl.state.currEditor) return
  106. this.actions.ctrlAndA();
  107. }
  108. });
  109. hotkeys(
  110. hotKeys.map((d) => d.hotKey).join(","),
  111. module.moduleName,
  112. function (event, handler) {
  113. event.preventDefault();
  114. const hotAct = hotKeys.find((d) =>
  115. d.hotKey.split(",").includes(handler.key)
  116. );
  117. hotAct?.action.call(module, handler.key);
  118. }
  119. );
  120. hotkeys.setScope(module.moduleName);
  121. }
  122. destroy() {
  123. hotkeys.deleteScope(this.module.moduleName);
  124. }
  125. defineHotKeys<T extends IHotKeyItem[]>(hotKeys: T) {
  126. return hotKeys;
  127. }
  128. }