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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
always show outline around drop target (wip)
Roman Pominovdb23a0adcc91
diff --git a/apps/designer/app/designer/shared/tree/placement-indicator.tsx b/apps/designer/app/designer/shared/tree/placement-indicator.tsxindex ba9e5f54b3..95909de34c 100644--- a/apps/designer/app/designer/shared/tree/placement-indicator.tsx+++ b/apps/designer/app/designer/shared/tree/placement-indicator.tsx@@ -1,4 +1,7 @@import { Box, Placement, Rect, styled } from "@webstudio-is/design-system";+import { Instance } from "@webstudio-is/react-sdk";+import { useMemo } from "react";+import { type ShiftedDropTarget } from "./tree";const CIRCLE_SIZE = 8;const LINE_THICKNESS = 2;@@ -36,11 +39,7 @@ const Line = styled(Box, {outlineWidth: OUTLINE_WIDTH,});-export const PlacementIndicatorLine = ({- placement,-}: {- placement: Placement;-}) => (+const PlacementIndicatorLine = ({ placement }: { placement: Placement }) => (<><CircleOutlinestyle={{@@ -74,13 +73,44 @@ const Outline = styled(Box, {borderRadius: "$2",});-export const PlacementIndicatorOutline = ({ rect }: { rect: Rect }) => (+const PlacementIndicatorOutline = ({ rect }: { rect: Rect }) => (<Outlinestyle={{top: rect.top,- left: rect.left + 2,- width: rect.width - 4,+ left: rect.left,+ width: rect.width,height: rect.height,}}/>);++export const PlacementIndicator = ({+ dropTarget,+ getDropTargetElement,+}: {+ dropTarget: ShiftedDropTarget;+ getDropTargetElement: (id: Instance["id"]) => HTMLElement | null | undefined;+}) => {+ const rect = useMemo(+ // @todo: update rect on scroll+ // @todo: don't render outline out of bounds+ // @todo: don't use [data-item-button-id]+ // @todo: probably should move this rect retriving logic into a hook+ () =>+ (+ (+ getDropTargetElement(dropTarget.instance.id) || undefined+ )?.querySelector("[data-item-button-id]") || undefined+ )?.getBoundingClientRect(),+ [dropTarget.instance.id, getDropTargetElement]+ );++ return (+ <>+ {rect && <PlacementIndicatorOutline rect={rect} />}+ {dropTarget.placement && (+ <PlacementIndicatorLine placement={dropTarget.placement} />+ )}+ </>+ );+};diff --git a/apps/designer/app/designer/shared/tree/tree.test.ts b/apps/designer/app/designer/shared/tree/tree.test.tsindex ed473c939d..3ae2708276 100644--- a/apps/designer/app/designer/shared/tree/tree.test.ts+++ b/apps/designer/app/designer/shared/tree/tree.test.ts@@ -77,17 +77,14 @@ describe("useHorizontalShift", () => {after?: Instance;placement?: Placement;}): DropTarget<Instance> => ({- element: null as unknown as HTMLElement, // element is not useddata: into,- rect: {- top: Math.random(),- left: Math.random(),- width: Math.random(),- height: Math.random(),- } as DOMRect,indexWithinChildren:after === undefined ? 0 : into.children.indexOf(after) + 1,placement,++ // not used+ element: null as unknown as HTMLElement,+ rect: null as unknown as DOMRect,});const render = (@@ -136,11 +133,8 @@ describe("useHorizontalShift", () => {// parent doesn't changeexpect(result?.instance.id).toBe(box2.id);- // placement is the original rect- expect(result?.placement).toEqual({- type: "rect",- rect: dropTarget.rect,- });+ // placement line geometry isn't provided+ expect(result?.placement).toBeUndefined();});test("placement line coordinates are always adjusted", () => {@@ -155,8 +149,9 @@ describe("useHorizontalShift", () => {// placement line is adjusted to account for depth,// even though depth didn't changeexpect(result?.placement).toEqual({- type: "line",- placement: { ...makePlacement(), length: 260, x: 40 },+ ...makePlacement(),+ length: 260,+ x: 40,});});@@ -200,8 +195,9 @@ describe("useHorizontalShift", () => {expect(result?.instance.id).toBe(tree.id);expect(result?.position).toBe(tree.children.indexOf(box3) + 1);expect(result?.placement).toEqual({- type: "line",- placement: { ...makePlacement(), x: 24, length: 276 },+ ...makePlacement(),+ x: 24,+ length: 276,});});@@ -215,16 +211,18 @@ describe("useHorizontalShift", () => {expect(result1?.instance.id).toBe(tree.id);expect(result1?.position).toBe(tree.children.indexOf(box3) + 1);expect(result1?.placement).toEqual({- type: "line",- placement: { ...makePlacement(), x: 24, length: 276 },+ ...makePlacement(),+ x: 24,+ length: 276,});const result2 = render(args, 1);expect(result2?.instance.id).toBe(box3.id);expect(result2?.position).toBe(box3.children.indexOf(box32) + 1);expect(result2?.placement).toEqual({- type: "line",- placement: { ...makePlacement(), x: 40, length: 260 },+ ...makePlacement(),+ x: 40,+ length: 260,});});diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsxindex 7a46e3160e..3cde0d439e 100644--- a/apps/designer/app/designer/shared/tree/tree.tsx+++ b/apps/designer/app/designer/shared/tree/tree.tsx@@ -6,7 +6,6 @@ import {} from "@webstudio-is/react-sdk";import {type DropTarget,- type Rect,type Placement,useDrag,useDrop,@@ -27,10 +26,7 @@ import {getPlacementIndicatorAlignment,INDENT,} from "./tree-node";-import {- PlacementIndicatorLine,- PlacementIndicatorOutline,-} from "./placement-indicator";+import { PlacementIndicator } from "./placement-indicator";import { useHotkeys } from "react-hotkeys-hook";type TreeProps = {@@ -58,11 +54,17 @@ export const Tree = ({selectedInstanceId,});- const rootNodeRef = useRef<HTMLElement | null>(null);+ const rootRef = useRef<HTMLElement | null>(null);const [dragItem, setDragItem] = useState<Instance>();const [dropTarget, setDropTarget] = useState<DropTarget<Instance>>();+ const getDropTargetElement = useCallback(+ (id: Instance["id"]): HTMLElement | null | undefined =>+ rootRef.current?.querySelector(`[data-drop-target-id="${id}"]`),+ []+ );+const [shiftedDropTarget, setHorizontalShift] = useHorizontalShift({dragItem,dropTarget,@@ -71,12 +73,9 @@ export const Tree = ({});const getFallbackDropTarget = () => {- const element = rootNodeRef.current?.querySelector(- `[data-drop-target-id="${root.id}"]`- );return {data: root,- element: element as HTMLElement,+ element: getDropTargetElement(root.id) as HTMLElement,final: true,};};@@ -137,9 +136,7 @@ export const Tree = ({return getFallbackDropTarget();}- const element = rootNodeRef.current?.querySelector(- `[data-drop-target-id="${data.id}"]`- );+ const element = getDropTargetElement(data.id);if (element == null) {return getFallbackDropTarget();@@ -246,7 +243,7 @@ export const Tree = ({pb: 2,}}ref={(element) => {- rootNodeRef.current = element;+ rootRef.current = element;autoScrollHandlers.targetRef(element);dragHandlers.rootRef(element);dropHandlers.rootRef(element);@@ -272,15 +269,10 @@ export const Tree = ({</Box>{shiftedDropTarget &&createPortal(- shiftedDropTarget.placement.type === "rect" ? (- <PlacementIndicatorOutline- rect={shiftedDropTarget.placement.rect}- />- ) : (- <PlacementIndicatorLine- placement={shiftedDropTarget.placement.placement}- />- ),+ <PlacementIndicator+ dropTarget={shiftedDropTarget}+ getDropTargetElement={getDropTargetElement}+ />,document.body)}</Box>@@ -492,12 +484,10 @@ const useExpandState = ({return { getIsExpanded, setIsExpanded };};-type ShiftedDropTarget = {+export type ShiftedDropTarget = {instance: Instance;position: number | "end";- placement:- | { type: "rect"; rect: Rect }- | { type: "line"; placement: Placement };+ placement?: Placement;};export const useHorizontalShift = ({@@ -529,7 +519,7 @@ export const useHorizontalShift = ({return undefined;}- const { data, placement, rect, indexWithinChildren } = dropTarget;+ const { data, placement, indexWithinChildren } = dropTarget;const shiftPlacement = (depth: number) => {const shift = getPlacementIndicatorAlignment(depth);@@ -540,16 +530,11 @@ export const useHorizontalShift = ({};};- // 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.+ // Placement type “inside-parent” means that useDrop() didn’t find any children.+ // In this case the placement line coordinates are meaningless in the context of the tree.+ // We're dropping the placement and not performing any shifting.if (placement.type === "inside-parent") {- return {- instance: data,- position: "end",- placement: { type: "rect", rect: rect },- };+ return { instance: data, position: "end" };}const dropTargetPath = getInstancePathWithPositions(root, data.id);@@ -561,10 +546,7 @@ export const useHorizontalShift = ({const withoutShift = {instance: data,position: indexWithinChildren,- placement: {- type: "line",- placement: shiftPlacement(currentDepth),- },+ placement: shiftPlacement(currentDepth),} as const;const isDragItem = (instance: Instance | undefined | string) =>@@ -605,10 +587,7 @@ export const useHorizontalShift = ({return {instance: newParent,position: newPosition,- placement: {- type: "line",- placement: shiftPlacement(currentDepth - shifted),- },+ placement: shiftPlacement(currentDepth - shifted),};}@@ -649,10 +628,7 @@ export const useHorizontalShift = ({return {instance: newParent,position: "end",- placement: {- type: "line",- placement: shiftPlacement(currentDepth + shifted),- },+ placement: shiftPlacement(currentDepth + shifted),};}