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.

fix a bug where it's possible to drag into the drag item (making it disappear) using shift

Roman Pominovcce2b23f5909
diff --git a/apps/designer/app/designer/shared/tree/placement-indicator.tsx b/apps/designer/app/designer/shared/tree/placement-indicator.tsx
index 0f8f1b21d1..cc3300b0a2 100644
--- a/apps/designer/app/designer/shared/tree/placement-indicator.tsx
+++ b/apps/designer/app/designer/shared/tree/placement-indicator.tsx
@@ -1,4 +1,4 @@
-import { Box, DropTarget, Rect, styled } from "@webstudio-is/design-system";
+import { Box, Placement, Rect, styled } from "@webstudio-is/design-system";
const CIRCLE_SIZE = 8;
const LINE_THICKNESS = 2;
@@ -26,15 +26,15 @@ const Line = styled(Box, {
});
export const PlacementIndicatorLine = ({
- line,
+ placement,
}: {
- line: DropTarget<unknown>["placement"];
+ placement: Placement;
}) => (
<Line
style={{
- top: line.y - LINE_THICKNESS / 2,
- left: line.x + (CIRCLE_SIZE / 2 - OVERLAP),
- width: line.length - (CIRCLE_SIZE / 2 - OVERLAP),
+ top: placement.y - LINE_THICKNESS / 2,
+ left: placement.x + (CIRCLE_SIZE / 2 - OVERLAP),
+ width: placement.length - (CIRCLE_SIZE / 2 - OVERLAP),
height: LINE_THICKNESS,
}}
>
diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsx
index 4ff214f6bf..722792c76c 100644
--- a/apps/designer/app/designer/shared/tree/tree.tsx
+++ b/apps/designer/app/designer/shared/tree/tree.tsx
@@ -12,6 +12,7 @@ import {
useHold,
Box,
useAutoScroll,
+ Placement,
} from "@webstudio-is/design-system";
import {
findInstanceById,
@@ -73,7 +74,7 @@ export const Tree = ({
position: number | "end";
placement:
| { type: "rect"; rect: Rect }
- | { type: "line"; line: DropTarget<unknown>["placement"] };
+ | { type: "line"; placement: Placement };
}
| undefined
>(() => {
@@ -87,8 +88,7 @@ export const Tree = ({
const { data, placement, rect, indexWithinChildren } = dropTarget;
- // Placement type “inside-parent” means that useDrop didn’t find any children,
- // and the “placement” corresponds to a line near an edge of the parent.
+ // Placement type “inside-parent” means that useDrop didn’t find any children.
// This means the drop target is empty or collapsed.
// In this case, we want to show a rect instead of a line.
// Also, we don’t want to apply shift, as there’s no line to shift.
@@ -106,34 +106,29 @@ export const Tree = ({
const currentDepth = dropTargetPath.length;
const desiredDepth = dragItemDepth + horizontalShift;
- const shiftLine = (depth: number) => {
- const shift = getPlacementIndicatorAlignment(depth);
- return {
- ...placement,
- x: placement.x + shift,
- length: placement.length - shift,
- };
- };
-
const withoutShift = {
instance: data,
position: indexWithinChildren,
- placement: { type: "line", line: shiftLine(currentDepth) },
+ placement: {
+ type: "line",
+ placement: shiftPlacement(placement, currentDepth),
+ },
} as const;
+ const isDragItem = (instance: Instance | undefined | string) =>
+ typeof instance === "object" && instance.id === dragItem.id;
+
if (desiredDepth < currentDepth) {
+ // There's a special case when the placement line is above the drag item.
+ // For reparenting above and below the drag item means the same thing.
+ const indexCorrected = isDragItem(data.children[indexWithinChildren])
+ ? indexWithinChildren + 1
+ : indexWithinChildren;
+
// Unless we're currently at the bottom of drop target's children,
// decreasing depth will not correspond to a new meaningful position
- if (indexWithinChildren !== data.children.length) {
- // But there's a special case when the index is `last - 1` and the last child is the drag item.
- // When drag item is removed from its current position, `last - 1` will become `last`.
- if (indexWithinChildren !== data.children.length - 1) {
- return withoutShift;
- }
- const lastChild = data.children[data.children.length - 1];
- if (typeof lastChild !== "object" || lastChild.id !== dragItem.id) {
- return withoutShift;
- }
+ if (indexCorrected !== data.children.length) {
+ return withoutShift;
}
const difference = Math.min(
@@ -153,7 +148,7 @@ export const Tree = ({
position: dropTargetPath[difference - 1].position + 1,
placement: {
type: "line",
- line: shiftLine(currentDepth - difference),
+ placement: shiftPlacement(placement, currentDepth - difference),
},
};
}
@@ -161,17 +156,30 @@ export const Tree = ({
if (desiredDepth > currentDepth) {
let shifted = 0;
let newParent = data;
- let potentialNewParent = data.children[indexWithinChildren - 1];
+
+ // There's a special case when the placement line is below the drag item.
+ // For reparenting above and below the drag item means the same thing.
+ const getChildAbove = (
+ parent: Instance,
+ index: number
+ ): string | undefined | Instance =>
+ isDragItem(parent.children[index - 1])
+ ? parent.children[index - 2]
+ : parent.children[index - 1];
+
+ let potentialNewParent = getChildAbove(data, indexWithinChildren);
while (
- potentialNewParent !== undefined &&
- typeof potentialNewParent !== "string" &&
+ typeof potentialNewParent === "object" &&
getIsExpanded(potentialNewParent) &&
components[potentialNewParent.component].canAcceptChild() &&
shifted < desiredDepth - currentDepth
) {
newParent = potentialNewParent;
- potentialNewParent = newParent.children[newParent.children.length - 1];
+ potentialNewParent = getChildAbove(
+ newParent,
+ newParent.children.length
+ );
shifted++;
}
@@ -182,7 +190,10 @@ export const Tree = ({
return {
instance: newParent,
position: "end",
- placement: { type: "line", line: shiftLine(currentDepth + shifted) },
+ placement: {
+ type: "line",
+ placement: shiftPlacement(placement, currentDepth + shifted),
+ },
};
}
@@ -382,7 +393,9 @@ export const Tree = ({
finalDropTarget.placement.type === "rect" ? (
<PlacementIndicatorOutline rect={finalDropTarget.placement.rect} />
) : (
- <PlacementIndicatorLine line={finalDropTarget.placement.line} />
+ <PlacementIndicatorLine
+ placement={finalDropTarget.placement.placement}
+ />
),
document.body
)}
@@ -440,3 +453,12 @@ const useExpandState = ({
return { getIsExpanded, setIsExpanded };
};
+
+const shiftPlacement = (placement: Placement, depth: number) => {
+ const shift = getPlacementIndicatorAlignment(depth);
+ return {
+ ...placement,
+ x: placement.x + shift,
+ length: placement.length - shift,
+ };
+};
diff --git a/packages/design-system/src/components/primitives/dnd/index.ts b/packages/design-system/src/components/primitives/dnd/index.ts
index e4e1a5075e..7b407146f1 100644
--- a/packages/design-system/src/components/primitives/dnd/index.ts
+++ b/packages/design-system/src/components/primitives/dnd/index.ts
@@ -3,4 +3,4 @@ export * from "./use-drop";
export * from "./use-auto-scroll";
export * from "./placement-indicator";
export * from "./use-hold";
-export { type Rect, type Point } from "./geometry-utils";
+export { type Rect, type Point, type Placement } from "./geometry-utils";