@open-pencil/scene-graph 0.17.0-beta.3 → 0.17.0-beta.5

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 (283) hide show
  1. package/dist/arrays.d.ts +17 -0
  2. package/dist/arrays.d.ts.map +1 -0
  3. package/dist/arrays.js +38 -0
  4. package/dist/arrays.js.map +1 -0
  5. package/dist/buffered-events.js +1 -1
  6. package/dist/buffered-events.js.map +1 -1
  7. package/dist/clipping.d.ts +7 -1
  8. package/dist/clipping.d.ts.map +1 -1
  9. package/dist/clipping.js +23 -19
  10. package/dist/clipping.js.map +1 -1
  11. package/dist/components/index.d.ts +2 -2
  12. package/dist/components/index.js +2 -2
  13. package/dist/components/properties.d.ts +1 -3
  14. package/dist/components/properties.d.ts.map +1 -1
  15. package/dist/components/properties.js +2 -9
  16. package/dist/components/properties.js.map +1 -1
  17. package/dist/corners.d.ts +22 -0
  18. package/dist/corners.d.ts.map +1 -0
  19. package/dist/corners.js +52 -0
  20. package/dist/corners.js.map +1 -0
  21. package/dist/css/grid.d.ts +4 -1
  22. package/dist/css/grid.d.ts.map +1 -1
  23. package/dist/css/grid.js +16 -7
  24. package/dist/css/grid.js.map +1 -1
  25. package/dist/css/index.d.ts +2 -2
  26. package/dist/css/index.js +2 -2
  27. package/dist/css/values.d.ts +6 -1
  28. package/dist/css/values.d.ts.map +1 -1
  29. package/dist/font-style.d.ts +2 -1
  30. package/dist/font-style.d.ts.map +1 -1
  31. package/dist/font-style.js +31 -6
  32. package/dist/font-style.js.map +1 -1
  33. package/dist/geometry.d.ts +68 -3
  34. package/dist/geometry.d.ts.map +1 -1
  35. package/dist/geometry.js +155 -5
  36. package/dist/geometry.js.map +1 -1
  37. package/dist/gradient.d.ts +9 -2
  38. package/dist/gradient.d.ts.map +1 -1
  39. package/dist/gradient.js +29 -1
  40. package/dist/gradient.js.map +1 -1
  41. package/dist/graph.d.ts +4 -0
  42. package/dist/graph.d.ts.map +1 -1
  43. package/dist/graph.js +30 -4
  44. package/dist/graph.js.map +1 -1
  45. package/dist/group-bounds.d.ts.map +1 -1
  46. package/dist/group-bounds.js +4 -3
  47. package/dist/group-bounds.js.map +1 -1
  48. package/dist/guides.d.ts +11 -2
  49. package/dist/guides.d.ts.map +1 -1
  50. package/dist/guides.js +30 -1
  51. package/dist/guides.js.map +1 -1
  52. package/dist/hit-test.d.ts.map +1 -1
  53. package/dist/hit-test.js +57 -5
  54. package/dist/hit-test.js.map +1 -1
  55. package/dist/icons/fit.d.ts +17 -0
  56. package/dist/icons/fit.d.ts.map +1 -0
  57. package/dist/icons/fit.js +21 -0
  58. package/dist/icons/fit.js.map +1 -0
  59. package/dist/icons/glyph.d.ts.map +1 -1
  60. package/dist/icons/glyph.js +5 -4
  61. package/dist/icons/glyph.js.map +1 -1
  62. package/dist/icons/index.d.ts +2 -1
  63. package/dist/icons/index.js +2 -1
  64. package/dist/images.d.ts +14 -4
  65. package/dist/images.d.ts.map +1 -1
  66. package/dist/images.js +45 -5
  67. package/dist/images.js.map +1 -1
  68. package/dist/instances/assignments.js +39 -6
  69. package/dist/instances/assignments.js.map +1 -1
  70. package/dist/instances/index.d.ts.map +1 -1
  71. package/dist/instances/index.js +39 -2
  72. package/dist/instances/index.js.map +1 -1
  73. package/dist/instances/kept-overrides.js +2 -2
  74. package/dist/instances/kept-overrides.js.map +1 -1
  75. package/dist/instances/matching.js +68 -0
  76. package/dist/instances/matching.js.map +1 -0
  77. package/dist/instances/overrides.d.ts +4 -1
  78. package/dist/instances/overrides.d.ts.map +1 -1
  79. package/dist/instances/overrides.js +13 -1
  80. package/dist/instances/overrides.js.map +1 -1
  81. package/dist/instances/protection.js +67 -0
  82. package/dist/instances/protection.js.map +1 -0
  83. package/dist/instances/swap.js +7 -5
  84. package/dist/instances/swap.js.map +1 -1
  85. package/dist/instances/sync.js +10 -116
  86. package/dist/instances/sync.js.map +1 -1
  87. package/dist/layout/guides.d.ts +17 -1
  88. package/dist/layout/guides.d.ts.map +1 -1
  89. package/dist/layout/guides.js +31 -11
  90. package/dist/layout/guides.js.map +1 -1
  91. package/dist/layout/index.d.ts +4 -2
  92. package/dist/layout/index.js +4 -2
  93. package/dist/layout/limits.d.ts +40 -0
  94. package/dist/layout/limits.d.ts.map +1 -0
  95. package/dist/layout/limits.js +54 -0
  96. package/dist/layout/limits.js.map +1 -0
  97. package/dist/layout/sizing.d.ts.map +1 -1
  98. package/dist/layout/sizing.js +0 -1
  99. package/dist/layout/sizing.js.map +1 -1
  100. package/dist/layout/spacing.d.ts +10 -0
  101. package/dist/layout/spacing.d.ts.map +1 -0
  102. package/dist/layout/spacing.js +12 -0
  103. package/dist/layout/spacing.js.map +1 -0
  104. package/dist/masks.d.ts +28 -0
  105. package/dist/masks.d.ts.map +1 -0
  106. package/dist/masks.js +53 -0
  107. package/dist/masks.js.map +1 -0
  108. package/dist/matrix.d.ts +2 -1
  109. package/dist/matrix.d.ts.map +1 -1
  110. package/dist/matrix.js +14 -1
  111. package/dist/matrix.js.map +1 -1
  112. package/dist/node-defaults.d.ts +9 -1
  113. package/dist/node-defaults.d.ts.map +1 -1
  114. package/dist/node-defaults.js +16 -1
  115. package/dist/node-defaults.js.map +1 -1
  116. package/dist/outline/flatten.d.ts.map +1 -1
  117. package/dist/outline/flatten.js +24 -17
  118. package/dist/outline/flatten.js.map +1 -1
  119. package/dist/outline/hit.d.ts +2 -8
  120. package/dist/outline/hit.d.ts.map +1 -1
  121. package/dist/outline/hit.js +16 -25
  122. package/dist/outline/hit.js.map +1 -1
  123. package/dist/outline/index.d.ts +3 -3
  124. package/dist/outline/index.js +3 -3
  125. package/dist/outline/shapes.d.ts +3 -1
  126. package/dist/outline/shapes.d.ts.map +1 -1
  127. package/dist/outline/shapes.js +18 -39
  128. package/dist/outline/shapes.js.map +1 -1
  129. package/dist/paints/index.d.ts +3 -1
  130. package/dist/paints/index.js +3 -1
  131. package/dist/paints/pattern.d.ts +13 -0
  132. package/dist/paints/pattern.d.ts.map +1 -0
  133. package/dist/paints/pattern.js +47 -0
  134. package/dist/paints/pattern.js.map +1 -0
  135. package/dist/paints/shows.d.ts +11 -0
  136. package/dist/paints/shows.d.ts.map +1 -0
  137. package/dist/paints/shows.js +16 -0
  138. package/dist/paints/shows.js.map +1 -0
  139. package/dist/paths/arc.d.ts +32 -0
  140. package/dist/paths/arc.d.ts.map +1 -0
  141. package/dist/paths/arc.js +49 -0
  142. package/dist/paths/arc.js.map +1 -0
  143. package/dist/paths/index.d.ts +5 -0
  144. package/dist/paths/index.js +5 -0
  145. package/dist/paths/network.d.ts +43 -0
  146. package/dist/paths/network.d.ts.map +1 -0
  147. package/dist/paths/network.js +249 -0
  148. package/dist/paths/network.js.map +1 -0
  149. package/dist/paths/sink.d.ts +20 -0
  150. package/dist/paths/sink.d.ts.map +1 -0
  151. package/dist/paths/sink.js +32 -0
  152. package/dist/paths/sink.js.map +1 -0
  153. package/dist/paths/smooth-corners.d.ts +25 -0
  154. package/dist/paths/smooth-corners.d.ts.map +1 -0
  155. package/dist/paths/smooth-corners.js +196 -0
  156. package/dist/paths/smooth-corners.js.map +1 -0
  157. package/dist/plugin-data/fields.d.ts +4 -0
  158. package/dist/plugin-data/fields.d.ts.map +1 -1
  159. package/dist/plugin-data/fields.js +2 -0
  160. package/dist/plugin-data/fields.js.map +1 -1
  161. package/dist/polygon.d.ts +1 -3
  162. package/dist/polygon.d.ts.map +1 -1
  163. package/dist/polygon.js +1 -16
  164. package/dist/polygon.js.map +1 -1
  165. package/dist/resize.d.ts.map +1 -1
  166. package/dist/resize.js +5 -5
  167. package/dist/resize.js.map +1 -1
  168. package/dist/scaling/node.d.ts.map +1 -1
  169. package/dist/scaling/node.js +25 -3
  170. package/dist/scaling/node.js.map +1 -1
  171. package/dist/text/auto-resize.d.ts +15 -0
  172. package/dist/text/auto-resize.d.ts.map +1 -0
  173. package/dist/text/auto-resize.js +23 -0
  174. package/dist/text/auto-resize.js.map +1 -0
  175. package/dist/text/index.d.ts +4 -3
  176. package/dist/text/index.js +4 -3
  177. package/dist/text/lengths.d.ts +6 -1
  178. package/dist/text/lengths.d.ts.map +1 -1
  179. package/dist/text/lengths.js +10 -1
  180. package/dist/text/lengths.js.map +1 -1
  181. package/dist/text/overflow.d.ts +14 -5
  182. package/dist/text/overflow.d.ts.map +1 -1
  183. package/dist/text/overflow.js +24 -6
  184. package/dist/text/overflow.js.map +1 -1
  185. package/dist/text/path.js +10 -0
  186. package/dist/text/path.js.map +1 -0
  187. package/dist/text/picture.js +5 -5
  188. package/dist/text/picture.js.map +1 -1
  189. package/dist/transfer/dependencies.d.ts.map +1 -1
  190. package/dist/transfer/dependencies.js +3 -2
  191. package/dist/transfer/dependencies.js.map +1 -1
  192. package/dist/transfer/nodes.js +2 -1
  193. package/dist/transfer/nodes.js.map +1 -1
  194. package/dist/types.d.ts +21 -8
  195. package/dist/types.d.ts.map +1 -1
  196. package/dist/types.js.map +1 -1
  197. package/dist/variables/index.d.ts +4 -3
  198. package/dist/variables/index.d.ts.map +1 -1
  199. package/dist/variables/index.js +4 -3
  200. package/dist/variables/index.js.map +1 -1
  201. package/dist/variables/lookup.d.ts +20 -0
  202. package/dist/variables/lookup.d.ts.map +1 -0
  203. package/dist/variables/lookup.js +34 -0
  204. package/dist/variables/lookup.js.map +1 -0
  205. package/dist/variables/paints.d.ts +35 -4
  206. package/dist/variables/paints.d.ts.map +1 -1
  207. package/dist/variables/paints.js +67 -16
  208. package/dist/variables/paints.js.map +1 -1
  209. package/dist/variables/resolution.d.ts +3 -1
  210. package/dist/variables/resolution.d.ts.map +1 -1
  211. package/dist/variables/resolution.js +8 -3
  212. package/dist/variables/resolution.js.map +1 -1
  213. package/dist/variables/subtree.js +1 -1
  214. package/dist/variables/subtree.js.map +1 -1
  215. package/dist/vector-network.d.ts.map +1 -1
  216. package/dist/vector-network.js +12 -76
  217. package/dist/vector-network.js.map +1 -1
  218. package/package.json +25 -1
  219. package/src/arrays.ts +37 -0
  220. package/src/buffered-events.ts +2 -1
  221. package/src/clipping.ts +11 -8
  222. package/src/components/properties.ts +1 -13
  223. package/src/corners.ts +93 -0
  224. package/src/css/grid.ts +21 -7
  225. package/src/css/index.ts +1 -1
  226. package/src/font-style.ts +38 -4
  227. package/src/geometry.ts +219 -6
  228. package/src/gradient.ts +23 -1
  229. package/src/graph.ts +22 -4
  230. package/src/group-bounds.ts +7 -3
  231. package/src/guides.ts +26 -1
  232. package/src/hit-test.ts +68 -10
  233. package/src/icons/fit.ts +24 -0
  234. package/src/icons/glyph.ts +5 -4
  235. package/src/icons/index.ts +1 -0
  236. package/src/images.ts +67 -7
  237. package/src/instances/assignments.ts +65 -7
  238. package/src/instances/index.ts +53 -4
  239. package/src/instances/kept-overrides.ts +2 -5
  240. package/src/instances/matching.ts +101 -0
  241. package/src/instances/overrides.ts +18 -0
  242. package/src/instances/protection.ts +103 -0
  243. package/src/instances/swap.ts +11 -9
  244. package/src/instances/sync.ts +11 -184
  245. package/src/layout/guides.ts +42 -9
  246. package/src/layout/index.ts +2 -0
  247. package/src/layout/limits.ts +64 -0
  248. package/src/layout/sizing.ts +0 -2
  249. package/src/layout/spacing.ts +11 -0
  250. package/src/masks.ts +51 -0
  251. package/src/matrix.ts +15 -2
  252. package/src/node-defaults.ts +23 -3
  253. package/src/outline/flatten.ts +30 -18
  254. package/src/outline/hit.ts +16 -33
  255. package/src/outline/index.ts +2 -2
  256. package/src/outline/shapes.ts +19 -36
  257. package/src/paints/index.ts +2 -0
  258. package/src/paints/pattern.ts +45 -0
  259. package/src/paints/shows.ts +13 -0
  260. package/src/paths/arc.ts +59 -0
  261. package/src/paths/index.ts +4 -0
  262. package/src/paths/network.ts +334 -0
  263. package/src/paths/sink.ts +50 -0
  264. package/src/paths/smooth-corners.ts +356 -0
  265. package/src/plugin-data/fields.ts +2 -0
  266. package/src/polygon.ts +0 -17
  267. package/src/resize.ts +6 -7
  268. package/src/scaling/node.ts +29 -3
  269. package/src/text/auto-resize.ts +25 -0
  270. package/src/text/index.ts +1 -0
  271. package/src/text/lengths.ts +11 -0
  272. package/src/text/overflow.ts +32 -6
  273. package/src/text/path.ts +13 -0
  274. package/src/text/picture.ts +5 -5
  275. package/src/transfer/dependencies.ts +3 -2
  276. package/src/transfer/nodes.ts +2 -1
  277. package/src/types.ts +25 -8
  278. package/src/variables/index.ts +1 -0
  279. package/src/variables/lookup.ts +31 -0
  280. package/src/variables/paints.ts +101 -20
  281. package/src/variables/resolution.ts +14 -2
  282. package/src/variables/subtree.ts +1 -1
  283. package/src/vector-network.ts +17 -138
@@ -1,3 +1,4 @@
1
+ import { appendAll } from '../arrays'
1
2
  import type { SceneGraph } from '../graph'
2
3
  import { imagePaintHashes } from '../images'
3
4
  import { overriddenBindingField } from '../instances/overrides'
@@ -70,7 +71,7 @@ export function layerDependencies(
70
71
  (styleLayers ??= sharedStyleLayers(graph)).get(id)
71
72
  const visit = (node: SceneNode): void => {
72
73
  for (const hash of layerImageHashes(node)) images.add(hash)
73
- pending.push(...layerVariableIds(node))
74
+ appendAll(pending, layerVariableIds(node))
74
75
  for (const id of Object.keys(node.variableModes))
75
76
  if (graph.variableCollections.has(id)) collections.add(id)
76
77
  for (const id of referencedStyleIds(node)) {
@@ -113,7 +114,7 @@ export function variableDependencies(
113
114
  variables.add(id)
114
115
  collections.add(variable.collectionId)
115
116
  for (const value of Object.values(variable.valuesByMode)) {
116
- pending.push(...variableValueReferences(value))
117
+ appendAll(pending, variableValueReferences(value))
117
118
  // A kept expression can name a mode of a collection none of its aliases reach.
118
119
  if (!isUnsupportedVariableValue(value)) continue
119
120
  for (const modeId of value.modes) {
@@ -67,7 +67,8 @@ export function prepareNodeTransfer(
67
67
  result.instanceOverrides = remapInstanceOverrideState(source.instanceOverrides, {
68
68
  node,
69
69
  variable,
70
- collection: (id) => refs.collections.get(id)
70
+ collection: (id) => refs.collections.get(id),
71
+ property: (id) => refs.properties.get(id)
71
72
  })
72
73
  Object.assign(
73
74
  result,
package/src/types.ts CHANGED
@@ -43,6 +43,8 @@ export interface NodeChangeObserver {
43
43
  before: (node: SceneNode) => void
44
44
  /** A node was just added. */
45
45
  created: (node: SceneNode) => void
46
+ /** A node is about to take a preview's changes, which no history keeps. */
47
+ previewed?: (node: SceneNode) => void
46
48
  /**
47
49
  * A variable or collection is about to change, be added, or be removed, and is still as it
48
50
  * was: absent, for one about to be added.
@@ -192,7 +194,10 @@ export interface Fill {
192
194
  blendMode?: BlendMode
193
195
  gradientStops?: GradientStop[]
194
196
  gradientTransform?: GradientTransform
197
+ /** The image an IMAGE paint draws, or the poster frame a VIDEO paint draws in its clip's place. */
195
198
  imageHash?: string
199
+ /** A VIDEO paint's clip, which `imageHash`'s poster frame stands in for when drawn. */
200
+ videoHash?: string
196
201
  imageScaleMode?: ImageScaleMode
197
202
  /** A crop's image-space window; Fill, Fit and Tile ignore it. */
198
203
  imageTransform?: GradientTransform
@@ -304,7 +309,7 @@ export interface Effect {
304
309
  }
305
310
 
306
311
  export type ConstraintType = 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' | 'SCALE'
307
- export type TextAutoResize = 'NONE' | 'HEIGHT' | 'WIDTH_AND_HEIGHT' | 'TRUNCATE'
312
+ export type TextAutoResize = 'NONE' | 'HEIGHT' | 'WIDTH_AND_HEIGHT'
308
313
 
309
314
  /** Where a connector end attaches to a layer: a side, the side Figma picks, or a fixed point. */
310
315
  export type ConnectorMagnet =
@@ -698,7 +703,7 @@ export interface SceneNode {
698
703
  height: number
699
704
  rotation: number
700
705
  source: SourceMetadata
701
- /** Materialized layout hint imported or generated outside the live Yoga layout pass. */
706
+ /** Materialized layout hint imported or generated outside the live layout pass. */
702
707
  derivedLayout: Partial<Rect> | null
703
708
 
704
709
  fills: Fill[]
@@ -789,7 +794,8 @@ export interface SceneNode {
789
794
  primaryAxisSizing: AxisSizingMode
790
795
  counterAxisSizing: AxisSizingMode
791
796
  itemSpacing: number
792
- counterAxisSpacing: number
797
+ /** Space between wrapped lines; null follows `itemSpacing`. Read it with `counterAxisSpacingOf`. */
798
+ counterAxisSpacing: number | null
793
799
  paddingTop: number
794
800
  paddingRight: number
795
801
  paddingBottom: number
@@ -942,9 +948,10 @@ export interface SceneNode {
942
948
  textPicture: Uint8Array | null
943
949
  derivedTextGlyphs: DerivedTextGlyph[] | null
944
950
  /**
945
- * How far a text's lines reach down from its top: measured at its width when its text or width
946
- * changes, or read from the lines a file saved, and never saved itself. A fixed-size box does not
947
- * clip them, as in Figma, so its visual bounds reach this far. Null when not known.
951
+ * How tall a text's lines are, from the top of the first to the bottom of the last: measured at
952
+ * its width when its text or width changes, or read from the lines a file saved, and never saved
953
+ * itself. A fixed-size box does not clip them, as in Figma, so its visual bounds reach past the
954
+ * box on the side away from its vertical alignment (`textLinesReach`). Null when not known.
948
955
  */
949
956
  derivedTextHeight: number | null
950
957
  /** Format-neutral layout path for text-on-path nodes. */
@@ -1128,14 +1135,24 @@ export interface Variable {
1128
1135
  version?: string
1129
1136
  }
1130
1137
 
1131
- /** Scalar numeric node fields accepted by numeric property controls. */
1138
+ /** Scalar numeric node fields accepted by numeric property controls; some may also be null. */
1132
1139
  export type NumericNodeProperty = {
1133
- [K in keyof SceneNode]-?: SceneNode[K] extends number ? K : never
1140
+ [K in keyof SceneNode]-?: SceneNode[K] extends number | null
1141
+ ? number extends SceneNode[K]
1142
+ ? K
1143
+ : never
1144
+ : never
1134
1145
  }[keyof SceneNode]
1135
1146
 
1136
1147
  export interface VariableCollectionMode {
1137
1148
  modeId: string
1138
1149
  name: string
1150
+ /**
1151
+ * The library mode this one came from, for a mode of a collection copied from a library. A
1152
+ * library that renames its mode renames this one with it, so the layers and the document set
1153
+ * to it keep following the library.
1154
+ */
1155
+ libraryModeId?: string
1139
1156
  /**
1140
1157
  * Where the mode applies in CSS: a selector (`[data-theme="dark"]`, `.compact`) or an
1141
1158
  * at-rule prelude (`@media (max-width: 640px)`). Absent means the default for its axis.
@@ -399,6 +399,7 @@ export * from './bindings'
399
399
  export * from './corners'
400
400
  export * from './formula'
401
401
  export * from './formula/schema'
402
+ export * from './lookup'
402
403
  export * from './paints'
403
404
  export * from './schema'
404
405
  export * from './text'
@@ -0,0 +1,31 @@
1
+ import type { SceneGraph } from '../graph'
2
+ import type { VariableCollection, VariableCollectionMode } from '../types'
3
+
4
+ /** What a reference by ID or name finds: one match, none, or several that share the name. */
5
+ export type NamedLookup<T> =
6
+ | { kind: 'found'; value: T }
7
+ | { kind: 'missing' }
8
+ | { kind: 'ambiguous'; matches: T[] }
9
+
10
+ function byName<T extends { name: string }>(items: Iterable<T>, name: string): NamedLookup<T> {
11
+ const matches = [...items].filter((item) => item.name === name)
12
+ if (matches.length === 1) return { kind: 'found', value: matches[0] }
13
+ return matches.length === 0 ? { kind: 'missing' } : { kind: 'ambiguous', matches }
14
+ }
15
+
16
+ /** The collection with this ID or, failing that, the one with this exact name. */
17
+ export function lookupCollection(graph: SceneGraph, ref: string): NamedLookup<VariableCollection> {
18
+ const byId = graph.variableCollections.get(ref)
19
+ if (byId) return { kind: 'found', value: byId }
20
+ return byName(graph.variableCollections.values(), ref)
21
+ }
22
+
23
+ /** The mode of `collection` with this ID or, failing that, the one with this exact name. */
24
+ export function lookupMode(
25
+ collection: VariableCollection,
26
+ ref: string
27
+ ): NamedLookup<VariableCollectionMode> {
28
+ const byId = collection.modes.find((mode) => mode.modeId === ref)
29
+ if (byId) return { kind: 'found', value: byId }
30
+ return byName(collection.modes, ref)
31
+ }
@@ -1,23 +1,28 @@
1
+ import { isEqual } from 'es-toolkit/predicate'
2
+
1
3
  import { copyFills, copyStrokes } from '../copy'
2
4
  import type { SceneGraph } from '../graph'
3
5
  import { hasInstanceOverride } from '../instances/overrides'
4
6
  import type { Color } from '../primitives'
5
7
  import type { Fill, SceneNode, Stroke } from '../types'
6
8
  import { paintBindingTarget, type PaintBindingList } from './fields'
7
- import { collectionsResolving, getNodeVariableModeId } from './resolution'
9
+ import { collectionsResolving, getNodeExplicitVariableModeId } from './resolution'
8
10
  import { subtreeNodes } from './subtree'
9
11
 
10
12
  /**
11
13
  * Figma keeps a bound paint's color and opacity as a cache of its variable: it draws and reads
12
14
  * what the paint holds, and writes the variable's color into it again only once the color the
13
- * variable gives that layer changes, so an edit to the paint sticks until then. The color each
14
- * binding last gave its paint lives here, beside the graph and never saved, by layer and binding
15
- * field. A layer without an entry, such as one just loaded, pasted, or restored, counts its
16
- * paint's own color as the last one applied.
15
+ * variable gives that layer changes, or the mode set for the layer changes, so an edit to the
16
+ * paint sticks until then. The color each binding last gave its paint, and the modes set for the
17
+ * layer then, live here, beside the graph and never saved, by layer and binding field. A layer
18
+ * without an entry, such as one just restored, counts its paint's own color as the last one
19
+ * applied, in the modes it has now.
17
20
  */
18
- interface AppliedColor {
21
+ export interface AppliedColor {
19
22
  readonly variableId: string
20
23
  readonly color: Color
24
+ /** The mode set for the layer, or null, in each collection the variable resolves through. */
25
+ readonly modes: Readonly<Record<string, string | null>>
21
26
  }
22
27
 
23
28
  const appliedColors = new WeakMap<SceneNode, Map<string, AppliedColor>>()
@@ -64,9 +69,32 @@ function sameColor(a: Color, b: Color): boolean {
64
69
  )
65
70
  }
66
71
 
67
- function lastApplied(node: SceneNode, bound: BoundPaint, paint: Fill): Color {
72
+ /**
73
+ * The modes set for a layer, or null, in the collections a variable resolves through. Figma
74
+ * refreshes a bound paint whenever one of them changes, even to a mode giving the same color.
75
+ */
76
+ function explicitModes(
77
+ graph: SceneGraph,
78
+ node: SceneNode,
79
+ variableId: string
80
+ ): Record<string, string | null> {
81
+ const modes: Record<string, string | null> = {}
82
+ for (const collectionId of collectionsResolving(graph, [variableId]))
83
+ modes[collectionId] = getNodeExplicitVariableModeId(graph, node.id, collectionId)
84
+ return modes
85
+ }
86
+
87
+ /** Whether the color a binding last applied is stale: another color, or set in other modes. */
88
+ function staleApplied(
89
+ node: SceneNode,
90
+ bound: BoundPaint,
91
+ paint: Fill,
92
+ color: Color,
93
+ modes: Readonly<Record<string, string | null>>
94
+ ): boolean {
68
95
  const entry = appliedColors.get(node)?.get(bound.field)
69
- return entry?.variableId === bound.variableId ? entry.color : paintColor(paint)
96
+ if (entry?.variableId !== bound.variableId) return !sameColor(paintColor(paint), color)
97
+ return !sameColor(entry.color, color) || !isEqual(entry.modes, modes)
70
98
  }
71
99
 
72
100
  /**
@@ -78,13 +106,63 @@ export function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void
78
106
  if (entries) appliedColors.set(copy, new Map(entries))
79
107
  }
80
108
 
81
- function recordApplied(node: SceneNode, bound: BoundPaint, color: Color): void {
109
+ /** The colors copied layers' bindings last applied, by layer id, as a copy captured them. */
110
+ export type AppliedBoundPaints = ReadonlyMap<string, ReadonlyMap<string, AppliedColor>>
111
+
112
+ /**
113
+ * The colors the bindings of the layers `nodeIds` names last applied, captured when they are
114
+ * copied, so a paste counts what the copied paints were applied from, not what the source has now.
115
+ */
116
+ export function captureAppliedBoundPaints(
117
+ graph: SceneGraph,
118
+ nodeIds: Iterable<string>
119
+ ): AppliedBoundPaints {
120
+ const captured = new Map<string, ReadonlyMap<string, AppliedColor>>()
121
+ for (const id of nodeIds) {
122
+ const node = graph.nodes.get(id)
123
+ const entries = node && appliedColors.get(node)
124
+ if (entries) captured.set(id, new Map(entries))
125
+ }
126
+ return captured
127
+ }
128
+
129
+ /**
130
+ * Pasted layers keep their paints, edits included, as Figma's copies do. One copied in this
131
+ * document takes the colors its source's bindings had last applied when it was copied, so a paste
132
+ * into another mode refreshes them as a move there would; one from elsewhere counts the colors
133
+ * its variables give it now as applied, as a loaded document does. `copies` maps each copied
134
+ * layer's id to its copy's.
135
+ */
136
+ export function keepPastedBoundPaints(
137
+ graph: SceneGraph,
138
+ copies: ReadonlyMap<string, string>,
139
+ applied: AppliedBoundPaints
140
+ ): void {
141
+ for (const [sourceId, copyId] of copies) {
142
+ const copy = graph.nodes.get(copyId)
143
+ if (!copy || !hasBoundPaints(copy)) continue
144
+ const entries = applied.get(sourceId)
145
+ if (!entries) {
146
+ recordWrittenBoundPaints(graph, copy, { fills: copy.fills, strokes: copy.strokes })
147
+ continue
148
+ }
149
+ appliedColors.set(copy, new Map(entries))
150
+ refresh(graph, copy, NONE_FORCED)
151
+ }
152
+ }
153
+
154
+ function recordApplied(
155
+ node: SceneNode,
156
+ bound: BoundPaint,
157
+ color: Color,
158
+ modes: Readonly<Record<string, string | null>>
159
+ ): void {
82
160
  let entries = appliedColors.get(node)
83
161
  if (!entries) {
84
162
  entries = new Map()
85
163
  appliedColors.set(node, entries)
86
164
  }
87
- entries.set(bound.field, { variableId: bound.variableId, color: { ...color } })
165
+ entries.set(bound.field, { variableId: bound.variableId, color: { ...color }, modes })
88
166
  }
89
167
 
90
168
  /** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */
@@ -143,9 +221,10 @@ function refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) =>
143
221
  const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
144
222
  const paint = node[bound.list].at(bound.index)
145
223
  if (!color || !paint) continue
146
- const last = lastApplied(node, bound, paint)
147
- recordApplied(node, bound, color)
148
- if ((!forced(bound.field) && sameColor(last, color)) || showsColor(paint, color)) continue
224
+ const modes = explicitModes(graph, node, bound.variableId)
225
+ const stale = staleApplied(node, bound, paint, color, modes)
226
+ recordApplied(node, bound, color, modes)
227
+ if ((!forced(bound.field) && !stale) || showsColor(paint, color)) continue
149
228
  if (bound.list === 'fills') {
150
229
  fills ??= copyFills(node.fills)
151
230
  applyColor(fills[bound.index], color)
@@ -204,15 +283,15 @@ export function recordWrittenBoundPaints(
204
283
  for (const bound of boundPaints(node)) {
205
284
  if (!written[bound.list]) continue
206
285
  const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
207
- if (color) recordApplied(node, bound, color)
286
+ if (color) recordApplied(node, bound, color, explicitModes(graph, node, bound.variableId))
208
287
  }
209
288
  }
210
289
 
211
290
  /**
212
291
  * Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose
213
- * mode the move changed, as Figma does when a layer enters a frame or page in another mode. A
214
- * move between parents in the same modes, or of layers that bind no paint, costs no more than
215
- * comparing those modes.
292
+ * mode set the move changed, as Figma does when a layer enters a frame or page that sets another
293
+ * mode, or one where none was. A move between parents setting the same modes, or of layers that
294
+ * bind no paint, costs no more than comparing those modes.
216
295
  */
217
296
  export function refreshMovedBoundPaints(
218
297
  graph: SceneGraph,
@@ -227,7 +306,8 @@ export function refreshMovedBoundPaints(
227
306
  [...graph.variableCollections.keys()].filter(
228
307
  (id) =>
229
308
  !previous ||
230
- getNodeVariableModeId(graph, previous, id) !== getNodeVariableModeId(graph, parentId, id)
309
+ getNodeExplicitVariableModeId(graph, previous, id) !==
310
+ getNodeExplicitVariableModeId(graph, parentId, id)
231
311
  )
232
312
  )
233
313
  if (changed.size === 0) return
@@ -289,8 +369,9 @@ export function keepCopyBoundPaints(
289
369
  }
290
370
  const color = graph.resolveColorVariableForNode(target.id, variableId)
291
371
  if (!color) return
292
- if (!keeps || !sameColor(lastApplied(target, bound, own), color)) applyColor(paint, color)
293
- recordApplied(target, bound, color)
372
+ const modes = explicitModes(graph, target, variableId)
373
+ if (!keeps || staleApplied(target, bound, own, color, modes)) applyColor(paint, color)
374
+ recordApplied(target, bound, color, modes)
294
375
  })
295
376
  }
296
377
  }
@@ -1,3 +1,4 @@
1
+ import { appendAll } from '../arrays'
1
2
  import type { SceneGraph } from '../graph'
2
3
  import type { Color } from '../primitives'
3
4
  import type { Variable, VariableLiteral, VariableValue } from '../types'
@@ -17,13 +18,24 @@ export function getNodeVariableModeId(
17
18
  nodeId: string,
18
19
  collectionId: string
19
20
  ): string {
21
+ return (
22
+ getNodeExplicitVariableModeId(graph, nodeId, collectionId) ?? defaultModeId(graph, collectionId)
23
+ )
24
+ }
25
+
26
+ /** The nearest mode set on a node or an ancestor for a collection, or null where none is. */
27
+ export function getNodeExplicitVariableModeId(
28
+ graph: SceneGraph,
29
+ nodeId: string,
30
+ collectionId: string
31
+ ): string | null {
20
32
  let node = graph.nodes.get(nodeId)
21
33
  while (node) {
22
34
  const modeId = node.variableModes[collectionId]
23
35
  if (modeId) return modeId
24
36
  node = node.parentId ? graph.nodes.get(node.parentId) : undefined
25
37
  }
26
- return defaultModeId(graph, collectionId)
38
+ return null
27
39
  }
28
40
 
29
41
  /**
@@ -44,7 +56,7 @@ export function collectionsResolving(
44
56
  if (!variable) continue
45
57
  collections.add(variable.collectionId)
46
58
  for (const value of Object.values(variable.valuesByMode))
47
- pending.push(...variableValueReferences(value))
59
+ appendAll(pending, variableValueReferences(value))
48
60
  }
49
61
  return collections
50
62
  }
@@ -9,7 +9,7 @@ export function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneN
9
9
  const node = graph.getNode(id)
10
10
  if (!node) continue
11
11
  nodes.push(node)
12
- pending.push(...node.childIds)
12
+ for (const childId of node.childIds) pending.push(childId)
13
13
  }
14
14
  return nodes
15
15
  }
@@ -3,15 +3,10 @@ import { isEqual } from 'es-toolkit/predicate'
3
3
  import { cloneVectorNetwork, copyVectorRegion } from './copy'
4
4
  import type { Mat3 } from './matrix'
5
5
  import type { VectorPathWindingRule } from './parse-path'
6
+ import { addOpenSegmentsToPath, addRegionToPath } from './paths/network'
7
+ import { svgPathSink } from './paths/sink'
6
8
  import type { Vector } from './primitives'
7
- import type {
8
- Fill,
9
- GeometryPath,
10
- SceneNode,
11
- VectorNetwork,
12
- VectorSegment,
13
- VectorVertex
14
- } from './types'
9
+ import type { Fill, GeometryPath, SceneNode, VectorNetwork, VectorSegment } from './types'
15
10
 
16
11
  /** Concatenate vector networks while preserving independent geometry. */
17
12
  export function mergeVectorNetworks(networks: readonly VectorNetwork[]): VectorNetwork {
@@ -344,137 +339,19 @@ function coordinate(value: number, decimals: number | null): number {
344
339
  return Math.round(value * factor) / factor
345
340
  }
346
341
 
347
- function segmentToSVG(
348
- seg: VectorSegment,
349
- vertices: VectorVertex[],
350
- forward: boolean,
351
- decimals: number | null
352
- ): string {
353
- const start = forward ? vertices[seg.start] : vertices[seg.end]
354
- const end = forward ? vertices[seg.end] : vertices[seg.start]
355
- // Tangents are relative to their own vertex (`segmentToAbsolute`), so tracing a segment end to
356
- // start swaps them but keeps their sign: each control point stays beside its vertex.
357
- const ts = forward ? seg.tangentStart : seg.tangentEnd
358
- const te = forward ? seg.tangentEnd : seg.tangentStart
359
-
360
- const isStraight =
361
- Math.abs(ts.x) < 0.001 &&
362
- Math.abs(ts.y) < 0.001 &&
363
- Math.abs(te.x) < 0.001 &&
364
- Math.abs(te.y) < 0.001
365
-
366
- if (isStraight) {
367
- return `L${coordinate(end.x, decimals)} ${coordinate(end.y, decimals)}`
368
- }
369
-
370
- const cp1x = coordinate(start.x + ts.x, decimals)
371
- const cp1y = coordinate(start.y + ts.y, decimals)
372
- const cp2x = coordinate(end.x + te.x, decimals)
373
- const cp2y = coordinate(end.y + te.y, decimals)
374
- return `C${cp1x} ${cp1y} ${cp2x} ${cp2y} ${coordinate(end.x, decimals)} ${coordinate(end.y, decimals)}`
375
- }
376
-
377
- function traceOrderedSegments(
378
- segmentIndices: readonly number[],
379
- segments: VectorSegment[],
380
- vertices: VectorVertex[],
381
- decimals: number | null
382
- ): string {
383
- if (segmentIndices.length === 0) return ''
384
- const first = segments[segmentIndices[0]]
385
- const second = segmentIndices.length > 1 ? segments[segmentIndices[1]] : null
386
- // Start at the first segment endpoint that does not connect to the second segment.
387
- const secondConnectsStart = second?.start === first.start || second?.end === first.start
388
- const secondConnectsEnd = second?.start === first.end || second?.end === first.end
389
- const firstForward = !second || secondConnectsEnd || !secondConnectsStart
390
- const startIndex = firstForward ? first.start : first.end
391
- let currentIndex = startIndex
392
- const parts = [
393
- `M${coordinate(vertices[startIndex].x, decimals)} ${coordinate(vertices[startIndex].y, decimals)}`
394
- ]
395
-
396
- for (const segmentIndex of segmentIndices) {
397
- const segment = segments[segmentIndex]
398
- const isConnected = segment.start === currentIndex || segment.end === currentIndex
399
- const forward = segment.start === currentIndex || !isConnected
400
- if (!isConnected) {
401
- const segmentStart = forward ? segment.start : segment.end
402
- parts.push(
403
- `M${coordinate(vertices[segmentStart].x, decimals)} ${coordinate(vertices[segmentStart].y, decimals)}`
404
- )
405
- }
406
- parts.push(segmentToSVG(segment, vertices, forward, decimals))
407
- currentIndex = forward ? segment.end : segment.start
408
- }
409
- if (parts.length === segmentIndices.length + 1 && currentIndex === startIndex) parts.push('Z')
410
- return parts.join('')
411
- }
412
-
413
- type SegmentAdjacency = ReadonlyMap<number, readonly number[]>
414
-
415
- function buildSegmentAdjacency(segments: VectorSegment[]): SegmentAdjacency {
416
- const adjacency = new Map<number, number[]>()
417
- for (let index = 0; index < segments.length; index++) {
418
- const segment = segments[index]
419
- for (const vertexIndex of [segment.start, segment.end]) {
420
- const incidentSegments = adjacency.get(vertexIndex)
421
- if (incidentSegments) incidentSegments.push(index)
422
- else adjacency.set(vertexIndex, [index])
423
- }
424
- }
425
- return adjacency
426
- }
427
-
428
- function findConnectedSegment(
429
- remaining: ReadonlySet<number>,
430
- adjacency: SegmentAdjacency,
431
- vertexIndex: number
432
- ): number | undefined {
433
- return adjacency.get(vertexIndex)?.find((index) => remaining.has(index))
434
- }
435
-
436
- function extendSegmentChain(
437
- chain: number[],
438
- startVertex: number,
439
- remaining: Set<number>,
440
- segments: VectorSegment[],
441
- adjacency: SegmentAdjacency
442
- ): void {
443
- let currentIndex = startVertex
444
- let nextIndex = findConnectedSegment(remaining, adjacency, currentIndex)
445
- while (nextIndex !== undefined) {
446
- chain.push(nextIndex)
447
- remaining.delete(nextIndex)
448
- const segment = segments[nextIndex]
449
- currentIndex = segment.start === currentIndex ? segment.end : segment.start
450
- nextIndex = findConnectedSegment(remaining, adjacency, currentIndex)
451
- }
342
+ /** Coordinates written at `decimals`, or as they are for `null`. */
343
+ function coordinateFormat(decimals: number | null): (value: number) => number {
344
+ return (value) => coordinate(value, decimals)
452
345
  }
453
346
 
347
+ /** A network's segments as one open path, chained as the canvas draws them. */
454
348
  function unfilledSegmentsToPath(network: VectorNetwork, decimals: number | null): string {
455
- const { vertices, segments } = network
456
- const remaining = new Set(segments.map((_, index) => index))
457
- const adjacency = buildSegmentAdjacency(segments)
458
349
  const parts: string[] = []
459
-
460
- while (remaining.size > 0) {
461
- const firstIndex = remaining.values().next().value
462
- if (firstIndex === undefined) break
463
- remaining.delete(firstIndex)
464
- const first = segments[firstIndex]
465
-
466
- const backward: number[] = []
467
- extendSegmentChain(backward, first.start, remaining, segments, adjacency)
468
- backward.reverse()
469
-
470
- const forward: number[] = []
471
- extendSegmentChain(forward, first.end, remaining, segments, adjacency)
472
-
473
- parts.push(
474
- traceOrderedSegments([...backward, firstIndex, ...forward], segments, vertices, decimals)
475
- )
476
- }
477
-
350
+ addOpenSegmentsToPath(
351
+ svgPathSink(parts, coordinateFormat(decimals)),
352
+ network.segments,
353
+ network.vertices
354
+ )
478
355
  return parts.join('')
479
356
  }
480
357
 
@@ -500,9 +377,11 @@ export function vectorNetworkToSVGPaths(
500
377
  /** Each region of a vector network as path data, its loops traced in turn. */
501
378
  export function regionSVGPaths(network: VectorNetwork, decimals: number | null = 2): string[] {
502
379
  const { vertices, segments, regions } = network
503
- return regions.map((region) =>
504
- region.loops.map((loop) => traceOrderedSegments(loop, segments, vertices, decimals)).join('')
505
- )
380
+ return regions.map((region) => {
381
+ const parts: string[] = []
382
+ addRegionToPath(svgPathSink(parts, coordinateFormat(decimals)), region, segments, vertices)
383
+ return parts.join('')
384
+ })
506
385
  }
507
386
 
508
387
  /** The segments of a vector network that no region encloses. */