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

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 (390) hide show
  1. package/dist/arrow-caps.d.ts.map +1 -1
  2. package/dist/arrow-caps.js +2 -5
  3. package/dist/arrow-caps.js.map +1 -1
  4. package/dist/asset-keys.d.ts +11 -1
  5. package/dist/asset-keys.d.ts.map +1 -1
  6. package/dist/asset-keys.js +16 -1
  7. package/dist/asset-keys.js.map +1 -1
  8. package/dist/behaviours/index.d.ts +2 -2
  9. package/dist/behaviours/index.js +2 -2
  10. package/dist/behaviours/kinds.d.ts +11 -5
  11. package/dist/behaviours/kinds.d.ts.map +1 -1
  12. package/dist/behaviours/kinds.js +76 -75
  13. package/dist/behaviours/kinds.js.map +1 -1
  14. package/dist/behaviours/layers.d.ts.map +1 -1
  15. package/dist/behaviours/layers.js +3 -2
  16. package/dist/behaviours/layers.js.map +1 -1
  17. package/dist/behaviours/model.d.ts +7 -4
  18. package/dist/behaviours/model.d.ts.map +1 -1
  19. package/dist/behaviours/model.js +16 -9
  20. package/dist/behaviours/model.js.map +1 -1
  21. package/dist/behaviours/schema.d.ts +118 -4
  22. package/dist/behaviours/schema.d.ts.map +1 -1
  23. package/dist/behaviours/schema.js +18 -3
  24. package/dist/behaviours/schema.js.map +1 -1
  25. package/dist/behaviours/spec.d.ts.map +1 -1
  26. package/dist/behaviours/spec.js +3 -2
  27. package/dist/behaviours/spec.js.map +1 -1
  28. package/dist/bindings.js +9 -1
  29. package/dist/bindings.js.map +1 -1
  30. package/dist/checkpoint.d.ts +31 -2
  31. package/dist/checkpoint.d.ts.map +1 -1
  32. package/dist/checkpoint.js +211 -10
  33. package/dist/checkpoint.js.map +1 -1
  34. package/dist/components/index.d.ts +3 -3
  35. package/dist/components/index.js +3 -3
  36. package/dist/components/properties.d.ts +18 -1
  37. package/dist/components/properties.d.ts.map +1 -1
  38. package/dist/components/properties.js +66 -3
  39. package/dist/components/properties.js.map +1 -1
  40. package/dist/components/variants.d.ts +16 -10
  41. package/dist/components/variants.d.ts.map +1 -1
  42. package/dist/components/variants.js +39 -13
  43. package/dist/components/variants.js.map +1 -1
  44. package/dist/connectors/hit.d.ts.map +1 -1
  45. package/dist/connectors/hit.js +1 -7
  46. package/dist/connectors/hit.js.map +1 -1
  47. package/dist/connectors/pieces.d.ts.map +1 -1
  48. package/dist/connectors/pieces.js +2 -5
  49. package/dist/connectors/pieces.js.map +1 -1
  50. package/dist/constants.d.ts +6 -1
  51. package/dist/constants.d.ts.map +1 -1
  52. package/dist/constants.js +17 -1
  53. package/dist/constants.js.map +1 -1
  54. package/dist/constraints.d.ts +25 -0
  55. package/dist/constraints.d.ts.map +1 -0
  56. package/dist/constraints.js +51 -0
  57. package/dist/constraints.js.map +1 -0
  58. package/dist/coordinate.d.ts +64 -1
  59. package/dist/coordinate.d.ts.map +1 -1
  60. package/dist/coordinate.js +217 -1
  61. package/dist/coordinate.js.map +1 -1
  62. package/dist/copy.d.ts.map +1 -1
  63. package/dist/copy.js +17 -16
  64. package/dist/copy.js.map +1 -1
  65. package/dist/css/blend.d.ts +11 -0
  66. package/dist/css/blend.d.ts.map +1 -0
  67. package/dist/css/blend.js +30 -0
  68. package/dist/css/blend.js.map +1 -0
  69. package/dist/css/index.d.ts +4 -2
  70. package/dist/css/index.js +4 -2
  71. package/dist/css/text.d.ts +28 -1
  72. package/dist/css/text.d.ts.map +1 -1
  73. package/dist/css/text.js +84 -17
  74. package/dist/css/text.js.map +1 -1
  75. package/dist/css/transform.d.ts +34 -0
  76. package/dist/css/transform.d.ts.map +1 -0
  77. package/dist/css/transform.js +320 -0
  78. package/dist/css/transform.js.map +1 -0
  79. package/dist/css/values.d.ts.map +1 -1
  80. package/dist/css/values.js +11 -1
  81. package/dist/css/values.js.map +1 -1
  82. package/dist/cubic.d.ts +7 -0
  83. package/dist/cubic.d.ts.map +1 -0
  84. package/dist/cubic.js +17 -0
  85. package/dist/cubic.js.map +1 -0
  86. package/dist/export-format.d.ts +14 -1
  87. package/dist/export-format.d.ts.map +1 -1
  88. package/dist/export-format.js +26 -1
  89. package/dist/export-format.js.map +1 -1
  90. package/dist/fields/text.d.ts +2 -2
  91. package/dist/fields/text.d.ts.map +1 -1
  92. package/dist/fields/text.js +2 -1
  93. package/dist/fields/text.js.map +1 -1
  94. package/dist/figjam/index.d.ts +2 -2
  95. package/dist/figjam/index.js +2 -2
  96. package/dist/figjam/palette.d.ts +13 -2
  97. package/dist/figjam/palette.d.ts.map +1 -1
  98. package/dist/figjam/palette.js +30 -2
  99. package/dist/figjam/palette.js.map +1 -1
  100. package/dist/generated/index.d.ts +2 -2
  101. package/dist/generated/index.js +2 -2
  102. package/dist/generated/shape-with-text.d.ts +7 -2
  103. package/dist/generated/shape-with-text.d.ts.map +1 -1
  104. package/dist/generated/shape-with-text.js +20 -3
  105. package/dist/generated/shape-with-text.js.map +1 -1
  106. package/dist/geometry.d.ts +37 -46
  107. package/dist/geometry.d.ts.map +1 -1
  108. package/dist/geometry.js +80 -137
  109. package/dist/geometry.js.map +1 -1
  110. package/dist/graph.d.ts +49 -12
  111. package/dist/graph.d.ts.map +1 -1
  112. package/dist/graph.js +125 -27
  113. package/dist/graph.js.map +1 -1
  114. package/dist/group-bounds.d.ts +14 -4
  115. package/dist/group-bounds.d.ts.map +1 -1
  116. package/dist/group-bounds.js +76 -11
  117. package/dist/group-bounds.js.map +1 -1
  118. package/dist/guides.d.ts +3 -1
  119. package/dist/guides.d.ts.map +1 -1
  120. package/dist/guides.js +5 -1
  121. package/dist/guides.js.map +1 -1
  122. package/dist/hit-test.d.ts +5 -0
  123. package/dist/hit-test.d.ts.map +1 -1
  124. package/dist/hit-test.js +57 -38
  125. package/dist/hit-test.js.map +1 -1
  126. package/dist/images.d.ts +27 -3
  127. package/dist/images.d.ts.map +1 -1
  128. package/dist/images.js +57 -3
  129. package/dist/images.js.map +1 -1
  130. package/dist/index.d.ts +2 -2
  131. package/dist/instances/fields.d.ts +2 -2
  132. package/dist/instances/fields.d.ts.map +1 -1
  133. package/dist/instances/fields.js +8 -0
  134. package/dist/instances/fields.js.map +1 -1
  135. package/dist/instances/index.d.ts +1 -3
  136. package/dist/instances/index.d.ts.map +1 -1
  137. package/dist/instances/index.js +39 -30
  138. package/dist/instances/index.js.map +1 -1
  139. package/dist/instances/kept-overrides.js +204 -0
  140. package/dist/instances/kept-overrides.js.map +1 -0
  141. package/dist/instances/main-component.d.ts +3 -1
  142. package/dist/instances/main-component.d.ts.map +1 -1
  143. package/dist/instances/main-component.js +10 -2
  144. package/dist/instances/main-component.js.map +1 -1
  145. package/dist/instances/overrides.d.ts +4 -1
  146. package/dist/instances/overrides.d.ts.map +1 -1
  147. package/dist/instances/overrides.js +18 -2
  148. package/dist/instances/overrides.js.map +1 -1
  149. package/dist/instances/placement.js +55 -12
  150. package/dist/instances/placement.js.map +1 -1
  151. package/dist/instances/swap.js +291 -0
  152. package/dist/instances/swap.js.map +1 -0
  153. package/dist/instances/sync.js +112 -39
  154. package/dist/instances/sync.js.map +1 -1
  155. package/dist/node-defaults.d.ts.map +1 -1
  156. package/dist/node-defaults.js +2 -1
  157. package/dist/node-defaults.js.map +1 -1
  158. package/dist/order-keys.d.ts +3 -1
  159. package/dist/order-keys.d.ts.map +1 -1
  160. package/dist/order-keys.js +6 -1
  161. package/dist/order-keys.js.map +1 -1
  162. package/dist/outline/flatten.d.ts +15 -0
  163. package/dist/outline/flatten.d.ts.map +1 -0
  164. package/dist/outline/flatten.js +189 -0
  165. package/dist/outline/flatten.js.map +1 -0
  166. package/dist/outline/heads.js +34 -0
  167. package/dist/outline/heads.js.map +1 -0
  168. package/dist/outline/hit.d.ts +21 -0
  169. package/dist/outline/hit.d.ts.map +1 -0
  170. package/dist/outline/hit.js +276 -0
  171. package/dist/outline/hit.js.map +1 -0
  172. package/dist/outline/index.d.ts +5 -0
  173. package/dist/outline/index.js +5 -0
  174. package/dist/outline/point.d.ts +16 -0
  175. package/dist/outline/point.d.ts.map +1 -0
  176. package/dist/outline/point.js +42 -0
  177. package/dist/outline/point.js.map +1 -0
  178. package/dist/outline/shapes.d.ts +35 -0
  179. package/dist/outline/shapes.d.ts.map +1 -0
  180. package/dist/outline/shapes.js +181 -0
  181. package/dist/outline/shapes.js.map +1 -0
  182. package/dist/plugin-data/fields.d.ts +4 -1
  183. package/dist/plugin-data/fields.d.ts.map +1 -1
  184. package/dist/plugin-data/fields.js +21 -4
  185. package/dist/plugin-data/fields.js.map +1 -1
  186. package/dist/resize.d.ts +22 -11
  187. package/dist/resize.d.ts.map +1 -1
  188. package/dist/resize.js +227 -93
  189. package/dist/resize.js.map +1 -1
  190. package/dist/scaling/index.d.ts +3 -2
  191. package/dist/scaling/index.js +3 -2
  192. package/dist/scaling/node.d.ts.map +1 -1
  193. package/dist/scaling/node.js +5 -1
  194. package/dist/scaling/node.js.map +1 -1
  195. package/dist/scaling/snapshot.d.ts +25 -0
  196. package/dist/scaling/snapshot.d.ts.map +1 -0
  197. package/dist/scaling/snapshot.js +39 -0
  198. package/dist/scaling/snapshot.js.map +1 -0
  199. package/dist/scaling/tree.d.ts +24 -2
  200. package/dist/scaling/tree.d.ts.map +1 -1
  201. package/dist/scaling/tree.js +38 -15
  202. package/dist/scaling/tree.js.map +1 -1
  203. package/dist/shaders/model.d.ts.map +1 -1
  204. package/dist/shaders/model.js +2 -1
  205. package/dist/shaders/model.js.map +1 -1
  206. package/dist/shared-styles.d.ts +16 -1
  207. package/dist/shared-styles.d.ts.map +1 -1
  208. package/dist/shared-styles.js +69 -2
  209. package/dist/shared-styles.js.map +1 -1
  210. package/dist/slots/authoring.d.ts +3 -2
  211. package/dist/slots/authoring.d.ts.map +1 -1
  212. package/dist/slots/authoring.js +5 -3
  213. package/dist/slots/authoring.js.map +1 -1
  214. package/dist/slots/content.d.ts.map +1 -1
  215. package/dist/slots/content.js +6 -1
  216. package/dist/slots/content.js.map +1 -1
  217. package/dist/slots/frames.d.ts +2 -0
  218. package/dist/slots/frames.d.ts.map +1 -1
  219. package/dist/slots/frames.js +7 -2
  220. package/dist/slots/frames.js.map +1 -1
  221. package/dist/text/direction.d.ts +3 -1
  222. package/dist/text/direction.d.ts.map +1 -1
  223. package/dist/text/direction.js +5 -1
  224. package/dist/text/direction.js.map +1 -1
  225. package/dist/text/index.d.ts +2 -2
  226. package/dist/text/index.js +3 -3
  227. package/dist/text/lengths.d.ts +6 -1
  228. package/dist/text/lengths.d.ts.map +1 -1
  229. package/dist/text/lengths.js +10 -1
  230. package/dist/text/lengths.js.map +1 -1
  231. package/dist/text/overflow.d.ts +2 -2
  232. package/dist/text/overflow.js.map +1 -1
  233. package/dist/text/picture.d.ts +1 -2
  234. package/dist/text/picture.d.ts.map +1 -1
  235. package/dist/text/picture.js +1 -4
  236. package/dist/text/picture.js.map +1 -1
  237. package/dist/transfer/apply.d.ts.map +1 -1
  238. package/dist/transfer/apply.js +1 -6
  239. package/dist/transfer/apply.js.map +1 -1
  240. package/dist/transfer/copied.d.ts +35 -3
  241. package/dist/transfer/copied.d.ts.map +1 -1
  242. package/dist/transfer/copied.js +44 -13
  243. package/dist/transfer/copied.js.map +1 -1
  244. package/dist/transfer/dependencies.d.ts +38 -0
  245. package/dist/transfer/dependencies.d.ts.map +1 -0
  246. package/dist/transfer/dependencies.js +100 -0
  247. package/dist/transfer/dependencies.js.map +1 -0
  248. package/dist/transfer/history.js +0 -1
  249. package/dist/transfer/history.js.map +1 -1
  250. package/dist/transfer/index.d.ts +5 -3
  251. package/dist/transfer/index.js +5 -3
  252. package/dist/transfer/nodes.d.ts.map +1 -1
  253. package/dist/transfer/nodes.js +16 -19
  254. package/dist/transfer/nodes.js.map +1 -1
  255. package/dist/transfer/plan.d.ts +0 -1
  256. package/dist/transfer/plan.js +3 -5
  257. package/dist/transfer/plan.js.map +1 -1
  258. package/dist/transfer/remove.js +5 -2
  259. package/dist/transfer/remove.js.map +1 -1
  260. package/dist/transfer/resources.d.ts +21 -0
  261. package/dist/transfer/resources.d.ts.map +1 -0
  262. package/dist/transfer/resources.js +38 -0
  263. package/dist/transfer/resources.js.map +1 -0
  264. package/dist/transfer/variables.d.ts +8 -1
  265. package/dist/transfer/variables.d.ts.map +1 -1
  266. package/dist/transfer/variables.js +24 -1
  267. package/dist/transfer/variables.js.map +1 -1
  268. package/dist/types.d.ts +73 -2
  269. package/dist/types.d.ts.map +1 -1
  270. package/dist/types.js.map +1 -1
  271. package/dist/undo.d.ts +28 -3
  272. package/dist/undo.d.ts.map +1 -1
  273. package/dist/undo.js +24 -7
  274. package/dist/undo.js.map +1 -1
  275. package/dist/variables/bindings.d.ts +37 -11
  276. package/dist/variables/bindings.d.ts.map +1 -1
  277. package/dist/variables/bindings.js +122 -74
  278. package/dist/variables/bindings.js.map +1 -1
  279. package/dist/variables/fields.d.ts +19 -1
  280. package/dist/variables/fields.d.ts.map +1 -1
  281. package/dist/variables/fields.js +36 -1
  282. package/dist/variables/fields.js.map +1 -1
  283. package/dist/variables/formula/check.d.ts +1 -1
  284. package/dist/variables/formula/index.d.ts +2 -2
  285. package/dist/variables/formula/index.js +1 -1
  286. package/dist/variables/formula/index.js.map +1 -1
  287. package/dist/variables/formula/print.d.ts +1 -1
  288. package/dist/variables/formula/signatures.js +1 -1
  289. package/dist/variables/index.d.ts +7 -7
  290. package/dist/variables/index.d.ts.map +1 -1
  291. package/dist/variables/index.js +57 -26
  292. package/dist/variables/index.js.map +1 -1
  293. package/dist/variables/paints.d.ts +57 -0
  294. package/dist/variables/paints.d.ts.map +1 -0
  295. package/dist/variables/paints.js +231 -0
  296. package/dist/variables/paints.js.map +1 -0
  297. package/dist/variables/resolution.d.ts +14 -11
  298. package/dist/variables/resolution.d.ts.map +1 -1
  299. package/dist/variables/resolution.js +36 -17
  300. package/dist/variables/resolution.js.map +1 -1
  301. package/dist/variables/subtree.js +17 -0
  302. package/dist/variables/subtree.js.map +1 -0
  303. package/dist/vector-network.d.ts.map +1 -1
  304. package/dist/vector-network.js +2 -8
  305. package/dist/vector-network.js.map +1 -1
  306. package/package.json +14 -1
  307. package/src/arrow-caps.ts +2 -5
  308. package/src/asset-keys.ts +20 -0
  309. package/src/behaviours/kinds.ts +51 -34
  310. package/src/behaviours/layers.ts +4 -2
  311. package/src/behaviours/model.ts +22 -11
  312. package/src/behaviours/schema.ts +20 -4
  313. package/src/behaviours/spec.ts +8 -2
  314. package/src/bindings.ts +11 -2
  315. package/src/checkpoint.ts +319 -17
  316. package/src/components/properties.ts +101 -2
  317. package/src/components/variants.ts +51 -30
  318. package/src/connectors/hit.ts +1 -12
  319. package/src/connectors/pieces.ts +2 -5
  320. package/src/constants.ts +17 -0
  321. package/src/constraints.ts +60 -0
  322. package/src/coordinate.ts +220 -0
  323. package/src/copy.ts +18 -19
  324. package/src/css/blend.ts +27 -0
  325. package/src/css/index.ts +18 -1
  326. package/src/css/text.ts +85 -17
  327. package/src/css/transform.ts +266 -0
  328. package/src/css/values.ts +8 -0
  329. package/src/cubic.ts +14 -0
  330. package/src/export-format.ts +29 -0
  331. package/src/fields/text.ts +3 -1
  332. package/src/figjam/palette.ts +29 -1
  333. package/src/generated/shape-with-text.ts +28 -2
  334. package/src/geometry.ts +97 -206
  335. package/src/graph.ts +155 -45
  336. package/src/group-bounds.ts +84 -17
  337. package/src/guides.ts +5 -0
  338. package/src/hit-test.ts +93 -64
  339. package/src/images.ts +71 -3
  340. package/src/instances/fields.ts +8 -0
  341. package/src/instances/index.ts +62 -31
  342. package/src/instances/kept-overrides.ts +280 -0
  343. package/src/instances/main-component.ts +12 -0
  344. package/src/instances/overrides.ts +27 -1
  345. package/src/instances/placement.ts +63 -10
  346. package/src/instances/swap.ts +456 -0
  347. package/src/instances/sync.ts +176 -50
  348. package/src/node-defaults.ts +3 -1
  349. package/src/order-keys.ts +6 -0
  350. package/src/outline/flatten.ts +190 -0
  351. package/src/outline/heads.ts +52 -0
  352. package/src/outline/hit.ts +369 -0
  353. package/src/outline/index.ts +4 -0
  354. package/src/outline/point.ts +59 -0
  355. package/src/outline/shapes.ts +207 -0
  356. package/src/plugin-data/fields.ts +21 -2
  357. package/src/resize.ts +267 -119
  358. package/src/scaling/index.ts +9 -1
  359. package/src/scaling/node.ts +9 -1
  360. package/src/scaling/snapshot.ts +51 -0
  361. package/src/scaling/tree.ts +63 -19
  362. package/src/shaders/model.ts +2 -3
  363. package/src/shared-styles.ts +90 -1
  364. package/src/slots/authoring.ts +5 -3
  365. package/src/slots/content.ts +6 -1
  366. package/src/slots/frames.ts +9 -7
  367. package/src/text/direction.ts +5 -0
  368. package/src/text/lengths.ts +10 -0
  369. package/src/text/overflow.ts +2 -2
  370. package/src/text/picture.ts +0 -4
  371. package/src/transfer/apply.ts +1 -13
  372. package/src/transfer/copied.ts +83 -15
  373. package/src/transfer/dependencies.ts +126 -0
  374. package/src/transfer/history.ts +0 -6
  375. package/src/transfer/index.ts +20 -2
  376. package/src/transfer/nodes.ts +17 -31
  377. package/src/transfer/plan.ts +3 -11
  378. package/src/transfer/remove.ts +5 -2
  379. package/src/transfer/resources.ts +54 -0
  380. package/src/transfer/variables.ts +34 -0
  381. package/src/types.ts +83 -1
  382. package/src/undo.ts +48 -10
  383. package/src/variables/bindings.ts +152 -88
  384. package/src/variables/fields.ts +46 -1
  385. package/src/variables/formula/index.ts +3 -1
  386. package/src/variables/index.ts +90 -39
  387. package/src/variables/paints.ts +296 -0
  388. package/src/variables/resolution.ts +39 -28
  389. package/src/variables/subtree.ts +15 -0
  390. package/src/vector-network.ts +4 -2
@@ -0,0 +1,296 @@
1
+ import { copyFills, copyStrokes } from '../copy'
2
+ import type { SceneGraph } from '../graph'
3
+ import { hasInstanceOverride } from '../instances/overrides'
4
+ import type { Color } from '../primitives'
5
+ import type { Fill, SceneNode, Stroke } from '../types'
6
+ import { paintBindingTarget, type PaintBindingList } from './fields'
7
+ import { collectionsResolving, getNodeVariableModeId } from './resolution'
8
+ import { subtreeNodes } from './subtree'
9
+
10
+ /**
11
+ * Figma keeps a bound paint's color and opacity as a cache of its variable: it draws and reads
12
+ * 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.
17
+ */
18
+ interface AppliedColor {
19
+ readonly variableId: string
20
+ readonly color: Color
21
+ }
22
+
23
+ const appliedColors = new WeakMap<SceneNode, Map<string, AppliedColor>>()
24
+
25
+ const PAINT_LISTS = ['fills', 'strokes'] as const
26
+
27
+ interface BoundPaint {
28
+ readonly field: string
29
+ readonly list: PaintBindingList
30
+ readonly index: number
31
+ readonly variableId: string
32
+ }
33
+
34
+ /** The paints of `node` whose color is bound to a variable, each with a paint there to color. */
35
+ function boundPaints(node: SceneNode): BoundPaint[] {
36
+ const paints: BoundPaint[] = []
37
+ for (const [field, variableId] of Object.entries(node.boundVariables)) {
38
+ const target = paintBindingTarget(field)
39
+ if (target && node[target.list].at(target.index)) paints.push({ field, ...target, variableId })
40
+ }
41
+ return paints
42
+ }
43
+
44
+ /** Whether a layer has a paint whose color is bound to a variable. */
45
+ export function hasBoundPaints(node: SceneNode): boolean {
46
+ for (const field in node.boundVariables) if (paintBindingTarget(field)) return true
47
+ return false
48
+ }
49
+
50
+ /** A paint's color with its opacity as alpha, as a variable's color carries it. */
51
+ function paintColor(paint: Fill): Color {
52
+ return { r: paint.color.r, g: paint.color.g, b: paint.color.b, a: paint.opacity }
53
+ }
54
+
55
+ /** Saved colors are single precision, so a color read back from a file still matches its source. */
56
+ const COLOR_TOLERANCE = 1e-6
57
+
58
+ function sameColor(a: Color, b: Color): boolean {
59
+ return (
60
+ Math.abs(a.r - b.r) <= COLOR_TOLERANCE &&
61
+ Math.abs(a.g - b.g) <= COLOR_TOLERANCE &&
62
+ Math.abs(a.b - b.b) <= COLOR_TOLERANCE &&
63
+ Math.abs(a.a - b.a) <= COLOR_TOLERANCE
64
+ )
65
+ }
66
+
67
+ function lastApplied(node: SceneNode, bound: BoundPaint, paint: Fill): Color {
68
+ const entry = appliedColors.get(node)?.get(bound.field)
69
+ return entry?.variableId === bound.variableId ? entry.color : paintColor(paint)
70
+ }
71
+
72
+ /**
73
+ * A layer copied from another keeps the colors its bindings last applied there, so the copy's
74
+ * paints, edits included, stay as they are until their variables' colors for it change.
75
+ */
76
+ export function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void {
77
+ const entries = appliedColors.get(source)
78
+ if (entries) appliedColors.set(copy, new Map(entries))
79
+ }
80
+
81
+ function recordApplied(node: SceneNode, bound: BoundPaint, color: Color): void {
82
+ let entries = appliedColors.get(node)
83
+ if (!entries) {
84
+ entries = new Map()
85
+ appliedColors.set(node, entries)
86
+ }
87
+ entries.set(bound.field, { variableId: bound.variableId, color: { ...color } })
88
+ }
89
+
90
+ /** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */
91
+ function applyColor(paint: Fill, color: Color): void {
92
+ paint.color = { ...color, a: 1 }
93
+ paint.opacity = color.a
94
+ }
95
+
96
+ /** Whether `paint` already shows `color` as a bound paint stores it. */
97
+ function showsColor(paint: Fill, color: Color): boolean {
98
+ return sameColor(paintColor(paint), color) && paint.color.a === 1
99
+ }
100
+
101
+ type PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>
102
+
103
+ /**
104
+ * Whether `node` is an instance, or a layer an instance copies from its component, with a paint
105
+ * list neither it nor an instance around it overrides: such paints follow the component, and
106
+ * refreshing them is no edit.
107
+ */
108
+ export function copiesComponentPaints(
109
+ graph: SceneGraph,
110
+ node: SceneNode,
111
+ list: PaintBindingList
112
+ ): boolean {
113
+ const walk = { copied: false }
114
+ const overriding = graph.closest(node.id, (current) => {
115
+ if (current.type !== 'INSTANCE') return false
116
+ walk.copied = true
117
+ return hasInstanceOverride(current.instanceOverrides, current.id, node.id, list)
118
+ })
119
+ return walk.copied && !overriding
120
+ }
121
+
122
+ /**
123
+ * Writes refreshed paints. A copy's paints that follow its component are derived values that a
124
+ * save leaves to the component, not edits written over it.
125
+ */
126
+ function writePaints(graph: SceneGraph, node: SceneNode, changes: PaintLists): void {
127
+ const derived = PAINT_LISTS.every(
128
+ (list) => !changes[list] || copiesComponentPaints(graph, node, list)
129
+ )
130
+ const write = () => graph.updateNode(node.id, changes)
131
+ if (derived) graph.applyComponentDerivedDuring(write)
132
+ else write()
133
+ }
134
+
135
+ /**
136
+ * Writes into each bound paint the color its variable gives the layer now, where that differs
137
+ * from the color last applied, or wherever `forced` says, and records what each binding gives.
138
+ */
139
+ function refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) => boolean): void {
140
+ let fills: Fill[] | undefined
141
+ let strokes: Stroke[] | undefined
142
+ for (const bound of boundPaints(node)) {
143
+ const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
144
+ const paint = node[bound.list].at(bound.index)
145
+ 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
149
+ if (bound.list === 'fills') {
150
+ fills ??= copyFills(node.fills)
151
+ applyColor(fills[bound.index], color)
152
+ } else {
153
+ strokes ??= copyStrokes(node.strokes)
154
+ applyColor(strokes[bound.index], color)
155
+ }
156
+ }
157
+ const changes: PaintLists = {}
158
+ if (fills) changes.fills = fills
159
+ if (strokes) changes.strokes = strokes
160
+ if (fills || strokes) writePaints(graph, node, changes)
161
+ }
162
+
163
+ const NONE_FORCED = (): boolean => false
164
+ const ALL_FORCED = (): boolean => true
165
+
166
+ /**
167
+ * Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as
168
+ * opacity, once that color differs from the one last applied, discarding edits made since, as
169
+ * Figma refreshes them when a variable's value or a layer's mode changes.
170
+ */
171
+ export function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void {
172
+ if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED)
173
+ }
174
+
175
+ /** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */
176
+ export function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field: string): void {
177
+ if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field)
178
+ }
179
+
180
+ /**
181
+ * Gives every bound paint in this subtree its variable's color where it sits. A new instance
182
+ * or a layer an instance sync adds is a copy, which shows its variables in its own context rather
183
+ * than whatever its component's paints hold.
184
+ */
185
+ export function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void {
186
+ for (const node of subtreeNodes(graph, [rootId]))
187
+ if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED)
188
+ }
189
+
190
+ /** Refreshes the bound paints in this subtree whose variables now give their layers another color. */
191
+ export function refreshBoundPaintsIn(graph: SceneGraph, rootId: string): void {
192
+ for (const node of subtreeNodes(graph, [rootId])) refreshBoundPaints(graph, node)
193
+ }
194
+
195
+ /**
196
+ * Records, for the paint lists a write sets, the colors their variables give the layer now, so a
197
+ * paint written with another color or opacity keeps it until that color changes.
198
+ */
199
+ export function recordWrittenBoundPaints(
200
+ graph: SceneGraph,
201
+ node: SceneNode,
202
+ written: PaintLists
203
+ ): void {
204
+ for (const bound of boundPaints(node)) {
205
+ if (!written[bound.list]) continue
206
+ const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
207
+ if (color) recordApplied(node, bound, color)
208
+ }
209
+ }
210
+
211
+ /**
212
+ * 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.
216
+ */
217
+ export function refreshMovedBoundPaints(
218
+ graph: SceneGraph,
219
+ nodeId: string,
220
+ previousParentId: string | null | undefined
221
+ ): void {
222
+ if (graph.isApplyingImportedState || graph.variableCollections.size === 0) return
223
+ const parentId = graph.nodes.get(nodeId)?.parentId
224
+ if (!parentId) return
225
+ const previous = previousParentId && graph.nodes.has(previousParentId) ? previousParentId : null
226
+ const changed = new Set(
227
+ [...graph.variableCollections.keys()].filter(
228
+ (id) =>
229
+ !previous ||
230
+ getNodeVariableModeId(graph, previous, id) !== getNodeVariableModeId(graph, parentId, id)
231
+ )
232
+ )
233
+ if (changed.size === 0) return
234
+ const reaches = new Map<string, boolean>()
235
+ const reachesChanged = (variableId: string) => {
236
+ let found = reaches.get(variableId)
237
+ if (found === undefined) {
238
+ found = [...collectionsResolving(graph, [variableId])].some((id) => changed.has(id))
239
+ reaches.set(variableId, found)
240
+ }
241
+ return found
242
+ }
243
+ for (const node of subtreeNodes(graph, [nodeId])) {
244
+ if (boundPaints(node).some((bound) => reachesChanged(bound.variableId)))
245
+ refresh(graph, node, NONE_FORCED)
246
+ }
247
+ }
248
+
249
+ function reboundPaint(
250
+ target: SceneNode,
251
+ bindings: Readonly<Record<string, string>>,
252
+ list: PaintBindingList
253
+ ): boolean {
254
+ return target[list].some((_, index) => {
255
+ const field = `${list}/${index}/color`
256
+ return field in bindings && bindings[field] !== target.boundVariables[field]
257
+ })
258
+ }
259
+
260
+ /**
261
+ * A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit
262
+ * to its paint's color or opacity does not reach them. An instance sync writing a copy's paints
263
+ * keeps the color and opacity of each one still bound to the same variable, and gives it the
264
+ * variable's color for the copy once that differs from the one last applied, or when it binds
265
+ * anew. `updates` holds what the sync writes, whose paints this sets in place.
266
+ */
267
+ export function keepCopyBoundPaints(
268
+ graph: SceneGraph,
269
+ target: SceneNode,
270
+ updates: Partial<SceneNode>
271
+ ): void {
272
+ const bindings = updates.boundVariables ?? target.boundVariables
273
+ for (const list of PAINT_LISTS) {
274
+ // A paint the component binds anew keeps its own paint otherwise, but takes the new color.
275
+ if (!updates[list] && reboundPaint(target, bindings, list))
276
+ Object.assign(updates, {
277
+ [list]: list === 'fills' ? copyFills(target.fills) : copyStrokes(target.strokes)
278
+ })
279
+ updates[list]?.forEach((paint, index) => {
280
+ const field = `${list}/${index}/color`
281
+ const variableId = bindings[field]
282
+ if (!variableId) return
283
+ const bound = { field, list, index, variableId }
284
+ const own = target[list].at(index)
285
+ const keeps = own !== undefined && target.boundVariables[field] === variableId
286
+ if (keeps) {
287
+ paint.color = { ...own.color }
288
+ paint.opacity = own.opacity
289
+ }
290
+ const color = graph.resolveColorVariableForNode(target.id, variableId)
291
+ if (!color) return
292
+ if (!keeps || !sameColor(lastApplied(target, bound, own), color)) applyColor(paint, color)
293
+ recordApplied(target, bound, color)
294
+ })
295
+ }
296
+ }
@@ -2,26 +2,20 @@ import type { SceneGraph } from '../graph'
2
2
  import type { Color } from '../primitives'
3
3
  import type { Variable, VariableLiteral, VariableValue } from '../types'
4
4
  import { MAX_FORMULA_DEPTH, VariableFormulaError } from './formula/tree'
5
- import { resolveVariableValue } from './value'
5
+ import { resolveVariableValue, variableValueReferences } from './value'
6
6
 
7
- export function getActiveModeId(graph: SceneGraph, collectionId: string): string {
8
- const mode = graph.activeMode.get(collectionId)
9
- if (mode) return mode
10
- const collection = graph.variableCollections.get(collectionId)
11
- return collection?.defaultModeId ?? ''
7
+ function defaultModeId(graph: SceneGraph, collectionId: string): string {
8
+ return graph.variableCollections.get(collectionId)?.defaultModeId ?? ''
12
9
  }
13
10
 
14
11
  /**
15
- * What a node without an explicit mode falls back to: the mode the editor shows (`active`), or
16
- * the collection's default (`default`), which is what a saved document means to other tools.
12
+ * The mode a node is in for a collection: the nearest explicit mode on it or an ancestor (the page
13
+ * included), else the collection's default, as Figma resolves it.
17
14
  */
18
- export type VariableModeFallback = 'active' | 'default'
19
-
20
15
  export function getNodeVariableModeId(
21
16
  graph: SceneGraph,
22
17
  nodeId: string,
23
- collectionId: string,
24
- fallback: VariableModeFallback = 'active'
18
+ collectionId: string
25
19
  ): string {
26
20
  let node = graph.nodes.get(nodeId)
27
21
  while (node) {
@@ -29,9 +23,30 @@ export function getNodeVariableModeId(
29
23
  if (modeId) return modeId
30
24
  node = node.parentId ? graph.nodes.get(node.parentId) : undefined
31
25
  }
32
- if (fallback === 'default')
33
- return graph.variableCollections.get(collectionId)?.defaultModeId ?? ''
34
- return getActiveModeId(graph, collectionId)
26
+ return defaultModeId(graph, collectionId)
27
+ }
28
+
29
+ /**
30
+ * The collections whose modes decide what these variables resolve to: their own, and those of the
31
+ * variables they alias, at any depth and in any mode.
32
+ */
33
+ export function collectionsResolving(
34
+ graph: SceneGraph,
35
+ variableIds: Iterable<string>
36
+ ): Set<string> {
37
+ const pending = [...variableIds]
38
+ const seen = new Set<string>()
39
+ const collections = new Set<string>()
40
+ for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
41
+ if (seen.has(id)) continue
42
+ seen.add(id)
43
+ const variable = graph.variables.get(id)
44
+ if (!variable) continue
45
+ collections.add(variable.collectionId)
46
+ for (const value of Object.values(variable.valuesByMode))
47
+ pending.push(...variableValueReferences(value))
48
+ }
49
+ return collections
35
50
  }
36
51
 
37
52
  export function resolveVariable(
@@ -47,7 +62,7 @@ export function resolveVariable(
47
62
  (collectionId) =>
48
63
  modeId && initial?.collectionId === collectionId
49
64
  ? modeId
50
- : getActiveModeId(graph, collectionId),
65
+ : defaultModeId(graph, collectionId),
51
66
  visited
52
67
  )
53
68
  }
@@ -252,10 +267,9 @@ export function resolveNumberVariable(graph: SceneGraph, variableId: string): nu
252
267
  export function resolveColorVariableForNode(
253
268
  graph: SceneGraph,
254
269
  nodeId: string,
255
- variableId: string,
256
- fallback: VariableModeFallback = 'active'
270
+ variableId: string
257
271
  ): Color | undefined {
258
- const value = resolveVariableForNode(graph, nodeId, variableId, fallback)
272
+ const value = resolveVariableForNode(graph, nodeId, variableId)
259
273
  if (value && typeof value === 'object' && 'r' in value) return value
260
274
  return undefined
261
275
  }
@@ -263,10 +277,9 @@ export function resolveColorVariableForNode(
263
277
  export function resolveNumberVariableForNode(
264
278
  graph: SceneGraph,
265
279
  nodeId: string,
266
- variableId: string,
267
- fallback: VariableModeFallback = 'active'
280
+ variableId: string
268
281
  ): number | undefined {
269
- const value = resolveVariableForNode(graph, nodeId, variableId, fallback)
282
+ const value = resolveVariableForNode(graph, nodeId, variableId)
270
283
  return typeof value === 'number' ? value : undefined
271
284
  }
272
285
 
@@ -274,20 +287,18 @@ export function resolveNumberVariableForNode(
274
287
  export function resolveVariableForNode(
275
288
  graph: SceneGraph,
276
289
  nodeId: string,
277
- variableId: string,
278
- fallback: VariableModeFallback = 'active'
290
+ variableId: string
279
291
  ): VariableLiteral | undefined {
280
292
  return resolveInModes(graph, variableId, (collectionId) =>
281
- getNodeVariableModeId(graph, nodeId, collectionId, fallback)
293
+ getNodeVariableModeId(graph, nodeId, collectionId)
282
294
  )
283
295
  }
284
296
 
285
297
  export function resolveStringVariableForNode(
286
298
  graph: SceneGraph,
287
299
  nodeId: string,
288
- variableId: string,
289
- fallback: VariableModeFallback = 'active'
300
+ variableId: string
290
301
  ): string | undefined {
291
- const value = resolveVariableForNode(graph, nodeId, variableId, fallback)
302
+ const value = resolveVariableForNode(graph, nodeId, variableId)
292
303
  return typeof value === 'string' ? value : undefined
293
304
  }
@@ -0,0 +1,15 @@
1
+ import type { SceneGraph } from '../graph'
2
+ import type { SceneNode } from '../types'
3
+
4
+ /** The layers in these subtrees, the roots included, that a binding change can reach. */
5
+ export function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneNode[] {
6
+ const nodes: SceneNode[] = []
7
+ const pending = [...roots]
8
+ for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
9
+ const node = graph.getNode(id)
10
+ if (!node) continue
11
+ nodes.push(node)
12
+ pending.push(...node.childIds)
13
+ }
14
+ return nodes
15
+ }
@@ -352,8 +352,10 @@ function segmentToSVG(
352
352
  ): string {
353
353
  const start = forward ? vertices[seg.start] : vertices[seg.end]
354
354
  const end = forward ? vertices[seg.end] : vertices[seg.start]
355
- const ts = forward ? seg.tangentStart : { x: -seg.tangentEnd.x, y: -seg.tangentEnd.y }
356
- const te = forward ? seg.tangentEnd : { x: -seg.tangentStart.x, y: -seg.tangentStart.y }
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
357
359
 
358
360
  const isStraight =
359
361
  Math.abs(ts.x) < 0.001 &&