Drag and drop JSON exports to canvas loads the scene (#676)

* Drag and drop JSON exports to canvas loads the scene

* remove unneeded onDragOver
This commit is contained in:
lissitz 2020-02-02 22:02:13 +01:00 committed by GitHub
parent 53994e71e5
commit 92a0f100b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 6 deletions

View File

@ -37,6 +37,7 @@ import {
addToLoadedScenes, addToLoadedScenes,
loadedScenes, loadedScenes,
calculateScrollCenter, calculateScrollCenter,
loadFromBlob,
} from "./scene"; } from "./scene";
import { renderScene } from "./renderer"; import { renderScene } from "./renderer";
@ -310,6 +311,8 @@ export class App extends React.Component<any, AppState> {
window.addEventListener("resize", this.onResize, false); window.addEventListener("resize", this.onResize, false);
window.addEventListener("unload", this.onUnload, false); window.addEventListener("unload", this.onUnload, false);
window.addEventListener("blur", this.onUnload, false); window.addEventListener("blur", this.onUnload, false);
window.addEventListener("dragover", e => e.preventDefault(), false);
window.addEventListener("drop", e => e.preventDefault(), false);
const searchParams = new URLSearchParams(window.location.search); const searchParams = new URLSearchParams(window.location.search);
const id = searchParams.get("id"); const id = searchParams.get("id");
@ -1784,6 +1787,19 @@ export class App extends React.Component<any, AppState> {
const hitElement = getElementAtPosition(elements, x, y); const hitElement = getElementAtPosition(elements, x, y);
document.documentElement.style.cursor = hitElement ? "move" : ""; document.documentElement.style.cursor = hitElement ? "move" : "";
}} }}
onDrop={e => {
const file = e.dataTransfer.files[0];
if (file?.type === "application/json") {
loadFromBlob(file)
.then(({ elements, appState }) =>
this.syncActionResult({
elements,
appState,
} as ActionResult),
)
.catch(err => console.error(err));
}
}}
> >
{t("labels.drawingCanvas")} {t("labels.drawingCanvas")}
</canvas> </canvas>

View File

@ -76,8 +76,16 @@ export async function saveAsJSON(
(window as any).handle, (window as any).handle,
); );
} }
export async function loadFromJSON() { export async function loadFromJSON() {
const blob = await fileOpen({
description: "Excalidraw files",
extensions: ["json"],
mimeTypes: ["application/json"],
});
return loadFromBlob(blob);
}
export async function loadFromBlob(blob: any) {
const updateAppState = (contents: string) => { const updateAppState = (contents: string) => {
const defaultAppState = getDefaultAppState(); const defaultAppState = getDefaultAppState();
let elements = []; let elements = [];
@ -92,11 +100,6 @@ export async function loadFromJSON() {
return { elements, appState }; return { elements, appState };
}; };
const blob = await fileOpen({
description: "Excalidraw files",
extensions: ["json"],
mimeTypes: ["application/json"],
});
if (blob.handle) { if (blob.handle) {
(window as any).handle = blob.handle; (window as any).handle = blob.handle;
} }

View File

@ -10,6 +10,7 @@ export {
export { export {
exportCanvas, exportCanvas,
loadFromJSON, loadFromJSON,
loadFromBlob,
saveAsJSON, saveAsJSON,
restoreFromLocalStorage, restoreFromLocalStorage,
saveToLocalStorage, saveToLocalStorage,