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

* feat: support updating library in updateScene API

* fix

* update docs

* Update src/packages/excalidraw/CHANGELOG.md
This commit is contained in:
Aakansha Doshi 2022-01-06 21:37:33 +05:30 committed by GitHub
parent 5f1616f2c5
commit b475412199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 9 deletions

View File

@ -1582,6 +1582,7 @@ class App extends React.Component<AppProps, AppState> {
appState?: Pick<AppState, K> | null; appState?: Pick<AppState, K> | null;
collaborators?: SceneData["collaborators"]; collaborators?: SceneData["collaborators"];
commitToHistory?: SceneData["commitToHistory"]; commitToHistory?: SceneData["commitToHistory"];
libraryItems?: SceneData["libraryItems"];
}) => { }) => {
if (sceneData.commitToHistory) { if (sceneData.commitToHistory) {
this.history.resumeRecording(); this.history.resumeRecording();
@ -1598,6 +1599,12 @@ class App extends React.Component<AppProps, AppState> {
if (sceneData.collaborators) { if (sceneData.collaborators) {
this.setState({ collaborators: sceneData.collaborators }); this.setState({ collaborators: sceneData.collaborators });
} }
if (sceneData.libraryItems) {
this.library.saveLibrary(
restoreLibraryItems(sceneData.libraryItems, "unpublished"),
);
}
}, },
); );

View File

@ -106,10 +106,6 @@ export const EXPORT_DATA_TYPES = {
export const EXPORT_SOURCE = window.location.origin; export const EXPORT_SOURCE = window.location.origin;
export const STORAGE_KEYS = {
LOCAL_STORAGE_LIBRARY: "excalidraw-library",
} as const;
// time in milliseconds // time in milliseconds
export const IMAGE_RENDER_TIMEOUT = 500; export const IMAGE_RENDER_TIMEOUT = 500;
export const TAP_TWICE_TIMEOUT = 300; export const TAP_TWICE_TIMEOUT = 300;

View File

@ -5,13 +5,13 @@ import {
getDefaultAppState, getDefaultAppState,
} from "../../appState"; } from "../../appState";
import { clearElementsForLocalStorage } from "../../element"; import { clearElementsForLocalStorage } from "../../element";
import { STORAGE_KEYS as APP_STORAGE_KEYS } from "../../constants";
export const STORAGE_KEYS = { export const STORAGE_KEYS = {
LOCAL_STORAGE_ELEMENTS: "excalidraw", LOCAL_STORAGE_ELEMENTS: "excalidraw",
LOCAL_STORAGE_APP_STATE: "excalidraw-state", LOCAL_STORAGE_APP_STATE: "excalidraw-state",
LOCAL_STORAGE_COLLAB: "excalidraw-collab", LOCAL_STORAGE_COLLAB: "excalidraw-collab",
LOCAL_STORAGE_KEY_COLLAB_FORCE_FLAG: "collabLinkForceLoadFlag", LOCAL_STORAGE_KEY_COLLAB_FORCE_FLAG: "collabLinkForceLoadFlag",
LOCAL_STORAGE_LIBRARY: "excalidraw-library",
}; };
export const saveUsernameToLocalStorage = (username: string) => { export const saveUsernameToLocalStorage = (username: string) => {
@ -113,9 +113,7 @@ export const getTotalStorageSize = () => {
try { try {
const appState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE); const appState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
const collab = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB); const collab = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
const library = localStorage.getItem( const library = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_LIBRARY);
APP_STORAGE_KEYS.LOCAL_STORAGE_LIBRARY,
);
const appStateSize = appState?.length || 0; const appStateSize = appState?.length || 0;
const collabSize = collab?.length || 0; const collabSize = collab?.length || 0;

View File

@ -7,7 +7,6 @@ import { TopErrorBoundary } from "../components/TopErrorBoundary";
import { import {
APP_NAME, APP_NAME,
EVENT, EVENT,
STORAGE_KEYS,
TITLE_TIMEOUT, TITLE_TIMEOUT,
URL_HASH_KEYS, URL_HASH_KEYS,
VERSION_TIMEOUT, VERSION_TIMEOUT,
@ -53,6 +52,7 @@ import { exportToBackend, getCollaborationLinkData, loadScene } from "./data";
import { import {
importFromLocalStorage, importFromLocalStorage,
saveToLocalStorage, saveToLocalStorage,
STORAGE_KEYS,
} from "./data/localStorage"; } from "./data/localStorage";
import CustomStats from "./CustomStats"; import CustomStats from "./CustomStats";
import { restoreAppState, RestoredDataState } from "../data/restore"; import { restoreAppState, RestoredDataState } from "../data/restore";

View File

@ -19,6 +19,8 @@ Please add the latest change on the top under the correct section.
### Features ### 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. - 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 - #### BREAKING CHANGE

View File

@ -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. | | `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. | | `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`. | | `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` ### `addFiles`

View File

@ -103,6 +103,24 @@ export default function App() {
> >
Reset Scene Reset Scene
</button> </button>
<button
onClick={() => {
excalidrawRef.current.updateScene({
libraryItems: [
{
status: "published",
elements: initialData.libraryItems[0],
},
{
status: "unpublished",
elements: initialData.libraryItems[1],
},
],
});
}}
>
Update Library
</button>
<label> <label>
<input <input
type="checkbox" type="checkbox"

View File

@ -255,6 +255,7 @@ export type SceneData = {
appState?: ImportedDataState["appState"]; appState?: ImportedDataState["appState"];
collaborators?: Map<string, Collaborator>; collaborators?: Map<string, Collaborator>;
commitToHistory?: boolean; commitToHistory?: boolean;
libraryItems?: LibraryItems | LibraryItems_v1;
}; };
export enum UserIdleState { export enum UserIdleState {