@open-pencil/scene-graph 0.14.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/arrow-caps.d.ts +44 -0
- package/dist/arrow-caps.d.ts.map +1 -0
- package/dist/arrow-caps.js +131 -0
- package/dist/arrow-caps.js.map +1 -0
- package/dist/chunks/checkpoint.js +78 -0
- package/dist/chunks/checkpoint.js.map +1 -0
- package/dist/chunks/components/properties.js +1571 -0
- package/dist/chunks/components/properties.js.map +1 -0
- package/dist/chunks/coordinate.js +87 -33
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/font-style.js +2 -1
- package/dist/chunks/font-style.js.map +1 -1
- package/dist/chunks/index.js +1283 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/constants.js +1 -18
- package/dist/constants2.d.ts +0 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +107 -11
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +2 -2
- package/dist/coordinate2.d.ts +2 -2
- package/dist/copy.d.ts +2 -2
- package/dist/copy.js +2 -2
- package/dist/geometry.d.ts +2 -2
- package/dist/geometry.js +2 -431
- package/dist/guides.d.ts +2 -0
- package/dist/guides.js +0 -0
- package/dist/guides2.d.ts +11 -0
- package/dist/guides2.d.ts.map +1 -0
- package/dist/hit-test.d.ts +1 -2
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +26 -14
- package/dist/hit-test.js.map +1 -1
- package/dist/images.js +0 -1
- package/dist/images.js.map +1 -1
- package/dist/images2.d.ts.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -6
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout-guides.d.ts +2 -0
- package/dist/layout-guides.js +2 -0
- package/dist/layout-guides2.d.ts +16 -0
- package/dist/layout-guides2.d.ts.map +1 -0
- package/dist/matrix2.d.ts +0 -1
- package/dist/matrix2.d.ts.map +1 -1
- package/dist/node-defaults.d.ts +1 -2
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/parse-path.d.ts +1 -2
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/preview.d.ts +2 -12
- package/dist/preview.js +1 -1
- package/dist/preview2.d.ts +12 -0
- package/dist/preview2.d.ts.map +1 -0
- package/dist/primitives2.d.ts.map +1 -1
- package/dist/resize.d.ts +18 -4
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +70 -22
- package/dist/resize.js.map +1 -1
- package/dist/snap.js +1 -1
- package/dist/source-metadata.d.ts +1 -2
- package/dist/source-metadata.d.ts.map +1 -1
- package/dist/source-metadata.js +1 -1
- package/dist/text-picture.d.ts +13 -2
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text-picture.js +2 -2
- package/dist/types.d.ts +499 -2
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1296
- package/dist/types2.d.ts +2 -430
- package/dist/undo.js +1 -1
- package/dist/undo2.d.ts +5 -0
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +1 -2
- package/dist/variables.d.ts.map +1 -1
- package/dist/variables.js +1 -1
- package/dist/variant-name.d.ts.map +1 -1
- package/dist/variant-properties.d.ts +10 -0
- package/dist/variant-properties.d.ts.map +1 -0
- package/dist/variant-properties.js +39 -0
- package/dist/variant-properties.js.map +1 -0
- package/dist/vector-network.js +1 -1
- package/dist/vector-network2.d.ts +1 -2
- package/dist/vector-network2.d.ts.map +1 -1
- package/package.json +40 -15
- package/src/arrow-caps.ts +146 -0
- package/src/bindings.ts +20 -0
- package/src/checkpoint.ts +123 -0
- package/src/components/properties.ts +238 -0
- package/src/constants.ts +6 -0
- package/src/coordinate.ts +226 -0
- package/src/copy.ts +294 -0
- package/src/events.ts +21 -0
- package/src/export-scale.ts +16 -0
- package/src/font-style.ts +77 -0
- package/src/geometry.ts +530 -0
- package/src/guides.ts +8 -0
- package/src/hit-test.ts +205 -0
- package/src/images.ts +34 -0
- package/src/index.ts +686 -0
- package/src/instance-overrides.test.ts +67 -0
- package/src/instance-overrides.ts +152 -0
- package/src/instances.ts +480 -0
- package/src/layout-guides.ts +103 -0
- package/src/matrix.ts +116 -0
- package/src/mutation-impact.ts +70 -0
- package/src/node-defaults.ts +186 -0
- package/src/parse-path.ts +191 -0
- package/src/preview.ts +80 -0
- package/src/primitives.ts +42 -0
- package/src/resize.ts +302 -0
- package/src/shared-styles.ts +73 -0
- package/src/snap.ts +129 -0
- package/src/source-metadata.ts +8 -0
- package/src/text-picture.ts +65 -0
- package/src/types.ts +635 -0
- package/src/undo.ts +171 -0
- package/src/variables.ts +399 -0
- package/src/variant-name.ts +15 -0
- package/src/variant-properties.ts +48 -0
- package/src/vector-network.ts +293 -0
- package/dist/chunks/copy.js +0 -576
- package/dist/chunks/copy.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/geometry.js.map +0 -1
- package/dist/preview.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/types2.d.ts.map +0 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Vector } from "./primitives2.js";
|
|
2
|
+
import { Stroke, StrokeCap, VectorNetwork } from "./types.js";
|
|
3
|
+
//#region src/arrow-caps.d.ts
|
|
4
|
+
type ArrowCapKind = 'ARROW_LINES' | 'ARROW_EQUILATERAL';
|
|
5
|
+
interface ArrowEndpoint {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
/** Direction the arrow points, in radians, away from the path. */
|
|
9
|
+
angle: number;
|
|
10
|
+
cap: ArrowCapKind;
|
|
11
|
+
}
|
|
12
|
+
interface ArrowWing {
|
|
13
|
+
from: Vector;
|
|
14
|
+
to: Vector;
|
|
15
|
+
}
|
|
16
|
+
declare function isArrowCap(cap: string | undefined): cap is ArrowCapKind;
|
|
17
|
+
/**
|
|
18
|
+
* Worst-case reach of an arrow head past the path: a head's base corners sit
|
|
19
|
+
* one full head side from the tip, and line-arrow wings are stroked at the
|
|
20
|
+
* shaft weight on top of that. Used to widen visual bounds and render margins
|
|
21
|
+
* so heads are not cropped by caches, exports, or culling. Vertex-level caps
|
|
22
|
+
* in the network count even when no stroke or node cap is an arrow.
|
|
23
|
+
*/
|
|
24
|
+
declare function arrowCapOverflow(strokes?: Stroke[], fallbackCap?: StrokeCap, network?: VectorNetwork | null): number;
|
|
25
|
+
/**
|
|
26
|
+
* Finds the open path ends that should carry an arrow head: vertices used by
|
|
27
|
+
* exactly one segment, with a per-vertex cap override falling back to the
|
|
28
|
+
* stroke cap. The returned angle points away from the path, following the
|
|
29
|
+
* terminal bezier tangent when the segment is curved.
|
|
30
|
+
*/
|
|
31
|
+
declare function collectArrowEndpoints(network: VectorNetwork, fallbackCap: StrokeCap): ArrowEndpoint[];
|
|
32
|
+
/**
|
|
33
|
+
* Points of a filled equilateral arrow head: tip first, then the two base
|
|
34
|
+
* corners. The tip sits exactly on the endpoint and the head sweeps back
|
|
35
|
+
* along the path.
|
|
36
|
+
*/
|
|
37
|
+
declare function equilateralArrowPoints(x: number, y: number, angle: number, weight: number): [Vector, Vector, Vector];
|
|
38
|
+
/** The two stroked wings of an open (lines-style) arrow head. */
|
|
39
|
+
declare function arrowLinesSegments(x: number, y: number, angle: number, weight: number): [ArrowWing, ArrowWing];
|
|
40
|
+
/** Arrow endpoints for a LINE node, whose local geometry runs (0,0) → (width,height). */
|
|
41
|
+
declare function lineArrowEndpoints(width: number, height: number, cap: StrokeCap): ArrowEndpoint[];
|
|
42
|
+
//#endregion
|
|
43
|
+
export { ArrowCapKind, ArrowEndpoint, ArrowWing, arrowCapOverflow, arrowLinesSegments, collectArrowEndpoints, equilateralArrowPoints, isArrowCap, lineArrowEndpoints };
|
|
44
|
+
//# sourceMappingURL=arrow-caps.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arrow-caps.d.ts","names":[],"sources":["../src/arrow-caps.ts"],"mappings":";;;KAGY;UAEK;EACf;EACA;;EAEA;EACA,KAAK;;UAGU;EACf,MAAM;EACN,IAAI;;iBAOU,WAAW,0BAA0B,OAAO;;;;;;;;iBAW5C,iBACd,UAAU,UACV,cAAc,WACd,UAAU;;;;;;;iBAmBI,sBACd,SAAS,eACT,aAAa,YACZ;;;;;;iBAyCa,uBACd,WACA,WACA,eACA,kBACE,QAAQ,QAAQ;;iBAiBJ,mBACd,WACA,WACA,eACA,kBACE,WAAW;;iBAWC,mBAAmB,eAAe,gBAAgB,KAAK,YAAY"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
//#region src/arrow-caps.ts
|
|
2
|
+
/** Arrow head side length relative to stroke weight, tuned to match Figma. */
|
|
3
|
+
const ARROW_SIDE_PER_WEIGHT = 4;
|
|
4
|
+
const WING_SWEEP = Math.PI / 6;
|
|
5
|
+
function isArrowCap(cap) {
|
|
6
|
+
return cap === "ARROW_LINES" || cap === "ARROW_EQUILATERAL";
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Worst-case reach of an arrow head past the path: a head's base corners sit
|
|
10
|
+
* one full head side from the tip, and line-arrow wings are stroked at the
|
|
11
|
+
* shaft weight on top of that. Used to widen visual bounds and render margins
|
|
12
|
+
* so heads are not cropped by caches, exports, or culling. Vertex-level caps
|
|
13
|
+
* in the network count even when no stroke or node cap is an arrow.
|
|
14
|
+
*/
|
|
15
|
+
function arrowCapOverflow(strokes, fallbackCap, network) {
|
|
16
|
+
const networkHasArrow = network?.vertices.some((vertex) => isArrowCap(vertex.strokeCap)) ?? false;
|
|
17
|
+
let overflow = 0;
|
|
18
|
+
for (const stroke of strokes ?? []) {
|
|
19
|
+
if (!stroke.visible) continue;
|
|
20
|
+
if (networkHasArrow || isArrowCap(stroke.cap ?? fallbackCap)) overflow = Math.max(overflow, 4.5 * stroke.weight);
|
|
21
|
+
}
|
|
22
|
+
return overflow;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Finds the open path ends that should carry an arrow head: vertices used by
|
|
26
|
+
* exactly one segment, with a per-vertex cap override falling back to the
|
|
27
|
+
* stroke cap. The returned angle points away from the path, following the
|
|
28
|
+
* terminal bezier tangent when the segment is curved.
|
|
29
|
+
*/
|
|
30
|
+
function collectArrowEndpoints(network, fallbackCap) {
|
|
31
|
+
const degrees = Array.from({ length: network.vertices.length }, () => 0);
|
|
32
|
+
for (const segment of network.segments) {
|
|
33
|
+
degrees[segment.start]++;
|
|
34
|
+
degrees[segment.end]++;
|
|
35
|
+
}
|
|
36
|
+
const endpoints = [];
|
|
37
|
+
for (const segment of network.segments) for (const side of ["start", "end"]) {
|
|
38
|
+
const vertexIndex = segment[side];
|
|
39
|
+
if (degrees[vertexIndex] !== 1) continue;
|
|
40
|
+
const vertex = network.vertices[vertexIndex];
|
|
41
|
+
const cap = vertex.strokeCap ?? fallbackCap;
|
|
42
|
+
if (!isArrowCap(cap)) continue;
|
|
43
|
+
const tangent = side === "start" ? segment.tangentStart : segment.tangentEnd;
|
|
44
|
+
let controlX;
|
|
45
|
+
let controlY;
|
|
46
|
+
if (Math.abs(tangent.x) > .001 || Math.abs(tangent.y) > .001) {
|
|
47
|
+
controlX = vertex.x + tangent.x;
|
|
48
|
+
controlY = vertex.y + tangent.y;
|
|
49
|
+
} else {
|
|
50
|
+
const other = network.vertices[side === "start" ? segment.end : segment.start];
|
|
51
|
+
controlX = other.x;
|
|
52
|
+
controlY = other.y;
|
|
53
|
+
}
|
|
54
|
+
const dx = vertex.x - controlX;
|
|
55
|
+
const dy = vertex.y - controlY;
|
|
56
|
+
if (dx === 0 && dy === 0) continue;
|
|
57
|
+
endpoints.push({
|
|
58
|
+
x: vertex.x,
|
|
59
|
+
y: vertex.y,
|
|
60
|
+
angle: Math.atan2(dy, dx),
|
|
61
|
+
cap
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return endpoints;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Points of a filled equilateral arrow head: tip first, then the two base
|
|
68
|
+
* corners. The tip sits exactly on the endpoint and the head sweeps back
|
|
69
|
+
* along the path.
|
|
70
|
+
*/
|
|
71
|
+
function equilateralArrowPoints(x, y, angle, weight) {
|
|
72
|
+
const halfBase = ARROW_SIDE_PER_WEIGHT / 2 * weight;
|
|
73
|
+
const depth = halfBase * Math.sqrt(3);
|
|
74
|
+
const dirX = Math.cos(angle);
|
|
75
|
+
const dirY = Math.sin(angle);
|
|
76
|
+
const perpX = -dirY;
|
|
77
|
+
const perpY = dirX;
|
|
78
|
+
const baseX = x - depth * dirX;
|
|
79
|
+
const baseY = y - depth * dirY;
|
|
80
|
+
return [
|
|
81
|
+
{
|
|
82
|
+
x,
|
|
83
|
+
y
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
x: baseX - halfBase * perpX,
|
|
87
|
+
y: baseY - halfBase * perpY
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
x: baseX + halfBase * perpX,
|
|
91
|
+
y: baseY + halfBase * perpY
|
|
92
|
+
}
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
/** The two stroked wings of an open (lines-style) arrow head. */
|
|
96
|
+
function arrowLinesSegments(x, y, angle, weight) {
|
|
97
|
+
const wingLength = ARROW_SIDE_PER_WEIGHT * weight;
|
|
98
|
+
const tip = {
|
|
99
|
+
x,
|
|
100
|
+
y
|
|
101
|
+
};
|
|
102
|
+
const makeWing = (wingAngle) => ({
|
|
103
|
+
from: tip,
|
|
104
|
+
to: {
|
|
105
|
+
x: x + wingLength * Math.cos(wingAngle),
|
|
106
|
+
y: y + wingLength * Math.sin(wingAngle)
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return [makeWing(angle + Math.PI - WING_SWEEP), makeWing(angle + Math.PI + WING_SWEEP)];
|
|
110
|
+
}
|
|
111
|
+
/** Arrow endpoints for a LINE node, whose local geometry runs (0,0) → (width,height). */
|
|
112
|
+
function lineArrowEndpoints(width, height, cap) {
|
|
113
|
+
if (!isArrowCap(cap)) return [];
|
|
114
|
+
if (width === 0 && height === 0) return [];
|
|
115
|
+
const angle = Math.atan2(height, width);
|
|
116
|
+
return [{
|
|
117
|
+
x: 0,
|
|
118
|
+
y: 0,
|
|
119
|
+
angle: Math.atan2(-height, -width),
|
|
120
|
+
cap
|
|
121
|
+
}, {
|
|
122
|
+
x: width,
|
|
123
|
+
y: height,
|
|
124
|
+
angle,
|
|
125
|
+
cap
|
|
126
|
+
}];
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
export { arrowCapOverflow, arrowLinesSegments, collectArrowEndpoints, equilateralArrowPoints, isArrowCap, lineArrowEndpoints };
|
|
130
|
+
|
|
131
|
+
//# sourceMappingURL=arrow-caps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arrow-caps.js","names":[],"sources":["../src/arrow-caps.ts"],"sourcesContent":["import type { Vector } from './primitives'\nimport type { Stroke, StrokeCap, VectorNetwork } from './types'\n\nexport type ArrowCapKind = 'ARROW_LINES' | 'ARROW_EQUILATERAL'\n\nexport interface ArrowEndpoint {\n x: number\n y: number\n /** Direction the arrow points, in radians, away from the path. */\n angle: number\n cap: ArrowCapKind\n}\n\nexport interface ArrowWing {\n from: Vector\n to: Vector\n}\n\n/** Arrow head side length relative to stroke weight, tuned to match Figma. */\nconst ARROW_SIDE_PER_WEIGHT = 4\nconst WING_SWEEP = Math.PI / 6\n\nexport function isArrowCap(cap: string | undefined): cap is ArrowCapKind {\n return cap === 'ARROW_LINES' || cap === 'ARROW_EQUILATERAL'\n}\n\n/**\n * Worst-case reach of an arrow head past the path: a head's base corners sit\n * one full head side from the tip, and line-arrow wings are stroked at the\n * shaft weight on top of that. Used to widen visual bounds and render margins\n * so heads are not cropped by caches, exports, or culling. Vertex-level caps\n * in the network count even when no stroke or node cap is an arrow.\n */\nexport function arrowCapOverflow(\n strokes?: Stroke[],\n fallbackCap?: StrokeCap,\n network?: VectorNetwork | null\n): number {\n const networkHasArrow = network?.vertices.some((vertex) => isArrowCap(vertex.strokeCap)) ?? false\n let overflow = 0\n for (const stroke of strokes ?? []) {\n if (!stroke.visible) continue\n if (networkHasArrow || isArrowCap(stroke.cap ?? fallbackCap)) {\n overflow = Math.max(overflow, (ARROW_SIDE_PER_WEIGHT + 0.5) * stroke.weight)\n }\n }\n return overflow\n}\n\n/**\n * Finds the open path ends that should carry an arrow head: vertices used by\n * exactly one segment, with a per-vertex cap override falling back to the\n * stroke cap. The returned angle points away from the path, following the\n * terminal bezier tangent when the segment is curved.\n */\nexport function collectArrowEndpoints(\n network: VectorNetwork,\n fallbackCap: StrokeCap\n): ArrowEndpoint[] {\n const degrees = Array.from({ length: network.vertices.length }, () => 0)\n for (const segment of network.segments) {\n degrees[segment.start]++\n degrees[segment.end]++\n }\n\n const endpoints: ArrowEndpoint[] = []\n for (const segment of network.segments) {\n for (const side of ['start', 'end'] as const) {\n const vertexIndex = segment[side]\n if (degrees[vertexIndex] !== 1) continue\n const vertex = network.vertices[vertexIndex]\n const cap = (vertex.strokeCap as StrokeCap | undefined) ?? fallbackCap\n if (!isArrowCap(cap)) continue\n\n const tangent = side === 'start' ? segment.tangentStart : segment.tangentEnd\n let controlX: number\n let controlY: number\n if (Math.abs(tangent.x) > 0.001 || Math.abs(tangent.y) > 0.001) {\n controlX = vertex.x + tangent.x\n controlY = vertex.y + tangent.y\n } else {\n const other = network.vertices[side === 'start' ? segment.end : segment.start]\n controlX = other.x\n controlY = other.y\n }\n const dx = vertex.x - controlX\n const dy = vertex.y - controlY\n if (dx === 0 && dy === 0) continue\n endpoints.push({ x: vertex.x, y: vertex.y, angle: Math.atan2(dy, dx), cap })\n }\n }\n return endpoints\n}\n\n/**\n * Points of a filled equilateral arrow head: tip first, then the two base\n * corners. The tip sits exactly on the endpoint and the head sweeps back\n * along the path.\n */\nexport function equilateralArrowPoints(\n x: number,\n y: number,\n angle: number,\n weight: number\n): [Vector, Vector, Vector] {\n const halfBase = (ARROW_SIDE_PER_WEIGHT / 2) * weight\n const depth = halfBase * Math.sqrt(3)\n const dirX = Math.cos(angle)\n const dirY = Math.sin(angle)\n const perpX = -dirY\n const perpY = dirX\n const baseX = x - depth * dirX\n const baseY = y - depth * dirY\n return [\n { x, y },\n { x: baseX - halfBase * perpX, y: baseY - halfBase * perpY },\n { x: baseX + halfBase * perpX, y: baseY + halfBase * perpY }\n ]\n}\n\n/** The two stroked wings of an open (lines-style) arrow head. */\nexport function arrowLinesSegments(\n x: number,\n y: number,\n angle: number,\n weight: number\n): [ArrowWing, ArrowWing] {\n const wingLength = ARROW_SIDE_PER_WEIGHT * weight\n const tip = { x, y }\n const makeWing = (wingAngle: number): ArrowWing => ({\n from: tip,\n to: { x: x + wingLength * Math.cos(wingAngle), y: y + wingLength * Math.sin(wingAngle) }\n })\n return [makeWing(angle + Math.PI - WING_SWEEP), makeWing(angle + Math.PI + WING_SWEEP)]\n}\n\n/** Arrow endpoints for a LINE node, whose local geometry runs (0,0) → (width,height). */\nexport function lineArrowEndpoints(width: number, height: number, cap: StrokeCap): ArrowEndpoint[] {\n if (!isArrowCap(cap)) return []\n if (width === 0 && height === 0) return []\n const angle = Math.atan2(height, width)\n return [\n { x: 0, y: 0, angle: Math.atan2(-height, -width), cap },\n { x: width, y: height, angle, cap }\n ]\n}\n"],"mappings":";;AAmBA,MAAM,wBAAwB;AAC9B,MAAM,aAAa,KAAK,KAAK;AAE7B,SAAgB,WAAW,KAA8C;CACvE,OAAO,QAAQ,iBAAiB,QAAQ;AAC1C;;;;;;;;AASA,SAAgB,iBACd,SACA,aACA,SACQ;CACR,MAAM,kBAAkB,SAAS,SAAS,MAAM,WAAW,WAAW,OAAO,SAAS,CAAC,KAAK;CAC5F,IAAI,WAAW;CACf,KAAK,MAAM,UAAU,WAAW,CAAC,GAAG;EAClC,IAAI,CAAC,OAAO,SAAS;EACrB,IAAI,mBAAmB,WAAW,OAAO,OAAO,WAAW,GACzD,WAAW,KAAK,IAAI,UAAW,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AO,KAAK,IAAI,KAAK;CAC3B,MAAM,OAAO,KAAK,IAAI,KAAK;CAC3B,MAAM,QAAQ,CAAC;CACf,MAAM,QAAQ;CACd,MAAM,QAAQ,IAAI,QAAQ;CAC1B,MAAM,QAAQ,IAAI,QAAQ;CAC1B,OAAO;EACL;GAAE;GAAG;EAAE;EACP;GAAE,GAAG,QAAQ,WAAW;GAAO,GAAG,QAAQ,WAAW;EAAM;EAC3D;GAAE,GAAG,QAAQ,WAAW;GAAO,GAAG,QAAQ,WAAW;EAAM;CAC7D;AACF;;AAGA,SAAgB,mBACd,GACA,GACA,OACA,QACwB;CACxB,MAAM,aAAa,wBAAwB;CAC3C,MAAM,MAAM;EAAE;EAAG;CAAE;CACnB,MAAM,YAAY,eAAkC;EAClD,MAAM;EACN,IAAI;GAAE,GAAG,IAAI,aAAa,KAAK,IAAI,SAAS;GAAG,GAAG,IAAI,aAAa,KAAK,IAAI,SAAS;EAAE;CACzF;CACA,OAAO,CAAC,SAAS,QAAQ,KAAK,KAAK,UAAU,GAAG,SAAS,QAAQ,KAAK,KAAK,UAAU,CAAC;AACxF;;AAGA,SAAgB,mBAAmB,OAAe,QAAgB,KAAiC;CACjG,IAAI,CAAC,WAAW,GAAG,GAAG,OAAO,CAAC;CAC9B,IAAI,UAAU,KAAK,WAAW,GAAG,OAAO,CAAC;CACzC,MAAM,QAAQ,KAAK,MAAM,QAAQ,KAAK;CACtC,OAAO,CACL;EAAE,GAAG;EAAG,GAAG;EAAG,OAAO,KAAK,MAAM,CAAC,QAAQ,CAAC,KAAK;EAAG;CAAI,GACtD;EAAE,GAAG;EAAO,GAAG;EAAQ;EAAO;CAAI,CACpC;AACF"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { isEqual } from "es-toolkit";
|
|
2
|
+
//#region src/checkpoint.ts
|
|
3
|
+
/** Capture rollback state without changing graph or surviving object identities. */
|
|
4
|
+
function captureGraphCheckpoint(graph) {
|
|
5
|
+
const nodeRefs = new Map(graph.nodes);
|
|
6
|
+
const variableRefs = new Map(graph.variables);
|
|
7
|
+
const collectionRefs = new Map(graph.variableCollections);
|
|
8
|
+
const snapshot = structuredClone({
|
|
9
|
+
nodes: graph.nodes,
|
|
10
|
+
variables: graph.variables,
|
|
11
|
+
variableCollections: graph.variableCollections,
|
|
12
|
+
activeMode: graph.activeMode,
|
|
13
|
+
instanceIndex: graph.instanceIndex,
|
|
14
|
+
enabledLibraries: graph.enabledLibraries,
|
|
15
|
+
rootId: graph.rootId,
|
|
16
|
+
figKiwiVersion: graph.figKiwiVersion,
|
|
17
|
+
figSchemaDeflated: graph.figSchemaDeflated,
|
|
18
|
+
documentColorSpace: graph.documentColorSpace
|
|
19
|
+
});
|
|
20
|
+
const images = new Map(graph.images);
|
|
21
|
+
function assertPropertiesOnly() {
|
|
22
|
+
if (snapshot.nodes.size !== graph.nodes.size || snapshot.variables.size !== graph.variables.size) throw new Error("Atomic tools must not create or remove nodes or variables");
|
|
23
|
+
for (const [id, before] of snapshot.nodes) {
|
|
24
|
+
const after = graph.nodes.get(id);
|
|
25
|
+
if (!after || after !== nodeRefs.get(id) || before.id !== after.id || before.type !== after.type || before.parentId !== after.parentId || before.componentId !== after.componentId || !isEqual(before.childIds, after.childIds)) throw new Error("Atomic tools must not change node hierarchy or identity");
|
|
26
|
+
}
|
|
27
|
+
for (const [id, before] of snapshot.variables) {
|
|
28
|
+
const after = graph.variables.get(id);
|
|
29
|
+
if (!after || after !== variableRefs.get(id) || before.id !== after.id || before.collectionId !== after.collectionId || before.type !== after.type) throw new Error("Atomic tools must not change variable identity");
|
|
30
|
+
}
|
|
31
|
+
assertDocumentUnchanged();
|
|
32
|
+
}
|
|
33
|
+
function assertDocumentUnchanged() {
|
|
34
|
+
if (!isEqual(snapshot.variableCollections, graph.variableCollections) || !isEqual(snapshot.activeMode, graph.activeMode) || !isEqual(snapshot.instanceIndex, graph.instanceIndex) || !isEqual(snapshot.enabledLibraries, graph.enabledLibraries) || images.size !== graph.images.size || [...images].some(([id, bytes]) => graph.images.get(id) !== bytes) || graph.rootId !== snapshot.rootId || graph.documentColorSpace !== snapshot.documentColorSpace || graph.figKiwiVersion !== snapshot.figKiwiVersion || !isEqual(graph.figSchemaDeflated, snapshot.figSchemaDeflated)) throw new Error("Atomic tools must not change collections, assets or document metadata");
|
|
35
|
+
}
|
|
36
|
+
function restore() {
|
|
37
|
+
const failedNodes = new Map(graph.nodes);
|
|
38
|
+
restoreObjects(graph.nodes, snapshot.nodes, nodeRefs);
|
|
39
|
+
restoreObjects(graph.variables, snapshot.variables, variableRefs);
|
|
40
|
+
restoreObjects(graph.variableCollections, snapshot.variableCollections, collectionRefs);
|
|
41
|
+
restoreMap(graph.activeMode, structuredClone(snapshot.activeMode));
|
|
42
|
+
restoreMap(graph.instanceIndex, structuredClone(snapshot.instanceIndex));
|
|
43
|
+
restoreMap(graph.enabledLibraries, structuredClone(snapshot.enabledLibraries));
|
|
44
|
+
restoreMap(graph.images, images);
|
|
45
|
+
graph.rootId = snapshot.rootId;
|
|
46
|
+
graph.figKiwiVersion = snapshot.figKiwiVersion;
|
|
47
|
+
graph.figSchemaDeflated = structuredClone(snapshot.figSchemaDeflated);
|
|
48
|
+
graph.documentColorSpace = snapshot.documentColorSpace;
|
|
49
|
+
graph.clearAbsPosCache();
|
|
50
|
+
for (const [id, node] of failedNodes) if (!graph.nodes.has(id)) graph.emitter.emit("node:deleted", id, node.parentId);
|
|
51
|
+
for (const [id, node] of graph.nodes) if (failedNodes.has(id)) graph.emitter.emit("node:updated", id, node);
|
|
52
|
+
else graph.emitter.emit("node:created", node);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
nodes: snapshot.nodes,
|
|
56
|
+
variables: snapshot.variables,
|
|
57
|
+
assertPropertiesOnly,
|
|
58
|
+
restore
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function restoreObjects(target, snapshot, refs) {
|
|
62
|
+
target.clear();
|
|
63
|
+
for (const [id, saved] of snapshot) {
|
|
64
|
+
const original = refs.get(id);
|
|
65
|
+
if (!original) throw new Error(`Missing checkpoint reference: ${id}`);
|
|
66
|
+
for (const key of Object.keys(original)) if (!(key in saved)) Reflect.deleteProperty(original, key);
|
|
67
|
+
Object.assign(original, structuredClone(saved));
|
|
68
|
+
target.set(id, original);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function restoreMap(target, saved) {
|
|
72
|
+
target.clear();
|
|
73
|
+
for (const [key, value] of saved) target.set(key, value);
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { captureGraphCheckpoint };
|
|
77
|
+
|
|
78
|
+
//# sourceMappingURL=checkpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkpoint.js","names":[],"sources":["../../src/checkpoint.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit'\n\nimport type { SceneGraph } from './index'\n\n/** Capture rollback state without changing graph or surviving object identities. */\nexport function captureGraphCheckpoint(graph: SceneGraph) {\n const nodeRefs = new Map(graph.nodes)\n const variableRefs = new Map(graph.variables)\n const collectionRefs = new Map(graph.variableCollections)\n const snapshot = structuredClone({\n nodes: graph.nodes,\n variables: graph.variables,\n variableCollections: graph.variableCollections,\n activeMode: graph.activeMode,\n instanceIndex: graph.instanceIndex,\n enabledLibraries: graph.enabledLibraries,\n rootId: graph.rootId,\n figKiwiVersion: graph.figKiwiVersion,\n figSchemaDeflated: graph.figSchemaDeflated,\n documentColorSpace: graph.documentColorSpace\n })\n // Atomic property tools cannot edit assets. Retain existing buffers, not copies of every bitmap.\n const images = new Map(graph.images)\n\n function assertPropertiesOnly() {\n if (\n snapshot.nodes.size !== graph.nodes.size ||\n snapshot.variables.size !== graph.variables.size\n )\n throw new Error('Atomic tools must not create or remove nodes or variables')\n for (const [id, before] of snapshot.nodes) {\n const after = graph.nodes.get(id)\n if (\n !after ||\n after !== nodeRefs.get(id) ||\n before.id !== after.id ||\n before.type !== after.type ||\n before.parentId !== after.parentId ||\n before.componentId !== after.componentId ||\n !isEqual(before.childIds, after.childIds)\n ) {\n throw new Error('Atomic tools must not change node hierarchy or identity')\n }\n }\n for (const [id, before] of snapshot.variables) {\n const after = graph.variables.get(id)\n if (\n !after ||\n after !== variableRefs.get(id) ||\n before.id !== after.id ||\n before.collectionId !== after.collectionId ||\n before.type !== after.type\n )\n throw new Error('Atomic tools must not change variable identity')\n }\n assertDocumentUnchanged()\n }\n\n function assertDocumentUnchanged() {\n if (\n !isEqual(snapshot.variableCollections, graph.variableCollections) ||\n !isEqual(snapshot.activeMode, graph.activeMode) ||\n !isEqual(snapshot.instanceIndex, graph.instanceIndex) ||\n !isEqual(snapshot.enabledLibraries, graph.enabledLibraries) ||\n images.size !== graph.images.size ||\n [...images].some(([id, bytes]) => graph.images.get(id) !== bytes) ||\n graph.rootId !== snapshot.rootId ||\n graph.documentColorSpace !== snapshot.documentColorSpace ||\n graph.figKiwiVersion !== snapshot.figKiwiVersion ||\n !isEqual(graph.figSchemaDeflated, snapshot.figSchemaDeflated)\n ) {\n throw new Error('Atomic tools must not change collections, assets or document metadata')\n }\n }\n\n function restore() {\n const failedNodes = new Map(graph.nodes)\n restoreObjects(graph.nodes, snapshot.nodes, nodeRefs)\n restoreObjects(graph.variables, snapshot.variables, variableRefs)\n restoreObjects(graph.variableCollections, snapshot.variableCollections, collectionRefs)\n restoreMap(graph.activeMode, structuredClone(snapshot.activeMode))\n restoreMap(graph.instanceIndex, structuredClone(snapshot.instanceIndex))\n restoreMap(graph.enabledLibraries, structuredClone(snapshot.enabledLibraries))\n restoreMap(graph.images, images)\n graph.rootId = snapshot.rootId\n graph.figKiwiVersion = snapshot.figKiwiVersion\n graph.figSchemaDeflated = structuredClone(snapshot.figSchemaDeflated)\n graph.documentColorSpace = snapshot.documentColorSpace\n graph.clearAbsPosCache()\n // Notify consumers only after the complete hierarchy and indexes have been restored.\n for (const [id, node] of failedNodes) {\n if (!graph.nodes.has(id)) graph.emitter.emit('node:deleted', id, node.parentId)\n }\n for (const [id, node] of graph.nodes) {\n if (failedNodes.has(id)) graph.emitter.emit('node:updated', id, node)\n else graph.emitter.emit('node:created', node)\n }\n }\n\n return { nodes: snapshot.nodes, variables: snapshot.variables, assertPropertiesOnly, restore }\n}\n\nfunction restoreObjects<T extends object>(\n target: Map<string, T>,\n snapshot: Map<string, T>,\n refs: Map<string, T>\n) {\n target.clear()\n for (const [id, saved] of snapshot) {\n const original = refs.get(id)\n if (!original) throw new Error(`Missing checkpoint reference: ${id}`)\n for (const key of Object.keys(original) as (keyof T)[]) {\n if (!(key in saved)) Reflect.deleteProperty(original, key)\n }\n Object.assign(original, structuredClone(saved))\n target.set(id, original)\n }\n}\n\nfunction restoreMap<K, V>(target: Map<K, V>, saved: Map<K, V>) {\n target.clear()\n for (const [key, value] of saved) target.set(key, value)\n}\n"],"mappings":";;;AAKA,SAAgB,uBAAuB,OAAmB;CACxD,MAAM,WAAW,IAAI,IAAI,MAAM,KAAK;CACpC,MAAM,eAAe,IAAI,IAAI,MAAM,SAAS;CAC5C,MAAM,iBAAiB,IAAI,IAAI,MAAM,mBAAmB;CACxD,MAAM,WAAW,gBAAgB;EAC/B,OAAO,MAAM;EACb,WAAW,MAAM;EACjB,qBAAqB,MAAM;EAC3B,YAAY,MAAM;EAClB,eAAe,MAAM;EACrB,kBAAkB,MAAM;EACxB,QAAQ,MAAM;EACd,gBAAgB,MAAM;EACtB,mBAAmB,MAAM;EACzB,oBAAoB,MAAM;CAC5B,CAAC;CAED,MAAM,SAAS,IAAI,IAAI,MAAM,MAAM;CAEnC,SAAS,uBAAuB;EAC9B,IACE,SAAS,MAAM,SAAS,MAAM,MAAM,QACpC,SAAS,UAAU,SAAS,MAAM,UAAU,MAE5C,MAAM,IAAI,MAAM,2DAA2D;EAC7E,KAAK,MAAM,CAAC,IAAI,WAAW,SAAS,OAAO;GACzC,MAAM,QAAQ,MAAM,MAAM,IAAI,EAAE;GAChC,IACE,CAAC,SACD,UAAU,SAAS,IAAI,EAAE,KACz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