2020-05-27 15:14:50 +02:00
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
|
|
|
FontFamily,
|
2020-05-28 11:41:34 +02:00
|
|
|
ExcalidrawSelectionElement,
|
2020-05-27 15:14:50 +02:00
|
|
|
} from "../element/types";
|
2020-11-04 17:49:15 +00:00
|
|
|
import { AppState, NormalizedZoomValue } from "../types";
|
2021-04-10 19:17:49 +02:00
|
|
|
import { ImportedDataState } from "./types";
|
2020-05-28 11:41:34 +02:00
|
|
|
import { isInvisiblySmallElement, getNormalizedDimensions } 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 {
|
|
|
|
FONT_FAMILY,
|
|
|
|
DEFAULT_FONT_FAMILY,
|
|
|
|
DEFAULT_TEXT_ALIGN,
|
|
|
|
DEFAULT_VERTICAL_ALIGN,
|
|
|
|
} 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";
|
2020-05-27 15:14:50 +02:00
|
|
|
|
2021-04-10 19:17:49 +02:00
|
|
|
type RestoredAppState = Omit<
|
|
|
|
AppState,
|
|
|
|
"offsetTop" | "offsetLeft" | "width" | "height"
|
|
|
|
>;
|
|
|
|
|
2021-05-10 11:01:10 +02:00
|
|
|
export const AllowedExcalidrawElementTypes: Record<
|
|
|
|
ExcalidrawElement["type"],
|
|
|
|
true
|
|
|
|
> = {
|
|
|
|
selection: true,
|
|
|
|
text: true,
|
|
|
|
rectangle: true,
|
|
|
|
diamond: true,
|
|
|
|
ellipse: true,
|
|
|
|
line: true,
|
|
|
|
arrow: true,
|
|
|
|
freedraw: true,
|
|
|
|
};
|
|
|
|
|
2021-04-10 19:17:49 +02:00
|
|
|
export type RestoredDataState = {
|
|
|
|
elements: ExcalidrawElement[];
|
|
|
|
appState: RestoredAppState;
|
|
|
|
};
|
|
|
|
|
2020-05-27 15:14:50 +02:00
|
|
|
const getFontFamilyByName = (fontFamilyName: string): FontFamily => {
|
|
|
|
for (const [id, fontFamilyString] of Object.entries(FONT_FAMILY)) {
|
|
|
|
if (fontFamilyString.includes(fontFamilyName)) {
|
|
|
|
return parseInt(id) as FontFamily;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DEFAULT_FONT_FAMILY;
|
|
|
|
};
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2021-05-24 20:35:53 +02:00
|
|
|
const restoreElementWithProperties = <
|
|
|
|
T extends ExcalidrawElement,
|
|
|
|
K extends keyof Omit<
|
|
|
|
Required<T>,
|
|
|
|
Exclude<keyof ExcalidrawElement, "type" | "x" | "y">
|
|
|
|
>
|
|
|
|
>(
|
2020-07-28 23:40:06 +02:00
|
|
|
element: Required<T>,
|
2021-05-24 20:35:53 +02:00
|
|
|
extra: Pick<T, K>,
|
2020-09-22 21:51:49 +02:00
|
|
|
): T => {
|
2020-05-28 11:41:34 +02:00
|
|
|
const base: Pick<T, keyof ExcalidrawElement> = {
|
2021-05-24 20:35:53 +02:00
|
|
|
type: (extra as Partial<T>).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-05-24 20:35:53 +02:00
|
|
|
x: (extra as Partial<T>).x ?? element.x ?? 0,
|
|
|
|
y: (extra as Partial<T>).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"),
|
2020-08-08 21:04:15 -07:00
|
|
|
boundElementIds: element.boundElementIds ?? [],
|
2020-05-28 11:41:34 +02:00
|
|
|
};
|
|
|
|
|
2020-10-28 18:28:07 +02:00
|
|
|
return ({
|
2020-05-28 11:41:34 +02:00
|
|
|
...base,
|
|
|
|
...getNormalizedDimensions(base),
|
|
|
|
...extra,
|
2020-10-28 18:28:07 +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>,
|
|
|
|
): typeof element => {
|
|
|
|
switch (element.type) {
|
|
|
|
case "text":
|
|
|
|
let fontSize = element.fontSize;
|
|
|
|
let fontFamily = element.fontFamily;
|
|
|
|
if ("font" in element) {
|
|
|
|
const [fontPx, _fontFamily]: [
|
|
|
|
string,
|
|
|
|
string,
|
|
|
|
] = (element as any).font.split(" ");
|
|
|
|
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,
|
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,
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
// 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"],
|
|
|
|
): ExcalidrawElement[] => {
|
|
|
|
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)) {
|
2020-09-22 21:51:49 +02:00
|
|
|
const migratedElement = restoreElement(element);
|
2020-05-28 11:41:34 +02:00
|
|
|
if (migratedElement) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-02-15 18:52:04 +05:30
|
|
|
export const restoreAppState = (
|
2020-10-13 13:46:52 +02:00
|
|
|
appState: ImportedDataState["appState"],
|
|
|
|
localAppState: Partial<AppState> | null,
|
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;
|
|
|
|
|
2020-10-13 13:46:52 +02:00
|
|
|
for (const [key, val] of Object.entries(defaultAppState) as [
|
|
|
|
keyof typeof defaultAppState,
|
|
|
|
any,
|
|
|
|
][]) {
|
|
|
|
const restoredValue = appState[key];
|
|
|
|
const localValue = localAppState ? localAppState[key] : undefined;
|
|
|
|
(nextAppState as any)[key] =
|
|
|
|
restoredValue !== undefined
|
|
|
|
? restoredValue
|
|
|
|
: localValue !== undefined
|
|
|
|
? localValue
|
|
|
|
: val;
|
2020-09-22 21:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...nextAppState,
|
2021-05-10 11:01:10 +02:00
|
|
|
elementType: AllowedExcalidrawElementTypes[nextAppState.elementType]
|
|
|
|
? nextAppState.elementType
|
|
|
|
: "selection",
|
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,
|
|
|
|
translation: defaultAppState.zoom.translation,
|
|
|
|
}
|
|
|
|
: appState.zoom || defaultAppState.zoom,
|
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 = (
|
2020-12-05 20:00:53 +05:30
|
|
|
data: ImportedDataState | 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-04-10 19:17:49 +02:00
|
|
|
): RestoredDataState => {
|
2020-03-07 10:20:38 -05:00
|
|
|
return {
|
2020-12-05 20:00:53 +05:30
|
|
|
elements: restoreElements(data?.elements),
|
|
|
|
appState: restoreAppState(data?.appState, localAppState || null),
|
2020-03-07 10:20:38 -05:00
|
|
|
};
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|