Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

feat: support updating library in updateScene API (#4546)

Aakansha Doshib475412199e9
diff --git a/src/components/App.tsx b/src/components/App.tsx
index 4766019c67..c5ec90a86f 100644
--- a/src/components/App.tsx
+++ b/src/components/App.tsx
@@ -1582,6 +1582,7 @@ class App extends React.Component<AppProps, AppState> {
appState?: Pick<AppState, K> | null;
collaborators?: SceneData["collaborators"];
commitToHistory?: SceneData["commitToHistory"];
+ libraryItems?: SceneData["libraryItems"];
}) => {
if (sceneData.commitToHistory) {
this.history.resumeRecording();
@@ -1598,6 +1599,12 @@ class App extends React.Component<AppProps, AppState> {
if (sceneData.collaborators) {
this.setState({ collaborators: sceneData.collaborators });
}
+
+ if (sceneData.libraryItems) {
+ this.library.saveLibrary(
+ restoreLibraryItems(sceneData.libraryItems, "unpublished"),
+ );
+ }
},
);
diff --git a/src/constants.ts b/src/constants.ts
index 38fd622285..26baf34b41 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -106,10 +106,6 @@ export const EXPORT_DATA_TYPES = {
export const EXPORT_SOURCE = window.location.origin;
-export const STORAGE_KEYS = {
- LOCAL_STORAGE_LIBRARY: "excalidraw-library",
-} as const;
-
// time in milliseconds
export const IMAGE_RENDER_TIMEOUT = 500;
export const TAP_TWICE_TIMEOUT = 300;
diff --git a/src/excalidraw-app/data/localStorage.ts b/src/excalidraw-app/data/localStorage.ts
index 6d564bb71e..095c61d487 100644
--- a/src/excalidraw-app/data/localStorage.ts
+++ b/src/excalidraw-app/data/localStorage.ts
@@ -5,13 +5,13 @@ import {
getDefaultAppState,
} from "../../appState";
import { clearElementsForLocalStorage } from "../../element";
-import { STORAGE_KEYS as APP_STORAGE_KEYS } from "../../constants";
export const STORAGE_KEYS = {
LOCAL_STORAGE_ELEMENTS: "excalidraw",
LOCAL_STORAGE_APP_STATE: "excalidraw-state",
LOCAL_STORAGE_COLLAB: "excalidraw-collab",
LOCAL_STORAGE_KEY_COLLAB_FORCE_FLAG: "collabLinkForceLoadFlag",
+ LOCAL_STORAGE_LIBRARY: "excalidraw-library",
};
export const saveUsernameToLocalStorage = (username: string) => {
@@ -113,9 +113,7 @@ export const getTotalStorageSize = () => {
try {
const appState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
const collab = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
- const library = localStorage.getItem(
- APP_STORAGE_KEYS.LOCAL_STORAGE_LIBRARY,
- );
+ const library = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_LIBRARY);
const appStateSize = appState?.length || 0;
const collabSize = collab?.length || 0;
diff --git a/src/excalidraw-app/index.tsx b/src/excalidraw-app/index.tsx
index 915e386ded..46c14c4c43 100644
--- a/src/excalidraw-app/index.tsx
+++ b/src/excalidraw-app/index.tsx
@@ -7,7 +7,6 @@ import { TopErrorBoundary } from "../components/TopErrorBoundary";
import {
APP_NAME,
EVENT,
- STORAGE_KEYS,
TITLE_TIMEOUT,
URL_HASH_KEYS,
VERSION_TIMEOUT,
@@ -53,6 +52,7 @@ import { exportToBackend, getCollaborationLinkData, loadScene } from "./data";
import {
importFromLocalStorage,
saveToLocalStorage,
+ STORAGE_KEYS,
} from "./data/localStorage";
import CustomStats from "./CustomStats";
import { restoreAppState, RestoredDataState } from "../data/restore";
diff --git a/src/packages/excalidraw/CHANGELOG.md b/src/packages/excalidraw/CHANGELOG.md
index eeaa46d5d5..c1adeb78e7 100644
--- a/src/packages/excalidraw/CHANGELOG.md
+++ b/src/packages/excalidraw/CHANGELOG.md
@@ -19,6 +19,8 @@ Please add the latest change on the top under the correct section.
### Features
+- Support updating library using [`updateScene`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#updateScene) API [#4546](https://github.com/excalidraw/excalidraw/pull/4546).
+
- Introduced primary colors to the app. The colors can be overriden. Check [readme](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#customizing-styles) on how to do so.
- #### BREAKING CHANGE
diff --git a/src/packages/excalidraw/README_NEXT.md b/src/packages/excalidraw/README_NEXT.md
index d444537c56..a5f0cb9e44 100644
--- a/src/packages/excalidraw/README_NEXT.md
+++ b/src/packages/excalidraw/README_NEXT.md
@@ -509,6 +509,7 @@ You can use this function to update the scene with the sceneData. It accepts the
| `appState` | [`ImportedDataState["appState"]`](https://github.com/excalidraw/excalidraw/blob/master/src/data/types.ts#L18) | The `appState` to be updated in the scene. |
| `collaborators` | <pre>Map<string, <a href="https://github.com/excalidraw/excalidraw/blob/master/src/types.ts#L29">Collaborator></a></pre> | The list of collaborators to be updated in the scene. |
| `commitToHistory` | `boolean` | Implies if the `history (undo/redo)` should be recorded. Defaults to `false`. |
+| `libraryItems` | [LibraryItems](https://github.com/excalidraw/excalidraw/blob/master/src/types.ts#L258) | The `libraryItems` to be update in the scene. |
### `addFiles`
diff --git a/src/packages/excalidraw/example/App.js b/src/packages/excalidraw/example/App.js
index 08f7ab9272..ecf6ab0ef9 100644
--- a/src/packages/excalidraw/example/App.js
+++ b/src/packages/excalidraw/example/App.js
@@ -103,6 +103,24 @@ export default function App() {
>
Reset Scene
</button>
+ <button
+ onClick={() => {
+ excalidrawRef.current.updateScene({
+ libraryItems: [
+ {
+ status: "published",
+ elements: initialData.libraryItems[0],
+ },
+ {
+ status: "unpublished",
+ elements: initialData.libraryItems[1],
+ },
+ ],
+ });
+ }}
+ >
+ Update Library
+ </button>
<label>
<input
type="checkbox"
diff --git a/src/types.ts b/src/types.ts
index 712728a4f2..cd0e2ee7b4 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -255,6 +255,7 @@ export type SceneData = {
appState?: ImportedDataState["appState"];
collaborators?: Map<string, Collaborator>;
commitToHistory?: boolean;
+ libraryItems?: LibraryItems | LibraryItems_v1;
};
export enum UserIdleState {