Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix: Fix centering element on init (#2445)

Andrés Riverafb582b45db47
diff --git a/src/components/App.tsx b/src/components/App.tsx
index 43901a3baf..a52781d8cc 100644
--- a/src/components/App.tsx
+++ b/src/components/App.tsx
@@ -593,6 +593,8 @@ class App extends React.Component<ExcalidrawProps, AppState> {
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 92633cce36..ddc6644fa5 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 0000000000..6ee4a2d348
--- /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(
+ <Excalidraw
+ width={WIDTH}
+ height={HEIGHT}
+ offsetLeft={OFFSET_LEFT}
+ offsetTop={OFFSET_TOP}
+ initialData={{
+ elements: [
+ API.createElement({
+ type: "rectangle",
+ id: "A",
+ width: ELEM_WIDTH,
+ height: ELEM_HEIGHT,
+ }),
+ ],
+ }}
+ />,
+ );
+
+ 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);
+ });
+ });
+});