2020-03-10 20:11:02 -07:00
|
|
|
import { ExcalidrawElement, ExcalidrawTextElement } from "./types";
|
2020-03-14 20:46:57 -07:00
|
|
|
import { randomSeed } from "roughjs/bin/math";
|
2020-03-10 20:11:02 -07:00
|
|
|
|
|
|
|
type ElementUpdate<TElement extends ExcalidrawElement> = Omit<
|
|
|
|
Partial<TElement>,
|
|
|
|
"id" | "seed"
|
|
|
|
>;
|
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
|
|
|
|
// the same drawing.
|
2020-03-09 22:34:50 -07:00
|
|
|
export function mutateElement(
|
2020-03-10 20:11:02 -07:00
|
|
|
element: ExcalidrawElement,
|
2020-03-14 20:46:57 -07:00
|
|
|
updates?: ElementUpdate<ExcalidrawElement>,
|
2020-03-10 20:11:02 -07:00
|
|
|
) {
|
2020-03-14 20:46:57 -07:00
|
|
|
if (updates) {
|
|
|
|
Object.assign(element, updates);
|
|
|
|
}
|
2020-03-10 20:11:02 -07:00
|
|
|
(element as any).version++;
|
2020-03-14 20:46:57 -07:00
|
|
|
(element as any).versionNonce = randomSeed();
|
2020-03-10 20:11:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function newElementWith(
|
|
|
|
element: ExcalidrawElement,
|
|
|
|
updates: ElementUpdate<ExcalidrawElement>,
|
|
|
|
): ExcalidrawElement {
|
2020-03-14 20:46:57 -07:00
|
|
|
return {
|
|
|
|
...element,
|
|
|
|
...updates,
|
|
|
|
version: element.version + 1,
|
|
|
|
versionNonce: randomSeed(),
|
|
|
|
};
|
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
|
|
|
|
// the same document.
|
2020-03-09 22:34:50 -07:00
|
|
|
export function mutateTextElement(
|
2020-03-10 20:11:02 -07:00
|
|
|
element: ExcalidrawTextElement,
|
|
|
|
updates: ElementUpdate<ExcalidrawTextElement>,
|
2020-03-09 22:34:50 -07:00
|
|
|
): void {
|
2020-03-10 20:11:02 -07:00
|
|
|
Object.assign(element, updates);
|
|
|
|
(element as any).version++;
|
2020-03-14 20:46:57 -07:00
|
|
|
(element as any).versionNonce = randomSeed();
|
2020-03-10 20:11:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function newTextElementWith(
|
|
|
|
element: ExcalidrawTextElement,
|
|
|
|
updates: ElementUpdate<ExcalidrawTextElement>,
|
|
|
|
): ExcalidrawTextElement {
|
2020-03-14 20:46:57 -07:00
|
|
|
return {
|
|
|
|
...element,
|
|
|
|
...updates,
|
|
|
|
version: element.version + 1,
|
|
|
|
versionNonce: randomSeed(),
|
|
|
|
};
|
2020-03-09 22:34:50 -07:00
|
|
|
}
|