Skip to content

Commit

Permalink
feat: add element.updated (#4070)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle committed Nov 24, 2021
1 parent dac970c commit f9d2d53
Show file tree
Hide file tree
Showing 14 changed files with 500 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/data/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { getDefaultAppState } from "../appState";
import { LinearElementEditor } from "../element/linearElementEditor";
import { bumpVersion } from "../element/mutateElement";
import { getUpdatedTimestamp } from "../utils";

type RestoredAppState = Omit<
AppState,
Expand Down Expand Up @@ -101,6 +102,7 @@ const restoreElementWithProperties = <
element.strokeSharpness ??
(isLinearElementType(element.type) ? "round" : "sharp"),
boundElementIds: element.boundElementIds ?? [],
updated: element.updated ?? getUpdatedTimestamp(),
};

return {
Expand Down
6 changes: 5 additions & 1 deletion src/element/mutateElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Scene from "../scene/Scene";
import { getSizeFromPoints } from "../points";
import { randomInteger } from "../random";
import { Point } from "../types";
import { getUpdatedTimestamp } from "../utils";

type ElementUpdate<TElement extends ExcalidrawElement> = Omit<
Partial<TElement>,
Expand Down Expand Up @@ -92,6 +93,7 @@ export const mutateElement = <TElement extends Mutable<ExcalidrawElement>>(

element.version++;
element.versionNonce = randomInteger();
element.updated = getUpdatedTimestamp();

if (informMutation) {
Scene.getScene(element)?.informMutation();
Expand Down Expand Up @@ -126,13 +128,14 @@ export const newElementWith = <TElement extends ExcalidrawElement>(
return {
...element,
...updates,
updated: getUpdatedTimestamp(),
version: element.version + 1,
versionNonce: randomInteger(),
};
};

/**
* Mutates element and updates `version` & `versionNonce`.
* Mutates element, bumping `version`, `versionNonce`, and `updated`.
*
* NOTE: does not trigger re-render.
*/
Expand All @@ -142,5 +145,6 @@ export const bumpVersion = (
) => {
element.version = (version ?? element.version) + 1;
element.versionNonce = randomInteger();
element.updated = getUpdatedTimestamp();
return element;
};
6 changes: 4 additions & 2 deletions src/element/newElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ExcalidrawFreeDrawElement,
FontFamilyValues,
} from "../element/types";
import { measureText, getFontString } from "../utils";
import { measureText, getFontString, getUpdatedTimestamp } from "../utils";
import { randomInteger, randomId } from "../random";
import { newElementWith } from "./mutateElement";
import { getNewGroupIdsForDuplication } from "../groups";
Expand All @@ -22,7 +22,7 @@ import { adjustXYWithRotation } from "../math";
import { getResizedElementAbsoluteCoords } from "./bounds";

type ElementConstructorOpts = MarkOptional<
Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted">,
Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted" | "updated">,
| "width"
| "height"
| "angle"
Expand Down Expand Up @@ -75,6 +75,7 @@ const _newElementBase = <T extends ExcalidrawElement>(
versionNonce: rest.versionNonce ?? 0,
isDeleted: false as false,
boundElementIds,
updated: getUpdatedTimestamp(),
});

export const newElement = (
Expand Down Expand Up @@ -337,6 +338,7 @@ export const duplicateElement = <TElement extends Mutable<ExcalidrawElement>>(
} else {
copy.id = randomId();
}
copy.updated = getUpdatedTimestamp();
copy.seed = randomInteger();
copy.groupIds = getNewGroupIdsForDuplication(
copy.groupIds,
Expand Down
2 changes: 2 additions & 0 deletions src/element/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type _ExcalidrawElementBase = Readonly<{
groupIds: readonly GroupId[];
/** Ids of (linear) elements that are bound to this element. */
boundElementIds: readonly ExcalidrawLinearElement["id"][] | null;
/** epoch (ms) timestamp of last element update */
updated: number;
}>;

export type ExcalidrawSelectionElement = _ExcalidrawElementBase & {
Expand Down
Loading

1 comment on commit f9d2d53

@vercel
Copy link

@vercel vercel bot commented on f9d2d53 Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.