2020-05-27 15:14:50 +02:00
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
2020-05-28 11:41:34 +02:00
|
|
|
ExcalidrawSelectionElement,
|
2021-06-13 21:26:55 +05:30
|
|
|
FontFamilyValues,
|
2020-05-27 15:14:50 +02:00
|
|
|
} from "../element/types";
|
2021-11-17 23:53:43 +05:30
|
|
|
import {
|
|
|
|
AppState,
|
|
|
|
BinaryFiles,
|
|
|
|
LibraryItem,
|
|
|
|
NormalizedZoomValue,
|
|
|
|
} from "../types";
|
2022-10-17 12:25:24 +02:00
|
|
|
import { ImportedDataState, LegacyAppState } from "./types";
|
2022-04-14 16:20:35 +02:00
|
|
|
import {
|
|
|
|
getNonDeletedElements,
|
|
|
|
getNormalizedDimensions,
|
|
|
|
isInvisiblySmallElement,
|
|
|
|
} from "../element";
|
2020-08-15 00:59:43 +09:00
|
|
|
import { isLinearElementType } from "../element/typeChecks";
|
2020-03-23 16:38:41 -07:00
|
|
|
import { randomId } from "../random";
|
2020-06-25 21:21:27 +02:00
|
|
|
import {
|
|
|
|
DEFAULT_FONT_FAMILY,
|
|
|
|
DEFAULT_TEXT_ALIGN,
|
|
|
|
DEFAULT_VERTICAL_ALIGN,
|
2022-10-08 21:42:05 +03:00
|
|
|
PRECEDING_ELEMENT_KEY,
|
2021-06-13 21:26:55 +05:30
|
|
|
FONT_FAMILY,
|
2020-06-25 21:21:27 +02:00
|
|
|
} from "../constants";
|
2020-09-22 21:51:49 +02:00
|
|
|
import { getDefaultAppState } from "../appState";
|
2021-05-24 20:35:53 +02:00
|
|
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
2021-07-04 22:23:35 +02:00
|
|
|
import { bumpVersion } from "../element/mutateElement";
|
2022-05-06 18:21:22 +05:30
|
|
|
import { getUpdatedTimestamp, updateActiveTool } from "../utils";
|
2021-11-26 12:46:23 +01:00
|
|
|
import { arrayToMap } from "../utils";
|
2020-05-27 15:14:50 +02:00
|
|
|
|
2021-04-10 19:17:49 +02:00
|
|
|
type RestoredAppState = Omit<
|
|
|
|
AppState,
|
|
|
|
"offsetTop" | "offsetLeft" | "width" | "height"
|
|
|
|
>;
|
|
|
|
|
2022-03-25 20:46:01 +05:30
|
|
|
export const AllowedExcalidrawActiveTools: Record<
|
|
|
|
AppState["activeTool"]["type"],
|
2022-03-11 19:53:42 +05:30
|
|
|
boolean
|
2021-05-10 11:01:10 +02:00
|
|
|
> = {
|
|
|
|
selection: true,
|
|
|
|
text: true,
|
|
|
|
rectangle: true,
|
|
|
|
diamond: true,
|
|
|
|
ellipse: true,
|
|
|
|
line: true,
|
2021-10-21 22:05:48 +02:00
|
|
|
image: true,
|
2021-05-10 11:01:10 +02:00
|
|
|
arrow: true,
|
|
|
|
freedraw: true,
|
2022-03-11 19:53:42 +05:30
|
|
|
eraser: false,
|
2022-05-06 18:21:22 +05:30
|
|
|
custom: true,
|
2021-05-10 11:01:10 +02:00
|
|
|
};
|
|
|
|
|
2021-04-10 19:17:49 +02:00
|
|
|
export type RestoredDataState = {
|
|
|
|
elements: ExcalidrawElement[];
|
|
|
|
appState: RestoredAppState;
|
2021-10-21 22:05:48 +02:00
|
|
|
files: BinaryFiles;
|
2021-04-10 19:17:49 +02:00
|
|
|
};
|
|
|
|
|
2021-06-13 21:26:55 +05:30
|
|
|
const getFontFamilyByName = (fontFamilyName: string): FontFamilyValues => {
|
|
|
|
if (Object.keys(FONT_FAMILY).includes(fontFamilyName)) {
|
|
|
|
return FONT_FAMILY[
|
|
|
|
fontFamilyName as keyof typeof FONT_FAMILY
|
|
|
|
] as FontFamilyValues;
|
2020-05-27 15:14:50 +02:00
|
|
|
}
|
|
|
|
return DEFAULT_FONT_FAMILY;
|
|
|
|
};
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2021-05-24 20:35:53 +02:00
|
|
|
const restoreElementWithProperties = <
|
2022-08-18 14:02:46 +02:00
|
|
|
T extends Required<Omit<ExcalidrawElement, "customData">> & {
|
|
|
|
customData?: ExcalidrawElement["customData"];
|
2021-12-14 16:07:01 +01:00
|
|
|
/** @deprecated */
|
|
|
|
boundElementIds?: readonly ExcalidrawElement["id"][];
|
2022-10-08 21:42:05 +03:00
|
|
|
/** metadata that may be present in elements during collaboration */
|
|
|
|
[PRECEDING_ELEMENT_KEY]?: string;
|
2021-12-14 16:07:01 +01:00
|
|
|
},
|
2022-08-18 14:02:46 +02:00
|
|
|
K extends Pick<T, keyof Omit<Required<T>, keyof ExcalidrawElement>>,
|
|
|
|
>(
|
|
|
|
element: T,
|
2021-10-21 22:05:48 +02:00
|
|
|
extra: Pick<
|
|
|
|
T,
|
|
|
|
// This extra Pick<T, keyof K> ensure no excess properties are passed.
|
|
|
|
// @ts-ignore TS complains here but type checks the call sites fine.
|
|
|
|
keyof K
|
|
|
|
> &
|
|
|
|
Partial<Pick<ExcalidrawElement, "type" | "x" | "y">>,
|
2020-09-22 21:51:49 +02:00
|
|
|
): T => {
|
2022-10-08 21:42:05 +03:00
|
|
|
const base: Pick<T, keyof ExcalidrawElement> & {
|
|
|
|
[PRECEDING_ELEMENT_KEY]?: string;
|
|
|
|
} = {
|
2021-10-21 22:05:48 +02:00
|
|
|
type: extra.type || element.type,
|
2020-10-04 11:12:47 -07:00
|
|
|
// all elements must have version > 0 so getSceneVersion() will pick up
|
2020-11-05 19:06:18 +02:00
|
|
|
// newly added elements
|
2020-05-28 11:41:34 +02:00
|
|
|
version: element.version || 1,
|
|
|
|
versionNonce: element.versionNonce ?? 0,
|
2020-10-26 15:45:51 +01:00
|
|
|
isDeleted: element.isDeleted ?? false,
|
2020-05-28 11:41:34 +02:00
|
|
|
id: element.id || randomId(),
|
|
|
|
fillStyle: element.fillStyle || "hachure",
|
|
|
|
strokeWidth: element.strokeWidth || 1,
|
|
|
|
strokeStyle: element.strokeStyle ?? "solid",
|
|
|
|
roughness: element.roughness ?? 1,
|
|
|
|
opacity: element.opacity == null ? 100 : element.opacity,
|
|
|
|
angle: element.angle || 0,
|
2021-10-21 22:05:48 +02:00
|
|
|
x: extra.x ?? element.x ?? 0,
|
|
|
|
y: extra.y ?? element.y ?? 0,
|
2020-05-28 11:41:34 +02:00
|
|
|
strokeColor: element.strokeColor,
|
|
|
|
backgroundColor: element.backgroundColor,
|
|
|
|
width: element.width || 0,
|
|
|
|
height: element.height || 0,
|
|
|
|
seed: element.seed ?? 1,
|
2020-08-08 21:04:15 -07:00
|
|
|
groupIds: element.groupIds ?? [],
|
2020-08-15 00:59:43 +09:00
|
|
|
strokeSharpness:
|
|
|
|
element.strokeSharpness ??
|
|
|
|
(isLinearElementType(element.type) ? "round" : "sharp"),
|
2021-12-14 16:07:01 +01:00
|
|
|
boundElements: element.boundElementIds
|
|
|
|
? element.boundElementIds.map((id) => ({ type: "arrow", id }))
|
|
|
|
: element.boundElements ?? [],
|
2021-11-24 18:38:33 +01:00
|
|
|
updated: element.updated ?? getUpdatedTimestamp(),
|
2022-02-03 20:34:59 +05:30
|
|
|
link: element.link ?? null,
|
2022-04-07 12:43:29 +01:00
|
|
|
locked: element.locked ?? false,
|
2020-05-28 11:41:34 +02:00
|
|
|
};
|
|
|
|
|
2022-08-18 14:02:46 +02:00
|
|
|
if ("customData" in element) {
|
|
|
|
base.customData = element.customData;
|
|
|
|
}
|
|
|
|
|
2022-10-08 21:42:05 +03:00
|
|
|
if (PRECEDING_ELEMENT_KEY in element) {
|
|
|
|
base[PRECEDING_ELEMENT_KEY] = element[PRECEDING_ELEMENT_KEY];
|
|
|
|
}
|
|
|
|
|
2021-11-01 15:24:05 +02:00
|
|
|
return {
|
2020-05-28 11:41:34 +02:00
|
|
|
...base,
|
|
|
|
...getNormalizedDimensions(base),
|
|
|
|
...extra,
|
2021-11-01 15:24:05 +02:00
|
|
|
} as unknown as T;
|
2020-09-22 21:51:49 +02:00
|
|
|
};
|
2020-05-28 11:41:34 +02:00
|
|
|
|
2020-09-22 21:51:49 +02:00
|
|
|
const restoreElement = (
|
2020-05-28 11:41:34 +02:00
|
|
|
element: Exclude<ExcalidrawElement, ExcalidrawSelectionElement>,
|
2021-10-21 22:05:48 +02:00
|
|
|
): typeof element | null => {
|
2020-05-28 11:41:34 +02:00
|
|
|
switch (element.type) {
|
|
|
|
case "text":
|
|
|
|
let fontSize = element.fontSize;
|
|
|
|
let fontFamily = element.fontFamily;
|
|
|
|
if ("font" in element) {
|
2021-11-01 15:24:05 +02:00
|
|
|
const [fontPx, _fontFamily]: [string, string] = (
|
|
|
|
element as any
|
|
|
|
).font.split(" ");
|
2020-05-28 11:41:34 +02:00
|
|
|
fontSize = parseInt(fontPx, 10);
|
|
|
|
fontFamily = getFontFamilyByName(_fontFamily);
|
|
|
|
}
|
2020-09-22 21:51:49 +02:00
|
|
|
return restoreElementWithProperties(element, {
|
2020-05-28 11:41:34 +02:00
|
|
|
fontSize,
|
|
|
|
fontFamily,
|
|
|
|
text: element.text ?? "",
|
|
|
|
baseline: element.baseline,
|
2020-06-25 21:21:27 +02:00
|
|
|
textAlign: element.textAlign || DEFAULT_TEXT_ALIGN,
|
|
|
|
verticalAlign: element.verticalAlign || DEFAULT_VERTICAL_ALIGN,
|
2021-12-16 21:14:03 +05:30
|
|
|
containerId: element.containerId ?? null,
|
2022-01-12 23:21:45 +01:00
|
|
|
originalText: element.originalText || element.text,
|
2020-05-28 11:41:34 +02:00
|
|
|
});
|
2021-05-09 16:42:10 +01:00
|
|
|
case "freedraw": {
|
|
|
|
return restoreElementWithProperties(element, {
|
|
|
|
points: element.points,
|
|
|
|
lastCommittedPoint: null,
|
|
|
|
simulatePressure: element.simulatePressure,
|
|
|
|
pressures: element.pressures,
|
|
|
|
});
|
|
|
|
}
|
2021-10-21 22:05:48 +02:00
|
|
|
case "image":
|
|
|
|
return restoreElementWithProperties(element, {
|
|
|
|
status: element.status || "pending",
|
|
|
|
fileId: element.fileId,
|
|
|
|
scale: element.scale || [1, 1],
|
|
|
|
});
|
2020-05-28 11:41:34 +02:00
|
|
|
case "line":
|
2021-05-10 16:19:31 +02:00
|
|
|
// @ts-ignore LEGACY type
|
|
|
|
// eslint-disable-next-line no-fallthrough
|
|
|
|
case "draw":
|
2020-05-28 11:41:34 +02:00
|
|
|
case "arrow": {
|
2020-12-08 15:02:55 +00:00
|
|
|
const {
|
|
|
|
startArrowhead = null,
|
|
|
|
endArrowhead = element.type === "arrow" ? "arrow" : null,
|
|
|
|
} = element;
|
|
|
|
|
2021-05-24 20:35:53 +02:00
|
|
|
let x = element.x;
|
|
|
|
let y = element.y;
|
|
|
|
let points = // migrate old arrow model to new one
|
|
|
|
!Array.isArray(element.points) || element.points.length < 2
|
|
|
|
? [
|
|
|
|
[0, 0],
|
|
|
|
[element.width, element.height],
|
|
|
|
]
|
|
|
|
: element.points;
|
|
|
|
|
|
|
|
if (points[0][0] !== 0 || points[0][1] !== 0) {
|
|
|
|
({ points, x, y } = LinearElementEditor.getNormalizedPoints(element));
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:51:49 +02:00
|
|
|
return restoreElementWithProperties(element, {
|
2021-05-10 11:01:10 +02:00
|
|
|
type:
|
|
|
|
(element.type as ExcalidrawElement["type"] | "draw") === "draw"
|
|
|
|
? "line"
|
|
|
|
: element.type,
|
2020-08-08 21:04:15 -07:00
|
|
|
startBinding: element.startBinding,
|
|
|
|
endBinding: element.endBinding,
|
2020-07-28 23:40:06 +02:00
|
|
|
lastCommittedPoint: null,
|
2020-12-08 15:02:55 +00:00
|
|
|
startArrowhead,
|
|
|
|
endArrowhead,
|
2021-05-24 20:35:53 +02:00
|
|
|
points,
|
|
|
|
x,
|
|
|
|
y,
|
2020-05-28 11:41:34 +02:00
|
|
|
});
|
|
|
|
}
|
2022-05-06 18:21:22 +05:30
|
|
|
|
2020-05-28 11:41:34 +02:00
|
|
|
// generic elements
|
|
|
|
case "ellipse":
|
2020-09-22 21:51:49 +02:00
|
|
|
return restoreElementWithProperties(element, {});
|
2020-05-28 11:41:34 +02:00
|
|
|
case "rectangle":
|
2020-09-22 21:51:49 +02:00
|
|
|
return restoreElementWithProperties(element, {});
|
2020-05-28 11:41:34 +02:00
|
|
|
case "diamond":
|
2020-09-22 21:51:49 +02:00
|
|
|
return restoreElementWithProperties(element, {});
|
2020-05-28 11:41:34 +02:00
|
|
|
|
2020-11-05 19:06:18 +02:00
|
|
|
// Don't use default case so as to catch a missing an element type case.
|
|
|
|
// We also don't want to throw, but instead return void so we filter
|
|
|
|
// out these unsupported elements from the restored array.
|
2020-05-28 11:41:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-13 13:46:52 +02:00
|
|
|
export const restoreElements = (
|
2020-09-22 21:51:49 +02:00
|
|
|
elements: ImportedDataState["elements"],
|
2021-07-04 22:23:35 +02:00
|
|
|
/** NOTE doesn't serve for reconciliation */
|
|
|
|
localElements: readonly ExcalidrawElement[] | null | undefined,
|
2020-09-22 21:51:49 +02:00
|
|
|
): ExcalidrawElement[] => {
|
2021-11-26 12:46:23 +01:00
|
|
|
const localElementsMap = localElements ? arrayToMap(localElements) : null;
|
2020-09-22 21:51:49 +02:00
|
|
|
return (elements || []).reduce((elements, element) => {
|
2020-05-28 11:41:34 +02:00
|
|
|
// filtering out selection, which is legacy, no longer kept in elements,
|
2020-11-05 19:06:18 +02:00
|
|
|
// and causing issues if retained
|
2020-05-28 11:41:34 +02:00
|
|
|
if (element.type !== "selection" && !isInvisiblySmallElement(element)) {
|
2021-10-21 22:05:48 +02:00
|
|
|
let migratedElement: ExcalidrawElement | null = restoreElement(element);
|
2020-05-28 11:41:34 +02:00
|
|
|
if (migratedElement) {
|
2021-11-26 12:46:23 +01:00
|
|
|
const localElement = localElementsMap?.get(element.id);
|
2021-07-04 22:23:35 +02:00
|
|
|
if (localElement && localElement.version > migratedElement.version) {
|
|
|
|
migratedElement = bumpVersion(migratedElement, localElement.version);
|
|
|
|
}
|
2020-05-28 11:41:34 +02:00
|
|
|
elements.push(migratedElement);
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
2020-05-28 11:41:34 +02:00
|
|
|
}
|
|
|
|
return elements;
|
|
|
|
}, [] as ExcalidrawElement[]);
|
2020-09-22 21:51:49 +02:00
|
|
|
};
|
|
|
|
|
2022-10-17 12:25:24 +02:00
|
|
|
const coalesceAppStateValue = <
|
|
|
|
T extends keyof ReturnType<typeof getDefaultAppState>,
|
|
|
|
>(
|
|
|
|
key: T,
|
|
|
|
appState: Exclude<ImportedDataState["appState"], null | undefined>,
|
|
|
|
defaultAppState: ReturnType<typeof getDefaultAppState>,
|
|
|
|
) => {
|
|
|
|
const value = appState[key];
|
|
|
|
// NOTE the value! assertion is needed in TS 4.5.5 (fixed in newer versions)
|
|
|
|
return value !== undefined ? value! : defaultAppState[key];
|
|
|
|
};
|
|
|
|
|
|
|
|
const LegacyAppStateMigrations: {
|
|
|
|
[K in keyof LegacyAppState]: (
|
|
|
|
ImportedDataState: Exclude<ImportedDataState["appState"], null | undefined>,
|
|
|
|
defaultAppState: ReturnType<typeof getDefaultAppState>,
|
|
|
|
) => [LegacyAppState[K][1], AppState[LegacyAppState[K][1]]];
|
|
|
|
} = {
|
|
|
|
isLibraryOpen: (appState, defaultAppState) => {
|
|
|
|
return [
|
|
|
|
"openSidebar",
|
|
|
|
"isLibraryOpen" in appState
|
|
|
|
? appState.isLibraryOpen
|
|
|
|
? "library"
|
|
|
|
: null
|
|
|
|
: coalesceAppStateValue("openSidebar", appState, defaultAppState),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
isLibraryMenuDocked: (appState, defaultAppState) => {
|
|
|
|
return [
|
|
|
|
"isSidebarDocked",
|
|
|
|
appState.isLibraryMenuDocked ??
|
|
|
|
coalesceAppStateValue("isSidebarDocked", appState, defaultAppState),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-02-15 18:52:04 +05:30
|
|
|
export const restoreAppState = (
|
2020-10-13 13:46:52 +02:00
|
|
|
appState: ImportedDataState["appState"],
|
2021-07-04 22:23:35 +02:00
|
|
|
localAppState: Partial<AppState> | null | undefined,
|
2021-04-10 19:17:49 +02:00
|
|
|
): RestoredAppState => {
|
2020-09-22 21:51:49 +02:00
|
|
|
appState = appState || {};
|
|
|
|
const defaultAppState = getDefaultAppState();
|
|
|
|
const nextAppState = {} as typeof defaultAppState;
|
2022-10-17 12:25:24 +02:00
|
|
|
|
|
|
|
// first, migrate all legacy AppState properties to new ones. We do it
|
|
|
|
// in one go before migrate the rest of the properties in case the new ones
|
|
|
|
// depend on checking any other key (i.e. they are coupled)
|
|
|
|
for (const legacyKey of Object.keys(
|
|
|
|
LegacyAppStateMigrations,
|
|
|
|
) as (keyof typeof LegacyAppStateMigrations)[]) {
|
|
|
|
if (legacyKey in appState) {
|
|
|
|
const [nextKey, nextValue] = LegacyAppStateMigrations[legacyKey](
|
|
|
|
appState,
|
|
|
|
defaultAppState,
|
|
|
|
);
|
|
|
|
(nextAppState as any)[nextKey] = nextValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 22:23:35 +02:00
|
|
|
for (const [key, defaultValue] of Object.entries(defaultAppState) as [
|
2020-10-13 13:46:52 +02:00
|
|
|
keyof typeof defaultAppState,
|
|
|
|
any,
|
|
|
|
][]) {
|
2022-10-17 12:25:24 +02:00
|
|
|
// if AppState contains a legacy key, prefer that one and migrate its
|
|
|
|
// value to the new one
|
2021-07-04 22:23:35 +02:00
|
|
|
const suppliedValue = appState[key];
|
2022-10-17 12:25:24 +02:00
|
|
|
|
2020-10-13 13:46:52 +02:00
|
|
|
const localValue = localAppState ? localAppState[key] : undefined;
|
|
|
|
(nextAppState as any)[key] =
|
2021-07-04 22:23:35 +02:00
|
|
|
suppliedValue !== undefined
|
|
|
|
? suppliedValue
|
2020-10-13 13:46:52 +02:00
|
|
|
: localValue !== undefined
|
|
|
|
? localValue
|
2021-07-04 22:23:35 +02:00
|
|
|
: defaultValue;
|
2020-09-22 21:51:49 +02:00
|
|
|
}
|
2022-05-06 18:21:22 +05:30
|
|
|
|
2020-09-22 21:51:49 +02:00
|
|
|
return {
|
|
|
|
...nextAppState,
|
2022-04-01 17:25:21 +01:00
|
|
|
cursorButton: localAppState?.cursorButton || "up",
|
2022-03-30 10:53:22 +02:00
|
|
|
// reset on fresh restore so as to hide the UI button if penMode not active
|
|
|
|
penDetected:
|
|
|
|
localAppState?.penDetected ??
|
|
|
|
(appState.penMode ? appState.penDetected ?? false : false),
|
2022-03-29 21:37:09 +02:00
|
|
|
activeTool: {
|
2022-05-06 18:21:22 +05:30
|
|
|
...updateActiveTool(
|
|
|
|
defaultAppState,
|
|
|
|
nextAppState.activeTool.type &&
|
|
|
|
AllowedExcalidrawActiveTools[nextAppState.activeTool.type]
|
|
|
|
? nextAppState.activeTool
|
|
|
|
: { type: "selection" },
|
|
|
|
),
|
2022-03-29 21:37:09 +02:00
|
|
|
lastActiveToolBeforeEraser: null,
|
|
|
|
locked: nextAppState.activeTool.locked ?? false,
|
|
|
|
},
|
2020-11-05 19:06:18 +02:00
|
|
|
// Migrates from previous version where appState.zoom was a number
|
2020-11-04 17:49:15 +00:00
|
|
|
zoom:
|
|
|
|
typeof appState.zoom === "number"
|
|
|
|
? {
|
|
|
|
value: appState.zoom as NormalizedZoomValue,
|
|
|
|
}
|
|
|
|
: appState.zoom || defaultAppState.zoom,
|
2022-06-21 20:03:23 +05:00
|
|
|
// when sidebar docked and user left it open in last session,
|
|
|
|
// keep it open. If not docked, keep it closed irrespective of last state.
|
2022-10-17 12:25:24 +02:00
|
|
|
openSidebar:
|
|
|
|
nextAppState.openSidebar === "library"
|
|
|
|
? nextAppState.isSidebarDocked
|
|
|
|
? "library"
|
|
|
|
: null
|
|
|
|
: nextAppState.openSidebar,
|
2020-09-22 21:51:49 +02:00
|
|
|
};
|
|
|
|
};
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-10-13 13:46:52 +02:00
|
|
|
export const restore = (
|
2022-04-20 14:40:03 +02:00
|
|
|
data: Pick<ImportedDataState, "appState" | "elements" | "files"> | null,
|
2020-10-13 13:46:52 +02:00
|
|
|
/**
|
|
|
|
* Local AppState (`this.state` or initial state from localStorage) so that we
|
|
|
|
* don't overwrite local state with default values (when values not
|
|
|
|
* explicitly specified).
|
|
|
|
* Supply `null` if you can't get access to it.
|
|
|
|
*/
|
|
|
|
localAppState: Partial<AppState> | null | undefined,
|
2021-07-04 22:23:35 +02:00
|
|
|
localElements: readonly ExcalidrawElement[] | null | undefined,
|
2021-04-10 19:17:49 +02:00
|
|
|
): RestoredDataState => {
|
2020-03-07 10:20:38 -05:00
|
|
|
return {
|
2021-07-04 22:23:35 +02:00
|
|
|
elements: restoreElements(data?.elements, localElements),
|
2020-12-05 20:00:53 +05:30
|
|
|
appState: restoreAppState(data?.appState, localAppState || null),
|
2021-10-21 22:05:48 +02:00
|
|
|
files: data?.files || {},
|
2020-03-07 10:20:38 -05:00
|
|
|
};
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2021-11-17 23:53:43 +05:30
|
|
|
|
2022-04-14 16:20:35 +02:00
|
|
|
const restoreLibraryItem = (libraryItem: LibraryItem) => {
|
|
|
|
const elements = restoreElements(
|
|
|
|
getNonDeletedElements(libraryItem.elements),
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
return elements.length ? { ...libraryItem, elements } : null;
|
|
|
|
};
|
|
|
|
|
2021-11-17 23:53:43 +05:30
|
|
|
export const restoreLibraryItems = (
|
2022-04-20 14:40:03 +02:00
|
|
|
libraryItems: ImportedDataState["libraryItems"] = [],
|
2021-11-17 23:53:43 +05:30
|
|
|
defaultStatus: LibraryItem["status"],
|
|
|
|
) => {
|
|
|
|
const restoredItems: LibraryItem[] = [];
|
|
|
|
for (const item of libraryItems) {
|
|
|
|
// migrate older libraries
|
|
|
|
if (Array.isArray(item)) {
|
2022-04-14 16:20:35 +02:00
|
|
|
const restoredItem = restoreLibraryItem({
|
2021-11-17 23:53:43 +05:30
|
|
|
status: defaultStatus,
|
|
|
|
elements: item,
|
|
|
|
id: randomId(),
|
|
|
|
created: Date.now(),
|
|
|
|
});
|
2022-04-14 16:20:35 +02:00
|
|
|
if (restoredItem) {
|
|
|
|
restoredItems.push(restoredItem);
|
|
|
|
}
|
2021-11-17 23:53:43 +05:30
|
|
|
} else {
|
2022-04-14 16:20:35 +02:00
|
|
|
const _item = item as MarkOptional<
|
|
|
|
LibraryItem,
|
|
|
|
"id" | "status" | "created"
|
|
|
|
>;
|
|
|
|
const restoredItem = restoreLibraryItem({
|
2021-11-17 23:53:43 +05:30
|
|
|
..._item,
|
|
|
|
id: _item.id || randomId(),
|
|
|
|
status: _item.status || defaultStatus,
|
|
|
|
created: _item.created || Date.now(),
|
|
|
|
});
|
2022-04-14 16:20:35 +02:00
|
|
|
if (restoredItem) {
|
|
|
|
restoredItems.push(restoredItem);
|
|
|
|
}
|
2021-11-17 23:53:43 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return restoredItems;
|
|
|
|
};
|