index.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.actions.pickComp("");
  16. },
  17. },
  18. // 切换到父组件
  19. {
  20. hotKey: "ctrl+up",
  21. action() {
  22. this.actions.pickParentComp(this.store.currCompId);
  23. },
  24. },
  25. {
  26. hotKey: "ctrl+c",
  27. action() {
  28. this.actions.ctrlc();
  29. },
  30. },{
  31. hotKey: "ctrl+g",
  32. action() {
  33. this.actions.toogleGroup();
  34. },
  35. },
  36. {
  37. hotKey: "ctrl+v",
  38. action() {
  39. this.actions.ctrlv();
  40. },
  41. },
  42. // 删除当前组件
  43. {
  44. hotKey: "Backspace,del",
  45. action() {
  46. this.actions.removeComp(this.store.currCompId);
  47. },
  48. },
  49. {
  50. hotKey: "q,w,a,s,d,e",
  51. action(key) {
  52. // this.actions.removeComp(this.store.currCompId);
  53. // console.log("image hot key down", key);
  54. this.actions.handleImageHotKey(key);
  55. },
  56. },
  57. // 回退
  58. {
  59. hotKey: "ctrl+z",
  60. action() {
  61. if (!this.controls.historyCtrl.state.enable) return;
  62. this.controls.historyCtrl.history.undo();
  63. },
  64. },
  65. // 重做
  66. {
  67. hotKey: "ctrl+shift+z",
  68. action() {
  69. if (!this.controls.historyCtrl.state.enable) return;
  70. this.controls.historyCtrl.history.redo();
  71. },
  72. }, //移动
  73. {
  74. hotKey: "up,down,left,right",
  75. action(key) {
  76. console.log("move key down", key);
  77. this.actions.handleSelectMoving(key);
  78. },
  79. },
  80. ]);
  81. init() {
  82. const { module, hotKeys } = this;
  83. hotkeys(
  84. hotKeys.map((d) => d.hotKey).join(","),
  85. module.moduleName,
  86. function (event, handler) {
  87. event.preventDefault();
  88. const hotAct = hotKeys.find((d) =>
  89. d.hotKey.split(",").includes(handler.key)
  90. );
  91. hotAct?.action.call(module, handler.key);
  92. }
  93. );
  94. hotkeys.setScope(module.moduleName);
  95. }
  96. destroy() {
  97. hotkeys.deleteScope(this.module.moduleName);
  98. }
  99. defineHotKeys<T extends IHotKeyItem[]>(hotKeys: T) {
  100. return hotKeys;
  101. }
  102. }