Import and export library from/to a file (#1940)
Co-authored-by: dwelle <luzar.david@gmail.com>pull/1975/head
parent
7eff6893c5
commit
ee8fa6aaad
@ -0,0 +1,43 @@
|
||||
import { loadLibraryFromBlob } from "./blob";
|
||||
import { LibraryItems, LibraryItem } from "../types";
|
||||
import { loadLibrary, saveLibrary } from "./localStorage";
|
||||
|
||||
export class Library {
|
||||
/** imports library (currently merges, removing duplicates) */
|
||||
static async importLibrary(blob: any) {
|
||||
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]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue