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
horisontal shift (wip)
Roman Pominov7548e54012c5
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.tsindex a4fd2b83b2..cb3877c5c0 100644--- a/apps/designer/app/canvas/shared/use-drag-drop.ts+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts@@ -92,7 +92,7 @@ export const useDragAndDrop = () => {const path = getInstancePath(rootInstance, dropTarget.data.id);path.reverse();- if (dropTarget.area !== "center") {+ if (dropTarget.area.isNearEdge === false) {path.shift();}diff --git a/apps/designer/app/designer/shared/tree/sortable-tree.tsx b/apps/designer/app/designer/shared/tree/sortable-tree.tsxindex aa547f910f..551415aa5a 100644--- a/apps/designer/app/designer/shared/tree/sortable-tree.tsx+++ b/apps/designer/app/designer/shared/tree/sortable-tree.tsx@@ -30,6 +30,12 @@ export const SortableTree = (const [dragItem, setDragItem] = useState<Instance>();const [dropTarget, setDropTarget] = useState<DropTarget<Instance>>();+ const horizontalShift = useRef(0);+ const dragItemDepth = useMemo(+ () => dragItem && getInstancePath(root, dragItem.id).length - 1,+ [dragItem, root]+ );+// @todo: not sure what this should return,// need to understand better when it's used//@@ -38,6 +44,7 @@ export const SortableTree = (return {data: root,element: rootRef.current as HTMLElement,+ final: true,};};@@ -66,8 +73,18 @@ export const SortableTree = (return instance || false;},+ // because we're reading from horizontalShift+ swapDropTargetNotPure: true,++ // @todo: fix magic number (half of item's height)+ edgeDistanceThreshold: 24 / 2,+swapDropTarget: (dropTarget) => {- if (dragItem === undefined || dropTarget === undefined) {+ if (+ dragItem === undefined ||+ dropTarget === undefined ||+ dragItemDepth === undefined+ ) {return getFallbackDropTarget();}@@ -78,8 +95,9 @@ export const SortableTree = (const path = getInstancePath(root, dropTarget.data.id);path.reverse();- if (dropTarget.area === "top" || dropTarget.area === "bottom") {- path.shift();+ if (dropTarget.area.isNearBottom) {+ const desiredDepth = dragItemDepth + horizontalShift.current;+ path.splice(0, path.length - desiredDepth);}// Don't allow to drop inside drag item or any of its children@@ -106,7 +124,7 @@ export const SortableTree = (return getFallbackDropTarget();}- return { data, element };+ return { data, element, final: true };},onDropTargetChange: (dropTarget) => {@@ -123,6 +141,9 @@ export const SortableTree = (});const dragHandlers = useDrag<Instance>({+ // @todo: fix magic number+ shiftDistanceThreshold: 15,+elementToData: (element) => {const dragItemElement = element.closest("[data-drag-item-id]");if (!(dragItemElement instanceof HTMLElement)) {@@ -141,6 +162,9 @@ export const SortableTree = (onMove: (point) => {dropHandlers.handleMove(point);},+ onShiftChange: ({ shifts }) => {+ horizontalShift.current = shifts;+ },onEnd: ({ isCanceled }) => {if (dropTarget && dragItem && isCanceled === false) {onDragEnd({@@ -152,6 +176,7 @@ export const SortableTree = (});}+ horizontalShift.current = 0;setDragItem(undefined);setDropTarget(undefined);dropHandlers.handleEnd();diff --git a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsxindex 1a6267dae1..6977e746e3 100644--- a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx+++ b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx@@ -219,7 +219,7 @@ export const Canvas = () => {const path = findItemPath(data, id) ?? [];- if (area !== "center") {+ if (area.isNearEdge === false) {path.shift();}diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.tsindex 9b585a34ad..50fd8bb4c2 100644--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts@@ -19,7 +19,13 @@ export type Placement = {direction: "horizontal" | "vertical";};-export type Area = "top" | "bottom" | "left" | "right" | "center";+export type Area = {+ isNearTop: boolean;+ isNearLeft: boolean;+ isNearRight: boolean;+ isNearBottom: boolean;+ isNearEdge: boolean;+};// https://stackoverflow.com/a/18157551/478603const getDistanceToRect = (rect: Rect, { x, y }: Point) => {@@ -53,19 +59,12 @@ export const getArea = (edgeDistanceThreshold: number,rect: Rect): Area => {- if (x - rect.left <= edgeDistanceThreshold) {- return "left";- }- if (rect.left + rect.width - x <= edgeDistanceThreshold) {- return "right";- }- if (y - rect.top <= edgeDistanceThreshold) {- return "top";- }- if (rect.top + rect.height - y <= edgeDistanceThreshold) {- return "bottom";- }- return "center";+ const isNearTop = y - rect.top <= edgeDistanceThreshold;+ const isNearLeft = x - rect.left <= edgeDistanceThreshold;+ const isNearRight = rect.left + rect.width - x <= edgeDistanceThreshold;+ const isNearBottom = rect.top + rect.height - y <= edgeDistanceThreshold;+ const isNearEdge = isNearTop || isNearLeft || isNearRight || isNearBottom;+ return { isNearTop, isNearLeft, isNearRight, isNearBottom, isNearEdge };};export const getPlacementBetween = (diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.ts b/packages/design-system/src/components/primitives/dnd/use-drop.tsindex e1ac984e40..7bd07e9ae5 100644--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts@@ -43,7 +43,17 @@ export type UseDropProps<Data> = {swapDropTarget: (// undefined is passed when no suitable element is found under the pointerdropTarget: (PartialDropTarget<Data> & { area: Area }) | undefined- ) => PartialDropTarget<Data>;+ ) => PartialDropTarget<Data> & {+ // Set "final" to true if you don't want to swap+ // any further for the current pointer position.+ // (Normally swapDropTarget is called repeatedly until it returns the same value twice)+ final?: boolean;+ };++ // Set this to true if your swapDropTarget function+ // reads from an outside state rather than only using the arguments.+ // In this case it will be called on every pointer move even if the arguments are the same.+ swapDropTargetNotPure?: boolean;onDropTargetChange: (dropTarget: DropTarget<Data>) => void;@@ -204,6 +214,7 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {edgeDistanceThreshold = 3,elementToData,swapDropTarget,+ swapDropTargetNotPure = false,} = latestProps.current;if (state.current.started === false) {@@ -229,33 +240,36 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {});// To avoid calling swapDropTarget unnecessarily on every pointermove- const isNewCandidate =- candidate?.element !== state.current.lastCandidateElement;- state.current.lastCandidateElement = candidate?.element;- const candidateArea =- candidate && pointerCoordinates- ? getArea(- pointerCoordinates,- edgeDistanceThreshold,- candidate.element.getBoundingClientRect()- )- : undefined;- const isNewArea = candidateArea !== state.current.lastCandidateArea;- state.current.lastCandidateArea = candidateArea;- if (- isNewCandidate === false &&- isNewArea === false &&- state.current.dropTarget- ) {- // Still need to call setDropTarget to update rect and/or placement.- // Because indexWithinChildren might have changed,- // or parent coordinates might have moved in case of a scroll- setDropTarget(state.current.dropTarget);- return;+ if (swapDropTargetNotPure === false) {+ const isNewCandidate =+ candidate?.element !== state.current.lastCandidateElement;+ state.current.lastCandidateElement = candidate?.element;+ const candidateArea =+ candidate && pointerCoordinates+ ? getArea(+ pointerCoordinates,+ edgeDistanceThreshold,+ candidate.element.getBoundingClientRect()+ )+ : undefined;+ const isNewArea =+ isSameArea(candidateArea, state.current.lastCandidateArea) === false;+ state.current.lastCandidateArea = candidateArea;+ if (+ isNewCandidate === false &&+ isNewArea === false &&+ state.current.dropTarget+ ) {+ // Still need to call setDropTarget to update rect and/or placement.+ // Because indexWithinChildren might have changed,+ // or parent coordinates might have moved in case of a scroll+ setDropTarget(state.current.dropTarget);+ return;+ }}- let didSwap = true;- while (didSwap || candidate == null) {+ let continueSwapping = true;+ while (continueSwapping || candidate == null) {const swappedTo = swapDropTarget(candidate && pointerCoordinates? {@@ -268,7 +282,8 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {}: undefined);- didSwap = swappedTo.element !== candidate?.element;+ continueSwapping =+ swappedTo.element !== candidate?.element && swappedTo.final !== true;candidate = swappedTo;}@@ -342,3 +357,18 @@ const findClosestDropTarget = <Data>({currentElement = currentElement.parentElement;}};++const isSameArea = (a: Area | undefined, b: Area | undefined) => {+ if (a === undefined && b === undefined) {+ return true;+ }+ if (a === undefined || b === undefined) {+ return false;+ }+ return (+ a.isNearBottom === b.isNearBottom &&+ a.isNearLeft === b.isNearLeft &&+ a.isNearRight === b.isNearRight &&+ a.isNearTop === b.isNearTop+ );+};