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.

decouple drop target logic, edge detection

Oleg Isonen1fdcfc8961e0
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
index 7bb16791bf..8bf8c99a98 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
@@ -3,10 +3,20 @@ import { ComponentStory, ComponentMeta } from "@storybook/react";
import { useState } from "react";
import { DropTarget } from "react-dnd";
import { Box } from "../../box";
-import { useDrag } from "./handlers";
+import { useDrag, useDropTargetRect } from "./handlers";
export const Playground = () => {
- const [dropTarget, setDropTarget] = useState<HTMLElement>();
+ const [dropTargetRect, setDropTargetRect] = useState<DOMRect>();
+
+ const { ref, handleMove } = useDropTargetRect({
+ isDropTarget(element: HTMLElement) {
+ return element.dataset.draggable === "true";
+ },
+ onDropTargetChange({ rect, area }: DOMRect) {
+ setDropTargetRect(rect);
+ console.log("area", area);
+ },
+ });
const dragProps = useDrag({
onStart(event: any) {
@@ -14,22 +24,17 @@ export const Playground = () => {
event.cancel();
}
},
- isDropTarget(element: HTMLElement) {
- return element.dataset.draggable === "true";
- },
- onDropTargetChange(element: HTMLElement) {
- setDropTarget(element);
- },
+ onMove: handleMove,
});
return (
- <div {...dragProps}>
+ <div {...dragProps} ref={ref}>
<Item background="$cyanA9" />
<Item background="$slateA9" />
<Item background="$blueA9" draggable={false}>
Not Draggable
</Item>
- <Outline element={dropTarget} />
+ <Outline rect={dropTargetRect} />
</div>
);
};
@@ -45,9 +50,8 @@ const Item = ({ background, draggable = true, children }: any) => (
</Box>
);
-const Outline = ({ element }: { element?: HTMLElement }) => {
- if (element === undefined) return null;
- const rect = element.getBoundingClientRect();
+const Outline = ({ rect }: { rect?: DOMRect }) => {
+ if (rect === undefined) return null;
const style = {
top: rect.top,
left: rect.left,
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts
index bafc812465..a6e74e051a 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts
@@ -3,7 +3,7 @@ import { useMove } from "./use-move";
type IsDropTarget = (element: HTMLElement) => boolean;
-const findDropTarget = ({
+const findClosestDropTarget = ({
root,
target,
isDropTarget,
@@ -36,7 +36,6 @@ type State =
status: "dragging";
pageX: number;
pageY: number;
- target: HTMLElement;
}
| {
status: "canceled";
@@ -45,10 +44,8 @@ type State =
export const useDrag = ({
onStart,
startDistanceThreashold = 3,
- isDropTarget,
- onDropTargetChange,
+ onMove,
}: any = {}) => {
- const rootRef = useRef<HTMLElement | null>(null);
const state = useRef<State>({
status: "idle",
});
@@ -83,38 +80,85 @@ export const useDrag = ({
return;
}
- if (rootRef.current === null) {
- return;
- }
-
- const nextTarget = findDropTarget({
- root: rootRef.current,
- target: event.target,
- isDropTarget,
- });
-
- const hasTargetChanged =
- state.current.status !== "dragging" ||
- nextTarget !== state.current.target;
-
state.current = {
status: "dragging",
pageX: event.pageX,
pageY: event.pageY,
- target: nextTarget,
};
- if (hasTargetChanged) {
- onDropTargetChange(nextTarget);
- }
+ onMove({ x: event.pageX, y: event.pageY });
},
onMoveEnd() {
state.current.status = "idle";
},
});
+ return props.moveProps;
+};
+
+type Coordinate = { x: number; y: number };
+type Area = "top" | "right" | "bottom" | "left" | "center";
+
+const elementFromPoint = (coordinate: Coordinate): HTMLElement | undefined => {
+ const element = document.elementFromPoint(coordinate.x, coordinate.y);
+ if (element instanceof HTMLElement) return element;
+};
+
+export const useDropTargetRect = ({
+ isDropTarget,
+ onDropTargetChange,
+ edgeThreshold = 3,
+}: any) => {
+ const rootRef = useRef<HTMLElement | null>(null);
+ const targetRef = useRef<HTMLElement>();
+ const targetRectRef = useRef<DOMRect>();
+ const areaRef = useRef<Area>("center");
+
return {
- ...props.moveProps,
+ handleMove(pointerCoordinate: Coordinate) {
+ const target = elementFromPoint(pointerCoordinate);
+ if (target === undefined || rootRef.current === null) {
+ return;
+ }
+
+ const nextTarget = findClosestDropTarget({
+ root: rootRef.current,
+ target,
+ isDropTarget,
+ });
+
+ const hasTargetChanged = nextTarget !== targetRef.current;
+
+ let nextArea: Area = "center";
+
+ if (hasTargetChanged) {
+ targetRectRef.current = nextTarget.getBoundingClientRect();
+ }
+
+ // We are at the edge and this means user wants to insert after that element into its parent
+ if (targetRectRef.current !== undefined) {
+ if (pointerCoordinate.y - targetRectRef.current.top <= edgeThreshold)
+ nextArea = "top";
+ if (targetRectRef.current.bottom - pointerCoordinate.y <= edgeThreshold)
+ nextArea = "bottom";
+ if (pointerCoordinate.x - targetRectRef.current.left <= edgeThreshold)
+ nextArea = "left";
+ if (targetRectRef.current.right - pointerCoordinate.x <= edgeThreshold)
+ nextArea = "right";
+ }
+
+ const hasAreaChanged = nextArea !== areaRef.current;
+
+ if (hasTargetChanged || hasAreaChanged) {
+ targetRef.current = nextTarget;
+ areaRef.current = nextArea;
+ onDropTargetChange({
+ rect: targetRectRef.current,
+ area: nextArea,
+ });
+ }
+ },
+
ref(rootElement: HTMLElement | null) {
rootRef.current = rootElement;
},