Add AppState to export json to fix various import bugs (#358)

* export background, app state

* review comments
This commit is contained in:
Brady Madden 2020-01-15 21:08:52 -05:00 committed by GitHub
parent 0e56cd4f56
commit a3aa57d98b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 11 deletions

View File

@ -40,7 +40,7 @@ export const actionChangeExportBackground: Action = {
export const actionSaveScene: Action = { export const actionSaveScene: Action = {
name: "saveScene", name: "saveScene",
perform: (elements, appState, value) => { perform: (elements, appState, value) => {
saveAsJSON(elements, appState.name); saveAsJSON(elements, appState);
return {}; return {};
}, },
PanelComponent: ({ updateData }) => ( PanelComponent: ({ updateData }) => (
@ -56,8 +56,12 @@ export const actionSaveScene: Action = {
export const actionLoadScene: Action = { export const actionLoadScene: Action = {
name: "loadScene", name: "loadScene",
perform: (elements, appState, loadedElements) => { perform: (
return { elements: loadedElements }; elements,
appState,
{ elements: loadedElements, appState: loadedAppState }
) => {
return { elements: loadedElements, appState: loadedAppState };
}, },
PanelComponent: ({ updateData }) => ( PanelComponent: ({ updateData }) => (
<ToolIcon <ToolIcon
@ -66,8 +70,8 @@ export const actionLoadScene: Action = {
title="Load" title="Load"
aria-label="Load" aria-label="Load"
onClick={() => { onClick={() => {
loadFromJSON().then(({ elements }) => { loadFromJSON().then(({ elements, appState }) => {
updateData(elements); updateData({ elements: elements, appState: appState });
}); });
}} }}
/> />

View File

@ -3,6 +3,7 @@ import rough from "roughjs/bin/rough";
import { ExcalidrawElement } from "../element/types"; import { ExcalidrawElement } from "../element/types";
import { getElementAbsoluteCoords } from "../element"; import { getElementAbsoluteCoords } from "../element";
import { getDefaultAppState } from "../appState";
import { renderScene } from "../renderer"; import { renderScene } from "../renderer";
import { AppState } from "../types"; import { AppState } from "../types";
@ -25,21 +26,22 @@ function saveFile(name: string, data: string) {
interface DataState { interface DataState {
elements: readonly ExcalidrawElement[]; elements: readonly ExcalidrawElement[];
appState: any; appState: AppState;
} }
export function saveAsJSON( export function saveAsJSON(
elements: readonly ExcalidrawElement[], elements: readonly ExcalidrawElement[],
name: string appState: AppState
) { ) {
const serialized = JSON.stringify({ const serialized = JSON.stringify({
version: 1, version: 1,
source: window.location.origin, source: window.location.origin,
elements: elements.map(({ shape, ...el }) => el) elements: elements.map(({ shape, ...el }) => el),
appState: appState
}); });
saveFile( saveFile(
`${name}.json`, `${appState.name}.json`,
"data:text/plain;charset=utf-8," + encodeURIComponent(serialized) "data:text/plain;charset=utf-8," + encodeURIComponent(serialized)
); );
} }
@ -64,14 +66,17 @@ export function loadFromJSON() {
return new Promise<DataState>(resolve => { return new Promise<DataState>(resolve => {
reader.onloadend = () => { reader.onloadend = () => {
if (reader.readyState === FileReader.DONE) { if (reader.readyState === FileReader.DONE) {
const defaultAppState = getDefaultAppState();
let elements = []; let elements = [];
let appState = defaultAppState;
try { try {
const data = JSON.parse(reader.result as string); const data = JSON.parse(reader.result as string);
elements = data.elements || []; elements = data.elements || [];
appState = { ...defaultAppState, ...data.appState };
} catch (e) { } catch (e) {
// Do nothing because elements array is already empty // Do nothing because elements array is already empty
} }
resolve(restore(elements, null)); resolve(restore(elements, appState));
} }
}; };
}); });
@ -215,7 +220,7 @@ export function exportCanvas(
function restore( function restore(
savedElements: readonly ExcalidrawElement[], savedElements: readonly ExcalidrawElement[],
savedState: any savedState: AppState
): DataState { ): DataState {
return { return {
elements: savedElements.map(element => ({ elements: savedElements.map(element => ({