From a7164e0332e47877547d569afe3ea87a7822f9df Mon Sep 17 00:00:00 2001 From: luoer Date: Mon, 23 Oct 2023 17:23:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BC=96=E8=BE=91=E5=99=A8=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=B9=B3=E7=A7=BB=E5=92=8C=E7=BC=A9=E6=94=BE=E5=AE=B9?= =?UTF-8?q?=E5=99=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/editor/config/editor.ts | 15 ++++ src/components/editor/config/index.ts | 2 +- src/components/editor/config/scene.ts | 65 ++++++++++++++ src/components/editor/index.vue | 5 +- .../editor/panel-main/components/block.vue | 6 +- src/components/editor/panel-main/index.vue | 85 +------------------ 6 files changed, 92 insertions(+), 86 deletions(-) create mode 100644 src/components/editor/config/editor.ts create mode 100644 src/components/editor/config/scene.ts diff --git a/src/components/editor/config/editor.ts b/src/components/editor/config/editor.ts new file mode 100644 index 0000000..9736b41 --- /dev/null +++ b/src/components/editor/config/editor.ts @@ -0,0 +1,15 @@ +import { Ref } from "vue"; +import { Container } from "./container"; +import { Block } from "./block"; + +/** + * TODO + */ +export class Editor { + public container: Ref = {} as Ref; + public content: Ref = {} as Ref; + + constructor() { + // TODO + } +} diff --git a/src/components/editor/config/index.ts b/src/components/editor/config/index.ts index 4973e54..8791e41 100644 --- a/src/components/editor/config/index.ts +++ b/src/components/editor/config/index.ts @@ -3,4 +3,4 @@ export * from "./blocker"; export * from "./container"; export * from "./context"; export * from "./ref-line"; - +export * from "./scene"; diff --git a/src/components/editor/config/scene.ts b/src/components/editor/config/scene.ts new file mode 100644 index 0000000..85b97fb --- /dev/null +++ b/src/components/editor/config/scene.ts @@ -0,0 +1,65 @@ +import { Ref } from "vue"; +import { Container } from "."; + +/** + * 场景 + * @description 处理平移和缩放事件 + */ +export class Scene { + private startX = 0; + private startY = 0; + private cacheX = 0; + private cacheY = 0; + public minZoom = 0.5; + public maxZoom = 10; + public zoomStep = 0.1; + + constructor(private container: Ref) { + this.onMouseDown = this.onMouseDown.bind(this); + this.onMouseMove = this.onMouseMove.bind(this); + this.onMouseUp = this.onMouseUp.bind(this); + this.onMouseWheel = this.onMouseWheel.bind(this); + } + + onMouseDown(e: MouseEvent) { + this.startX = e.x; + this.startY = e.y; + this.cacheX = this.container.value.x; + this.cacheY = this.container.value.y; + window.addEventListener("mousemove", this.onMouseMove); + window.addEventListener("mouseup", this.onMouseUp); + } + + onMouseMove(e: MouseEvent) { + this.container.value.x = this.cacheX + (e.x - this.startX); + this.container.value.y = this.cacheY + (e.y - this.startY); + } + + onMouseUp() { + window.removeEventListener("mousemove", this.onMouseMove); + window.removeEventListener("mouseup", this.onMouseUp); + } + + onMouseWheel(e: WheelEvent) { + e.preventDefault(); + + const el = e.currentTarget as HTMLElement; + const rect = el.getBoundingClientRect(); + const x = (e.clientX - rect.x) / this.container.value.zoom; + const y = (e.clientY - rect.y) / this.container.value.zoom; + const delta = -e.deltaY > 0 ? this.zoomStep : -this.zoomStep; + + this.container.value.zoom += delta; + if (this.container.value.zoom < this.minZoom) { + this.container.value.zoom = this.minZoom; + return; + } + if (this.container.value.zoom > this.maxZoom) { + this.container.value.zoom = this.maxZoom; + return; + } + + this.container.value.x += -x * delta + el.offsetWidth * (delta / 2); + this.container.value.y += -y * delta + el.offsetHeight * (delta / 2); + } +} diff --git a/src/components/editor/index.vue b/src/components/editor/index.vue index d3efb3a..0192809 100644 --- a/src/components/editor/index.vue +++ b/src/components/editor/index.vue @@ -1,6 +1,6 @@