index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { Effect, ModuleControl } from "queenjs";
  2. import { EditorModule } from "../../module";
  3. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  4. import { Matrix } from "../SelectCtrl/matrix";
  5. import {ref, onMounted, nextTick} from "vue";
  6. import { RxValue } from "../ReactCtrl/rxValue";
  7. import { DisplayObject } from "../SelectCtrl/objects/displayObject";
  8. import { ObjsContainer } from "../SelectCtrl/ObjsContainer";
  9. import { settingsStore } from "@queenjs-modules/queditor/module/stores/settings";
  10. const KeySpace = 32;
  11. export class EditorCtrl extends ModuleControl<EditorModule> {
  12. state = RxValue.create({
  13. width: 1500,
  14. height: 1000,
  15. pos: {
  16. x: 0, y:0
  17. },
  18. scale: 1,
  19. keyCode: -1,
  20. page: {w: 375, h: 650}
  21. })
  22. doms:any = {};
  23. useEditor() {
  24. const EditorRef = ref<HTMLElement>();
  25. const ParentRef = ref<HTMLElement>();
  26. const PageRef = ref<HTMLElement>();
  27. const CanvasRef = ref<HTMLElement>();
  28. const init = () => {
  29. if (!EditorRef.value || !CanvasRef.value || !ParentRef.value) return;
  30. console.log("xxxxxxxx=>" , ParentRef.value, PageRef.value);
  31. EditorRef.value.style.width = this.state.width + "px";
  32. EditorRef.value.style.height = this.state.height + "px";
  33. EditorRef.value.style.position = "absolute";
  34. // EditorRef.value.style.backgroundColor = "greenyellow";
  35. EditorRef.value.style.left = "0";
  36. EditorRef.value.style.top = "0";
  37. CanvasRef.value.style.width = "100%"
  38. CanvasRef.value.style.height = "100%"
  39. EditorRef.value.style.position = "absolute";
  40. this.initEditorEvent(EditorRef.value, ParentRef.value as any);
  41. }
  42. onMounted(()=>{
  43. setTimeout(init, 200);
  44. })
  45. this.doms = {editor:EditorRef, parent: ParentRef, page:PageRef, canvas: CanvasRef};
  46. return this.doms;
  47. }
  48. isKeyDown( key:number) {
  49. return this.state.keyCode === key;
  50. }
  51. isMoving() {
  52. return this.isKeyDown(KeySpace);
  53. }
  54. initEditorEvent(editorLayer:HTMLElement, parent:HTMLElement) {
  55. // 监听键盘的 keydown 事件
  56. document.addEventListener("keydown", (event)=>{
  57. this.state.keyCode = event.keyCode;
  58. });
  59. // 监听键盘的 keyup 事件
  60. document.addEventListener("keyup", (event)=>{
  61. this.state.keyCode = -1;
  62. isMoving = false;
  63. });
  64. parent.addEventListener("wheel", (event:any)=>{
  65. // 检查滚轮方向
  66. let delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)))
  67. console.log( delta );
  68. let s = this.state.scale + 0.1 * delta
  69. if ( s < 0.5 ) s = 0.5;
  70. if (s > 2) s = 2.0;
  71. this.state.setScale( s)
  72. });
  73. let isMoving = false;
  74. let moveX = 0, moveY = 0;
  75. parent.addEventListener("mousemove",(e:MouseEvent)=>{
  76. //this.state.setMouse({x: e.offsetX, y: e.offsetY});
  77. if (isMoving) {
  78. const xOffset = e.pageX - moveX;
  79. const yOffset = e.pageY - moveY;
  80. console.log(this.state.pos, xOffset, yOffset);
  81. this.state.setPos({x: this.state.pos.x + xOffset, y: this.state.pos.y + yOffset});
  82. }
  83. moveX = e.pageX;
  84. moveY = e.pageY;
  85. });
  86. document.addEventListener("mouseup",(e:MouseEvent)=>{
  87. isMoving = false;
  88. })
  89. this.state.onScaleChanged((v)=>{
  90. editorLayer.style.transformOrigin = "50% 50%";
  91. editorLayer.style.transform = `scale(${v})`;
  92. })
  93. this.state.onPosChanged((v)=>{
  94. editorLayer.style.left= v.x + "px";
  95. editorLayer.style.top = v.y + "px";
  96. })
  97. parent.addEventListener("mousedown", (e:MouseEvent)=>{
  98. isMoving = this.isKeyDown(KeySpace)
  99. if (isMoving) {
  100. editorLayer.focus();
  101. }
  102. moveX = e.pageX;
  103. moveY = e.pageY;
  104. })
  105. window.addEventListener("resize", ()=>this.updateEditorSize());
  106. this.state.onPageChanged(()=>this.updateCardSize());
  107. setTimeout(() => {
  108. this.updateEditorSize();
  109. }, 100);
  110. }
  111. updateCardSize() {
  112. const page = this.doms.page.value as HTMLElement;
  113. const state = this.state;
  114. page.style.left = (state.width - state.page.w) / 2.0 + "px";
  115. page.style.top = (state.height - state.page.h) / 2.0 + "px";
  116. console.log("size");
  117. this.updateEditorSize();
  118. }
  119. updateEditorSize() {
  120. const state = this.state;
  121. const parent = this.doms.parent.value as HTMLElement;
  122. const left = ( parent.clientWidth - state.width) / 2.0;
  123. const top = (parent.clientHeight - state.height) / 2.0;
  124. this.state.setPos({x: left, y: top});
  125. }
  126. }