From fa359034c51547c54929000a09db5596817db5f9 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Sat, 30 May 2020 17:32:32 +0530 Subject: [PATCH] scroll the closest element to center (#1670) Co-authored-by: Sanghyeon Lee --- src/actions/actionCanvas.tsx | 17 ++++++++---- src/components/App.tsx | 2 ++ src/components/LayerUI.tsx | 5 +++- src/components/MobileMenu.tsx | 6 ++++- src/data/index.ts | 2 +- src/data/localStorage.ts | 1 - src/data/restore.ts | 5 +++- src/element/bounds.ts | 26 +++++++++++++++++- src/element/index.ts | 1 + src/scene/scroll.ts | 50 ++++++++++++++++++++++++++++++++--- 10 files changed, 100 insertions(+), 15 deletions(-) diff --git a/src/actions/actionCanvas.tsx b/src/actions/actionCanvas.tsx index 3f6304047..d66bdd379 100644 --- a/src/actions/actionCanvas.tsx +++ b/src/actions/actionCanvas.tsx @@ -4,7 +4,7 @@ import { getDefaultAppState } from "../appState"; import { trash, zoomIn, zoomOut, resetZoom } from "../components/icons"; import { ToolButton } from "../components/ToolButton"; import { t } from "../i18n"; -import { getNormalizedZoom, calculateScrollCenter } from "../scene"; +import { getNormalizedZoom, normalizeScroll } from "../scene"; import { KEYS } from "../keys"; import { getShortcutKey } from "../utils"; import useIsMobile from "../is-mobile"; @@ -202,15 +202,22 @@ export const actionZoomToFit = register({ name: "zoomToFit", perform: (elements, appState) => { const nonDeletedElements = elements.filter((element) => !element.isDeleted); - const scrollCenter = calculateScrollCenter(nonDeletedElements); const commonBounds = getCommonBounds(nonDeletedElements); - const zoom = calculateZoom(commonBounds, appState.zoom, scrollCenter); + const [x1, y1, x2, y2] = commonBounds; + const centerX = (x1 + x2) / 2; + const centerY = (y1 + y2) / 2; + const scrollX = normalizeScroll(window.innerWidth / 2 - centerX); + const scrollY = normalizeScroll(window.innerHeight / 2 - centerY); + const zoom = calculateZoom(commonBounds, appState.zoom, { + scrollX, + scrollY, + }); return { appState: { ...appState, - scrollX: scrollCenter.scrollX, - scrollY: scrollCenter.scrollY, + scrollX, + scrollY, zoom, }, commitToHistory: false, diff --git a/src/components/App.tsx b/src/components/App.tsx index 56c69a389..3dab6d6d1 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -847,6 +847,8 @@ class App extends React.Component { remoteElements.filter((element: { isDeleted: boolean }) => { return !element.isDeleted; }), + this.state, + this.canvas, ), }); } diff --git a/src/components/LayerUI.tsx b/src/components/LayerUI.tsx index b63edddf0..75d8304e1 100644 --- a/src/components/LayerUI.tsx +++ b/src/components/LayerUI.tsx @@ -256,7 +256,9 @@ const LayerUI = ({