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-03-07 10:20:38 -05:00
|
|
|
import { AppState } from "../types";
|
2020-09-22 21:51:49 +02:00
|
|
|
import { DataState, 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";
|
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
|
|
|
|
2020-09-22 21:51:49 +02:00
|
|
|
const restoreElementWithProperties = <T extends ExcalidrawElement>(
|
2020-07-28 23:40:06 +02:00
|
|
|
element: Required<T>,
|
|
|
|
extra: Omit<Required<T>, keyof ExcalidrawElement>,
|
2020-09-22 21:51:49 +02:00
|
|
|
): T => {
|
2020-05-28 11:41:34 +02:00
|
|
|
const base: Pick<T, keyof ExcalidrawElement> = {
|
|
|
|
type: element.type,
|
2020-10-04 11:12:47 -07:00
|
|
|
// all elements must have version > 0 so getSceneVersion() will pick up
|
2020-05-28 11:41:34 +02:00
|
|
|
// newly added elements
|
|
|
|
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,
|
|
|
|
x: element.x || 0,
|
|
|
|
y: element.y || 0,
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...base,
|
|
|
|
...getNormalizedDimensions(base),
|
|
|
|
...extra,
|
|
|
|
} 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
|
|
|
});
|
|
|
|
case "draw":
|
|
|
|
case "line":
|
|
|
|
case "arrow": {
|
2020-09-22 21:51:49 +02:00
|
|
|
return restoreElementWithProperties(element, {
|
2020-08-08 21:04:15 -07:00
|
|
|
startBinding: element.startBinding,
|
|
|
|
endBinding: element.endBinding,
|
2020-05-28 11:41:34 +02:00
|
|
|
points:
|
|
|
|
// migrate old arrow model to new one
|
|
|
|
!Array.isArray(element.points) || element.points.length < 2
|
|
|
|
? [
|
|
|
|
[0, 0],
|
|
|
|
[element.width, element.height],
|
|
|
|
]
|
|
|
|
: element.points,
|
2020-07-28 23:40:06 +02:00
|
|
|
lastCommittedPoint: null,
|
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
|
|
|
|
|
|
|
// 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-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,
|
|
|
|
// and causing issues if retained
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-10-13 13:46:52 +02:00
|
|
|
const restoreAppState = (
|
|
|
|
appState: ImportedDataState["appState"],
|
|
|
|
localAppState: Partial<AppState> | null,
|
|
|
|
): AppState => {
|
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,
|
|
|
|
offsetLeft: appState.offsetLeft || 0,
|
|
|
|
offsetTop: appState.offsetTop || 0,
|
|
|
|
};
|
|
|
|
};
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-10-13 13:46:52 +02:00
|
|
|
export const restore = (
|
|
|
|
data: ImportedDataState,
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
): DataState => {
|
2020-03-07 10:20:38 -05:00
|
|
|
return {
|
2020-09-22 21:51:49 +02:00
|
|
|
elements: restoreElements(data.elements),
|
2020-10-13 13:46:52 +02:00
|
|
|
appState: restoreAppState(data.appState, localAppState || null),
|
2020-03-07 10:20:38 -05:00
|
|
|
};
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|