diff --git a/src/components/App.tsx b/src/components/App.tsx index 43901a3b..a52781d8 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -593,6 +593,8 @@ class App extends React.Component { scene.elements, { ...scene.appState, + width: this.state.width, + height: this.state.height, offsetTop: this.state.offsetTop, offsetLeft: this.state.offsetLeft, }, diff --git a/src/packages/excalidraw/CHANGELOG.MD b/src/packages/excalidraw/CHANGELOG.MD index 92633cce..ddc6644f 100644 --- a/src/packages/excalidraw/CHANGELOG.MD +++ b/src/packages/excalidraw/CHANGELOG.MD @@ -26,6 +26,7 @@ Please add the latest change on the top under the correct section. ### Fixes +- Fix scroll-to-center on init for non-zero canvas offsets [#2445](https://github.com/excalidraw/excalidraw/pull/2445) - Hide collab button when onCollabButtonClick not supplied [#2598](https://github.com/excalidraw/excalidraw/pull/2598) - Fix resizing the pasted charts [#2586](https://github.com/excalidraw/excalidraw/pull/2586) - Fix element visibility and zoom on cursor when canvas offset isn't 0. [#2534](https://github.com/excalidraw/excalidraw/pull/2534) diff --git a/src/tests/scroll.test.tsx b/src/tests/scroll.test.tsx new file mode 100644 index 00000000..6ee4a2d3 --- /dev/null +++ b/src/tests/scroll.test.tsx @@ -0,0 +1,48 @@ +import React from "react"; +import { render, waitFor } from "./test-utils"; +import Excalidraw from "../packages/excalidraw/index"; +import { API } from "./helpers/api"; + +const { h } = window; + +describe("appState", () => { + it("scroll-to-center on init works with non-zero offsets", async () => { + const WIDTH = 600; + const HEIGHT = 700; + const OFFSET_LEFT = 200; + const OFFSET_TOP = 100; + + const ELEM_WIDTH = 100; + const ELEM_HEIGHT = 60; + + await render( + , + ); + + await waitFor(() => { + expect(h.state.width).toBe(WIDTH); + expect(h.state.height).toBe(HEIGHT); + expect(h.state.offsetLeft).toBe(OFFSET_LEFT); + expect(h.state.offsetTop).toBe(OFFSET_TOP); + + // assert scroll is in center + expect(h.state.scrollX).toBe(WIDTH / 2 - ELEM_WIDTH / 2); + expect(h.state.scrollY).toBe(HEIGHT / 2 - ELEM_HEIGHT / 2); + }); + }); +});