From 4208c97b6276b0f7910fade46f67bb0746c0eb95 Mon Sep 17 00:00:00 2001 From: Ryan Di Date: Fri, 2 May 2025 17:52:14 +1000 Subject: [PATCH] configurable allowance --- .../excalidraw/scene/scrollConstraints.ts | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/excalidraw/scene/scrollConstraints.ts b/packages/excalidraw/scene/scrollConstraints.ts index 9fa91cada..04dc9bdb2 100644 --- a/packages/excalidraw/scene/scrollConstraints.ts +++ b/packages/excalidraw/scene/scrollConstraints.ts @@ -11,6 +11,7 @@ import { getNormalizedZoom } from "./normalize"; * * @param scrollConstraints - The constraints of the scrollable area including width, height, and position. * @param appState - An object containing the current horizontal and vertical scroll positions. + * @param overscrollAllowance - Optional parameter to specify the overscroll allowance percentage. * @returns An object containing the calculated optimal horizontal and vertical scroll positions and zoom level. * * @example @@ -20,6 +21,7 @@ import { getNormalizedZoom } from "./normalize"; export const calculateConstrainedScrollCenter = ( state: AppState, { scrollX, scrollY }: Pick, + overscrollAllowance?: number, ): { scrollX: AppState["scrollX"]; scrollY: AppState["scrollY"]; @@ -57,6 +59,7 @@ export const calculateConstrainedScrollCenter = ( height, zoom: _zoom, allowOverscroll: false, + overscrollAllowance, }); return { @@ -167,15 +170,22 @@ const calculateConstraints = ({ height, zoom, allowOverscroll, + overscrollAllowance, }: { scrollConstraints: ScrollConstraints; width: AppState["width"]; height: AppState["height"]; zoom: AppState["zoom"]; allowOverscroll: boolean; + overscrollAllowance?: number; }) => { - // Set the overscroll allowance percentage - const OVERSCROLL_ALLOWANCE_PERCENTAGE = 0.2; + // Validate the overscroll allowance percentage + const validatedOverscroll = + overscrollAllowance != null && + overscrollAllowance >= 0 && + overscrollAllowance <= 1 + ? overscrollAllowance + : 0.2; /** * Calculates the center position of the constrained scroll area. @@ -194,10 +204,8 @@ const calculateConstraints = ({ * @returns The overscroll allowance values for the X and Y axes. */ const calculateOverscrollAllowance = () => { - const overscrollAllowanceX = - OVERSCROLL_ALLOWANCE_PERCENTAGE * scrollConstraints.width; - const overscrollAllowanceY = - OVERSCROLL_ALLOWANCE_PERCENTAGE * scrollConstraints.height; + const overscrollAllowanceX = validatedOverscroll * scrollConstraints.width; + const overscrollAllowanceY = validatedOverscroll * scrollConstraints.height; return Math.min(overscrollAllowanceX, overscrollAllowanceY); }; @@ -284,14 +292,14 @@ const calculateConstraints = ({ ); const { constrainedScrollCenterX, constrainedScrollCenterY } = calculateConstrainedScrollCenter(constrainedZoom); - const overscrollAllowance = calculateOverscrollAllowance(); + const overscrollAllowanceValue = calculateOverscrollAllowance(); const shouldAdjustForCenteredViewX = constrainedZoom <= zoomLevelX; const shouldAdjustForCenteredViewY = constrainedZoom <= zoomLevelY; const { maxScrollX, minScrollX, maxScrollY, minScrollY } = calculateMinMaxScrollValues( shouldAdjustForCenteredViewX, shouldAdjustForCenteredViewY, - overscrollAllowance, + overscrollAllowanceValue, constrainedScrollCenterX, constrainedScrollCenterY, constrainedZoom,