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.

fix clash between useMove and text editing

Roman Pominov605b96f62f2e
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 845119228e..067169cb23 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -1,5 +1,8 @@
import { useLayoutEffect, useRef } from "react";
-import { useRootInstance } from "~/shared/nano-states";
+import {
+ useRootInstance,
+ useTextEditingInstanceId,
+} from "~/shared/nano-states";
import { findInstanceById, getInstancePath } from "~/shared/tree-utils";
import {
type DropTarget,
@@ -34,6 +37,7 @@ const initialState = {
export const useDragAndDrop = () => {
const [rootInstance] = useRootInstance();
+ const [textEditingInstanceId] = useTextEditingInstanceId();
const state = useRef({ ...initialState });
@@ -127,21 +131,28 @@ export const useDragAndDrop = () => {
});
const useDragHandlers = useDrag({
+ 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);
- if (!instance || instance.id === rootInstance.id) {
- event.cancel();
+ if (!instance) {
return;
}
- // @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?
-
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 7e3628cd1b..37f87c28a1 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
@@ -87,15 +87,17 @@ export const TabContent = ({ publish, onSetActiveTab }: TabContentProps) => {
};
const useDragHandlers = useDrag({
+ isDragItem(element) {
+ const { dragComponent } = element.dataset;
+ return (
+ dragComponent != null &&
+ (componentNames as string[]).includes(dragComponent)
+ );
+ },
onStart(event) {
const { dragComponent } = event.target.dataset;
-
- const component =
- dragComponent != null &&
- componentNames.find((c) => c === dragComponent);
-
+ const component = componentNames.find((c) => c === dragComponent);
if (!component) {
- event.cancel();
return;
}
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 a694bf15c6..9dd0ebaadd 100644
--- a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx
+++ b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx
@@ -202,10 +202,12 @@ export const Canvas = () => {
const autoScrollHandlers = useAutoScroll();
const useDragHandlers = useDrag({
+ isDragItem(element) {
+ return element.dataset.id != null;
+ },
onStart(event) {
const id = event.target.dataset.id;
if (id == null) {
- event.cancel();
return;
}
setDragItemId(id);
diff --git a/packages/design-system/src/components/primitives/dnd/sortable-list.stories.tsx b/packages/design-system/src/components/primitives/dnd/sortable-list.stories.tsx
index 37edace62c..fb3630aec0 100644
--- a/packages/design-system/src/components/primitives/dnd/sortable-list.stories.tsx
+++ b/packages/design-system/src/components/primitives/dnd/sortable-list.stories.tsx
@@ -83,11 +83,10 @@ export const SortableList = ({
const autoScrollHandlers = useAutoScroll();
const useDragHandlers = useDrag({
+ isDragItem(element) {
+ return element instanceof HTMLLIElement;
+ },
onStart(event) {
- if (!(event.target instanceof HTMLLIElement)) {
- event.cancel();
- return;
- }
setDragItemId(event.target.dataset.id);
autoScrollHandlers.setEnabled(true);
},
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 e097a7af6e..b3fbcec434 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drag.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drag.ts
@@ -3,7 +3,7 @@ import { useRef, useEffect, useMemo } from "react";
type State =
| {
- status: "idle" | "canceled";
+ status: "idle";
}
| {
status: "pending" | "dragging";
@@ -21,12 +21,8 @@ const initialState = {
export type UseDragProps = {
startDistanceThreashold?: number;
shiftDistanceThreshold?: number;
- onStart: (event: {
- target: HTMLElement;
- x: number;
- y: number;
- cancel: () => void;
- }) => void;
+ isDragItem: (element: HTMLElement) => boolean;
+ onStart: (event: { target: HTMLElement }) => void;
onMove: (event: { x: number; y: number }) => void;
onShiftChange?: (event: { shifts: number }) => void;
onEnd: () => void;
@@ -44,14 +40,11 @@ export const useDrag = ({
// eslint-disable-next-line @typescript-eslint/no-empty-function
onShiftChange = () => {},
onEnd,
+ isDragItem,
}: UseDragProps): UseDragHandlers => {
const state = useRef<State>(initialState);
const rootRef = useRef<HTMLElement | null>(null);
- const cancel = () => {
- state.current = { status: "canceled" };
- };
-
const detectShift = () => {
if (state.current.status !== "dragging") {
return;
@@ -69,6 +62,9 @@ export const useDrag = ({
};
const { onPointerDown } = useMove({
+ shouldStart: (e) => {
+ return e.target instanceof HTMLElement && isDragItem(e.target);
+ },
onMoveStart({
clientX: x,
clientY: y,
@@ -86,13 +82,9 @@ export const useDrag = ({
initialY: y,
shifts: 0,
};
- onStart({ target, x, y, cancel });
+ onStart({ target });
},
onMove({ clientX: x, clientY: y }: { clientX: number; clientY: number }) {
- if (state.current.status === "canceled") {
- return;
- }
-
// We want to start dragging only when the user has moved more than startDistanceThreashold.
if (
state.current.status === "pending" &&
diff --git a/packages/design-system/src/components/primitives/dnd/use-move.ts b/packages/design-system/src/components/primitives/dnd/use-move.ts
index ace0784d79..393d73d562 100644
--- a/packages/design-system/src/components/primitives/dnd/use-move.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-move.ts
@@ -70,6 +70,8 @@ interface MoveEndEvent extends BaseMoveEvent {
}
interface MoveEvents {
+ shouldStart: (e: PointerEvent) => boolean;
+
/** Handler that is called when a move interaction starts. */
onMoveStart?: (e: MoveStartEvent) => void;
/** Handler that is called when the element is moved. */
@@ -190,7 +192,11 @@ export function useMove(props: MoveEvents): MoveResult {
return {
onPointerDown: (e: PointerEvent) => {
- if (e.button === 0 && state.current.id == null) {
+ if (
+ e.button === 0 &&
+ state.current.id == null &&
+ latestProps.current.shouldStart(e)
+ ) {
start();
e.stopPropagation();
e.preventDefault();