@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
package/src/resize.ts CHANGED
@@ -23,6 +23,8 @@ export type ResizeSnapshot = Pick<
23
23
  | 'y'
24
24
  | 'width'
25
25
  | 'height'
26
+ | 'flipX'
27
+ | 'flipY'
26
28
  | 'vectorNetwork'
27
29
  | 'fillGeometry'
28
30
  | 'strokeGeometry'
@@ -45,26 +47,52 @@ const CONSTRAINT_CONTAINER_TYPES = new Set([
45
47
  'BOOLEAN_OPERATION'
46
48
  ])
47
49
 
50
+ type AxisPlacement = { position: number; size: number; flipped?: boolean }
51
+
52
+ /** A child's box after its parent resizes, and the axes on which it turned over. */
53
+ export type ConstrainedRect = Rect & { flipsX?: boolean; flipsY?: boolean }
54
+
55
+ /**
56
+ * Where a constraint places a child on one axis when its parent goes from `parentBefore` to
57
+ * `parentAfter`, as Figma does: Right and Left & right move or grow it by the change in the
58
+ * parent's size rounded to whole pixels, Center by the change in its rounded half, and Scale by
59
+ * the exact ratio. The child's own box is never rounded, so a resize to the same size changes
60
+ * nothing. Left & right that would leave no size keeps the axis as it was; past zero, the child
61
+ * turns over and its box runs back from the edge it was pinned to, as the layer is mirrored in
62
+ * Figma. `scaleStep` snaps a scaled position to that fraction of a pixel.
63
+ */
48
64
  function constrainedAxis(
49
65
  position: number,
50
66
  size: number,
51
67
  parentBefore: number,
52
68
  parentAfter: number,
53
- constraint: ConstraintType
54
- ): { position: number; size: number } {
55
- const delta = parentAfter - parentBefore
69
+ constraint: ConstraintType,
70
+ scaleStep = 0,
71
+ allowFlips = true
72
+ ): AxisPlacement {
73
+ const delta = Math.round(parentAfter) - Math.round(parentBefore)
56
74
  if (constraint === 'MAX') return { position: position + delta, size }
57
- if (constraint === 'CENTER') return { position: position + delta / 2, size }
58
- if (constraint === 'STRETCH') return { position, size: Math.max(1, size + delta) }
75
+ if (constraint === 'CENTER') {
76
+ return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }
77
+ }
78
+ if (constraint === 'STRETCH') {
79
+ const stretched = size + delta
80
+ if (!allowFlips) return { position, size: Math.abs(stretched) }
81
+ if (stretched === 0) return { position, size }
82
+ if (stretched > 0) return { position, size: stretched }
83
+ return { position: position + stretched, size: -stretched, flipped: true }
84
+ }
59
85
  if (constraint === 'SCALE' && parentBefore > 0) {
60
86
  const scale = parentAfter / parentBefore
61
- return { position: position * scale, size: Math.max(1, size * scale) }
87
+ const scaled = position * scale
88
+ return {
89
+ position: scaleStep > 0 ? Math.round(scaled / scaleStep) * scaleStep : scaled,
90
+ size: size * scale
91
+ }
62
92
  }
63
93
  return { position, size }
64
94
  }
65
95
 
66
- type AxisPlacement = { position: number; size: number }
67
-
68
96
  const FLEXIBLE_CONSTRAINTS: ReadonlySet<ConstraintType> = new Set(['STRETCH', 'SCALE'])
69
97
 
70
98
  /** The axis at `size`, keeping the edge or centre its constraint pins where it was placed. */
@@ -108,75 +136,60 @@ function keepLockedRatio(
108
136
  : { x: resizedInPlace(x, y.size * aspectRatio, horizontal), y }
109
137
  }
110
138
 
111
- /** A child's box after its parent resizes, keeping a locked aspect ratio as Figma does. */
112
- export function constrainedChildRect(
139
+ type ChildRectPlacer = (
113
140
  child: Rect,
114
141
  parentBefore: Pick<Rect, 'width' | 'height'>,
115
142
  parentAfter: Pick<Rect, 'width' | 'height'>,
116
143
  horizontal: ConstraintType,
117
144
  vertical: ConstraintType,
118
- aspectRatio: number | null = null
119
- ): Rect {
120
- let x = constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal)
121
- let y = constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical)
122
- ;({ x, y } = keepLockedRatio(x, y, horizontal, vertical, aspectRatio))
123
- return {
124
- x: Math.round(x.position),
125
- y: Math.round(y.position),
126
- width: Math.round(x.size),
127
- height: Math.round(y.size)
145
+ aspectRatio?: number | null
146
+ ) => ConstrainedRect
147
+
148
+ /** Places a child by its constraints, snapping a scaled position to `scaleStep` when it is set. */
149
+ function childRectPlacer(scaleStep: number, allowFlips = true): ChildRectPlacer {
150
+ return (child, parentBefore, parentAfter, horizontal, vertical, aspectRatio = null) => {
151
+ const { x, y } = keepLockedRatio(
152
+ constrainedAxis(
153
+ child.x,
154
+ child.width,
155
+ parentBefore.width,
156
+ parentAfter.width,
157
+ horizontal,
158
+ scaleStep,
159
+ allowFlips
160
+ ),
161
+ constrainedAxis(
162
+ child.y,
163
+ child.height,
164
+ parentBefore.height,
165
+ parentAfter.height,
166
+ vertical,
167
+ scaleStep,
168
+ allowFlips
169
+ ),
170
+ horizontal,
171
+ vertical,
172
+ aspectRatio
173
+ )
174
+ const rect: ConstrainedRect = { x: x.position, y: y.position, width: x.size, height: y.size }
175
+ if (x.flipped) rect.flipsX = true
176
+ if (y.flipped) rect.flipsY = true
177
+ return rect
128
178
  }
129
179
  }
130
180
 
131
- function layoutConstrainedAxis(
132
- position: number,
133
- size: number,
134
- parentBefore: number,
135
- parentAfter: number,
136
- constraint: ConstraintType
137
- ): { position: number; size: number } {
138
- const delta = Math.round(parentAfter) - Math.round(parentBefore)
139
- if (constraint === 'MAX') return { position: position + delta, size }
140
- if (constraint === 'CENTER') {
141
- return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }
142
- }
143
- if (constraint === 'STRETCH') return { position, size: Math.abs(size + delta) }
144
- if (constraint === 'SCALE' && parentBefore > 0) {
145
- const scale = parentAfter / parentBefore
146
- return { position: Math.round(position * scale * 128) / 128, size: size * scale }
147
- }
148
- return { position, size }
149
- }
181
+ /**
182
+ * A child's box after its parent is resized, by a handle or `resize()`, keeping a locked aspect
183
+ * ratio as Figma does.
184
+ */
185
+ export const constrainedChildRect = childRectPlacer(0)
150
186
 
151
187
  /**
152
188
  * Where a child goes when layout resizes its parent, as Figma places a layer that ignores auto
153
- * layout when its Hug frame grows (measured on desktop 126): Right and Left & right move or grow
154
- * it by the change in the parent's size rounded to whole pixels, Center by the change in its
155
- * rounded half, and Scale by the exact ratio, its position to 1/128 of a pixel. Sizes are not
156
- * rounded, so applying a run of changes one at a time lands where applying them at once does.
189
+ * layout when its Hug frame grows (measured on desktop 126): as `constrainedChildRect`, except
190
+ * that a scaled position lands on 1/128 of a pixel.
157
191
  */
158
- export function layoutConstrainedChildRect(
159
- child: Rect,
160
- parentBefore: Pick<Rect, 'width' | 'height'>,
161
- parentAfter: Pick<Rect, 'width' | 'height'>,
162
- horizontal: ConstraintType,
163
- vertical: ConstraintType,
164
- aspectRatio: number | null = null
165
- ): Rect {
166
- const placed = keepLockedRatio(
167
- layoutConstrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal),
168
- layoutConstrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical),
169
- horizontal,
170
- vertical,
171
- aspectRatio
172
- )
173
- return {
174
- x: placed.x.position,
175
- y: placed.y.position,
176
- width: placed.x.size,
177
- height: placed.y.size
178
- }
179
- }
192
+ export const layoutConstrainedChildRect = childRectPlacer(1 / 128, false)
180
193
 
181
194
  export function scaledChildRect(
182
195
  child: Rect,
@@ -229,6 +242,8 @@ export function createResizeSnapshot(node: SceneNode): ResizeSnapshot {
229
242
  y: node.y,
230
243
  width: node.width,
231
244
  height: node.height,
245
+ flipX: node.flipX,
246
+ flipY: node.flipY,
232
247
  vectorNetwork: node.vectorNetwork ? cloneVectorNetwork(node.vectorNetwork) : null,
233
248
  fillGeometry: copyGeometryPaths(node.fillGeometry),
234
249
  strokeGeometry: copyGeometryPaths(node.strokeGeometry),
@@ -323,6 +338,35 @@ export function scaledGeometryChanges(
323
338
  return changes
324
339
  }
325
340
 
341
+ /**
342
+ * The saved outlines a size change invalidates on a layer drawn from its live box: its fill
343
+ * outline follows the box, and its saved stroke outline goes, so the stroke is drawn again at its
344
+ * weight through the live shape. A vector and text draw from outlines of their own, which scaling
345
+ * keeps. Without this, a layer that came from a `.fig` with frame outlines keeps drawing the old
346
+ * box after layout, the Plugin API or an instance sync resizes it.
347
+ */
348
+ export function shapeGeometryChanges(
349
+ node: SceneNode,
350
+ width: number,
351
+ height: number
352
+ ): Pick<Partial<SceneNode>, 'fillGeometry' | 'strokeGeometry'> {
353
+ if (node.type === 'VECTOR' || node.type === 'TEXT') return {}
354
+ if (node.fillGeometry.length === 0 && node.strokeGeometry.length === 0) return {}
355
+ // A box with no extent has no outline to scale from; the live shape draws both again.
356
+ if (node.width <= 0 || node.height <= 0) return { fillGeometry: [], strokeGeometry: [] }
357
+ const changes = scaledGeometryChanges(
358
+ createResizeSnapshot(node),
359
+ node.width,
360
+ node.height,
361
+ width,
362
+ height
363
+ )
364
+ const geometry: Pick<Partial<SceneNode>, 'fillGeometry' | 'strokeGeometry'> = {}
365
+ if (changes.fillGeometry) geometry.fillGeometry = changes.fillGeometry
366
+ if (changes.strokeGeometry) geometry.strokeGeometry = changes.strokeGeometry
367
+ return geometry
368
+ }
369
+
326
370
  type PathTextFields = Pick<SceneNode, 'derivedTextGlyphs' | 'textPathData' | 'textPathBox'>
327
371
 
328
372
  // Path text scales with its path, including baked path text that kept only rotated glyphs;
@@ -384,6 +428,24 @@ export function collectResizeDescendants(
384
428
  return snapshots.size > 0 ? snapshots : null
385
429
  }
386
430
 
431
+ /**
432
+ * The flips a child takes when its constraints place it from `from`, written only where they
433
+ * differ from the ones it has now, so a drag that crosses back undoes its own turn.
434
+ */
435
+ export function flipChanges(
436
+ from: Pick<SceneNode, 'flipX' | 'flipY'>,
437
+ current: Pick<SceneNode, 'flipX' | 'flipY'>,
438
+ flipsX = false,
439
+ flipsY = false
440
+ ): Partial<Pick<SceneNode, 'flipX' | 'flipY'>> {
441
+ const changes: Partial<Pick<SceneNode, 'flipX' | 'flipY'>> = {}
442
+ const flipX = from.flipX !== flipsX
443
+ const flipY = from.flipY !== flipsY
444
+ if (flipX !== current.flipX) changes.flipX = flipX
445
+ if (flipY !== current.flipY) changes.flipY = flipY
446
+ return changes
447
+ }
448
+
387
449
  export function computeConstrainedResizeChanges(
388
450
  graph: ResizeGraph,
389
451
  rootId: string,
@@ -410,7 +472,7 @@ export function computeConstrainedResizeChanges(
410
472
  compute(childId, original, child)
411
473
  continue
412
474
  }
413
- const rect = scalesChildren
475
+ const rect: ConstrainedRect = scalesChildren
414
476
  ? scaledChildRect(original, parentBefore, parentAfter)
415
477
  : constrainedChildRect(
416
478
  original,
@@ -420,13 +482,15 @@ export function computeConstrainedResizeChanges(
420
482
  child.verticalConstraint,
421
483
  enforcedAspectRatio(child)
422
484
  )
485
+ const { flipsX, flipsY, ...box } = rect
423
486
  const childChanges: Partial<SceneNode> = {
424
- ...rect,
425
- ...scaledGeometryChanges(original, original.width, original.height, rect.width, rect.height)
487
+ ...box,
488
+ ...flipChanges(original, child, flipsX, flipsY),
489
+ ...scaledGeometryChanges(original, original.width, original.height, box.width, box.height)
426
490
  }
427
491
  changes.set(childId, childChanges)
428
492
  // The final pass sees layout containers after Yoga has resolved HUG/FILL sizing.
429
- compute(childId, original, child.layoutMode === 'NONE' ? rect : child)
493
+ compute(childId, original, child.layoutMode === 'NONE' ? box : child)
430
494
  }
431
495
  }
432
496
 
@@ -1,6 +1,7 @@
1
1
  import { isEqual, omit } from 'es-toolkit'
2
2
 
3
3
  import type { SceneGraph } from './graph'
4
+ import { STROKE_LINE_KEYS } from './strokes'
4
5
  import type {
5
6
  CharacterStyleOverride,
6
7
  SceneNode,
@@ -99,9 +100,6 @@ export function getSharedStyles(graph: SceneGraph, kind: SharedStyleKind): Share
99
100
  return styles.sort((left, right) => left.name.localeCompare(right.name))
100
101
  }
101
102
 
102
- /** What a stroke draws along beside its paint: the layer's, not its stroke style's. */
103
- const STROKE_LINE_KEYS = ['weight', 'align', 'cap', 'join', 'dashPattern'] as const
104
-
105
103
  /**
106
104
  * Whether new strokes only change how their lines run, keeping every paint. A stroke style gives
107
105
  * paints only, so such a write keeps it; any other strokes write leaves it, as setting even the
@@ -1,3 +1,4 @@
1
+ import { propertyOwner } from '../components/properties'
1
2
  import type { SceneGraph } from '../graph'
2
3
  import { numberedName } from '../naming'
3
4
  import type { ComponentPropertyDefinition, SceneNode, SlotSettings } from '../types'
@@ -11,8 +12,9 @@ export const DEFAULT_SLOT_SETTINGS: SlotSettings = {
11
12
  }
12
13
 
13
14
  /**
14
- * The main component a layer belongs to, which defines the slots inside it. Layers of an
15
- * instance are not authored there, so a layer under an instance has none.
15
+ * The main component or set that defines the slots inside a layer: the component the layer
16
+ * belongs to, or that component's set. Layers of an instance are not authored there, so a
17
+ * layer under an instance has none.
16
18
  */
17
19
  export function slotOwner(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
18
20
  if (!node.parentId) return undefined
@@ -21,7 +23,7 @@ export function slotOwner(graph: SceneGraph, node: SceneNode): SceneNode | undef
21
23
  (current) =>
22
24
  current.type === 'COMPONENT' || current.type === 'INSTANCE' || current.type === 'CANVAS'
23
25
  )
24
- return owner?.type === 'COMPONENT' ? owner : undefined
26
+ return owner?.type === 'COMPONENT' ? propertyOwner(graph, owner) : undefined
25
27
  }
26
28
 
27
29
  /** Whether a frame inside a main component can become a slot. */
@@ -3,6 +3,7 @@ import type { SceneGraph } from '../graph'
3
3
  import { getInstanceOverride, setInstanceOverride } from '../instances/overrides'
4
4
  import { cloneChildrenWithMapping } from '../instances/sync'
5
5
  import type { SceneNode } from '../types'
6
+ import { applyBoundPaintColorsIn } from '../variables/paints'
6
7
  import { ownsSlotContent, slotPropertyId } from './frames'
7
8
 
8
9
  /**
@@ -108,7 +109,9 @@ export function resetSlotContent(
108
109
  Reflect.deleteProperty(assignments, propertyId)
109
110
  graph.updateNode(instance.id, { componentPropertyAssignments: assignments })
110
111
  const source = componentSource(instance, frame)
111
- if (source && graph.nodes.has(source)) cloneChildrenWithMapping(graph, source, frame.id)
112
+ if (!source || !graph.nodes.has(source)) return
113
+ cloneChildrenWithMapping(graph, source, frame.id)
114
+ for (const childId of frame.childIds) applyBoundPaintColorsIn(graph, childId)
112
115
  }
113
116
 
114
117
  /** Remove everything from the slot; the instance keeps owning it, now empty. */
@@ -1,8 +1,23 @@
1
1
  import type { SceneNode } from './types'
2
2
 
3
- export function markSourceFieldsEdited(node: SceneNode, changeKeys: string[]): void {
4
- if (changeKeys.length === 0) return
3
+ /** Values a peer computes for itself; recording one as an edit would differ between replicas. */
4
+ const DERIVED_FIELDS: ReadonlySet<string> = new Set(['textPicture'])
5
+
6
+ /** Told each field an edit marked for the first time, so a step can take the mark back. */
7
+ export type SourceMarkObserver = (nodeId: string, field: string) => void
8
+
9
+ export function markSourceFieldsEdited(
10
+ node: SceneNode,
11
+ changeKeys: string[],
12
+ observe?: SourceMarkObserver
13
+ ): void {
14
+ const edited = changeKeys.filter((key) => !DERIVED_FIELDS.has(key))
15
+ if (edited.length === 0) return
5
16
  const editedFields = new Set(node.source.editedFields)
6
- for (const key of changeKeys) editedFields.add(key)
17
+ for (const key of edited) {
18
+ if (editedFields.has(key)) continue
19
+ observe?.(node.id, key)
20
+ editedFields.add(key)
21
+ }
7
22
  node.source.editedFields = [...editedFields]
8
23
  }
package/src/strokes.ts ADDED
@@ -0,0 +1,121 @@
1
+ import { isEqual } from 'es-toolkit'
2
+
3
+ import { copyStroke } from './copy'
4
+ import type { SceneNode, Stroke } from './types'
5
+
6
+ /** What a stroke draws along beside its paint. */
7
+ export const STROKE_LINE_KEYS = [
8
+ 'weight',
9
+ 'align',
10
+ 'cap',
11
+ 'join',
12
+ 'dashPattern'
13
+ ] as const satisfies readonly (keyof Stroke)[]
14
+
15
+ type StrokeLineKey = (typeof STROKE_LINE_KEYS)[number]
16
+
17
+ /**
18
+ * The layer field that keeps each line value. Figma keeps one of each per layer; a layer here
19
+ * also keeps them on each stroke, which the renderer draws.
20
+ */
21
+ export const STROKE_LINE_FIELDS = {
22
+ weight: 'strokeWeight',
23
+ align: 'strokeAlign',
24
+ cap: 'strokeCap',
25
+ join: 'strokeJoin',
26
+ dashPattern: 'dashPattern'
27
+ } as const satisfies Record<StrokeLineKey, keyof SceneNode>
28
+
29
+ export type StrokeLines = Partial<Pick<Stroke, StrokeLineKey>>
30
+
31
+ /** The value a stroke draws with; a stroke without a dash draws solid. */
32
+ export function strokeLine(stroke: Stroke, key: StrokeLineKey): Stroke[StrokeLineKey] {
33
+ return key === 'dashPattern' ? (stroke.dashPattern ?? []) : stroke[key]
34
+ }
35
+
36
+ /** `strokes` with each line field `values` names, as a layer field set alone gives every stroke. */
37
+ export function withStrokeLines(strokes: readonly Stroke[], values: StrokeLines): Stroke[] {
38
+ return strokes.map((stroke) => {
39
+ const next = copyStroke(stroke)
40
+ for (const key of STROKE_LINE_KEYS) {
41
+ const value = values[key]
42
+ if (value !== undefined) Reflect.set(next, key, structuredClone(value))
43
+ }
44
+ return next
45
+ })
46
+ }
47
+
48
+ /**
49
+ * What else `changes` sets so a layer's line fields and its strokes agree: new strokes set the
50
+ * layer's fields to the first stroke's, and a layer field set without strokes goes to every
51
+ * stroke. Strokes win when `changes` sets both, so restoring strokes restores what they draw.
52
+ * Only fields whose value changes are returned, so an instance overrides no more than an edit
53
+ * changed. A layer without strokes keeps its fields for the next stroke (`newStrokeGeometry`).
54
+ */
55
+ export function strokeLineChanges(
56
+ node: SceneNode,
57
+ changes: Partial<SceneNode>
58
+ ): Partial<SceneNode> {
59
+ if (changes.strokes) {
60
+ const first = changes.strokes.at(0)
61
+ if (!first) return {}
62
+ const derived: Partial<SceneNode> = {}
63
+ for (const key of STROKE_LINE_KEYS) {
64
+ const field = STROKE_LINE_FIELDS[key]
65
+ const current = field in changes ? changes[field] : node[field]
66
+ // A stroke without its own cap or join draws with the layer's.
67
+ const value = strokeLine(first, key) ?? current
68
+ if (!isEqual(current, value)) Reflect.set(derived, field, structuredClone(value))
69
+ }
70
+ // Strokes without their own cap or join take the layer's, so every stroke says what it draws.
71
+ const cap = derived.strokeCap ?? changes.strokeCap ?? node.strokeCap
72
+ const join = derived.strokeJoin ?? changes.strokeJoin ?? node.strokeJoin
73
+ if (changes.strokes.some((stroke) => stroke.cap === undefined || stroke.join === undefined)) {
74
+ derived.strokes = changes.strokes.map((stroke) => ({
75
+ ...copyStroke(stroke),
76
+ cap: stroke.cap ?? cap,
77
+ join: stroke.join ?? join
78
+ }))
79
+ }
80
+ return derived
81
+ }
82
+ if (node.strokes.length === 0) return {}
83
+ const values: StrokeLines = {}
84
+ let set = false
85
+ for (const key of STROKE_LINE_KEYS) {
86
+ const value = changes[STROKE_LINE_FIELDS[key]]
87
+ if (value === undefined) continue
88
+ Reflect.set(values, key, value)
89
+ set = true
90
+ }
91
+ // Most writes, such as a move or a layout pass, set no line field.
92
+ if (!set) return {}
93
+ const strokes = withStrokeLines(node.strokes, values)
94
+ return isEqual(strokes, node.strokes) ? {} : { strokes }
95
+ }
96
+
97
+ /**
98
+ * Whether the strokes `changes` sets differ from `node`'s by more than the layer fields the same
99
+ * write sets: a paint, a stroke added or removed, or a stroke's line that no layer field records.
100
+ * Figma overrides a weight or dash set on an instance layer alone, so a component's later paint
101
+ * change still reaches the copy (live).
102
+ */
103
+ export function changesMoreThanStrokeLines(node: SceneNode, changes: Partial<SceneNode>): boolean {
104
+ const next = changes.strokes
105
+ if (!next) return false
106
+ if (next.length !== node.strokes.length) return true
107
+ const lines: StrokeLines = {}
108
+ for (const key of STROKE_LINE_KEYS) {
109
+ const value = changes[STROKE_LINE_FIELDS[key]]
110
+ if (value !== undefined) Reflect.set(lines, key, value)
111
+ }
112
+ // Strokes written without a cap or join take the layer's, as `strokeLineChanges` gives them,
113
+ // and a stroke without a dash draws as solid as one with an empty dash.
114
+ const drawn = (stroke: Stroke): Stroke => ({
115
+ ...stroke,
116
+ cap: stroke.cap ?? node.strokeCap,
117
+ join: stroke.join ?? node.strokeJoin,
118
+ dashPattern: stroke.dashPattern ?? []
119
+ })
120
+ return !isEqual(withStrokeLines(node.strokes, lines).map(drawn), next.map(drawn))
121
+ }
@@ -12,16 +12,18 @@ export function detectTextDirection(text: string): Exclude<TextDirection, 'AUTO'
12
12
  return 'LTR'
13
13
  }
14
14
 
15
+ /** A node exported without the field, or drawn from partial props, reads as `AUTO`. */
15
16
  export function resolveTextDirection(
16
- direction: TextDirection,
17
+ direction: TextDirection | undefined,
17
18
  text: string
18
19
  ): Exclude<TextDirection, 'AUTO'> {
19
- return direction === 'AUTO' ? detectTextDirection(text) : direction
20
+ return !direction || direction === 'AUTO' ? detectTextDirection(text) : direction
20
21
  }
21
22
 
22
- export function resolveNodeTextDirection(
23
- node: Pick<SceneNode, 'textDirection' | 'text'>
24
- ): 'LTR' | 'RTL' {
23
+ export function resolveNodeTextDirection(node: {
24
+ textDirection?: TextDirection
25
+ text: string
26
+ }): 'LTR' | 'RTL' {
25
27
  return resolveTextDirection(node.textDirection, node.text)
26
28
  }
27
29
 
@@ -13,6 +13,16 @@ export function lineHeightPixels(lineHeight: TextLength | null, fontSize: number
13
13
  return lineHeight ? textLengthPixels(lineHeight, fontSize) : null
14
14
  }
15
15
 
16
+ /**
17
+ * How far apart a line height sets lines at `fontSize`, as Figma steps them: a height in pixels
18
+ * as it is, a percent one to whole pixels (Figma desktop 126); null when it is automatic.
19
+ */
20
+ export function lineStepPixels(lineHeight: TextLength | null, fontSize: number): number | null {
21
+ if (!lineHeight) return null
22
+ const pixels = textLengthPixels(lineHeight, fontSize)
23
+ return lineHeight.unit === 'PIXELS' ? pixels : Math.round(pixels)
24
+ }
25
+
16
26
  /** A length in pixels, as a bound number variable and most authored formats give one. */
17
27
  export function pixelLength(value: number): TextLength {
18
28
  return { value, unit: 'PIXELS' }
@@ -1,12 +1,35 @@
1
- import type { VisualBoundsNode } from '../geometry'
1
+ type TextBoxNode = { height: number; textAlignVertical?: string }
2
+ type TextBoundsNode = TextBoxNode & { type?: string; derivedTextHeight?: number | null }
2
3
 
3
- type TextBoundsNode = Pick<VisualBoundsNode, 'type' | 'height' | 'derivedTextHeight'>
4
+ export type TextLinesReach = { minY: number; maxY: number }
4
5
 
5
6
  /**
6
- * How far a text's lines reach below its box: a fixed-size box does not clip them, as in Figma,
7
- * and the renderer starts them at its top.
7
+ * Where the top of laid-out lines `contentHeight` tall sits in a text's box, by its vertical
8
+ * alignment. Lines taller than the box overflow on the side away from the alignment, as Figma
9
+ * lays them out.
8
10
  */
9
- export function textLinesOverflow(node: TextBoundsNode): number {
10
- if (node.type !== 'TEXT' || !node.derivedTextHeight) return 0
11
- return Math.max(0, node.derivedTextHeight - node.height)
11
+ export function textVerticalOffset(node: TextBoxNode, contentHeight: number): number {
12
+ const available = node.height - contentHeight
13
+ if (node.textAlignVertical === 'CENTER') {
14
+ return available / 2
15
+ }
16
+ if (node.textAlignVertical === 'BOTTOM') {
17
+ return available
18
+ }
19
+ return 0
20
+ }
21
+
22
+ /**
23
+ * The rows a text draws its lines over, in its own axes: its box, grown by the lines a fixed-size
24
+ * box lets run past it above or below, as in Figma.
25
+ */
26
+ export function textLinesReach(node: TextBoundsNode): TextLinesReach {
27
+ if (node.type !== 'TEXT' || !node.derivedTextHeight) {
28
+ return { minY: 0, maxY: node.height }
29
+ }
30
+ const linesTop = textVerticalOffset(node, node.derivedTextHeight)
31
+ return {
32
+ minY: Math.min(0, linesTop),
33
+ maxY: Math.max(node.height, linesTop + node.derivedTextHeight)
34
+ }
12
35
  }
@@ -75,7 +75,8 @@ export function prepareNodeTransfer(
75
75
  result.componentId = source.componentId ? node(source.componentId) : null
76
76
  result.instanceOverrides = remapInstanceOverrideState(source.instanceOverrides, {
77
77
  node,
78
- variable
78
+ variable,
79
+ property: (id) => refs.properties.get(id)
79
80
  })
80
81
  result.boundVariables = Object.fromEntries(
81
82
  Object.entries(source.boundVariables).map(([field, id]) => [field, variable(id)])
@@ -117,10 +118,11 @@ export function prepareNodeTransfer(
117
118
  id: property(definition.id),
118
119
  defaultValue: propertyValue(definition.id, definition.defaultValue)
119
120
  }))
120
- result.componentPropertyReferences = source.componentPropertyReferences.map((reference) => ({
121
- ...reference,
122
- propertyId: property(reference.propertyId)
123
- }))
121
+ // A reference to a property that neither the transfer nor its destination defines names nothing.
122
+ result.componentPropertyReferences = source.componentPropertyReferences.flatMap((reference) => {
123
+ const propertyId = refs.properties.get(reference.propertyId)
124
+ return propertyId ? [{ ...reference, propertyId }] : []
125
+ })
124
126
  result.componentPropertyAssignments = values(source.componentPropertyAssignments)
125
127
  // Variant values are keyed by display names, unlike definition-ID assignments.
126
128
  result.componentPropertyValues = { ...source.componentPropertyValues }
package/src/types.ts CHANGED
@@ -41,6 +41,10 @@ export interface NodeChangeObserver {
41
41
  before: (node: SceneNode) => void
42
42
  /** A node was just added. */
43
43
  created: (node: SceneNode) => void
44
+ /** What changes from now until `resume` is someone else's; `waitOutsideEdit` calls this. */
45
+ pause?: () => void
46
+ /** The wait ended, and what changed meanwhile belongs outside the edit. */
47
+ resume?: () => void
44
48
  }
45
49
 
46
50
  export type DocumentColorSpace = 'srgb' | 'display-p3'
@@ -184,6 +188,17 @@ export interface MeshGradientPoint {
184
188
 
185
189
  export type GradientTransform = Matrix
186
190
 
191
+ /** Figma's adjustments of an image paint, each from -1 to 1; 0 leaves the image as it is. */
192
+ export interface ImageFilters {
193
+ exposure?: number
194
+ contrast?: number
195
+ saturation?: number
196
+ temperature?: number
197
+ tint?: number
198
+ highlights?: number
199
+ shadows?: number
200
+ }
201
+
187
202
  export interface Fill {
188
203
  type: FillType
189
204
  color: Color
@@ -202,6 +217,8 @@ export interface Fill {
202
217
  imageTransform?: GradientTransform
203
218
  /** Clockwise turn of a Fill, Fit or Tile image, in multiples of 90°; a crop holds its own. */
204
219
  imageRotation?: number
220
+ /** An image's adjustments, kept and saved; the canvas does not draw them. */
221
+ filters?: ImageFilters
205
222
  sourceNodeId?: string
206
223
  scale?: number
207
224
  spacing?: number