@pairdraw/scene-graph 0.19.1 → 0.20.0

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 (225) hide show
  1. package/dist/behaviours/index.d.ts +2 -2
  2. package/dist/behaviours/index.js +2 -2
  3. package/dist/behaviours/spec.d.ts +6 -1
  4. package/dist/behaviours/spec.d.ts.map +1 -1
  5. package/dist/behaviours/spec.js +62 -5
  6. package/dist/behaviours/spec.js.map +1 -1
  7. package/dist/canvas-layout.js +66 -0
  8. package/dist/canvas-layout.js.map +1 -0
  9. package/dist/components/index.d.ts +2 -2
  10. package/dist/components/index.js +2 -2
  11. package/dist/components/variants.d.ts +12 -1
  12. package/dist/components/variants.d.ts.map +1 -1
  13. package/dist/components/variants.js +30 -1
  14. package/dist/components/variants.js.map +1 -1
  15. package/dist/constants.d.ts +25 -1
  16. package/dist/constants.d.ts.map +1 -1
  17. package/dist/constants.js +94 -1
  18. package/dist/constants.js.map +1 -1
  19. package/dist/coordinate.js +1 -1
  20. package/dist/copy.d.ts.map +1 -1
  21. package/dist/copy.js +17 -1
  22. package/dist/copy.js.map +1 -1
  23. package/dist/css/index.d.ts +2 -2
  24. package/dist/css/index.js +2 -2
  25. package/dist/css/text.d.ts +22 -1
  26. package/dist/css/text.d.ts.map +1 -1
  27. package/dist/css/text.js +81 -1
  28. package/dist/css/text.js.map +1 -1
  29. package/dist/design-memory/index.d.ts +3 -0
  30. package/dist/design-memory/index.js +3 -0
  31. package/dist/design-memory/model.d.ts +33 -0
  32. package/dist/design-memory/model.d.ts.map +1 -0
  33. package/dist/design-memory/model.js +142 -0
  34. package/dist/design-memory/model.js.map +1 -0
  35. package/dist/design-memory/schema.d.ts +76 -0
  36. package/dist/design-memory/schema.d.ts.map +1 -0
  37. package/dist/design-memory/schema.js +72 -0
  38. package/dist/design-memory/schema.js.map +1 -0
  39. package/dist/effects.d.ts +7 -4
  40. package/dist/effects.d.ts.map +1 -1
  41. package/dist/effects.js +9 -4
  42. package/dist/effects.js.map +1 -1
  43. package/dist/empty-values.js +93 -0
  44. package/dist/empty-values.js.map +1 -0
  45. package/dist/fields/components.d.ts +9 -0
  46. package/dist/fields/components.d.ts.map +1 -0
  47. package/dist/fields/components.js +24 -0
  48. package/dist/fields/components.js.map +1 -0
  49. package/dist/fields/index.d.ts +3 -2
  50. package/dist/fields/index.js +3 -2
  51. package/dist/fields/layout.d.ts +3 -1
  52. package/dist/fields/layout.d.ts.map +1 -1
  53. package/dist/fields/layout.js +10 -1
  54. package/dist/fields/layout.js.map +1 -1
  55. package/dist/generated/index.d.ts +2 -2
  56. package/dist/generated/index.js +2 -2
  57. package/dist/generated/shape-with-text.d.ts +7 -2
  58. package/dist/generated/shape-with-text.d.ts.map +1 -1
  59. package/dist/generated/shape-with-text.js +20 -3
  60. package/dist/generated/shape-with-text.js.map +1 -1
  61. package/dist/graph.d.ts +8 -1
  62. package/dist/graph.d.ts.map +1 -1
  63. package/dist/graph.js +36 -7
  64. package/dist/graph.js.map +1 -1
  65. package/dist/grid-values.d.ts +10 -0
  66. package/dist/grid-values.d.ts.map +1 -0
  67. package/dist/grid-values.js +53 -0
  68. package/dist/grid-values.js.map +1 -0
  69. package/dist/group-bounds.d.ts +4 -1
  70. package/dist/group-bounds.d.ts.map +1 -1
  71. package/dist/group-bounds.js +11 -1
  72. package/dist/group-bounds.js.map +1 -1
  73. package/dist/hit-test/drop.d.ts +20 -0
  74. package/dist/hit-test/drop.d.ts.map +1 -0
  75. package/dist/hit-test/drop.js +54 -0
  76. package/dist/hit-test/drop.js.map +1 -0
  77. package/dist/hit-test/geometry.js +86 -0
  78. package/dist/hit-test/geometry.js.map +1 -0
  79. package/dist/hit-test/masks.d.ts +14 -0
  80. package/dist/hit-test/masks.d.ts.map +1 -0
  81. package/dist/hit-test/masks.js +41 -0
  82. package/dist/hit-test/masks.js.map +1 -0
  83. package/dist/hit-test/paint.d.ts +6 -0
  84. package/dist/hit-test/paint.d.ts.map +1 -0
  85. package/dist/hit-test/paint.js +19 -0
  86. package/dist/hit-test/paint.js.map +1 -0
  87. package/dist/hit-test.d.ts +4 -15
  88. package/dist/hit-test.d.ts.map +1 -1
  89. package/dist/hit-test.js +33 -178
  90. package/dist/hit-test.js.map +1 -1
  91. package/dist/index.d.ts +2 -2
  92. package/dist/instances/fields.d.ts.map +1 -1
  93. package/dist/instances/fields.js +2 -0
  94. package/dist/instances/fields.js.map +1 -1
  95. package/dist/instances/index.d.ts +2 -1
  96. package/dist/instances/index.d.ts.map +1 -1
  97. package/dist/instances/index.js +1 -1
  98. package/dist/instances/index.js.map +1 -1
  99. package/dist/instances/source-walk.d.ts +12 -0
  100. package/dist/instances/source-walk.d.ts.map +1 -0
  101. package/dist/node-defaults.d.ts +21 -1
  102. package/dist/node-defaults.d.ts.map +1 -1
  103. package/dist/node-defaults.js +63 -50
  104. package/dist/node-defaults.js.map +1 -1
  105. package/dist/plugin-data/fields.d.ts +75 -0
  106. package/dist/plugin-data/fields.d.ts.map +1 -1
  107. package/dist/plugin-data/fields.js +18 -1
  108. package/dist/plugin-data/fields.js.map +1 -1
  109. package/dist/preview.d.ts.map +1 -1
  110. package/dist/preview.js +2 -0
  111. package/dist/preview.js.map +1 -1
  112. package/dist/resize.d.ts.map +1 -1
  113. package/dist/resize.js +3 -6
  114. package/dist/resize.js.map +1 -1
  115. package/dist/scaling/node.d.ts.map +1 -1
  116. package/dist/scaling/node.js +28 -4
  117. package/dist/scaling/node.js.map +1 -1
  118. package/dist/shared-styles.d.ts +3 -1
  119. package/dist/shared-styles.d.ts.map +1 -1
  120. package/dist/shared-styles.js +7 -1
  121. package/dist/shared-styles.js.map +1 -1
  122. package/dist/text/path.js +11 -0
  123. package/dist/text/path.js.map +1 -0
  124. package/dist/text/picture.d.ts.map +1 -1
  125. package/dist/text/picture.js +5 -5
  126. package/dist/text/picture.js.map +1 -1
  127. package/dist/token-sources/document.d.ts +26 -0
  128. package/dist/token-sources/document.d.ts.map +1 -0
  129. package/dist/token-sources/document.js +80 -0
  130. package/dist/token-sources/document.js.map +1 -0
  131. package/dist/token-sources/index.d.ts +3 -0
  132. package/dist/token-sources/index.js +3 -0
  133. package/dist/token-sources/schema.d.ts +43 -0
  134. package/dist/token-sources/schema.d.ts.map +1 -0
  135. package/dist/token-sources/schema.js +50 -0
  136. package/dist/token-sources/schema.js.map +1 -0
  137. package/dist/transfer/copied.d.ts +14 -0
  138. package/dist/transfer/copied.d.ts.map +1 -0
  139. package/dist/transfer/copied.js +52 -0
  140. package/dist/transfer/copied.js.map +1 -0
  141. package/dist/transfer/index.d.ts +2 -1
  142. package/dist/transfer/index.js +2 -1
  143. package/dist/transfer/nodes.js +7 -0
  144. package/dist/transfer/nodes.js.map +1 -1
  145. package/dist/types.d.ts +104 -15
  146. package/dist/types.d.ts.map +1 -1
  147. package/dist/types.js.map +1 -1
  148. package/dist/undo.d.ts +6 -0
  149. package/dist/undo.d.ts.map +1 -1
  150. package/dist/undo.js +13 -0
  151. package/dist/undo.js.map +1 -1
  152. package/dist/variables/index.d.ts.map +1 -1
  153. package/dist/variables/index.js.map +1 -1
  154. package/dist/variations/copy.d.ts +16 -0
  155. package/dist/variations/copy.d.ts.map +1 -0
  156. package/dist/variations/copy.js +78 -0
  157. package/dist/variations/copy.js.map +1 -0
  158. package/dist/variations/index.d.ts +6 -0
  159. package/dist/variations/index.js +6 -0
  160. package/dist/variations/match.d.ts +23 -0
  161. package/dist/variations/match.d.ts.map +1 -0
  162. package/dist/variations/match.js +80 -0
  163. package/dist/variations/match.js.map +1 -0
  164. package/dist/variations/model.d.ts +39 -0
  165. package/dist/variations/model.d.ts.map +1 -0
  166. package/dist/variations/model.js +90 -0
  167. package/dist/variations/model.js.map +1 -0
  168. package/dist/variations/pick.d.ts +43 -0
  169. package/dist/variations/pick.d.ts.map +1 -0
  170. package/dist/variations/pick.js +211 -0
  171. package/dist/variations/pick.js.map +1 -0
  172. package/dist/variations/schema.d.ts +27 -0
  173. package/dist/variations/schema.d.ts.map +1 -0
  174. package/dist/variations/schema.js +27 -0
  175. package/dist/variations/schema.js.map +1 -0
  176. package/package.json +21 -1
  177. package/src/behaviours/spec.ts +94 -4
  178. package/src/canvas-layout.ts +76 -0
  179. package/src/components/variants.ts +39 -0
  180. package/src/constants.ts +75 -0
  181. package/src/copy.ts +20 -1
  182. package/src/css/index.ts +12 -1
  183. package/src/css/text.ts +86 -0
  184. package/src/design-memory/index.ts +2 -0
  185. package/src/design-memory/model.ts +143 -0
  186. package/src/design-memory/schema.ts +84 -0
  187. package/src/effects.ts +16 -7
  188. package/src/empty-values.ts +101 -0
  189. package/src/fields/components.ts +21 -0
  190. package/src/fields/index.ts +1 -0
  191. package/src/fields/layout.ts +10 -0
  192. package/src/generated/shape-with-text.ts +28 -2
  193. package/src/graph.ts +48 -6
  194. package/src/grid-values.ts +47 -0
  195. package/src/group-bounds.ts +13 -0
  196. package/src/hit-test/drop.ts +90 -0
  197. package/src/hit-test/geometry.ts +131 -0
  198. package/src/hit-test/masks.ts +57 -0
  199. package/src/hit-test/paint.ts +24 -0
  200. package/src/hit-test.ts +78 -291
  201. package/src/instances/fields.ts +2 -0
  202. package/src/instances/index.ts +1 -0
  203. package/src/node-defaults.ts +71 -52
  204. package/src/plugin-data/fields.ts +18 -1
  205. package/src/preview.ts +2 -0
  206. package/src/resize.ts +3 -14
  207. package/src/scaling/node.ts +34 -5
  208. package/src/shared-styles.ts +8 -0
  209. package/src/text/path.ts +14 -0
  210. package/src/text/picture.ts +5 -2
  211. package/src/token-sources/document.ts +104 -0
  212. package/src/token-sources/index.ts +2 -0
  213. package/src/token-sources/schema.ts +59 -0
  214. package/src/transfer/copied.ts +72 -0
  215. package/src/transfer/index.ts +1 -0
  216. package/src/transfer/nodes.ts +10 -0
  217. package/src/types.ts +117 -14
  218. package/src/undo.ts +11 -0
  219. package/src/variables/index.ts +1 -4
  220. package/src/variations/copy.ts +89 -0
  221. package/src/variations/index.ts +5 -0
  222. package/src/variations/match.ts +100 -0
  223. package/src/variations/model.ts +132 -0
  224. package/src/variations/pick.ts +293 -0
  225. package/src/variations/schema.ts +29 -0
@@ -0,0 +1,293 @@
1
+ import { isEqual, pick } from 'es-toolkit'
2
+
3
+ import { cloneNodeProps } from '../copy'
4
+ import { MAIN_COMPONENT_FIELDS, PARENT_LAYOUT_FIELDS, TRANSFORM_FIELDS } from '../fields'
5
+ import type { SceneGraph } from '../graph'
6
+ import { findInstanceAncestor, retargetCopiedOverrides } from '../instances'
7
+ import { readPluginData } from '../plugin-data/field'
8
+ import { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'
9
+ import type { NodeType, PrototypeReaction, SceneNode } from '../types'
10
+ import { withStandIn } from './copy'
11
+ import { matchLayers } from './match'
12
+ import { variationSetOf, versionOf, type VariationSetView } from './model'
13
+
14
+ /** Why a version cannot replace its original. */
15
+ export type VariationPickProblem =
16
+ | 'not-in-set'
17
+ | 'not-a-version'
18
+ | 'original-missing'
19
+ | 'original-in-instance'
20
+ | 'original-locked'
21
+ | 'version-type-differs'
22
+
23
+ const PROBLEM_MESSAGES: Record<VariationPickProblem, (nodeId: string) => string> = {
24
+ 'not-in-set': (nodeId) => `"${nodeId}" is not in a set of variations`,
25
+ 'not-a-version': (nodeId) => `"${nodeId}" is not a version: pick one of the set's layers`,
26
+ 'original-missing': () => 'The layer these are variations of is gone; discard them instead',
27
+ 'original-in-instance': () =>
28
+ 'The layer these are variations of is inside an instance; vary the instance or its main component',
29
+ 'original-locked': () =>
30
+ 'The layer these are variations of is locked; unlock it to replace it with a version',
31
+ 'version-type-differs': () =>
32
+ 'This version is a different kind of layer than the original (such as a frame for an ' +
33
+ 'instance), so it cannot replace it; change the layers inside a version instead of ' +
34
+ 'replacing the version, or make a new version'
35
+ }
36
+
37
+ /** A layer that belongs to no set, or a version that cannot be used, with the reason. */
38
+ export class VariationError extends Error {
39
+ constructor(
40
+ readonly problem: VariationPickProblem,
41
+ nodeId: string
42
+ ) {
43
+ super(PROBLEM_MESSAGES[problem](nodeId))
44
+ this.name = 'VariationError'
45
+ }
46
+ }
47
+
48
+ /** Never taken from a version: what places a layer in the tree, who it is, and its review. */
49
+ const OWN_FIELDS: ReadonlySet<string> = new Set<keyof SceneNode>([
50
+ 'id',
51
+ 'parentId',
52
+ 'childIds',
53
+ 'source',
54
+ 'status'
55
+ ])
56
+
57
+ /** What the original keeps of its own place: its name, where it sits, and how it is shown. */
58
+ const ORIGINAL_PLACE_FIELDS: ReadonlySet<string> = new Set<keyof SceneNode>([
59
+ 'name',
60
+ ...TRANSFORM_FIELDS.filter((field) => field !== 'flipX' && field !== 'flipY'),
61
+ ...PARENT_LAYOUT_FIELDS,
62
+ 'visible',
63
+ 'locked'
64
+ ])
65
+
66
+ /** What a main component original keeps: being a component, and its properties and publishing. */
67
+ const MAIN_COMPONENT_KEPT: ReadonlySet<string> = new Set<keyof SceneNode>([
68
+ 'type',
69
+ ...MAIN_COMPONENT_FIELDS
70
+ ])
71
+
72
+ function standInMark(node: SceneNode | undefined): string | undefined {
73
+ return node ? readPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn) : undefined
74
+ }
75
+
76
+ /** Removes stand-in marks from a layer moving into the original, and from its layers. */
77
+ function clearStandInMarks(graph: SceneGraph, rootId: string): void {
78
+ for (const node of subtree(graph, rootId)) {
79
+ if (standInMark(node) !== undefined) {
80
+ graph.updateNode(node.id, { pluginData: withStandIn(node, null) })
81
+ }
82
+ }
83
+ }
84
+
85
+ /** The layer at `rootId` and every layer inside it. */
86
+ function* subtree(graph: SceneGraph, rootId: string): Generator<SceneNode> {
87
+ const pending = [rootId]
88
+ for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
89
+ const node = graph.getNode(id)
90
+ if (!node) continue
91
+ pending.push(...node.childIds)
92
+ yield node
93
+ }
94
+ }
95
+
96
+ function isMain(node: SceneNode | undefined): boolean {
97
+ return node?.type === 'COMPONENT' || node?.type === 'COMPONENT_SET'
98
+ }
99
+
100
+ /** Whether a version whose root is `version` may replace an original of type `original`. */
101
+ function replaces(version: NodeType, original: NodeType): boolean {
102
+ // A main component's versions are frames.
103
+ return version === original || (original === 'COMPONENT' && version === 'FRAME')
104
+ }
105
+
106
+ /** Why `versionId` cannot replace the original of `view`, or null when it can. */
107
+ export function variationPickProblem(
108
+ graph: SceneGraph,
109
+ view: VariationSetView,
110
+ versionId: string
111
+ ): VariationPickProblem | null {
112
+ const version = graph.getNode(versionId)
113
+ if (version?.parentId !== view.section.id) return 'not-a-version'
114
+ const { original } = view
115
+ if (!original) return 'original-missing'
116
+ if (original.parentId && findInstanceAncestor(graph, original.parentId)) {
117
+ return 'original-in-instance'
118
+ }
119
+ if (original.locked) return 'original-locked'
120
+ return replaces(version.type, original.type) ? null : 'version-type-differs'
121
+ }
122
+
123
+ /**
124
+ * The set and version `versionId` names, a version or a layer inside one, and why it cannot
125
+ * replace its original, or null when it can. Throws when `versionId` is in no set's versions.
126
+ */
127
+ export function variationToPick(
128
+ graph: SceneGraph,
129
+ versionId: string
130
+ ): { view: VariationSetView; version: SceneNode; problem: VariationPickProblem | null } {
131
+ const view = variationSetOf(graph, versionId)
132
+ if (!view) throw new VariationError('not-in-set', versionId)
133
+ const version = versionOf(graph, view, versionId)
134
+ if (!version) throw new VariationError('not-a-version', versionId)
135
+ return { view, version, problem: variationPickProblem(graph, view, version.id) }
136
+ }
137
+
138
+ /**
139
+ * The version's fields that differ from what `original` holds, as `original` takes them, or null
140
+ * when none do, so an unchanged layer gets no update at all.
141
+ */
142
+ function changedFields(
143
+ versionLayer: SceneNode,
144
+ original: SceneNode,
145
+ isRoot: boolean
146
+ ): Partial<SceneNode> | null {
147
+ // A stand-in mark stays with the version's layer; the original never takes it.
148
+ const version =
149
+ standInMark(versionLayer) === undefined
150
+ ? versionLayer
151
+ : { ...versionLayer, pluginData: withStandIn(versionLayer, null) }
152
+ const kept = (key: string) =>
153
+ OWN_FIELDS.has(key) ||
154
+ (isRoot && ORIGINAL_PLACE_FIELDS.has(key)) ||
155
+ (isRoot && original.type === 'COMPONENT' && MAIN_COMPONENT_KEPT.has(key))
156
+ const keys = (Object.keys(version) as (keyof SceneNode)[]).filter(
157
+ (key) => version[key] !== original[key] && !kept(key) && !isEqual(version[key], original[key])
158
+ )
159
+ return keys.length > 0 ? pick(cloneNodeProps(version, null), keys) : null
160
+ }
161
+
162
+ function renamedReactions(
163
+ reactions: PrototypeReaction[],
164
+ rename: (id: string) => string
165
+ ): PrototypeReaction[] | null {
166
+ const renamed = (action: PrototypeReaction['actions'][number]) =>
167
+ action.type === 'NODE' && action.nodeId !== null && rename(action.nodeId) !== action.nodeId
168
+ if (!reactions.some((reaction) => reaction.actions.some(renamed))) return null
169
+ return reactions.map((reaction) => ({
170
+ ...reaction,
171
+ actions: reaction.actions.map((action) =>
172
+ action.type === 'NODE' && action.nodeId !== null
173
+ ? { ...action, nodeId: rename(action.nodeId) }
174
+ : action
175
+ )
176
+ }))
177
+ }
178
+
179
+ /**
180
+ * Points what the kept layers say about each other, connector ends and prototype targets, at
181
+ * the original's layers where they named the version's.
182
+ */
183
+ function renameReferences(graph: SceneGraph, rootId: string, pairs: ReadonlyMap<string, string>) {
184
+ const rename = (id: string) => pairs.get(id) ?? id
185
+ for (const node of subtree(graph, rootId)) {
186
+ const changes: Partial<SceneNode> = {}
187
+ for (const end of ['connectorStart', 'connectorEnd'] as const) {
188
+ const endpoint = node[end]
189
+ if (endpoint?.nodeId && pairs.has(endpoint.nodeId)) {
190
+ changes[end] = { ...endpoint, nodeId: rename(endpoint.nodeId) }
191
+ }
192
+ }
193
+ const reactions = renamedReactions(node.prototypeReactions, rename)
194
+ if (reactions) changes.prototypeReactions = reactions
195
+ if (Object.keys(changes).length > 0) graph.updateNode(node.id, changes)
196
+ }
197
+ }
198
+
199
+ /**
200
+ * Gives `originalLayer` the children of `versionLayer`, in its order: paired layers stay, layers
201
+ * only the version has move in, and layers only the original had go, apart from main components
202
+ * and sets, which keep their place. Returns the pairs to visit next.
203
+ */
204
+ function applyChildren(
205
+ graph: SceneGraph,
206
+ pairs: ReadonlyMap<string, string>,
207
+ paired: ReadonlySet<string>,
208
+ versionLayer: SceneNode,
209
+ originalLayer: SceneNode
210
+ ): Array<[string, string]> {
211
+ const originalChildren = [...originalLayer.childIds]
212
+ // A main component or set is never deleted: instances elsewhere draw from it.
213
+ const keptMains = originalChildren.flatMap((childId, index) =>
214
+ !paired.has(childId) && isMain(graph.getNode(childId)) ? [{ childId, index }] : []
215
+ )
216
+ const unpaired = originalChildren.filter(
217
+ (childId) => !paired.has(childId) && !isMain(graph.getNode(childId))
218
+ )
219
+ for (const childId of unpaired) graph.deleteNode(childId)
220
+
221
+ const next: Array<[string, string]> = []
222
+ const order: string[] = []
223
+ for (const childId of versionLayer.childIds) {
224
+ const pairedId = pairs.get(childId)
225
+ // A stand-in for a component the document no longer has stands for nothing; any other
226
+ // marked layer moving in is an ordinary layer, such as a copy of a stand-in.
227
+ const mark = pairedId === undefined ? standInMark(graph.getNode(childId)) : undefined
228
+ if (mark !== undefined && !graph.getNode(mark)) continue
229
+ order.push(pairedId ?? childId)
230
+ if (pairedId === undefined) clearStandInMarks(graph, childId)
231
+ // A main component's stand-in leaves the component as it is (`standsIn`).
232
+ else if (graph.getNode(childId)?.type === graph.getNode(pairedId)?.type) {
233
+ next.push([childId, pairedId])
234
+ }
235
+ }
236
+ // A kept main component keeps its place among the layers.
237
+ for (const { childId, index } of keptMains) order.splice(index, 0, childId)
238
+ order.forEach((target, index) => {
239
+ // Read the list afresh: a move replaces it. A layer already in place is not moved.
240
+ if (graph.getNode(originalLayer.id)?.childIds[index] !== target) {
241
+ graph.insertChildAt(target, originalLayer.id, index)
242
+ }
243
+ })
244
+ return next
245
+ }
246
+
247
+ /**
248
+ * Makes the original of the set holding `versionId` look like that version, then removes the
249
+ * set. The original keeps its id, name, place among its siblings, and how its parent lays it
250
+ * out, and so does every layer of it that pairs with one of the version's (`matchLayers`), so
251
+ * what points at them keeps working; layers only the version has move in, and layers only the
252
+ * original had are deleted, apart from main components and sets, which are kept as they are,
253
+ * as when the version holds their stand-ins. Only fields that differ are written and only layers out of place
254
+ * are moved, so a pick sends no more changes than it makes. A main component stays one, keeping
255
+ * its properties and publishing. Comments stay pinned where they were; one on a removed version
256
+ * falls back to its stored point, as on any deleted layer. The caller records the edit as one step and lays out the original and its parent.
257
+ */
258
+ export function applyVariation(graph: SceneGraph, versionId: string): { originalId: string } {
259
+ const { view, version, problem } = variationToPick(graph, versionId)
260
+ if (problem || !view.original) throw new VariationError(problem ?? 'original-missing', versionId)
261
+ const original = view.original
262
+
263
+ const pairs = matchLayers(graph, original.id, version.id)
264
+ const paired = new Set(pairs.values())
265
+ const pending: Array<[string, string]> = [[version.id, original.id]]
266
+ // Pairs are appended while the list is walked, parents before their children.
267
+ for (const [versionLayerId, originalLayerId] of pending) {
268
+ const versionLayer = graph.getNode(versionLayerId)
269
+ const originalLayer = graph.getNode(originalLayerId)
270
+ if (!versionLayer || !originalLayer) continue
271
+ const changes = changedFields(versionLayer, originalLayer, originalLayerId === original.id)
272
+ if (changes) graph.updateNode(originalLayerId, changes)
273
+ pending.push(...applyChildren(graph, pairs, paired, versionLayer, originalLayer))
274
+ }
275
+
276
+ // Instances among the kept layers record overrides of the version's layers; they are the
277
+ // original's now.
278
+ retargetCopiedOverrides(graph, pairs)
279
+ renameReferences(graph, original.id, pairs)
280
+ graph.deleteNode(view.section.id)
281
+ return { originalId: original.id }
282
+ }
283
+
284
+ /** Removes the set holding `nodeId` and every version in it; the original is left as it is. */
285
+ export function discardVariations(
286
+ graph: SceneGraph,
287
+ nodeId: string
288
+ ): { originalId: string | null } {
289
+ const view = variationSetOf(graph, nodeId)
290
+ if (!view) throw new VariationError('not-in-set', nodeId)
291
+ graph.deleteNode(view.section.id)
292
+ return { originalId: view.original?.id ?? null }
293
+ }
@@ -0,0 +1,29 @@
1
+ import * as v from 'valibot'
2
+
3
+ /** The most versions one request makes. */
4
+ export const VARIATION_MAX_COUNT = 8
5
+
6
+ /** The longest label a version's name carries. */
7
+ export const VARIATION_LABEL_MAX_LENGTH = 40
8
+
9
+ /** The most layers one request copies, counting every version's layers. */
10
+ export const VARIATION_MAX_LAYERS = 10_000
11
+
12
+ /**
13
+ * What marks a section as a set of variations: the layer they are versions of, and a fingerprint
14
+ * of that layer when they were made, so a front end can say it changed since. The versions are
15
+ * the section's own children, so none of them needs a mark of its own.
16
+ */
17
+ export const variationSetSchema = v.object({
18
+ version: v.literal(1),
19
+ originalId: v.pipe(v.string(), v.nonEmpty()),
20
+ originalFingerprint: v.optional(v.string())
21
+ })
22
+
23
+ export type VariationSet = v.InferOutput<typeof variationSetSchema>
24
+
25
+ /**
26
+ * On a layer of a version, the id of the main component or component set of the original it
27
+ * stands in for: an instance of the component, or a frame of instances of the set's variants.
28
+ */
29
+ export const variationStandInSchema = v.pipe(v.string(), v.nonEmpty())