Remove invisibly small elements on scene load (#754)

This commit is contained in:
Gasim Gasimzada 2020-02-10 18:38:41 +04:00 committed by GitHub
parent fa12125db0
commit ad4ad238ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,7 +11,11 @@ import { ExportType } from "./types";
import { exportToCanvas, exportToSvg } from "./export"; import { exportToCanvas, exportToSvg } from "./export";
import nanoid from "nanoid"; import nanoid from "nanoid";
import { fileOpen, fileSave } from "browser-nativefs"; import { fileOpen, fileSave } from "browser-nativefs";
import { getCommonBounds, normalizeDimensions } from "../element"; import {
getCommonBounds,
normalizeDimensions,
isInvisiblySmallElement,
} from "../element";
import { Point } from "roughjs/bin/geometry"; import { Point } from "roughjs/bin/geometry";
import { t } from "../i18n"; import { t } from "../i18n";
@ -334,51 +338,53 @@ function restore(
savedState: AppState | null, savedState: AppState | null,
opts?: { scrollToContent: boolean }, opts?: { scrollToContent: boolean },
): DataState { ): DataState {
const elements = savedElements.map(element => { const elements = savedElements
let points: Point[] = []; .filter(el => !isInvisiblySmallElement(el))
if (element.type === "arrow") { .map(element => {
if (Array.isArray(element.points)) { let points: Point[] = [];
// if point array is empty, add one point to the arrow if (element.type === "arrow") {
// this is used as fail safe to convert incoming data to a valid if (Array.isArray(element.points)) {
// arrow. In the new arrow, width and height are not being usde // if point array is empty, add one point to the arrow
points = element.points.length > 0 ? element.points : [[0, 0]]; // 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],
];
}
} 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;
}
} else { } else {
// convert old arrow type to a new one normalizeDimensions(element);
// old arrow spec used width and height
// to determine the endpoints
points = [
[0, 0],
[element.width, element.height],
];
} }
} 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;
}
} else {
normalizeDimensions(element);
}
return { return {
...element, ...element,
id: element.id || nanoid(), id: element.id || nanoid(),
fillStyle: element.fillStyle || "hachure", fillStyle: element.fillStyle || "hachure",
strokeWidth: element.strokeWidth || 1, strokeWidth: element.strokeWidth || 1,
roughness: element.roughness || 1, roughness: element.roughness || 1,
opacity: opacity:
element.opacity === null || element.opacity === undefined element.opacity === null || element.opacity === undefined
? 100 ? 100
: element.opacity, : element.opacity,
points, points,
}; };
}); });
if (opts?.scrollToContent && savedState) { if (opts?.scrollToContent && savedState) {
savedState = { ...savedState, ...calculateScrollCenter(elements) }; savedState = { ...savedState, ...calculateScrollCenter(elements) };