From b475412199e927a3fa01f35048e9aece84884190 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Thu, 6 Jan 2022 21:37:33 +0530 Subject: [PATCH] feat: support updating library in updateScene API (#4546) * feat: support updating library in updateScene API * fix * update docs * Update src/packages/excalidraw/CHANGELOG.md --- src/components/App.tsx | 7 +++++++ src/constants.ts | 4 ---- src/excalidraw-app/data/localStorage.ts | 6 ++---- src/excalidraw-app/index.tsx | 2 +- src/packages/excalidraw/CHANGELOG.md | 2 ++ src/packages/excalidraw/README_NEXT.md | 1 + src/packages/excalidraw/example/App.js | 18 ++++++++++++++++++ src/types.ts | 1 + 8 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 4766019c6..c5ec90a86 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -1582,6 +1582,7 @@ class App extends React.Component { appState?: Pick | 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 { 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 38fd62228..26baf34b4 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 6d564bb71..095c61d48 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 915e386de..46c14c4c4 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 eeaa46d5d..c1adeb78e 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 d444537c5..a5f0cb9e4 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` |
MapCollaborator>
| 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 08f7ab927..ecf6ab0ef 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 +