From 121ba8199393fda3327e436de4e9f4940a151f02 Mon Sep 17 00:00:00 2001 From: Carlos Date: Sun, 23 Mar 2025 20:42:17 -0700 Subject: [PATCH] feat: enhance drag event handling with structured event properties and cleanup unused code --- packages/core/src/commands/config/config.ts | 20 ++----- packages/core/src/commands/index.ts | 1 + .../core/src/commands/view/ComponentDrag.ts | 58 ++++++++++--------- 3 files changed, 38 insertions(+), 41 deletions(-) diff --git a/packages/core/src/commands/config/config.ts b/packages/core/src/commands/config/config.ts index 77976070a..4daab89b0 100644 --- a/packages/core/src/commands/config/config.ts +++ b/packages/core/src/commands/config/config.ts @@ -61,21 +61,13 @@ const config: () => CommandsConfig = () => ({ 'core:component-drag': { run: (options: CommandOptions) => ({ ...options, - addStyle: () => { - return { - showPercentages: true, - }; - // target.addStyle({ opacity: 0.5 }); - // // TODO: is this the best way to do this? - // const lineElement = em.view?.el.querySelector('.gjs-guide-info__line') as HTMLElement | null; - // if (lineElement) lineElement.style.backgroundColor = 'green'; - // const contentElement = em.view?.el.querySelector('.gjs-guide-info__content') as HTMLElement | null; - // if (contentElement) contentElement.style.color = 'green'; - // return { - // guideInfoLine: { backgroundColor: 'green' }, - // guideInfoContent: { color: 'green' }, - // }; + customElements: { + guideIndicator: false, }, + // addStyle: ({ componentView, matchedEl }) => { + // // TODO: test if you can conver to percentage + // // componentView.setStyle({ opacity: 0.5 }); + // }, }), }, }, diff --git a/packages/core/src/commands/index.ts b/packages/core/src/commands/index.ts index a43f88b4f..eba5f1740 100644 --- a/packages/core/src/commands/index.ts +++ b/packages/core/src/commands/index.ts @@ -388,6 +388,7 @@ export default class CommandsModule extends Module Record; }; +// TODO: should we export this type? and if so, we should create 1 type for every event? +export type DragEventProps = { + coords?: { x: number; y: number }; + matchedEl?: Component; + lastMatchedEl?: Component; + elGuideInfo?: HTMLElement; + elGuideInfoCnt?: HTMLElement; + size?: number; + sizePercent?: number; +}; + const evName = 'dmode'; export default { @@ -73,6 +83,7 @@ export default { this.guidesContainer = this.getGuidesContainer(); this.guidesTarget = this.getGuidesTarget(); this.guidesStatic = this.getGuidesStatic(); + this.lastMatchedEl = null; let drg = this.dragger; if (!drg) { @@ -380,20 +391,13 @@ export default { }); } - this.editor.trigger(`${evName}:drag:start`, { - coords: { x: 0, y: 0 }, // TODO: pass the real coords - targetEl: event.target, - }); + const dragStartProps: DragEventProps = { matchedEl: target }; - this.em.Canvas.addSpot({ - type: CanvasSpotBuiltInTypes.Drag, - component: target, - componentView: target.view, - }); + this.editor.trigger(`${evName}:drag:start`, dragStartProps); }, onDrag(mouseEvent: MouseEvent, _dragger: Dragger) { - const { guidesTarget, opts } = this; + const { guidesTarget, lastMatchedEl, opts } = this; let guideNearElements = []; this.updateGuides(guidesTarget); @@ -404,37 +408,37 @@ export default { const { x, y } = mouseEvent; const guideNearElement = guideNearElements[0] ?? {}; const { size, sizePercent, matched, elGuideInfo, elGuideInfoCnt } = guideNearElement; - const matchedEl = matched?.origin; - // TODO: revisit event props - this.editor.trigger(`${evName}:drag:move`, { + let matchedEl = undefined; + + if (matched?.origin) { + matchedEl = matched.origin; + if (matchedEl !== lastMatchedEl) this.lastMatchedEl = matchedEl; + } + + const dragMoveProps: DragEventProps = { coords: { x, y }, matchedEl, + lastMatchedEl, elGuideInfo, elGuideInfoCnt, size, sizePercent, - }); + }; - // TODO: add the spot to the canvas to show the origin? - // if (matchedEl) { - // this.em.Canvas.addSpot({ - // type: CanvasSpotBuiltInTypes.Drag, - // }); - // } + this.editor.trigger(`${evName}:drag:move`, dragMoveProps); }, onEnd(ev: Event, _dragger: Dragger, opt = {}) { - const { editor, opts, id } = this; + const { editor, opts, id, lastMatchedEl } = this; opts?.onEnd?.(ev, opt, { event: ev, ...opt, ...this._getDragData() }); editor.stopCommand(id); this.hideGuidesInfo(); this.em.trigger(`${evName}:end`, this.getEventOpts()); - this.em.trigger(`${evName}:drag:end`, undefined); - this.em.Canvas.removeSpots({ - type: CanvasSpotBuiltInTypes.Drag, - component: this.target, - }); + + const dragEndProps: DragEventProps = { lastMatchedEl }; + + this.em.trigger(`${evName}:drag:end`, dragEndProps); }, hideGuidesInfo() {