index.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { ModuleControl } from "queenjs";
  2. import { EditorModule } from "../../module";
  3. type sourceType = {
  4. _id?: string;
  5. file?: {
  6. url?: string;
  7. };
  8. };
  9. export class DragAddCtrl extends ModuleControl<EditorModule> {
  10. dragingCompKey = "";
  11. dragingCompData: sourceType = {};
  12. _cancel?: () => void;
  13. _mouseUping = false;
  14. updateCompKey(k: string) {
  15. if (this._mouseUping) return;
  16. const isSame = k == this.dragingCompKey;
  17. this.dragingCompKey = k;
  18. if (!isSame && k) {
  19. if (this._cancel) this._cancel();
  20. this._cancel = this.initEvent();
  21. }
  22. }
  23. updateCompData(data: object) {
  24. this.dragingCompData = data;
  25. }
  26. async dragComp(e: MouseEvent) {
  27. const scope = this;
  28. await scope.actions.dragCompToDesign(e, scope.dragingCompKey as any, scope.dragingCompData);
  29. if (
  30. scope.dragingCompData?._id &&
  31. (scope.dragingCompKey == "Image" || scope.dragingCompKey == "Video")
  32. ) {
  33. scope.store.currComp.value.url = scope.dragingCompData?.file?.url;
  34. scope.dragingCompData = {};
  35. }
  36. scope.dragingCompKey = "";
  37. }
  38. async dragTpl() {
  39. await this.actions.clickFrameToDesign(this.dragingCompData);
  40. this.dragingCompKey = "";
  41. }
  42. initEvent() {
  43. const scope = this;
  44. async function mouseup(e: MouseEvent) {
  45. console.log("mouseup=>", scope.dragingCompKey);
  46. scope._mouseUping = true;
  47. setTimeout(() => {
  48. scope._mouseUping = false;
  49. }, 1000);
  50. if (scope._cancel) scope._cancel();
  51. if (!scope.dragingCompKey) return;
  52. if (scope.dragingCompKey == "tpl") {
  53. scope.dragTpl();
  54. } else {
  55. scope.dragComp(e);
  56. }
  57. }
  58. document.addEventListener("mouseup", mouseup);
  59. return () => {
  60. document.removeEventListener("mouseup", mouseup);
  61. scope._cancel = undefined;
  62. };
  63. }
  64. }