Fix library import (#2360)
This commit is contained in:
parent
dad9ad9bf4
commit
5b829772d9
@ -532,7 +532,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private addToLibrary = async (url: string) => {
|
private importLibraryFromUrl = async (url: string) => {
|
||||||
window.history.replaceState({}, "Excalidraw", window.location.origin);
|
window.history.replaceState({}, "Excalidraw", window.location.origin);
|
||||||
try {
|
try {
|
||||||
const request = await fetch(url);
|
const request = await fetch(url);
|
||||||
@ -683,7 +683,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
|
|||||||
const addToLibraryUrl = searchParams.get("addLibrary");
|
const addToLibraryUrl = searchParams.get("addLibrary");
|
||||||
|
|
||||||
if (addToLibraryUrl) {
|
if (addToLibraryUrl) {
|
||||||
await this.addToLibrary(addToLibraryUrl);
|
await this.importLibraryFromUrl(addToLibraryUrl);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@ import { loadLibraryFromBlob } from "./blob";
|
|||||||
import { LibraryItems, LibraryItem } from "../types";
|
import { LibraryItems, LibraryItem } from "../types";
|
||||||
import { restoreElements } from "./restore";
|
import { restoreElements } from "./restore";
|
||||||
import { STORAGE_KEYS } from "../constants";
|
import { STORAGE_KEYS } from "../constants";
|
||||||
|
import { getNonDeletedElements } from "../element";
|
||||||
|
import { NonDeleted, ExcalidrawElement } from "../element/types";
|
||||||
|
|
||||||
export class Library {
|
export class Library {
|
||||||
private static libraryCache: LibraryItems | null = null;
|
private static libraryCache: LibraryItems | null = null;
|
||||||
@ -43,9 +45,15 @@ export class Library {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const existingLibraryItems = await Library.loadLibrary();
|
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<ExcalidrawElement>[])[]);
|
||||||
|
|
||||||
Library.saveLibrary([...existingLibraryItems, ...filtered]);
|
Library.saveLibrary([...existingLibraryItems, ...filtered]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +130,7 @@ export type SocketUpdateData = SocketUpdateDataSource[keyof SocketUpdateDataSour
|
|||||||
_brand: "socketUpdateData";
|
_brand: "socketUpdateData";
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LibraryItem = NonDeleted<ExcalidrawElement>[];
|
export type LibraryItem = readonly NonDeleted<ExcalidrawElement>[];
|
||||||
export type LibraryItems = readonly LibraryItem[];
|
export type LibraryItems = readonly LibraryItem[];
|
||||||
|
|
||||||
export interface ExcalidrawProps {
|
export interface ExcalidrawProps {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user