Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

delete old use-drop-target, fix findClosestDrop...

Roman Pominov48340e9822d7
diff --git a/packages/design-system/src/components/primitives/dnd/index.ts b/packages/design-system/src/components/primitives/dnd/index.ts
index 9055bde691..357d77b756 100644
--- a/packages/design-system/src/components/primitives/dnd/index.ts
+++ b/packages/design-system/src/components/primitives/dnd/index.ts
@@ -1,4 +1,4 @@
export * from "./placement";
export * from "./use-drag";
-export * from "./use-drop-target2";
+export * from "./use-drop-target";
export * from "./use-auto-scroll";
diff --git a/packages/design-system/src/components/primitives/dnd/use-drop-target.ts b/packages/design-system/src/components/primitives/dnd/use-drop-target.ts
index f3c5ea501b..c8e859c209 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drop-target.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drop-target.ts
@@ -2,104 +2,120 @@ import { useRef } from "react";
export type Area = "top" | "right" | "bottom" | "left" | "center";
-export type Parameters = {
- isDropTarget: (element: HTMLElement) => boolean;
+export type DropTarget<Data> = {
+ data: Data;
+ element: HTMLElement;
+ rect: DOMRect;
+};
+
+const isSameRect = (a: DOMRect, b: DOMRect) =>
+ a.top === b.top &&
+ a.left === b.left &&
+ a.bottom === b.bottom &&
+ a.right === b.right;
+
+// We pass around data, to avoid extra data lookups.
+// For example, data found in isDropTarget
+// doesn't have to be looked up again in swapDropTarget.
+export type UseDropTargetProps<Data> = {
+ // To check that the element can qualify as a target
+ isDropTarget: (element: HTMLElement) => Data | false;
+
+ // Given the potential target that has passed isDropTarget check,
+ // and the position of the pointer on the target,
+ // you can swap to another target
+ swapDropTarget?: (args: { element: HTMLElement; data: Data; area: Area }) => {
+ element: HTMLElement;
+ data: Data;
+ };
+
+ onDropTargetChange: (dropTarget: DropTarget<Data>) => void;
edgeDistanceThreshold?: number;
- holdTimeThreshold?: number;
- onDropTargetChange: (event: {
- rect: DOMRect;
- area: Area;
- target: HTMLElement;
- }) => void;
- onHold?: (event: { target: HTMLElement }) => void;
+
+ // @todo: not sure we should do any data comparisons here.
+ // On the contrary, we may want to rely on the assumption
+ // that the data for the same element is always the same (for caching)
+ isSameData?: (data1: Data, data2: Data) => boolean;
};
-export type Handlers = {
+export type UseDropTargetHandlers = {
handleMove: (pointerCoordinate: Coordinate) => void;
handleScroll: () => void;
handleEnd: () => void;
rootRef: (element: HTMLElement | null) => void;
};
-export const useDropTarget = ({
- isDropTarget,
+export const useDropTarget = <Data>({
edgeDistanceThreshold = 3,
- holdTimeThreshold = 500,
+ isSameData = (data1: Data, data2: Data) => data1 === data2,
+ isDropTarget,
+ swapDropTarget = (args) => args,
onDropTargetChange,
- // eslint-disable-next-line @typescript-eslint/no-empty-function
- onHold = () => {},
-}: Parameters): Handlers => {
+}: UseDropTargetProps<Data>): UseDropTargetHandlers => {
const state = useRef({
root: null as HTMLElement | null,
- target: undefined as HTMLElement | undefined,
- area: "center" as Area,
- rect: undefined as DOMRect | undefined,
- holdTimeout: undefined as NodeJS.Timeout | undefined,
pointerCoordinate: undefined as Coordinate | undefined,
- dropIndex: undefined as number | undefined,
+ dropTarget: undefined as DropTarget<Data> | undefined,
});
- const clearHoldTimeout = () => {
- if (state.current.holdTimeout) {
- clearTimeout(state.current.holdTimeout);
- state.current.holdTimeout = undefined;
- }
- };
-
- const detectHold = (target: HTMLElement) => {
- clearHoldTimeout();
- state.current.holdTimeout = setTimeout(() => {
- state.current.holdTimeout = undefined;
- if (target === state.current.target) {
- onHold({ target });
- }
- }, holdTimeThreshold);
- };
-
const detectTarget = () => {
- const pointerCoordinate = state.current.pointerCoordinate;
- if (pointerCoordinate === undefined) {
+ const { pointerCoordinate, root } = state.current;
+ if (pointerCoordinate === undefined || root === null) {
return;
}
const target = elementFromPoint(pointerCoordinate);
- if (target === undefined || state.current.root === null) {
+ if (target === undefined) {
return;
}
- const nextTarget = findClosestDropTarget({
- root: state.current.root,
+ // @todo: cache this? Not expensive by itself, but it may call isDropTarget a lot
+ const potentialTarget = findClosestDropTarget({
+ root,
target,
isDropTarget,
});
-
- const hasTargetChanged = nextTarget !== state.current.target;
- state.current.target = nextTarget;
-
- if (hasTargetChanged) {
- detectHold(nextTarget);
+ if (potentialTarget === undefined) {
+ return;
}
- const nextRect = nextTarget.getBoundingClientRect();
-
- const hasRectChanged = nextRect !== state.current.rect;
- state.current.rect = nextRect;
+ // @todo: if neither potentialTarget nor area has changed since the last time,
+ // don't call swapDropTarget.
+
+ const dropTarget = {
+ ...potentialTarget,
+ rect: potentialTarget.element.getBoundingClientRect(),
+ };
+
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const swappedTarget = swapDropTarget({
+ element: dropTarget.element,
+ data: dropTarget.data,
+ area: getArea(
+ pointerCoordinate,
+ edgeDistanceThreshold,
+ dropTarget.rect
+ ),
+ });
- const nextArea = getArea(
- pointerCoordinate,
- edgeDistanceThreshold,
- nextRect
- );
+ if (swappedTarget.element === dropTarget.element) {
+ break;
+ }
- const hasAreaChanged = nextArea !== state.current.area;
- state.current.area = nextArea;
+ dropTarget.element = swappedTarget.element;
+ dropTarget.data = swappedTarget.data;
+ dropTarget.rect = swappedTarget.element.getBoundingClientRect();
+ }
- if (hasTargetChanged || hasAreaChanged || hasRectChanged) {
- onDropTargetChange({
- rect: nextRect,
- area: nextArea,
- target: nextTarget,
- });
+ if (
+ state.current.dropTarget === undefined ||
+ state.current.dropTarget.element !== dropTarget.element ||
+ !isSameData(state.current.dropTarget.data, dropTarget.data) ||
+ !isSameRect(state.current.dropTarget.rect, dropTarget.rect)
+ ) {
+ state.current.dropTarget = dropTarget;
+ onDropTargetChange(dropTarget);
}
};
@@ -114,8 +130,8 @@ export const useDropTarget = ({
},
handleEnd() {
- // Clear onHold timeout if we ended interaction before it fired
- clearHoldTimeout();
+ state.current.pointerCoordinate = undefined;
+ state.current.dropTarget = undefined;
},
rootRef(rootElement: HTMLElement | null) {
@@ -131,6 +147,8 @@ const elementFromPoint = (coordinate: Coordinate): HTMLElement | undefined => {
if (element instanceof HTMLElement) return element;
};
+// @todo: it seems we only care about whether the area is center or on an edge.
+// Knowing which edge doesn't help much.
const getArea = (
pointerCoordinate: Coordinate,
edgeDistanceThreshold: number,
@@ -150,22 +168,30 @@ const getArea = (
return area;
};
-const findClosestDropTarget = ({
+// @todo: maybe rather than climbing the DOM tree,
+// we should use document.elementsFromPoint() array?
+// Might work better with absolute positioned elements.
+const findClosestDropTarget = <Data>({
root,
target,
isDropTarget,
}: {
root: HTMLElement;
target: HTMLElement;
- isDropTarget: (target: HTMLElement) => boolean;
-}): HTMLElement => {
+ isDropTarget: (target: HTMLElement) => Data | false;
+}) => {
let currentTarget: HTMLElement | null = target;
- while (currentTarget !== null && currentTarget !== root) {
- const isValid = isDropTarget(currentTarget);
- if (isValid) {
- return currentTarget;
+ while (currentTarget !== null) {
+ const data = isDropTarget(currentTarget);
+ if (data !== false) {
+ return { data: data, element: currentTarget };
}
+
+ // @todo: what makes us think that document.elementFromPoint() is inside the root in the first place?
+ if (currentTarget === root) {
+ break;
+ }
+
currentTarget = currentTarget.parentElement;
}
- return root;
};
diff --git a/packages/design-system/src/components/primitives/dnd/use-drop-target2.ts b/packages/design-system/src/components/primitives/dnd/use-drop-target2.ts
deleted file mode 100644
index ccf9c20965..0000000000
--- a/packages/design-system/src/components/primitives/dnd/use-drop-target2.ts
+++ /dev/null
@@ -1,191 +0,0 @@
-import { useRef } from "react";
-
-export type Area = "top" | "right" | "bottom" | "left" | "center";
-
-export type DropTarget<Data> = {
- data: Data;
- element: HTMLElement;
- rect: DOMRect;
-};
-
-const isSameRect = (a: DOMRect, b: DOMRect) =>
- a.top === b.top &&
- a.left === b.left &&
- a.bottom === b.bottom &&
- a.right === b.right;
-
-// We pass around data, to avoid extra data lookups.
-// For example, data found in isDropTarget
-// doesn't have to be looked up again in swapDropTarget.
-export type UseDropTargetProps<Data> = {
- // To check that the element can qualify as a target
- isDropTarget: (element: HTMLElement) => Data | false;
-
- // Given the potential target that has passed isDropTarget check,
- // and the position of the pointer on the target,
- // you can swap to another target
- swapDropTarget?: (args: { element: HTMLElement; data: Data; area: Area }) => {
- element: HTMLElement;
- data: Data;
- };
-
- onDropTargetChange: (dropTarget: DropTarget<Data>) => void;
- edgeDistanceThreshold?: number;
-
- // @todo: not sure we should do any data comparisons here.
- // On the contrary, we may want to rely on the assumption
- // that the data for the same element is always the same (for caching)
- isSameData?: (data1: Data, data2: Data) => boolean;
-};
-
-export type UseDropTargetHandlers = {
- handleMove: (pointerCoordinate: Coordinate) => void;
- handleScroll: () => void;
- handleEnd: () => void;
- rootRef: (element: HTMLElement | null) => void;
-};
-
-export const useDropTarget = <Data>({
- edgeDistanceThreshold = 3,
- isSameData = (data1: Data, data2: Data) => data1 === data2,
- isDropTarget,
- swapDropTarget = (args) => args,
- onDropTargetChange,
-}: UseDropTargetProps<Data>): UseDropTargetHandlers => {
- const state = useRef({
- root: null as HTMLElement | null,
- pointerCoordinate: undefined as Coordinate | undefined,
- dropTarget: undefined as DropTarget<Data> | undefined,
- });
-
- const detectTarget = () => {
- const { pointerCoordinate, root } = state.current;
- if (pointerCoordinate === undefined || root === null) {
- return;
- }
-
- const target = elementFromPoint(pointerCoordinate);
- if (target === undefined) {
- return;
- }
-
- // @todo: cache this? Not expensive by itself, but it may call isDropTarget a lot
- const potentialTarget = findClosestDropTarget({
- root,
- target,
- isDropTarget,
- });
- if (potentialTarget === undefined) {
- return;
- }
-
- // @todo: if neither potentialTarget nor area has changed since the last time,
- // don't call swapDropTarget.
-
- const dropTarget = {
- ...potentialTarget,
- rect: potentialTarget.element.getBoundingClientRect(),
- };
-
- // eslint-disable-next-line no-constant-condition
- while (true) {
- const swappedTarget = swapDropTarget({
- element: dropTarget.element,
- data: dropTarget.data,
- area: getArea(
- pointerCoordinate,
- edgeDistanceThreshold,
- dropTarget.rect
- ),
- });
-
- if (swappedTarget.element === dropTarget.element) {
- break;
- }
-
- dropTarget.element = swappedTarget.element;
- dropTarget.data = swappedTarget.data;
- dropTarget.rect = swappedTarget.element.getBoundingClientRect();
- }
-
- if (
- state.current.dropTarget === undefined ||
- state.current.dropTarget.element !== dropTarget.element ||
- !isSameData(state.current.dropTarget.data, dropTarget.data) ||
- !isSameRect(state.current.dropTarget.rect, dropTarget.rect)
- ) {
- state.current.dropTarget = dropTarget;
- onDropTargetChange(dropTarget);
- }
- };
-
- return {
- handleMove(pointerCoordinate: Coordinate) {
- state.current.pointerCoordinate = pointerCoordinate;
- detectTarget();
- },
-
- handleScroll() {
- detectTarget();
- },
-
- handleEnd() {
- state.current.pointerCoordinate = undefined;
- state.current.dropTarget = undefined;
- },
-
- rootRef(rootElement: HTMLElement | null) {
- state.current.root = rootElement;
- },
- };
-};
-
-type Coordinate = { x: number; y: number };
-
-const elementFromPoint = (coordinate: Coordinate): HTMLElement | undefined => {
- const element = document.elementFromPoint(coordinate.x, coordinate.y);
- if (element instanceof HTMLElement) return element;
-};
-
-// @todo: it seems we only care about whether the area is center or on an edge.
-// Knowing which edge doesn't help much.
-const getArea = (
- pointerCoordinate: Coordinate,
- edgeDistanceThreshold: number,
- targetRect?: DOMRect
-) => {
- let area: Area = "center";
- if (targetRect === undefined) return area;
- // We are at the edge and this means user wants to insert after that element into its parent
- if (pointerCoordinate.y - targetRect.top <= edgeDistanceThreshold)
- area = "top";
- if (targetRect.bottom - pointerCoordinate.y <= edgeDistanceThreshold)
- area = "bottom";
- if (pointerCoordinate.x - targetRect.left <= edgeDistanceThreshold)
- area = "left";
- if (targetRect.right - pointerCoordinate.x <= edgeDistanceThreshold)
- area = "right";
- return area;
-};
-
-// @todo: maybe rather than climbing the DOM tree,
-// we should use document.elementsFromPoint() array?
-// Might work better with absolute positioned elements.
-const findClosestDropTarget = <Data>({
- root,
- target,
- isDropTarget,
-}: {
- root: HTMLElement;
- target: HTMLElement;
- isDropTarget: (target: HTMLElement) => Data | false;
-}) => {
- let currentTarget: HTMLElement | null = target;
- while (currentTarget !== null && currentTarget !== root) {
- const data = isDropTarget(currentTarget);
- if (data !== false) {
- return { data: data, element: currentTarget };
- }
- currentTarget = currentTarget.parentElement;
- }
-};