2020-03-14 21:48:51 -07:00
|
|
|
import { ExcalidrawElement } from "./types";
|
|
|
|
import { invalidateShapeForElement } from "../renderer/renderElement";
|
2020-07-30 14:50:59 +05:30
|
|
|
import Scene from "../scene/Scene";
|
2020-03-17 11:01:11 -07:00
|
|
|
import { getSizeFromPoints } from "../points";
|
2020-03-23 16:38:41 -07:00
|
|
|
import { randomInteger } from "../random";
|
2020-06-01 11:35:44 +02:00
|
|
|
import { Point } from "../types";
|
2021-11-24 18:38:33 +01:00
|
|
|
import { getUpdatedTimestamp } from "../utils";
|
2023-03-04 19:21:57 +01:00
|
|
|
import { Mutable } from "../utility-types";
|
2020-03-10 20:11:02 -07:00
|
|
|
|
|
|
|
type ElementUpdate<TElement extends ExcalidrawElement> = Omit<
|
|
|
|
Partial<TElement>,
|
2020-07-30 20:14:38 +02:00
|
|
|
"id" | "version" | "versionNonce"
|
2020-03-10 20:11:02 -07:00
|
|
|
>;
|
2020-03-09 22:34:50 -07:00
|
|
|
|
2020-03-09 23:37:42 -07:00
|
|
|
// This function tracks updates of text elements for the purposes for collaboration.
|
|
|
|
// The version is used to compare updates when more than one user is working in
|
2020-03-16 19:07:47 -07:00
|
|
|
// the same drawing. Note: this will trigger the component to update. Make sure you
|
|
|
|
// are calling it either from a React event handler or within unstable_batchedUpdates().
|
2020-05-20 16:21:37 +03:00
|
|
|
export const mutateElement = <TElement extends Mutable<ExcalidrawElement>>(
|
2020-03-14 21:48:51 -07:00
|
|
|
element: TElement,
|
|
|
|
updates: ElementUpdate<TElement>,
|
2021-10-21 22:05:48 +02:00
|
|
|
informMutation = true,
|
|
|
|
): TElement => {
|
2020-05-28 09:50:56 +02:00
|
|
|
let didChange = false;
|
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
// casting to any because can't use `in` operator
|
|
|
|
// (see https://github.com/microsoft/TypeScript/issues/21732)
|
2021-10-21 22:05:48 +02:00
|
|
|
const { points, fileId } = updates as any;
|
2020-03-14 21:48:51 -07:00
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
if (typeof points !== "undefined") {
|
|
|
|
updates = { ...getSizeFromPoints(points), ...updates };
|
2020-03-17 11:01:11 -07:00
|
|
|
}
|
|
|
|
|
2020-03-14 21:48:51 -07:00
|
|
|
for (const key in updates) {
|
|
|
|
const value = (updates as any)[key];
|
|
|
|
if (typeof value !== "undefined") {
|
2020-05-28 09:50:56 +02:00
|
|
|
if (
|
|
|
|
(element as any)[key] === value &&
|
2021-10-21 22:05:48 +02:00
|
|
|
// if object, always update because its attrs could have changed
|
|
|
|
// (except for specific keys we handle below)
|
|
|
|
(typeof value !== "object" ||
|
|
|
|
value === null ||
|
|
|
|
key === "groupIds" ||
|
|
|
|
key === "scale")
|
2020-05-28 09:50:56 +02:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-01 11:35:44 +02:00
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
if (key === "scale") {
|
|
|
|
const prevScale = (element as any)[key];
|
|
|
|
const nextScale = value;
|
|
|
|
if (prevScale[0] === nextScale[0] && prevScale[1] === nextScale[1]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (key === "points") {
|
2020-06-01 11:35:44 +02:00
|
|
|
const prevPoints = (element as any)[key];
|
|
|
|
const nextPoints = value;
|
|
|
|
if (prevPoints.length === nextPoints.length) {
|
|
|
|
let didChangePoints = false;
|
2020-11-29 18:32:51 +02:00
|
|
|
let index = prevPoints.length;
|
|
|
|
while (--index) {
|
|
|
|
const prevPoint: Point = prevPoints[index];
|
|
|
|
const nextPoint: Point = nextPoints[index];
|
2020-06-01 11:35:44 +02:00
|
|
|
if (
|
|
|
|
prevPoint[0] !== nextPoint[0] ||
|
|
|
|
prevPoint[1] !== nextPoint[1]
|
|
|
|
) {
|
|
|
|
didChangePoints = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!didChangePoints) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 09:50:56 +02:00
|
|
|
(element as any)[key] = value;
|
|
|
|
didChange = true;
|
2020-03-14 21:48:51 -07:00
|
|
|
}
|
2020-03-14 20:46:57 -07:00
|
|
|
}
|
2020-05-28 09:50:56 +02:00
|
|
|
if (!didChange) {
|
2021-10-21 22:05:48 +02:00
|
|
|
return element;
|
2020-05-28 09:50:56 +02:00
|
|
|
}
|
|
|
|
|
2020-03-14 21:48:51 -07:00
|
|
|
if (
|
|
|
|
typeof updates.height !== "undefined" ||
|
|
|
|
typeof updates.width !== "undefined" ||
|
2021-10-21 22:05:48 +02:00
|
|
|
typeof fileId != "undefined" ||
|
2020-03-17 20:55:40 +01:00
|
|
|
typeof points !== "undefined"
|
2020-03-14 21:48:51 -07:00
|
|
|
) {
|
|
|
|
invalidateShapeForElement(element);
|
|
|
|
}
|
2020-03-09 22:34:50 -07:00
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
element.version++;
|
2020-03-23 16:38:41 -07:00
|
|
|
element.versionNonce = randomInteger();
|
2021-11-24 18:38:33 +01:00
|
|
|
element.updated = getUpdatedTimestamp();
|
2021-10-21 22:05:48 +02:00
|
|
|
|
|
|
|
if (informMutation) {
|
|
|
|
Scene.getScene(element)?.informMutation();
|
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-03-10 20:11:02 -07:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const newElementWith = <TElement extends ExcalidrawElement>(
|
2020-03-14 21:48:51 -07:00
|
|
|
element: TElement,
|
|
|
|
updates: ElementUpdate<TElement>,
|
2021-03-20 13:29:53 +01:00
|
|
|
): TElement => {
|
|
|
|
let didChange = false;
|
|
|
|
for (const key in updates) {
|
|
|
|
const value = (updates as any)[key];
|
|
|
|
if (typeof value !== "undefined") {
|
|
|
|
if (
|
|
|
|
(element as any)[key] === value &&
|
2021-10-21 22:05:48 +02:00
|
|
|
// if object, always update because its attrs could have changed
|
|
|
|
(typeof value !== "object" || value === null)
|
2021-03-20 13:29:53 +01:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
didChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!didChange) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...element,
|
|
|
|
...updates,
|
2021-11-24 18:38:33 +01:00
|
|
|
updated: getUpdatedTimestamp(),
|
2021-03-20 13:29:53 +01:00
|
|
|
version: element.version + 1,
|
|
|
|
versionNonce: randomInteger(),
|
|
|
|
};
|
|
|
|
};
|
2021-04-25 14:09:38 +02:00
|
|
|
|
|
|
|
/**
|
2021-11-24 18:38:33 +01:00
|
|
|
* Mutates element, bumping `version`, `versionNonce`, and `updated`.
|
2021-04-25 14:09:38 +02:00
|
|
|
*
|
|
|
|
* NOTE: does not trigger re-render.
|
|
|
|
*/
|
2021-07-04 22:23:35 +02:00
|
|
|
export const bumpVersion = (
|
|
|
|
element: Mutable<ExcalidrawElement>,
|
|
|
|
version?: ExcalidrawElement["version"],
|
|
|
|
) => {
|
|
|
|
element.version = (version ?? element.version) + 1;
|
2021-04-25 14:09:38 +02:00
|
|
|
element.versionNonce = randomInteger();
|
2021-11-24 18:38:33 +01:00
|
|
|
element.updated = getUpdatedTimestamp();
|
2021-04-25 14:09:38 +02:00
|
|
|
return element;
|
|
|
|
};
|