diff --git a/src/components/App.tsx b/src/components/App.tsx index f0ca4e5e..41f4e109 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -532,7 +532,7 @@ class App extends React.Component { return false; } - private addToLibrary = async (url: string) => { + private importLibraryFromUrl = async (url: string) => { window.history.replaceState({}, "Excalidraw", window.location.origin); try { const request = await fetch(url); @@ -683,7 +683,7 @@ class App extends React.Component { const addToLibraryUrl = searchParams.get("addLibrary"); if (addToLibraryUrl) { - await this.addToLibrary(addToLibraryUrl); + await this.importLibraryFromUrl(addToLibraryUrl); } }; diff --git a/src/data/library.ts b/src/data/library.ts index 506f738e..4f5c11a8 100644 --- a/src/data/library.ts +++ b/src/data/library.ts @@ -2,6 +2,8 @@ import { loadLibraryFromBlob } from "./blob"; import { LibraryItems, LibraryItem } from "../types"; import { restoreElements } from "./restore"; import { STORAGE_KEYS } from "../constants"; +import { getNonDeletedElements } from "../element"; +import { NonDeleted, ExcalidrawElement } from "../element/types"; export class Library { private static libraryCache: LibraryItems | null = null; @@ -43,9 +45,15 @@ export class Library { }; const existingLibraryItems = await Library.loadLibrary(); - const filtered = libraryFile.library!.filter((libraryItem) => - isUniqueitem(existingLibraryItems, libraryItem), - ); + + const filtered = libraryFile.library!.reduce((acc, libraryItem) => { + const restored = getNonDeletedElements(restoreElements(libraryItem)); + if (isUniqueitem(existingLibraryItems, restored)) { + acc.push(restored); + } + return acc; + }, [] as (readonly NonDeleted[])[]); + Library.saveLibrary([...existingLibraryItems, ...filtered]); } diff --git a/src/types.ts b/src/types.ts index a59bde7f..5d09b910 100644 --- a/src/types.ts +++ b/src/types.ts @@ -130,7 +130,7 @@ export type SocketUpdateData = SocketUpdateDataSource[keyof SocketUpdateDataSour _brand: "socketUpdateData"; }; -export type LibraryItem = NonDeleted[]; +export type LibraryItem = readonly NonDeleted[]; export type LibraryItems = readonly LibraryItem[]; export interface ExcalidrawProps {