diff --git a/src/components/App.tsx b/src/components/App.tsx index 16b5c001..82c8e25c 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -298,6 +298,7 @@ class App extends React.Component { height: window.innerHeight, }; private scene: Scene; + private resizeObserver: ResizeObserver | undefined; constructor(props: ExcalidrawProps) { super(props); const defaultAppState = getDefaultAppState(); @@ -786,6 +787,15 @@ class App extends React.Component { this.scene.addCallback(this.onSceneUpdated); this.addEventListeners(); + if ( + "ResizeObserver" in window && + this.excalidrawContainerRef?.current?.parentElement + ) { + this.resizeObserver = new ResizeObserver(() => this.setCanvasOffsets()); + this.resizeObserver?.observe( + this.excalidrawContainerRef.current.parentElement, + ); + } const searchParams = new URLSearchParams(window.location.search.slice(1)); if (searchParams.has("web-share-target")) { @@ -799,6 +809,7 @@ class App extends React.Component { } public componentWillUnmount() { + this.resizeObserver?.disconnect(); this.unmounted = true; this.removeEventListeners(); this.scene.destroy(); diff --git a/src/packages/excalidraw/CHANGELOG.md b/src/packages/excalidraw/CHANGELOG.md index 61861e87..2cc6bacb 100644 --- a/src/packages/excalidraw/CHANGELOG.md +++ b/src/packages/excalidraw/CHANGELOG.md @@ -18,7 +18,8 @@ Please add the latest change on the top under the correct section. ### Features -- Export types for the package so now it can be used with typescript[#3337](https://github.com/excalidraw/excalidraw/pull/3337). The types are available at `@excalidraw/excalirdraw/types`. +- Calculate offsets when excalidraw container resizes using resize observer api [#3374](https://github.com/excalidraw/excalidraw/pull/3374). +- Export types for the package so now it can be used with typescript [#3337](https://github.com/excalidraw/excalidraw/pull/3337). The types are available at `@excalidraw/excalirdraw/types`. - Add `renderCustomStats` prop to render extra stats on host, and expose `setToastMessage` API via refs which can be used to show toast with custom message [#3360](https://github.com/excalidraw/excalidraw/pull/3360). - Support passing a CSRF token when importing libraries to prevent prompting before installation. The token is passed from [https://libraries.excalidraw.com](https://libraries.excalidraw.com/) using the `token` URL key [#3329](https://github.com/excalidraw/excalidraw/pull/3329). - #### BREAKING CHANGE diff --git a/src/packages/excalidraw/README_NEXT.md b/src/packages/excalidraw/README_NEXT.md index 075aa4f6..380636e6 100644 --- a/src/packages/excalidraw/README_NEXT.md +++ b/src/packages/excalidraw/README_NEXT.md @@ -493,7 +493,7 @@ You can pass a `ref` when you want to access some excalidraw APIs. We expose the | getAppState |
 () => AppState
| Returns current appState | | history | `{ clear: () => void }` | This is the history API. `history.clear()` will clear the history | | setScrollToContent |
 (ExcalidrawElement[]) => void 
| Scroll to the nearest element to center | -| setCanvasOffsets | `() => void` | Updates the offsets for the Excalidraw component so that the coordinates are computed correctly (for example the cursor position). You should call this API when your app changes the dimensions/position of the Excalidraw container, such as when toggling a sidebar. You don't have to call this when the position is changed on page scroll (we handled that ourselves). | +| setCanvasOffsets | `() => void` | Updates the offsets for the Excalidraw component so that the coordinates are computed correctly (for example the cursor position). You don't have to call this when the position is changed on page scroll or when the excalidraw container resizes (we handle that ourselves). For any other cases if the position of excalidraw is updated (example due to scroll on parent container and not page scroll) you should call this API. | | importLibrary | `(url: string, token?: string) => void` | Imports library from given URL. You should call this on `hashchange`, passing the `addLibrary` value if you detect it. Optionally pass a CSRF `token` to skip prompting during installation (retrievable via `token` key from the url coming from [https://libraries.excalidraw.com](https://libraries.excalidraw.com/)). | | setToastMessage | `(message: string) => void` | This API can be used to show the toast with custom message. |