Rewrite restore to guard against missing migrations (#1664)

This commit is contained in:
David Luzar 2020-05-28 11:41:34 +02:00 committed by GitHub
parent 56f8bc092d
commit 44a88d2d58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 131 additions and 117 deletions

View file

@ -45,7 +45,7 @@ export {
getPerfectElementSize,
isInvisiblySmallElement,
resizePerfectLineForNWHandler,
normalizeDimensions,
getNormalizedDimensions,
} from "./sizeHelpers";
export { showSelectedShapeActions } from "./showSelectedShapeActions";

View file

@ -81,31 +81,32 @@ export const resizePerfectLineForNWHandler = (
}
};
/**
* @returns {boolean} whether element was normalized
*/
export const normalizeDimensions = (
element: ExcalidrawElement | null,
): element is ExcalidrawElement => {
if (!element || (element.width >= 0 && element.height >= 0)) {
return false;
}
export const getNormalizedDimensions = (
element: Pick<ExcalidrawElement, "width" | "height" | "x" | "y">,
): {
width: ExcalidrawElement["width"];
height: ExcalidrawElement["height"];
x: ExcalidrawElement["x"];
y: ExcalidrawElement["y"];
} => {
const ret = {
width: element.width,
height: element.height,
x: element.x,
y: element.y,
};
if (element.width < 0) {
const nextWidth = Math.abs(element.width);
mutateElement(element, {
width: nextWidth,
x: element.x - nextWidth,
});
ret.width = nextWidth;
ret.x = element.x - nextWidth;
}
if (element.height < 0) {
const nextHeight = Math.abs(element.height);
mutateElement(element, {
height: nextHeight,
y: element.y - nextHeight,
});
ret.height = nextHeight;
ret.y = element.y - nextHeight;
}
return true;
return ret;
};

View file

@ -24,12 +24,17 @@ type _ExcalidrawElementBase = Readonly<{
groupIds: GroupId[];
}>;
export type ExcalidrawSelectionElement = _ExcalidrawElementBase & {
type: "selection";
};
/**
* These are elements that don't have any additional properties.
*/
export type ExcalidrawGenericElement = _ExcalidrawElementBase & {
type: "selection" | "rectangle" | "diamond" | "ellipse";
};
export type ExcalidrawGenericElement =
| ExcalidrawSelectionElement
| (_ExcalidrawElementBase & {
type: "rectangle" | "diamond" | "ellipse";
});
/**
* ExcalidrawElement should be JSON serializable and (eventually) contain