index.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. {
  27. hotKey: "Backspace,del",
  28. action() {
  29. this.actions.removeComp(this.store.currCompId);
  30. },
  31. },
  32. {
  33. hotKey: "q,w,a,s,d,e",
  34. action(key) {
  35. // this.actions.removeComp(this.store.currCompId);
  36. // console.log("image hot key down", key);
  37. this.actions.handleImageHotKey(key);
  38. },
  39. },
  40. // 回退
  41. {
  42. hotKey: "ctrl+z",
  43. action() {
  44. this.controls.historyCtrl.history.undo();
  45. },
  46. },
  47. // 重做
  48. {
  49. hotKey: "ctrl+shift+z",
  50. action() {
  51. this.controls.historyCtrl.history.redo();
  52. },
  53. },
  54. ]);
  55. init() {
  56. const { module, hotKeys } = this;
  57. hotkeys(
  58. hotKeys.map((d) => d.hotKey).join(","),
  59. module.moduleName,
  60. function (event, handler) {
  61. event.preventDefault();
  62. const hotAct = hotKeys.find((d) =>
  63. d.hotKey.split(",").includes(handler.key)
  64. );
  65. hotAct?.action.call(module, handler.key);
  66. }
  67. );
  68. hotkeys.setScope(module.moduleName);
  69. }
  70. destroy() {
  71. hotkeys.deleteScope(this.module.moduleName);
  72. }
  73. defineHotKeys<T extends IHotKeyItem[]>(hotKeys: T) {
  74. return hotKeys;
  75. }
  76. }