Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 638c544bd1 | |||
| 79b5bab273 | |||
| b2920f0baf | |||
| 4be8dd9879 | |||
| 801c936fd4 | |||
| d4836f9e25 | |||
| 75192db663 | |||
| ef9afb0d37 | |||
| a5769f96cd | |||
| 2c6f513aed | |||
| bfd4af2367 | |||
| 2177e1cfa0 | |||
| 5cd609cea7 | |||
| 226a6eef0a | |||
| 1caec99b29 | |||
| e18c1dd213 |
@@ -29,7 +29,7 @@
|
||||
<a href="https://docs.excalidraw.com/docs/introduction/contributing">
|
||||
<img alt="PRs welcome!" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" /></a>
|
||||
<a href="https://discord.gg/UexuTaE">
|
||||
<img alt="Chat on Discord" src="https://img.shields.io/discord/723672430744174682?color=738ad6&label=Chat%20on%20Discord&logo=discord&logoColor=ffffff&widge=false"/></a>
|
||||
<img alt="Chat on Discord" src="https://img.shields.io/discord/723672430744174682?color=738ad6&label=Chat%20on%20Discord&logo=discord&logoColor=ffffff&widget=false"/></a>
|
||||
<a href="https://deepwiki.com/excalidraw/excalidraw">
|
||||
<img alt="Ask DeepWiki" src="https://deepwiki.com/badge.svg" /></a>
|
||||
<a href="https://twitter.com/excalidraw">
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
# Transactions
|
||||
|
||||
Transactions let many live scene updates render immediately while still committing a single durable history entry.
|
||||
|
||||
Typical use case:
|
||||
|
||||
- AI streaming emits many `NEVER` updates for real-time feedback.
|
||||
- Those updates advance the live scene and store snapshot, but do not create undo history.
|
||||
- A transaction records the intended logical delta separately, then commits one synthetic durable increment at the end.
|
||||
|
||||
## Key Files
|
||||
|
||||
### `packages/excalidraw/transaction/`
|
||||
|
||||
- `transaction.ts`
|
||||
- `Transaction` is the per-tx instance.
|
||||
- `updateScene()` applies live updates with `captureUpdate: NEVER`.
|
||||
- `updateElements()` is a typed partial-patch helper over `updateScene()`.
|
||||
- `commit()` builds synthetic snapshots and commits one durable entry.
|
||||
- `cancel()` ends the tx without committing history.
|
||||
- `ledger.ts`
|
||||
- `TransactionLedger` keeps per-element baseline, target, and touched props across tx steps.
|
||||
- `buildSyntheticSnapshots()` reconciles tx intent with the live scene using `live-wins-per-prop`.
|
||||
- `undoOverridePlanner.ts`
|
||||
- Tracks tx intermediate values.
|
||||
- Produces undo override candidates for regular user history entries that were recorded while a tx was active.
|
||||
- `manager.ts`
|
||||
- Owns tx lifecycle state and active-tx priority.
|
||||
- Injects tx undo markers into durable deltas at record time.
|
||||
- Resolves effective history deltas at undo/redo time.
|
||||
- `diff.ts` / `types.ts`
|
||||
- Shared diff helpers and transaction-specific types.
|
||||
|
||||
### `packages/element/src/store.ts`
|
||||
|
||||
- `store.commitSyntheticIncrement()`
|
||||
- Builds `StoreSnapshot -> StoreChange -> StoreDelta` from logical before/after snapshots.
|
||||
- Commits the synthetic durable increment immediately through an isolated path.
|
||||
- Does not flush unrelated pending micro actions.
|
||||
- `StoreDelta.markers`
|
||||
- Carries extra history markers such as `txUndoOverrides`.
|
||||
|
||||
### `packages/excalidraw/history.ts`
|
||||
|
||||
- `onBeforeRecord()`
|
||||
- Lets the transaction manager attach markers before a durable delta becomes a history entry.
|
||||
- `setEffectiveDeltaResolver()`
|
||||
- Lets the transaction manager rewrite a history delta at undo/redo time before it is applied.
|
||||
|
||||
## Flow
|
||||
|
||||
1. Caller creates a transaction.
|
||||
2. `tx.updateScene()` applies live updates with `NEVER`.
|
||||
3. The ledger records net element changes; appState intent is accumulated separately.
|
||||
4. Regular user durable edits that happen while the tx is active may receive tx undo markers.
|
||||
5. `tx.commit()` asks the ledger for logical before/after snapshots and calls `store.commitSyntheticIncrement()`.
|
||||
6. Undo/redo later uses the manager's effective-delta resolver to decide whether ended tx markers should patch the baseline of a regular history entry.
|
||||
|
||||
Result:
|
||||
|
||||
- live rendering stays immediate;
|
||||
- one tx still produces one durable history entry;
|
||||
- interleaved regular edits can undo back to the correct pre-tx baseline.
|
||||
|
||||
## Undo Examples
|
||||
|
||||
### Interleaved Regular Edit
|
||||
|
||||
1. Baseline: `rect = (x: 0, stroke: black)`
|
||||
2. Tx updates it live to `(x: 200, stroke: red)` with `NEVER`
|
||||
3. User makes a regular durable edit: `(x: 200, stroke: purple)`
|
||||
4. Tx commits; live stays purple
|
||||
|
||||
The user history entry was recorded while the tx was active, so its local baseline is `red`.
|
||||
If undo later runs after the tx has ended, restoring `red` is wrong: the correct pre-tx baseline is `black`.
|
||||
|
||||
To fix this:
|
||||
|
||||
- record-time attaches a tx undo marker to the user history entry
|
||||
- perform-time checks whether the tx has already ended
|
||||
- if ended, the effective delta is patched so undo restores `black` instead of `red`
|
||||
|
||||
### Active Tx vs Ended Tx
|
||||
|
||||
Using the same example:
|
||||
|
||||
- if the user undoes while the tx is still active, the marker stays inactive and undo restores the tx intermediate value (`red`)
|
||||
- if the user undoes after the tx has ended, the marker becomes active and undo restores the pre-tx baseline (`black`)
|
||||
|
||||
So markers are recorded early, but applied conditionally.
|
||||
|
||||
## AppState
|
||||
|
||||
Elements use ledger snapshots. AppState does not.
|
||||
|
||||
The transaction instead keeps:
|
||||
|
||||
- `initialAppState`: observed appState at tx start
|
||||
- `accumulatedAppState`: merged caller intent from `tx.updateScene({ appState })`
|
||||
|
||||
At commit, the transaction computes the final appState patch and passes it into `store.commitSyntheticIncrement()`.
|
||||
|
||||
## Example
|
||||
|
||||
```ts
|
||||
const tx = app.transactionManager.create();
|
||||
|
||||
tx.updateScene({ elements: updatedElementsA });
|
||||
tx.updateScene({ elements: updatedElementsB });
|
||||
tx.updateElements({
|
||||
elements: [
|
||||
{ id: "rect-1", type: "rectangle", updates: { strokeColor: "#f00" } },
|
||||
{ id: "rect-2", type: "rectangle", updates: { x: 120 } },
|
||||
],
|
||||
});
|
||||
|
||||
tx.commit();
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- `commit()` and `cancel()` are idempotent.
|
||||
- `updateScene()` and `updateElements()` throw after the tx has ended.
|
||||
- Conflict handling inside synthetic snapshots is fixed to `live-wins-per-prop`.
|
||||
- Multi-tx interleaving is supported through manager-owned lifecycle tracking and active-tx priority.
|
||||
@@ -97,8 +97,8 @@ const config = {
|
||||
href: "https://discord.gg/UexuTaE",
|
||||
},
|
||||
{
|
||||
label: "Twitter",
|
||||
href: "https://twitter.com/excalidraw",
|
||||
label: "𝕏",
|
||||
href: "https://x.com/excalidraw",
|
||||
},
|
||||
{
|
||||
label: "Linkedin",
|
||||
|
||||
@@ -405,9 +405,8 @@ export const ROUGHNESS = {
|
||||
|
||||
export const STROKE_WIDTH = {
|
||||
thin: 1,
|
||||
medium: 2,
|
||||
bold: 4,
|
||||
extraBold: 8,
|
||||
bold: 2,
|
||||
extraBold: 4,
|
||||
} as const;
|
||||
|
||||
export const DEFAULT_ELEMENT_PROPS: {
|
||||
@@ -430,8 +429,6 @@ export const DEFAULT_ELEMENT_PROPS: {
|
||||
locked: false,
|
||||
};
|
||||
|
||||
export const DEFAULT_FREE_DRAW_STROKE_SHAPE = "variable" as const;
|
||||
|
||||
export const LIBRARY_SIDEBAR_TAB = "library";
|
||||
export const CANVAS_SEARCH_TAB = "search";
|
||||
|
||||
|
||||
@@ -168,14 +168,12 @@ export class ElementBounds {
|
||||
),
|
||||
),
|
||||
);
|
||||
const padding =
|
||||
element.strokeShape === "fixed" ? element.strokeWidth / 2 : 0;
|
||||
|
||||
return [
|
||||
minX + element.x - padding,
|
||||
minY + element.y - padding,
|
||||
maxX + element.x + padding,
|
||||
maxY + element.y + padding,
|
||||
minX + element.x,
|
||||
minY + element.y,
|
||||
maxX + element.x,
|
||||
maxY + element.y,
|
||||
];
|
||||
} else if (isLinearElement(element)) {
|
||||
bounds = getLinearElementRotatedBounds(element, cx, cy, elementsMap);
|
||||
@@ -705,11 +703,10 @@ const getFreeDrawElementAbsoluteCoords = (
|
||||
element: ExcalidrawFreeDrawElement,
|
||||
): [number, number, number, number, number, number] => {
|
||||
const [minX, minY, maxX, maxY] = getBoundsFromPoints(element.points);
|
||||
const padding = element.strokeShape === "fixed" ? element.strokeWidth / 2 : 0;
|
||||
const x1 = minX + element.x - padding;
|
||||
const y1 = minY + element.y - padding;
|
||||
const x2 = maxX + element.x + padding;
|
||||
const y2 = maxY + element.y + padding;
|
||||
const x1 = minX + element.x;
|
||||
const y1 = minY + element.y;
|
||||
const x2 = maxX + element.x;
|
||||
const y2 = maxY + element.y;
|
||||
return [x1, y1, x2, y2, (x1 + x2) / 2, (y1 + y2) / 2];
|
||||
};
|
||||
|
||||
|
||||
@@ -27,7 +27,6 @@ import type {
|
||||
|
||||
import type { FrameNameBounds } from "@excalidraw/excalidraw/types";
|
||||
|
||||
import { isLoopFreeDrawElement } from "./freedraw";
|
||||
import { isPathALoop } from "./utils";
|
||||
import {
|
||||
doBoundsIntersect,
|
||||
@@ -94,7 +93,7 @@ export const shouldTestInside = (element: ExcalidrawElement) => {
|
||||
}
|
||||
|
||||
if (element.type === "freedraw") {
|
||||
return isDraggableFromInside && isLoopFreeDrawElement(element);
|
||||
return isDraggableFromInside && isPathALoop(element.points);
|
||||
}
|
||||
|
||||
return isDraggableFromInside || isImageElement(element);
|
||||
@@ -755,8 +754,8 @@ export const isPointInElement = (
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
if (
|
||||
(isLinearElement(element) && !isPathALoop(element.points)) ||
|
||||
(isFreeDrawElement(element) && !isLoopFreeDrawElement(element))
|
||||
(isLinearElement(element) || isFreeDrawElement(element)) &&
|
||||
!isPathALoop(element.points)
|
||||
) {
|
||||
// There isn't any "inside" for a non-looping path
|
||||
return false;
|
||||
|
||||
@@ -1,181 +0,0 @@
|
||||
import {
|
||||
pointDistance,
|
||||
vectorCross,
|
||||
vectorDot,
|
||||
vectorFromPoint,
|
||||
} from "@excalidraw/math";
|
||||
|
||||
import type { LocalPoint } from "@excalidraw/math";
|
||||
|
||||
import { isPathALoop } from "./utils";
|
||||
|
||||
import type { ExcalidrawFreeDrawElement } from "./types";
|
||||
|
||||
type ZoomValue = NonNullable<Parameters<typeof isPathALoop>[1]>;
|
||||
|
||||
type FixedFreeDrawSimplificationProfile = {
|
||||
minPointDistancePx: number;
|
||||
maxPointDistance: number;
|
||||
strokeDistanceFactor: number;
|
||||
collinearityFactor: number;
|
||||
minAlignment: number;
|
||||
zoomScaling: "sqrt" | "none";
|
||||
};
|
||||
|
||||
const FIXED_FREEDRAW_CAPTURE_PROFILE: FixedFreeDrawSimplificationProfile = {
|
||||
minPointDistancePx: 0.35,
|
||||
maxPointDistance: 0.85,
|
||||
strokeDistanceFactor: 0.08,
|
||||
collinearityFactor: 0.3,
|
||||
minAlignment: 0.985,
|
||||
zoomScaling: "sqrt",
|
||||
};
|
||||
|
||||
const hasSyntheticLoopClosure = (
|
||||
points: readonly LocalPoint[],
|
||||
): points is readonly [LocalPoint, LocalPoint, ...LocalPoint[]] => {
|
||||
if (points.length < 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const firstPoint = points[0];
|
||||
const lastPoint = points[points.length - 1];
|
||||
|
||||
return firstPoint[0] === lastPoint[0] && firstPoint[1] === lastPoint[1];
|
||||
};
|
||||
|
||||
const stripSyntheticLoopClosure = (points: readonly LocalPoint[]) =>
|
||||
hasSyntheticLoopClosure(points) ? points.slice(0, -1) : points;
|
||||
|
||||
export const isLoopFreeDrawElement = (
|
||||
element: ExcalidrawFreeDrawElement,
|
||||
zoomValue: ZoomValue = 1 as ZoomValue,
|
||||
) => element.strokeShape !== "fixed" && isPathALoop(element.points, zoomValue);
|
||||
|
||||
export const getFixedFreeDrawPoints = (
|
||||
element: ExcalidrawFreeDrawElement,
|
||||
): readonly LocalPoint[] => stripSyntheticLoopClosure(element.points);
|
||||
|
||||
export const getFixedFreeDrawPointSamplingDistance = (
|
||||
strokeWidth: number,
|
||||
zoomValue: ZoomValue = 1 as ZoomValue,
|
||||
profile: FixedFreeDrawSimplificationProfile = FIXED_FREEDRAW_CAPTURE_PROFILE,
|
||||
) =>
|
||||
Math.min(
|
||||
Math.max(
|
||||
profile.minPointDistancePx /
|
||||
(profile.zoomScaling === "sqrt"
|
||||
? Math.max(1, Math.sqrt(zoomValue))
|
||||
: 1),
|
||||
strokeWidth * profile.strokeDistanceFactor,
|
||||
),
|
||||
profile.maxPointDistance,
|
||||
);
|
||||
|
||||
const isRedundantFixedFreeDrawPoint = (
|
||||
previousPoint: LocalPoint,
|
||||
currentPoint: LocalPoint,
|
||||
nextPoint: LocalPoint,
|
||||
strokeWidth: number,
|
||||
zoomValue: ZoomValue,
|
||||
profile: FixedFreeDrawSimplificationProfile,
|
||||
) => {
|
||||
const previousSegmentLength = pointDistance(previousPoint, currentPoint);
|
||||
const nextSegmentLength = pointDistance(currentPoint, nextPoint);
|
||||
|
||||
if (!previousSegmentLength || !nextSegmentLength) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const previousVector = vectorFromPoint(currentPoint, previousPoint);
|
||||
const nextVector = vectorFromPoint(nextPoint, currentPoint);
|
||||
const alignment =
|
||||
vectorDot(previousVector, nextVector) /
|
||||
(previousSegmentLength * nextSegmentLength);
|
||||
|
||||
if (alignment < profile.minAlignment) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const chord = vectorFromPoint(nextPoint, previousPoint);
|
||||
const chordLength = pointDistance(previousPoint, nextPoint);
|
||||
|
||||
if (!chordLength) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const distanceToChord =
|
||||
Math.abs(vectorCross(vectorFromPoint(currentPoint, previousPoint), chord)) /
|
||||
chordLength;
|
||||
|
||||
return (
|
||||
distanceToChord <=
|
||||
getFixedFreeDrawPointSamplingDistance(strokeWidth, zoomValue, profile) *
|
||||
profile.collinearityFactor
|
||||
);
|
||||
};
|
||||
|
||||
export const getFixedFreeDrawPointAction = ({
|
||||
points,
|
||||
nextPoint,
|
||||
strokeWidth,
|
||||
zoomValue,
|
||||
isFinalPoint = false,
|
||||
profile,
|
||||
}: {
|
||||
points: readonly LocalPoint[];
|
||||
nextPoint: LocalPoint;
|
||||
strokeWidth: number;
|
||||
zoomValue: ZoomValue;
|
||||
isFinalPoint?: boolean;
|
||||
profile?: FixedFreeDrawSimplificationProfile;
|
||||
}) => {
|
||||
const simplificationProfile = profile ?? FIXED_FREEDRAW_CAPTURE_PROFILE;
|
||||
const lastPoint = points[points.length - 1];
|
||||
|
||||
if (!lastPoint) {
|
||||
return "append" as const;
|
||||
}
|
||||
|
||||
if (lastPoint[0] === nextPoint[0] && lastPoint[1] === nextPoint[1]) {
|
||||
return "discard" as const;
|
||||
}
|
||||
|
||||
const samplingDistance = getFixedFreeDrawPointSamplingDistance(
|
||||
strokeWidth,
|
||||
zoomValue,
|
||||
simplificationProfile,
|
||||
);
|
||||
|
||||
if (points.length === 1) {
|
||||
return !isFinalPoint &&
|
||||
pointDistance(lastPoint, nextPoint) < samplingDistance
|
||||
? ("discard" as const)
|
||||
: ("append" as const);
|
||||
}
|
||||
|
||||
const previousPoint = points[points.length - 2];
|
||||
|
||||
if (
|
||||
isRedundantFixedFreeDrawPoint(
|
||||
previousPoint,
|
||||
lastPoint,
|
||||
nextPoint,
|
||||
strokeWidth,
|
||||
zoomValue,
|
||||
simplificationProfile,
|
||||
)
|
||||
) {
|
||||
return "replace" as const;
|
||||
}
|
||||
|
||||
if (!isFinalPoint && pointDistance(lastPoint, nextPoint) < samplingDistance) {
|
||||
return "discard" as const;
|
||||
}
|
||||
|
||||
return "append" as const;
|
||||
};
|
||||
|
||||
export const getRenderableFixedFreeDrawPoints = (
|
||||
element: ExcalidrawFreeDrawElement,
|
||||
): readonly LocalPoint[] => getFixedFreeDrawPoints(element);
|
||||
@@ -69,7 +69,6 @@ export * from "./duplicate";
|
||||
export * from "./elbowArrow";
|
||||
export * from "./elementLink";
|
||||
export * from "./embeddable";
|
||||
export * from "./freedraw";
|
||||
export * from "./flowchart";
|
||||
export * from "./arrows/focus";
|
||||
export * from "./fractionalIndex";
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
DEFAULT_FREE_DRAW_STROKE_SHAPE,
|
||||
getSizeFromPoints,
|
||||
randomInteger,
|
||||
getUpdatedTimestamp,
|
||||
@@ -19,8 +18,6 @@ import type {
|
||||
ElementsMap,
|
||||
ExcalidrawElbowArrowElement,
|
||||
ExcalidrawElement,
|
||||
ExcalidrawFreeDrawElement,
|
||||
FreeDrawStrokeShape,
|
||||
NonDeletedSceneElementsMap,
|
||||
} from "./types";
|
||||
|
||||
@@ -180,30 +177,6 @@ export const newElementWith = <TElement extends ExcalidrawElement>(
|
||||
};
|
||||
};
|
||||
|
||||
export const newFreeDrawElementWithStrokeShape = <
|
||||
TElement extends ExcalidrawFreeDrawElement,
|
||||
>(
|
||||
element: TElement,
|
||||
strokeShape: FreeDrawStrokeShape,
|
||||
): TElement => {
|
||||
if (strokeShape === DEFAULT_FREE_DRAW_STROKE_SHAPE) {
|
||||
if (!("strokeShape" in element)) {
|
||||
return element;
|
||||
}
|
||||
const nextElement = newElementWith(
|
||||
element,
|
||||
{} as ElementUpdate<TElement>,
|
||||
true,
|
||||
);
|
||||
delete (nextElement as Mutable<Partial<TElement>>).strokeShape;
|
||||
return nextElement;
|
||||
}
|
||||
|
||||
return newElementWith(element, {
|
||||
strokeShape,
|
||||
} as ElementUpdate<TElement>);
|
||||
};
|
||||
|
||||
/**
|
||||
* Mutates element, bumping `version`, `versionNonce`, and `updated`.
|
||||
*
|
||||
|
||||
@@ -445,7 +445,6 @@ export const newFreeDrawElement = (
|
||||
points?: ExcalidrawFreeDrawElement["points"];
|
||||
simulatePressure: boolean;
|
||||
pressures?: ExcalidrawFreeDrawElement["pressures"];
|
||||
strokeShape?: ExcalidrawFreeDrawElement["strokeShape"];
|
||||
} & ElementConstructorOpts,
|
||||
): NonDeleted<ExcalidrawFreeDrawElement> => {
|
||||
return {
|
||||
@@ -453,7 +452,6 @@ export const newFreeDrawElement = (
|
||||
points: opts.points || [],
|
||||
pressures: opts.pressures || [],
|
||||
simulatePressure: opts.simulatePressure,
|
||||
...(opts.strokeShape === "fixed" ? { strokeShape: opts.strokeShape } : {}),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -417,27 +417,16 @@ const drawElementOnCanvas = (
|
||||
case "freedraw": {
|
||||
// Draw directly to canvas
|
||||
context.save();
|
||||
context.lineJoin = "round";
|
||||
context.lineCap = "round";
|
||||
|
||||
const shapes = ShapeCache.generateElementShape(element, renderConfig);
|
||||
const isFixedStroke = element.strokeShape === "fixed";
|
||||
|
||||
for (const shape of shapes) {
|
||||
if (typeof shape === "string") {
|
||||
const strokeColor =
|
||||
context.fillStyle =
|
||||
renderConfig.theme === THEME.DARK
|
||||
? applyDarkModeFilter(element.strokeColor)
|
||||
: element.strokeColor;
|
||||
|
||||
if (isFixedStroke) {
|
||||
context.strokeStyle = strokeColor;
|
||||
context.lineWidth = element.strokeWidth;
|
||||
context.stroke(new Path2D(shape));
|
||||
} else {
|
||||
context.fillStyle = strokeColor;
|
||||
context.fill(new Path2D(shape));
|
||||
}
|
||||
context.fill(new Path2D(shape));
|
||||
} else {
|
||||
rc.draw(shape);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { simplify } from "points-on-curve";
|
||||
import { DEFAULT_FREE_DRAW_STROKE_SHAPE } from "@excalidraw/common";
|
||||
import { getStroke } from "perfect-freehand";
|
||||
|
||||
import {
|
||||
@@ -14,7 +13,6 @@ import {
|
||||
import {
|
||||
pointFrom,
|
||||
pointDistance,
|
||||
round,
|
||||
type LocalPoint,
|
||||
pointRotateRads,
|
||||
} from "@excalidraw/math";
|
||||
@@ -54,10 +52,6 @@ import {
|
||||
isIframeLikeElement,
|
||||
isLinearElement,
|
||||
} from "./typeChecks";
|
||||
import {
|
||||
getRenderableFixedFreeDrawPoints,
|
||||
isLoopFreeDrawElement,
|
||||
} from "./freedraw";
|
||||
import { getCornerRadius, isPathALoop } from "./utils";
|
||||
import { headingForPointIsHorizontal } from "./heading";
|
||||
|
||||
@@ -250,12 +244,7 @@ export const generateRoughOptions = (
|
||||
}
|
||||
case "line":
|
||||
case "freedraw": {
|
||||
const isLoop =
|
||||
element.type === "freedraw"
|
||||
? isLoopFreeDrawElement(element)
|
||||
: isPathALoop(element.points);
|
||||
|
||||
if (isLoop) {
|
||||
if (isPathALoop(element.points)) {
|
||||
options.fillStyle = element.fillStyle;
|
||||
options.fill =
|
||||
element.backgroundColor === "transparent"
|
||||
@@ -977,7 +966,7 @@ const _generateElementShape = (
|
||||
const shapes: ElementShapes[typeof element.type] = [];
|
||||
|
||||
// (1) background fill (rc shape), optional
|
||||
if (isLoopFreeDrawElement(element)) {
|
||||
if (isPathALoop(element.points)) {
|
||||
// generate rough polygon to fill freedraw shape
|
||||
const simplifiedPoints = simplify(
|
||||
element.points as Mutable<LocalPoint[]>,
|
||||
@@ -1184,13 +1173,6 @@ export const toggleLinePolygonState = (
|
||||
|
||||
// NOTE not cached (-> for SVG export)
|
||||
const getFreeDrawSvgPath = (element: ExcalidrawFreeDrawElement) => {
|
||||
if (element.strokeShape === "fixed") {
|
||||
return getSvgPathFromFixedFreeDrawPoints(
|
||||
getRenderableFixedFreeDrawPoints(element),
|
||||
element.strokeWidth,
|
||||
) as SVGPathString;
|
||||
}
|
||||
|
||||
return getSvgPathFromStroke(
|
||||
getFreedrawOutlinePoints(element),
|
||||
) as SVGPathString;
|
||||
@@ -1199,243 +1181,34 @@ const getFreeDrawSvgPath = (element: ExcalidrawFreeDrawElement) => {
|
||||
export const getFreedrawOutlinePoints = (
|
||||
element: ExcalidrawFreeDrawElement,
|
||||
) => {
|
||||
const strokeShape = element.strokeShape ?? DEFAULT_FREE_DRAW_STROKE_SHAPE;
|
||||
const isFixedStroke = strokeShape === "fixed";
|
||||
// If input points are empty (should they ever be?) return a dot
|
||||
const inputPoints = isFixedStroke
|
||||
? element.points.length
|
||||
? element.points
|
||||
: [[0, 0]]
|
||||
: element.simulatePressure
|
||||
const inputPoints = element.simulatePressure
|
||||
? element.points
|
||||
: element.points.length
|
||||
? element.points.map(([x, y], i) => [x, y, element.pressures[i]])
|
||||
: [[0, 0, 0.5]];
|
||||
|
||||
return getStroke(inputPoints as number[][], {
|
||||
simulatePressure: isFixedStroke ? false : element.simulatePressure,
|
||||
size: isFixedStroke ? element.strokeWidth : element.strokeWidth * 4.25,
|
||||
thinning: isFixedStroke ? 0 : 0.6,
|
||||
simulatePressure: element.simulatePressure,
|
||||
size: element.strokeWidth * 4.25,
|
||||
thinning: 0.6,
|
||||
smoothing: 0.5,
|
||||
streamline: 0.5,
|
||||
easing: isFixedStroke ? (t) => t : (t) => Math.sin((t * Math.PI) / 2), // https://easings.net/#easeOutSine
|
||||
easing: (t) => Math.sin((t * Math.PI) / 2), // https://easings.net/#easeOutSine
|
||||
last: true,
|
||||
}) as [number, number][];
|
||||
};
|
||||
|
||||
const med = (A: readonly number[], B: readonly number[]) => {
|
||||
const med = (A: number[], B: number[]) => {
|
||||
return [(A[0] + B[0]) / 2, (A[1] + B[1]) / 2];
|
||||
};
|
||||
|
||||
const roundPoint = (point: readonly number[]) =>
|
||||
`${round(point[0], 2)},${round(point[1], 2)} `;
|
||||
|
||||
const averagePoint = (A: readonly number[], B: readonly number[]) =>
|
||||
`${round((A[0] + B[0]) / 2, 2)},${round((A[1] + B[1]) / 2, 2)} `;
|
||||
|
||||
const getReadonlyPointDistance = (
|
||||
pointA: readonly number[],
|
||||
pointB: readonly number[],
|
||||
) => Math.hypot(pointA[0] - pointB[0], pointA[1] - pointB[1]);
|
||||
|
||||
// Trim SVG path data so number are each two decimal points. This
|
||||
// improves SVG exports, and prevents rendering errors on points
|
||||
// with long decimals.
|
||||
const TO_FIXED_PRECISION = /(\s?[A-Z]?,?-?[0-9]*\.[0-9]{0,2})(([0-9]|e|-)*)/g;
|
||||
|
||||
const getSvgPathFromPoints = (
|
||||
points: ReadonlyArray<readonly number[]>,
|
||||
closed = false,
|
||||
): string => {
|
||||
const len = points.length;
|
||||
|
||||
if (len < 2) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const path = points
|
||||
.slice(1)
|
||||
.map((point) => `L${roundPoint(point)}`)
|
||||
.join("");
|
||||
|
||||
return `M${roundPoint(points[0])}${path}${closed ? "Z" : ""}`;
|
||||
};
|
||||
|
||||
const FIXED_FREEDRAW_MIN_SMOOTH_ALIGNMENT = 0.6;
|
||||
const FIXED_FREEDRAW_MIN_SMOOTH_SEGMENT_LENGTH = 0.2;
|
||||
const FIXED_FREEDRAW_MIN_CORNER_ALIGNMENT = -0.25;
|
||||
const FIXED_FREEDRAW_MIN_CORNER_ROUNDING = 0.75;
|
||||
const FIXED_FREEDRAW_MAX_CORNER_ROUNDING = 6;
|
||||
const FIXED_FREEDRAW_CORNER_ROUNDING_FACTOR = 0.35;
|
||||
const FIXED_FREEDRAW_CORNER_ROUNDING_WIDTH_FACTOR = 1.5;
|
||||
const FIXED_FREEDRAW_MIN_TERMINAL_STUB = 0.75;
|
||||
const FIXED_FREEDRAW_MAX_TERMINAL_STUB = 2.5;
|
||||
const FIXED_FREEDRAW_TERMINAL_STUB_WIDTH_FACTOR = 1.5;
|
||||
|
||||
const shouldSmoothFixedFreeDrawPoint = (
|
||||
previousPoint: readonly number[],
|
||||
currentPoint: readonly number[],
|
||||
nextPoint: readonly number[],
|
||||
) => {
|
||||
const previousDeltaX = currentPoint[0] - previousPoint[0];
|
||||
const previousDeltaY = currentPoint[1] - previousPoint[1];
|
||||
const nextDeltaX = nextPoint[0] - currentPoint[0];
|
||||
const nextDeltaY = nextPoint[1] - currentPoint[1];
|
||||
const previousSegmentLength = Math.hypot(previousDeltaX, previousDeltaY);
|
||||
const nextSegmentLength = Math.hypot(nextDeltaX, nextDeltaY);
|
||||
|
||||
if (
|
||||
previousSegmentLength < FIXED_FREEDRAW_MIN_SMOOTH_SEGMENT_LENGTH ||
|
||||
nextSegmentLength < FIXED_FREEDRAW_MIN_SMOOTH_SEGMENT_LENGTH
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const alignment =
|
||||
(previousDeltaX * nextDeltaX + previousDeltaY * nextDeltaY) /
|
||||
(previousSegmentLength * nextSegmentLength);
|
||||
|
||||
return alignment >= FIXED_FREEDRAW_MIN_SMOOTH_ALIGNMENT;
|
||||
};
|
||||
|
||||
const getFixedFreeDrawRoundedCorner = (
|
||||
previousPoint: readonly number[],
|
||||
currentPoint: readonly number[],
|
||||
nextPoint: readonly number[],
|
||||
strokeWidth: number,
|
||||
) => {
|
||||
const previousDeltaX = currentPoint[0] - previousPoint[0];
|
||||
const previousDeltaY = currentPoint[1] - previousPoint[1];
|
||||
const nextDeltaX = nextPoint[0] - currentPoint[0];
|
||||
const nextDeltaY = nextPoint[1] - currentPoint[1];
|
||||
const previousSegmentLength = Math.hypot(previousDeltaX, previousDeltaY);
|
||||
const nextSegmentLength = Math.hypot(nextDeltaX, nextDeltaY);
|
||||
|
||||
if (!previousSegmentLength || !nextSegmentLength) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const alignment =
|
||||
(previousDeltaX * nextDeltaX + previousDeltaY * nextDeltaY) /
|
||||
(previousSegmentLength * nextSegmentLength);
|
||||
|
||||
if (
|
||||
alignment >= FIXED_FREEDRAW_MIN_SMOOTH_ALIGNMENT ||
|
||||
alignment <= FIXED_FREEDRAW_MIN_CORNER_ALIGNMENT
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cornerRounding = Math.min(
|
||||
Math.max(
|
||||
strokeWidth * FIXED_FREEDRAW_CORNER_ROUNDING_WIDTH_FACTOR,
|
||||
FIXED_FREEDRAW_MIN_CORNER_ROUNDING,
|
||||
),
|
||||
previousSegmentLength * FIXED_FREEDRAW_CORNER_ROUNDING_FACTOR,
|
||||
nextSegmentLength * FIXED_FREEDRAW_CORNER_ROUNDING_FACTOR,
|
||||
FIXED_FREEDRAW_MAX_CORNER_ROUNDING,
|
||||
);
|
||||
|
||||
if (!cornerRounding) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
entryPoint: [
|
||||
currentPoint[0] - (previousDeltaX / previousSegmentLength) * cornerRounding,
|
||||
currentPoint[1] - (previousDeltaY / previousSegmentLength) * cornerRounding,
|
||||
] as const,
|
||||
exitPoint: [
|
||||
currentPoint[0] + (nextDeltaX / nextSegmentLength) * cornerRounding,
|
||||
currentPoint[1] + (nextDeltaY / nextSegmentLength) * cornerRounding,
|
||||
] as const,
|
||||
};
|
||||
};
|
||||
|
||||
const getFixedFreeDrawTerminalStubThreshold = (strokeWidth: number) =>
|
||||
Math.min(
|
||||
Math.max(
|
||||
strokeWidth * FIXED_FREEDRAW_TERMINAL_STUB_WIDTH_FACTOR,
|
||||
FIXED_FREEDRAW_MIN_TERMINAL_STUB,
|
||||
),
|
||||
FIXED_FREEDRAW_MAX_TERMINAL_STUB,
|
||||
);
|
||||
|
||||
const getSvgPathFromFixedFreeDrawPoints = (
|
||||
points: ReadonlyArray<readonly number[]>,
|
||||
strokeWidth: number,
|
||||
): string => {
|
||||
const len = points.length;
|
||||
|
||||
if (len < 2) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (len === 2) {
|
||||
return `M${roundPoint(points[0])}L${roundPoint(points[1])}`;
|
||||
}
|
||||
|
||||
let path = `M${roundPoint(points[0])}`;
|
||||
const lastPoint = points[len - 1];
|
||||
let endsAtLastPoint = false;
|
||||
|
||||
for (let index = 1; index < len - 1; index++) {
|
||||
const previousPoint = points[index - 1];
|
||||
const currentPoint = points[index];
|
||||
const nextPoint = points[index + 1];
|
||||
const isLastCurveSegment = index === len - 2;
|
||||
const terminalStubThreshold = getFixedFreeDrawTerminalStubThreshold(
|
||||
strokeWidth,
|
||||
);
|
||||
const shouldSmooth = shouldSmoothFixedFreeDrawPoint(
|
||||
previousPoint,
|
||||
currentPoint,
|
||||
nextPoint,
|
||||
);
|
||||
const roundedCorner = shouldSmooth
|
||||
? null
|
||||
: getFixedFreeDrawRoundedCorner(
|
||||
previousPoint,
|
||||
currentPoint,
|
||||
nextPoint,
|
||||
strokeWidth,
|
||||
);
|
||||
const shouldCollapseSmoothTerminalStub =
|
||||
isLastCurveSegment &&
|
||||
shouldSmooth &&
|
||||
getReadonlyPointDistance(currentPoint, nextPoint) / 2 <=
|
||||
terminalStubThreshold;
|
||||
const shouldCollapseRoundedTerminalStub =
|
||||
isLastCurveSegment &&
|
||||
!!roundedCorner &&
|
||||
getReadonlyPointDistance(roundedCorner.exitPoint, lastPoint) <=
|
||||
terminalStubThreshold;
|
||||
|
||||
path += shouldSmooth
|
||||
? `Q${roundPoint(currentPoint)}${
|
||||
shouldCollapseSmoothTerminalStub
|
||||
? roundPoint(lastPoint)
|
||||
: averagePoint(currentPoint, nextPoint)
|
||||
}`
|
||||
: roundedCorner
|
||||
? `L${roundPoint(roundedCorner.entryPoint)}Q${roundPoint(
|
||||
currentPoint,
|
||||
)}${
|
||||
shouldCollapseRoundedTerminalStub
|
||||
? roundPoint(lastPoint)
|
||||
: roundPoint(roundedCorner.exitPoint)
|
||||
}`
|
||||
: `L${roundPoint(currentPoint)}`;
|
||||
endsAtLastPoint =
|
||||
shouldCollapseSmoothTerminalStub || shouldCollapseRoundedTerminalStub;
|
||||
}
|
||||
|
||||
return endsAtLastPoint ? path : `${path}L${roundPoint(lastPoint)}`;
|
||||
};
|
||||
|
||||
const getSvgPathFromStroke = (
|
||||
points: ReadonlyArray<readonly number[]>,
|
||||
): string => {
|
||||
const getSvgPathFromStroke = (points: number[][]): string => {
|
||||
if (!points.length) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -111,6 +111,67 @@ export class Store {
|
||||
this.scheduleAction(CaptureUpdateAction.IMMEDIATELY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Commits a synthetic durable history entry without changing the live scene.
|
||||
*
|
||||
* Builds StoreSnapshot → StoreChange → StoreDelta from the provided
|
||||
* logical before/after element maps and optional appState patches, then
|
||||
* emits the resulting durable increment immediately through an isolated
|
||||
* path that does not flush pending micro actions.
|
||||
*
|
||||
* appState patches are merged on top of the current observed appState
|
||||
* baseline so only the provided keys participate in the synthetic diff.
|
||||
*/
|
||||
public commitSyntheticIncrement(params: {
|
||||
logicalBefore: {
|
||||
elements: SceneElementsMap;
|
||||
appState?: Partial<ObservedAppState>;
|
||||
};
|
||||
logicalAfter: {
|
||||
elements: SceneElementsMap;
|
||||
appState?: Partial<ObservedAppState>;
|
||||
};
|
||||
}): boolean {
|
||||
const { logicalBefore, logicalAfter } = params;
|
||||
const observedAppStateBaseline = this.snapshot.appState;
|
||||
const syntheticAppStateBefore = logicalBefore.appState
|
||||
? { ...observedAppStateBaseline, ...logicalBefore.appState }
|
||||
: observedAppStateBaseline;
|
||||
const syntheticAppStateAfter = logicalAfter.appState
|
||||
? { ...observedAppStateBaseline, ...logicalAfter.appState }
|
||||
: observedAppStateBaseline;
|
||||
const didAppStateChange = Delta.isRightDifferent(
|
||||
syntheticAppStateBefore,
|
||||
syntheticAppStateAfter,
|
||||
);
|
||||
const prevSnapshot = StoreSnapshot.create(
|
||||
logicalBefore.elements,
|
||||
syntheticAppStateBefore,
|
||||
{
|
||||
didElementsChange: true,
|
||||
didAppStateChange,
|
||||
},
|
||||
);
|
||||
const nextSnapshot = StoreSnapshot.create(
|
||||
logicalAfter.elements,
|
||||
syntheticAppStateAfter,
|
||||
{
|
||||
didElementsChange: true,
|
||||
didAppStateChange,
|
||||
},
|
||||
);
|
||||
const change = StoreChange.create(prevSnapshot, nextSnapshot);
|
||||
const delta = StoreDelta.calculate(prevSnapshot, nextSnapshot);
|
||||
|
||||
if (delta.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.emitIsolatedDurableIncrement(change, delta);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule special "micro" actions, to-be executed before the next commit, before it executes a scheduled "macro" action.
|
||||
*/
|
||||
@@ -206,6 +267,7 @@ export class Store {
|
||||
public clear(): void {
|
||||
this.snapshot = StoreSnapshot.empty();
|
||||
this.scheduledMacroActions = new Set();
|
||||
this.scheduledMicroActions = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,6 +307,24 @@ export class Store {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits a synthetic durable increment immediately without draining the
|
||||
* regular micro-action queue.
|
||||
*/
|
||||
private emitIsolatedDurableIncrement(change: StoreChange, delta: StoreDelta) {
|
||||
const nextSnapshot = this.applyChangeToSnapshot(change);
|
||||
|
||||
if (!nextSnapshot) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.emitDurableIncrement(nextSnapshot, change, delta);
|
||||
} finally {
|
||||
this.snapshot = nextSnapshot;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs change calculation and emits an ephemeral increment.
|
||||
*
|
||||
@@ -491,6 +571,89 @@ export class EphemeralIncrement extends StoreIncrement {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializable delta marker used to recover pre-tx undo baselines
|
||||
* without rewriting history stack entries.
|
||||
*/
|
||||
export type TxUndoOverride = {
|
||||
txId: string;
|
||||
elementId: string;
|
||||
prop: string;
|
||||
expectedInsertedValue: unknown;
|
||||
preTxBaselineValue: unknown;
|
||||
consumedKey: string;
|
||||
};
|
||||
|
||||
export type StoreDeltaMarkers = {
|
||||
txUndoOverrides?: TxUndoOverride[];
|
||||
};
|
||||
|
||||
const cloneTxUndoOverride = (override: TxUndoOverride): TxUndoOverride => ({
|
||||
...override,
|
||||
});
|
||||
|
||||
const cloneStoreDeltaMarkers = (
|
||||
markers: StoreDeltaMarkers | undefined,
|
||||
): StoreDeltaMarkers | undefined =>
|
||||
markers
|
||||
? {
|
||||
...markers,
|
||||
txUndoOverrides: markers.txUndoOverrides?.map(cloneTxUndoOverride),
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const pruneStoreDeltaMarkers = (delta: StoreDelta) => {
|
||||
const txUndoOverrides = delta.markers?.txUndoOverrides;
|
||||
if (!txUndoOverrides || txUndoOverrides.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedEntries = delta.elements.updated as Record<
|
||||
string,
|
||||
Delta<Record<string, unknown>>
|
||||
>;
|
||||
const nextTxUndoOverrides = txUndoOverrides.filter((override) => {
|
||||
const updatedEntry = updatedEntries[override.elementId];
|
||||
return (
|
||||
!!updatedEntry &&
|
||||
Object.prototype.hasOwnProperty.call(updatedEntry.inserted, override.prop)
|
||||
);
|
||||
});
|
||||
|
||||
if (nextTxUndoOverrides.length === txUndoOverrides.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (nextTxUndoOverrides.length === 0) {
|
||||
delta.markers = undefined;
|
||||
return;
|
||||
}
|
||||
|
||||
delta.markers = {
|
||||
...delta.markers,
|
||||
txUndoOverrides: nextTxUndoOverrides.map(cloneTxUndoOverride),
|
||||
};
|
||||
};
|
||||
|
||||
export const mergeStoreDeltaMarkers = (
|
||||
delta: StoreDelta,
|
||||
markers: StoreDeltaMarkers,
|
||||
) => {
|
||||
const txUndoOverrides = markers.txUndoOverrides ?? [];
|
||||
if (txUndoOverrides.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = delta.markers?.txUndoOverrides ?? [];
|
||||
delta.markers = {
|
||||
...delta.markers,
|
||||
txUndoOverrides: [
|
||||
...existing.map(cloneTxUndoOverride),
|
||||
...txUndoOverrides.map(cloneTxUndoOverride),
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Represents a captured delta by the Store.
|
||||
*/
|
||||
@@ -499,6 +662,7 @@ export class StoreDelta {
|
||||
public readonly id: string,
|
||||
public readonly elements: ElementsDelta,
|
||||
public readonly appState: AppStateDelta,
|
||||
public markers?: StoreDeltaMarkers,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -508,12 +672,16 @@ export class StoreDelta {
|
||||
elements: ElementsDelta,
|
||||
appState: AppStateDelta,
|
||||
opts: {
|
||||
id: string;
|
||||
} = {
|
||||
id: randomId(),
|
||||
},
|
||||
id?: string;
|
||||
markers?: StoreDeltaMarkers;
|
||||
} = {},
|
||||
) {
|
||||
return new this(opts.id, elements, appState);
|
||||
return new this(
|
||||
opts.id ?? randomId(),
|
||||
elements,
|
||||
appState,
|
||||
cloneStoreDeltaMarkers(opts.markers),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -538,11 +706,12 @@ export class StoreDelta {
|
||||
* Restore a store delta instance from a DTO.
|
||||
*/
|
||||
public static restore(storeDeltaDTO: DTO<StoreDelta>) {
|
||||
const { id, elements, appState } = storeDeltaDTO;
|
||||
const { id, elements, appState, markers } = storeDeltaDTO;
|
||||
return new this(
|
||||
id,
|
||||
ElementsDelta.restore(elements),
|
||||
AppStateDelta.restore(appState),
|
||||
cloneStoreDeltaMarkers(markers),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -553,11 +722,12 @@ export class StoreDelta {
|
||||
id,
|
||||
elements: { added, removed, updated },
|
||||
appState: { delta: appStateDelta },
|
||||
markers,
|
||||
}: DTO<StoreDelta>) {
|
||||
const elements = ElementsDelta.create(added, removed, updated);
|
||||
const appState = AppStateDelta.create(appStateDelta);
|
||||
|
||||
return new this(id, elements, appState);
|
||||
return new this(id, elements, appState, cloneStoreDeltaMarkers(markers));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -569,8 +739,10 @@ export class StoreDelta {
|
||||
for (const delta of deltas) {
|
||||
aggregatedDelta.elements.squash(delta.elements);
|
||||
aggregatedDelta.appState.squash(delta.appState);
|
||||
mergeStoreDeltaMarkers(aggregatedDelta, delta.markers ?? {});
|
||||
}
|
||||
|
||||
pruneStoreDeltaMarkers(aggregatedDelta);
|
||||
return aggregatedDelta;
|
||||
}
|
||||
|
||||
@@ -578,7 +750,12 @@ export class StoreDelta {
|
||||
* Inverse store delta, creates new instance of `StoreDelta`.
|
||||
*/
|
||||
public static inverse(delta: StoreDelta) {
|
||||
return this.create(delta.elements.inverse(), delta.appState.inverse());
|
||||
const inversed = this.create(
|
||||
delta.elements.inverse(),
|
||||
delta.appState.inverse(),
|
||||
{ markers: delta.markers },
|
||||
);
|
||||
return inversed;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -614,7 +791,7 @@ export class StoreDelta {
|
||||
nextElements: SceneElementsMap,
|
||||
modifierOptions?: "deleted" | "inserted",
|
||||
): StoreDelta {
|
||||
return this.create(
|
||||
const nextDelta = this.create(
|
||||
delta.elements.applyLatestChanges(
|
||||
prevElements,
|
||||
nextElements,
|
||||
@@ -623,8 +800,11 @@ export class StoreDelta {
|
||||
delta.appState,
|
||||
{
|
||||
id: delta.id,
|
||||
markers: delta.markers,
|
||||
},
|
||||
);
|
||||
pruneStoreDeltaMarkers(nextDelta);
|
||||
return nextDelta;
|
||||
}
|
||||
|
||||
public static empty() {
|
||||
|
||||
@@ -26,7 +26,6 @@ export type PointerType = "mouse" | "pen" | "touch";
|
||||
export type StrokeRoundness = "round" | "sharp";
|
||||
export type RoundnessType = ValueOf<typeof ROUNDNESS>;
|
||||
export type StrokeStyle = "solid" | "dashed" | "dotted";
|
||||
export type FreeDrawStrokeShape = "variable" | "fixed";
|
||||
export type TextAlign = typeof TEXT_ALIGN[keyof typeof TEXT_ALIGN];
|
||||
|
||||
type VerticalAlignKeys = keyof typeof VERTICAL_ALIGN;
|
||||
@@ -391,7 +390,6 @@ export type ExcalidrawFreeDrawElement = _ExcalidrawElementBase &
|
||||
points: readonly LocalPoint[];
|
||||
pressures: readonly number[];
|
||||
simulatePressure: boolean;
|
||||
strokeShape?: FreeDrawStrokeShape;
|
||||
}>;
|
||||
|
||||
export type FileId = string & { _brand: "FileId" };
|
||||
|
||||
@@ -6,11 +6,7 @@ import type { LocalPoint } from "@excalidraw/math";
|
||||
|
||||
import { getElementAbsoluteCoords, getElementBounds } from "../src/bounds";
|
||||
|
||||
import type {
|
||||
ExcalidrawElement,
|
||||
ExcalidrawFreeDrawElement,
|
||||
ExcalidrawLinearElement,
|
||||
} from "../src/types";
|
||||
import type { ExcalidrawElement, ExcalidrawLinearElement } from "../src/types";
|
||||
|
||||
const _ce = ({
|
||||
x,
|
||||
@@ -121,31 +117,6 @@ describe("getElementBounds", () => {
|
||||
expect(y2).toEqual(42.90569415042095);
|
||||
});
|
||||
|
||||
it("fixed freedraw", () => {
|
||||
const element = {
|
||||
..._ce({
|
||||
x: 40,
|
||||
y: 30,
|
||||
w: 10,
|
||||
h: 0,
|
||||
a: 0,
|
||||
t: "freedraw",
|
||||
}),
|
||||
strokeWidth: 8,
|
||||
points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(10, 0)],
|
||||
pressures: [],
|
||||
simulatePressure: true,
|
||||
strokeShape: "fixed",
|
||||
} as unknown as ExcalidrawFreeDrawElement;
|
||||
|
||||
const [x1, y1, x2, y2] = getElementBounds(element, arrayToMap([element]));
|
||||
|
||||
expect(x1).toEqual(36);
|
||||
expect(y1).toEqual(26);
|
||||
expect(x2).toEqual(54);
|
||||
expect(y2).toEqual(34);
|
||||
});
|
||||
|
||||
it("curved line", () => {
|
||||
const element = {
|
||||
..._ce({
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
import { pointFrom } from "@excalidraw/math";
|
||||
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
|
||||
import { vi } from "vitest";
|
||||
|
||||
vi.mock("perfect-freehand", () => ({
|
||||
getStroke: vi.fn(() => []),
|
||||
}));
|
||||
|
||||
import { getStroke } from "perfect-freehand";
|
||||
|
||||
import { getFreedrawOutlinePoints, ShapeCache } from "../src/shape";
|
||||
|
||||
describe("freedraw stroke shape", () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(getStroke).mockClear();
|
||||
ShapeCache.destroy();
|
||||
});
|
||||
|
||||
it("renders fixed strokes from the centerline path", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
points: [pointFrom(0, 0), pointFrom(10, 10), pointFrom(20, 15)],
|
||||
});
|
||||
|
||||
const shapes = ShapeCache.generateElementShape(element, null);
|
||||
|
||||
expect(shapes).toEqual([expect.any(String)]);
|
||||
expect(shapes[0]).not.toContain("Z");
|
||||
expect(vi.mocked(getStroke)).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rounds fixed stroke corners without flattening them", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
strokeWidth: 1,
|
||||
points: [pointFrom(0, 0), pointFrom(10, 0), pointFrom(10, 10)],
|
||||
});
|
||||
|
||||
const [path] = ShapeCache.generateElementShape(element, null);
|
||||
|
||||
expect(path).toBe("M0,0 L8.5,0 Q10,0 10,1.5 L10,10 ");
|
||||
});
|
||||
|
||||
it("does not round fixed stroke hairpin turns", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
strokeWidth: 1,
|
||||
points: [pointFrom(0, 0), pointFrom(10, 0), pointFrom(9, 1)],
|
||||
});
|
||||
|
||||
const [path] = ShapeCache.generateElementShape(element, null);
|
||||
|
||||
expect(path).toBe("M0,0 L10,0 L9,1 ");
|
||||
});
|
||||
|
||||
it("curves directly to the endpoint for tiny final smooth segments", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
strokeWidth: 1,
|
||||
points: [pointFrom(0, 0), pointFrom(0.5, 0.1), pointFrom(0.8, 0.3)],
|
||||
});
|
||||
|
||||
const [path] = ShapeCache.generateElementShape(element, null);
|
||||
|
||||
expect(path).toBe("M0,0 Q0.5,0.1 0.8,0.3 ");
|
||||
});
|
||||
|
||||
it("curves directly to the endpoint for tiny final rounded corners", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
strokeWidth: 1,
|
||||
points: [pointFrom(0, 0), pointFrom(10, 0), pointFrom(10, 1)],
|
||||
});
|
||||
|
||||
const [path] = ShapeCache.generateElementShape(element, null);
|
||||
|
||||
expect(path).toBe("M0,0 L9.65,0 Q10,0 10,1 ");
|
||||
});
|
||||
|
||||
it("smooths dense fixed stroke points without dropping them", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
points: [
|
||||
pointFrom(0, 0),
|
||||
pointFrom(0.3, 0.1),
|
||||
pointFrom(0.6, 0.35),
|
||||
pointFrom(1, 0.9),
|
||||
],
|
||||
});
|
||||
|
||||
const [path] = ShapeCache.generateElementShape(element, null);
|
||||
|
||||
expect(path).toContain("Q0.3,0.1 ");
|
||||
expect(path).toContain("Q0.6,0.35 ");
|
||||
});
|
||||
|
||||
it("drops synthetic loop closure for fixed strokes", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
points: [pointFrom(0, 0), pointFrom(10, 0), pointFrom(0, 0)],
|
||||
});
|
||||
|
||||
const [path] = ShapeCache.generateElementShape(element, null);
|
||||
|
||||
expect(path).toBe("M0,0 L10,0 ");
|
||||
});
|
||||
|
||||
it("uses fixed perfect-freehand settings for fixed strokes", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
strokeWidth: 8,
|
||||
points: [pointFrom(0, 0), pointFrom(10, 10), pointFrom(20, 15)],
|
||||
});
|
||||
|
||||
getFreedrawOutlinePoints(element);
|
||||
|
||||
expect(vi.mocked(getStroke)).toHaveBeenCalledWith(
|
||||
element.points,
|
||||
expect.objectContaining({
|
||||
simulatePressure: false,
|
||||
size: element.strokeWidth,
|
||||
thinning: 0,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps pressure-aware perfect-freehand settings for variable strokes", () => {
|
||||
const element = API.createElement({
|
||||
type: "freedraw",
|
||||
points: [pointFrom(0, 0), pointFrom(10, 10), pointFrom(20, 15)],
|
||||
});
|
||||
|
||||
getFreedrawOutlinePoints({
|
||||
...element,
|
||||
simulatePressure: false,
|
||||
pressures: [0.2, 0.8, 0.4],
|
||||
});
|
||||
|
||||
expect(vi.mocked(getStroke)).toHaveBeenCalledWith(
|
||||
[
|
||||
[0, 0, 0.2],
|
||||
[10, 10, 0.8],
|
||||
[20, 15, 0.4],
|
||||
],
|
||||
expect.objectContaining({
|
||||
simulatePressure: false,
|
||||
size: element.strokeWidth * 4.25,
|
||||
thinning: 0.6,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,140 @@
|
||||
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
|
||||
|
||||
import { arrayToMap } from "@excalidraw/common";
|
||||
|
||||
import type App from "@excalidraw/excalidraw/components/App";
|
||||
|
||||
import type { ObservedAppState } from "@excalidraw/excalidraw/types";
|
||||
import type {
|
||||
ExcalidrawElement,
|
||||
SceneElementsMap,
|
||||
} from "@excalidraw/element/types";
|
||||
|
||||
import {
|
||||
CaptureUpdateAction,
|
||||
Store,
|
||||
StoreSnapshot,
|
||||
newElementWith,
|
||||
} from "../src";
|
||||
|
||||
const getScheduledMicroActionCount = (store: Store) =>
|
||||
(
|
||||
store as unknown as {
|
||||
scheduledMicroActions: Array<() => void>;
|
||||
}
|
||||
).scheduledMicroActions.length;
|
||||
|
||||
const flushMicroActions = (store: Store) => {
|
||||
(
|
||||
store as unknown as {
|
||||
flushMicroActions: () => void;
|
||||
}
|
||||
).flushMicroActions();
|
||||
};
|
||||
|
||||
const toSceneElementsMap = (
|
||||
elements: readonly ExcalidrawElement[],
|
||||
): SceneElementsMap => arrayToMap(elements) as SceneElementsMap;
|
||||
|
||||
const createStoreHarness = (elements: SceneElementsMap) => {
|
||||
const appState: ObservedAppState = StoreSnapshot.empty().appState;
|
||||
const app = {
|
||||
scene: {
|
||||
getElementsMapIncludingDeleted: () => elements,
|
||||
},
|
||||
state: appState,
|
||||
} as unknown as App;
|
||||
|
||||
const store = new Store(app);
|
||||
store.snapshot = StoreSnapshot.create(elements, appState);
|
||||
|
||||
return { store, appState };
|
||||
};
|
||||
|
||||
describe("Store synthetic increment isolation", () => {
|
||||
it("keeps pending immediate micro actions queued across synthetic commit", () => {
|
||||
const element = API.createElement({
|
||||
type: "rectangle",
|
||||
id: "store-isolated-immediate",
|
||||
x: 0,
|
||||
y: 0,
|
||||
});
|
||||
const baselineElements = toSceneElementsMap([element]);
|
||||
const { store } = createStoreHarness(baselineElements);
|
||||
|
||||
const incrementTypes: Array<"durable" | "ephemeral"> = [];
|
||||
const detach = store.onStoreIncrementEmitter.on((increment) => {
|
||||
incrementTypes.push(increment.type);
|
||||
});
|
||||
|
||||
store.scheduleMicroAction({
|
||||
action: CaptureUpdateAction.IMMEDIATELY,
|
||||
elements: [newElementWith(element, { y: 240 })],
|
||||
appState: undefined,
|
||||
});
|
||||
|
||||
expect(getScheduledMicroActionCount(store)).toBe(1);
|
||||
|
||||
const committed = store.commitSyntheticIncrement({
|
||||
logicalBefore: {
|
||||
elements: baselineElements,
|
||||
},
|
||||
logicalAfter: {
|
||||
elements: toSceneElementsMap([newElementWith(element, { x: 120 })]),
|
||||
},
|
||||
});
|
||||
|
||||
expect(committed).toBe(true);
|
||||
expect(incrementTypes).toEqual(["durable"]);
|
||||
expect(getScheduledMicroActionCount(store)).toBe(1);
|
||||
|
||||
flushMicroActions(store);
|
||||
|
||||
expect(incrementTypes).toEqual(["durable", "durable"]);
|
||||
expect(getScheduledMicroActionCount(store)).toBe(0);
|
||||
detach();
|
||||
});
|
||||
|
||||
it("keeps pending eventually micro actions queued across synthetic commit", () => {
|
||||
const element = API.createElement({
|
||||
type: "rectangle",
|
||||
id: "store-isolated-eventually",
|
||||
x: 0,
|
||||
y: 0,
|
||||
});
|
||||
const baselineElements = toSceneElementsMap([element]);
|
||||
const { store } = createStoreHarness(baselineElements);
|
||||
|
||||
const incrementTypes: Array<"durable" | "ephemeral"> = [];
|
||||
const detach = store.onStoreIncrementEmitter.on((increment) => {
|
||||
incrementTypes.push(increment.type);
|
||||
});
|
||||
|
||||
store.scheduleMicroAction({
|
||||
action: CaptureUpdateAction.EVENTUALLY,
|
||||
elements: [newElementWith(element, { y: 240 })],
|
||||
appState: undefined,
|
||||
});
|
||||
|
||||
expect(getScheduledMicroActionCount(store)).toBe(1);
|
||||
|
||||
const committed = store.commitSyntheticIncrement({
|
||||
logicalBefore: {
|
||||
elements: baselineElements,
|
||||
},
|
||||
logicalAfter: {
|
||||
elements: toSceneElementsMap([newElementWith(element, { x: 120 })]),
|
||||
},
|
||||
});
|
||||
|
||||
expect(committed).toBe(true);
|
||||
expect(incrementTypes).toEqual(["durable"]);
|
||||
expect(getScheduledMicroActionCount(store)).toBe(1);
|
||||
|
||||
flushMicroActions(store);
|
||||
|
||||
expect(incrementTypes).toEqual(["durable", "ephemeral"]);
|
||||
expect(getScheduledMicroActionCount(store)).toBe(0);
|
||||
detach();
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,6 @@ import { pointFrom } from "@excalidraw/math";
|
||||
|
||||
import { bindOrUnbindBindingElement } from "@excalidraw/element/binding";
|
||||
import {
|
||||
isLoopFreeDrawElement,
|
||||
isValidPolygon,
|
||||
LinearElementEditor,
|
||||
newElementWith,
|
||||
@@ -243,11 +242,7 @@ export const actionFinalize = register<FormData>({
|
||||
// If the multi point line closes the loop,
|
||||
// set the last point to first point.
|
||||
// This ensures that loop remains closed at different scales.
|
||||
const isLoop = isLineElement(element)
|
||||
? isPathALoop(element.points, appState.zoom.value)
|
||||
: isFreeDrawElement(element)
|
||||
? isLoopFreeDrawElement(element, appState.zoom.value)
|
||||
: false;
|
||||
const isLoop = isPathALoop(element.points, appState.zoom.value);
|
||||
|
||||
if (isLoop && (isLineElement(element) || isFreeDrawElement(element))) {
|
||||
const linePoints = element.points;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { queryByTestId } from "@testing-library/react";
|
||||
import { pointFrom } from "@excalidraw/math";
|
||||
|
||||
import {
|
||||
COLOR_PALETTE,
|
||||
@@ -11,7 +10,7 @@ import {
|
||||
import { Excalidraw } from "../index";
|
||||
import { API } from "../tests/helpers/api";
|
||||
import { UI } from "../tests/helpers/ui";
|
||||
import { act, render } from "../tests/test-utils";
|
||||
import { render } from "../tests/test-utils";
|
||||
|
||||
describe("element locking", () => {
|
||||
beforeEach(async () => {
|
||||
@@ -81,20 +80,6 @@ describe("element locking", () => {
|
||||
const centerTextAlign = queryByTestId(document.body, `align-right`);
|
||||
expect(centerTextAlign).toBeChecked();
|
||||
});
|
||||
|
||||
it("should show the active freedraw stroke type", () => {
|
||||
UI.clickTool("freedraw");
|
||||
|
||||
API.setAppState({
|
||||
currentItemStrokeShape: "fixed",
|
||||
});
|
||||
|
||||
const fixedStrokeShape = queryByTestId(
|
||||
document.body,
|
||||
"strokeShape-fixed",
|
||||
);
|
||||
expect(fixedStrokeShape).toBeChecked();
|
||||
});
|
||||
});
|
||||
|
||||
describe("properties when elements selected", () => {
|
||||
@@ -159,9 +144,6 @@ describe("element locking", () => {
|
||||
expect(
|
||||
queryByTestId(document.body, `strokeWidth-thin`),
|
||||
).not.toBeChecked();
|
||||
expect(
|
||||
queryByTestId(document.body, `strokeWidth-medium`),
|
||||
).not.toBeChecked();
|
||||
expect(
|
||||
queryByTestId(document.body, `strokeWidth-bold`),
|
||||
).not.toBeChecked();
|
||||
@@ -177,7 +159,6 @@ describe("element locking", () => {
|
||||
});
|
||||
const text = API.createElement({
|
||||
type: "text",
|
||||
strokeWidth: STROKE_WIDTH.bold,
|
||||
fontFamily: FONT_FAMILY["Comic Shanns"],
|
||||
});
|
||||
API.setElements([rect, text]);
|
||||
@@ -188,35 +169,5 @@ describe("element locking", () => {
|
||||
"active",
|
||||
);
|
||||
});
|
||||
|
||||
it("should highlight the fixed freedraw stroke type for selected elements", () => {
|
||||
const freedraw = API.createElement({
|
||||
type: "freedraw",
|
||||
strokeShape: "fixed",
|
||||
points: [pointFrom(0, 0), pointFrom(10, 10), pointFrom(20, 15)],
|
||||
});
|
||||
API.setElements([freedraw]);
|
||||
API.setSelectedElements([freedraw]);
|
||||
|
||||
const fixedStrokeShape = queryByTestId(
|
||||
document.body,
|
||||
"strokeShape-fixed",
|
||||
);
|
||||
expect(fixedStrokeShape).toBeChecked();
|
||||
});
|
||||
|
||||
it("should apply fixed freedraw stroke type to newly drawn strokes", () => {
|
||||
UI.clickTool("freedraw");
|
||||
|
||||
act(() => {
|
||||
queryByTestId(document.body, "strokeShape-fixed")?.click();
|
||||
});
|
||||
|
||||
const freedraw = UI.createElement("freedraw", {
|
||||
points: [pointFrom(0, 0), pointFrom(10, 10), pointFrom(20, 15)],
|
||||
});
|
||||
|
||||
expect(freedraw.strokeShape).toBe("fixed");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,6 @@ import { pointFrom } from "@excalidraw/math";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
import {
|
||||
DEFAULT_FREE_DRAW_STROKE_SHAPE,
|
||||
DEFAULT_ELEMENT_BACKGROUND_COLOR_PALETTE,
|
||||
DEFAULT_ELEMENT_BACKGROUND_PICKS,
|
||||
DEFAULT_ELEMENT_STROKE_COLOR_PALETTE,
|
||||
@@ -37,7 +36,6 @@ import {
|
||||
import { LinearElementEditor } from "@excalidraw/element";
|
||||
|
||||
import { newElementWith } from "@excalidraw/element";
|
||||
import { newFreeDrawElementWithStrokeShape } from "@excalidraw/element";
|
||||
import { getArrowheadForPicker } from "@excalidraw/element";
|
||||
|
||||
import {
|
||||
@@ -49,7 +47,6 @@ import {
|
||||
isArrowElement,
|
||||
isBoundToContainer,
|
||||
isElbowArrow,
|
||||
isFreeDrawElement,
|
||||
isLinearElement,
|
||||
isLineElement,
|
||||
isTextElement,
|
||||
@@ -73,7 +70,6 @@ import type {
|
||||
ElementsMap,
|
||||
ExcalidrawBindableElement,
|
||||
ExcalidrawElement,
|
||||
ExcalidrawFreeDrawElement,
|
||||
ExcalidrawLinearElement,
|
||||
ExcalidrawTextElement,
|
||||
FontFamilyValues,
|
||||
@@ -109,11 +105,8 @@ import {
|
||||
SloppinessArtistIcon,
|
||||
SloppinessCartoonistIcon,
|
||||
StrokeWidthBaseIcon,
|
||||
StrokeWidthMediumIcon,
|
||||
StrokeWidthBoldIcon,
|
||||
StrokeWidthExtraBoldIcon,
|
||||
StrokeShapeFixedIcon,
|
||||
StrokeShapeVariableIcon,
|
||||
FontSizeSmallIcon,
|
||||
FontSizeMediumIcon,
|
||||
FontSizeLargeIcon,
|
||||
@@ -194,29 +187,6 @@ export const changeProperty = (
|
||||
});
|
||||
};
|
||||
|
||||
const getFreeDrawStrokeShape = (item: {
|
||||
strokeShape?: ExcalidrawFreeDrawElement["strokeShape"];
|
||||
currentItemStrokeShape?: AppState["currentItemStrokeShape"];
|
||||
}) =>
|
||||
item.strokeShape ??
|
||||
item.currentItemStrokeShape ??
|
||||
DEFAULT_FREE_DRAW_STROKE_SHAPE;
|
||||
|
||||
const getAppStateWithCurrentItemStrokeShape = (
|
||||
appState: AppState,
|
||||
strokeShape: NonNullable<ExcalidrawFreeDrawElement["strokeShape"]>,
|
||||
) => {
|
||||
// if (strokeShape === DEFAULT_FREE_DRAW_STROKE_SHAPE) {
|
||||
// const { currentItemStrokeShape, ...nextAppState } = appState;
|
||||
// return nextAppState;
|
||||
// }
|
||||
|
||||
return {
|
||||
...appState,
|
||||
currentItemStrokeShape: strokeShape,
|
||||
};
|
||||
};
|
||||
|
||||
export const getFormValue = function <T extends Primitive>(
|
||||
elements: readonly ExcalidrawElement[],
|
||||
app: AppClassProperties,
|
||||
@@ -604,12 +574,6 @@ export const actionChangeStrokeWidth = register<
|
||||
icon: StrokeWidthBaseIcon,
|
||||
testId: "strokeWidth-thin",
|
||||
},
|
||||
{
|
||||
value: STROKE_WIDTH.medium,
|
||||
text: t("labels.medium"),
|
||||
icon: StrokeWidthMediumIcon,
|
||||
testId: "strokeWidth-medium",
|
||||
},
|
||||
{
|
||||
value: STROKE_WIDTH.bold,
|
||||
text: t("labels.bold"),
|
||||
@@ -638,60 +602,6 @@ export const actionChangeStrokeWidth = register<
|
||||
),
|
||||
});
|
||||
|
||||
export const actionChangeStrokeShape = register<
|
||||
NonNullable<ExcalidrawFreeDrawElement["strokeShape"]>
|
||||
>({
|
||||
name: "changeStrokeShape",
|
||||
label: "labels.strokeShape",
|
||||
trackEvent: false,
|
||||
perform: (elements, appState, value) => {
|
||||
invariant(value, "actionChangeStrokeShape: value must be defined");
|
||||
return {
|
||||
elements: changeProperty(elements, appState, (el) =>
|
||||
isFreeDrawElement(el)
|
||||
? newFreeDrawElementWithStrokeShape(el, value)
|
||||
: el,
|
||||
),
|
||||
appState: getAppStateWithCurrentItemStrokeShape(appState, value),
|
||||
captureUpdate: CaptureUpdateAction.IMMEDIATELY,
|
||||
};
|
||||
},
|
||||
PanelComponent: ({ elements, appState, updateData, app }) => (
|
||||
<fieldset>
|
||||
<legend>{t("labels.strokeShape")}</legend>
|
||||
<div className="buttonList">
|
||||
<RadioSelection
|
||||
group="stroke-shape"
|
||||
options={[
|
||||
{
|
||||
value: "variable",
|
||||
text: t("labels.strokeShape_variable"),
|
||||
icon: StrokeShapeVariableIcon,
|
||||
testId: "strokeShape-variable",
|
||||
},
|
||||
{
|
||||
value: "fixed",
|
||||
text: t("labels.strokeShape_fixed"),
|
||||
icon: StrokeShapeFixedIcon,
|
||||
testId: "strokeShape-fixed",
|
||||
},
|
||||
]}
|
||||
value={getFormValue(
|
||||
elements,
|
||||
app,
|
||||
(element) =>
|
||||
getFreeDrawStrokeShape(element as ExcalidrawFreeDrawElement),
|
||||
isFreeDrawElement,
|
||||
(hasSelection) =>
|
||||
hasSelection ? null : getFreeDrawStrokeShape(appState),
|
||||
)}
|
||||
onChange={(value) => updateData(value)}
|
||||
/>
|
||||
</div>
|
||||
</fieldset>
|
||||
),
|
||||
});
|
||||
|
||||
export const actionChangeSloppiness = register<ExcalidrawElement["roughness"]>({
|
||||
name: "changeSloppiness",
|
||||
label: "labels.sloppiness",
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
DEFAULT_FREE_DRAW_STROKE_SHAPE,
|
||||
DEFAULT_FONT_SIZE,
|
||||
DEFAULT_FONT_FAMILY,
|
||||
DEFAULT_TEXT_ALIGN,
|
||||
@@ -11,7 +10,6 @@ import {
|
||||
import { newElementWith } from "@excalidraw/element";
|
||||
|
||||
import {
|
||||
isFreeDrawElement,
|
||||
hasBoundTextElement,
|
||||
canApplyRoundnessTypeToElement,
|
||||
getDefaultRoundnessTypeForElement,
|
||||
@@ -20,7 +18,6 @@ import {
|
||||
isExcalidrawElement,
|
||||
isTextElement,
|
||||
} from "@excalidraw/element";
|
||||
import { newFreeDrawElementWithStrokeShape } from "@excalidraw/element";
|
||||
|
||||
import {
|
||||
getBoundTextElement,
|
||||
@@ -157,17 +154,6 @@ export const actionPasteStyles = register({
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
isFreeDrawElement(newElement) &&
|
||||
isFreeDrawElement(elementStylesToCopyFrom)
|
||||
) {
|
||||
newElement = newFreeDrawElementWithStrokeShape(
|
||||
newElement,
|
||||
elementStylesToCopyFrom.strokeShape ??
|
||||
DEFAULT_FREE_DRAW_STROKE_SHAPE,
|
||||
);
|
||||
}
|
||||
|
||||
if (isFrameLikeElement(element)) {
|
||||
newElement = newElementWith(newElement, {
|
||||
roundness: null,
|
||||
|
||||
@@ -172,7 +172,6 @@ const APP_STATE_STORAGE_CONF = (<
|
||||
currentItemStrokeColor: { browser: true, export: false, server: false },
|
||||
currentItemStrokeStyle: { browser: true, export: false, server: false },
|
||||
currentItemStrokeWidth: { browser: true, export: false, server: false },
|
||||
currentItemStrokeShape: { browser: true, export: false, server: false },
|
||||
currentItemTextAlign: { browser: true, export: false, server: false },
|
||||
currentHoveredFontFamily: { browser: false, export: false, server: false },
|
||||
cursorButton: { browser: true, export: false, server: false },
|
||||
|
||||
@@ -394,11 +394,6 @@ const CombinedShapeProperties = ({
|
||||
hasStrokeWidth(element.type),
|
||||
)) &&
|
||||
renderAction("changeStrokeWidth")}
|
||||
{(appState.activeTool.type === "freedraw" ||
|
||||
targetElements.some(
|
||||
(element) => element.type === "freedraw",
|
||||
)) &&
|
||||
renderAction("changeStrokeShape")}
|
||||
{(hasStrokeStyle(appState.activeTool.type) ||
|
||||
targetElements.some((element) =>
|
||||
hasStrokeStyle(element.type),
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
clamp,
|
||||
pointFrom,
|
||||
pointDistance,
|
||||
round,
|
||||
vector,
|
||||
pointRotateRads,
|
||||
vectorFromPoint,
|
||||
@@ -120,7 +119,6 @@ import {
|
||||
getElementAbsoluteCoords,
|
||||
bindOrUnbindBindingElements,
|
||||
fixBindingsAfterDeletion,
|
||||
getFixedFreeDrawPointAction,
|
||||
getHoveredElementForBinding,
|
||||
isBindingEnabled,
|
||||
updateBoundElements,
|
||||
@@ -363,6 +361,7 @@ import { restoreAppState, restoreElements } from "../data/restore";
|
||||
import { getCenter, getDistance } from "../gesture";
|
||||
import { History } from "../history";
|
||||
import { defaultLang, getLanguage, languages, setLanguage, t } from "../i18n";
|
||||
import { TransactionManager } from "../transaction";
|
||||
|
||||
import {
|
||||
calculateScrollCenter,
|
||||
@@ -617,19 +616,6 @@ const gesture: Gesture = {
|
||||
initialScale: null,
|
||||
};
|
||||
|
||||
const FREEDRAW_POINT_DECIMALS = 2;
|
||||
const FREEDRAW_PRESSURE_DECIMALS = 3;
|
||||
const FREEDRAW_DOT_EPSILON = 1 / 10 ** FREEDRAW_POINT_DECIMALS;
|
||||
|
||||
const roundFreeDrawCoordinate = (value: number) =>
|
||||
round(value, FREEDRAW_POINT_DECIMALS);
|
||||
|
||||
const roundFreeDrawPressure = (value: number) =>
|
||||
round(value, FREEDRAW_PRESSURE_DECIMALS);
|
||||
|
||||
const getRoundedFreeDrawPoint = (x: number, y: number) =>
|
||||
pointFrom<LocalPoint>(roundFreeDrawCoordinate(x), roundFreeDrawCoordinate(y));
|
||||
|
||||
class App extends React.Component<AppProps, AppState> {
|
||||
canvas: AppClassProperties["canvas"];
|
||||
interactiveCanvas: AppClassProperties["interactiveCanvas"] = null;
|
||||
@@ -652,7 +638,8 @@ class App extends React.Component<AppProps, AppState> {
|
||||
public library: AppClassProperties["library"];
|
||||
public libraryItemsFromStorage: LibraryItems | undefined;
|
||||
public id: string;
|
||||
private store: Store;
|
||||
public transactionManager: TransactionManager;
|
||||
public store: Store;
|
||||
private history: History;
|
||||
public excalidrawContainerValue: {
|
||||
container: HTMLDivElement | null;
|
||||
@@ -847,6 +834,8 @@ class App extends React.Component<AppProps, AppState> {
|
||||
|
||||
this.store = new Store(this);
|
||||
this.history = new History(this.store);
|
||||
this.transactionManager = new TransactionManager(this);
|
||||
this.transactionManager.attachHistory(this.history);
|
||||
|
||||
this.excalidrawContainerValue = {
|
||||
container: this.excalidrawContainerRef.current,
|
||||
@@ -854,7 +843,6 @@ class App extends React.Component<AppProps, AppState> {
|
||||
};
|
||||
|
||||
this.fonts = new Fonts(this.scene);
|
||||
this.history = new History(this.store);
|
||||
|
||||
this.actionManager.registerAll(actions);
|
||||
this.actionManager.registerAction(createUndoAction(this.history));
|
||||
@@ -4617,6 +4605,11 @@ class App extends React.Component<AppProps, AppState> {
|
||||
);
|
||||
};
|
||||
|
||||
/** Creates a new transaction for batching mutations into a single undo entry. */
|
||||
public createTransaction: TransactionManager["create"] = () => {
|
||||
return this.transactionManager.create();
|
||||
};
|
||||
|
||||
public mutateElement = <TElement extends Mutable<ExcalidrawElement>>(
|
||||
element: TElement,
|
||||
updates: ElementUpdate<TElement>,
|
||||
@@ -8095,7 +8088,7 @@ class App extends React.Component<AppProps, AppState> {
|
||||
|
||||
setCursor(this.interactiveCanvas, CURSOR_TYPE.GRABBING);
|
||||
let { clientX: lastX, clientY: lastY } = event;
|
||||
const onPointerMove = ((event: PointerEvent) => {
|
||||
const onPointerMove = withBatchedUpdatesThrottled((event: PointerEvent) => {
|
||||
const deltaX = lastX - event.clientX;
|
||||
const deltaY = lastY - event.clientY;
|
||||
lastX = event.clientX;
|
||||
@@ -8161,7 +8154,7 @@ class App extends React.Component<AppProps, AppState> {
|
||||
window.removeEventListener(EVENT.POINTER_MOVE, onPointerMove);
|
||||
window.removeEventListener(EVENT.POINTER_UP, teardown);
|
||||
window.removeEventListener(EVENT.BLUR, teardown);
|
||||
// onPointerMove.flush();
|
||||
onPointerMove.flush();
|
||||
}),
|
||||
);
|
||||
window.addEventListener(EVENT.BLUR, teardown);
|
||||
@@ -8271,14 +8264,14 @@ class App extends React.Component<AppProps, AppState> {
|
||||
isDraggingScrollBar = true;
|
||||
pointerDownState.lastCoords.x = event.clientX;
|
||||
pointerDownState.lastCoords.y = event.clientY;
|
||||
const onPointerMove = (event: PointerEvent) => {
|
||||
const onPointerMove = withBatchedUpdatesThrottled((event: PointerEvent) => {
|
||||
const target = event.target;
|
||||
if (!(target instanceof HTMLElement)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.handlePointerMoveOverScrollbars(event, pointerDownState);
|
||||
};
|
||||
});
|
||||
const onPointerUp = withBatchedUpdates(() => {
|
||||
lastPointerUp = null;
|
||||
isDraggingScrollBar = false;
|
||||
@@ -8289,7 +8282,7 @@ class App extends React.Component<AppProps, AppState> {
|
||||
this.savePointer(event.clientX, event.clientY, "up");
|
||||
window.removeEventListener(EVENT.POINTER_MOVE, onPointerMove);
|
||||
window.removeEventListener(EVENT.POINTER_UP, onPointerUp);
|
||||
// onPointerMove.flush();
|
||||
onPointerMove.flush();
|
||||
});
|
||||
|
||||
lastPointerUp = onPointerUp;
|
||||
@@ -8848,13 +8841,10 @@ class App extends React.Component<AppProps, AppState> {
|
||||
opacity: this.state.currentItemOpacity,
|
||||
roundness: null,
|
||||
simulatePressure,
|
||||
strokeShape: this.state.currentItemStrokeShape,
|
||||
locked: false,
|
||||
frameId: topLayerFrame ? topLayerFrame.id : null,
|
||||
points: [pointFrom<LocalPoint>(0, 0)],
|
||||
pressures: simulatePressure
|
||||
? []
|
||||
: [roundFreeDrawPressure(event.pressure)],
|
||||
pressures: simulatePressure ? [] : [event.pressure],
|
||||
});
|
||||
|
||||
this.scene.insertElement(element);
|
||||
@@ -9496,7 +9486,7 @@ class App extends React.Component<AppProps, AppState> {
|
||||
private onPointerMoveFromPointerDownHandler(
|
||||
pointerDownState: PointerDownState,
|
||||
) {
|
||||
return (event: PointerEvent) => {
|
||||
return withBatchedUpdatesThrottled((event: PointerEvent) => {
|
||||
if (this.state.openDialog?.name === "elementLinkSelector") {
|
||||
return;
|
||||
}
|
||||
@@ -10192,36 +10182,20 @@ class App extends React.Component<AppProps, AppState> {
|
||||
const points = newElement.points;
|
||||
const dx = pointerCoords.x - newElement.x;
|
||||
const dy = pointerCoords.y - newElement.y;
|
||||
const nextPoint = getRoundedFreeDrawPoint(dx, dy);
|
||||
const pressure = roundFreeDrawPressure(event.pressure);
|
||||
const pointAction =
|
||||
newElement.strokeShape === "fixed"
|
||||
? getFixedFreeDrawPointAction({
|
||||
points,
|
||||
nextPoint,
|
||||
strokeWidth: newElement.strokeWidth,
|
||||
zoomValue: this.state.zoom.value,
|
||||
})
|
||||
: points.length > 0 &&
|
||||
points[points.length - 1][0] === nextPoint[0] &&
|
||||
points[points.length - 1][1] === nextPoint[1]
|
||||
? "discard"
|
||||
: "append";
|
||||
|
||||
if (pointAction !== "discard") {
|
||||
const lastPoint = points.length > 0 && points[points.length - 1];
|
||||
const discardPoint =
|
||||
lastPoint && lastPoint[0] === dx && lastPoint[1] === dy;
|
||||
|
||||
if (!discardPoint) {
|
||||
const pressures = newElement.simulatePressure
|
||||
? newElement.pressures
|
||||
: pointAction === "replace"
|
||||
? [...newElement.pressures.slice(0, -1), pressure]
|
||||
: [...newElement.pressures, pressure];
|
||||
: [...newElement.pressures, event.pressure];
|
||||
|
||||
this.scene.mutateElement(
|
||||
newElement,
|
||||
{
|
||||
points:
|
||||
pointAction === "replace"
|
||||
? [...points.slice(0, -1), nextPoint]
|
||||
: [...points, nextPoint],
|
||||
points: [...points, pointFrom<LocalPoint>(dx, dy)],
|
||||
pressures,
|
||||
},
|
||||
{
|
||||
@@ -10379,7 +10353,7 @@ class App extends React.Component<AppProps, AppState> {
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// Returns whether the pointer move happened over either scrollbar
|
||||
@@ -10424,7 +10398,7 @@ class App extends React.Component<AppProps, AppState> {
|
||||
this.removePointer(childEvent);
|
||||
pointerDownState.drag.blockDragging = false;
|
||||
if (pointerDownState.eventListeners.onMove) {
|
||||
// pointerDownState.eventListeners.onMove.flush();
|
||||
pointerDownState.eventListeners.onMove.flush();
|
||||
}
|
||||
const {
|
||||
newElement,
|
||||
@@ -10661,48 +10635,23 @@ class App extends React.Component<AppProps, AppState> {
|
||||
);
|
||||
|
||||
const points = newElement.points;
|
||||
const dx = pointerCoords.x - newElement.x;
|
||||
const dy = pointerCoords.y - newElement.y;
|
||||
let nextPoint = getRoundedFreeDrawPoint(dx, dy);
|
||||
let dx = pointerCoords.x - newElement.x;
|
||||
let dy = pointerCoords.y - newElement.y;
|
||||
|
||||
// Allows dots to avoid being flagged as infinitely small
|
||||
if (nextPoint[0] === points[0][0] && nextPoint[1] === points[0][1]) {
|
||||
nextPoint = getRoundedFreeDrawPoint(
|
||||
dx + FREEDRAW_DOT_EPSILON,
|
||||
dy + FREEDRAW_DOT_EPSILON,
|
||||
);
|
||||
if (dx === points[0][0] && dy === points[0][1]) {
|
||||
dy += 0.0001;
|
||||
dx += 0.0001;
|
||||
}
|
||||
|
||||
const lastPoint = points[points.length - 1];
|
||||
const pressure = roundFreeDrawPressure(childEvent.pressure);
|
||||
const pointAction =
|
||||
newElement.strokeShape === "fixed"
|
||||
? getFixedFreeDrawPointAction({
|
||||
points,
|
||||
nextPoint,
|
||||
strokeWidth: newElement.strokeWidth,
|
||||
zoomValue: this.state.zoom.value,
|
||||
isFinalPoint: true,
|
||||
})
|
||||
: !lastPoint ||
|
||||
lastPoint[0] !== nextPoint[0] ||
|
||||
lastPoint[1] !== nextPoint[1]
|
||||
? "append"
|
||||
: "discard";
|
||||
const pressures = newElement.simulatePressure
|
||||
? []
|
||||
: [...newElement.pressures, childEvent.pressure];
|
||||
|
||||
if (pointAction !== "discard") {
|
||||
this.scene.mutateElement(newElement, {
|
||||
points:
|
||||
pointAction === "replace"
|
||||
? [...points.slice(0, -1), nextPoint]
|
||||
: [...points, nextPoint],
|
||||
pressures: newElement.simulatePressure
|
||||
? []
|
||||
: pointAction === "replace"
|
||||
? [...newElement.pressures.slice(0, -1), pressure]
|
||||
: [...newElement.pressures, pressure],
|
||||
});
|
||||
}
|
||||
this.scene.mutateElement(newElement, {
|
||||
points: [...points, pointFrom<LocalPoint>(dx, dy)],
|
||||
pressures,
|
||||
});
|
||||
|
||||
this.actionManager.executeAction(actionFinalize);
|
||||
|
||||
|
||||
@@ -1164,22 +1164,11 @@ export const StrokeWidthBaseIcon = createIcon(
|
||||
modifiedTablerIconProps,
|
||||
);
|
||||
|
||||
export const StrokeWidthMediumIcon = createIcon(
|
||||
<path
|
||||
d="M5 10h10"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>,
|
||||
modifiedTablerIconProps,
|
||||
);
|
||||
|
||||
export const StrokeWidthBoldIcon = createIcon(
|
||||
<path
|
||||
d="M5 10h10"
|
||||
stroke="currentColor"
|
||||
strokeWidth="3.75"
|
||||
strokeWidth="2.5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>,
|
||||
@@ -1190,38 +1179,7 @@ export const StrokeWidthExtraBoldIcon = createIcon(
|
||||
<path
|
||||
d="M5 10h10"
|
||||
stroke="currentColor"
|
||||
strokeWidth="5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>,
|
||||
modifiedTablerIconProps,
|
||||
);
|
||||
|
||||
export const StrokeShapeVariableIcon = createIcon(
|
||||
<>
|
||||
<path
|
||||
d="M3.5 12.5c1.8-3.5 4.1-5.2 6.8-5.2 2.4 0 4.4 1.3 6.2 3.9"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.25"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
<path
|
||||
d="M7.75 10.8c1-.9 2-1.35 3.05-1.35 1.2 0 2.4.57 3.7 1.72"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
</>,
|
||||
modifiedTablerIconProps,
|
||||
);
|
||||
|
||||
export const StrokeShapeFixedIcon = createIcon(
|
||||
<path
|
||||
d="M3.5 12h13"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2.5"
|
||||
strokeWidth="3.75"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>,
|
||||
|
||||
@@ -96,13 +96,6 @@ type RestoredAppState = Omit<
|
||||
"offsetTop" | "offsetLeft" | "width" | "height"
|
||||
>;
|
||||
|
||||
const normalizeFreeDrawStrokeShape = (
|
||||
strokeShape: unknown,
|
||||
): AppState["currentItemStrokeShape"] =>
|
||||
strokeShape === "fixed" || strokeShape === "stable"
|
||||
? "fixed"
|
||||
: undefined;
|
||||
|
||||
export const AllowedExcalidrawActiveTools: Record<
|
||||
AppState["activeTool"]["type"],
|
||||
boolean
|
||||
@@ -419,12 +412,10 @@ export const restoreElement = (
|
||||
|
||||
return element;
|
||||
case "freedraw": {
|
||||
const strokeShape = normalizeFreeDrawStrokeShape(element.strokeShape);
|
||||
return restoreElementWithProperties(element, {
|
||||
points: element.points,
|
||||
simulatePressure: element.simulatePressure,
|
||||
pressures: element.pressures,
|
||||
...(strokeShape ? { strokeShape } : {}),
|
||||
});
|
||||
}
|
||||
case "image":
|
||||
@@ -953,16 +944,6 @@ export const restoreAppState = (
|
||||
|
||||
return {
|
||||
...nextAppState,
|
||||
...(normalizeFreeDrawStrokeShape(
|
||||
appState.currentItemStrokeShape ?? localAppState?.currentItemStrokeShape,
|
||||
)
|
||||
? {
|
||||
currentItemStrokeShape: normalizeFreeDrawStrokeShape(
|
||||
appState.currentItemStrokeShape ??
|
||||
localAppState?.currentItemStrokeShape,
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
cursorButton: localAppState?.cursorButton || "up",
|
||||
// reset on fresh restore so as to hide the UI button if penMode not active
|
||||
penDetected:
|
||||
|
||||
@@ -87,10 +87,22 @@ export class HistoryChangedEvent {
|
||||
) {}
|
||||
}
|
||||
|
||||
export type HistoryBeforeRecordListener = (delta: StoreDelta) => void;
|
||||
export type HistoryPerformDirection = "undo" | "redo";
|
||||
export type HistoryEffectiveDeltaResolverContext = {
|
||||
direction: HistoryPerformDirection;
|
||||
};
|
||||
export type HistoryEffectiveDeltaResolver = (
|
||||
delta: HistoryDelta,
|
||||
context: HistoryEffectiveDeltaResolverContext,
|
||||
) => HistoryDelta;
|
||||
|
||||
export class History {
|
||||
public readonly onHistoryChangedEmitter = new Emitter<
|
||||
[HistoryChangedEvent]
|
||||
>();
|
||||
private readonly onBeforeRecordEmitter = new Emitter<[StoreDelta]>();
|
||||
private effectiveDeltaResolver: HistoryEffectiveDeltaResolver | null = null;
|
||||
|
||||
public readonly undoStack: HistoryDelta[] = [];
|
||||
public readonly redoStack: HistoryDelta[] = [];
|
||||
@@ -105,6 +117,20 @@ export class History {
|
||||
|
||||
constructor(private readonly store: Store) {}
|
||||
|
||||
/**
|
||||
* Registers a hook that runs before a durable delta is converted
|
||||
* into a history entry.
|
||||
*/
|
||||
public onBeforeRecord(callback: HistoryBeforeRecordListener) {
|
||||
return this.onBeforeRecordEmitter.on(callback);
|
||||
}
|
||||
|
||||
public setEffectiveDeltaResolver(
|
||||
resolver: HistoryEffectiveDeltaResolver | null,
|
||||
) {
|
||||
this.effectiveDeltaResolver = resolver;
|
||||
}
|
||||
|
||||
public clear() {
|
||||
this.undoStack.length = 0;
|
||||
this.redoStack.length = 0;
|
||||
@@ -119,6 +145,8 @@ export class History {
|
||||
return;
|
||||
}
|
||||
|
||||
this.onBeforeRecordEmitter.trigger(delta);
|
||||
|
||||
// construct history entry, so once it's emitted, it's not recorded again
|
||||
const historyDelta = HistoryDelta.inverse(delta);
|
||||
|
||||
@@ -131,15 +159,14 @@ export class History {
|
||||
this.redoStack.length = 0;
|
||||
}
|
||||
|
||||
this.onHistoryChangedEmitter.trigger(
|
||||
new HistoryChangedEvent(this.isUndoStackEmpty, this.isRedoStackEmpty),
|
||||
);
|
||||
this.emitHistoryChanged();
|
||||
}
|
||||
|
||||
public undo(elements: SceneElementsMap, appState: AppState) {
|
||||
return this.perform(
|
||||
elements,
|
||||
appState,
|
||||
"undo",
|
||||
() => History.pop(this.undoStack),
|
||||
(entry: HistoryDelta) => History.push(this.redoStack, entry),
|
||||
);
|
||||
@@ -149,6 +176,7 @@ export class History {
|
||||
return this.perform(
|
||||
elements,
|
||||
appState,
|
||||
"redo",
|
||||
() => History.pop(this.redoStack),
|
||||
(entry: HistoryDelta) => History.push(this.undoStack, entry),
|
||||
);
|
||||
@@ -157,6 +185,7 @@ export class History {
|
||||
private perform(
|
||||
elements: SceneElementsMap,
|
||||
appState: AppState,
|
||||
direction: HistoryPerformDirection,
|
||||
pop: () => HistoryDelta | null,
|
||||
push: (entry: HistoryDelta) => void,
|
||||
): [SceneElementsMap, AppState] | void {
|
||||
@@ -177,9 +206,15 @@ export class History {
|
||||
|
||||
// iterate through the history entries in case they result in no visible changes
|
||||
while (historyDelta) {
|
||||
// Roundtrip invariant: whichever delta we execute must be the one that
|
||||
// continues through applyLatestChanges -> inverse -> opposite stack.
|
||||
let entryToPush = historyDelta;
|
||||
try {
|
||||
const effectiveDelta = this.resolveEffectiveDelta(historyDelta, {
|
||||
direction,
|
||||
});
|
||||
[nextElements, nextAppState, containsVisibleChange] =
|
||||
historyDelta.applyTo(nextElements, nextAppState, prevSnapshot);
|
||||
effectiveDelta.applyTo(nextElements, nextAppState, prevSnapshot);
|
||||
|
||||
const prevElements = prevSnapshot.elements;
|
||||
const nextSnapshot = prevSnapshot.maybeClone(
|
||||
@@ -190,7 +225,7 @@ export class History {
|
||||
|
||||
const change = StoreChange.create(prevSnapshot, nextSnapshot);
|
||||
const delta = HistoryDelta.applyLatestChanges(
|
||||
historyDelta,
|
||||
effectiveDelta,
|
||||
prevElements,
|
||||
nextElements,
|
||||
);
|
||||
@@ -203,12 +238,12 @@ export class History {
|
||||
delta,
|
||||
});
|
||||
|
||||
historyDelta = delta;
|
||||
entryToPush = delta;
|
||||
}
|
||||
|
||||
prevSnapshot = nextSnapshot;
|
||||
} finally {
|
||||
push(historyDelta);
|
||||
push(entryToPush);
|
||||
}
|
||||
|
||||
if (containsVisibleChange) {
|
||||
@@ -222,12 +257,16 @@ export class History {
|
||||
} finally {
|
||||
// trigger the history change event before returning completely
|
||||
// also trigger it just once, no need doing so on each entry
|
||||
this.onHistoryChangedEmitter.trigger(
|
||||
new HistoryChangedEvent(this.isUndoStackEmpty, this.isRedoStackEmpty),
|
||||
);
|
||||
this.emitHistoryChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private emitHistoryChanged() {
|
||||
this.onHistoryChangedEmitter.trigger(
|
||||
new HistoryChangedEvent(this.isUndoStackEmpty, this.isRedoStackEmpty),
|
||||
);
|
||||
}
|
||||
|
||||
private static pop(stack: HistoryDelta[]): HistoryDelta | null {
|
||||
if (!stack.length) {
|
||||
return null;
|
||||
@@ -246,4 +285,15 @@ export class History {
|
||||
const inversedEntry = HistoryDelta.inverse(entry);
|
||||
return stack.push(inversedEntry);
|
||||
}
|
||||
|
||||
private resolveEffectiveDelta(
|
||||
delta: HistoryDelta,
|
||||
context: HistoryEffectiveDeltaResolverContext,
|
||||
): HistoryDelta {
|
||||
if (!this.effectiveDeltaResolver) {
|
||||
return delta;
|
||||
}
|
||||
|
||||
return this.effectiveDeltaResolver(delta, context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,9 +30,6 @@
|
||||
"changeBackground": "Change background color",
|
||||
"fill": "Fill",
|
||||
"strokeWidth": "Stroke width",
|
||||
"strokeShape": "Stroke type",
|
||||
"strokeShape_variable": "Variable",
|
||||
"strokeShape_fixed": "Fixed",
|
||||
"strokeStyle": "Stroke style",
|
||||
"strokeStyle_solid": "Solid",
|
||||
"strokeStyle_dashed": "Dashed",
|
||||
|
||||
@@ -83,13 +83,13 @@
|
||||
"@codemirror/language": "^6.0.0",
|
||||
"@codemirror/state": "^6.0.0",
|
||||
"@codemirror/view": "^6.0.0",
|
||||
"@lezer/highlight": "^1.0.0",
|
||||
"@excalidraw/common": "0.18.0",
|
||||
"@excalidraw/element": "0.18.0",
|
||||
"@excalidraw/laser-pointer": "1.3.1",
|
||||
"@excalidraw/math": "0.18.0",
|
||||
"@excalidraw/mermaid-to-excalidraw": "2.2.2",
|
||||
"@excalidraw/random-username": "1.1.0",
|
||||
"@lezer/highlight": "^1.0.0",
|
||||
"browser-fs-access": "0.38.0",
|
||||
"canvas-roundrect-polyfill": "0.0.1",
|
||||
"clsx": "1.1.1",
|
||||
|
||||
@@ -11,10 +11,10 @@ export const withBatchedUpdates = <
|
||||
TFunction extends ((event: any) => void) | (() => void),
|
||||
>(
|
||||
func: Parameters<TFunction>["length"] extends 0 | 1 ? TFunction : never,
|
||||
) => func;
|
||||
// ((event) => {
|
||||
// unstable_batchedUpdates(func as TFunction, event);
|
||||
// }) as TFunction;
|
||||
) =>
|
||||
((event) => {
|
||||
unstable_batchedUpdates(func as TFunction, event);
|
||||
}) as TFunction;
|
||||
|
||||
/**
|
||||
* barches React state updates and throttles the calls to a single call per
|
||||
|
||||
@@ -376,11 +376,6 @@ const renderElementToSvg = (
|
||||
}
|
||||
case "freedraw": {
|
||||
const wrapper = svgRoot.ownerDocument.createElementNS(SVG_NS, "g");
|
||||
const isFixedStroke = element.strokeShape === "fixed";
|
||||
const strokeColor =
|
||||
renderConfig.theme === THEME.DARK
|
||||
? applyDarkModeFilter(element.strokeColor)
|
||||
: element.strokeColor;
|
||||
|
||||
const shapes = ShapeCache.generateElementShape(element, renderConfig);
|
||||
// always ordered as [background, stroke]
|
||||
@@ -389,15 +384,12 @@ const renderElementToSvg = (
|
||||
// stroke (SVGPathString)
|
||||
|
||||
const path = svgRoot.ownerDocument.createElementNS(SVG_NS, "path");
|
||||
if (isFixedStroke) {
|
||||
path.setAttribute("fill", "none");
|
||||
path.setAttribute("stroke", strokeColor);
|
||||
path.setAttribute("stroke-width", `${element.strokeWidth}`);
|
||||
path.setAttribute("stroke-linecap", "round");
|
||||
path.setAttribute("stroke-linejoin", "round");
|
||||
} else {
|
||||
path.setAttribute("fill", strokeColor);
|
||||
}
|
||||
path.setAttribute(
|
||||
"fill",
|
||||
renderConfig.theme === THEME.DARK
|
||||
? applyDarkModeFilter(element.strokeColor)
|
||||
: element.strokeColor,
|
||||
);
|
||||
path.setAttribute("d", shape);
|
||||
wrapper.appendChild(path);
|
||||
} else {
|
||||
|
||||
@@ -160,40 +160,6 @@ describe("restoreElements", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should restore fixed freedraw element correctly", () => {
|
||||
const freedrawElement = API.createElement({
|
||||
type: "freedraw",
|
||||
id: "id-freedraw02",
|
||||
strokeShape: "fixed",
|
||||
points: [pointFrom(0, 0), pointFrom(10, 10)],
|
||||
});
|
||||
|
||||
const restoredFreedraw = restore.restoreElements(
|
||||
[freedrawElement],
|
||||
null,
|
||||
)[0] as ExcalidrawFreeDrawElement;
|
||||
|
||||
expect(restoredFreedraw.strokeShape).toBe("fixed");
|
||||
});
|
||||
|
||||
it("should restore legacy stable freedraw element as fixed", () => {
|
||||
const restoredFreedraw = restore.restoreElements(
|
||||
[
|
||||
{
|
||||
...API.createElement({
|
||||
type: "freedraw",
|
||||
id: "id-freedraw03",
|
||||
points: [pointFrom(0, 0), pointFrom(10, 10)],
|
||||
}),
|
||||
strokeShape: "stable",
|
||||
} as any,
|
||||
],
|
||||
null,
|
||||
)[0] as ExcalidrawFreeDrawElement;
|
||||
|
||||
expect(restoredFreedraw.strokeShape).toBe("fixed");
|
||||
});
|
||||
|
||||
it("should restore line and draw elements correctly", () => {
|
||||
const lineElement = API.createElement({ type: "line", id: "id-line01" });
|
||||
|
||||
@@ -675,26 +641,6 @@ describe("restoreAppState", () => {
|
||||
expect(restoredAppState.name).toBe(stubImportedAppState.name);
|
||||
});
|
||||
|
||||
it("should restore fixed freedraw stroke type from app state", () => {
|
||||
const restoredAppState = restore.restoreAppState(
|
||||
{ currentItemStrokeShape: "fixed" } as ImportedDataState["appState"],
|
||||
null,
|
||||
);
|
||||
|
||||
expect(restoredAppState.currentItemStrokeShape).toBe("fixed");
|
||||
});
|
||||
|
||||
it("should restore legacy stable freedraw stroke type from app state as fixed", () => {
|
||||
const restoredAppState = restore.restoreAppState(
|
||||
{
|
||||
currentItemStrokeShape: "stable",
|
||||
} as unknown as ImportedDataState["appState"],
|
||||
null,
|
||||
);
|
||||
|
||||
expect(restoredAppState.currentItemStrokeShape).toBe("fixed");
|
||||
});
|
||||
|
||||
it("should return local app state when imported data state is null", () => {
|
||||
const stubLocalAppState = getDefaultAppState();
|
||||
stubLocalAppState.cursorButton = "down";
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
import React from "react";
|
||||
|
||||
import { Excalidraw } from "../index";
|
||||
import { API } from "./helpers/api";
|
||||
import { Pointer, UI } from "./helpers/ui";
|
||||
import { render } from "./test-utils";
|
||||
|
||||
describe("freedraw", () => {
|
||||
beforeEach(async () => {
|
||||
await render(<Excalidraw />);
|
||||
});
|
||||
|
||||
it("rounds stored points and drops duplicates after rounding", () => {
|
||||
const mouse = new Pointer("mouse");
|
||||
|
||||
UI.clickTool("freedraw");
|
||||
mouse.downAt(10, 20);
|
||||
mouse.moveTo(10.1234, 20.5678);
|
||||
mouse.moveTo(10.1249, 20.5681);
|
||||
mouse.upAt(20.9999, 30.0001);
|
||||
|
||||
const freedraw = window.h.elements.at(-1);
|
||||
|
||||
expect(freedraw).toEqual(expect.objectContaining({ type: "freedraw" }));
|
||||
expect((freedraw as any).points).toEqual([
|
||||
[0, 0],
|
||||
[0.12, 0.57],
|
||||
[11, 10],
|
||||
]);
|
||||
});
|
||||
|
||||
it("does not snap fixed strokes closed when ending near the start point", () => {
|
||||
const mouse = new Pointer("mouse");
|
||||
|
||||
API.setAppState({ currentItemStrokeShape: "fixed" });
|
||||
UI.clickTool("freedraw");
|
||||
mouse.downAt(10, 10);
|
||||
mouse.moveTo(40, 10);
|
||||
mouse.moveTo(30, 30);
|
||||
mouse.upAt(12, 12);
|
||||
|
||||
const freedraw = window.h.elements.at(-1) as any;
|
||||
|
||||
expect(freedraw.points[0]).toEqual([0, 0]);
|
||||
expect(freedraw.points.at(-1)).not.toEqual([0, 0]);
|
||||
});
|
||||
|
||||
it("coalesces nearly straight fixed freedraw points at the tip", () => {
|
||||
const mouse = new Pointer("mouse");
|
||||
|
||||
API.setAppState({ currentItemStrokeShape: "fixed" });
|
||||
UI.clickTool("freedraw");
|
||||
mouse.downAt(10, 10);
|
||||
|
||||
[
|
||||
[10.2, 10.01],
|
||||
[10.4, 10.03],
|
||||
[10.6, 10.02],
|
||||
[10.8, 10.04],
|
||||
[11, 10.03],
|
||||
[11.2, 10.05],
|
||||
[11.4, 10.04],
|
||||
[11.6, 10.06],
|
||||
[11.8, 10.05],
|
||||
].forEach(([x, y]) => {
|
||||
mouse.moveTo(x, y);
|
||||
});
|
||||
mouse.upAt(12, 10.05);
|
||||
|
||||
const freedraw = window.h.elements.at(-1) as any;
|
||||
|
||||
expect(freedraw.points.length).toBeLessThan(5);
|
||||
expect(freedraw.points).toEqual([
|
||||
[0, 0],
|
||||
[2, 0.05],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -201,9 +201,6 @@ export class API {
|
||||
? ExcalidrawTextElement["containerId"]
|
||||
: never;
|
||||
points?: T extends "arrow" | "line" | "freedraw" ? readonly LocalPoint[] : never;
|
||||
strokeShape?: T extends "freedraw"
|
||||
? ExcalidrawFreeDrawElement["strokeShape"]
|
||||
: never;
|
||||
locked?: boolean;
|
||||
fileId?: T extends "image" ? string : never;
|
||||
scale?: T extends "image" ? ExcalidrawImageElement["scale"] : never;
|
||||
@@ -321,7 +318,6 @@ export class API {
|
||||
type: type as "freedraw",
|
||||
simulatePressure: true,
|
||||
points: rest.points,
|
||||
strokeShape: rest.strokeShape ?? appState.currentItemStrokeShape,
|
||||
...base,
|
||||
});
|
||||
break;
|
||||
|
||||
@@ -28,7 +28,6 @@ import type { GlobalPoint, LocalPoint, Radians } from "@excalidraw/math";
|
||||
import type { TransformHandleType } from "@excalidraw/element";
|
||||
import type {
|
||||
ExcalidrawElement,
|
||||
ExcalidrawFreeDrawElement,
|
||||
ExcalidrawLinearElement,
|
||||
ExcalidrawTextElement,
|
||||
ExcalidrawArrowElement,
|
||||
@@ -433,10 +432,8 @@ type DrawingToolName = Exclude<
|
||||
"lock" | "selection" | "eraser" | "lasso"
|
||||
>;
|
||||
|
||||
type Element<T extends DrawingToolName> = T extends "line"
|
||||
type Element<T extends DrawingToolName> = T extends "line" | "freedraw"
|
||||
? ExcalidrawLinearElement
|
||||
: T extends "freedraw"
|
||||
? ExcalidrawFreeDrawElement
|
||||
: T extends "arrow"
|
||||
? ExcalidrawArrowElement
|
||||
: T extends "text"
|
||||
|
||||
@@ -449,29 +449,28 @@ const stripProps = (
|
||||
}, {} as Record<string, any>);
|
||||
|
||||
export const checkpointHistory = (history: History, name: string) => {
|
||||
expect(
|
||||
history.undoStack.map((x) => ({
|
||||
...x,
|
||||
elements: {
|
||||
...x.elements,
|
||||
added: stripProps(x.elements.added, ["seed", "versionNonce"]),
|
||||
removed: stripProps(x.elements.removed, ["seed", "versionNonce"]),
|
||||
updated: stripProps(x.elements.updated, ["seed", "versionNonce"]),
|
||||
},
|
||||
})),
|
||||
).toMatchSnapshot(`[${name}] undo stack`);
|
||||
const normalizeHistoryEntry = (entry: History["undoStack"][number]) => {
|
||||
const { markers, ...rest } = entry;
|
||||
|
||||
expect(
|
||||
history.redoStack.map((x) => ({
|
||||
...x,
|
||||
return {
|
||||
...rest,
|
||||
...(markers ? { markers } : {}),
|
||||
elements: {
|
||||
...x.elements,
|
||||
added: stripProps(x.elements.added, ["seed", "versionNonce"]),
|
||||
removed: stripProps(x.elements.removed, ["seed", "versionNonce"]),
|
||||
updated: stripProps(x.elements.updated, ["seed", "versionNonce"]),
|
||||
...entry.elements,
|
||||
added: stripProps(entry.elements.added, ["seed", "versionNonce"]),
|
||||
removed: stripProps(entry.elements.removed, ["seed", "versionNonce"]),
|
||||
updated: stripProps(entry.elements.updated, ["seed", "versionNonce"]),
|
||||
},
|
||||
})),
|
||||
).toMatchSnapshot(`[${name}] redo stack`);
|
||||
};
|
||||
};
|
||||
|
||||
expect(history.undoStack.map(normalizeHistoryEntry)).toMatchSnapshot(
|
||||
`[${name}] undo stack`,
|
||||
);
|
||||
|
||||
expect(history.redoStack.map(normalizeHistoryEntry)).toMatchSnapshot(
|
||||
`[${name}] redo stack`,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,289 @@
|
||||
import { type StoreDelta } from "@excalidraw/element";
|
||||
|
||||
import type { Delta } from "@excalidraw/element";
|
||||
|
||||
import type { Mutable } from "@excalidraw/common/utility-types";
|
||||
import type {
|
||||
ExcalidrawElement,
|
||||
OrderedExcalidrawElement,
|
||||
SceneElementsMap,
|
||||
} from "@excalidraw/element/types";
|
||||
|
||||
import type {
|
||||
ElementChange,
|
||||
ElementPropName,
|
||||
TouchedElementProps,
|
||||
} from "./types";
|
||||
|
||||
const LEDGER_IGNORED_PROPS = new Set([
|
||||
"version",
|
||||
"versionNonce",
|
||||
"seed",
|
||||
"updated",
|
||||
"index",
|
||||
]);
|
||||
|
||||
export const TX_UNDO_OVERRIDE_IGNORED_PROPS = new Set([
|
||||
"version",
|
||||
"versionNonce",
|
||||
"isDeleted",
|
||||
]);
|
||||
|
||||
type ElementRecord = Record<string, unknown>;
|
||||
export type ElementUpdatedProps = Omit<
|
||||
Partial<OrderedExcalidrawElement>,
|
||||
"id" | "updated" | "seed"
|
||||
>;
|
||||
export type ElementUpdatedPropName = Extract<keyof ElementUpdatedProps, string>;
|
||||
type ElementPropValueMap = ElementUpdatedProps;
|
||||
|
||||
export type ElementUpdatedEntry = Delta<ElementPropValueMap>;
|
||||
export type ElementUpdatedEntryMap = Record<string, ElementUpdatedEntry>;
|
||||
|
||||
const isPlainObject = (value: unknown): value is Record<string, unknown> =>
|
||||
!!value && typeof value === "object" && !Array.isArray(value);
|
||||
|
||||
export const getElementProp = <TProp extends ElementPropName>(
|
||||
element: ExcalidrawElement,
|
||||
prop: TProp,
|
||||
): ExcalidrawElement[TProp] =>
|
||||
(element as ElementRecord)[prop] as ExcalidrawElement[TProp];
|
||||
|
||||
export const setOrderedElementProp = <TProp extends ElementPropName>(
|
||||
element: Mutable<OrderedExcalidrawElement>,
|
||||
prop: TProp,
|
||||
value: OrderedExcalidrawElement[TProp],
|
||||
) => {
|
||||
(element as ElementRecord)[prop] = value;
|
||||
};
|
||||
|
||||
/** Deep equality used by ledger conflict/touched-prop detection. */
|
||||
export const isLedgerValueEqual = (left: unknown, right: unknown): boolean => {
|
||||
if (Object.is(left, right)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Array.isArray(left) && Array.isArray(right)) {
|
||||
if (left.length !== right.length) {
|
||||
return false;
|
||||
}
|
||||
for (let index = 0; index < left.length; index += 1) {
|
||||
if (!isLedgerValueEqual(left[index], right[index])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isPlainObject(left) && isPlainObject(right)) {
|
||||
const leftKeys = Object.keys(left);
|
||||
const rightKeys = Object.keys(right);
|
||||
if (leftKeys.length !== rightKeys.length) {
|
||||
return false;
|
||||
}
|
||||
for (const key of leftKeys) {
|
||||
if (!Object.prototype.hasOwnProperty.call(right, key)) {
|
||||
return false;
|
||||
}
|
||||
if (!isLedgerValueEqual(left[key], right[key])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/** Shallow-copies a scene map. Entries share references with the original. */
|
||||
export const shallowCopySceneElements = (
|
||||
elements: ReadonlyMap<string, ExcalidrawElement>,
|
||||
): SceneElementsMap => new Map(elements) as SceneElementsMap;
|
||||
|
||||
export const createAllTouchedElementProps = (): TouchedElementProps => ({
|
||||
kind: "all",
|
||||
});
|
||||
|
||||
export const createPartialTouchedElementProps = (
|
||||
props: Iterable<ElementPropName> = [],
|
||||
): TouchedElementProps => ({
|
||||
kind: "partial",
|
||||
props: new Set(props),
|
||||
});
|
||||
|
||||
export const hasTouchedProps = (touchedProps: TouchedElementProps): boolean =>
|
||||
touchedProps.kind === "all" || touchedProps.props.size > 0;
|
||||
|
||||
export const touchesWholeElement = (
|
||||
touchedProps: TouchedElementProps,
|
||||
): boolean => touchedProps.kind === "all";
|
||||
|
||||
export const isPartialTouchedProps = (
|
||||
touchedProps: TouchedElementProps,
|
||||
): touchedProps is Extract<TouchedElementProps, { kind: "partial" }> =>
|
||||
touchedProps.kind === "partial";
|
||||
|
||||
export const hasTouchedProp = (
|
||||
touchedProps: TouchedElementProps,
|
||||
prop: ElementPropName,
|
||||
): boolean => touchedProps.kind === "all" || touchedProps.props.has(prop);
|
||||
|
||||
export const mergeTouchedProps = (
|
||||
left: TouchedElementProps,
|
||||
right: TouchedElementProps,
|
||||
): TouchedElementProps => {
|
||||
if (left.kind === "all" || right.kind === "all") {
|
||||
return createAllTouchedElementProps();
|
||||
}
|
||||
|
||||
return createPartialTouchedElementProps([...left.props, ...right.props]);
|
||||
};
|
||||
|
||||
/** Returns changed property names between two element snapshots. */
|
||||
export const collectTouchedProps = (
|
||||
before: ExcalidrawElement | null,
|
||||
after: ExcalidrawElement | null,
|
||||
): TouchedElementProps => {
|
||||
if (!before || !after) {
|
||||
return createAllTouchedElementProps();
|
||||
}
|
||||
|
||||
const touchedProps = new Set<ElementPropName>();
|
||||
const keys = new Set<ElementPropName>([
|
||||
...(Object.keys(before) as ElementPropName[]),
|
||||
...(Object.keys(after) as ElementPropName[]),
|
||||
]);
|
||||
|
||||
for (const key of keys) {
|
||||
if (LEDGER_IGNORED_PROPS.has(key)) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
!isLedgerValueEqual(
|
||||
getElementProp(before, key),
|
||||
getElementProp(after, key),
|
||||
)
|
||||
) {
|
||||
touchedProps.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
return createPartialTouchedElementProps(touchedProps);
|
||||
};
|
||||
|
||||
/** Returns ids whose element snapshot changed between two points in time. */
|
||||
export const collectChangedElementIds = (
|
||||
before: ReadonlyMap<string, ExcalidrawElement>,
|
||||
after: ReadonlyMap<string, ExcalidrawElement>,
|
||||
) => collectElementChanges(before, after).map((change) => change.id);
|
||||
|
||||
export const collectElementChanges = (
|
||||
before: ReadonlyMap<string, ExcalidrawElement>,
|
||||
after: ReadonlyMap<string, ExcalidrawElement>,
|
||||
): ElementChange[] => {
|
||||
const changes: ElementChange[] = [];
|
||||
const candidateIds = new Set<string>([...before.keys(), ...after.keys()]);
|
||||
|
||||
for (const id of candidateIds) {
|
||||
const beforeElement = before.get(id) ?? null;
|
||||
const afterElement = after.get(id) ?? null;
|
||||
const touchedProps = collectTouchedProps(beforeElement, afterElement);
|
||||
if (!hasTouchedProps(touchedProps)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
changes.push({
|
||||
id,
|
||||
before: beforeElement,
|
||||
after: afterElement,
|
||||
touchedProps,
|
||||
});
|
||||
}
|
||||
|
||||
return changes;
|
||||
};
|
||||
|
||||
export const serializeConsumedPropKey = (
|
||||
elementId: string,
|
||||
prop: ElementPropName,
|
||||
) => `${elementId}\u0000${prop}`;
|
||||
|
||||
export const getUpdatedElementEntries = (delta: StoreDelta) =>
|
||||
delta.elements.updated as ElementUpdatedEntryMap;
|
||||
|
||||
export const getElementPropEntries = (props: ElementPropValueMap) =>
|
||||
Object.entries(props) as [
|
||||
ElementUpdatedPropName,
|
||||
ElementUpdatedProps[ElementUpdatedPropName],
|
||||
][];
|
||||
|
||||
export const hasUpdatedElementEntries = (delta: StoreDelta) =>
|
||||
Object.keys(getUpdatedElementEntries(delta)).length > 0;
|
||||
|
||||
export const serializeIntermediateValue = (value: unknown): string => {
|
||||
const serialize = (input: unknown, seen: WeakSet<object>): string => {
|
||||
if (input === null) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
switch (typeof input) {
|
||||
case "undefined":
|
||||
return "undefined";
|
||||
case "boolean":
|
||||
return input ? "boolean:true" : "boolean:false";
|
||||
case "number":
|
||||
if (Number.isNaN(input)) {
|
||||
return "number:NaN";
|
||||
}
|
||||
if (Object.is(input, -0)) {
|
||||
return "number:-0";
|
||||
}
|
||||
return `number:${input}`;
|
||||
case "bigint":
|
||||
return `bigint:${input.toString()}`;
|
||||
case "string":
|
||||
return `string:${JSON.stringify(input)}`;
|
||||
case "symbol":
|
||||
return `symbol:${String(input)}`;
|
||||
case "function":
|
||||
return `function:${input.name}`;
|
||||
case "object":
|
||||
break;
|
||||
default:
|
||||
return `unknown:${String(input)}`;
|
||||
}
|
||||
|
||||
if (Array.isArray(input)) {
|
||||
if (seen.has(input)) {
|
||||
return "[CircularArray]";
|
||||
}
|
||||
seen.add(input);
|
||||
const serialized = `[${input
|
||||
.map((item) => serialize(item, seen))
|
||||
.join(",")}]`;
|
||||
seen.delete(input);
|
||||
return serialized;
|
||||
}
|
||||
|
||||
if (isPlainObject(input)) {
|
||||
if (seen.has(input)) {
|
||||
return "{CircularObject}";
|
||||
}
|
||||
seen.add(input);
|
||||
const serialized = `{${Object.keys(input)
|
||||
.sort()
|
||||
.map((key) => `${JSON.stringify(key)}:${serialize(input[key], seen)}`)
|
||||
.join(",")}}`;
|
||||
seen.delete(input);
|
||||
return serialized;
|
||||
}
|
||||
|
||||
try {
|
||||
return `object:${JSON.stringify(input)}`;
|
||||
} catch {
|
||||
return `object:${Object.prototype.toString.call(input)}`;
|
||||
}
|
||||
};
|
||||
|
||||
return serialize(value, new WeakSet<object>());
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
export { collectChangedElementIds } from "./diff";
|
||||
export { TransactionLedger } from "./ledger";
|
||||
export { Transaction } from "./transaction";
|
||||
export { TransactionManager } from "./manager";
|
||||
|
||||
export type {
|
||||
AppStateResolver,
|
||||
AppStateResolverContext,
|
||||
TransactionElementUpdate,
|
||||
TransactionLedgerEntry,
|
||||
TransactionStatus,
|
||||
TransactionSummary,
|
||||
} from "./types";
|
||||
@@ -0,0 +1,333 @@
|
||||
import { deepCopyElement } from "@excalidraw/element";
|
||||
|
||||
import type { Mutable } from "@excalidraw/common/utility-types";
|
||||
import type {
|
||||
ExcalidrawElement,
|
||||
OrderedExcalidrawElement,
|
||||
SceneElementsMap,
|
||||
} from "@excalidraw/element/types";
|
||||
|
||||
import {
|
||||
collectElementChanges,
|
||||
collectTouchedProps,
|
||||
getElementProp,
|
||||
hasTouchedProps,
|
||||
isLedgerValueEqual,
|
||||
mergeTouchedProps,
|
||||
setOrderedElementProp,
|
||||
shallowCopySceneElements,
|
||||
touchesWholeElement,
|
||||
} from "./diff";
|
||||
|
||||
import type { TransactionLedgerEntry } from "./types";
|
||||
|
||||
/**
|
||||
* Keeps transaction-level scene mutations and materializes synthetic snapshots
|
||||
* for a single durable history commit.
|
||||
*/
|
||||
export class TransactionLedger {
|
||||
private readonly entries = new Map<string, TransactionLedgerEntry>();
|
||||
|
||||
/** Whether the transaction has any net element mutations. */
|
||||
hasEntries() {
|
||||
return this.entries.size > 0;
|
||||
}
|
||||
|
||||
/** Returns the ledger entry for an element, if any. */
|
||||
getEntry(elementId: string): TransactionLedgerEntry | undefined {
|
||||
return this.entries.get(elementId);
|
||||
}
|
||||
|
||||
/** Releases all ledger entries. */
|
||||
clear() {
|
||||
this.entries.clear();
|
||||
}
|
||||
|
||||
/** Records one element mutation step into the ledger. */
|
||||
recordStep(
|
||||
before: ReadonlyMap<string, ExcalidrawElement>,
|
||||
after: ReadonlyMap<string, ExcalidrawElement>,
|
||||
) {
|
||||
for (const change of collectElementChanges(before, after)) {
|
||||
const {
|
||||
id: elementId,
|
||||
before: beforeElement,
|
||||
after: afterElement,
|
||||
touchedProps,
|
||||
} = change;
|
||||
|
||||
const existing = this.entries.get(elementId);
|
||||
if (!existing) {
|
||||
this.entries.set(elementId, {
|
||||
baselineElement: beforeElement
|
||||
? deepCopyElement(beforeElement)
|
||||
: null,
|
||||
targetElement: afterElement ? deepCopyElement(afterElement) : null,
|
||||
touchedProps,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
existing.targetElement = afterElement
|
||||
? deepCopyElement(afterElement)
|
||||
: null;
|
||||
existing.touchedProps = mergeTouchedProps(
|
||||
existing.touchedProps,
|
||||
touchedProps,
|
||||
);
|
||||
|
||||
// Created then deleted inside one transaction leaves no durable footprint.
|
||||
if (!existing.baselineElement && !existing.targetElement) {
|
||||
this.entries.delete(elementId);
|
||||
continue;
|
||||
}
|
||||
if (!existing.baselineElement && existing.targetElement?.isDeleted) {
|
||||
this.entries.delete(elementId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds synthetic element before/after snapshots with a fixed
|
||||
* "live-wins-per-prop" strategy.
|
||||
*/
|
||||
buildSyntheticSnapshots(live: ReadonlyMap<string, ExcalidrawElement>) {
|
||||
// Shallow copy — untouched elements stay as live references.
|
||||
// Only elements mutated in-place (prop-level updates) are deep-copied below.
|
||||
const elementsBefore = shallowCopySceneElements(live);
|
||||
const elementsAfter = shallowCopySceneElements(live);
|
||||
|
||||
for (const [elementId, entry] of this.entries) {
|
||||
this.reconcileEntrySnapshots(
|
||||
elementId,
|
||||
entry,
|
||||
live,
|
||||
elementsBefore,
|
||||
elementsAfter,
|
||||
);
|
||||
}
|
||||
|
||||
return { elementsBefore, elementsAfter };
|
||||
}
|
||||
|
||||
private reconcileEntrySnapshots(
|
||||
elementId: string,
|
||||
entry: TransactionLedgerEntry,
|
||||
live: ReadonlyMap<string, ExcalidrawElement>,
|
||||
elementsBefore: SceneElementsMap,
|
||||
elementsAfter: SceneElementsMap,
|
||||
) {
|
||||
if (!entry.baselineElement) {
|
||||
this.applyCreatedElementSnapshots(
|
||||
elementId,
|
||||
entry.targetElement,
|
||||
live,
|
||||
elementsBefore,
|
||||
elementsAfter,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!entry.targetElement) {
|
||||
this.applyDeletedElementSnapshots(
|
||||
elementId,
|
||||
entry.baselineElement,
|
||||
live,
|
||||
elementsBefore,
|
||||
elementsAfter,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
this.applyUpdatedElementSnapshots(
|
||||
elementId,
|
||||
entry,
|
||||
live,
|
||||
elementsBefore,
|
||||
elementsAfter,
|
||||
);
|
||||
}
|
||||
|
||||
private applyCreatedElementSnapshots(
|
||||
elementId: string,
|
||||
targetElement: ExcalidrawElement | null,
|
||||
live: ReadonlyMap<string, ExcalidrawElement>,
|
||||
elementsBefore: SceneElementsMap,
|
||||
elementsAfter: SceneElementsMap,
|
||||
) {
|
||||
if (!targetElement) {
|
||||
return;
|
||||
}
|
||||
|
||||
const liveElement = live.get(elementId) ?? null;
|
||||
if (
|
||||
!liveElement ||
|
||||
liveElement.isDeleted ||
|
||||
hasTouchedProps(collectTouchedProps(targetElement, liveElement))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
elementsBefore.delete(elementId);
|
||||
elementsAfter.set(
|
||||
elementId,
|
||||
deepCopyElement(targetElement) as OrderedExcalidrawElement,
|
||||
);
|
||||
}
|
||||
|
||||
private applyDeletedElementSnapshots(
|
||||
elementId: string,
|
||||
baselineElement: ExcalidrawElement,
|
||||
live: ReadonlyMap<string, ExcalidrawElement>,
|
||||
elementsBefore: SceneElementsMap,
|
||||
elementsAfter: SceneElementsMap,
|
||||
) {
|
||||
const liveElement = live.get(elementId) ?? null;
|
||||
if (liveElement && !liveElement.isDeleted) {
|
||||
return;
|
||||
}
|
||||
|
||||
elementsBefore.set(
|
||||
elementId,
|
||||
deepCopyElement(baselineElement) as OrderedExcalidrawElement,
|
||||
);
|
||||
elementsAfter.delete(elementId);
|
||||
}
|
||||
|
||||
private applyUpdatedElementSnapshots(
|
||||
elementId: string,
|
||||
entry: TransactionLedgerEntry,
|
||||
live: ReadonlyMap<string, ExcalidrawElement>,
|
||||
elementsBefore: SceneElementsMap,
|
||||
elementsAfter: SceneElementsMap,
|
||||
) {
|
||||
const liveElement = live.get(elementId) ?? null;
|
||||
const targetElement = entry.targetElement;
|
||||
const baselineElement = entry.baselineElement;
|
||||
const beforeElement = elementsBefore.get(elementId);
|
||||
const afterElement = elementsAfter.get(elementId);
|
||||
|
||||
if (
|
||||
!liveElement ||
|
||||
!baselineElement ||
|
||||
!targetElement ||
|
||||
!beforeElement ||
|
||||
!afterElement
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (touchesWholeElement(entry.touchedProps)) {
|
||||
this.applyWholeElementSnapshots(
|
||||
elementId,
|
||||
baselineElement,
|
||||
targetElement,
|
||||
liveElement,
|
||||
elementsBefore,
|
||||
elementsAfter,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
this.applyPerPropSnapshots({
|
||||
entry,
|
||||
liveElement,
|
||||
baselineElement,
|
||||
targetElement,
|
||||
beforeElement,
|
||||
afterElement,
|
||||
elementId,
|
||||
elementsBefore,
|
||||
elementsAfter,
|
||||
});
|
||||
}
|
||||
|
||||
private applyWholeElementSnapshots(
|
||||
elementId: string,
|
||||
baselineElement: ExcalidrawElement,
|
||||
targetElement: ExcalidrawElement,
|
||||
liveElement: ExcalidrawElement,
|
||||
elementsBefore: SceneElementsMap,
|
||||
elementsAfter: SceneElementsMap,
|
||||
) {
|
||||
const hasLiveConflict = hasTouchedProps(
|
||||
collectTouchedProps(targetElement, liveElement),
|
||||
);
|
||||
if (hasLiveConflict) {
|
||||
return;
|
||||
}
|
||||
|
||||
elementsBefore.set(
|
||||
elementId,
|
||||
deepCopyElement(baselineElement) as OrderedExcalidrawElement,
|
||||
);
|
||||
elementsAfter.set(
|
||||
elementId,
|
||||
deepCopyElement(targetElement) as OrderedExcalidrawElement,
|
||||
);
|
||||
}
|
||||
|
||||
private applyPerPropSnapshots(args: {
|
||||
entry: TransactionLedgerEntry;
|
||||
liveElement: ExcalidrawElement;
|
||||
baselineElement: ExcalidrawElement;
|
||||
targetElement: ExcalidrawElement;
|
||||
beforeElement: ExcalidrawElement;
|
||||
afterElement: ExcalidrawElement;
|
||||
elementId: string;
|
||||
elementsBefore: SceneElementsMap;
|
||||
elementsAfter: SceneElementsMap;
|
||||
}) {
|
||||
const {
|
||||
entry,
|
||||
liveElement,
|
||||
baselineElement,
|
||||
targetElement,
|
||||
beforeElement,
|
||||
afterElement,
|
||||
elementId,
|
||||
elementsBefore,
|
||||
elementsAfter,
|
||||
} = args;
|
||||
|
||||
// Deep-copy before mutating so we never touch live elements.
|
||||
const mutableBefore = deepCopyElement(
|
||||
beforeElement,
|
||||
) as Mutable<OrderedExcalidrawElement>;
|
||||
const mutableAfter = deepCopyElement(
|
||||
afterElement,
|
||||
) as Mutable<OrderedExcalidrawElement>;
|
||||
elementsBefore.set(elementId, mutableBefore as OrderedExcalidrawElement);
|
||||
elementsAfter.set(elementId, mutableAfter as OrderedExcalidrawElement);
|
||||
|
||||
if (entry.touchedProps.kind !== "partial") {
|
||||
return;
|
||||
}
|
||||
|
||||
let appliedProps = 0;
|
||||
for (const prop of entry.touchedProps.props) {
|
||||
const liveValue = getElementProp(liveElement, prop);
|
||||
const targetValue = getElementProp(targetElement, prop);
|
||||
if (!isLedgerValueEqual(liveValue, targetValue)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
setOrderedElementProp(
|
||||
mutableBefore,
|
||||
prop,
|
||||
getElementProp(baselineElement, prop),
|
||||
);
|
||||
setOrderedElementProp(mutableAfter, prop, targetValue);
|
||||
appliedProps += 1;
|
||||
}
|
||||
|
||||
if (appliedProps === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
mutableBefore.version = baselineElement.version;
|
||||
mutableBefore.versionNonce = baselineElement.versionNonce;
|
||||
mutableAfter.version = targetElement.version;
|
||||
mutableAfter.versionNonce = targetElement.versionNonce;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
import {
|
||||
Delta,
|
||||
ElementsDelta,
|
||||
mergeStoreDeltaMarkers,
|
||||
type StoreDelta,
|
||||
type TxUndoOverride,
|
||||
} from "@excalidraw/element";
|
||||
|
||||
import type { Mutable } from "@excalidraw/common/utility-types";
|
||||
|
||||
import {
|
||||
HistoryDelta,
|
||||
type HistoryBeforeRecordListener,
|
||||
type HistoryEffectiveDeltaResolverContext,
|
||||
} from "../history";
|
||||
|
||||
import {
|
||||
type ElementUpdatedPropName,
|
||||
getUpdatedElementEntries,
|
||||
hasUpdatedElementEntries,
|
||||
isLedgerValueEqual,
|
||||
type ElementUpdatedProps,
|
||||
type ElementUpdatedEntryMap,
|
||||
} from "./diff";
|
||||
import { Transaction } from "./transaction";
|
||||
|
||||
import type { AppClassProperties } from "../types";
|
||||
import type { TransactionStatus } from "./types";
|
||||
|
||||
type TransactionRecord = {
|
||||
tx: Transaction | null;
|
||||
phase: TransactionStatus;
|
||||
};
|
||||
type TransactionHistoryBridge = {
|
||||
onBeforeRecord: (callback: HistoryBeforeRecordListener) => () => void;
|
||||
setEffectiveDeltaResolver: (
|
||||
resolver:
|
||||
| ((
|
||||
delta: HistoryDelta,
|
||||
context: HistoryEffectiveDeltaResolverContext,
|
||||
) => HistoryDelta)
|
||||
| null,
|
||||
) => void;
|
||||
};
|
||||
|
||||
type MutableElementUpdatedProps = Mutable<ElementUpdatedProps>;
|
||||
|
||||
const setElementUpdatedOverride = (
|
||||
overrides: MutableElementUpdatedProps,
|
||||
prop: ElementUpdatedPropName,
|
||||
value: unknown,
|
||||
) => {
|
||||
(overrides as Record<ElementUpdatedPropName, unknown>)[prop] = value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Thin factory that holds the app reference and creates Transaction instances.
|
||||
*/
|
||||
export class TransactionManager {
|
||||
private readonly app: AppClassProperties;
|
||||
/**
|
||||
* Single authoritative lifecycle registry for transactions.
|
||||
*
|
||||
* We retain ended/canceled metadata after the tx object is released because
|
||||
* history markers only persist `txId`; undo/redo still needs to resolve
|
||||
* whether a tx was active or already ended when applying effective deltas.
|
||||
*/
|
||||
private readonly transactionRecords = new Map<string, TransactionRecord>();
|
||||
/**
|
||||
* Active transaction ids ordered by most-recent registration first.
|
||||
* This preserves deterministic priority when multiple active txs overlap on
|
||||
* the same element+prop and compete to reserve override markers.
|
||||
*/
|
||||
private readonly activeTransactionIdsByPriority: string[] = [];
|
||||
private detachBeforeRecordHook: (() => void) | null = null;
|
||||
|
||||
constructor(app: AppClassProperties) {
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds transaction bookkeeping to history lifecycle hooks.
|
||||
* Call once during app initialization.
|
||||
*/
|
||||
attachHistory(history: TransactionHistoryBridge) {
|
||||
this.detachBeforeRecordHook?.();
|
||||
history.setEffectiveDeltaResolver((delta, context) =>
|
||||
this.resolveEffectiveDelta(delta, context),
|
||||
);
|
||||
this.detachBeforeRecordHook = history.onBeforeRecord((delta) =>
|
||||
this.onDurableIncrement(delta),
|
||||
);
|
||||
}
|
||||
|
||||
private removeActiveTransactionId(txId: string) {
|
||||
const txIndex = this.activeTransactionIdsByPriority.indexOf(txId);
|
||||
if (txIndex >= 0) {
|
||||
this.activeTransactionIdsByPriority.splice(txIndex, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private getRequiredTransactionRecord(txId: string): TransactionRecord {
|
||||
const record = this.transactionRecords.get(txId);
|
||||
if (!record) {
|
||||
throw new Error(`Unknown transaction: ${txId}`);
|
||||
}
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
registerTransaction(tx: Transaction) {
|
||||
this.transactionRecords.set(tx.id, {
|
||||
tx,
|
||||
phase: "active",
|
||||
});
|
||||
this.activeTransactionIdsByPriority.unshift(tx.id);
|
||||
}
|
||||
|
||||
detachTransactionInstance(txId: string) {
|
||||
const record = this.getRequiredTransactionRecord(txId);
|
||||
record.tx = null;
|
||||
this.removeActiveTransactionId(txId);
|
||||
}
|
||||
|
||||
getStatus(txId: string): TransactionStatus {
|
||||
return this.getRequiredTransactionRecord(txId).phase;
|
||||
}
|
||||
|
||||
private markTransactionFinished(
|
||||
txId: string,
|
||||
phase: Exclude<TransactionStatus, "active">,
|
||||
): TransactionStatus {
|
||||
const record = this.getRequiredTransactionRecord(txId);
|
||||
if (record.phase !== "active") {
|
||||
return record.phase;
|
||||
}
|
||||
|
||||
record.phase = phase;
|
||||
this.removeActiveTransactionId(txId);
|
||||
return record.phase;
|
||||
}
|
||||
|
||||
markTransactionCommitted(txId: string): TransactionStatus {
|
||||
return this.markTransactionFinished(txId, "committed");
|
||||
}
|
||||
|
||||
markTransactionCanceled(txId: string): TransactionStatus {
|
||||
return this.markTransactionFinished(txId, "canceled");
|
||||
}
|
||||
|
||||
onDurableIncrement(delta: StoreDelta) {
|
||||
if (this.activeTransactionIdsByPriority.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (!hasUpdatedElementEntries(delta)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const txUndoOverrides = this.collectUndoOverrides(delta);
|
||||
if (txUndoOverrides.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
mergeStoreDeltaMarkers(delta, { txUndoOverrides });
|
||||
}
|
||||
|
||||
private collectUndoOverrides(delta: StoreDelta): TxUndoOverride[] {
|
||||
const overrides: TxUndoOverride[] = [];
|
||||
const reservedConsumedKeys = new Set<string>();
|
||||
|
||||
for (const txId of this.activeTransactionIdsByPriority) {
|
||||
const record = this.transactionRecords.get(txId);
|
||||
if (!record || record.phase !== "active" || !record.tx) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const txOverrides = record.tx.collectUndoOverridesForDelta(
|
||||
delta,
|
||||
reservedConsumedKeys,
|
||||
);
|
||||
|
||||
for (const override of txOverrides) {
|
||||
if (reservedConsumedKeys.has(override.consumedKey)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
reservedConsumedKeys.add(override.consumedKey);
|
||||
overrides.push(override);
|
||||
}
|
||||
}
|
||||
|
||||
return overrides;
|
||||
}
|
||||
|
||||
private resolveEffectiveDelta(
|
||||
delta: HistoryDelta,
|
||||
_context: HistoryEffectiveDeltaResolverContext,
|
||||
): HistoryDelta {
|
||||
const txUndoOverrides = delta.markers?.txUndoOverrides;
|
||||
if (!txUndoOverrides || txUndoOverrides.length === 0) {
|
||||
return delta;
|
||||
}
|
||||
|
||||
const updatedEntries = getUpdatedElementEntries(delta);
|
||||
const insertedOverridesByElement = new Map<
|
||||
string,
|
||||
MutableElementUpdatedProps
|
||||
>();
|
||||
|
||||
for (const override of txUndoOverrides) {
|
||||
if (!this.shouldApplyUndoOverride(override.txId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const currentEntry = updatedEntries[override.elementId];
|
||||
if (!currentEntry) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const prop = override.prop as ElementUpdatedPropName;
|
||||
const currentInsertedValue = currentEntry.inserted[prop];
|
||||
if (
|
||||
!isLedgerValueEqual(
|
||||
currentInsertedValue,
|
||||
override.expectedInsertedValue,
|
||||
)
|
||||
) {
|
||||
// Guard against over-applying once the delta has already evolved.
|
||||
continue;
|
||||
}
|
||||
|
||||
const elementOverrides = insertedOverridesByElement.get(
|
||||
override.elementId,
|
||||
);
|
||||
if (elementOverrides) {
|
||||
setElementUpdatedOverride(
|
||||
elementOverrides,
|
||||
prop,
|
||||
override.preTxBaselineValue,
|
||||
);
|
||||
} else {
|
||||
const nextOverrides: MutableElementUpdatedProps = {};
|
||||
setElementUpdatedOverride(
|
||||
nextOverrides,
|
||||
prop,
|
||||
override.preTxBaselineValue,
|
||||
);
|
||||
insertedOverridesByElement.set(override.elementId, nextOverrides);
|
||||
}
|
||||
}
|
||||
|
||||
if (insertedOverridesByElement.size === 0) {
|
||||
return delta;
|
||||
}
|
||||
|
||||
const nextUpdatedEntries: ElementUpdatedEntryMap = {
|
||||
...updatedEntries,
|
||||
};
|
||||
for (const [elementId, insertedOverrides] of insertedOverridesByElement) {
|
||||
const currentEntry = updatedEntries[elementId];
|
||||
if (!currentEntry) {
|
||||
continue;
|
||||
}
|
||||
|
||||
nextUpdatedEntries[elementId] = Delta.create(
|
||||
{ ...currentEntry.deleted },
|
||||
{ ...currentEntry.inserted, ...insertedOverrides },
|
||||
);
|
||||
}
|
||||
|
||||
const effectiveElements = ElementsDelta.create(
|
||||
delta.elements.added,
|
||||
delta.elements.removed,
|
||||
nextUpdatedEntries,
|
||||
);
|
||||
|
||||
return HistoryDelta.create(effectiveElements, delta.appState, {
|
||||
id: delta.id,
|
||||
markers: delta.markers,
|
||||
}) as HistoryDelta;
|
||||
}
|
||||
|
||||
private shouldApplyUndoOverride(txId: string): boolean {
|
||||
const record = this.transactionRecords.get(txId);
|
||||
return !!record && record.phase !== "active";
|
||||
}
|
||||
|
||||
create(): Transaction {
|
||||
return new Transaction(this.app, this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
import { randomId } from "@excalidraw/common";
|
||||
import {
|
||||
CaptureUpdateAction,
|
||||
newElementWith,
|
||||
type ElementUpdate,
|
||||
type StoreDelta,
|
||||
type TxUndoOverride,
|
||||
} from "@excalidraw/element";
|
||||
|
||||
import { shallowCopySceneElements } from "./diff";
|
||||
import { TransactionLedger } from "./ledger";
|
||||
|
||||
import {
|
||||
type AppStateResolver,
|
||||
type AppStateResolverContext,
|
||||
type TransactionElementOfType,
|
||||
type TransactionElementUpdate,
|
||||
type TransactionStatus,
|
||||
type TransactionSummary,
|
||||
} from "./types";
|
||||
import { TxUndoOverridePlanner } from "./undoOverridePlanner";
|
||||
|
||||
import type { TransactionManager } from "./manager";
|
||||
import type {
|
||||
AppClassProperties,
|
||||
AppState,
|
||||
ObservedAppState,
|
||||
SceneData,
|
||||
} from "../types";
|
||||
|
||||
type CommitOptions = {
|
||||
resolveAppState?: AppStateResolver;
|
||||
};
|
||||
|
||||
/**
|
||||
* A transaction that records mutations via `updateScene(NEVER)` and commits
|
||||
* a single synthetic durable history entry at the end.
|
||||
*/
|
||||
export class Transaction {
|
||||
public readonly id = `tx-${randomId()}`;
|
||||
|
||||
private readonly app: AppClassProperties;
|
||||
private readonly manager: TransactionManager;
|
||||
private readonly ledger = new TransactionLedger();
|
||||
private readonly undoOverridePlanner = new TxUndoOverridePlanner();
|
||||
private readonly initialAppState: Partial<ObservedAppState>;
|
||||
|
||||
private accumulatedAppState: Record<string, unknown> = {};
|
||||
private cachedSummary: TransactionSummary | null = null;
|
||||
|
||||
constructor(app: AppClassProperties, manager: TransactionManager) {
|
||||
this.app = app;
|
||||
this.manager = manager;
|
||||
this.initialAppState = { ...app.store.snapshot.appState };
|
||||
this.manager.registerTransaction(this);
|
||||
}
|
||||
|
||||
get status(): TransactionStatus {
|
||||
return this.manager.getStatus(this.id);
|
||||
}
|
||||
|
||||
private assertActive(action: string): void {
|
||||
const status = this.status;
|
||||
if (status !== "active") {
|
||||
throw new Error(
|
||||
`Cannot ${action} — transaction ${this.id} is already ${status}.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private closeTransaction() {
|
||||
this.manager.detachTransactionInstance(this.id);
|
||||
this.undoOverridePlanner.clear();
|
||||
}
|
||||
|
||||
public collectUndoOverridesForDelta(
|
||||
delta: StoreDelta,
|
||||
reservedConsumedKeys: Set<string>,
|
||||
): TxUndoOverride[] {
|
||||
if (this.status !== "active") {
|
||||
return [];
|
||||
}
|
||||
|
||||
const candidates =
|
||||
this.undoOverridePlanner.collectCandidatesForDurableDelta(
|
||||
delta,
|
||||
(elementId) => this.ledger.getEntry(elementId),
|
||||
reservedConsumedKeys,
|
||||
);
|
||||
|
||||
if (candidates.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const overrides: TxUndoOverride[] = [];
|
||||
for (const candidate of candidates) {
|
||||
this.undoOverridePlanner.markConsumed(candidate.consumedKey);
|
||||
overrides.push({
|
||||
txId: this.id,
|
||||
...candidate,
|
||||
});
|
||||
}
|
||||
|
||||
return overrides;
|
||||
}
|
||||
|
||||
updateScene<K extends keyof AppState>(data: {
|
||||
elements?: SceneData["elements"];
|
||||
appState?: Pick<AppState, K> | null;
|
||||
}): void {
|
||||
this.assertActive("updateScene");
|
||||
|
||||
// Snapshot before (shallow copy — replaceAllElements mutates the map in-place)
|
||||
const before = shallowCopySceneElements(
|
||||
this.app.scene.getElementsMapIncludingDeleted(),
|
||||
);
|
||||
|
||||
// Apply through the real updateScene with NEVER.
|
||||
this.app.api.updateScene({
|
||||
elements: data.elements,
|
||||
appState: data.appState,
|
||||
captureUpdate: CaptureUpdateAction.NEVER,
|
||||
});
|
||||
|
||||
// Snapshot after
|
||||
const after = this.app.scene.getElementsMapIncludingDeleted();
|
||||
|
||||
this.undoOverridePlanner.recordStep(before, after);
|
||||
|
||||
// Record element diff into ledger
|
||||
this.ledger.recordStep(before, after);
|
||||
|
||||
// Accumulate appState intent
|
||||
if (data.appState) {
|
||||
this.accumulatedAppState = {
|
||||
...this.accumulatedAppState,
|
||||
...(data.appState as Record<string, unknown>),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Partial element updates convenience API.
|
||||
*
|
||||
* Example:
|
||||
* tx.updateElements({
|
||||
* elements: [
|
||||
* { id: "a", type: "rectangle", updates: { strokeColor: "#f00" } },
|
||||
* { id: "b", type: "rectangle", updates: { x: 10, y: 20 } },
|
||||
* ],
|
||||
* })
|
||||
*/
|
||||
updateElements<K extends keyof AppState>(data: {
|
||||
elements: readonly TransactionElementUpdate[];
|
||||
appState?: Pick<AppState, K> | null;
|
||||
}): void {
|
||||
const updatesById = new Map<string, TransactionElementUpdate>();
|
||||
|
||||
for (const update of data.elements) {
|
||||
updatesById.set(update.id, update);
|
||||
}
|
||||
|
||||
if (updatesById.size === 0) {
|
||||
this.updateScene({ appState: data.appState });
|
||||
return;
|
||||
}
|
||||
|
||||
const nextElements = this.app.scene
|
||||
.getElementsIncludingDeleted()
|
||||
.map((element) => {
|
||||
const update = updatesById.get(element.id);
|
||||
if (!update) {
|
||||
return element;
|
||||
}
|
||||
if (element.type !== update.type) {
|
||||
throw new Error(
|
||||
`Cannot apply tx.updateElements update for "${update.id}": expected "${update.type}", got "${element.type}".`,
|
||||
);
|
||||
}
|
||||
|
||||
type MatchingElement = TransactionElementOfType<typeof update.type>;
|
||||
return newElementWith(
|
||||
element as MatchingElement,
|
||||
update.updates as ElementUpdate<MatchingElement>,
|
||||
);
|
||||
});
|
||||
|
||||
this.updateScene({
|
||||
elements: nextElements,
|
||||
appState: data.appState,
|
||||
});
|
||||
}
|
||||
|
||||
commit(options?: CommitOptions): TransactionSummary {
|
||||
if (this.cachedSummary) {
|
||||
return this.cachedSummary;
|
||||
}
|
||||
|
||||
this.manager.markTransactionCommitted(this.id);
|
||||
let historyCommitted = false;
|
||||
try {
|
||||
historyCommitted = this.hasPendingWork()
|
||||
? this.commitHistoryEntry(options)
|
||||
: false;
|
||||
} finally {
|
||||
this.closeTransaction();
|
||||
}
|
||||
|
||||
const status = this.status;
|
||||
this.cachedSummary = {
|
||||
id: this.id,
|
||||
status,
|
||||
historyCommitted,
|
||||
};
|
||||
this.ledger.clear();
|
||||
return this.cachedSummary;
|
||||
}
|
||||
|
||||
private hasPendingWork() {
|
||||
return this.ledger.hasEntries() || this.hasAccumulatedAppStateIntent();
|
||||
}
|
||||
|
||||
private hasAccumulatedAppStateIntent() {
|
||||
return Object.keys(this.accumulatedAppState).length > 0;
|
||||
}
|
||||
|
||||
private commitHistoryEntry(options?: CommitOptions) {
|
||||
const liveMap = this.app.scene.getElementsMapIncludingDeleted();
|
||||
const { elementsBefore, elementsAfter } =
|
||||
this.ledger.buildSyntheticSnapshots(liveMap);
|
||||
|
||||
const appStateDelta = this.resolveCommitAppStateDelta(options);
|
||||
|
||||
return this.app.store.commitSyntheticIncrement({
|
||||
logicalBefore: { elements: elementsBefore },
|
||||
logicalAfter: {
|
||||
elements: elementsAfter,
|
||||
appState: appStateDelta,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private resolveCommitAppStateDelta(
|
||||
options?: CommitOptions,
|
||||
): Partial<ObservedAppState> | undefined {
|
||||
if (!this.hasAccumulatedAppStateIntent()) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!options?.resolveAppState) {
|
||||
return this.accumulatedAppState as Partial<ObservedAppState>;
|
||||
}
|
||||
|
||||
const context: AppStateResolverContext = {
|
||||
initial: this.initialAppState,
|
||||
accumulated: this.accumulatedAppState as Partial<ObservedAppState>,
|
||||
live: { ...this.app.store.snapshot.appState },
|
||||
};
|
||||
const resolved = options.resolveAppState(context);
|
||||
|
||||
if (!resolved || Object.keys(resolved).length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
cancel(): TransactionSummary {
|
||||
if (this.cachedSummary) {
|
||||
return this.cachedSummary;
|
||||
}
|
||||
|
||||
if (this.status === "active") {
|
||||
this.manager.markTransactionCanceled(this.id);
|
||||
}
|
||||
|
||||
this.closeTransaction();
|
||||
const status = this.status;
|
||||
this.cachedSummary = {
|
||||
id: this.id,
|
||||
status,
|
||||
historyCommitted: false,
|
||||
};
|
||||
this.ledger.clear();
|
||||
return this.cachedSummary;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import type { ElementUpdate } from "@excalidraw/element";
|
||||
|
||||
import type {
|
||||
ExcalidrawElement,
|
||||
ExcalidrawNonSelectionElement,
|
||||
} from "@excalidraw/element/types";
|
||||
|
||||
import type { ObservedAppState } from "../types";
|
||||
|
||||
export type ElementPropName = Extract<keyof ExcalidrawElement, string>;
|
||||
|
||||
export type TouchedElementProps =
|
||||
| { kind: "all" }
|
||||
| { kind: "partial"; props: Set<ElementPropName> };
|
||||
|
||||
export type ElementChange = {
|
||||
id: ExcalidrawElement["id"];
|
||||
before: ExcalidrawElement | null;
|
||||
after: ExcalidrawElement | null;
|
||||
touchedProps: TouchedElementProps;
|
||||
};
|
||||
|
||||
/** Per-element ledger record captured during a transaction session. */
|
||||
export type TransactionLedgerEntry = {
|
||||
baselineElement: ExcalidrawElement | null;
|
||||
targetElement: ExcalidrawElement | null;
|
||||
touchedProps: TouchedElementProps;
|
||||
};
|
||||
|
||||
/** Lifecycle state of a transaction. */
|
||||
export type TransactionStatus = "active" | "committed" | "canceled";
|
||||
|
||||
/** Per-element partial patch used by tx.updateElements(). */
|
||||
export type TransactionUpdatableElementType =
|
||||
ExcalidrawNonSelectionElement["type"];
|
||||
|
||||
export type TransactionElementOfType<
|
||||
TType extends TransactionUpdatableElementType,
|
||||
> = Extract<ExcalidrawNonSelectionElement, { type: TType }>;
|
||||
|
||||
export type TransactionElementUpdate<
|
||||
TType extends TransactionUpdatableElementType = TransactionUpdatableElementType,
|
||||
> = TType extends TransactionUpdatableElementType
|
||||
? {
|
||||
id: ExcalidrawElement["id"];
|
||||
type: TType;
|
||||
updates: ElementUpdate<TransactionElementOfType<TType>>;
|
||||
}
|
||||
: never;
|
||||
|
||||
/** Final summary returned when a transaction is committed or canceled. */
|
||||
export type TransactionSummary = {
|
||||
id: string;
|
||||
status: TransactionStatus;
|
||||
historyCommitted: boolean;
|
||||
};
|
||||
|
||||
/** Three-way appState context provided to the resolver at commit time. */
|
||||
export type AppStateResolverContext = {
|
||||
/** AppState snapshot captured when the transaction was created. */
|
||||
initial: Partial<ObservedAppState>;
|
||||
/** Merged appState intent from all updateScene calls during the transaction. */
|
||||
accumulated: Partial<ObservedAppState>;
|
||||
/** Current live appState at commit time. */
|
||||
live: Partial<ObservedAppState>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Caller-provided resolver that determines which appState changes are
|
||||
* recorded in the history entry.
|
||||
*
|
||||
* Unlike elements — where per-property conflict detection works because
|
||||
* element properties are largely independent — appState keys are often
|
||||
* interdependent (e.g. selectedElementIds ↔ selectedGroupIds must stay
|
||||
* consistent). The correct merge strategy therefore depends on the
|
||||
* caller's semantic context, not on a generic policy.
|
||||
*
|
||||
* Return the appState delta to record in history, or undefined to skip
|
||||
* appState changes entirely.
|
||||
*/
|
||||
export type AppStateResolver = (
|
||||
context: AppStateResolverContext,
|
||||
) => Partial<ObservedAppState> | undefined;
|
||||
@@ -0,0 +1,283 @@
|
||||
import type { StoreDelta, TxUndoOverride } from "@excalidraw/element";
|
||||
|
||||
import type { ExcalidrawElement } from "@excalidraw/element/types";
|
||||
|
||||
import {
|
||||
TX_UNDO_OVERRIDE_IGNORED_PROPS,
|
||||
collectElementChanges,
|
||||
getElementProp,
|
||||
getElementPropEntries,
|
||||
getUpdatedElementEntries,
|
||||
hasTouchedProp,
|
||||
isPartialTouchedProps,
|
||||
isLedgerValueEqual,
|
||||
serializeConsumedPropKey,
|
||||
serializeIntermediateValue,
|
||||
touchesWholeElement,
|
||||
type ElementUpdatedEntry,
|
||||
} from "./diff";
|
||||
|
||||
import type {
|
||||
ElementPropName,
|
||||
TouchedElementProps,
|
||||
TransactionLedgerEntry,
|
||||
} from "./types";
|
||||
|
||||
type TxUndoOverrideCandidate = Omit<TxUndoOverride, "txId">;
|
||||
|
||||
/**
|
||||
* Per-element-prop history of tx intermediate values.
|
||||
*
|
||||
* We keep:
|
||||
* - the full sequence for exact deep-equality fallback
|
||||
* - a serialized signature set for fast negative lookups
|
||||
* - the latest value for the most common positive lookup path
|
||||
*/
|
||||
class TxIntermediateValueHistory {
|
||||
private readonly values: unknown[] = [];
|
||||
private readonly signatures = new Set<string>();
|
||||
private latestValue: unknown;
|
||||
private hasLatestValue = false;
|
||||
|
||||
/** Appends a new intermediate value, skipping consecutive duplicates. */
|
||||
add(value: unknown) {
|
||||
if (this.hasLatestValue && isLedgerValueEqual(this.latestValue, value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.values.push(value);
|
||||
this.signatures.add(serializeIntermediateValue(value));
|
||||
this.latestValue = value;
|
||||
this.hasLatestValue = true;
|
||||
}
|
||||
|
||||
/** Returns whether the candidate appeared in this tx prop history. */
|
||||
contains(candidate: unknown) {
|
||||
const candidateSignature = serializeIntermediateValue(candidate);
|
||||
if (!this.signatures.has(candidateSignature)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
this.hasLatestValue &&
|
||||
isLedgerValueEqual(this.latestValue, candidate)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.values.some((value) => isLedgerValueEqual(value, candidate));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks tx intermediate values and computes undo baseline override markers
|
||||
* for durable user deltas recorded while tx is active.
|
||||
*
|
||||
* High-level flow:
|
||||
* 1. `recordStep()` observes every in-tx scene mutation and records the
|
||||
* intermediate value reached by each touched element prop.
|
||||
* 2. When a durable user delta is about to be recorded,
|
||||
* `collectCandidatesForDurableDelta()` checks whether that delta's
|
||||
* deleted-baseline values match any tx intermediate value.
|
||||
* 3. If they do, we emit override candidates so undo can restore the pre-tx
|
||||
* baseline once the tx has ended.
|
||||
* 4. `markConsumed()` ensures only the first polluted durable entry for a
|
||||
* given element+prop gets patched; later user actions keep their own
|
||||
* action-local undo baseline.
|
||||
*/
|
||||
export class TxUndoOverridePlanner {
|
||||
private readonly intermediateValuesByElementProp = new Map<
|
||||
string,
|
||||
Map<ElementPropName, TxIntermediateValueHistory>
|
||||
>();
|
||||
private readonly consumedOverridePropKeys = new Set<string>();
|
||||
|
||||
/** Resets planner state when the transaction finishes. */
|
||||
clear() {
|
||||
this.consumedOverridePropKeys.clear();
|
||||
this.intermediateValuesByElementProp.clear();
|
||||
}
|
||||
|
||||
/** Records per-prop intermediate values reached by one in-tx scene step. */
|
||||
recordStep(
|
||||
before: ReadonlyMap<string, ExcalidrawElement>,
|
||||
after: ReadonlyMap<string, ExcalidrawElement>,
|
||||
) {
|
||||
for (const change of collectElementChanges(before, after)) {
|
||||
const { id: elementId, after: afterElement, touchedProps } = change;
|
||||
|
||||
if (!afterElement || !isPartialTouchedProps(touchedProps)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const prop of touchedProps.props) {
|
||||
this.recordIntermediateValue(
|
||||
elementId,
|
||||
prop,
|
||||
getElementProp(afterElement, prop),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects override candidates for one durable user delta recorded while the
|
||||
* tx is active.
|
||||
*/
|
||||
collectCandidatesForDurableDelta(
|
||||
delta: StoreDelta,
|
||||
getLedgerEntry: (elementId: string) => TransactionLedgerEntry | undefined,
|
||||
reservedConsumedKeys: Set<string>,
|
||||
): TxUndoOverrideCandidate[] {
|
||||
const candidates: TxUndoOverrideCandidate[] = [];
|
||||
|
||||
for (const [elementId, deltaEntry] of Object.entries(
|
||||
getUpdatedElementEntries(delta),
|
||||
)) {
|
||||
const ledgerEntry = getLedgerEntry(elementId);
|
||||
if (!ledgerEntry) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const elementCandidates = this.collectCandidatesForElement(
|
||||
elementId,
|
||||
deltaEntry,
|
||||
ledgerEntry,
|
||||
reservedConsumedKeys,
|
||||
);
|
||||
if (elementCandidates.length > 0) {
|
||||
candidates.push(...elementCandidates);
|
||||
}
|
||||
}
|
||||
|
||||
return candidates;
|
||||
}
|
||||
|
||||
/** Evaluates one element's updated entry against the tx ledger snapshot. */
|
||||
private collectCandidatesForElement(
|
||||
elementId: string,
|
||||
deltaEntry: ElementUpdatedEntry,
|
||||
ledgerEntry: TransactionLedgerEntry,
|
||||
reservedConsumedKeys: Set<string>,
|
||||
): TxUndoOverrideCandidate[] {
|
||||
const { baselineElement, touchedProps } = ledgerEntry;
|
||||
if (!baselineElement || touchesWholeElement(touchedProps)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const candidates: TxUndoOverrideCandidate[] = [];
|
||||
for (const [prop, deletedValue] of getElementPropEntries(
|
||||
deltaEntry.deleted,
|
||||
)) {
|
||||
const candidate = this.createCandidateForProp({
|
||||
elementId,
|
||||
prop,
|
||||
deletedValue,
|
||||
baselineElement,
|
||||
touchedProps,
|
||||
reservedConsumedKeys,
|
||||
});
|
||||
if (candidate) {
|
||||
candidates.push(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
return candidates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an override candidate for one element+prop when the durable delta's
|
||||
* deleted baseline was polluted by a tx intermediate value.
|
||||
*/
|
||||
private createCandidateForProp(args: {
|
||||
elementId: string;
|
||||
prop: ElementPropName;
|
||||
deletedValue: unknown;
|
||||
baselineElement: ExcalidrawElement;
|
||||
touchedProps: TouchedElementProps;
|
||||
reservedConsumedKeys: Set<string>;
|
||||
}): TxUndoOverrideCandidate | null {
|
||||
const { elementId, prop, deletedValue, baselineElement, touchedProps } =
|
||||
args;
|
||||
|
||||
if (
|
||||
TX_UNDO_OVERRIDE_IGNORED_PROPS.has(prop) ||
|
||||
!hasTouchedProp(touchedProps, prop)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const consumedPropKey = serializeConsumedPropKey(elementId, prop);
|
||||
// Override only the first polluted user entry for this element+prop.
|
||||
// Later user actions should keep action-local undo baselines.
|
||||
if (
|
||||
this.consumedOverridePropKeys.has(consumedPropKey) ||
|
||||
args.reservedConsumedKeys.has(consumedPropKey)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!this.matchesIntermediateValue(elementId, prop, deletedValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
elementId,
|
||||
prop,
|
||||
expectedInsertedValue: deletedValue,
|
||||
preTxBaselineValue: getElementProp(baselineElement, prop),
|
||||
consumedKey: consumedPropKey,
|
||||
};
|
||||
}
|
||||
|
||||
/** Marks an element+prop override as consumed by an earlier durable entry. */
|
||||
markConsumed(consumedPropKey: string) {
|
||||
this.consumedOverridePropKeys.add(consumedPropKey);
|
||||
}
|
||||
|
||||
/** Returns the per-prop history map for one element, creating it if needed. */
|
||||
private getOrCreatePropValues(elementId: string) {
|
||||
const existing = this.intermediateValuesByElementProp.get(elementId);
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
const created = new Map<ElementPropName, TxIntermediateValueHistory>();
|
||||
this.intermediateValuesByElementProp.set(elementId, created);
|
||||
return created;
|
||||
}
|
||||
|
||||
/** Appends one observed intermediate value for an element prop. */
|
||||
private recordIntermediateValue(
|
||||
elementId: string,
|
||||
prop: ElementPropName,
|
||||
value: unknown,
|
||||
) {
|
||||
const propValues = this.getOrCreatePropValues(elementId);
|
||||
const history = propValues.get(prop);
|
||||
if (history) {
|
||||
history.add(value);
|
||||
return;
|
||||
}
|
||||
|
||||
const nextHistory = new TxIntermediateValueHistory();
|
||||
nextHistory.add(value);
|
||||
propValues.set(prop, nextHistory);
|
||||
}
|
||||
|
||||
/** Checks whether a durable delta baseline matches any tx intermediate value. */
|
||||
private matchesIntermediateValue(
|
||||
elementId: string,
|
||||
prop: ElementPropName,
|
||||
candidate: unknown,
|
||||
) {
|
||||
const history = this.intermediateValuesByElementProp
|
||||
.get(elementId)
|
||||
?.get(prop);
|
||||
if (!history) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return history.contains(candidate);
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,6 @@ import type {
|
||||
ExcalidrawNonSelectionElement,
|
||||
BindMode,
|
||||
ExcalidrawTextElement,
|
||||
FreeDrawStrokeShape,
|
||||
} from "@excalidraw/element/types";
|
||||
|
||||
import type {
|
||||
@@ -359,7 +358,6 @@ export interface AppState {
|
||||
currentItemFillStyle: ExcalidrawElement["fillStyle"];
|
||||
currentItemStrokeWidth: number;
|
||||
currentItemStrokeStyle: ExcalidrawElement["strokeStyle"];
|
||||
currentItemStrokeShape?: FreeDrawStrokeShape;
|
||||
currentItemRoughness: number;
|
||||
currentItemOpacity: number;
|
||||
currentItemFontFamily: FontFamilyValues;
|
||||
@@ -798,6 +796,9 @@ export type AppClassProperties = {
|
||||
files: BinaryFiles;
|
||||
editorInterface: App["editorInterface"];
|
||||
scene: App["scene"];
|
||||
store: App["store"];
|
||||
transactionManager: App["transactionManager"];
|
||||
createTransaction: App["createTransaction"];
|
||||
syncActionResult: App["syncActionResult"];
|
||||
fonts: App["fonts"];
|
||||
pasteFromClipboard: App["pasteFromClipboard"];
|
||||
@@ -890,7 +891,7 @@ export type PointerDownState = Readonly<{
|
||||
// We need to have these in the state so that we can unsubscribe them
|
||||
eventListeners: {
|
||||
// It's defined on the initial pointer down event
|
||||
onMove: null | ((event: PointerEvent) => void);
|
||||
onMove: null | ReturnType<typeof throttleRAF>;
|
||||
// It's defined on the initial pointer down event
|
||||
onUp: null | ((event: PointerEvent) => void);
|
||||
// It's defined on the initial pointer down event
|
||||
|
||||
Reference in New Issue
Block a user