diff --git a/src/components/App.tsx b/src/components/App.tsx index e035045d..33901a7b 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -287,7 +287,7 @@ export type ExcalidrawImperativeAPI = { history: { clear: InstanceType["resetHistory"]; }; - setScrollToContent: InstanceType["setScrollToContent"]; + scrollToContent: InstanceType["scrollToContent"]; getSceneElements: InstanceType["getSceneElements"]; getAppState: () => InstanceType["state"]; refresh: InstanceType["refresh"]; @@ -361,7 +361,7 @@ class App extends React.Component { history: { clear: this.resetHistory, }, - setScrollToContent: this.setScrollToContent, + scrollToContent: this.scrollToContent, getSceneElements: this.getSceneElements, getAppState: () => this.state, refresh: this.refresh, @@ -1421,9 +1421,17 @@ class App extends React.Component { this.actionManager.executeAction(actionToggleStats); }; - setScrollToContent = (remoteElements: readonly ExcalidrawElement[]) => { + scrollToContent = ( + target: + | ExcalidrawElement + | readonly ExcalidrawElement[] = this.scene.getElements(), + ) => { this.setState({ - ...calculateScrollCenter(remoteElements, this.state, this.canvas), + ...calculateScrollCenter( + Array.isArray(target) ? target : [target], + this.state, + this.canvas, + ), }); }; diff --git a/src/packages/excalidraw/CHANGELOG.md b/src/packages/excalidraw/CHANGELOG.md index 9d0faca1..d3bce033 100644 --- a/src/packages/excalidraw/CHANGELOG.md +++ b/src/packages/excalidraw/CHANGELOG.md @@ -17,6 +17,12 @@ Please add the latest change on the top under the correct section. ### Features +- Support scrolling to center to a single Excalidraw element, or not supplying any argument at all which will default to current elements on the scene [#3482](https://github.com/excalidraw/excalidraw/pull/3482). + + #### BREAKING CHANGE + + - Renamed `setScrollToContent` to [`scrollToContent`](https://github.com/excalidraw/excalidraw/blob/master/src/components/App.tsx#L265).. + - Make library local to given excalidraw instance (previously, all instances on the same page shared one global library) [#3451](https://github.com/excalidraw/excalidraw/pull/3451). - Added prop [onLibraryChange](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#onLibraryChange) which if supplied will be called when library is updated. diff --git a/src/packages/excalidraw/README_NEXT.md b/src/packages/excalidraw/README_NEXT.md index 68317af4..22d64034 100644 --- a/src/packages/excalidraw/README_NEXT.md +++ b/src/packages/excalidraw/README_NEXT.md @@ -443,7 +443,7 @@ You can pass a `ref` when you want to access some excalidraw APIs. We expose the | getSceneElements |
 () => ExcalidrawElement[]
| Returns all the elements excluding the deleted in the scene | | 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 | +| scrollToContent |
 (target?: ExcalidrawElement | ExcalidrawElement[]) => void 
| Scroll the nearest element out of the elements supplied to the center. Defaults to the elements on the scene. | | refresh | `() => 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](#importlibrary) | `(url: string, token?: string) => void` | Imports library from given URL | | setToastMessage | `(message: string) => void` | This API can be used to show the toast with custom message. |