@pairdraw/scene-graph 0.18.0 → 0.19.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 (181) hide show
  1. package/dist/components/index.d.ts +2 -2
  2. package/dist/components/index.js +2 -2
  3. package/dist/components/properties.d.ts +6 -1
  4. package/dist/components/properties.d.ts.map +1 -1
  5. package/dist/components/properties.js +14 -3
  6. package/dist/components/properties.js.map +1 -1
  7. package/dist/copy.d.ts.map +1 -1
  8. package/dist/copy.js +1 -0
  9. package/dist/copy.js.map +1 -1
  10. package/dist/css/gradient.d.ts +23 -0
  11. package/dist/css/gradient.d.ts.map +1 -0
  12. package/dist/css/gradient.js +404 -0
  13. package/dist/css/gradient.js.map +1 -0
  14. package/dist/css/index.d.ts +2 -1
  15. package/dist/css/index.js +2 -1
  16. package/dist/css/shadows.d.ts.map +1 -1
  17. package/dist/css/shadows.js +2 -13
  18. package/dist/css/shadows.js.map +1 -1
  19. package/dist/css/values.js +24 -1
  20. package/dist/css/values.js.map +1 -1
  21. package/dist/font-style.d.ts +2 -1
  22. package/dist/font-style.d.ts.map +1 -1
  23. package/dist/font-style.js +31 -6
  24. package/dist/font-style.js.map +1 -1
  25. package/dist/geometry.d.ts +10 -1
  26. package/dist/geometry.d.ts.map +1 -1
  27. package/dist/geometry.js +21 -6
  28. package/dist/geometry.js.map +1 -1
  29. package/dist/graph.d.ts +49 -3
  30. package/dist/graph.d.ts.map +1 -1
  31. package/dist/graph.js +179 -17
  32. package/dist/graph.js.map +1 -1
  33. package/dist/hit-test.d.ts +3 -1
  34. package/dist/hit-test.d.ts.map +1 -1
  35. package/dist/hit-test.js +38 -23
  36. package/dist/hit-test.js.map +1 -1
  37. package/dist/icons/index.d.ts +2 -2
  38. package/dist/icons/index.js +2 -2
  39. package/dist/icons/model.d.ts +8 -2
  40. package/dist/icons/model.d.ts.map +1 -1
  41. package/dist/icons/model.js +10 -1
  42. package/dist/icons/model.js.map +1 -1
  43. package/dist/index.d.ts +2 -2
  44. package/dist/instances/assignments.js +40 -6
  45. package/dist/instances/assignments.js.map +1 -1
  46. package/dist/instances/fields.d.ts +2 -2
  47. package/dist/instances/fields.d.ts.map +1 -1
  48. package/dist/instances/fields.js +18 -1
  49. package/dist/instances/fields.js.map +1 -1
  50. package/dist/instances/index.d.ts +38 -2
  51. package/dist/instances/index.d.ts.map +1 -1
  52. package/dist/instances/index.js +134 -15
  53. package/dist/instances/index.js.map +1 -1
  54. package/dist/instances/overrides.d.ts +10 -1
  55. package/dist/instances/overrides.d.ts.map +1 -1
  56. package/dist/instances/overrides.js +28 -2
  57. package/dist/instances/overrides.js.map +1 -1
  58. package/dist/instances/placement.js +1 -1
  59. package/dist/instances/placement.js.map +1 -1
  60. package/dist/instances/swap-overrides.js +94 -0
  61. package/dist/instances/swap-overrides.js.map +1 -0
  62. package/dist/instances/sync.js +90 -12
  63. package/dist/instances/sync.js.map +1 -1
  64. package/dist/layout/draw-order.d.ts +11 -0
  65. package/dist/layout/draw-order.d.ts.map +1 -0
  66. package/dist/layout/draw-order.js +14 -0
  67. package/dist/layout/draw-order.js.map +1 -0
  68. package/dist/layout/index.d.ts +2 -1
  69. package/dist/layout/index.js +2 -1
  70. package/dist/node-defaults.d.ts +11 -1
  71. package/dist/node-defaults.d.ts.map +1 -1
  72. package/dist/node-defaults.js +16 -1
  73. package/dist/node-defaults.js.map +1 -1
  74. package/dist/order-keys.d.ts +3 -1
  75. package/dist/order-keys.d.ts.map +1 -1
  76. package/dist/order-keys.js +6 -1
  77. package/dist/order-keys.js.map +1 -1
  78. package/dist/resize.d.ts +29 -9
  79. package/dist/resize.d.ts.map +1 -1
  80. package/dist/resize.js +95 -62
  81. package/dist/resize.js.map +1 -1
  82. package/dist/shared-styles.d.ts.map +1 -1
  83. package/dist/shared-styles.js +1 -8
  84. package/dist/shared-styles.js.map +1 -1
  85. package/dist/slots/authoring.d.ts +3 -2
  86. package/dist/slots/authoring.d.ts.map +1 -1
  87. package/dist/slots/authoring.js +5 -3
  88. package/dist/slots/authoring.js.map +1 -1
  89. package/dist/slots/content.d.ts.map +1 -1
  90. package/dist/slots/content.js +4 -1
  91. package/dist/slots/content.js.map +1 -1
  92. package/dist/source-metadata.d.ts +4 -2
  93. package/dist/source-metadata.d.ts.map +1 -1
  94. package/dist/source-metadata.js +10 -3
  95. package/dist/source-metadata.js.map +1 -1
  96. package/dist/strokes.d.ts +39 -0
  97. package/dist/strokes.d.ts.map +1 -0
  98. package/dist/strokes.js +104 -0
  99. package/dist/strokes.js.map +1 -0
  100. package/dist/text/direction.d.ts +6 -2
  101. package/dist/text/direction.d.ts.map +1 -1
  102. package/dist/text/direction.js +2 -1
  103. package/dist/text/direction.js.map +1 -1
  104. package/dist/text/index.d.ts +3 -3
  105. package/dist/text/index.js +3 -3
  106. package/dist/text/lengths.d.ts +6 -1
  107. package/dist/text/lengths.d.ts.map +1 -1
  108. package/dist/text/lengths.js +10 -1
  109. package/dist/text/lengths.js.map +1 -1
  110. package/dist/text/overflow.d.ts +22 -6
  111. package/dist/text/overflow.d.ts.map +1 -1
  112. package/dist/text/overflow.js +24 -6
  113. package/dist/text/overflow.js.map +1 -1
  114. package/dist/transfer/nodes.js +9 -5
  115. package/dist/transfer/nodes.js.map +1 -1
  116. package/dist/types.d.ts +18 -2
  117. package/dist/types.d.ts.map +1 -1
  118. package/dist/types.js.map +1 -1
  119. package/dist/undo.d.ts +36 -1
  120. package/dist/undo.d.ts.map +1 -1
  121. package/dist/undo.js +52 -2
  122. package/dist/undo.js.map +1 -1
  123. package/dist/variables/bindings.d.ts +14 -6
  124. package/dist/variables/bindings.d.ts.map +1 -1
  125. package/dist/variables/bindings.js +42 -67
  126. package/dist/variables/bindings.js.map +1 -1
  127. package/dist/variables/formula/signatures.js +1 -1
  128. package/dist/variables/index.d.ts +10 -4
  129. package/dist/variables/index.d.ts.map +1 -1
  130. package/dist/variables/index.js +58 -15
  131. package/dist/variables/index.js.map +1 -1
  132. package/dist/variables/paints.d.ts +101 -0
  133. package/dist/variables/paints.d.ts.map +1 -0
  134. package/dist/variables/paints.js +313 -0
  135. package/dist/variables/paints.js.map +1 -0
  136. package/dist/variables/resolution.d.ts +14 -1
  137. package/dist/variables/resolution.d.ts.map +1 -1
  138. package/dist/variables/resolution.js +44 -4
  139. package/dist/variables/resolution.js.map +1 -1
  140. package/dist/variables/subtree.js +17 -0
  141. package/dist/variables/subtree.js.map +1 -0
  142. package/package.json +6 -1
  143. package/src/components/properties.ts +17 -2
  144. package/src/copy.ts +1 -0
  145. package/src/css/gradient.ts +438 -0
  146. package/src/css/index.ts +1 -0
  147. package/src/css/shadows.ts +2 -18
  148. package/src/css/values.ts +27 -1
  149. package/src/font-style.ts +38 -4
  150. package/src/geometry.ts +25 -6
  151. package/src/graph.ts +207 -17
  152. package/src/hit-test.ts +67 -24
  153. package/src/icons/model.ts +11 -1
  154. package/src/instances/assignments.ts +65 -7
  155. package/src/instances/fields.ts +21 -0
  156. package/src/instances/index.ts +237 -14
  157. package/src/instances/overrides.ts +36 -1
  158. package/src/instances/placement.ts +5 -1
  159. package/src/instances/swap-overrides.ts +123 -0
  160. package/src/instances/sync.ts +139 -12
  161. package/src/layout/draw-order.ts +13 -0
  162. package/src/layout/index.ts +1 -0
  163. package/src/node-defaults.ts +21 -0
  164. package/src/order-keys.ts +6 -0
  165. package/src/resize.ts +133 -69
  166. package/src/shared-styles.ts +1 -3
  167. package/src/slots/authoring.ts +5 -3
  168. package/src/slots/content.ts +4 -1
  169. package/src/source-metadata.ts +18 -3
  170. package/src/strokes.ts +121 -0
  171. package/src/text/direction.ts +7 -5
  172. package/src/text/lengths.ts +10 -0
  173. package/src/text/overflow.ts +30 -7
  174. package/src/transfer/nodes.ts +7 -5
  175. package/src/types.ts +17 -0
  176. package/src/undo.ts +79 -2
  177. package/src/variables/bindings.ts +52 -74
  178. package/src/variables/index.ts +80 -14
  179. package/src/variables/paints.ts +413 -0
  180. package/src/variables/resolution.ts +56 -5
  181. package/src/variables/subtree.ts +15 -0
@@ -0,0 +1,123 @@
1
+ import type { SceneGraph } from '../graph'
2
+ import type { SceneNode } from '../types'
3
+ import { INSTANCE_OVERRIDE_FIELDS } from './fields'
4
+ import {
5
+ isLayerReferenceOverride,
6
+ overriddenBindingField,
7
+ overriddenVariableModeCollection,
8
+ setInstanceOverride
9
+ } from './overrides'
10
+ import { copyProp } from './sync'
11
+
12
+ /** What a swap keeps for one layer, so the layer that replaces it can take the values over. */
13
+ interface CapturedLayer {
14
+ snapshot: SceneNode
15
+ fields: string[]
16
+ }
17
+
18
+ /** Overrides kept across a swap, keyed by each layer's name path inside the instance. */
19
+ export type CapturedInstanceOverrides = Map<string, CapturedLayer>
20
+
21
+ const OVERRIDE_FIELD_KEYS = new Set<string>(INSTANCE_OVERRIDE_FIELDS)
22
+
23
+ function isInstanceOverrideField(field: string): field is keyof SceneNode {
24
+ return OVERRIDE_FIELD_KEYS.has(field)
25
+ }
26
+
27
+ /**
28
+ * A layer's place inside its instance: the names from the instance down, with the position among
29
+ * siblings of the same name appended, so two layers called the same stay apart.
30
+ */
31
+ function childPath(parentPath: string, name: string, index: number): string {
32
+ return `${parentPath}/${name}#${index}`
33
+ }
34
+
35
+ function forEachChildPath(
36
+ graph: SceneGraph,
37
+ parentId: string,
38
+ parentPath: string,
39
+ visit: (child: SceneNode, path: string) => void
40
+ ): void {
41
+ const seen = new Map<string, number>()
42
+ for (const child of graph.getChildren(parentId)) {
43
+ const index = seen.get(child.name) ?? 0
44
+ seen.set(child.name, index + 1)
45
+ const path = childPath(parentPath, child.name, index)
46
+ visit(child, path)
47
+ forEachChildPath(graph, child.id, path, visit)
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Every override an instance's layers record, keyed by path, so a swap of the component can give
53
+ * them back to the layers of the variant that replace them, as Figma keeps overrides where the
54
+ * two variants share a layer.
55
+ */
56
+ export function captureInstanceOverrides(
57
+ graph: SceneGraph,
58
+ instance: SceneNode
59
+ ): CapturedInstanceOverrides {
60
+ const captured: CapturedInstanceOverrides = new Map()
61
+ forEachChildPath(graph, instance.id, '', (child, path) => {
62
+ const fields = instance.instanceOverrides.descendants.get(child.id)
63
+ if (!fields?.size) return
64
+ const kept = [...fields.keys()].filter(
65
+ (field) => isInstanceOverrideField(field) || !isLayerReferenceOverride(field)
66
+ )
67
+ if (kept.length) captured.set(path, { snapshot: structuredClone(child), fields: kept })
68
+ })
69
+ return captured
70
+ }
71
+
72
+ function changesFor(layer: CapturedLayer): Partial<SceneNode> {
73
+ const changes: Partial<SceneNode> = {}
74
+ const bindings: Record<string, string> = {}
75
+ const modes: Record<string, string> = {}
76
+ let bindingsTouched = false
77
+ let modesTouched = false
78
+ for (const field of layer.fields) {
79
+ const binding = overriddenBindingField(field)
80
+ if (binding) {
81
+ bindingsTouched = true
82
+ if (binding in layer.snapshot.boundVariables)
83
+ bindings[binding] = layer.snapshot.boundVariables[binding]
84
+ continue
85
+ }
86
+ const collection = overriddenVariableModeCollection(field)
87
+ if (collection) {
88
+ modesTouched = true
89
+ if (collection in layer.snapshot.variableModes)
90
+ modes[collection] = layer.snapshot.variableModes[collection]
91
+ continue
92
+ }
93
+ if (isInstanceOverrideField(field)) copyProp(changes, layer.snapshot, field)
94
+ }
95
+ if (bindingsTouched) changes.boundVariables = bindings
96
+ if (modesTouched) changes.variableModes = modes
97
+ return changes
98
+ }
99
+
100
+ /**
101
+ * Gives the captured overrides back to the layers of the instance's new component, matching each
102
+ * layer by path, and records them again for the layers that take the old ones' places.
103
+ */
104
+ export function restoreInstanceOverrides(
105
+ graph: SceneGraph,
106
+ instance: SceneNode,
107
+ captured: CapturedInstanceOverrides
108
+ ): void {
109
+ if (!captured.size) return
110
+ const paths: Array<{ child: SceneNode; layer: CapturedLayer }> = []
111
+ forEachChildPath(graph, instance.id, '', (child, path) => {
112
+ const layer = captured.get(path)
113
+ if (layer) paths.push({ child, layer })
114
+ })
115
+ if (!paths.length) return
116
+ for (const { child, layer } of paths) {
117
+ const changes = changesFor(layer)
118
+ if (Object.keys(changes).length) graph.updateNode(child.id, changes)
119
+ for (const field of layer.fields)
120
+ setInstanceOverride(instance.instanceOverrides, instance.id, child.id, field)
121
+ }
122
+ graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })
123
+ }
@@ -1,4 +1,6 @@
1
+ import { uniq } from 'es-toolkit/array'
1
2
  // Child cloning and property synchronization shared by instance creation, swap, and sync.
3
+ import { pick } from 'es-toolkit/object'
2
4
  import { isEqual } from 'es-toolkit/predicate'
3
5
 
4
6
  import { cloneNodeProps, copyEffects, copyFills, copyStrokes, copyStyleRuns } from '../copy'
@@ -6,7 +8,21 @@ import type { NodeCloneMode } from '../copy'
6
8
  import type { SceneGraph } from '../graph'
7
9
  import { scaleNodeChanges } from '../scaling/node'
8
10
  import { ownsSlotContent, slotPropertyId } from '../slots/frames'
11
+ import {
12
+ STROKE_LINE_FIELDS,
13
+ STROKE_LINE_KEYS,
14
+ strokeLine,
15
+ type StrokeLines,
16
+ withStrokeLines
17
+ } from '../strokes'
9
18
  import type { SceneNode } from '../types'
19
+ import { resolvedNumericBindings } from '../variables/bindings'
20
+ import { isNumericVariableBindingField, paintBindingTarget } from '../variables/fields'
21
+ import {
22
+ applyBoundPaintColorsIn,
23
+ copyAppliedBoundPaints,
24
+ keepCopyBoundPaints
25
+ } from '../variables/paints'
10
26
  import { scaleVariableBindingUnits } from '../variables/units'
11
27
  import { applyEnclosingAssignments, assignedFieldValues, propertyDrivenFields } from './assignments'
12
28
  import { keepOwnContentSize, remeasureChangedText, type SyncedPlacement } from './content-size'
@@ -78,7 +94,14 @@ export function bindingProtection(scopes: ReadonlySet<string>[]): (field: string
78
94
  if (field !== 'boundVariables' || !perField) fields.add(field)
79
95
  }
80
96
  }
81
- return (field) => fields.has(field)
97
+ return (field) => {
98
+ if (fields.has(field)) return true
99
+ // Paints an instance set itself own their bindings too, bound or not.
100
+ const binding = overriddenBindingField(field)
101
+ if (binding === null) return false
102
+ const paint = paintBindingTarget(binding)
103
+ return paint !== null && fields.has(paint.list)
104
+ }
82
105
  }
83
106
 
84
107
  export function syncBindingFields(
@@ -193,8 +216,17 @@ function copySyncedFields(
193
216
  export function updateSyncedProps(
194
217
  graph: SceneGraph,
195
218
  target: SceneNode,
196
- updates: Partial<SceneNode>
219
+ updates: Partial<SceneNode>,
220
+ /** A field the copy keeps as its own, which the sync leaves as it is. */
221
+ kept: (field: string) => boolean = () => false
197
222
  ): void {
223
+ resolveBoundNumbersInPlace(graph, target, updates, kept)
224
+ if (updates.fills || updates.strokes || updates.boundVariables) {
225
+ applySyncedModes(graph, target, updates)
226
+ keepCopyBoundPaints(graph, target, updates)
227
+ }
228
+ // After the copy's own bound paints, which can bring its strokes into the write.
229
+ applySyncedStrokeLines(graph, target, updates, kept)
198
230
  const changed = Object.fromEntries(
199
231
  Object.entries(updates).filter(
200
232
  ([key, value]) => !isEqual(target[key as keyof SceneNode], value)
@@ -204,6 +236,85 @@ export function updateSyncedProps(
204
236
  if (Object.keys(changed).length) graph.updateNode(target.id, changed)
205
237
  }
206
238
 
239
+ /**
240
+ * A bound number the sync writes takes the value it resolves to where the copy sits, not the
241
+ * one its component holds: an instance that sets another mode, or sits in one that does, shows
242
+ * that mode's value, as Figma draws it. A field the sync leaves alone, such as one an instance
243
+ * overrides, keeps its own value.
244
+ */
245
+ function resolveBoundNumbersInPlace(
246
+ graph: SceneGraph,
247
+ target: SceneNode,
248
+ updates: Partial<SceneNode>,
249
+ kept: (field: string) => boolean
250
+ ): void {
251
+ const bound = updates.boundVariables ?? target.boundVariables
252
+ // The fields the sync writes, and the ones it binds anew, which keep an equal value only in
253
+ // the component's mode.
254
+ const rebound = updates.boundVariables
255
+ ? Object.keys(bound).filter((field) => bound[field] !== target.boundVariables[field])
256
+ : []
257
+ const synced = uniq([...Object.keys(updates), ...rebound]).filter(
258
+ (field) => isNumericVariableBindingField(field) && bound[field] && !kept(field)
259
+ )
260
+ if (synced.length === 0) return
261
+ applySyncedModes(graph, target, updates)
262
+ const resolved = resolvedNumericBindings(graph, { ...target, ...updates })
263
+ Object.assign(updates, pick(resolved, synced as (keyof SceneNode)[]))
264
+ }
265
+
266
+ /** The modes the copy takes in this sync decide what its bindings resolve to. */
267
+ function applySyncedModes(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void {
268
+ if (updates.variableModes && !isEqual(updates.variableModes, target.variableModes))
269
+ graph.updateNode(target.id, { variableModes: updates.variableModes })
270
+ }
271
+
272
+ /**
273
+ * Strokes a sync or swap gives a copy draw with the copy's own line fields: the weight,
274
+ * alignment, cap, join, or dash it overrides, and a bound weight resolved in its modes. Figma
275
+ * keeps a stroke weight override when the component's color changes (live).
276
+ */
277
+ export function applySyncedStrokeLines(
278
+ graph: SceneGraph,
279
+ target: SceneNode,
280
+ updates: Partial<SceneNode>,
281
+ kept: (field: string) => boolean
282
+ ): void {
283
+ const strokes = updates.strokes
284
+ const first = strokes?.at(0)
285
+ if (!strokes || !first) return
286
+ const boundWeight = kept('strokeWeight')
287
+ ? undefined
288
+ : (updates.boundVariables ?? target.boundVariables).strokeWeight
289
+ const weightScale =
290
+ (updates.variableBindingScales ?? target.variableBindingScales).strokeWeight ?? 1
291
+ const resolvedWeight = boundWeight
292
+ ? graph.resolveNumberVariableForNode(target.id, boundWeight, 'active')
293
+ : undefined
294
+ const lines: StrokeLines = {}
295
+ let set = false
296
+ for (const key of STROKE_LINE_KEYS) {
297
+ const field = STROKE_LINE_FIELDS[key]
298
+ const own = kept(field)
299
+ // A bound weight takes the value it resolves to where the copy sits, not its component's.
300
+ const synced = own ? target[field] : updates[field]
301
+ const value =
302
+ field === 'strokeWeight' && resolvedWeight !== undefined
303
+ ? resolvedWeight * weightScale
304
+ : synced
305
+ if (value === undefined) continue
306
+ // A field the copy overrides holds for every stroke; the component's own values keep its
307
+ // strokes' differences unless a binding resolved the first one apart.
308
+ const differs = own
309
+ ? strokes.some((stroke) => !isEqual(value, strokeLine(stroke, key)))
310
+ : !isEqual(value, strokeLine(first, key))
311
+ if (!differs) continue
312
+ Reflect.set(lines, key, value)
313
+ set = true
314
+ }
315
+ if (set) updates.strokes = withStrokeLines(strokes, lines)
316
+ }
317
+
207
318
  function cloneChildInCoordinates(
208
319
  graph: SceneGraph,
209
320
  src: SceneNode,
@@ -213,10 +324,12 @@ function cloneChildInCoordinates(
213
324
  ): SceneNode {
214
325
  const componentScale =
215
326
  (src.componentScale * targetParent.componentScale) / sourceParent.componentScale
216
- return graph.createNode(src.type, targetParent.id, {
327
+ const clone = graph.createNode(src.type, targetParent.id, {
217
328
  ...cloneNodeProps(sourceInTargetCoordinates(src, componentScale), src.id, mode),
218
329
  componentScale
219
330
  })
331
+ copyAppliedBoundPaints(src, clone)
332
+ return clone
220
333
  }
221
334
 
222
335
  export function cloneChildrenWithMapping(
@@ -267,17 +380,28 @@ function childBindingProtection(
267
380
  return (field: string) => protectedField(field) || ownsSelfFillField(child, field)
268
381
  }
269
382
 
383
+ /** Links a copy to the component layer it matched, reporting each changed node and its owner. */
270
384
  function linkMatchedChild(
271
- overrides: InstanceOverrideState,
385
+ graph: SceneGraph,
386
+ context: InstanceSyncContext,
272
387
  instParentId: string,
273
388
  instChild: SceneNode,
274
389
  compChildId: string
275
390
  ): void {
276
- if (instChild.type === 'INSTANCE') {
391
+ const { overrides } = context
392
+ const recorded = hasNodeInstanceOverride(
393
+ overrides,
394
+ instParentId,
395
+ instChild.id,
396
+ 'sourceComponentId'
397
+ )
398
+ if (instChild.type === 'INSTANCE' || recorded) {
399
+ const owner = graph.nodes.get(context.instanceId)
400
+ graph.willChangeNode(owner)
277
401
  setInstanceOverride(overrides, instParentId, instChild.id, 'sourceComponentId', compChildId)
278
- } else {
279
- instChild.componentId = compChildId
402
+ if (owner) graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })
280
403
  }
404
+ if (instChild.type !== 'INSTANCE') graph.updateNode(instChild.id, { componentId: compChildId })
281
405
  }
282
406
 
283
407
  function matchFallbackChildren(
@@ -285,7 +409,7 @@ function matchFallbackChildren(
285
409
  compParent: SceneNode,
286
410
  instParent: SceneNode,
287
411
  instParentId: string,
288
- overrides: InstanceOverrideState,
412
+ context: InstanceSyncContext,
289
413
  instChildMap: Map<string, SceneNode>,
290
414
  usedInstChildIds: Set<string>
291
415
  ): void {
@@ -326,7 +450,7 @@ function matchFallbackChildren(
326
450
  remainingCandidateCount.set(compChild.type, candidateCount - 1)
327
451
  instChildMap.set(compChildId, match)
328
452
  usedInstChildIds.add(match.id)
329
- linkMatchedChild(overrides, instParentId, match, compChildId)
453
+ linkMatchedChild(graph, context, instParentId, match, compChildId)
330
454
  }
331
455
  }
332
456
 
@@ -440,6 +564,7 @@ function placeMissingChildren(
440
564
  cloneChildrenWithMapping(graph, compChildId, clone.id)
441
565
  }
442
566
  applyEnclosingAssignments(graph, clone)
567
+ applyBoundPaintColorsIn(graph, clone.id)
443
568
  instChildMap.set(compChildId, clone)
444
569
  usedInstChildIds.add(clone.id)
445
570
  context.matched.add(clone.id)
@@ -490,7 +615,7 @@ export function syncChildren(
490
615
  compParent,
491
616
  instParent,
492
617
  instParentId,
493
- overrides,
618
+ context,
494
619
  instChildMap,
495
620
  usedInstChildIds
496
621
  )
@@ -508,6 +633,8 @@ export function syncChildren(
508
633
 
509
634
  const protectedField = childBindingProtection(graph, instChild, overrides)
510
635
  const driven = propertyDrivenFields(graph, instChild, compChild)
636
+ const kept = (key: string) =>
637
+ driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
511
638
  const componentScale =
512
639
  (compChild.componentScale * instParent.componentScale) / compParent.componentScale
513
640
  const effectiveSource = sourceWithEffectiveFill(graph, compChild, instChild)
@@ -529,11 +656,11 @@ export function syncChildren(
529
656
  compParent,
530
657
  instParent,
531
658
  factor: componentScale / compChild.componentScale,
532
- skip: (key) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
659
+ skip: kept
533
660
  })
534
661
  Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
535
662
  keepOwnContentSize(graph, updates, instChild, source, placement)
536
- updateSyncedProps(graph, instChild, updates)
663
+ updateSyncedProps(graph, instChild, updates, kept)
537
664
 
538
665
  if (
539
666
  compChild.childIds.length > 0 &&
@@ -0,0 +1,13 @@
1
+ import type { SceneNode } from '../types'
2
+
3
+ /**
4
+ * A frame's children in the order they are drawn, back to front. An auto layout frame asked for
5
+ * Figma's "Reverse Z-index" paints them in reverse: the first layer sits on top, so the last one
6
+ * is drawn first. Everywhere else, and in a frame without auto layout, layer order draws.
7
+ */
8
+ export function drawnChildIds(
9
+ parent: Pick<SceneNode, 'childIds' | 'layoutMode' | 'itemReverseZIndex'>
10
+ ): string[] {
11
+ if (parent.layoutMode === 'NONE' || !parent.itemReverseZIndex) return parent.childIds
12
+ return [...parent.childIds].reverse()
13
+ }
@@ -1,4 +1,5 @@
1
1
  export * from './aspect-ratio'
2
+ export * from './draw-order'
2
3
  export * from './guides'
3
4
  export * from './keywords'
4
5
  export * from './sizing'
@@ -53,6 +53,27 @@ export function newStrokeGeometry(
53
53
  return { weight: first?.weight ?? node.strokeWeight, align: first?.align ?? node.strokeAlign }
54
54
  }
55
55
 
56
+ /**
57
+ * The name a layer of `type` carries when nothing names it, as Figma writes a type's name: words
58
+ * of a compound type apart, so a shape with text is a "Shape with text".
59
+ */
60
+ export function defaultNodeName(type: NodeType): string {
61
+ const words = type.toLowerCase().replaceAll('_', ' ')
62
+ return words.charAt(0).toUpperCase() + words.slice(1)
63
+ }
64
+
65
+ /**
66
+ * The name Figma gives a layer that follows its text: the characters trimmed, on one line, and
67
+ * the type's own name while they hold nothing.
68
+ */
69
+ export function textLayerName(type: NodeType, text: string): string {
70
+ const lines = text
71
+ .split(/\r\n|\r|\n/)
72
+ .map((line) => line.trim())
73
+ .filter((line) => line !== '')
74
+ return lines.join(' ') || defaultNodeName(type)
75
+ }
76
+
56
77
  export function createDefaultNode(
57
78
  generateId: () => string,
58
79
  type: NodeType,
package/src/order-keys.ts CHANGED
@@ -9,6 +9,12 @@ const ORDER_KEY_MAX = 126 // '~'
9
9
  const ORDER_KEY_MID = 'O'
10
10
  const INDEX_KEY_BASE = ORDER_KEY_MAX - ORDER_KEY_MIN + 1
11
11
 
12
+ /** Sorts order keys as Figma does: by plain string comparison. */
13
+ export function compareOrderKeys(a: string, b: string): number {
14
+ if (a === b) return 0
15
+ return a < b ? -1 : 1
16
+ }
17
+
12
18
  /** The order key for position `index` of a freshly numbered sibling list. */
13
19
  export function fractionalPosition(index: number): string {
14
20
  const numTildes = Math.floor(index / INDEX_KEY_BASE)