mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
parent
7ab0c1aba8
commit
6428b59ccb
18 changed files with 599 additions and 20 deletions
|
@ -348,11 +348,12 @@ export const exportCanvas = async (
|
|||
window.alert(t("alerts.couldNotCopyToClipboard"));
|
||||
}
|
||||
} else if (type === "backend") {
|
||||
const appState = getDefaultAppState();
|
||||
if (exportBackground) {
|
||||
appState.viewBackgroundColor = viewBackgroundColor;
|
||||
}
|
||||
exportToBackend(elements, appState);
|
||||
exportToBackend(elements, {
|
||||
...appState,
|
||||
viewBackgroundColor: exportBackground
|
||||
? appState.viewBackgroundColor
|
||||
: getDefaultAppState().viewBackgroundColor,
|
||||
});
|
||||
}
|
||||
|
||||
// clean up the DOM
|
||||
|
|
|
@ -1,11 +1,54 @@
|
|||
import { ExcalidrawElement } from "../element/types";
|
||||
import { AppState } from "../types";
|
||||
import { AppState, LibraryItems } from "../types";
|
||||
import { clearAppStateForLocalStorage } from "../appState";
|
||||
import { restore } from "./restore";
|
||||
|
||||
const LOCAL_STORAGE_KEY = "excalidraw";
|
||||
const LOCAL_STORAGE_KEY_STATE = "excalidraw-state";
|
||||
const LOCAL_STORAGE_KEY_COLLAB = "excalidraw-collab";
|
||||
const LOCAL_STORAGE_KEY_LIBRARY = "excalidraw-library";
|
||||
|
||||
let _LATEST_LIBRARY_ITEMS: LibraryItems | null = null;
|
||||
export const loadLibrary = (): Promise<LibraryItems> => {
|
||||
return new Promise(async (resolve) => {
|
||||
if (_LATEST_LIBRARY_ITEMS) {
|
||||
return resolve(JSON.parse(JSON.stringify(_LATEST_LIBRARY_ITEMS)));
|
||||
}
|
||||
|
||||
try {
|
||||
const data = localStorage.getItem(LOCAL_STORAGE_KEY_LIBRARY);
|
||||
if (!data) {
|
||||
return resolve([]);
|
||||
}
|
||||
|
||||
const items = (JSON.parse(data) as ExcalidrawElement[][]).map(
|
||||
(elements) => restore(elements, null).elements,
|
||||
) as Mutable<LibraryItems>;
|
||||
|
||||
// clone to ensure we don't mutate the cached library elements in the app
|
||||
_LATEST_LIBRARY_ITEMS = JSON.parse(JSON.stringify(items));
|
||||
|
||||
resolve(items);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
resolve([]);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const saveLibrary = (items: LibraryItems) => {
|
||||
const prevLibraryItems = _LATEST_LIBRARY_ITEMS;
|
||||
try {
|
||||
const serializedItems = JSON.stringify(items);
|
||||
// cache optimistically so that consumers have access to the latest
|
||||
// immediately
|
||||
_LATEST_LIBRARY_ITEMS = JSON.parse(serializedItems);
|
||||
localStorage.setItem(LOCAL_STORAGE_KEY_LIBRARY, serializedItems);
|
||||
} catch (e) {
|
||||
_LATEST_LIBRARY_ITEMS = prevLibraryItems;
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
|
||||
export const saveUsernameToLocalStorage = (username: string) => {
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue