@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.2

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.
Files changed (236) hide show
  1. package/dist/arrow-caps.d.ts.map +1 -1
  2. package/dist/arrow-caps.js +2 -5
  3. package/dist/arrow-caps.js.map +1 -1
  4. package/dist/behaviours/index.d.ts +2 -2
  5. package/dist/behaviours/index.js +2 -2
  6. package/dist/behaviours/kinds.d.ts +11 -5
  7. package/dist/behaviours/kinds.d.ts.map +1 -1
  8. package/dist/behaviours/kinds.js +76 -75
  9. package/dist/behaviours/kinds.js.map +1 -1
  10. package/dist/behaviours/layers.d.ts.map +1 -1
  11. package/dist/behaviours/layers.js +3 -2
  12. package/dist/behaviours/layers.js.map +1 -1
  13. package/dist/behaviours/model.d.ts +7 -4
  14. package/dist/behaviours/model.d.ts.map +1 -1
  15. package/dist/behaviours/model.js +16 -9
  16. package/dist/behaviours/model.js.map +1 -1
  17. package/dist/behaviours/schema.d.ts +118 -4
  18. package/dist/behaviours/schema.d.ts.map +1 -1
  19. package/dist/behaviours/schema.js +18 -3
  20. package/dist/behaviours/schema.js.map +1 -1
  21. package/dist/behaviours/spec.d.ts.map +1 -1
  22. package/dist/behaviours/spec.js +3 -2
  23. package/dist/behaviours/spec.js.map +1 -1
  24. package/dist/bindings.js +9 -1
  25. package/dist/bindings.js.map +1 -1
  26. package/dist/checkpoint.d.ts +31 -2
  27. package/dist/checkpoint.d.ts.map +1 -1
  28. package/dist/checkpoint.js +176 -5
  29. package/dist/checkpoint.js.map +1 -1
  30. package/dist/components/index.d.ts +3 -3
  31. package/dist/components/index.js +3 -3
  32. package/dist/components/properties.d.ts +13 -1
  33. package/dist/components/properties.d.ts.map +1 -1
  34. package/dist/components/properties.js +58 -3
  35. package/dist/components/properties.js.map +1 -1
  36. package/dist/components/variants.d.ts +16 -10
  37. package/dist/components/variants.d.ts.map +1 -1
  38. package/dist/components/variants.js +39 -13
  39. package/dist/components/variants.js.map +1 -1
  40. package/dist/connectors/hit.d.ts.map +1 -1
  41. package/dist/connectors/hit.js +1 -7
  42. package/dist/connectors/hit.js.map +1 -1
  43. package/dist/connectors/pieces.d.ts.map +1 -1
  44. package/dist/connectors/pieces.js +2 -5
  45. package/dist/connectors/pieces.js.map +1 -1
  46. package/dist/copy.d.ts.map +1 -1
  47. package/dist/copy.js +9 -15
  48. package/dist/copy.js.map +1 -1
  49. package/dist/css/blend.d.ts +11 -0
  50. package/dist/css/blend.d.ts.map +1 -0
  51. package/dist/css/blend.js +30 -0
  52. package/dist/css/blend.js.map +1 -0
  53. package/dist/css/index.d.ts +2 -1
  54. package/dist/css/index.js +2 -1
  55. package/dist/cubic.d.ts +7 -0
  56. package/dist/cubic.d.ts.map +1 -0
  57. package/dist/cubic.js +17 -0
  58. package/dist/cubic.js.map +1 -0
  59. package/dist/export-format.d.ts +14 -1
  60. package/dist/export-format.d.ts.map +1 -1
  61. package/dist/export-format.js +26 -1
  62. package/dist/export-format.js.map +1 -1
  63. package/dist/fields/text.d.ts +2 -2
  64. package/dist/fields/text.d.ts.map +1 -1
  65. package/dist/fields/text.js +2 -1
  66. package/dist/fields/text.js.map +1 -1
  67. package/dist/figjam/index.d.ts +2 -2
  68. package/dist/figjam/index.js +2 -2
  69. package/dist/figjam/palette.d.ts +13 -2
  70. package/dist/figjam/palette.d.ts.map +1 -1
  71. package/dist/figjam/palette.js +30 -2
  72. package/dist/figjam/palette.js.map +1 -1
  73. package/dist/generated/index.d.ts +2 -2
  74. package/dist/generated/index.js +2 -2
  75. package/dist/generated/shape-with-text.d.ts +7 -2
  76. package/dist/generated/shape-with-text.d.ts.map +1 -1
  77. package/dist/generated/shape-with-text.js +20 -3
  78. package/dist/generated/shape-with-text.js.map +1 -1
  79. package/dist/geometry.d.ts +30 -46
  80. package/dist/geometry.d.ts.map +1 -1
  81. package/dist/geometry.js +63 -131
  82. package/dist/geometry.js.map +1 -1
  83. package/dist/graph.d.ts +1 -0
  84. package/dist/graph.d.ts.map +1 -1
  85. package/dist/graph.js +15 -3
  86. package/dist/graph.js.map +1 -1
  87. package/dist/guides.d.ts +3 -1
  88. package/dist/guides.d.ts.map +1 -1
  89. package/dist/guides.js +5 -1
  90. package/dist/guides.js.map +1 -1
  91. package/dist/hit-test.d.ts.map +1 -1
  92. package/dist/hit-test.js +42 -29
  93. package/dist/hit-test.js.map +1 -1
  94. package/dist/index.d.ts +2 -2
  95. package/dist/instances/fields.d.ts +2 -2
  96. package/dist/instances/fields.d.ts.map +1 -1
  97. package/dist/instances/fields.js +8 -0
  98. package/dist/instances/fields.js.map +1 -1
  99. package/dist/instances/index.d.ts +1 -3
  100. package/dist/instances/index.d.ts.map +1 -1
  101. package/dist/instances/index.js +36 -30
  102. package/dist/instances/index.js.map +1 -1
  103. package/dist/instances/kept-overrides.js +204 -0
  104. package/dist/instances/kept-overrides.js.map +1 -0
  105. package/dist/instances/main-component.d.ts +3 -1
  106. package/dist/instances/main-component.d.ts.map +1 -1
  107. package/dist/instances/main-component.js +10 -2
  108. package/dist/instances/main-component.js.map +1 -1
  109. package/dist/instances/overrides.d.ts +4 -1
  110. package/dist/instances/overrides.d.ts.map +1 -1
  111. package/dist/instances/overrides.js +18 -2
  112. package/dist/instances/overrides.js.map +1 -1
  113. package/dist/instances/swap.js +288 -0
  114. package/dist/instances/swap.js.map +1 -0
  115. package/dist/instances/sync.js +84 -32
  116. package/dist/instances/sync.js.map +1 -1
  117. package/dist/node-defaults.d.ts.map +1 -1
  118. package/dist/node-defaults.js +1 -1
  119. package/dist/node-defaults.js.map +1 -1
  120. package/dist/outline/flatten.d.ts +15 -0
  121. package/dist/outline/flatten.d.ts.map +1 -0
  122. package/dist/outline/flatten.js +189 -0
  123. package/dist/outline/flatten.js.map +1 -0
  124. package/dist/outline/heads.js +34 -0
  125. package/dist/outline/heads.js.map +1 -0
  126. package/dist/outline/hit.d.ts +21 -0
  127. package/dist/outline/hit.d.ts.map +1 -0
  128. package/dist/outline/hit.js +276 -0
  129. package/dist/outline/hit.js.map +1 -0
  130. package/dist/outline/index.d.ts +5 -0
  131. package/dist/outline/index.js +5 -0
  132. package/dist/outline/point.d.ts +16 -0
  133. package/dist/outline/point.d.ts.map +1 -0
  134. package/dist/outline/point.js +42 -0
  135. package/dist/outline/point.js.map +1 -0
  136. package/dist/outline/shapes.d.ts +35 -0
  137. package/dist/outline/shapes.d.ts.map +1 -0
  138. package/dist/outline/shapes.js +181 -0
  139. package/dist/outline/shapes.js.map +1 -0
  140. package/dist/plugin-data/fields.d.ts +2 -1
  141. package/dist/plugin-data/fields.d.ts.map +1 -1
  142. package/dist/plugin-data/fields.js +19 -4
  143. package/dist/plugin-data/fields.js.map +1 -1
  144. package/dist/scaling/tree.js +1 -1
  145. package/dist/shared-styles.d.ts +14 -1
  146. package/dist/shared-styles.d.ts.map +1 -1
  147. package/dist/shared-styles.js +63 -2
  148. package/dist/shared-styles.js.map +1 -1
  149. package/dist/slots/content.d.ts.map +1 -1
  150. package/dist/slots/content.js +2 -0
  151. package/dist/slots/content.js.map +1 -1
  152. package/dist/slots/frames.d.ts +2 -0
  153. package/dist/slots/frames.d.ts.map +1 -1
  154. package/dist/slots/frames.js +7 -2
  155. package/dist/slots/frames.js.map +1 -1
  156. package/dist/text/index.d.ts +2 -2
  157. package/dist/text/index.js +3 -3
  158. package/dist/text/lengths.d.ts +6 -1
  159. package/dist/text/lengths.d.ts.map +1 -1
  160. package/dist/text/lengths.js +10 -1
  161. package/dist/text/lengths.js.map +1 -1
  162. package/dist/text/overflow.d.ts +2 -2
  163. package/dist/text/overflow.js.map +1 -1
  164. package/dist/text/picture.d.ts +1 -2
  165. package/dist/text/picture.d.ts.map +1 -1
  166. package/dist/text/picture.js +1 -4
  167. package/dist/text/picture.js.map +1 -1
  168. package/dist/transfer/index.js +1 -1
  169. package/dist/transfer/nodes.js +9 -5
  170. package/dist/transfer/nodes.js.map +1 -1
  171. package/dist/types.d.ts +27 -1
  172. package/dist/types.d.ts.map +1 -1
  173. package/dist/types.js.map +1 -1
  174. package/dist/variables/bindings.d.ts +28 -5
  175. package/dist/variables/bindings.d.ts.map +1 -1
  176. package/dist/variables/bindings.js +104 -14
  177. package/dist/variables/bindings.js.map +1 -1
  178. package/dist/variables/fields.d.ts +19 -1
  179. package/dist/variables/fields.d.ts.map +1 -1
  180. package/dist/variables/fields.js +36 -1
  181. package/dist/variables/fields.js.map +1 -1
  182. package/dist/variables/index.d.ts +3 -3
  183. package/dist/variables/index.d.ts.map +1 -1
  184. package/dist/variables/index.js +23 -8
  185. package/dist/variables/index.js.map +1 -1
  186. package/package.json +7 -1
  187. package/src/arrow-caps.ts +2 -5
  188. package/src/behaviours/kinds.ts +51 -34
  189. package/src/behaviours/layers.ts +4 -2
  190. package/src/behaviours/model.ts +22 -11
  191. package/src/behaviours/schema.ts +20 -4
  192. package/src/behaviours/spec.ts +8 -2
  193. package/src/bindings.ts +11 -2
  194. package/src/checkpoint.ts +251 -4
  195. package/src/components/properties.ts +92 -2
  196. package/src/components/variants.ts +51 -30
  197. package/src/connectors/hit.ts +1 -12
  198. package/src/connectors/pieces.ts +2 -5
  199. package/src/copy.ts +9 -15
  200. package/src/css/blend.ts +27 -0
  201. package/src/css/index.ts +1 -0
  202. package/src/cubic.ts +14 -0
  203. package/src/export-format.ts +29 -0
  204. package/src/fields/text.ts +3 -1
  205. package/src/figjam/palette.ts +29 -1
  206. package/src/generated/shape-with-text.ts +28 -2
  207. package/src/geometry.ts +78 -198
  208. package/src/graph.ts +27 -3
  209. package/src/guides.ts +5 -0
  210. package/src/hit-test.ts +67 -54
  211. package/src/instances/fields.ts +8 -0
  212. package/src/instances/index.ts +58 -31
  213. package/src/instances/kept-overrides.ts +280 -0
  214. package/src/instances/main-component.ts +12 -0
  215. package/src/instances/overrides.ts +27 -1
  216. package/src/instances/swap.ts +451 -0
  217. package/src/instances/sync.ts +141 -42
  218. package/src/node-defaults.ts +2 -1
  219. package/src/outline/flatten.ts +190 -0
  220. package/src/outline/heads.ts +52 -0
  221. package/src/outline/hit.ts +369 -0
  222. package/src/outline/index.ts +4 -0
  223. package/src/outline/point.ts +59 -0
  224. package/src/outline/shapes.ts +207 -0
  225. package/src/plugin-data/fields.ts +15 -2
  226. package/src/shared-styles.ts +82 -1
  227. package/src/slots/content.ts +2 -0
  228. package/src/slots/frames.ts +9 -7
  229. package/src/text/lengths.ts +10 -0
  230. package/src/text/overflow.ts +2 -2
  231. package/src/text/picture.ts +0 -4
  232. package/src/transfer/nodes.ts +7 -5
  233. package/src/types.ts +28 -0
  234. package/src/variables/bindings.ts +125 -14
  235. package/src/variables/fields.ts +46 -1
  236. package/src/variables/index.ts +35 -2
@@ -1,5 +1,10 @@
1
+ import { createSceneMutationImpact, recordSceneMutations } from "./mutation-impact.js";
1
2
  import { isEqual } from "es-toolkit";
2
3
  //#region src/checkpoint.ts
4
+ const NODE_COUNT_ERROR = "Atomic tools must not create or remove nodes or variables";
5
+ const NODE_IDENTITY_ERROR = "Atomic tools must not change node hierarchy or identity";
6
+ const VARIABLE_IDENTITY_ERROR = "Atomic tools must not change variable identity";
7
+ const DOCUMENT_ERROR = "Atomic tools must not change collections, assets or document metadata";
3
8
  /** Capture rollback state without changing graph or surviving object identities. */
4
9
  function captureGraphCheckpoint(graph) {
5
10
  const nodeRefs = new Map(graph.nodes);
@@ -19,19 +24,19 @@ function captureGraphCheckpoint(graph) {
19
24
  });
20
25
  const images = new Map(graph.images);
21
26
  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");
27
+ if (snapshot.nodes.size !== graph.nodes.size || snapshot.variables.size !== graph.variables.size) throw new Error(NODE_COUNT_ERROR);
23
28
  for (const [id, before] of snapshot.nodes) {
24
29
  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");
30
+ 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(NODE_IDENTITY_ERROR);
26
31
  }
27
32
  for (const [id, before] of snapshot.variables) {
28
33
  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");
34
+ if (!after || after !== variableRefs.get(id) || before.id !== after.id || before.collectionId !== after.collectionId || before.type !== after.type) throw new Error(VARIABLE_IDENTITY_ERROR);
30
35
  }
31
36
  assertDocumentUnchanged();
32
37
  }
33
38
  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");
39
+ 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(DOCUMENT_ERROR);
35
40
  }
36
41
  function restore() {
37
42
  const failedNodes = new Map(graph.nodes);
@@ -72,7 +77,173 @@ function restoreMap(target, saved) {
72
77
  target.clear();
73
78
  for (const [key, value] of saved) target.set(key, value);
74
79
  }
80
+ /**
81
+ * Starts observing `graph` for an edit that may only change properties. Node identities are
82
+ * kept as pointers, so replacing, adding, or removing a node anywhere is found; a node's fields
83
+ * are compared and restored only where the graph reported it, so code that writes into a node
84
+ * directly calls `SceneGraph.willChangeNode` first, and a node event without such a report fails
85
+ * the edit.
86
+ */
87
+ function captureTouchedCheckpoint(graph) {
88
+ const nodeRefs = new Map(graph.nodes);
89
+ const variableRefs = new Map(graph.variables);
90
+ const collectionRefs = new Map(graph.variableCollections);
91
+ const saved = structuredClone({
92
+ variables: graph.variables,
93
+ variableCollections: graph.variableCollections,
94
+ activeMode: graph.activeMode,
95
+ instanceIndex: graph.instanceIndex,
96
+ enabledLibraries: graph.enabledLibraries
97
+ });
98
+ const images = new Map(graph.images);
99
+ const { rootId, figKiwiVersion, figSchemaDeflated, documentColorSpace } = graph;
100
+ const nodes = /* @__PURE__ */ new Map();
101
+ const reported = /* @__PURE__ */ new Set();
102
+ const created = /* @__PURE__ */ new Set();
103
+ const impact = createSceneMutationImpact();
104
+ /** Copies a node as the edit found it: the original object, should the edit have replaced it. */
105
+ function copy(node) {
106
+ nodes.set(node.id, structuredClone(nodeRefs.get(node.id) ?? node));
107
+ }
108
+ const skipped = (node) => nodes.has(node.id) || created.has(node.id);
109
+ /**
110
+ * Copies `node` and each ancestor not copied yet, up to but excluding its page. An ancestor
111
+ * copied already had its own ancestors copied with it, so the walk stops there.
112
+ */
113
+ function copyLineage(node) {
114
+ if (skipped(node)) return;
115
+ copy(node);
116
+ if (!node.parentId) return;
117
+ graph.closest(node.parentId, (ancestor) => {
118
+ if (ancestor.type === "CANVAS" || ancestor.id === rootId || skipped(ancestor)) return true;
119
+ copy(ancestor);
120
+ return false;
121
+ });
122
+ }
123
+ const unobserve = graph.observeNodeChanges({
124
+ created: (node) => {
125
+ created.add(node.id);
126
+ reported.add(node.id);
127
+ },
128
+ before: (node) => {
129
+ reported.add(node.id);
130
+ copyLineage(node);
131
+ }
132
+ });
133
+ const unbindEvents = recordSceneMutations(graph, impact);
134
+ let observing = true;
135
+ function stop() {
136
+ if (!observing) return;
137
+ observing = false;
138
+ unobserve();
139
+ unbindEvents();
140
+ }
141
+ function assertNodes() {
142
+ if (created.size > 0 || impact.deletedNodeIds.size > 0 || graph.nodes.size !== nodeRefs.size || graph.variables.size !== variableRefs.size) throw new Error(NODE_COUNT_ERROR);
143
+ for (const [id, node] of graph.nodes) {
144
+ const original = nodeRefs.get(id);
145
+ if (!original) throw new Error(NODE_COUNT_ERROR);
146
+ if (original !== node) throw new Error(NODE_IDENTITY_ERROR);
147
+ }
148
+ for (const [id, before] of nodes) {
149
+ const after = graph.nodes.get(id);
150
+ if (!after || before.id !== after.id || before.type !== after.type || before.parentId !== after.parentId || before.componentId !== after.componentId || !isEqual(before.childIds, after.childIds)) throw new Error(NODE_IDENTITY_ERROR);
151
+ }
152
+ for (const id of [
153
+ ...impact.changedNodeIds,
154
+ ...impact.previousParentIds,
155
+ ...impact.currentParentIds
156
+ ]) if (!nodes.has(id) && !created.has(id)) throw new Error(`Atomic tools must report layer changes to the scene graph (SceneGraph.willChangeNode): ${id}`);
157
+ }
158
+ function assertPropertiesOnly() {
159
+ assertNodes();
160
+ for (const [id, before] of saved.variables) {
161
+ const after = graph.variables.get(id);
162
+ if (!after || after !== variableRefs.get(id) || before.id !== after.id || before.collectionId !== after.collectionId || before.type !== after.type) throw new Error(VARIABLE_IDENTITY_ERROR);
163
+ }
164
+ if (!isEqual(saved.variableCollections, graph.variableCollections) || !isEqual(saved.activeMode, graph.activeMode) || !isEqual(saved.instanceIndex, graph.instanceIndex) || !isEqual(saved.enabledLibraries, graph.enabledLibraries) || images.size !== graph.images.size || [...images].some(([id, bytes]) => graph.images.get(id) !== bytes) || graph.rootId !== rootId || graph.documentColorSpace !== documentColorSpace || graph.figKiwiVersion !== figKiwiVersion || graph.figSchemaDeflated !== figSchemaDeflated) throw new Error(DOCUMENT_ERROR);
165
+ }
166
+ /** Puts the node map back as it was, in its order, if the edit changed its entries. */
167
+ function restoreNodeMap() {
168
+ const removed = /* @__PURE__ */ new Map();
169
+ const readded = /* @__PURE__ */ new Set();
170
+ let changed = graph.nodes.size !== nodeRefs.size;
171
+ if (!changed) {
172
+ const live = graph.nodes.entries();
173
+ for (const [id, node] of nodeRefs) {
174
+ const next = live.next();
175
+ if (next.done || next.value[0] !== id || next.value[1] !== node) {
176
+ changed = true;
177
+ break;
178
+ }
179
+ }
180
+ }
181
+ if (!changed) return {
182
+ removed,
183
+ readded
184
+ };
185
+ const failed = new Map(graph.nodes);
186
+ graph.nodes.clear();
187
+ for (const [id, node] of nodeRefs) {
188
+ graph.nodes.set(id, node);
189
+ if (!failed.has(id)) readded.add(id);
190
+ }
191
+ for (const [id, node] of failed) if (!nodeRefs.has(id)) removed.set(id, node);
192
+ return {
193
+ removed,
194
+ readded
195
+ };
196
+ }
197
+ function restore() {
198
+ stop();
199
+ const { removed, readded } = restoreNodeMap();
200
+ for (const [id, copy] of nodes) {
201
+ const original = nodeRefs.get(id);
202
+ if (!original) continue;
203
+ for (const key of Object.keys(original)) if (!Object.hasOwn(copy, key)) Reflect.deleteProperty(original, key);
204
+ Object.assign(original, structuredClone(copy));
205
+ }
206
+ restoreObjects(graph.variables, saved.variables, variableRefs);
207
+ restoreObjects(graph.variableCollections, saved.variableCollections, collectionRefs);
208
+ restoreMap(graph.activeMode, structuredClone(saved.activeMode));
209
+ restoreMap(graph.instanceIndex, structuredClone(saved.instanceIndex));
210
+ restoreMap(graph.enabledLibraries, structuredClone(saved.enabledLibraries));
211
+ restoreMap(graph.images, images);
212
+ graph.rootId = rootId;
213
+ graph.figKiwiVersion = figKiwiVersion;
214
+ graph.figSchemaDeflated = figSchemaDeflated;
215
+ graph.documentColorSpace = documentColorSpace;
216
+ graph.clearAbsPosCache();
217
+ const touched = /* @__PURE__ */ new Set([
218
+ ...nodes.keys(),
219
+ ...created,
220
+ ...readded,
221
+ ...removed.keys(),
222
+ ...impact.changedNodeIds,
223
+ ...impact.previousParentIds,
224
+ ...impact.currentParentIds
225
+ ]);
226
+ for (const id of touched) if (graph.nodes.get(id)?.type === "CONNECTOR") graph.connectorIds.add(id);
227
+ else graph.connectorIds.delete(id);
228
+ for (const [id, node] of removed) graph.emitter.emit("node:deleted", id, node.parentId);
229
+ for (const id of touched) {
230
+ const node = graph.nodes.get(id);
231
+ if (!node) continue;
232
+ if (readded.has(id)) graph.emitter.emit("node:created", node);
233
+ else graph.emitter.emit("node:updated", id, node);
234
+ }
235
+ }
236
+ return {
237
+ nodes,
238
+ reported,
239
+ variables: saved.variables,
240
+ variableCollections: saved.variableCollections,
241
+ assertPropertiesOnly,
242
+ restore,
243
+ stop
244
+ };
245
+ }
75
246
  //#endregion
76
- export { captureGraphCheckpoint };
247
+ export { captureGraphCheckpoint, captureTouchedCheckpoint };
77
248
 
78
249
  //# sourceMappingURL=checkpoint.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkpoint.js","names":[],"sources":["../src/checkpoint.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit'\n\nimport type { SceneGraph } from './graph'\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,SAA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7D,OAAO,MAAM;CACb,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,OAAO,IAAI,KAAK,KAAK;AACzD"}
1
+ {"version":3,"file":"checkpoint.js","names":[],"sources":["../src/checkpoint.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit'\n\nimport type { SceneGraph } from './graph'\nimport { createSceneMutationImpact, recordSceneMutations } from './mutation-impact'\nimport type { SceneNode, Variable, VariableCollection } from './types'\n\nconst NODE_COUNT_ERROR = 'Atomic tools must not create or remove nodes or variables'\nconst NODE_IDENTITY_ERROR = 'Atomic tools must not change node hierarchy or identity'\nconst VARIABLE_IDENTITY_ERROR = 'Atomic tools must not change variable identity'\nconst DOCUMENT_ERROR = 'Atomic tools must not change collections, assets or document metadata'\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(NODE_COUNT_ERROR)\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(NODE_IDENTITY_ERROR)\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(VARIABLE_IDENTITY_ERROR)\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(DOCUMENT_ERROR)\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\n/**\n * Rollback state for an edit that may only change properties, which costs what the edit touches\n * rather than a copy of the document: a node is copied the first time the graph reports it about\n * to change (`SceneGraph.observeNodeChanges`), together with its ancestors below the page, which\n * keep instance override maps the graph writes into in place.\n */\nexport interface TouchedCheckpoint {\n /** Copies of the reported nodes and their ancestors below the page, as they were before. */\n readonly nodes: ReadonlyMap<string, SceneNode>\n /** Nodes the graph reported changing or creating. */\n readonly reported: ReadonlySet<string>\n /** Copies of every variable, as variables have no change report. */\n readonly variables: ReadonlyMap<string, Variable>\n readonly variableCollections: ReadonlyMap<string, VariableCollection>\n /** Throws unless the edit only changed properties of existing nodes and variables. */\n assertPropertiesOnly(): void\n /** Stops observing and puts back everything the edit changed, keeping object identities. */\n restore(): void\n /** Stops observing the graph. */\n stop(): void\n}\n\n/**\n * Starts observing `graph` for an edit that may only change properties. Node identities are\n * kept as pointers, so replacing, adding, or removing a node anywhere is found; a node's fields\n * are compared and restored only where the graph reported it, so code that writes into a node\n * directly calls `SceneGraph.willChangeNode` first, and a node event without such a report fails\n * the edit.\n */\nexport function captureTouchedCheckpoint(graph: SceneGraph): TouchedCheckpoint {\n const nodeRefs = new Map(graph.nodes)\n const variableRefs = new Map(graph.variables)\n const collectionRefs = new Map(graph.variableCollections)\n const saved = structuredClone({\n variables: graph.variables,\n variableCollections: graph.variableCollections,\n activeMode: graph.activeMode,\n instanceIndex: graph.instanceIndex,\n enabledLibraries: graph.enabledLibraries\n })\n // Property edits cannot edit assets. Retain existing buffers, not copies of every bitmap.\n const images = new Map(graph.images)\n const { rootId, figKiwiVersion, figSchemaDeflated, documentColorSpace } = graph\n const nodes = new Map<string, SceneNode>()\n const reported = new Set<string>()\n const created = new Set<string>()\n const impact = createSceneMutationImpact()\n\n /** Copies a node as the edit found it: the original object, should the edit have replaced it. */\n function copy(node: SceneNode) {\n nodes.set(node.id, structuredClone(nodeRefs.get(node.id) ?? node))\n }\n\n const skipped = (node: SceneNode) => nodes.has(node.id) || created.has(node.id)\n\n /**\n * Copies `node` and each ancestor not copied yet, up to but excluding its page. An ancestor\n * copied already had its own ancestors copied with it, so the walk stops there.\n */\n function copyLineage(node: SceneNode) {\n if (skipped(node)) return\n copy(node)\n if (!node.parentId) return\n graph.closest(node.parentId, (ancestor) => {\n if (ancestor.type === 'CANVAS' || ancestor.id === rootId || skipped(ancestor)) return true\n copy(ancestor)\n return false\n })\n }\n\n const unobserve = graph.observeNodeChanges({\n created: (node) => {\n created.add(node.id)\n reported.add(node.id)\n },\n before: (node) => {\n reported.add(node.id)\n copyLineage(node)\n }\n })\n const unbindEvents = recordSceneMutations(graph, impact)\n let observing = true\n\n function stop() {\n if (!observing) return\n observing = false\n unobserve()\n unbindEvents()\n }\n\n function assertNodes() {\n if (\n created.size > 0 ||\n impact.deletedNodeIds.size > 0 ||\n graph.nodes.size !== nodeRefs.size ||\n graph.variables.size !== variableRefs.size\n )\n throw new Error(NODE_COUNT_ERROR)\n for (const [id, node] of graph.nodes) {\n const original = nodeRefs.get(id)\n if (!original) throw new Error(NODE_COUNT_ERROR)\n if (original !== node) throw new Error(NODE_IDENTITY_ERROR)\n }\n for (const [id, before] of nodes) {\n const after = graph.nodes.get(id)\n if (\n !after ||\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(NODE_IDENTITY_ERROR)\n }\n for (const id of [\n ...impact.changedNodeIds,\n ...impact.previousParentIds,\n ...impact.currentParentIds\n ]) {\n if (!nodes.has(id) && !created.has(id)) {\n throw new Error(\n `Atomic tools must report layer changes to the scene graph (SceneGraph.willChangeNode): ${id}`\n )\n }\n }\n }\n\n function assertPropertiesOnly() {\n assertNodes()\n for (const [id, before] of saved.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(VARIABLE_IDENTITY_ERROR)\n }\n if (\n !isEqual(saved.variableCollections, graph.variableCollections) ||\n !isEqual(saved.activeMode, graph.activeMode) ||\n !isEqual(saved.instanceIndex, graph.instanceIndex) ||\n !isEqual(saved.enabledLibraries, graph.enabledLibraries) ||\n images.size !== graph.images.size ||\n [...images].some(([id, bytes]) => graph.images.get(id) !== bytes) ||\n graph.rootId !== rootId ||\n graph.documentColorSpace !== documentColorSpace ||\n graph.figKiwiVersion !== figKiwiVersion ||\n // Every writer assigns a new schema rather than writing into it.\n graph.figSchemaDeflated !== figSchemaDeflated\n )\n throw new Error(DOCUMENT_ERROR)\n }\n\n /** Puts the node map back as it was, in its order, if the edit changed its entries. */\n function restoreNodeMap(): { removed: Map<string, SceneNode>; readded: Set<string> } {\n const removed = new Map<string, SceneNode>()\n const readded = new Set<string>()\n let changed = graph.nodes.size !== nodeRefs.size\n if (!changed) {\n // In lockstep, so a node removed and set again, which moves to the end, is put back too.\n const live = graph.nodes.entries()\n for (const [id, node] of nodeRefs) {\n const next = live.next()\n if (next.done || next.value[0] !== id || next.value[1] !== node) {\n changed = true\n break\n }\n }\n }\n if (!changed) return { removed, readded }\n const failed = new Map(graph.nodes)\n graph.nodes.clear()\n for (const [id, node] of nodeRefs) {\n graph.nodes.set(id, node)\n if (!failed.has(id)) readded.add(id)\n }\n for (const [id, node] of failed) if (!nodeRefs.has(id)) removed.set(id, node)\n return { removed, readded }\n }\n\n function restore() {\n stop()\n const { removed, readded } = restoreNodeMap()\n for (const [id, copy] of nodes) {\n const original = nodeRefs.get(id)\n if (!original) continue\n for (const key of Object.keys(original)) {\n if (!Object.hasOwn(copy, key)) Reflect.deleteProperty(original, key)\n }\n Object.assign(original, structuredClone(copy))\n }\n restoreObjects(graph.variables, saved.variables, variableRefs)\n restoreObjects(graph.variableCollections, saved.variableCollections, collectionRefs)\n restoreMap(graph.activeMode, structuredClone(saved.activeMode))\n restoreMap(graph.instanceIndex, structuredClone(saved.instanceIndex))\n restoreMap(graph.enabledLibraries, structuredClone(saved.enabledLibraries))\n restoreMap(graph.images, images)\n graph.rootId = rootId\n graph.figKiwiVersion = figKiwiVersion\n graph.figSchemaDeflated = figSchemaDeflated\n graph.documentColorSpace = documentColorSpace\n graph.clearAbsPosCache()\n const touched = new Set([\n ...nodes.keys(),\n ...created,\n ...readded,\n ...removed.keys(),\n ...impact.changedNodeIds,\n ...impact.previousParentIds,\n ...impact.currentParentIds\n ])\n for (const id of touched) {\n if (graph.nodes.get(id)?.type === 'CONNECTOR') graph.connectorIds.add(id)\n else graph.connectorIds.delete(id)\n }\n // Notify consumers only after the complete hierarchy and indexes have been restored.\n for (const [id, node] of removed) graph.emitter.emit('node:deleted', id, node.parentId)\n for (const id of touched) {\n const node = graph.nodes.get(id)\n if (!node) continue\n if (readded.has(id)) graph.emitter.emit('node:created', node)\n else graph.emitter.emit('node:updated', id, node)\n }\n }\n\n return {\n nodes,\n reported,\n variables: saved.variables,\n variableCollections: saved.variableCollections,\n assertPropertiesOnly,\n restore,\n stop\n }\n}\n"],"mappings":";;;AAMA,MAAM,mBAAmB;AACzB,MAAM,sBAAsB;AAC5B,MAAM,0BAA0B;AAChC,MAAM,iBAAiB;;AAGvB,SAAgB,uBAAuB,OAAmB;CACxD,MAAM,WAAW,IAAI,IAAI,MAAM,KAAK;CACpC,MAAM,eAAe,IAAI,IAAI,MAAM,SAAS;CAC5C,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ACH,GAAG;GACH,GAAG,QAAQ,KAAK;GAChB,GAAG,OAAO;GACV,GAAG,OAAO;GACV,GAAG,OAAO;EACZ,CAAC;EACD,KAAK,MAAM,MAAM,SACf,IAAI,MAAM,MAAM,IAAI,EAAE,CAAC,EAAE,SAAS,aAAa,MAAM,aAAa,IAAI,EAAE;OACnE,MAAM,aAAa,OAAO,EAAE;EAGnC,KAAK,MAAM,CAAC,IAAI,SAAS,SAAS,MAAM,QAAQ,KAAK,gBAAgB,IAAI,KAAK,QAAQ;EACtF,KAAK,MAAM,MAAM,SAAS;GACxB,MAAM,OAAO,MAAM,MAAM,IAAI,EAAE;GAC/B,IAAI,CAAC,MAAM;GACX,IAAI,QAAQ,IAAI,EAAE,GAAG,MAAM,QAAQ,KAAK,gBAAgB,IAAI;QACvD,MAAM,QAAQ,KAAK,gBAAgB,IAAI,IAAI;EAClD;CACF;CAEA,OAAO;EACL;EACA;EACA,WAAW,MAAM;EACjB,qBAAqB,MAAM;EAC3B;EACA;EACA;CACF;AACF"}
@@ -1,4 +1,4 @@
1
- import { ComponentPropertyTarget, applyComponentPropertyValue, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, findComponentPropertyTarget, findComponentPropertyTargets, removeComponentProperty, resolveComponentPropertyValue } from "./properties.js";
2
- import { compareCanvasPosition, defaultVariant, restingVariant, variantDefaultValue } from "./variants.js";
1
+ import { ComponentPropertyTarget, applyComponentPropertyValue, assignmentOverrideField, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, findComponentPropertyTarget, findComponentPropertyTargets, moveVariantPropertiesToSet, overriddenAssignmentId, reapplyComponentPropertyAssignments, recordAssignmentOverride, removeComponentProperty, resolveComponentPropertyValue } from "./properties.js";
2
+ import { compareCanvasPosition, defaultVariant, resolvedVariantDefinition, variantDefaultValue, variantOptions } from "./variants.js";
3
3
  import { InstanceExposureIssue, exposableInstances, exposedInstances, instanceExposureIssue } from "./exposure.js";
4
- export { ComponentPropertyTarget, type InstanceExposureIssue, applyComponentPropertyValue, compareCanvasPosition, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, defaultVariant, exposableInstances, exposedInstances, findComponentPropertyTarget, findComponentPropertyTargets, instanceExposureIssue, removeComponentProperty, resolveComponentPropertyValue, restingVariant, variantDefaultValue };
4
+ export { ComponentPropertyTarget, type InstanceExposureIssue, applyComponentPropertyValue, assignmentOverrideField, compareCanvasPosition, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, defaultVariant, exposableInstances, exposedInstances, findComponentPropertyTarget, findComponentPropertyTargets, instanceExposureIssue, moveVariantPropertiesToSet, overriddenAssignmentId, reapplyComponentPropertyAssignments, recordAssignmentOverride, removeComponentProperty, resolveComponentPropertyValue, resolvedVariantDefinition, variantDefaultValue, variantOptions };
@@ -1,4 +1,4 @@
1
- import { applyComponentPropertyValue, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, findComponentPropertyTarget, findComponentPropertyTargets, removeComponentProperty, resolveComponentPropertyValue } from "./properties.js";
2
- import { compareCanvasPosition, defaultVariant, restingVariant, variantDefaultValue } from "./variants.js";
1
+ import { applyComponentPropertyValue, assignmentOverrideField, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, findComponentPropertyTarget, findComponentPropertyTargets, moveVariantPropertiesToSet, overriddenAssignmentId, reapplyComponentPropertyAssignments, recordAssignmentOverride, removeComponentProperty, resolveComponentPropertyValue } from "./properties.js";
2
+ import { compareCanvasPosition, defaultVariant, resolvedVariantDefinition, variantDefaultValue, variantOptions } from "./variants.js";
3
3
  import { exposableInstances, exposedInstances, instanceExposureIssue } from "./exposure.js";
4
- export { applyComponentPropertyValue, compareCanvasPosition, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, defaultVariant, exposableInstances, exposedInstances, findComponentPropertyTarget, findComponentPropertyTargets, instanceExposureIssue, removeComponentProperty, resolveComponentPropertyValue, restingVariant, variantDefaultValue };
4
+ export { applyComponentPropertyValue, assignmentOverrideField, compareCanvasPosition, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, defaultVariant, exposableInstances, exposedInstances, findComponentPropertyTarget, findComponentPropertyTargets, instanceExposureIssue, moveVariantPropertiesToSet, overriddenAssignmentId, reapplyComponentPropertyAssignments, recordAssignmentOverride, removeComponentProperty, resolveComponentPropertyValue, resolvedVariantDefinition, variantDefaultValue, variantOptions };
@@ -12,9 +12,21 @@ declare function resolveComponentPropertyValue(graph: SceneGraph, value: string)
12
12
  declare function findComponentPropertyTarget(graph: SceneGraph, instance: SceneNode, propertyId: string): ComponentPropertyTarget | null;
13
13
  declare function findComponentPropertyTargets(graph: SceneGraph, instance: SceneNode, propertyId: string): ComponentPropertyTarget[];
14
14
  declare function applyComponentPropertyValue(graph: SceneGraph, instanceId: string, definition: ComponentPropertyDefinition, value: string): SceneNode | null;
15
+ declare function reapplyComponentPropertyAssignments(graph: SceneGraph, instanceId: string): ComponentPropertyDefinition[];
16
+ declare function assignmentOverrideField(propertyId: string): string;
17
+ declare function overriddenAssignmentId(field: string): string | undefined;
18
+ declare function recordAssignmentOverride(graph: SceneGraph, instanceId: string, propertyId: string): void;
15
19
  declare function removeComponentProperty(graph: SceneGraph, ownerId: string, propertyId: string): boolean;
20
+ /**
21
+ * Moves the boolean, text, instance swap, and slot properties a set's variants define themselves
22
+ * onto the set, under their IDs, so the layers and instances naming them still do: a variant's
23
+ * properties are its set's, as Figma keeps them, and combining components moves theirs to the
24
+ * set (desktop 126). A property the set already has under that ID is not added again. Returns
25
+ * the IDs of the set and the variants it changed.
26
+ */
27
+ declare function moveVariantPropertiesToSet(graph: SceneGraph, setId: string): string[];
16
28
  /** A new component property ID, in the `prop:` form the editor, plugin API, and design JSX share. */
17
29
  declare function createComponentPropertyId(): string;
18
30
  //#endregion
19
- export { ComponentPropertyTarget, applyComponentPropertyValue, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, findComponentPropertyTarget, findComponentPropertyTargets, removeComponentProperty, resolveComponentPropertyValue };
31
+ export { ComponentPropertyTarget, applyComponentPropertyValue, assignmentOverrideField, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, findComponentPropertyTarget, findComponentPropertyTargets, moveVariantPropertiesToSet, overriddenAssignmentId, reapplyComponentPropertyAssignments, recordAssignmentOverride, removeComponentProperty, resolveComponentPropertyValue };
20
32
  //# sourceMappingURL=properties.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"properties.d.ts","names":[],"sources":["../../src/components/properties.ts"],"mappings":";;;UAYiB;EACf,MAAM;EACN,OAAO;EACP,QAAQ;;iBAGM,wBAAwB,OAAO,YAAY,UAAU,YAAY;iBAQjE,6BACd,OAAO,YACP,UAAU,YACT;iBAUa,8BAA8B,OAAO,YAAY,gBAAgB;iBAkBjE,4BACd,OAAO,YACP,UAAU,WACV,qBACC;iBAIa,6BACd,OAAO,YACP,UAAU,WACV,qBACC;iBA4Ea,4BACd,OAAO,YACP,oBACA,YAAY,6BACZ,gBACC;iBA6Da,wBACd,OAAO,YACP,iBACA;;iBAwBc"}
1
+ {"version":3,"file":"properties.d.ts","names":[],"sources":["../../src/components/properties.ts"],"mappings":";;;UAWiB;EACf,MAAM;EACN,OAAO;EACP,QAAQ;;iBAGM,wBAAwB,OAAO,YAAY,UAAU,YAAY;iBAQjE,6BACd,OAAO,YACP,UAAU,YACT;iBAUa,8BAA8B,OAAO,YAAY,gBAAgB;iBAkBjE,4BACd,OAAO,YACP,UAAU,WACV,qBACC;iBAIa,6BACd,OAAO,YACP,UAAU,WACV,qBACC;iBA4Ea,4BACd,OAAO,YACP,oBACA,YAAY,6BACZ,gBACC;iBAwBa,oCACd,OAAO,YACP,qBACC;iBAqBa,wBAAwB;iBAIxB,uBAAuB;iBAMvB,yBACd,OAAO,YACP,oBACA;iBAsDc,wBACd,OAAO,YACP,iBACA;;;;;;;;iBA8Bc,2BAA2B,OAAO,YAAY;;iBA+B9C"}
@@ -1,7 +1,6 @@
1
1
  import { setInstanceOverride } from "../instances/overrides.js";
2
2
  import { randomHex } from "../random.js";
3
- import { instanceMainComponent } from "../instances/main-component.js";
4
- import { findInstanceAncestor } from "../instances/index.js";
3
+ import { findInstanceAncestor, instanceMainComponent } from "../instances/main-component.js";
5
4
  import { walkInstanceSources } from "../instances/source-walk.js";
6
5
  //#region src/components/properties.ts
7
6
  function componentPropertyOwners(graph, instance) {
@@ -83,6 +82,34 @@ function applyComponentPropertyValue(graph, instanceId, definition, value) {
83
82
  } });
84
83
  return definition.type === "INSTANCE_SWAP" ? target : instance;
85
84
  }
85
+ function reapplyComponentPropertyAssignments(graph, instanceId) {
86
+ const instance = graph.getNode(instanceId);
87
+ if (instance?.type !== "INSTANCE") return [];
88
+ const definitions = new Map(componentPropertyDefinitions(graph, instance).map((definition) => [definition.id, definition]));
89
+ const applied = [];
90
+ for (const [propertyId, value] of Object.entries(instance.componentPropertyAssignments)) {
91
+ const definition = definitions.get(propertyId);
92
+ if (definition && definition.type !== "VARIANT") {
93
+ applyComponentPropertyValue(graph, instanceId, definition, value);
94
+ applied.push(definition);
95
+ }
96
+ }
97
+ return applied;
98
+ }
99
+ const ASSIGNMENT_OVERRIDE_PREFIX = "componentPropertyAssignments/";
100
+ function assignmentOverrideField(propertyId) {
101
+ return `${ASSIGNMENT_OVERRIDE_PREFIX}${propertyId}`;
102
+ }
103
+ function overriddenAssignmentId(field) {
104
+ return field.startsWith(ASSIGNMENT_OVERRIDE_PREFIX) ? field.slice(29) : void 0;
105
+ }
106
+ function recordAssignmentOverride(graph, instanceId, propertyId) {
107
+ const instance = graph.getNode(instanceId);
108
+ const owner = instance?.parentId ? findInstanceAncestor(graph, instance.parentId) : void 0;
109
+ if (!instance || !owner) return;
110
+ setInstanceOverride(owner.instanceOverrides, owner.id, instance.id, assignmentOverrideField(propertyId), instance.componentPropertyAssignments[propertyId]);
111
+ graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides });
112
+ }
86
113
  function componentSubtree(graph, componentId) {
87
114
  const component = graph.getNode(componentId);
88
115
  if (!component) return [];
@@ -115,11 +142,39 @@ function removeComponentProperty(graph, ownerId, propertyId) {
115
142
  for (const node of nodes) removePropertyFromNode(graph, node, propertyId);
116
143
  return true;
117
144
  }
145
+ /**
146
+ * Moves the boolean, text, instance swap, and slot properties a set's variants define themselves
147
+ * onto the set, under their IDs, so the layers and instances naming them still do: a variant's
148
+ * properties are its set's, as Figma keeps them, and combining components moves theirs to the
149
+ * set (desktop 126). A property the set already has under that ID is not added again. Returns
150
+ * the IDs of the set and the variants it changed.
151
+ */
152
+ function moveVariantPropertiesToSet(graph, setId) {
153
+ const set = graph.getNode(setId);
154
+ if (set?.type !== "COMPONENT_SET") return [];
155
+ const changed = [];
156
+ const known = new Set(set.componentPropertyDefinitions.map((definition) => definition.id));
157
+ const moved = [];
158
+ for (const variant of graph.getChildren(setId)) {
159
+ const own = variant.componentPropertyDefinitions.filter((definition) => definition.type !== "VARIANT");
160
+ if (variant.type !== "COMPONENT" || own.length === 0) continue;
161
+ for (const definition of own) {
162
+ if (known.has(definition.id)) continue;
163
+ known.add(definition.id);
164
+ moved.push(definition);
165
+ }
166
+ graph.updateNode(variant.id, { componentPropertyDefinitions: variant.componentPropertyDefinitions.filter((definition) => definition.type === "VARIANT") });
167
+ changed.push(variant.id);
168
+ }
169
+ if (moved.length === 0) return changed;
170
+ graph.updateNode(setId, { componentPropertyDefinitions: [...moved, ...set.componentPropertyDefinitions] });
171
+ return [setId, ...changed];
172
+ }
118
173
  /** A new component property ID, in the `prop:` form the editor, plugin API, and design JSX share. */
119
174
  function createComponentPropertyId() {
120
175
  return `prop:${randomHex(8)}`;
121
176
  }
122
177
  //#endregion
123
- export { applyComponentPropertyValue, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, findComponentPropertyTarget, findComponentPropertyTargets, removeComponentProperty, resolveComponentPropertyValue };
178
+ export { applyComponentPropertyValue, assignmentOverrideField, componentPropertyDefinitions, componentPropertyOwners, createComponentPropertyId, findComponentPropertyTarget, findComponentPropertyTargets, moveVariantPropertiesToSet, overriddenAssignmentId, reapplyComponentPropertyAssignments, recordAssignmentOverride, removeComponentProperty, resolveComponentPropertyValue };
124
179
 
125
180
  //# sourceMappingURL=properties.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"properties.js","names":[],"sources":["../../src/components/properties.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport { findInstanceAncestor } from '../instances'\nimport { instanceMainComponent } from '../instances/main-component'\nimport { setInstanceOverride } from '../instances/overrides'\nimport { walkInstanceSources } from '../instances/source-walk'\nimport { randomHex } from '../random'\nimport type {\n ComponentPropertyDefinition,\n ComponentPropertyReferenceField,\n SceneNode\n} from '../types'\n\nexport interface ComponentPropertyTarget {\n node: SceneNode\n field: ComponentPropertyReferenceField\n source: SceneNode\n}\n\nexport function componentPropertyOwners(graph: SceneGraph, instance: SceneNode): SceneNode[] {\n if (instance.type !== 'INSTANCE') return []\n const component = instanceMainComponent(graph, instance)\n if (!component) return []\n const parent = component.parentId ? graph.getNode(component.parentId) : null\n return parent?.type === 'COMPONENT_SET' ? [parent, component] : [component]\n}\n\nexport function componentPropertyDefinitions(\n graph: SceneGraph,\n instance: SceneNode\n): ComponentPropertyDefinition[] {\n const definitions = new Map<string, ComponentPropertyDefinition>()\n for (const owner of componentPropertyOwners(graph, instance)) {\n for (const definition of owner.componentPropertyDefinitions) {\n if (!definitions.has(definition.id)) definitions.set(definition.id, definition)\n }\n }\n return [...definitions.values()]\n}\n\nexport function resolveComponentPropertyValue(graph: SceneGraph, value: string): SceneNode | null {\n const direct = graph.getNode(value)\n if (direct?.type === 'COMPONENT') return direct\n if (direct?.type === 'COMPONENT_SET') {\n const componentId = direct.childIds.find((id) => graph.getNode(id)?.type === 'COMPONENT')\n return componentId ? (graph.getNode(componentId) ?? null) : null\n }\n for (const node of graph.getAllNodes()) {\n if (\n node.type === 'COMPONENT' &&\n (node.componentKey === value || node.sourceLibraryKey === value || node.source.id === value)\n ) {\n return node\n }\n }\n return null\n}\n\nexport function findComponentPropertyTarget(\n graph: SceneGraph,\n instance: SceneNode,\n propertyId: string\n): ComponentPropertyTarget | null {\n return findComponentPropertyTargets(graph, instance, propertyId)[0] ?? null\n}\n\nexport function findComponentPropertyTargets(\n graph: SceneGraph,\n instance: SceneNode,\n propertyId: string\n): ComponentPropertyTarget[] {\n const targets: ComponentPropertyTarget[] = []\n walkInstanceSources(\n graph,\n instance,\n (source, target) => {\n const reference = source.componentPropertyReferences.find(\n (candidate) => candidate.propertyId === propertyId\n )\n if (reference) targets.push({ node: target, field: reference.field, source })\n return true\n },\n true\n )\n return targets\n}\n\nfunction applyTextProperty(\n graph: SceneGraph,\n owner: SceneNode,\n targets: ComponentPropertyTarget[],\n value: string\n): void {\n for (const item of targets) {\n if (item.field !== 'TEXT' || item.node.type !== 'TEXT') continue\n graph.updateNode(item.node.id, { text: value })\n if (findInstanceAncestor(graph, item.node.id)) {\n setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'text', value)\n }\n }\n}\n\nfunction applyBooleanProperty(\n graph: SceneGraph,\n owner: SceneNode,\n targets: ComponentPropertyTarget[],\n value: string\n): void {\n for (const item of targets) {\n if (item.field !== 'VISIBLE') continue\n const instance = findInstanceAncestor(graph, item.node.id)\n graph.updateNode(item.node.id, { visible: value === 'true' })\n if (instance) {\n setInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n item.node.id,\n 'visible',\n value === 'true'\n )\n }\n }\n}\n\nfunction applyInstanceSwapProperty(\n graph: SceneGraph,\n owner: SceneNode,\n targets: ComponentPropertyTarget[],\n target: SceneNode\n): void {\n for (const item of targets) {\n if (item.field !== 'INSTANCE_SWAP' || item.node.type !== 'INSTANCE') continue\n graph.swapInstanceComponent(item.node.id, target.id)\n setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'name', target.name)\n setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'componentId', target.id)\n setInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n item.node.id,\n 'sourceComponentId',\n item.source.id\n )\n graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })\n }\n}\n\nexport function applyComponentPropertyValue(\n graph: SceneGraph,\n instanceId: string,\n definition: ComponentPropertyDefinition,\n value: string\n): SceneNode | null {\n const instance = graph.getNode(instanceId)\n if (instance?.type !== 'INSTANCE') return null\n if (definition.type === 'VARIANT') return null\n const targets = findComponentPropertyTargets(graph, instance, definition.id)\n const target =\n definition.type === 'INSTANCE_SWAP' ? resolveComponentPropertyValue(graph, value) : null\n if (definition.type === 'INSTANCE_SWAP' && !target) return null\n if (definition.type === 'TEXT') {\n applyTextProperty(graph, instance, targets, value)\n } else if (definition.type === 'BOOLEAN') {\n applyBooleanProperty(graph, instance, targets, value)\n } else if (target) {\n applyInstanceSwapProperty(graph, instance, targets, target)\n }\n graph.updateNode(instance.id, {\n componentPropertyAssignments: {\n ...instance.componentPropertyAssignments,\n [definition.id]: definition.type === 'INSTANCE_SWAP' && target ? target.id : value\n }\n })\n return definition.type === 'INSTANCE_SWAP' ? target : instance\n}\n\nfunction componentSubtree(graph: SceneGraph, componentId: string): SceneNode[] {\n const component = graph.getNode(componentId)\n if (!component) return []\n const nodes = [component]\n const visit = (node: SceneNode): void => {\n for (const child of graph.getChildren(node.id)) {\n nodes.push(child)\n visit(child)\n }\n }\n visit(component)\n return nodes\n}\n\nfunction componentInstances(graph: SceneGraph, componentId: string): SceneNode[] {\n return [...graph.getAllNodes()].filter(\n (node) => node.type === 'INSTANCE' && node.componentId === componentId\n )\n}\n\nfunction removePropertyFromNode(graph: SceneGraph, node: SceneNode, propertyId: string): void {\n if (node.componentPropertyReferences.some((reference) => reference.propertyId === propertyId)) {\n graph.updateNode(node.id, {\n componentPropertyReferences: node.componentPropertyReferences.filter(\n (reference) => reference.propertyId !== propertyId\n )\n })\n }\n if (node.type === 'INSTANCE' && propertyId in node.componentPropertyAssignments) {\n graph.updateNode(node.id, {\n componentPropertyAssignments: Object.fromEntries(\n Object.entries(node.componentPropertyAssignments).filter(([id]) => id !== propertyId)\n )\n })\n }\n}\n\nexport function removeComponentProperty(\n graph: SceneGraph,\n ownerId: string,\n propertyId: string\n): boolean {\n const owner = graph.getNode(ownerId)\n if (!owner || (owner.type !== 'COMPONENT' && owner.type !== 'COMPONENT_SET')) return false\n graph.updateNode(owner.id, {\n componentPropertyDefinitions: owner.componentPropertyDefinitions.filter(\n (definition) => definition.id !== propertyId\n )\n })\n const nodes = [\n ...componentSubtree(graph, owner.id),\n ...componentInstances(graph, owner.id),\n ...(owner.type === 'COMPONENT_SET'\n ? owner.childIds.flatMap((id) => [\n ...componentSubtree(graph, id),\n ...componentInstances(graph, id)\n ])\n : [])\n ]\n for (const node of nodes) removePropertyFromNode(graph, node, propertyId)\n return true\n}\n\n/** A new component property ID, in the `prop:` form the editor, plugin API, and design JSX share. */\nexport function createComponentPropertyId(): string {\n return `prop:${randomHex(8)}`\n}\n"],"mappings":";;;;;;AAkBA,SAAgB,wBAAwB,OAAmB,UAAkC;CAC3F,IAAI,SAAS,SAAS,YAAY,OAAO,CAAC;CAC1C,MAAM,YAAY,sBAAsB,OAAO,QAAQ;CACvD,IAAI,CAAC,WAAW,OAAO,CAAC;CACxB,MAAM,SAAS,UAAU,WAAW,MAAM,QAAQ,UAAU,QAAQ,IAAI;CACxE,OAAO,QAAQ,SAAS,kBAAkB,CAAC,QAAQ,SAAS,IAAI,CAAC,SAAS;AAC5E;AAEA,SAAgB,6BACd,OACA,UAC+B;CAC/B,MAAM,8BAAc,IAAI,IAAyC;CACjE,KAAK,MAAM,SAAS,wBAAwB,OAAO,QAAQ,GACzD,KAAK,MAAM,cAAc,MAAM,8BAC7B,IAAI,CAAC,YAAY,IAAI,WAAW,EAAE,GAAG,YAAY,IAAI,WAAW,IAAI,UAAU;CAGlF,OAAO,CAAC,GAAG,YAAY,OAAO,CAAC;AACjC;AAEA,SAAgB,8BAA8B,OAAmB,OAAiC;CAChG,MAAM,SAAS,MAAM,QAAQ,KAAK;CAClC,IAAI,QAAQ,SAAS,aAAa,OAAO;CACzC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I,MAAM,SAAS,kBACf,MAAM,SAAS,SAAS,OAAO,CAC7B,GAAG,iBAAiB,OAAO,EAAE,GAC7B,GAAG,mBAAmB,OAAO,EAAE,CACjC,CAAC,IACD,CAAC;CACP;CACA,KAAK,MAAM,QAAQ,OAAO,uBAAuB,OAAO,MAAM,UAAU;CACxE,OAAO;AACT;;AAGA,SAAgB,4BAAoC;CAClD,OAAO,QAAQ,UAAU,CAAC;AAC5B"}
1
+ {"version":3,"file":"properties.js","names":[],"sources":["../../src/components/properties.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport { findInstanceAncestor, instanceMainComponent } from '../instances/main-component'\nimport { setInstanceOverride } from '../instances/overrides'\nimport { walkInstanceSources } from '../instances/source-walk'\nimport { randomHex } from '../random'\nimport type {\n ComponentPropertyDefinition,\n ComponentPropertyReferenceField,\n SceneNode\n} from '../types'\n\nexport interface ComponentPropertyTarget {\n node: SceneNode\n field: ComponentPropertyReferenceField\n source: SceneNode\n}\n\nexport function componentPropertyOwners(graph: SceneGraph, instance: SceneNode): SceneNode[] {\n if (instance.type !== 'INSTANCE') return []\n const component = instanceMainComponent(graph, instance)\n if (!component) return []\n const parent = component.parentId ? graph.getNode(component.parentId) : null\n return parent?.type === 'COMPONENT_SET' ? [parent, component] : [component]\n}\n\nexport function componentPropertyDefinitions(\n graph: SceneGraph,\n instance: SceneNode\n): ComponentPropertyDefinition[] {\n const definitions = new Map<string, ComponentPropertyDefinition>()\n for (const owner of componentPropertyOwners(graph, instance)) {\n for (const definition of owner.componentPropertyDefinitions) {\n if (!definitions.has(definition.id)) definitions.set(definition.id, definition)\n }\n }\n return [...definitions.values()]\n}\n\nexport function resolveComponentPropertyValue(graph: SceneGraph, value: string): SceneNode | null {\n const direct = graph.getNode(value)\n if (direct?.type === 'COMPONENT') return direct\n if (direct?.type === 'COMPONENT_SET') {\n const componentId = direct.childIds.find((id) => graph.getNode(id)?.type === 'COMPONENT')\n return componentId ? (graph.getNode(componentId) ?? null) : null\n }\n for (const node of graph.getAllNodes()) {\n if (\n node.type === 'COMPONENT' &&\n (node.componentKey === value || node.sourceLibraryKey === value || node.source.id === value)\n ) {\n return node\n }\n }\n return null\n}\n\nexport function findComponentPropertyTarget(\n graph: SceneGraph,\n instance: SceneNode,\n propertyId: string\n): ComponentPropertyTarget | null {\n return findComponentPropertyTargets(graph, instance, propertyId)[0] ?? null\n}\n\nexport function findComponentPropertyTargets(\n graph: SceneGraph,\n instance: SceneNode,\n propertyId: string\n): ComponentPropertyTarget[] {\n const targets: ComponentPropertyTarget[] = []\n walkInstanceSources(\n graph,\n instance,\n (source, target) => {\n const reference = source.componentPropertyReferences.find(\n (candidate) => candidate.propertyId === propertyId\n )\n if (reference) targets.push({ node: target, field: reference.field, source })\n return true\n },\n true\n )\n return targets\n}\n\nfunction applyTextProperty(\n graph: SceneGraph,\n owner: SceneNode,\n targets: ComponentPropertyTarget[],\n value: string\n): void {\n for (const item of targets) {\n if (item.field !== 'TEXT' || item.node.type !== 'TEXT') continue\n graph.updateNode(item.node.id, { text: value })\n if (findInstanceAncestor(graph, item.node.id)) {\n setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'text', value)\n }\n }\n}\n\nfunction applyBooleanProperty(\n graph: SceneGraph,\n owner: SceneNode,\n targets: ComponentPropertyTarget[],\n value: string\n): void {\n for (const item of targets) {\n if (item.field !== 'VISIBLE') continue\n const instance = findInstanceAncestor(graph, item.node.id)\n graph.updateNode(item.node.id, { visible: value === 'true' })\n if (instance) {\n setInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n item.node.id,\n 'visible',\n value === 'true'\n )\n }\n }\n}\n\nfunction applyInstanceSwapProperty(\n graph: SceneGraph,\n owner: SceneNode,\n targets: ComponentPropertyTarget[],\n target: SceneNode\n): void {\n for (const item of targets) {\n if (item.field !== 'INSTANCE_SWAP' || item.node.type !== 'INSTANCE') continue\n graph.swapInstanceComponent(item.node.id, target.id)\n setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'name', target.name)\n setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'componentId', target.id)\n setInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n item.node.id,\n 'sourceComponentId',\n item.source.id\n )\n graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })\n }\n}\n\nexport function applyComponentPropertyValue(\n graph: SceneGraph,\n instanceId: string,\n definition: ComponentPropertyDefinition,\n value: string\n): SceneNode | null {\n const instance = graph.getNode(instanceId)\n if (instance?.type !== 'INSTANCE') return null\n if (definition.type === 'VARIANT') return null\n const targets = findComponentPropertyTargets(graph, instance, definition.id)\n const target =\n definition.type === 'INSTANCE_SWAP' ? resolveComponentPropertyValue(graph, value) : null\n if (definition.type === 'INSTANCE_SWAP' && !target) return null\n if (definition.type === 'TEXT') {\n applyTextProperty(graph, instance, targets, value)\n } else if (definition.type === 'BOOLEAN') {\n applyBooleanProperty(graph, instance, targets, value)\n } else if (target) {\n applyInstanceSwapProperty(graph, instance, targets, target)\n }\n graph.updateNode(instance.id, {\n componentPropertyAssignments: {\n ...instance.componentPropertyAssignments,\n [definition.id]: definition.type === 'INSTANCE_SWAP' && target ? target.id : value\n }\n })\n return definition.type === 'INSTANCE_SWAP' ? target : instance\n}\n\nexport function reapplyComponentPropertyAssignments(\n graph: SceneGraph,\n instanceId: string\n): ComponentPropertyDefinition[] {\n const instance = graph.getNode(instanceId)\n if (instance?.type !== 'INSTANCE') {\n return []\n }\n const definitions = new Map(\n componentPropertyDefinitions(graph, instance).map((definition) => [definition.id, definition])\n )\n const applied: ComponentPropertyDefinition[] = []\n for (const [propertyId, value] of Object.entries(instance.componentPropertyAssignments)) {\n const definition = definitions.get(propertyId)\n if (definition && definition.type !== 'VARIANT') {\n applyComponentPropertyValue(graph, instanceId, definition, value)\n applied.push(definition)\n }\n }\n return applied\n}\n\nconst ASSIGNMENT_OVERRIDE_PREFIX = 'componentPropertyAssignments/'\n\nexport function assignmentOverrideField(propertyId: string): string {\n return `${ASSIGNMENT_OVERRIDE_PREFIX}${propertyId}`\n}\n\nexport function overriddenAssignmentId(field: string): string | undefined {\n return field.startsWith(ASSIGNMENT_OVERRIDE_PREFIX)\n ? field.slice(ASSIGNMENT_OVERRIDE_PREFIX.length)\n : undefined\n}\n\nexport function recordAssignmentOverride(\n graph: SceneGraph,\n instanceId: string,\n propertyId: string\n): void {\n const instance = graph.getNode(instanceId)\n const owner = instance?.parentId ? findInstanceAncestor(graph, instance.parentId) : undefined\n if (!instance || !owner) {\n return\n }\n setInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n instance.id,\n assignmentOverrideField(propertyId),\n instance.componentPropertyAssignments[propertyId]\n )\n graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })\n}\n\nfunction componentSubtree(graph: SceneGraph, componentId: string): SceneNode[] {\n const component = graph.getNode(componentId)\n if (!component) return []\n const nodes = [component]\n const visit = (node: SceneNode): void => {\n for (const child of graph.getChildren(node.id)) {\n nodes.push(child)\n visit(child)\n }\n }\n visit(component)\n return nodes\n}\n\nfunction componentInstances(graph: SceneGraph, componentId: string): SceneNode[] {\n return [...graph.getAllNodes()].filter(\n (node) => node.type === 'INSTANCE' && node.componentId === componentId\n )\n}\n\nfunction removePropertyFromNode(graph: SceneGraph, node: SceneNode, propertyId: string): void {\n if (node.componentPropertyReferences.some((reference) => reference.propertyId === propertyId)) {\n graph.updateNode(node.id, {\n componentPropertyReferences: node.componentPropertyReferences.filter(\n (reference) => reference.propertyId !== propertyId\n )\n })\n }\n if (node.type === 'INSTANCE' && propertyId in node.componentPropertyAssignments) {\n graph.updateNode(node.id, {\n componentPropertyAssignments: Object.fromEntries(\n Object.entries(node.componentPropertyAssignments).filter(([id]) => id !== propertyId)\n )\n })\n }\n}\n\nexport function removeComponentProperty(\n graph: SceneGraph,\n ownerId: string,\n propertyId: string\n): boolean {\n const owner = graph.getNode(ownerId)\n if (!owner || (owner.type !== 'COMPONENT' && owner.type !== 'COMPONENT_SET')) return false\n graph.updateNode(owner.id, {\n componentPropertyDefinitions: owner.componentPropertyDefinitions.filter(\n (definition) => definition.id !== propertyId\n )\n })\n const nodes = [\n ...componentSubtree(graph, owner.id),\n ...componentInstances(graph, owner.id),\n ...(owner.type === 'COMPONENT_SET'\n ? owner.childIds.flatMap((id) => [\n ...componentSubtree(graph, id),\n ...componentInstances(graph, id)\n ])\n : [])\n ]\n for (const node of nodes) removePropertyFromNode(graph, node, propertyId)\n return true\n}\n\n/**\n * Moves the boolean, text, instance swap, and slot properties a set's variants define themselves\n * onto the set, under their IDs, so the layers and instances naming them still do: a variant's\n * properties are its set's, as Figma keeps them, and combining components moves theirs to the\n * set (desktop 126). A property the set already has under that ID is not added again. Returns\n * the IDs of the set and the variants it changed.\n */\nexport function moveVariantPropertiesToSet(graph: SceneGraph, setId: string): string[] {\n const set = graph.getNode(setId)\n if (set?.type !== 'COMPONENT_SET') return []\n const changed: string[] = []\n const known = new Set(set.componentPropertyDefinitions.map((definition) => definition.id))\n const moved: ComponentPropertyDefinition[] = []\n for (const variant of graph.getChildren(setId)) {\n const own = variant.componentPropertyDefinitions.filter(\n (definition) => definition.type !== 'VARIANT'\n )\n if (variant.type !== 'COMPONENT' || own.length === 0) continue\n for (const definition of own) {\n if (known.has(definition.id)) continue\n known.add(definition.id)\n moved.push(definition)\n }\n graph.updateNode(variant.id, {\n componentPropertyDefinitions: variant.componentPropertyDefinitions.filter(\n (definition) => definition.type === 'VARIANT'\n )\n })\n changed.push(variant.id)\n }\n if (moved.length === 0) return changed\n graph.updateNode(setId, {\n componentPropertyDefinitions: [...moved, ...set.componentPropertyDefinitions]\n })\n return [setId, ...changed]\n}\n\n/** A new component property ID, in the `prop:` form the editor, plugin API, and design JSX share. */\nexport function createComponentPropertyId(): string {\n return `prop:${randomHex(8)}`\n}\n"],"mappings":";;;;;AAiBA,SAAgB,wBAAwB,OAAmB,UAAkC;CAC3F,IAAI,SAAS,SAAS,YAAY,OAAO,CAAC;CAC1C,MAAM,YAAY,sBAAsB,OAAO,QAAQ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,EAAE;CACzB;CACA,IAAI,MAAM,WAAW,GAAG,OAAO;CAC/B,MAAM,WAAW,OAAO,EACtB,8BAA8B,CAAC,GAAG,OAAO,GAAG,IAAI,4BAA4B,EAC9E,CAAC;CACD,OAAO,CAAC,OAAO,GAAG,OAAO;AAC3B;;AAGA,SAAgB,4BAAoC;CAClD,OAAO,QAAQ,UAAU,CAAC;AAC5B"}
@@ -1,27 +1,33 @@
1
1
  import { SceneGraph } from "../graph.js";
2
- import { SceneNode } from "../types.js";
2
+ import { ComponentPropertyDefinition, SceneNode } from "../types.js";
3
3
  //#region src/components/variants.d.ts
4
4
  /** Variants in reading order on the canvas: top to bottom, then left to right, then by name. */
5
5
  declare function compareCanvasPosition(a: SceneNode, b: SceneNode): number;
6
6
  /**
7
- * A component set's default variant, the one an instance starts as: the variant in its
8
- * top-left corner, as Figma defines it.
7
+ * A component set's default variant, the one an instance starts as: the first in reading order,
8
+ * and the first in layer order of those at the same place (measured in Figma desktop 126).
9
9
  */
10
10
  declare function defaultVariant(graph: SceneGraph, set: SceneNode): SceneNode | undefined;
11
11
  /**
12
- * The value a variant property starts at: its recorded default when that is one of its options,
13
- * or else the default variant's, since a document may leave the recorded default empty.
12
+ * The value a variant property starts at: the default variant's, as Figma reads it whatever
13
+ * default the document records, and follows when another variant moves to the top left. The
14
+ * recorded default stands only while the default variant has no value for the property.
14
15
  */
15
16
  declare function variantDefaultValue(graph: SceneGraph, set: SceneNode, property: {
16
17
  name: string;
17
18
  defaultValue: string;
18
- variantOptions?: string[];
19
19
  }): string;
20
20
  /**
21
- * The variant with every property at its default value: the recorded defaults when they name a
22
- * variant, else the default variant, as a document that leaves them out draws it.
21
+ * A variant property's values: the ones the document records, then any its variants use that it
22
+ * does not, since older documents record none and a set's page may have loaded only some
23
+ * variants.
23
24
  */
24
- declare function restingVariant(graph: SceneGraph, set: SceneNode): SceneNode | undefined;
25
+ declare function variantOptions(graph: SceneGraph, set: SceneNode, property: {
26
+ name: string;
27
+ variantOptions?: string[];
28
+ }): string[];
29
+ /** A set's definition with a variant property's values and default filled in from its variants. */
30
+ declare function resolvedVariantDefinition(graph: SceneGraph, set: SceneNode, definition: ComponentPropertyDefinition): ComponentPropertyDefinition;
25
31
  //#endregion
26
- export { compareCanvasPosition, defaultVariant, restingVariant, variantDefaultValue };
32
+ export { compareCanvasPosition, defaultVariant, resolvedVariantDefinition, variantDefaultValue, variantOptions };
27
33
  //# sourceMappingURL=variants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"variants.d.ts","names":[],"sources":["../../src/components/variants.ts"],"mappings":";;;;iBAIgB,sBAAsB,GAAG,WAAW,GAAG;;;;;iBAQvC,eAAe,OAAO,YAAY,KAAK,YAAY;;;;;iBAWnD,oBACd,OAAO,YACP,KAAK,WACL;EAAY;EAAc;EAAsB;;;;;;iBAUlC,eAAe,OAAO,YAAY,KAAK,YAAY"}
1
+ {"version":3,"file":"variants.d.ts","names":[],"sources":["../../src/components/variants.ts"],"mappings":";;;;iBAMgB,sBAAsB,GAAG,WAAW,GAAG;;;;;iBAiBvC,eAAe,OAAO,YAAY,KAAK,YAAY;;;;;;iBAcnD,oBACd,OAAO,YACP,KAAK,WACL;EAAY;EAAc;;;;;;;iBAWZ,eACd,OAAO,YACP,KAAK,WACL;EAAY;EAAc;;;iBAWZ,0BACd,OAAO,YACP,KAAK,WACL,YAAY,8BACX"}
@@ -1,32 +1,58 @@
1
+ import { compact, uniq } from "es-toolkit/array";
1
2
  //#region src/components/variants.ts
2
3
  /** Variants in reading order on the canvas: top to bottom, then left to right, then by name. */
3
4
  function compareCanvasPosition(a, b) {
4
5
  return a.y - b.y || a.x - b.x || a.name.localeCompare(b.name);
5
6
  }
6
7
  /**
7
- * A component set's default variant, the one an instance starts as: the variant in its
8
- * top-left corner, as Figma defines it.
8
+ * Whether `a` comes before `b` reading the set as Figma does: variants whose rows overlap, edges
9
+ * touching included, read left to right, and others top to bottom.
10
+ */
11
+ function readsBefore(a, b) {
12
+ return a.y <= b.y + b.height && b.y <= a.y + a.height ? a.x < b.x || a.x === b.x && a.y < b.y : a.y < b.y;
13
+ }
14
+ /**
15
+ * A component set's default variant, the one an instance starts as: the first in reading order,
16
+ * and the first in layer order of those at the same place (measured in Figma desktop 126).
9
17
  */
10
18
  function defaultVariant(graph, set) {
11
- return graph.getChildren(set.id).filter((child) => child.type === "COMPONENT").sort(compareCanvasPosition)[0];
19
+ let first;
20
+ for (const child of graph.getChildren(set.id)) {
21
+ if (child.type !== "COMPONENT") continue;
22
+ if (!first || readsBefore(child, first)) first = child;
23
+ }
24
+ return first;
12
25
  }
13
26
  /**
14
- * The value a variant property starts at: its recorded default when that is one of its options,
15
- * or else the default variant's, since a document may leave the recorded default empty.
27
+ * The value a variant property starts at: the default variant's, as Figma reads it whatever
28
+ * default the document records, and follows when another variant moves to the top left. The
29
+ * recorded default stands only while the default variant has no value for the property.
16
30
  */
17
31
  function variantDefaultValue(graph, set, property) {
18
- if (property.variantOptions?.includes(property.defaultValue)) return property.defaultValue;
19
- return defaultVariant(graph, set)?.componentPropertyValues[property.name] ?? property.defaultValue;
32
+ return defaultVariant(graph, set)?.componentPropertyValues[property.name] || property.defaultValue;
20
33
  }
21
34
  /**
22
- * The variant with every property at its default value: the recorded defaults when they name a
23
- * variant, else the default variant, as a document that leaves them out draws it.
35
+ * A variant property's values: the ones the document records, then any its variants use that it
36
+ * does not, since older documents record none and a set's page may have loaded only some
37
+ * variants.
24
38
  */
25
- function restingVariant(graph, set) {
26
- const values = set.componentPropertyDefinitions.filter((definition) => definition.type === "VARIANT").map((property) => [property.name, variantDefaultValue(graph, set, property)]);
27
- return graph.getChildren(set.id).find((child) => child.type === "COMPONENT" && values.every(([name, value]) => child.componentPropertyValues[name] === value)) ?? defaultVariant(graph, set);
39
+ function variantOptions(graph, set, property) {
40
+ const used = graph.getChildren(set.id).map((child) => child.type === "COMPONENT" ? child.componentPropertyValues[property.name] : "");
41
+ return compact(uniq([...property.variantOptions ?? [], ...used]));
42
+ }
43
+ /** A set's definition with a variant property's values and default filled in from its variants. */
44
+ function resolvedVariantDefinition(graph, set, definition) {
45
+ if (definition.type !== "VARIANT") return definition;
46
+ const options = {
47
+ ...definition,
48
+ variantOptions: variantOptions(graph, set, definition)
49
+ };
50
+ return {
51
+ ...options,
52
+ defaultValue: variantDefaultValue(graph, set, options)
53
+ };
28
54
  }
29
55
  //#endregion
30
- export { compareCanvasPosition, defaultVariant, restingVariant, variantDefaultValue };
56
+ export { compareCanvasPosition, defaultVariant, resolvedVariantDefinition, variantDefaultValue, variantOptions };
31
57
 
32
58
  //# sourceMappingURL=variants.js.map