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.

crude implementation of placement indicator

Roman Pominovd8e65066cc0b
diff --git a/apps/designer/app/canvas/shared/nano-states.ts b/apps/designer/app/canvas/shared/nano-states.ts
index 3e627733d0..88e6ff9a07 100644
--- a/apps/designer/app/canvas/shared/nano-states.ts
+++ b/apps/designer/app/canvas/shared/nano-states.ts
@@ -1,9 +1,9 @@
import { createValueContainer, useValue } from "react-nano-state";
import { type Instance } from "@webstudio-is/react-sdk";
-import { type DropData } from "~/shared/canvas-components";
+// import { type DropData } from "~/shared/canvas-components";
-const dropDataContainer = createValueContainer<DropData | undefined>();
-export const useDropData = () => useValue(dropDataContainer);
+// const dropDataContainer = createValueContainer<DropData | undefined>();
+// export const useDropData = () => useValue(dropDataContainer);
export const selectedInstanceContainer = createValueContainer<
Instance | undefined
diff --git a/apps/designer/app/canvas/shared/use-drag-drop-handlers.ts b/apps/designer/app/canvas/shared/use-drag-drop-handlers.ts
index a6d3939f70..861a6c616f 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop-handlers.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop-handlers.ts
@@ -1,119 +1,122 @@
-import { useState } from "react";
-import { publish, useSubscribe } from "@webstudio-is/react-sdk";
-import { type DragData, type DropData } from "~/shared/canvas-components";
-import { findInstanceById } from "~/shared/tree-utils";
-import {
- findClosestChild,
- findInsertionIndex,
- getDragOverInfo,
-} from "~/shared/dom-utils";
-import {
- useDropData,
- useHoveredElement,
- useHoveredInstance,
- useSelectedInstance,
-} from "./nano-states";
-import { useRootInstance } from "~/shared/nano-states";
-import memoize from "lodash.memoize";
-//import {usePointerOutline} from './use-pointer-outline'
-
-const getBoundingClientRect = memoize((element: Element | HTMLElement) =>
- element.getBoundingClientRect()
-);
-
-const getComputedStyle = memoize((element: Element | HTMLElement) =>
- window.getComputedStyle(element)
-);
-
-export const useDragDropHandlers = () => {
- const [rootInstance] = useRootInstance();
- const [, setSelectedInstance] = useSelectedInstance();
- const [, setHoveredInstance] = useHoveredInstance();
- const [, setHoveredElement] = useHoveredElement();
- const [dropData, setDropData] = useDropData();
- const [dragData, setDragData] = useState<DragData>();
-
- useSubscribe<"dragStartInstance">("dragStartInstance", () => {
- setSelectedInstance(undefined);
- });
-
- useSubscribe<"dragEndInstance">("dragEndInstance", () => {
- // Cleanup
- if (getBoundingClientRect.cache?.clear) getBoundingClientRect.cache.clear();
- if (getComputedStyle.cache?.clear) getComputedStyle.cache.clear();
- setDropData(undefined);
- setDragData(undefined);
-
- if (
- rootInstance === undefined ||
- dropData === undefined ||
- dragData === undefined ||
- // Can't reparent an instance inside itself
- dropData.instance.id === dragData.instance.id
- ) {
- return;
- }
-
- const isNew =
- findInstanceById(rootInstance, dragData.instance.id) === undefined;
-
- const data = {
- instance: dragData.instance,
- dropData,
- };
-
- if (isNew) {
- publish<"insertInstance", typeof data>({
- type: "insertInstance",
- payload: data,
- });
- return;
- }
-
- publish<"reparentInstance", typeof data>({
- type: "reparentInstance",
- payload: data,
- });
- });
-
- //const updatePointerOutline = usePointerOutline();
- useSubscribe<"dragInstance", DragData>("dragInstance", (dragData) => {
- const { currentOffset } = dragData;
- // updatePointerOutline(currentOffset)
- const dragOver = getDragOverInfo(currentOffset, getBoundingClientRect);
-
- if (rootInstance === undefined || dragOver.element === undefined) return;
-
- const dropInstance = findInstanceById(rootInstance, dragOver.element.id);
-
- if (dropInstance === undefined) return;
-
- const closestChild = findClosestChild(
- dragOver.element,
- currentOffset,
- getBoundingClientRect,
- getComputedStyle
- );
-
- let position = 0;
-
- // When element has children.
- if (dragOver.element !== undefined && closestChild !== undefined) {
- position = findInsertionIndex(dragOver, closestChild);
- }
-
- const dropData = {
- instance: dropInstance,
- position,
- };
-
- setDragData(dragData);
- setDropData(dropData);
- setHoveredInstance(dropInstance);
- setHoveredElement(dragOver.element);
- publish<"dropPreview", { dropData: DropData; dragData: DragData }>({
- type: "dropPreview",
- payload: { dropData, dragData },
- });
- });
-};
+// import { useState } from "react";
+// import { publish, useSubscribe } from "@webstudio-is/react-sdk";
+// import { type DragData, type DropData } from "~/shared/canvas-components";
+// import { findInstanceById } from "~/shared/tree-utils";
+// import {
+// findClosestChild,
+// findInsertionIndex,
+// getDragOverInfo,
+// } from "~/shared/dom-utils";
+// import {
+// useDropData,
+// useHoveredElement,
+// useHoveredInstance,
+// useSelectedInstance,
+// } from "./nano-states";
+// import { useRootInstance } from "~/shared/nano-states";
+// import memoize from "lodash.memoize";
+// //import {usePointerOutline} from './use-pointer-outline'
+
+// const getBoundingClientRect = memoize((element: Element | HTMLElement) =>
+// element.getBoundingClientRect()
+// );
+
+// const getComputedStyle = memoize((element: Element | HTMLElement) =>
+// window.getComputedStyle(element)
+// );
+
+// export const useDragDropHandlers = () => {
+// const [rootInstance] = useRootInstance();
+// const [, setSelectedInstance] = useSelectedInstance();
+// const [, setHoveredInstance] = useHoveredInstance();
+// const [, setHoveredElement] = useHoveredElement();
+// const [dropData, setDropData] = useDropData();
+// const [dragData, setDragData] = useState<DragData>();
+
+// useSubscribe<"dragStartInstance">("dragStartInstance", () => {
+// setSelectedInstance(undefined);
+// });
+
+// useSubscribe<"dragEndInstance">("dragEndInstance", () => {
+// // Cleanup
+// if (getBoundingClientRect.cache?.clear) getBoundingClientRect.cache.clear();
+// if (getComputedStyle.cache?.clear) getComputedStyle.cache.clear();
+// setDropData(undefined);
+// setDragData(undefined);
+
+// if (
+// rootInstance === undefined ||
+// dropData === undefined ||
+// dragData === undefined ||
+// // Can't reparent an instance inside itself
+// dropData.instance.id === dragData.instance.id
+// ) {
+// return;
+// }
+
+// const isNew =
+// findInstanceById(rootInstance, dragData.instance.id) === undefined;
+
+// const data = {
+// instance: dragData.instance,
+// dropData,
+// };
+
+// if (isNew) {
+// publish<"insertInstance", typeof data>({
+// type: "insertInstance",
+// payload: data,
+// });
+// return;
+// }
+
+// publish<"reparentInstance", typeof data>({
+// type: "reparentInstance",
+// payload: data,
+// });
+// });
+
+// //const updatePointerOutline = usePointerOutline();
+// useSubscribe<"dragInstance", DragData>("dragInstance", (dragData) => {
+// const { currentOffset } = dragData;
+// // updatePointerOutline(currentOffset)
+// const dragOver = getDragOverInfo(currentOffset, getBoundingClientRect);
+
+// if (rootInstance === undefined || dragOver.element === undefined) return;
+
+// const dropInstance = findInstanceById(rootInstance, dragOver.element.id);
+
+// if (dropInstance === undefined) return;
+
+// const closestChild = findClosestChild(
+// dragOver.element,
+// currentOffset,
+// getBoundingClientRect,
+// getComputedStyle
+// );
+
+// let position = 0;
+
+// // When element has children.
+// if (dragOver.element !== undefined && closestChild !== undefined) {
+// position = findInsertionIndex(dragOver, closestChild);
+// }
+
+// const dropData = {
+// instance: dropInstance,
+// position,
+// };
+
+// setDragData(dragData);
+// setDropData(dropData);
+// setHoveredInstance(dropInstance);
+// setHoveredElement(dragOver.element);
+// publish<"dropPreview", { dropData: DropData; dragData: DragData }>({
+// type: "dropPreview",
+// payload: { dropData, dragData },
+// });
+// });
+// };
+
+// @todo: delete this file
+export const tmp = 1;
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 40a596d7d2..b1694c7062 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -1,20 +1,57 @@
-import React, { useEffect } from "react";
+import React, { useEffect, useRef } from "react";
import { useRootInstance } from "~/shared/nano-states";
import {
useDropTarget,
useDrag,
usePlacement,
+ type Rect,
+ type DropTarget,
} from "~/shared/design-system/components/primitives/dnd";
import { findInstanceById, getInstancePath } from "~/shared/tree-utils";
import { primitives } from "~/shared/canvas-components";
-import { type Instance } from "@webstudio-is/react-sdk";
+import { publish, type Instance } from "@webstudio-is/react-sdk";
+
+// This is used in tree-preview
+// @todo: Update it to use the new events, or fire these events as well.
+export type DragData = { instance: Instance };
+export type DropData = {
+ instance: { id: Instance["id"] };
+ position: number | "end";
+};
+// publish<"dropPreview", { dropData: DropData; dragData: DragData }>({
+// type: "dropPreview",
+// payload: { dropData, dragData },
+// });
+
+// data shared between iframe and main window
+export type DropTargetSharedData = { rect: Rect; placementRect: Rect };
export const useDragAndDrop = () => {
const [rootInstance] = useRootInstance();
+ const state = useRef({
+ dropTarget: undefined as DropTarget<Instance> | undefined,
+ placement: undefined as { index: number; placementRect: Rect } | undefined,
+ });
+
+ const publishDropTargetChange = () => {
+ const { dropTarget, placement } = state.current;
+ if (dropTarget === undefined || placement === undefined) {
+ return;
+ }
+ publish<"dropTargetChange", DropTargetSharedData>({
+ type: "dropTargetChange",
+ payload: {
+ rect: dropTarget.rect,
+ placementRect: placement.placementRect,
+ },
+ });
+ };
+
const placementHandlers = usePlacement({
onPlacementChange: (placement) => {
- console.log(placement);
+ state.current.placement = placement;
+ publishDropTargetChange();
},
});
@@ -48,6 +85,8 @@ export const useDragAndDrop = () => {
return dropTarget;
}
+ // @todo: Don't allow to dpop inside drag item or any of its children
+
const path = getInstancePath(rootInstance, dropTarget.data.id);
path.reverse().shift();
@@ -65,19 +104,24 @@ export const useDragAndDrop = () => {
},
onDropTargetChange(dropTarget) {
+ state.current.dropTarget = dropTarget;
placementHandlers.handleTargetChange(dropTarget.element);
+
+ // @todo: The line above may or may not fire publishDropTargetChange as well.
+ // So we're firing duplicate events.
+ // One more reason to merge useDropTarget and usePlacement.
+ publishDropTargetChange();
},
});
const dragProps = useDrag({
onStart(_event) {
- // const id = event.target.dataset.id;
- // if (id == null) {
- // event.cancel();
- // return;
- // }
- // setDragItemId(id);
// autoScrollHandlers.setEnabled(true);
+
+ // @todo: Find drag item instance or cancel the event
+
+ // @todo: Pass drag item instance id
+ publish<"dragStart">({ type: "dragStart" });
},
onMove: (poiterCoordinate) => {
dropTargetHandlers.handleMove(poiterCoordinate);
@@ -88,9 +132,8 @@ export const useDragAndDrop = () => {
dropTargetHandlers.handleEnd();
// autoScrollHandlers.setEnabled(false);
placementHandlers.handleEnd();
- // setDragItemId(undefined);
- // setPalcement(undefined);
- // dropTargetId.current = undefined;
+
+ publish<"dragEnd">({ type: "dragEnd" });
},
});
@@ -124,5 +167,5 @@ export const useDragAndDrop = () => {
// NOTE: need to make the dependencies more stable,
// because as is this will fire on every render
- }, [dragProps, dropTargetHandlers]);
+ }, [dragProps, dropTargetHandlers, placementHandlers]);
};
diff --git a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
index fd70b8a7f4..ed606f3f20 100644
--- a/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
+++ b/apps/designer/app/designer/features/tree-preview/tree-preview.tsx
@@ -2,7 +2,7 @@ import { Instance, useSubscribe } from "@webstudio-is/react-sdk";
import produce from "immer";
import { useState } from "react";
import { Tree } from "~/designer/shared/tree";
-import { type DragData, type DropData } from "~/shared/canvas-components";
+import { type DragData, type DropData } from "~/canvas/shared/use-drag-drop";
import { Flex } from "~/shared/design-system";
import { useRootInstance } from "~/shared/nano-states";
import {
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/canvas-tools.tsx b/apps/designer/app/designer/features/workspace/canvas-tools/canvas-tools.tsx
index 425c5f68c3..bc2bfe2334 100644
--- a/apps/designer/app/designer/features/workspace/canvas-tools/canvas-tools.tsx
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/canvas-tools.tsx
@@ -1,11 +1,18 @@
import { type Publish } from "@webstudio-is/react-sdk";
import { Box } from "~/shared/design-system";
+import { PlacementIndicator } from "~/shared/design-system/components/primitives/dnd";
import {
useIsPreviewMode,
useIsScrolling,
useSubscribeScrollState,
+ useDragAndDropState,
+ useSubscribeDragAndDropState,
} from "~/shared/nano-states";
-import { HoveredInstanceOutline, SelectedInstanceOutline } from "./outline";
+import {
+ HoveredInstanceOutline,
+ SelectedInstanceOutline,
+ DropTargetOutline,
+} from "./outline";
import { TextToolbar } from "./text-toolbar";
import { useSubscribeInstanceRect } from "./hooks/use-subscribe-instance-rect";
import { useSubscribeSelectionRect } from "./hooks/use-subscribe-selection-rect";
@@ -29,10 +36,25 @@ export const CanvasTools = ({ publish }: CanvasToolsProps) => {
useSubscribeInstanceRect();
useSubscribeSelectionRect();
useSubscribeScrollState();
+ useSubscribeDragAndDropState();
useSubscribeTextEditingInstanceId();
const [isPreviewMode] = useIsPreviewMode();
const [isScrolling] = useIsScrolling();
+ const [dragAndDropState] = useDragAndDropState();
+
+ if (
+ dragAndDropState.isDragging &&
+ dragAndDropState.dropTarget !== undefined
+ ) {
+ return (
+ <Box css={toolsStyle}>
+ <DropTargetOutline dropTarget={dragAndDropState.dropTarget} />
+ <PlacementIndicator rect={dragAndDropState.dropTarget.placementRect} />
+ </Box>
+ );
+ }
+
if (isPreviewMode || isScrolling) {
return null;
}
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/outline/drop-target-outline.tsx b/apps/designer/app/designer/features/workspace/canvas-tools/outline/drop-target-outline.tsx
new file mode 100644
index 0000000000..fc8d758766
--- /dev/null
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/outline/drop-target-outline.tsx
@@ -0,0 +1,16 @@
+import { Outline } from "./outline";
+import { DropTargetSharedData } from "~/canvas/shared/use-drag-drop";
+import { Label } from "./label";
+
+export const DropTargetOutline = ({
+ dropTarget,
+}: {
+ dropTarget: DropTargetSharedData;
+}) => {
+ return (
+ <Outline rect={dropTarget.rect}>
+ {/* @todo: real component name */}
+ <Label component={"Box"} instanceRect={dropTarget.rect} />
+ </Outline>
+ );
+};
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/outline/index.ts b/apps/designer/app/designer/features/workspace/canvas-tools/outline/index.ts
index 1cf83e8390..4b15c92ef0 100644
--- a/apps/designer/app/designer/features/workspace/canvas-tools/outline/index.ts
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/outline/index.ts
@@ -1,2 +1,3 @@
export { SelectedInstanceOutline } from "./selected-instance-outline";
export { HoveredInstanceOutline } from "./hovered-instance-outline";
+export { DropTargetOutline } from "./drop-target-outline";
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/outline/label.tsx b/apps/designer/app/designer/features/workspace/canvas-tools/outline/label.tsx
index 3541b5d55e..0268ec8df2 100644
--- a/apps/designer/app/designer/features/workspace/canvas-tools/outline/label.tsx
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/outline/label.tsx
@@ -3,6 +3,7 @@ import { styled } from "~/shared/design-system";
import { primitives } from "~/shared/canvas-components";
import { type Instance } from "@webstudio-is/react-sdk";
+type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
type LabelPosition = "top" | "inside" | "bottom";
type LabelRefCallback = (element: HTMLElement | null) => void;
@@ -13,7 +14,7 @@ type LabelRefCallback = (element: HTMLElement | null) => void;
* - else inside-top - last resort because it covers a bit of the instance content
*/
const useLabelPosition = (
- instanceRect: DOMRect
+ instanceRect: Rect
): [LabelRefCallback, LabelPosition] => {
const [position, setPosition] = useState<LabelPosition>("top");
@@ -70,7 +71,7 @@ const LabelContainer = styled(
type LabelProps = {
component: Instance["component"];
- instanceRect: DOMRect;
+ instanceRect: Rect;
};
export const Label = ({ component, instanceRect }: LabelProps) => {
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/outline/outline.tsx b/apps/designer/app/designer/features/workspace/canvas-tools/outline/outline.tsx
index 511a6d33eb..5e2c4adee8 100644
--- a/apps/designer/app/designer/features/workspace/canvas-tools/outline/outline.tsx
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/outline/outline.tsx
@@ -1,6 +1,8 @@
import { useMemo } from "react";
import { styled } from "~/shared/design-system";
+type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
+
const OutlineContainer = styled("div", {
position: "absolute",
pointerEvents: "none",
@@ -10,7 +12,7 @@ const OutlineContainer = styled("div", {
left: 0,
});
-const useStyle = (rect?: DOMRect) => {
+const useStyle = (rect?: Rect) => {
return useMemo(() => {
if (rect === undefined) return;
return {
@@ -23,7 +25,7 @@ const useStyle = (rect?: DOMRect) => {
type OutlineProps = {
children: JSX.Element;
- rect?: DOMRect;
+ rect?: Rect;
};
export const Outline = ({ children, rect }: OutlineProps) => {
diff --git a/apps/designer/app/shared/canvas-components/draggable.ts b/apps/designer/app/shared/canvas-components/draggable.ts
deleted file mode 100644
index 7f44becafc..0000000000
--- a/apps/designer/app/shared/canvas-components/draggable.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { type Instance } from "@webstudio-is/react-sdk";
-
-interface XYCoord {
- x: number;
- y: number;
-}
-
-export type DragData = {
- instance: Instance;
- currentOffset: XYCoord;
-};
-
-export type DropData = {
- instance: Instance;
- position: number | "end";
-};
diff --git a/apps/designer/app/shared/canvas-components/index.ts b/apps/designer/app/shared/canvas-components/index.ts
index 31680dcff4..284ba59cba 100644
--- a/apps/designer/app/shared/canvas-components/index.ts
+++ b/apps/designer/app/shared/canvas-components/index.ts
@@ -1,3 +1,2 @@
export * from "./instance-data";
export * as primitives from "./primitives";
-export * from "./draggable";
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.ts
index bdf9131cd8..105ee80544 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
@@ -2,7 +2,7 @@ import { useRef } from "react";
export type Area = "top" | "right" | "bottom" | "left" | "center";
-type DropTarget<Data> = {
+export type DropTarget<Data> = {
data: Data;
element: HTMLElement;
rect: DOMRect;
diff --git a/apps/designer/app/shared/nano-states/index.ts b/apps/designer/app/shared/nano-states/index.ts
index fdd3e37a94..bf06cc8410 100644
--- a/apps/designer/app/shared/nano-states/index.ts
+++ b/apps/designer/app/shared/nano-states/index.ts
@@ -1,2 +1,3 @@
export * from "./nano-states";
export * from "./use-subscribe-scroll-state";
+export * from "./use-subscribe-drag-drop-state";
diff --git a/apps/designer/app/shared/nano-states/nano-states.ts b/apps/designer/app/shared/nano-states/nano-states.ts
index f8a24be2cb..91f6c45152 100644
--- a/apps/designer/app/shared/nano-states/nano-states.ts
+++ b/apps/designer/app/shared/nano-states/nano-states.ts
@@ -1,5 +1,6 @@
import { createValueContainer, useValue } from "react-nano-state";
import { type Breakpoint, type Instance } from "@webstudio-is/react-sdk";
+import { DropTargetSharedData } from "~/canvas/shared/use-drag-drop";
export const rootInstanceContainer = createValueContainer<
Instance | undefined
@@ -33,3 +34,11 @@ const textEditingInstanceIdContainer = createValueContainer<
>();
export const useTextEditingInstanceId = () =>
useValue(textEditingInstanceIdContainer);
+
+export type DragAndDropState =
+ | { isDragging: false }
+ | { isDragging: true; dropTarget?: DropTargetSharedData };
+const dragAndDropStateContainer = createValueContainer<DragAndDropState>({
+ isDragging: false,
+});
+export const useDragAndDropState = () => useValue(dragAndDropStateContainer);
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.ts
new file mode 100644
index 0000000000..98af31e9a3
--- /dev/null
+++ b/apps/designer/app/shared/nano-states/use-subscribe-drag-drop-state.ts
@@ -0,0 +1,24 @@
+import { useSubscribe } from "@webstudio-is/react-sdk";
+import { useDragAndDropState } from "./nano-states";
+import { DropTargetSharedData } from "~/canvas/shared/use-drag-drop";
+
+export const useSubscribeDragAndDropState = () => {
+ const [state, setState] = useDragAndDropState();
+
+ useSubscribe<"dropTargetChange", DropTargetSharedData>(
+ "dropTargetChange",
+ (dropTarget) => {
+ if (state.isDragging) {
+ setState({ ...state, dropTarget });
+ }
+ }
+ );
+
+ useSubscribe<"dragStart">("dragStart", () => {
+ setState({ isDragging: true });
+ });
+
+ useSubscribe<"dragEnd">("dragEnd", () => {
+ setState({ isDragging: false });
+ });
+};