2020-03-14 21:48:51 -07:00
|
|
|
import { Point } from "../types";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { AppState } from "../types";
|
|
|
|
import { DataState } from "./types";
|
|
|
|
import { isInvisiblySmallElement, normalizeDimensions } from "../element";
|
|
|
|
import nanoid from "nanoid";
|
|
|
|
import { calculateScrollCenter } from "../scene";
|
|
|
|
|
|
|
|
export function restore(
|
2020-03-17 20:55:40 +01:00
|
|
|
// we're making the elements mutable for this API because we want to
|
|
|
|
// efficiently remove/tweak properties on them (to migrate old scenes)
|
|
|
|
savedElements: readonly Mutable<ExcalidrawElement>[],
|
2020-03-07 10:20:38 -05:00
|
|
|
savedState: AppState | null,
|
|
|
|
opts?: { scrollToContent: boolean },
|
|
|
|
): DataState {
|
|
|
|
const elements = savedElements
|
2020-03-10 19:41:41 +01:00
|
|
|
.filter(el => {
|
|
|
|
// filtering out selection, which is legacy, no longer kept in elements,
|
|
|
|
// and causing issues if retained
|
|
|
|
return el.type !== "selection" && !isInvisiblySmallElement(el);
|
|
|
|
})
|
2020-03-07 10:20:38 -05:00
|
|
|
.map(element => {
|
|
|
|
let points: Point[] = [];
|
|
|
|
if (element.type === "arrow") {
|
|
|
|
if (Array.isArray(element.points)) {
|
|
|
|
// if point array is empty, add one point to the arrow
|
|
|
|
// this is used as fail safe to convert incoming data to a valid
|
|
|
|
// arrow. In the new arrow, width and height are not being usde
|
|
|
|
points = element.points.length > 0 ? element.points : [[0, 0]];
|
|
|
|
} else {
|
|
|
|
// convert old arrow type to a new one
|
|
|
|
// old arrow spec used width and height
|
|
|
|
// to determine the endpoints
|
|
|
|
points = [
|
|
|
|
[0, 0],
|
|
|
|
[element.width, element.height],
|
|
|
|
];
|
|
|
|
}
|
2020-03-17 20:55:40 +01:00
|
|
|
element.points = points;
|
2020-03-07 10:20:38 -05:00
|
|
|
} else if (element.type === "line") {
|
|
|
|
// old spec, pre-arrows
|
|
|
|
// old spec, post-arrows
|
|
|
|
if (!Array.isArray(element.points) || element.points.length === 0) {
|
|
|
|
points = [
|
|
|
|
[0, 0],
|
|
|
|
[element.width, element.height],
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
points = element.points;
|
|
|
|
}
|
2020-03-17 20:55:40 +01:00
|
|
|
element.points = points;
|
2020-03-07 10:20:38 -05:00
|
|
|
} else {
|
|
|
|
normalizeDimensions(element);
|
2020-03-17 20:55:40 +01:00
|
|
|
// old spec, where non-linear elements used to have empty points arrays
|
|
|
|
if ("points" in element) {
|
|
|
|
delete element.points;
|
|
|
|
}
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...element,
|
2020-03-14 20:46:57 -07:00
|
|
|
// all elements must have version > 0 so getDrawingVersion() will pick up newly added elements
|
|
|
|
version: element.version || 1,
|
2020-03-07 10:20:38 -05:00
|
|
|
id: element.id || nanoid(),
|
|
|
|
fillStyle: element.fillStyle || "hachure",
|
|
|
|
strokeWidth: element.strokeWidth || 1,
|
2020-03-12 17:49:11 +01:00
|
|
|
roughness: element.roughness ?? 1,
|
2020-03-07 10:20:38 -05:00
|
|
|
opacity:
|
|
|
|
element.opacity === null || element.opacity === undefined
|
|
|
|
? 100
|
|
|
|
: element.opacity,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (opts?.scrollToContent && savedState) {
|
|
|
|
savedState = { ...savedState, ...calculateScrollCenter(elements) };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
elements: elements,
|
|
|
|
appState: savedState,
|
|
|
|
};
|
|
|
|
}
|