index.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import hotkeys from "hotkeys-js";
  2. import { ModuleControl } from "queenjs";
  3. import { EditorModule } from "../..";
  4. type IHotKeyItem = { hotKey: string; action: (this: EditorModule, key:string) => void };
  5. export class HotKeyCtrl extends ModuleControl<EditorModule> {
  6. // 热键配置
  7. hotKeys = this.defineHotKeys([
  8. // 切换到父组件
  9. {
  10. hotKey: "ctrl+up",
  11. action() {
  12. this.actions.pickParentComp(this.store.currCompId);
  13. },
  14. },
  15. // 删除当前组件
  16. {
  17. hotKey: "Backspace,del",
  18. action(key:string) {
  19. this.actions.removeComp(this.store.currCompId);
  20. },
  21. },
  22. {
  23. hotKey: "q,w,a,s,d,e",
  24. action(key:string) {
  25. // this.actions.removeComp(this.store.currCompId);
  26. // console.log("image hot key down", key);
  27. this.actions.handleImageHotKey(key)
  28. },
  29. },
  30. ]);
  31. init() {
  32. const { module, hotKeys } = this;
  33. hotkeys(
  34. hotKeys.map((d) => d.hotKey).join(","),
  35. module.moduleName,
  36. function (event, handler) {
  37. event.preventDefault();
  38. const hotAct = hotKeys.find((d) =>
  39. d.hotKey.split(",").includes(handler.key)
  40. );
  41. hotAct?.action.call(module, handler.key);
  42. }
  43. );
  44. hotkeys.setScope(module.moduleName);
  45. }
  46. destroy() {
  47. hotkeys.deleteScope(this.module.moduleName);
  48. }
  49. defineHotKeys<T extends IHotKeyItem[]>(hotKeys: T) {
  50. return hotKeys;
  51. }
  52. }