One-click installable libraries (#2179)

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Pete Hunt 2020-10-01 10:12:43 -07:00 committed by GitHub
parent 6b7516bc3c
commit 8ab9ffbe28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 1 deletions

View file

@ -175,6 +175,7 @@ import {
} from "../element/binding";
import { MaybeTransformHandleType } from "../element/transformHandles";
import { renderSpreadsheet } from "../charts";
import { isValidLibrary } from "../data/json";
/**
* @param func handler taking at most single parameter (event).
@ -492,6 +493,31 @@ class App extends React.Component<ExcalidrawProps, AppState> {
return false;
}
private addToLibrary = async (url: string) => {
window.history.replaceState({}, "Excalidraw", window.location.origin);
try {
const request = await fetch(url);
const blob = await request.blob();
const json = JSON.parse(await blob.text());
if (!isValidLibrary(json)) {
throw new Error();
}
if (
window.confirm(
t("alerts.confirmAddLibrary", { numShapes: json.library.length }),
)
) {
await Library.importLibrary(blob);
this.setState({
isLibraryOpen: true,
});
}
} catch (error) {
window.alert(t("alerts.errorLoadingLibrary"));
console.error(error);
}
};
private initializeScene = async () => {
if ("launchQueue" in window && "LaunchParams" in window) {
(window as any).launchQueue.setConsumer(
@ -590,6 +616,12 @@ class App extends React.Component<ExcalidrawProps, AppState> {
commitToHistory: true,
});
}
const addToLibraryUrl = searchParams.get("addLibrary");
if (addToLibraryUrl) {
await this.addToLibrary(addToLibraryUrl);
}
};
public async componentDidMount() {