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.

allow to pass data from isDragItem to onStart

Roman Pominov2a7d3fcf8e4f
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 36ac8b85e1..54cbf9ea09 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -138,32 +138,31 @@ export const useDragAndDrop = () => {
},
});
- const useDragHandlers = useDrag({
+ const useDragHandlers = useDrag<Instance>({
isDragItem(element) {
if (rootInstance === undefined || element.id === "") {
return false;
}
+
const instance = findInstanceById(rootInstance, element.id);
- return (
- instance !== undefined &&
- instance.id !== rootInstance.id &&
- // We can't drag if we are editing text.
- instance.id !== textEditingInstanceId
- );
- },
- onStart(event) {
- const instance =
- rootInstance !== undefined &&
- event.target.id !== "" &&
- findInstanceById(rootInstance, event.target.id);
-
- // For TypeScript
- // @todo: Allow to pass information from isDragItem to here,
- // so we wouldn't need to do this?
- if (!instance) {
- return;
+
+ if (instance === undefined) {
+ return false;
}
+ // We can't drag if we are editing text
+ if (instance.id === textEditingInstanceId) {
+ return false;
+ }
+
+ // Cannot drag root
+ if (instance.id === rootInstance.id) {
+ return false;
+ }
+
+ return instance;
+ },
+ onStart({ data: instance }) {
state.current.dragItem = instance;
autoScrollHandlers.setEnabled(true);
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/components/components.tsx b/apps/designer/app/designer/features/sidebar-left/panels/components/components.tsx
index f7368e059e..f302f33c0d 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/components/components.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/components/components.tsx
@@ -100,27 +100,21 @@ export const TabContent = ({ publish, onSetActiveTab }: TabContentProps) => {
return { x: (x - canvasRect.x) / scale, y: (y - canvasRect.y) / scale };
};
- const useDragHandlers = useDrag({
+ const useDragHandlers = useDrag<Instance["component"]>({
isDragItem(element) {
- return elementToComponentName(element) !== undefined;
- },
- onStart(event) {
- const component = elementToComponentName(event.target);
-
- // For TypeScript
- // @todo: Allow to pass information from isDragItem to here,
- // so we wouldn't need to do this?
- if (!component) {
- return;
+ const componentName = elementToComponentName(element);
+ if (componentName === undefined) {
+ return false;
}
-
- setDragComponent(component);
-
+ return componentName;
+ },
+ onStart({ data: componentName }) {
+ setDragComponent(componentName);
publish<"dragStart", DragStartPayload>({
type: "dragStart",
payload: {
origin: "panel",
- dragItem: createInstance({ component }),
+ dragItem: createInstance({ component: componentName }),
},
});
},
diff --git a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx
index f3f6d798d7..360c419d29 100644
--- a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx
+++ b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx
@@ -201,15 +201,15 @@ export const Canvas = () => {
const autoScrollHandlers = useAutoScroll();
- const useDragHandlers = useDrag({
+ const useDragHandlers = useDrag<string>({
isDragItem(element) {
- return element instanceof HTMLElement && element.dataset.id != null;
- },
- onStart(event) {
- const id = event.target.dataset.id;
- if (id == null) {
- return;
+ const id = element instanceof HTMLElement && element.dataset.id;
+ if (id) {
+ return id;
}
+ return false;
+ },
+ onStart({ data: id }) {
setDragItemId(id);
autoScrollHandlers.setEnabled(true);
},
diff --git a/packages/design-system/src/components/primitives/dnd/placement.tsx b/packages/design-system/src/components/primitives/dnd/placement.tsx
index 13969d61f4..69b69d7990 100644
--- a/packages/design-system/src/components/primitives/dnd/placement.tsx
+++ b/packages/design-system/src/components/primitives/dnd/placement.tsx
@@ -71,15 +71,13 @@ export const usePlacement = (props: UsePlacementProps): Handlers => {
const result: Rect[] = [];
for (const child of parent.children) {
- if (child instanceof HTMLElement) {
- const rect = child.getBoundingClientRect();
- result.push({
- top: rect.top - parentRect.top,
- left: rect.left - parentRect.left,
- width: rect.width,
- height: rect.height,
- });
- }
+ const rect = child.getBoundingClientRect();
+ result.push({
+ top: rect.top - parentRect.top,
+ left: rect.left - parentRect.left,
+ width: rect.width,
+ height: rect.height,
+ });
}
state.current.childrenRectsCache.set(parent, result);
diff --git a/packages/design-system/src/components/primitives/dnd/use-drag.ts b/packages/design-system/src/components/primitives/dnd/use-drag.ts
index d992b0cac5..8dffecd8ef 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drag.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drag.ts
@@ -18,11 +18,11 @@ const initialState = {
status: "idle",
} as const;
-export type UseDragProps = {
+export type UseDragProps<DragItemData> = {
startDistanceThreashold?: number;
shiftDistanceThreshold?: number;
- isDragItem: (element: Element) => boolean;
- onStart: (event: { target: HTMLElement }) => void;
+ isDragItem: (element: Element) => DragItemData | false;
+ onStart: (event: { target: HTMLElement; data: DragItemData }) => void;
onMove: (event: { x: number; y: number }) => void;
onShiftChange?: (event: { shifts: number }) => void;
onEnd: () => void;
@@ -32,18 +32,18 @@ export type UseDragHandlers = {
rootRef: (element: HTMLElement | null) => void;
};
-export const useDrag = ({
+export const useDrag = <DragItemData>({
startDistanceThreashold = 3,
shiftDistanceThreshold = 20,
onStart,
onMove,
- // eslint-disable-next-line @typescript-eslint/no-empty-function
- onShiftChange = () => {},
+ onShiftChange,
onEnd,
isDragItem,
-}: UseDragProps): UseDragHandlers => {
+}: UseDragProps<DragItemData>): UseDragHandlers => {
const state = useRef<State>(initialState);
const rootRef = useRef<HTMLElement | null>(null);
+ const dragItemData = useRef<DragItemData>();
const detectShift = () => {
if (state.current.status !== "dragging") {
@@ -57,13 +57,25 @@ export const useDrag = ({
if (shifts !== state.current.shifts) {
state.current.shifts = shifts;
- onShiftChange({ shifts });
+ onShiftChange?.({ shifts });
}
};
const { onPointerDown } = useMove({
- shouldStart: (e) => {
- return e.target instanceof Element && isDragItem(e.target);
+ shouldStart: ({ target }) => {
+ if (!(target instanceof Element)) {
+ return false;
+ }
+
+ const data = isDragItem(target);
+
+ if (data === false) {
+ return false;
+ }
+
+ dragItemData.current = data;
+
+ return true;
},
onMoveStart({
clientX: x,
@@ -82,7 +94,9 @@ export const useDrag = ({
initialY: y,
shifts: 0,
};
- onStart({ target });
+ if (dragItemData.current !== undefined) {
+ onStart({ target, data: dragItemData.current });
+ }
},
onMove({ clientX: x, clientY: y }: { clientX: number; clientY: number }) {
// We want to start dragging only when the user has moved more than startDistanceThreashold.