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.

auto scroll works

Roman Pominovf99b93b2604c
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 7bf6360b59..193fbd1fb6 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -59,7 +59,7 @@ export const useDragAndDrop = () => {
});
};
- const autoScrollHandlers = useAutoScroll();
+ const autoScrollHandlers = useAutoScroll({ fullScreen: true });
const placementHandlers = usePlacement({
onPlacementChange: (placement) => {
@@ -183,24 +183,14 @@ export const useDragAndDrop = () => {
placementHandlers.handleScroll();
};
- const rootElement = document.body;
+ document.body.addEventListener("pointerdown", handlePointerDown);
+ dropTargetHandlers.rootRef(document.body);
+ window.addEventListener("scroll", handleScroll);
- rootElement.addEventListener("pointerdown", handlePointerDown);
- dropTargetHandlers.rootRef(rootElement);
-
- // @todo: we're probably subscribing on a wrong element here
- rootElement.addEventListener("scroll", handleScroll);
-
- // @todo: We need a special mode for when we want to scroll the whole page.
- // Because our rect for edge detection is a viewport,
- // but the element which we want to scroll has a rect corresponding to the document.
- // Maybe just have two separate refs?
- autoScrollHandlers.targetRef(document.body.parentElement);
() => {
- rootElement.removeEventListener("pointerdown", handlePointerDown);
- rootElement.removeEventListener("scroll", handleScroll);
+ document.body.removeEventListener("pointerdown", handlePointerDown);
dropTargetHandlers.rootRef(null);
- autoScrollHandlers.targetRef(null);
+ window.removeEventListener("scroll", handleScroll);
};
// @todo: need to make the dependencies more stable,
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-auto-scroll.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-auto-scroll.ts
index 7970dcfbc9..0629db791b 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-auto-scroll.ts
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-auto-scroll.ts
@@ -41,6 +41,10 @@ const getSpeed = (
export type UseAutoScrollProps = {
edgeDistanceThreshold?: number;
+ // If set to true entire document will be scrolled.
+ // No need to set targetRef in this case.
+ fullScreen?: boolean;
+
// min/max speed of the scroll animation in pixels per second
minSpeed?: number;
maxSpeed?: number;
@@ -56,6 +60,7 @@ export const useAutoScroll = ({
edgeDistanceThreshold = 100,
minSpeed = 1,
maxSpeed = 500,
+ fullScreen = false,
}: UseAutoScrollProps = {}): UseAutoScrollHandlers => {
const state = useRef({
target: null as HTMLElement | null,
@@ -66,6 +71,35 @@ export const useAutoScroll = ({
stepScheduled: false,
});
+ const getViewportRect = () => {
+ if (fullScreen) {
+ return {
+ top: 0,
+ left: 0,
+ bottom: window.innerHeight,
+ right: window.innerWidth,
+ };
+ }
+
+ if (state.current.target === null) {
+ return;
+ }
+
+ return state.current.target.getBoundingClientRect();
+ };
+
+ const scrollBy = (x: number, y: number) => {
+ if (fullScreen) {
+ window.scrollBy(x, y);
+ }
+
+ if (state.current.target === null) {
+ return;
+ }
+
+ state.current.target.scrollBy(x, y);
+ };
+
const step = (timestamp: number) => {
state.current.stepScheduled = false;
@@ -80,7 +114,7 @@ export const useAutoScroll = ({
const elapsed = timestamp - state.current.prevTimestamp;
// to avoid a big jump when auto-scroll becomes enabled
- if (elapsed > 3000) {
+ if (elapsed > FRAME_PERIOD * 100) {
state.current.prevTimestamp = timestamp;
scheduleStep();
return;
@@ -93,12 +127,7 @@ export const useAutoScroll = ({
state.current.prevTimestamp = timestamp;
- // for TypeScript
- if (state.current.target === null) {
- return;
- }
-
- state.current.target.scrollBy(
+ scrollBy(
(state.current.speedX / 1000) * elapsed,
(state.current.speedY / 1000) * elapsed
);
@@ -115,11 +144,14 @@ export const useAutoScroll = ({
return {
handleMove({ x, y }) {
- if (!state.current.enabled || state.current.target === null) {
+ if (!state.current.enabled) {
return;
}
- const rect = state.current.target.getBoundingClientRect();
+ const rect = getViewportRect();
+ if (rect === undefined) {
+ return;
+ }
state.current.speedY = getSpeed(
y,