mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
scroll the closest element to center (#1670)
Co-authored-by: Sanghyeon Lee <yongdamsh@gmail.com>
This commit is contained in:
parent
0db7ac78c4
commit
fa359034c5
10 changed files with 100 additions and 15 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue