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
implement onShift
Oleg Isonen22a36432abca
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsxindex 8b0646f2ea..cdd0890dff 100644--- a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx@@ -48,6 +48,12 @@ export const Playground = () => {setPlacementIndicatorRect(undefined);handleHoldEnd();},+ onShift({ direction, target }: any) {+ target.textContent = `shifted ${direction}`;+ setTimeout(() => {+ target.textContent = "";+ }, 1000);+ },});return (diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.tsindex cca4170964..3d9cd80583 100644--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts@@ -7,13 +7,19 @@ type State =}| {status: "pending";- pageX: number;- pageY: number;+ x: number;+ y: number;+ initialX: number;+ initialY: number;+ hasShifted: boolean;}| {status: "dragging";- pageX: number;- pageY: number;+ x: number;+ y: number;+ initialX: number;+ initialY: number;+ hasShifted: boolean;}| {status: "canceled";@@ -25,8 +31,11 @@ const initialState = {export const useDrag = ({startDistanceThreashold = 3,+ shiftDistanceThreshold = 20,+ verticalDistanceTolerance = 5,onStart,onMove,+ onShift,onEnd,}: any = {}) => {const state = useRef<State>(initialState);@@ -35,10 +44,26 @@ export const useDrag = ({state.current = { status: "canceled" };};+ const detectShift = (target: HTMLElement) => {+ if (state.current.status !== "dragging" || state.current.hasShifted) {+ return;+ }+ const deltaX = state.current.x - state.current.initialX;+ const hasShifted = Math.abs(deltaX) > shiftDistanceThreshold;+ const hasVerticallyMoved =+ Math.abs(state.current.y - state.current.initialY) >+ verticalDistanceTolerance;+ if (hasShifted && hasVerticallyMoved === false) {+ const direction = deltaX > 0 ? "right" : "left";+ state.current.hasShifted = true;+ onShift({ direction, cancel, target });+ }+ };+const props = useMove({onMoveStart({- pageX,- pageY,+ pageX: x,+ pageY: y,target,}: {pageX: number;@@ -47,12 +72,23 @@ export const useDrag = ({}) {state.current = {status: "pending",- pageX,- pageY,+ x,+ y,+ initialX: x,+ initialY: y,+ hasShifted: false,};onStart({ target, cancel });},- onMove({ pageX, pageY }: { pageX: number; pageY: number }) {+ onMove({+ pageX: x,+ pageY: y,+ target,+ }: {+ pageX: number;+ pageY: number;+ target: HTMLElement;+ }) {if (state.current.status === "canceled") {return;}@@ -60,19 +96,29 @@ export const useDrag = ({// We want to start dragging only when the user has moved more than startDistanceThreashold.if (state.current.status === "pending" &&- Math.abs(pageX - state.current.pageX) < startDistanceThreashold &&- Math.abs(pageY - state.current.pageY) < startDistanceThreashold+ Math.abs(x - state.current.x) < startDistanceThreashold &&+ Math.abs(y - state.current.y) < startDistanceThreashold) {return;}- state.current = {- status: "dragging",- pageX,- pageY,- };+ // We shouldn't be in non-pending state when we get here, unles user has ended the drag and yet somehow we ended up with onMove() call.+ if (+ state.current.status === "pending" ||+ state.current.status === "dragging"+ ) {+ state.current = {+ status: "dragging",+ x,+ y,+ initialX: state.current.initialX,+ initialY: state.current.initialY,+ hasShifted: state.current.hasShifted,+ };+ }- onMove({ x: pageX, y: pageY });+ onMove({ x, y });+ detectShift(target);},onMoveEnd() {state.current = initialState;diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.tsindex 9f46118dc1..7c6d9996b1 100644--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts@@ -17,7 +17,7 @@ export const useDropTarget = ({const areaRef = useRef<Area>("center");const holdTimeoutRef = useRef<NodeJS.Timeout>();- const checkHold = (target: HTMLElement) => {+ const detectHold = (target: HTMLElement) => {clearTimeout(holdTimeoutRef.current);holdTimeoutRef.current = setTimeout(() => {if (target === targetRef.current) {@@ -44,7 +44,7 @@ export const useDropTarget = ({if (hasTargetChanged) {targetRectRef.current = nextTarget.getBoundingClientRect();- checkHold(nextTarget);+ detectHold(nextTarget);}const nextArea = getArea(