2020-07-27 15:29:19 +03:00
|
|
|
import { loadLibraryFromBlob } from "./blob";
|
|
|
|
import { LibraryItems, LibraryItem } from "../types";
|
2021-11-17 23:53:43 +05:30
|
|
|
import { restoreElements, restoreLibraryItems } from "./restore";
|
2020-11-08 17:08:22 +01:00
|
|
|
import { getNonDeletedElements } from "../element";
|
2021-06-01 23:52:13 +05:30
|
|
|
import type App from "../components/App";
|
2020-07-27 15:29:19 +03:00
|
|
|
|
2021-04-21 23:38:24 +05:30
|
|
|
class Library {
|
|
|
|
private libraryCache: LibraryItems | null = null;
|
|
|
|
private app: App;
|
2020-10-30 21:01:41 +01:00
|
|
|
|
2021-04-21 23:38:24 +05:30
|
|
|
constructor(app: App) {
|
|
|
|
this.app = app;
|
|
|
|
}
|
|
|
|
|
|
|
|
resetLibrary = async () => {
|
|
|
|
await this.app.props.onLibraryChange?.([]);
|
|
|
|
this.libraryCache = [];
|
2020-10-30 21:01:41 +01:00
|
|
|
};
|
|
|
|
|
2021-05-09 21:42:12 +02:00
|
|
|
restoreLibraryItem = (libraryItem: LibraryItem): LibraryItem | null => {
|
2021-11-17 23:53:43 +05:30
|
|
|
const elements = getNonDeletedElements(
|
|
|
|
restoreElements(libraryItem.elements, null),
|
|
|
|
);
|
|
|
|
return elements.length ? { ...libraryItem, elements } : null;
|
2021-05-09 21:42:12 +02:00
|
|
|
};
|
|
|
|
|
2020-07-27 15:29:19 +03:00
|
|
|
/** imports library (currently merges, removing duplicates) */
|
2021-11-17 23:53:43 +05:30
|
|
|
async importLibrary(blob: Blob, defaultStatus = "unpublished") {
|
2020-07-27 15:29:19 +03:00
|
|
|
const libraryFile = await loadLibraryFromBlob(blob);
|
2021-11-17 23:53:43 +05:30
|
|
|
if (!libraryFile || !(libraryFile.libraryItems || libraryFile.library)) {
|
2020-07-27 15:29:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if library item does not exist already in current library
|
|
|
|
*/
|
|
|
|
const isUniqueitem = (
|
|
|
|
existingLibraryItems: LibraryItems,
|
|
|
|
targetLibraryItem: LibraryItem,
|
|
|
|
) => {
|
|
|
|
return !existingLibraryItems.find((libraryItem) => {
|
2021-11-17 23:53:43 +05:30
|
|
|
if (libraryItem.elements.length !== targetLibraryItem.elements.length) {
|
2020-07-27 15:29:19 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// detect z-index difference by checking the excalidraw elements
|
2020-11-05 19:06:18 +02:00
|
|
|
// are in order
|
2021-11-17 23:53:43 +05:30
|
|
|
return libraryItem.elements.every((libItemExcalidrawItem, idx) => {
|
2020-07-27 15:29:19 +03:00
|
|
|
return (
|
2021-11-17 23:53:43 +05:30
|
|
|
libItemExcalidrawItem.id === targetLibraryItem.elements[idx].id &&
|
2020-07-27 15:29:19 +03:00
|
|
|
libItemExcalidrawItem.versionNonce ===
|
2021-11-17 23:53:43 +05:30
|
|
|
targetLibraryItem.elements[idx].versionNonce
|
2020-07-27 15:29:19 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-21 23:38:24 +05:30
|
|
|
const existingLibraryItems = await this.loadLibrary();
|
2020-11-08 17:08:22 +01:00
|
|
|
|
2021-11-17 23:53:43 +05:30
|
|
|
const library = libraryFile.libraryItems || libraryFile.library || [];
|
|
|
|
const restoredLibItems = restoreLibraryItems(
|
|
|
|
library,
|
|
|
|
defaultStatus as "published" | "unpublished",
|
|
|
|
);
|
|
|
|
const filteredItems = [];
|
|
|
|
for (const item of restoredLibItems) {
|
|
|
|
const restoredItem = this.restoreLibraryItem(item as LibraryItem);
|
2021-05-09 21:42:12 +02:00
|
|
|
if (restoredItem && isUniqueitem(existingLibraryItems, restoredItem)) {
|
2021-11-17 23:53:43 +05:30
|
|
|
filteredItems.push(restoredItem);
|
2020-11-08 17:08:22 +01:00
|
|
|
}
|
2021-11-17 23:53:43 +05:30
|
|
|
}
|
2020-11-08 17:08:22 +01:00
|
|
|
|
2021-11-17 23:53:43 +05:30
|
|
|
await this.saveLibrary([...filteredItems, ...existingLibraryItems]);
|
2020-07-27 15:29:19 +03:00
|
|
|
}
|
2020-10-30 21:01:41 +01:00
|
|
|
|
2021-04-21 23:38:24 +05:30
|
|
|
loadLibrary = (): Promise<LibraryItems> => {
|
2020-10-30 21:01:41 +01:00
|
|
|
return new Promise(async (resolve) => {
|
2021-04-21 23:38:24 +05:30
|
|
|
if (this.libraryCache) {
|
|
|
|
return resolve(JSON.parse(JSON.stringify(this.libraryCache)));
|
2020-10-30 21:01:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-04-21 23:38:24 +05:30
|
|
|
const libraryItems = this.app.libraryItemsFromStorage;
|
|
|
|
if (!libraryItems) {
|
2020-10-30 21:01:41 +01:00
|
|
|
return resolve([]);
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:42:12 +02:00
|
|
|
const items = libraryItems.reduce((acc, item) => {
|
|
|
|
const restoredItem = this.restoreLibraryItem(item);
|
|
|
|
if (restoredItem) {
|
|
|
|
acc.push(item);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, [] as Mutable<LibraryItems>);
|
2020-10-30 21:01:41 +01:00
|
|
|
|
|
|
|
// clone to ensure we don't mutate the cached library elements in the app
|
2021-04-21 23:38:24 +05:30
|
|
|
this.libraryCache = JSON.parse(JSON.stringify(items));
|
2020-10-30 21:01:41 +01:00
|
|
|
|
|
|
|
resolve(items);
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
2020-11-05 19:06:18 +02:00
|
|
|
console.error(error);
|
2020-10-30 21:01:41 +01:00
|
|
|
resolve([]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-21 23:38:24 +05:30
|
|
|
saveLibrary = async (items: LibraryItems) => {
|
|
|
|
const prevLibraryItems = this.libraryCache;
|
2020-10-30 21:01:41 +01:00
|
|
|
try {
|
|
|
|
const serializedItems = JSON.stringify(items);
|
2021-04-21 23:38:24 +05:30
|
|
|
// cache optimistically so that the app has access to the latest
|
2020-11-05 19:06:18 +02:00
|
|
|
// immediately
|
2021-04-21 23:38:24 +05:30
|
|
|
this.libraryCache = JSON.parse(serializedItems);
|
|
|
|
await this.app.props.onLibraryChange?.(items);
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
2021-04-21 23:38:24 +05:30
|
|
|
this.libraryCache = prevLibraryItems;
|
|
|
|
throw error;
|
2020-10-30 21:01:41 +01:00
|
|
|
}
|
|
|
|
};
|
2020-07-27 15:29:19 +03:00
|
|
|
}
|
2021-04-21 23:38:24 +05:30
|
|
|
|
|
|
|
export default Library;
|