From 0884f2695aae9a1b88b345791d81c4f5018d8070 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sat, 19 Aug 2023 04:35:08 +0400 Subject: [PATCH] Add support for target spot --- src/canvas/config/config.ts | 2 ++ src/canvas/index.ts | 2 +- src/canvas/model/CanvasSpot.ts | 14 +++++++------- src/utils/Sorter.ts | 21 ++++++++++++++++----- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/canvas/config/config.ts b/src/canvas/config/config.ts index 25d7da2e9..956bb0be2 100644 --- a/src/canvas/config/config.ts +++ b/src/canvas/config/config.ts @@ -92,8 +92,10 @@ const config: CanvasConfig = { notTextable: ['button', 'a', 'input[type=checkbox]', 'input[type=radio]'], allowExternalDrop: true, customSpots: { + // select: true, hover: true, spacing: true, + target: true, }, }; diff --git a/src/canvas/index.ts b/src/canvas/index.ts index d6bca00a4..885a7d051 100644 --- a/src/canvas/index.ts +++ b/src/canvas/index.ts @@ -724,7 +724,7 @@ export default class CanvasModule extends Module { ['model', 'droppable'].forEach(i => (this[i] = {})); } - setSpot(props: Omit, opts: AddOptions = {}) { + setSpot(props: Omit & { id?: string }, opts: AddOptions = {}) { const spotProps = props as T; const spots = this.getSpots(spotProps); diff --git a/src/canvas/model/CanvasSpot.ts b/src/canvas/model/CanvasSpot.ts index e42a3855e..0039f6e23 100644 --- a/src/canvas/model/CanvasSpot.ts +++ b/src/canvas/model/CanvasSpot.ts @@ -9,8 +9,8 @@ export enum CanvasSpotBuiltInTypes { Select = 'select', Hover = 'hover', Spacing = 'spacing', - Resize = 'resize', Target = 'target', + Resize = 'resize', } export type CanvasSpotBuiltInType = `${CanvasSpotBuiltInTypes}`; @@ -78,14 +78,14 @@ export default class CanvasSpot ext return this.componentView?.el; } - get style() { - const { width, height, x, y } = this.boxRect; + getStyle(opts: { boxRect?: BoxRect } = {}): Partial { + const { width, height, x, y } = opts.boxRect || this.boxRect; return { - width, - height, - top: 0, - left: 0, + width: `${width}px`, + height: `${height}px`, + top: '0', + left: '0', position: 'absolute', translate: `${x}px ${y}px`, }; diff --git a/src/utils/Sorter.ts b/src/utils/Sorter.ts index ae302e06d..04d634038 100644 --- a/src/utils/Sorter.ts +++ b/src/utils/Sorter.ts @@ -1,13 +1,12 @@ import { bindAll, each, isArray, isFunction, isString, result } from 'underscore'; import { BlockProperties } from '../block_manager/model/Block'; import CanvasModule from '../canvas'; +import { CanvasSpotBuiltInTypes } from '../canvas/model/CanvasSpot'; import { $, Collection, Model, View } from '../common'; import EditorModel from '../editor/model/Editor'; import { getPointerEvent, isTextNode, off, on } from './dom'; import { getElement, getModel, matches } from './mixins'; -const noop = () => {}; - type DropContent = BlockProperties['content']; interface Dim { @@ -56,6 +55,15 @@ export interface SorterOptions { scale?: number; } +const noop = () => {}; + +const targetSpotType = CanvasSpotBuiltInTypes.Target; + +const spotTarget = { + id: 'sorter-target', + type: targetSpotType, +}; + export default class Sorter extends View { opt!: SorterOptions; elT!: number; @@ -396,7 +404,7 @@ export default class Sorter extends View { if (src) { srcModel = this.getSourceModel(src); - srcModel && srcModel.set && srcModel.set('status', 'freezed'); + srcModel?.set && srcModel.set('status', 'freezed'); this.srcModel = srcModel; } @@ -488,8 +496,10 @@ export default class Sorter extends View { targetModel.set('status', ''); } - if (model && model.set) { - model.set('status', 'selected-parent'); + if (model?.set) { + const cv = this.em!.Canvas; + cv.setSpot({ ...spotTarget, component: model as any }); + !cv.hasCustomSpot(targetSpotType) && model.set('status', 'selected-parent'); this.targetModel = model; } } @@ -1208,6 +1218,7 @@ export default class Sorter extends View { this.disableTextable(); this.selectTargetModel(); this.toggleSortCursor(); + this.em?.Canvas.removeSpot(spotTarget); delete this.toMove; delete this.eventMove;