From 5a56848f8fe4083e315a1f0f232a18348e7b76ee Mon Sep 17 00:00:00 2001 From: mohamedsalem401 Date: Tue, 8 Oct 2024 18:23:07 +0300 Subject: [PATCH] Add drop area calculation to tree nodes --- .../src/utils/sorter/CanvasComponentNode.ts | 5 ++++ .../core/src/utils/sorter/SortableTreeNode.ts | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/core/src/utils/sorter/CanvasComponentNode.ts b/packages/core/src/utils/sorter/CanvasComponentNode.ts index 087d302e7..9c05fc65a 100644 --- a/packages/core/src/utils/sorter/CanvasComponentNode.ts +++ b/packages/core/src/utils/sorter/CanvasComponentNode.ts @@ -1,6 +1,11 @@ import { BaseComponentNode } from './BaseComponentNode'; export default class CanvasComponentNode extends BaseComponentNode { + protected _dropAreaConfig = { + ratio: 0.8, + minDroppableDimension: 8, // 5px + maxUndroppableDimension: 15, // 15px + }; /** * Get the associated view of this component. * @returns The view associated with the component, or undefined if none. diff --git a/packages/core/src/utils/sorter/SortableTreeNode.ts b/packages/core/src/utils/sorter/SortableTreeNode.ts index ee18b619b..2a9a5b0d0 100644 --- a/packages/core/src/utils/sorter/SortableTreeNode.ts +++ b/packages/core/src/utils/sorter/SortableTreeNode.ts @@ -1,5 +1,6 @@ import { View } from '../../common'; import Dimension from './Dimension'; +import { DroppableZoneConfig } from './types'; import { DragSource } from './types'; /** @@ -10,6 +11,11 @@ import { DragSource } from './types'; export abstract class SortableTreeNode { protected _model: T; protected _dragSource: DragSource; + protected _dropAreaConfig: DroppableZoneConfig = { + ratio: 1, + minDroppableDimension: Number.MAX_VALUE, + maxUndroppableDimension: 0, + }; /** The dimensions of the node. */ public nodeDimensions?: Dimension; /** The dimensions of the child elements within the target node. */ @@ -92,6 +98,23 @@ export abstract class SortableTreeNode { return this._dragSource; } + get dropArea(): Dimension | undefined { + // If no parent, there's no reason to reduce the drop zone + if (!this.getParent()) return this.nodeDimensions?.clone(); + return this.nodeDimensions?.getDropArea(this._dropAreaConfig); + } + + /** + * Checks if the given coordinates are within the bounds of this node. + * + * @param {number} x - The X coordinate to check. + * @param {number} y - The Y coordinate to check. + * @returns {boolean} - True if the coordinates are within bounds, otherwise false. + */ + public isWithinDropBounds(x: number, y: number): boolean { + return !!this.dropArea && this.dropArea.isWithinBounds(x, y); + } + equals(node?: SortableTreeNode): node is SortableTreeNode { return !!node?._model && this._model === node._model; }