2020-07-27 15:29:19 +03:00
|
|
|
import { loadLibraryFromBlob } from "./blob";
|
|
|
|
import { LibraryItems, LibraryItem } from "../types";
|
|
|
|
import { loadLibrary, saveLibrary } from "./localStorage";
|
|
|
|
|
|
|
|
export class Library {
|
|
|
|
/** imports library (currently merges, removing duplicates) */
|
2020-10-19 10:53:37 +02:00
|
|
|
static async importLibrary(blob: Blob) {
|
2020-07-27 15:29:19 +03:00
|
|
|
const libraryFile = await loadLibraryFromBlob(blob);
|
|
|
|
if (!libraryFile || !libraryFile.library) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if library item does not exist already in current library
|
|
|
|
*/
|
|
|
|
const isUniqueitem = (
|
|
|
|
existingLibraryItems: LibraryItems,
|
|
|
|
targetLibraryItem: LibraryItem,
|
|
|
|
) => {
|
|
|
|
return !existingLibraryItems.find((libraryItem) => {
|
|
|
|
if (libraryItem.length !== targetLibraryItem.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// detect z-index difference by checking the excalidraw elements
|
|
|
|
// are in order
|
|
|
|
return libraryItem.every((libItemExcalidrawItem, idx) => {
|
|
|
|
return (
|
|
|
|
libItemExcalidrawItem.id === targetLibraryItem[idx].id &&
|
|
|
|
libItemExcalidrawItem.versionNonce ===
|
|
|
|
targetLibraryItem[idx].versionNonce
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const existingLibraryItems = await loadLibrary();
|
|
|
|
const filtered = libraryFile.library!.filter((libraryItem) =>
|
|
|
|
isUniqueitem(existingLibraryItems, libraryItem),
|
|
|
|
);
|
|
|
|
saveLibrary([...existingLibraryItems, ...filtered]);
|
|
|
|
}
|
|
|
|
}
|