mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Make data restoration functions immutable
- Make mutations in App component
This commit is contained in:
parent
2ef809fe5d
commit
642a11e380
2 changed files with 63 additions and 39 deletions
|
@ -22,6 +22,11 @@ function saveFile(name: string, data: string) {
|
|||
link.remove();
|
||||
}
|
||||
|
||||
interface DataState {
|
||||
elements: ExcalidrawElement[];
|
||||
appState: any;
|
||||
}
|
||||
|
||||
export function saveAsJSON(elements: ExcalidrawElement[], name: string) {
|
||||
const serialized = JSON.stringify({
|
||||
version: 1,
|
||||
|
@ -35,7 +40,7 @@ export function saveAsJSON(elements: ExcalidrawElement[], name: string) {
|
|||
);
|
||||
}
|
||||
|
||||
export function loadFromJSON(elements: ExcalidrawElement[]) {
|
||||
export function loadFromJSON() {
|
||||
const input = document.createElement("input");
|
||||
const reader = new FileReader();
|
||||
input.type = "file";
|
||||
|
@ -52,12 +57,17 @@ export function loadFromJSON(elements: ExcalidrawElement[]) {
|
|||
|
||||
input.click();
|
||||
|
||||
return new Promise(resolve => {
|
||||
return new Promise<DataState>(resolve => {
|
||||
reader.onloadend = () => {
|
||||
if (reader.readyState === FileReader.DONE) {
|
||||
const data = JSON.parse(reader.result as string);
|
||||
restore(elements, data.elements, null);
|
||||
resolve();
|
||||
let elements = [];
|
||||
try {
|
||||
const data = JSON.parse(reader.result as string);
|
||||
elements = data.elements || [];
|
||||
} catch (e) {
|
||||
// Do nothing because elements array is already empty
|
||||
}
|
||||
resolve(restore(elements, null));
|
||||
}
|
||||
};
|
||||
});
|
||||
|
@ -130,43 +140,48 @@ export function exportAsPNG(
|
|||
}
|
||||
|
||||
function restore(
|
||||
elements: ExcalidrawElement[],
|
||||
savedElements: string | ExcalidrawElement[] | null,
|
||||
savedState: string | null
|
||||
) {
|
||||
try {
|
||||
if (savedElements) {
|
||||
elements.splice(
|
||||
0,
|
||||
elements.length,
|
||||
...(typeof savedElements === "string"
|
||||
? JSON.parse(savedElements)
|
||||
: savedElements)
|
||||
);
|
||||
elements.forEach((element: ExcalidrawElement) => {
|
||||
element.id = element.id || nanoid();
|
||||
element.fillStyle = element.fillStyle || "hachure";
|
||||
element.strokeWidth = element.strokeWidth || 1;
|
||||
element.roughness = element.roughness || 1;
|
||||
element.opacity =
|
||||
element.opacity === null || element.opacity === undefined
|
||||
? 100
|
||||
: element.opacity;
|
||||
});
|
||||
}
|
||||
|
||||
return savedState ? JSON.parse(savedState) : null;
|
||||
} catch (e) {
|
||||
elements.splice(0, elements.length);
|
||||
return null;
|
||||
}
|
||||
savedElements: ExcalidrawElement[],
|
||||
savedState: any
|
||||
): DataState {
|
||||
return {
|
||||
elements: savedElements.map(element => ({
|
||||
...element,
|
||||
id: element.id || nanoid(),
|
||||
fillStyle: element.fillStyle || "hachure",
|
||||
strokeWidth: element.strokeWidth || 1,
|
||||
roughness: element.roughness || 1,
|
||||
opacity:
|
||||
element.opacity === null || element.opacity === undefined
|
||||
? 100
|
||||
: element.opacity
|
||||
})),
|
||||
appState: savedState
|
||||
};
|
||||
}
|
||||
|
||||
export function restoreFromLocalStorage(elements: ExcalidrawElement[]) {
|
||||
export function restoreFromLocalStorage() {
|
||||
const savedElements = localStorage.getItem(LOCAL_STORAGE_KEY);
|
||||
const savedState = localStorage.getItem(LOCAL_STORAGE_KEY_STATE);
|
||||
|
||||
return restore(elements, savedElements, savedState);
|
||||
let elements = [];
|
||||
if (savedElements) {
|
||||
try {
|
||||
elements = JSON.parse(savedElements);
|
||||
} catch (e) {
|
||||
// Do nothing because elements array is already empty
|
||||
}
|
||||
}
|
||||
|
||||
let appState = null;
|
||||
if (savedState) {
|
||||
try {
|
||||
appState = JSON.parse(savedState);
|
||||
} catch (e) {
|
||||
// Do nothing because appState is already null
|
||||
}
|
||||
}
|
||||
|
||||
return restore(elements, appState);
|
||||
}
|
||||
|
||||
export function saveToLocalStorage(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue