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.

cancel, start threashold

Oleg Isonen86ef0cdf1ff3
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.tsx
new file mode 100644
index 0000000000..081ed511cb
--- /dev/null
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
@@ -0,0 +1,36 @@
+import { getEnvelopeEndpointWithUrlEncodedAuth } from "@sentry/core";
+import { ComponentStory, ComponentMeta } from "@storybook/react";
+import { Box } from "../../box";
+import { useDrag } from "./handlers";
+
+export const Playground = () => {
+ const dragProps = useDrag({
+ onStart(event: any) {
+ if (event.element.dataset.draggable === "false") {
+ event.cancel();
+ }
+ //console.log(event.element, event.element.dataset.draggable);
+ },
+ });
+
+ return (
+ <div {...dragProps}>
+ <Item background="$cyanA9" />
+ <Item background="$slateA9" />
+ <Item background="$blueA9" draggable={false}>
+ Not Draggable
+ </Item>
+ </div>
+ );
+};
+
+export default {} as ComponentMeta<typeof Playground>;
+
+const Item = ({ background, draggable, children }: any) => (
+ <Box
+ css={{ width: 100, height: 100, background, margin: 10 }}
+ data-draggable={draggable}
+ >
+ {children}
+ </Box>
+);
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts
new file mode 100644
index 0000000000..bb524a35db
--- /dev/null
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts
@@ -0,0 +1,69 @@
+import { useEffect, useRef } from "react";
+import { useMove } from "./use-move";
+
+type State =
+ | {
+ status: "idle";
+ }
+ | {
+ status: "pending";
+ pageX: number;
+ pageY: number;
+ }
+ | {
+ status: "dragging";
+ pageX: number;
+ pageY: number;
+ }
+ | {
+ status: "canceled";
+ };
+
+export const useDrag = ({ onStart, startDistanceThreashold = 3 }: any = {}) => {
+ const state = useRef<State>({
+ status: "idle",
+ });
+
+ const cancel = () => {
+ state.current = { status: "canceled" };
+ };
+
+ const props = useMove({
+ onMoveStart(event: any) {
+ state.current = {
+ status: "pending",
+ pageX: event.pageX,
+ pageY: event.pageY,
+ };
+
+ onStart({ ...event, cancel });
+ console.log(event);
+ },
+ onMove(event: any) {
+ if (state.current.status === "canceled") {
+ return;
+ }
+
+ if (
+ state.current.status === "pending" &&
+ Math.abs(event.pageX - state.current.pageX) < startDistanceThreashold &&
+ Math.abs(event.pageY - state.current.pageY) < startDistanceThreashold
+ ) {
+ return;
+ }
+
+ state.current = {
+ status: "dragging",
+ pageX: event.pageX,
+ pageY: event.pageY,
+ };
+
+ console.log("dragging", event);
+ },
+ onMoveEnd() {
+ state.current.status = "idle";
+ },
+ });
+
+ return props.moveProps;
+};
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-move.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-move.ts
new file mode 100644
index 0000000000..ac2da7433d
--- /dev/null
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-move.ts
@@ -0,0 +1,272 @@
+/*
+ * Copyright 2020 Adobe. All rights reserved.
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License. You may obtain a copy
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
+ * OF ANY KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+
+import type { MoveEvents, PointerType } from "@react-types/shared";
+import React, { HTMLAttributes, useMemo, useRef } from "react";
+import { useGlobalListeners } from "@react-aria/utils";
+
+interface MoveResult {
+ /** Props to spread on the target element. */
+ moveProps: HTMLAttributes<HTMLElement>;
+}
+
+interface EventBase {
+ shiftKey: boolean;
+ ctrlKey: boolean;
+ metaKey: boolean;
+ altKey: boolean;
+}
+
+/**
+ * Handles move interactions across mouse, touch, and keyboard, including dragging with
+ * the mouse or touch, and using the arrow keys. Normalizes behavior across browsers and
+ * platforms, and ignores emulated mouse events on touch devices.
+ */
+export function useMove(props: /*MoveEvents*/ any): MoveResult {
+ let { onMoveStart, onMove, onMoveEnd } = props;
+
+ let state = useRef<{
+ didMove: boolean;
+ lastPosition: { pageX: number; pageY: number } | null;
+ id: number | null;
+ }>({ didMove: false, lastPosition: null, id: null });
+
+ let { addGlobalListener, removeGlobalListener } = useGlobalListeners();
+
+ let moveProps = useMemo(() => {
+ let moveProps: HTMLAttributes<HTMLElement> = {};
+
+ let start = () => {
+ //disableTextSelection();
+ state.current.didMove = false;
+ };
+ let move = (
+ originalEvent: any /*EventBase*/,
+ pointerType: PointerType,
+ deltaX: number,
+ deltaY: number
+ ) => {
+ if (deltaX === 0 && deltaY === 0) {
+ return;
+ }
+
+ if (!state.current.didMove) {
+ state.current.didMove = true;
+ onMoveStart?.({
+ type: "movestart",
+ pointerType,
+ shiftKey: originalEvent.shiftKey,
+ metaKey: originalEvent.metaKey,
+ ctrlKey: originalEvent.ctrlKey,
+ altKey: originalEvent.altKey,
+ element: originalEvent.target,
+ pageX: originalEvent.pageX,
+ pageY: originalEvent.pageY,
+ });
+ }
+ onMove?.({
+ type: "move",
+ pointerType,
+ deltaX: deltaX,
+ deltaY: deltaY,
+ shiftKey: originalEvent.shiftKey,
+ metaKey: originalEvent.metaKey,
+ ctrlKey: originalEvent.ctrlKey,
+ altKey: originalEvent.altKey,
+ element: originalEvent.target,
+ pageX: originalEvent.pageX,
+ pageY: originalEvent.pageY,
+ });
+ };
+ let end = (originalEvent: EventBase, pointerType: PointerType) => {
+ //restoreTextSelection();
+ if (state.current.didMove) {
+ onMoveEnd?.({
+ type: "moveend",
+ pointerType,
+ shiftKey: originalEvent.shiftKey,
+ metaKey: originalEvent.metaKey,
+ ctrlKey: originalEvent.ctrlKey,
+ altKey: originalEvent.altKey,
+ });
+ }
+ };
+
+ if (typeof PointerEvent === "undefined") {
+ let onMouseMove = (e: MouseEvent) => {
+ if (e.button === 0 && state.current.lastPosition !== null) {
+ move(
+ e,
+ "mouse",
+ e.pageX - state.current.lastPosition.pageX,
+ e.pageY - state.current.lastPosition.pageY
+ );
+ state.current.lastPosition = { pageX: e.pageX, pageY: e.pageY };
+ }
+ };
+ let onMouseUp = (e: MouseEvent) => {
+ if (e.button === 0) {
+ end(e, "mouse");
+ removeGlobalListener(window, "mousemove", onMouseMove, false);
+ removeGlobalListener(window, "mouseup", onMouseUp, false);
+ }
+ };
+ moveProps.onMouseDown = (e: React.MouseEvent) => {
+ if (e.button === 0) {
+ start();
+ e.stopPropagation();
+ e.preventDefault();
+ state.current.lastPosition = { pageX: e.pageX, pageY: e.pageY };
+ addGlobalListener(window, "mousemove", onMouseMove, false);
+ addGlobalListener(window, "mouseup", onMouseUp, false);
+ }
+ };
+
+ let onTouchMove = (e: TouchEvent) => {
+ let touch = [...e.changedTouches].findIndex(
+ ({ identifier }) => identifier === state.current.id
+ );
+ if (touch >= 0 && state.current.lastPosition !== null) {
+ let { pageX, pageY } = e.changedTouches[touch];
+ move(
+ e,
+ "touch",
+ pageX - state.current.lastPosition.pageX,
+ pageY - state.current.lastPosition.pageY
+ );
+ state.current.lastPosition = { pageX, pageY };
+ }
+ };
+ let onTouchEnd = (e: TouchEvent) => {
+ let touch = [...e.changedTouches].findIndex(
+ ({ identifier }) => identifier === state.current.id
+ );
+ if (touch >= 0) {
+ end(e, "touch");
+ state.current.id = null;
+ removeGlobalListener(window, "touchmove", onTouchMove);
+ removeGlobalListener(window, "touchend", onTouchEnd);
+ removeGlobalListener(window, "touchcancel", onTouchEnd);
+ }
+ };
+ moveProps.onTouchStart = (e: React.TouchEvent) => {
+ if (e.changedTouches.length === 0 || state.current.id != null) {
+ return;
+ }
+
+ let { pageX, pageY, identifier } = e.changedTouches[0];
+ start();
+ e.stopPropagation();
+ e.preventDefault();
+ state.current.lastPosition = { pageX, pageY };
+ state.current.id = identifier;
+ addGlobalListener(window, "touchmove", onTouchMove, false);
+ addGlobalListener(window, "touchend", onTouchEnd, false);
+ addGlobalListener(window, "touchcancel", onTouchEnd, false);
+ };
+ } else {
+ let onPointerMove = (e: PointerEvent) => {
+ if (
+ e.pointerId === state.current.id &&
+ state.current.lastPosition !== null
+ ) {
+ let pointerType = (e.pointerType || "mouse") as PointerType;
+
+ // Problems with PointerEvent#movementX/movementY:
+ // 1. it is always 0 on macOS Safari.
+ // 2. On Chrome Android, it's scaled by devicePixelRatio, but not on Chrome macOS
+ move(
+ e,
+ pointerType,
+ e.pageX - state.current.lastPosition.pageX,
+ e.pageY - state.current.lastPosition.pageY
+ );
+ state.current.lastPosition = { pageX: e.pageX, pageY: e.pageY };
+ }
+ };
+
+ let onPointerUp = (e: PointerEvent) => {
+ if (e.pointerId === state.current.id) {
+ let pointerType = (e.pointerType || "mouse") as PointerType;
+ end(e, pointerType);
+ state.current.id = null;
+ removeGlobalListener(window, "pointermove", onPointerMove, false);
+ removeGlobalListener(window, "pointerup", onPointerUp, false);
+ removeGlobalListener(window, "pointercancel", onPointerUp, false);
+ }
+ };
+
+ moveProps.onPointerDown = (e: React.PointerEvent) => {
+ if (e.button === 0 && state.current.id == null) {
+ start();
+ e.stopPropagation();
+ e.preventDefault();
+ state.current.lastPosition = { pageX: e.pageX, pageY: e.pageY };
+ state.current.id = e.pointerId;
+ addGlobalListener(window, "pointermove", onPointerMove, false);
+ addGlobalListener(window, "pointerup", onPointerUp, false);
+ addGlobalListener(window, "pointercancel", onPointerUp, false);
+ }
+ };
+ }
+
+ let triggerKeyboardMove = (
+ e: EventBase,
+ deltaX: number,
+ deltaY: number
+ ) => {
+ start();
+ move(e, "keyboard", deltaX, deltaY);
+ end(e, "keyboard");
+ };
+
+ moveProps.onKeyDown = (e) => {
+ switch (e.key) {
+ case "Left":
+ case "ArrowLeft":
+ e.preventDefault();
+ e.stopPropagation();
+ triggerKeyboardMove(e, -1, 0);
+ break;
+ case "Right":
+ case "ArrowRight":
+ e.preventDefault();
+ e.stopPropagation();
+ triggerKeyboardMove(e, 1, 0);
+ break;
+ case "Up":
+ case "ArrowUp":
+ e.preventDefault();
+ e.stopPropagation();
+ triggerKeyboardMove(e, 0, -1);
+ break;
+ case "Down":
+ case "ArrowDown":
+ e.preventDefault();
+ e.stopPropagation();
+ triggerKeyboardMove(e, 0, 1);
+ break;
+ }
+ };
+
+ return moveProps;
+ }, [
+ state,
+ onMoveStart,
+ onMove,
+ onMoveEnd,
+ addGlobalListener,
+ removeGlobalListener,
+ ]);
+
+ return { moveProps };
+}
diff --git a/apps/designer/package.json b/apps/designer/package.json
index d2d0240c6f..98dfd85189 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -48,6 +48,8 @@
"@radix-ui/react-toggle-group": "^0.1.5",
"@radix-ui/react-tooltip": "^0.1.7",
"@radix-ui/react-use-layout-effect": "^0.1.0",
+ "@react-aria/interactions": "^3.9.1",
+ "@react-aria/utils": "^3.13.1",
"@remix-run/node": "^1.6.4",
"@remix-run/react": "^1.6.4",
"@remix-run/serve": "^1.6.4",
@@ -92,6 +94,7 @@
},
"devDependencies": {
"@babel/core": "^7.18.5",
+ "@react-types/shared": "^3.13.1",
"@remix-run/dev": "^1.6.4",
"@storybook/addon-actions": "^6.5.9",
"@storybook/addon-essentials": "^6.5.9",
diff --git a/yarn.lock b/yarn.lock
index 62cd43092e..8927ddece2 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1975,7 +1975,7 @@
dependencies:
regenerator-runtime "^0.13.2"
-"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.5", "@babel/runtime@^7.13.10", "@babel/runtime@^7.17.8", "@babel/runtime@^7.3.1", "@babel/runtime@^7.5.0", "@babel/runtime@^7.7.6", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
+"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.11.2", "@babel/runtime@^7.12.5", "@babel/runtime@^7.13.10", "@babel/runtime@^7.17.8", "@babel/runtime@^7.3.1", "@babel/runtime@^7.5.0", "@babel/runtime@^7.6.2", "@babel/runtime@^7.7.6", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.18.6.tgz#6a1ef59f838debd670421f8c7f2cbb8da9751580"
integrity sha512-t9wi7/AW6XtKahAe20Yw0/mMljKq0B1r2fPdvaAdV/KPDZewFXdaaa6K7lxmZBZ8FBNpCiAT6iHPmd6QO9bKfQ==
@@ -3277,6 +3277,33 @@
dependencies:
"@babel/runtime" "^7.13.10"
+"@react-aria/interactions@^3.9.1":
+ version "3.9.1"
+ resolved "https://registry.yarnpkg.com/@react-aria/interactions/-/interactions-3.9.1.tgz#1860b905d9a0b17ed74dd7fe769370e017cb3015"
+ integrity sha512-qPTJpUwIiis2OwpVzDd3I8dBjBkelDnvAW+dJkX+8B840JP5a7E1zVoAuR7OOAXqKa95R7miwK+5la1aSeWoDg==
+ dependencies:
+ "@babel/runtime" "^7.6.2"
+ "@react-aria/utils" "^3.13.1"
+ "@react-types/shared" "^3.13.1"
+
+"@react-aria/ssr@^3.2.0":
+ version "3.2.0"
+ resolved "https://registry.yarnpkg.com/@react-aria/ssr/-/ssr-3.2.0.tgz#88460384b43204f91c972d5b0de24ee44d6a2984"
+ integrity sha512-wwJFdkl+Q8NU5yJ4NvdAOqx5LM3QtUVoSjuK7Ey8jZ4WS4bB0EqT3Kr3IInBs257HzZ5nXCiKXKE4NGXXuIRWA==
+ dependencies:
+ "@babel/runtime" "^7.6.2"
+
+"@react-aria/utils@^3.13.1":
+ version "3.13.1"
+ resolved "https://registry.yarnpkg.com/@react-aria/utils/-/utils-3.13.1.tgz#45557fdc7ae9de057a83014013bf09e54d074c96"
+ integrity sha512-usW6RoLKil4ylgDbRcaQ5YblNGv5ZihI4I9NB8pdazhw53cSRyLaygLdmHO33xgpPnAhb6Nb/tv8d5p6cAde+A==
+ dependencies:
+ "@babel/runtime" "^7.6.2"
+ "@react-aria/ssr" "^3.2.0"
+ "@react-stately/utils" "^3.5.0"
+ "@react-types/shared" "^3.13.1"
+ clsx "^1.1.1"
+
"@react-dnd/[email protected]", "@react-dnd/asap@^4.0.0":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@react-dnd/asap/-/asap-4.0.1.tgz#5291850a6b58ce6f2da25352a64f1b0674871aab"
@@ -3297,6 +3324,18 @@
resolved "https://registry.yarnpkg.com/@react-dnd/shallowequal/-/shallowequal-2.0.0.tgz#a3031eb54129f2c66b2753f8404266ec7bf67f0a"
integrity sha512-Pc/AFTdwZwEKJxFJvlxrSmGe/di+aAOBn60sremrpLo6VI/6cmiUYNNwlI5KNYttg7uypzA3ILPMPgxB2GYZEg==
+"@react-stately/utils@^3.5.0":
+ version "3.5.0"
+ resolved "https://registry.yarnpkg.com/@react-stately/utils/-/utils-3.5.0.tgz#e76231cfc93042814dcc5d96436e50c807c1d905"
+ integrity sha512-WzzwlQtJrf7egaN+lt02/f/wkFKbcscsbinmXs9pL7QyYm+BCQ9xXj01w0juzt93piZgB/kfIYJqInEdpudh1A==
+ dependencies:
+ "@babel/runtime" "^7.6.2"
+
+"@react-types/shared@^3.13.1":
+ version "3.13.1"
+ resolved "https://registry.yarnpkg.com/@react-types/shared/-/shared-3.13.1.tgz#eda5e3744971606f753baf7879136bf8e3f707ab"
+ integrity sha512-EHQqILDJeDvnloy5VV9lnnEjpCMwH1ghptCfa/lz9Ht9nwco3qGCvUABkWyND7yU1Adt3A/1oJxhpRUu3eTlyg==
+
"@remix-run/dev@^1.6.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@remix-run/dev/-/dev-1.6.4.tgz#7a2a7fc24e66434c5cf64d6e79a62d35d09b73de"
@@ -7002,7 +7041,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.1.0.tgz#62937c6adfea771247c34b54d320fb99624f5702"
integrity sha512-3avwM37fSK5oP6M5rQ9CNe99lwxhXDOeSWVPAOYF6OazUTgZCMb0yWlJpmdD74REy1gkEaFiub2ULv4fq9GUhA==
-clsx@^1.0.4:
+clsx@^1.0.4, clsx@^1.1.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/clsx/-/clsx-1.2.1.tgz#0ddc4a20a549b59c93a4116bb26f5294ca17dc12"
integrity sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg==