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
identify drag item & add some todos
Roman Pominovdaf9f31fef28
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.tsindex f5b0b6f075..7bf6360b59 100644--- a/apps/designer/app/canvas/shared/use-drag-drop.ts+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts@@ -32,13 +32,16 @@ export type DropTargetSharedData = {instanceComponent: Instance["component"];};+const initialState = {+ dropTarget: undefined as DropTarget<Instance> | undefined,+ placement: undefined as { index: number; placementRect: Rect } | undefined,+ dragItem: undefined as Instance | undefined,+};+export const useDragAndDrop = () => {const [rootInstance] = useRootInstance();- const state = useRef({- dropTarget: undefined as DropTarget<Instance> | undefined,- placement: undefined as { index: number; placementRect: Rect } | undefined,- });+ const state = useRef({ ...initialState });const publishDropTargetChange = () => {const { dropTarget, placement } = state.current;@@ -125,13 +128,29 @@ export const useDragAndDrop = () => {});const dragProps = useDrag({- onStart(_event) {- // @todo: Find drag item instance or cancel the event+ onStart(event) {+ const instance =+ rootInstance !== undefined &&+ event.target.id !== "" &&+ findInstanceById(rootInstance, event.target.id);++ // @todo: If we can't find the data corresponding to the target element,+ // should we climb up the DOM tree for another element?+ // Should the hook do that for us?++ if (!instance || instance.id === rootInstance.id) {+ event.cancel();+ return;+ }++ state.current.dragItem = instance;autoScrollHandlers.setEnabled(true);- // @todo: Pass drag item instance id- publish<"dragStart">({ type: "dragStart" });+ publish<"dragStart", { dragItem: { instanceId: Instance["id"] } }>({+ type: "dragStart",+ payload: { dragItem: { instanceId: instance.id } },+ });},onMove: (poiterCoordinate) => {dropTargetHandlers.handleMove(poiterCoordinate);@@ -142,8 +161,8 @@ export const useDragAndDrop = () => {dropTargetHandlers.handleEnd();autoScrollHandlers.setEnabled(false);placementHandlers.handleEnd();-publish<"dragEnd">({ type: "dragEnd" });+ state.current = { ...initialState };},});diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target2.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target2.tsindex 105ee80544..ccf9c20965 100644--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target2.ts+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target2.ts@@ -31,6 +31,10 @@ export type UseDropTargetProps<Data> = {onDropTargetChange: (dropTarget: DropTarget<Data>) => void;edgeDistanceThreshold?: number;++ // @todo: not sure we should do any data comparisons here.+ // On the contrary, we may want to rely on the assumption+ // that the data for the same element is always the same (for caching)isSameData?: (data1: Data, data2: Data) => boolean;};@@ -65,6 +69,7 @@ export const useDropTarget = <Data>({return;}+ // @todo: cache this? Not expensive by itself, but it may call isDropTarget a lotconst potentialTarget = findClosestDropTarget({root,target,@@ -142,6 +147,8 @@ const elementFromPoint = (coordinate: Coordinate): HTMLElement | undefined => {if (element instanceof HTMLElement) return element;};+// @todo: it seems we only care about whether the area is center or on an edge.+// Knowing which edge doesn't help much.const getArea = (pointerCoordinate: Coordinate,edgeDistanceThreshold: number,@@ -161,6 +168,9 @@ const getArea = (return area;};+// @todo: maybe rather than climbing the DOM tree,+// we should use document.elementsFromPoint() array?+// Might work better with absolute positioned elements.const findClosestDropTarget = <Data>({root,target,diff --git a/apps/designer/app/shared/nano-states/nano-states.ts b/apps/designer/app/shared/nano-states/nano-states.tsindex 91f6c45152..c41c2febf7 100644--- a/apps/designer/app/shared/nano-states/nano-states.ts+++ b/apps/designer/app/shared/nano-states/nano-states.ts@@ -37,7 +37,11 @@ export const useTextEditingInstanceId = () =>export type DragAndDropState =| { isDragging: false }- | { isDragging: true; dropTarget?: DropTargetSharedData };+ | {+ isDragging: true;+ dropTarget?: DropTargetSharedData;+ dragItem: { instanceId: Instance["id"] };+ };const dragAndDropStateContainer = createValueContainer<DragAndDropState>({isDragging: false,});diff --git a/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.ts b/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.tsindex 98af31e9a3..8e995465fd 100644--- a/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.ts+++ b/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.ts@@ -1,4 +1,4 @@-import { useSubscribe } from "@webstudio-is/react-sdk";+import { useSubscribe, type Instance } from "@webstudio-is/react-sdk";import { useDragAndDropState } from "./nano-states";import { DropTargetSharedData } from "~/canvas/shared/use-drag-drop";@@ -14,9 +14,12 @@ export const useSubscribeDragAndDropState = () => {});- useSubscribe<"dragStart">("dragStart", () => {- setState({ isDragging: true });- });+ useSubscribe<"dragStart", { dragItem: { instanceId: Instance["id"] } }>(+ "dragStart",+ ({ dragItem }) => {+ setState({ isDragging: true, dragItem });+ }+ );useSubscribe<"dragEnd">("dragEnd", () => {setState({ isDragging: false });