Import and export library from/to a file (#1940)

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Mohammed Salman 2020-07-27 15:29:19 +03:00 committed by GitHub
parent 7eff6893c5
commit ee8fa6aaad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 199 additions and 39 deletions

View file

@ -2,17 +2,11 @@ import { getDefaultAppState, cleanAppStateForExport } from "../appState";
import { restore } from "./restore";
import { t } from "../i18n";
import { AppState } from "../types";
import { LibraryData } from "./types";
import { calculateScrollCenter } from "../scene";
/**
* @param blob
* @param appState if provided, used for centering scroll to restored scene
*/
export const loadFromBlob = async (blob: any, appState?: AppState) => {
if (blob.handle) {
(window as any).handle = blob.handle;
}
let contents;
const loadFileContents = async (blob: any) => {
let contents: string;
if ("text" in Blob) {
contents = await blob.text();
} else {
@ -26,7 +20,19 @@ export const loadFromBlob = async (blob: any, appState?: AppState) => {
};
});
}
return contents;
};
/**
* @param blob
* @param appState if provided, used for centering scroll to restored scene
*/
export const loadFromBlob = async (blob: any, appState?: AppState) => {
if (blob.handle) {
(window as any).handle = blob.handle;
}
const contents = await loadFileContents(blob);
const defaultAppState = getDefaultAppState();
let elements = [];
let _appState = appState || defaultAppState;
@ -47,3 +53,12 @@ export const loadFromBlob = async (blob: any, appState?: AppState) => {
return restore(elements, _appState);
};
export const loadLibraryFromBlob = async (blob: any) => {
const contents = await loadFileContents(blob);
const data: LibraryData = JSON.parse(contents);
if (data.type !== "excalidrawlib") {
throw new Error(t("alerts.couldNotLoadInvalidFile"));
}
return data;
};