Browse Source

feat: enhance drag event handling with structured event properties and cleanup unused code

carlos/505-improve-grapesjs-absolute-mode
Carlos 11 months ago
parent
commit
121ba81993
  1. 20
      packages/core/src/commands/config/config.ts
  2. 1
      packages/core/src/commands/index.ts
  3. 58
      packages/core/src/commands/view/ComponentDrag.ts

20
packages/core/src/commands/config/config.ts

@ -61,21 +61,13 @@ const config: () => CommandsConfig = () => ({
'core:component-drag': { 'core:component-drag': {
run: (options: CommandOptions) => ({ run: (options: CommandOptions) => ({
...options, ...options,
addStyle: () => { customElements: {
return { guideIndicator: false,
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' },
// };
}, },
// addStyle: ({ componentView, matchedEl }) => {
// // TODO: test if you can conver to percentage
// // componentView.setStyle({ opacity: 0.5 });
// },
}), }),
}, },
}, },

1
packages/core/src/commands/index.ts

@ -388,6 +388,7 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
const id = command.id as string; const id = command.id as string;
const editor = em.Editor; const editor = em.Editor;
// TODO: add test
if (!this.isActive(id) || options.force || !config.strict) { if (!this.isActive(id) || options.force || !config.strict) {
const defaultOptionsRunFn = config.defaultOptions?.[id]?.run; const defaultOptionsRunFn = config.defaultOptions?.[id]?.run;
isFunction(defaultOptionsRunFn) && (options = defaultOptionsRunFn(options)); isFunction(defaultOptionsRunFn) && (options = defaultOptionsRunFn(options));

58
packages/core/src/commands/view/ComponentDrag.ts

@ -4,7 +4,6 @@ import type { CommandObject } from './CommandAbstract';
import type Editor from '../../editor'; import type Editor from '../../editor';
import type Component from '../../dom_components/model/Component'; import type Component from '../../dom_components/model/Component';
import type EditorModel from '../../editor/model/Editor'; import type EditorModel from '../../editor/model/Editor';
import { CanvasSpotBuiltInTypes } from '../../canvas/model/CanvasSpot';
type Rect = { left: number; width: number; top: number; height: number }; type Rect = { left: number; width: number; top: number; height: number };
type OrigRect = { left: number; width: number; top: number; height: number; rect: Rect }; type OrigRect = { left: number; width: number; top: number; height: number; rect: Rect };
@ -35,6 +34,17 @@ type Opts = {
addStyle?: () => Record<string, unknown>; addStyle?: () => Record<string, unknown>;
}; };
// 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'; const evName = 'dmode';
export default { export default {
@ -73,6 +83,7 @@ export default {
this.guidesContainer = this.getGuidesContainer(); this.guidesContainer = this.getGuidesContainer();
this.guidesTarget = this.getGuidesTarget(); this.guidesTarget = this.getGuidesTarget();
this.guidesStatic = this.getGuidesStatic(); this.guidesStatic = this.getGuidesStatic();
this.lastMatchedEl = null;
let drg = this.dragger; let drg = this.dragger;
if (!drg) { if (!drg) {
@ -380,20 +391,13 @@ export default {
}); });
} }
this.editor.trigger(`${evName}:drag:start`, { const dragStartProps: DragEventProps = { matchedEl: target };
coords: { x: 0, y: 0 }, // TODO: pass the real coords
targetEl: event.target,
});
this.em.Canvas.addSpot({ this.editor.trigger(`${evName}:drag:start`, dragStartProps);
type: CanvasSpotBuiltInTypes.Drag,
component: target,
componentView: target.view,
});
}, },
onDrag(mouseEvent: MouseEvent, _dragger: Dragger) { onDrag(mouseEvent: MouseEvent, _dragger: Dragger) {
const { guidesTarget, opts } = this; const { guidesTarget, lastMatchedEl, opts } = this;
let guideNearElements = []; let guideNearElements = [];
this.updateGuides(guidesTarget); this.updateGuides(guidesTarget);
@ -404,37 +408,37 @@ export default {
const { x, y } = mouseEvent; const { x, y } = mouseEvent;
const guideNearElement = guideNearElements[0] ?? {}; const guideNearElement = guideNearElements[0] ?? {};
const { size, sizePercent, matched, elGuideInfo, elGuideInfoCnt } = guideNearElement; const { size, sizePercent, matched, elGuideInfo, elGuideInfoCnt } = guideNearElement;
const matchedEl = matched?.origin; let matchedEl = undefined;
// TODO: revisit event props
this.editor.trigger(`${evName}:drag:move`, { if (matched?.origin) {
matchedEl = matched.origin;
if (matchedEl !== lastMatchedEl) this.lastMatchedEl = matchedEl;
}
const dragMoveProps: DragEventProps = {
coords: { x, y }, coords: { x, y },
matchedEl, matchedEl,
lastMatchedEl,
elGuideInfo, elGuideInfo,
elGuideInfoCnt, elGuideInfoCnt,
size, size,
sizePercent, sizePercent,
}); };
// TODO: add the spot to the canvas to show the origin? this.editor.trigger(`${evName}:drag:move`, dragMoveProps);
// if (matchedEl) {
// this.em.Canvas.addSpot({
// type: CanvasSpotBuiltInTypes.Drag,
// });
// }
}, },
onEnd(ev: Event, _dragger: Dragger, opt = {}) { 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() }); opts?.onEnd?.(ev, opt, { event: ev, ...opt, ...this._getDragData() });
editor.stopCommand(id); editor.stopCommand(id);
this.hideGuidesInfo(); this.hideGuidesInfo();
this.em.trigger(`${evName}:end`, this.getEventOpts()); this.em.trigger(`${evName}:end`, this.getEventOpts());
this.em.trigger(`${evName}:drag:end`, undefined);
this.em.Canvas.removeSpots({ const dragEndProps: DragEventProps = { lastMatchedEl };
type: CanvasSpotBuiltInTypes.Drag,
component: this.target, this.em.trigger(`${evName}:drag:end`, dragEndProps);
});
}, },
hideGuidesInfo() { hideGuidesInfo() {

Loading…
Cancel
Save