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
cleanup & fixes
Roman Pominovb7e518c0c13b
diff --git a/packages/design-system/src/components/primitives/dnd/dom-utils.ts b/packages/design-system/src/components/primitives/dnd/dom-utils.tsnew file mode 100644index 0000000000..3b14d30feb--- /dev/null+++ b/packages/design-system/src/components/primitives/dnd/dom-utils.ts@@ -0,0 +1,62 @@+import {+ type Rect,+ type ChildrenOrientation,+ getRectsOrientation,+} from "./geometry-utils";++export type UsableElement = HTMLElement | SVGElement;++export const toUseableElement = (+ element: EventTarget | Element | undefined | null+): UsableElement | undefined => {+ if (element instanceof HTMLElement || element instanceof SVGElement) {+ return element;+ }+};++// By looking at a specific child and it's neighbours,+// determines their orientation relative to each other+export const getLocalChildrenOrientation = (+ parent: UsableElement,+ childrentRects: Rect[],+ childIndex: number+): ChildrenOrientation => {+ const previous = childrentRects[childIndex - 1] as Rect | undefined;+ const current = childrentRects[childIndex] as Rect | undefined;+ const next = childrentRects[childIndex + 1] as Rect | undefined;++ if (current === undefined || (next === undefined && previous === undefined)) {+ const probe = document.createElement("div");+ const { children } = parent;+ if (childIndex > children.length - 1) {+ parent.appendChild(probe);+ } else {+ parent.insertBefore(probe, children[childIndex]);+ }+ const probeRect = probe.getBoundingClientRect();+ parent.removeChild(probe);++ return probeRect.width === 0 && probeRect.height !== 0+ ? "horizontal"+ : "vertical";+ }++ return getRectsOrientation(previous, current, next);+};++export const getChildrenRects = (parent: UsableElement) => {+ const parentRect = parent.getBoundingClientRect();++ // We convert to relative coordinates to be able to store the result in cache.+ // Otherwise we would have to clear cache on scroll etc.+ const toRelativeCoordinates = (rect: Rect) => ({+ left: rect.left - parentRect.left,+ top: rect.top - parentRect.top,+ width: rect.width,+ height: rect.height,+ });++ return Array.from(parent.children).map((child) =>+ toRelativeCoordinates(child.getBoundingClientRect())+ );+};diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.tsindex b1445a2d3d..b3b8089531 100644--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts@@ -69,6 +69,12 @@ export const getPlacementBetween = (return undefined;}+ // if rects aren't aligned (vertically or horizontally)+ // we don't want to put placement between them+ if (distanceX >= 0 && distanceY >= 0) {+ return undefined;+ }+if (distanceX < 0 || (distanceY >= 0 && distanceY < distanceX)) {const minX = Math.min(a.left, b.left);const maxX = Math.max(a.left + a.width, b.left + b.width);@@ -167,32 +173,69 @@ export const getPlacementInside = (};};+const getSegmentsOrder = (+ aStart: number,+ aEnd: number,+ bStart: number,+ bEnd: number+): "a-after-b" | "b-after-a" | "overlap" => {+ if (aStart >= bEnd) {+ return "a-after-b";+ }+ if (bStart >= aEnd) {+ return "b-after-a";+ }+ return "overlap";+};++const getTwoRectsOrientation = (+ first: Rect,+ second: Rect+): ChildrenOrientation => {+ const xOrder = getSegmentsOrder(+ first.left,+ first.left + first.width,+ second.left,+ second.left + second.width+ );+ const yOrder = getSegmentsOrder(+ first.top,+ first.top + first.height,+ second.top,+ second.top + second.height+ );+ if (xOrder !== "overlap" && yOrder === "overlap") {+ return "horizontal";+ }+ if (xOrder === "overlap" && yOrder !== "overlap") {+ return "vertical";+ }+ return "mixed";+};+export const getRectsOrientation = (first: Rect | undefined,second: Rect,third: Rect | undefined): ChildrenOrientation => {- // @todo: We need a slightly more sofisticated way to determine orientation.- // For example, detect this as "vertical":- // [ ]- // [ ]-// @todo: add "vertical-reversed", "horizontal-reversed", "mixed-reversed"- if (- (first === undefined || first.top === second.top) &&- (third === undefined || third.top === second.top)- ) {- return "horizontal";- }+ const orientations = [+ first && getTwoRectsOrientation(first, second),+ third && getTwoRectsOrientation(second, third),+ ];++ const includesVertical = orientations.includes("vertical");+ const includesHorizontal = orientations.includes("horizontal");- if (- (first === undefined || first.left === second.left) &&- (third === undefined || third.left === second.left)- ) {+ if (includesVertical && !includesHorizontal) {return "vertical";}+ if (includesHorizontal && !includesVertical) {+ return "horizontal";+ }+return "mixed";};diff --git a/packages/design-system/src/components/primitives/dnd/use-drag.ts b/packages/design-system/src/components/primitives/dnd/use-drag.tsindex 16d23ea0e4..41d862e2fa 100644--- a/packages/design-system/src/components/primitives/dnd/use-drag.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drag.ts@@ -1,5 +1,6 @@import { useMove } from "./use-move";import { useRef, useEffect, useMemo } from "react";+import { type UsableElement, toUseableElement } from "./dom-utils";type State =| {@@ -21,7 +22,7 @@ const initialState = {export type UseDragProps<DragItemData> = {startDistanceThreashold?: number;shiftDistanceThreshold?: number;- isDragItem: (element: Element) => DragItemData | false;+ isDragItem: (element: UsableElement) => DragItemData | false;onStart: (event: { target: HTMLElement; data: DragItemData }) => void;onMove: (event: { x: number; y: number }) => void;onShiftChange?: (event: { shifts: number }) => void;@@ -63,11 +64,13 @@ export const useDrag = <DragItemData>({const { onPointerDown } = useMove({shouldStart: ({ target }) => {- if (!(target instanceof Element)) {+ const targetElement = toUseableElement(target);++ if (!targetElement) {return false;}- const data = isDragItem(target);+ const data = isDragItem(targetElement);if (data === false) {return false;diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.ts b/packages/design-system/src/components/primitives/dnd/use-drop.tsindex 25a2931d7f..3f7f68a992 100644--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts@@ -6,52 +6,16 @@ import {getPlacementBetween,getPlacementInside,getPlacementNextTo,- getRectsOrientation,getIndexAdjustment,type Rect,- type ChildrenOrientation,type Placement,} from "./geometry-utils";--// @todo: use this in useDrag as well-type UsableElement = HTMLElement | SVGElement;-const toUseableElement = (- element: Element | undefined | null-): UsableElement | undefined => {- if (element instanceof HTMLElement || element instanceof SVGElement) {- return element;- }-};--// By looking at a specific child and it's neighbours,-// determines their orientation relative to each other-const getLocalChildrenOrientation = (- parent: UsableElement,- childrentRects: Rect[],- childIndex: number-): ChildrenOrientation => {- const previous = childrentRects[childIndex - 1] as Rect | undefined;- const current = childrentRects[childIndex] as Rect | undefined;- const next = childrentRects[childIndex + 1] as Rect | undefined;-- if (current === undefined || (next === undefined && previous === undefined)) {- const probe = document.createElement("div");- const { children } = parent;- if (childIndex > children.length - 1) {- parent.appendChild(probe);- } else {- parent.insertBefore(probe, children[childIndex]);- }- const probeRect = probe.getBoundingClientRect();- parent.removeChild(probe);-- return probeRect.width === 0 && probeRect.height !== 0- ? "horizontal"- : "vertical";- }-- return getRectsOrientation(previous, current, next);-};+import {+ type UsableElement,+ toUseableElement,+ getLocalChildrenOrientation,+ getChildrenRects,+} from "./dom-utils";// Partial information about a drop target// used during the selection of a new drop target@@ -116,25 +80,12 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {// We want to return a stable object to avoid re-renders when it's a dependencyreturn useMemo(() => {- const getChildrenRects = (parent: UsableElement, parentRect: Rect) => {+ const getChildrenRectsMemoized = (parent: UsableElement) => {const fromCache = state.current.childrenRectsCache.get(parent);if (fromCache !== undefined) {return fromCache;}-- // We convert to relative coordinates to be able to store the result in cache.- // Otherwise we would have to clear cache on scroll.- const toRelativeCoordinates = (rect: Rect) => ({- left: rect.left - parentRect.left,- top: rect.top - parentRect.top,- width: rect.width,- height: rect.height,- });-- const result = Array.from(parent.children).map((child) =>- toRelativeCoordinates(child.getBoundingClientRect())- );-+ const result = getChildrenRects(parent);state.current.childrenRectsCache.set(parent, result);return result;};@@ -152,10 +103,7 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {y: pointerCoordinates.y - parentRect.top,};- const childrenRects = getChildrenRects(- partialDropTarget.element,- parentRect- );+ const childrenRects = getChildrenRectsMemoized(partialDropTarget.element);const closestChildIndex =childrenRects.length === 0@@ -302,7 +250,6 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {return {handleMove(pointerCoordinates) {- // console.log("handleMove", pointerCoordinates);state.current.pointerCoordinates = pointerCoordinates;detectTarget();},@@ -322,9 +269,6 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {}, []);};-// @todo: maybe rather than climbing the DOM tree,-// we should use document.elementsFromPoint() array?-// Might work better with absolutly positioned elements.const findClosestDropTarget = <Data>({root,initialElement,