Library MVP (#1787)

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Pete Hunt 2020-07-10 02:20:23 -07:00 committed by GitHub
parent 7ab0c1aba8
commit 6428b59ccb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 599 additions and 20 deletions

View file

@ -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

View file

@ -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 {