@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
@@ -1,18 +1,31 @@
1
- import { isEqual } from 'es-toolkit/predicate'
1
+ import { isEmptyObject, isEqual } from 'es-toolkit/predicate'
2
2
 
3
- import type { VariableModeFallback } from '.'
4
- import { copyFills, copyStrokes } from '../copy'
5
3
  import type { SceneGraph } from '../graph'
6
- import { hasInstanceOverride } from '../instances'
4
+ import { hasInstanceOverride } from '../instances/overrides'
7
5
  import type { Color } from '../primitives'
8
6
  import { pixelLength } from '../text/lengths'
9
- import type { Fill, SceneNode, Stroke, StyleRun, TextLength } from '../types'
10
- import { NUMERIC_FIELDS, isNumericVariableBindingField, paintBindingTarget } from './fields'
7
+ import type { Effect, SceneNode, StyleRun, TextLength } from '../types'
8
+ import {
9
+ effectBindableProperties,
10
+ effectBindingTarget,
11
+ NUMERIC_FIELDS,
12
+ isNumericVariableBindingField,
13
+ type EffectBindingProperty
14
+ } from './fields'
15
+ import { refreshBoundPaints } from './paints'
16
+ import { collectionsResolving } from './resolution'
17
+ import { subtreeNodes } from './subtree'
11
18
  import { variableText, variableTruthy } from './text'
12
19
  import { variableValueReferences } from './value'
13
20
  export {
21
+ EFFECT_BINDING_PROPERTIES,
22
+ effectBindableProperties,
23
+ effectBindingField,
24
+ effectBindingTarget,
25
+ effectBindingVariableType,
14
26
  isNumericVariableBindingField,
15
27
  paintBindingTarget,
28
+ type EffectBindingProperty,
16
29
  TEXT_CONTENT_VARIABLE_TYPES,
17
30
  VISIBILITY_VARIABLE_TYPES,
18
31
  type PaintBindingList
@@ -46,6 +59,12 @@ export function assignVariableBindingUnits(
46
59
  /** Fields a bound number sets as a length in pixels, as Figma's do, whatever unit they held. */
47
60
  const TEXT_LENGTH_FIELDS: ReadonlySet<string> = new Set(['lineHeight', 'letterSpacing'])
48
61
 
62
+ /** Whether the instance a layer sits in, or the instance itself, overrides one of its fields. */
63
+ function overriddenInInstance(graph: SceneGraph, nodeId: string, field: string): boolean {
64
+ const instance = graph.closest(nodeId, (node) => node.type === 'INSTANCE')
65
+ return !!instance && hasInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)
66
+ }
67
+
49
68
  /** A bound number as the field stores it. */
50
69
  export function boundNumberValue(field: string, value: number): number | TextLength {
51
70
  return TEXT_LENGTH_FIELDS.has(field) ? pixelLength(value) : value
@@ -55,17 +74,13 @@ export function boundNumberValue(field: string, value: number): number | TextLen
55
74
  * The values a node's numeric bindings resolve to in its mode, in scene units, for the fields
56
75
  * whose stored value differs. A width or height an instance overrides keeps its own value.
57
76
  */
58
- export function resolvedNumericBindings(
59
- graph: SceneGraph,
60
- node: SceneNode,
61
- fallback: VariableModeFallback = 'active'
62
- ): Partial<SceneNode> {
77
+ export function resolvedNumericBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode> {
63
78
  const updates: Partial<SceneNode> = {}
64
79
  for (const [field, variableId] of Object.entries(node.boundVariables)) {
65
80
  if (!NUMERIC_FIELDS.has(field)) continue
66
- if ((field === 'width' || field === 'height') && hasInstanceOverride(graph, node.id, field))
81
+ if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))
67
82
  continue
68
- const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
83
+ const value = graph.resolveNumberVariableForNode(node.id, variableId)
69
84
  if (value === undefined || !Number.isFinite(value)) continue
70
85
  const scale = node.variableBindingScales[field] ?? 1
71
86
  const effective = value * scale
@@ -86,16 +101,12 @@ export function resolvedNumericBindings(
86
101
  * A bound font family is left as stored: changing it needs the font loaded, which resolving a
87
102
  * binding cannot do.
88
103
  */
89
- export function resolvedValueBindings(
90
- graph: SceneGraph,
91
- node: SceneNode,
92
- fallback: VariableModeFallback = 'active'
93
- ): Partial<SceneNode> {
104
+ export function resolvedValueBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode> {
94
105
  const updates: Partial<SceneNode> = {}
95
106
  const resolve = (field: 'text' | 'visible') => {
96
107
  const variableId = node.boundVariables[field]
97
- if (!variableId || hasInstanceOverride(graph, node.id, field)) return undefined
98
- return graph.resolveVariableForNode(node.id, variableId, fallback)
108
+ if (!variableId || overriddenInInstance(graph, node.id, field)) return undefined
109
+ return graph.resolveVariableForNode(node.id, variableId)
99
110
  }
100
111
  const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
101
112
  if (text !== undefined && text !== node.text) updates.text = text
@@ -114,8 +125,7 @@ const NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeig
114
125
  */
115
126
  export function resolvedRunBindings(
116
127
  graph: SceneGraph,
117
- node: SceneNode,
118
- fallback: VariableModeFallback = 'active'
128
+ node: SceneNode
119
129
  ): Pick<Partial<SceneNode>, 'styleRuns'> {
120
130
  const styleRuns = node.styleRuns.map((run): StyleRun => {
121
131
  const bound = run.style.boundVariables
@@ -124,7 +134,7 @@ export function resolvedRunBindings(
124
134
  for (const field of NUMERIC_RUN_FIELDS) {
125
135
  const variableId = bound[field]
126
136
  if (!variableId) continue
127
- const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
137
+ const value = graph.resolveNumberVariableForNode(node.id, variableId)
128
138
  if (value === undefined || !Number.isFinite(value)) continue
129
139
  const next = boundNumberValue(field, value)
130
140
  if (isEqual(style[field], next)) continue
@@ -144,41 +154,53 @@ function boundVariableIds(node: SceneNode): string[] {
144
154
  ].filter((id): id is string => typeof id === 'string')
145
155
  }
146
156
 
147
- /**
148
- * A bound paint takes its whole color from the variable, alpha included, the way Figma stores
149
- * it: the variable's RGB at full alpha, and its alpha as the paint's opacity.
150
- */
151
- function applyBoundColor(paint: Fill | Stroke, color: Color): void {
152
- paint.color = { ...color, a: 1 }
153
- paint.opacity = color.a
157
+ /** `effect` with one bound property set to its variable's value; a colour keeps its alpha. */
158
+ function withBoundEffectValue(
159
+ effect: Effect,
160
+ property: EffectBindingProperty,
161
+ value: number | Color
162
+ ): Effect {
163
+ if (typeof value !== 'number') return property === 'color' ? { ...effect, color: value } : effect
164
+ if (property === 'offsetX') return { ...effect, offset: { ...effect.offset, x: value } }
165
+ if (property === 'offsetY') return { ...effect, offset: { ...effect.offset, y: value } }
166
+ if (property === 'radius' || property === 'spread') return { ...effect, [property]: value }
167
+ return effect
154
168
  }
155
169
 
156
- /** The fills and strokes a node's color bindings resolve to in its mode, when it has any. */
157
- export function resolvedPaintBindings(
170
+ /** A bound effect length in an occurrence's coordinates, which are 1:1 outside instances. */
171
+ function scaledLength(length: number | undefined, node: SceneNode): number | undefined {
172
+ return length === undefined ? undefined : length * node.componentScale
173
+ }
174
+
175
+ /**
176
+ * The effects a node's effect bindings resolve to in its mode, when one differs from what the node
177
+ * holds. Lengths stay as the variables give them, negative ones included, as Figma keeps them;
178
+ * drawing treats a negative blur as none. In an instance they take its scale against its
179
+ * component, whenever they were bound, as Figma scales them; a layer scaled outside an instance
180
+ * goes back to the variable's own value.
181
+ */
182
+ export function resolvedEffectBindings(
158
183
  graph: SceneGraph,
159
- node: SceneNode,
160
- fallback: VariableModeFallback = 'active'
161
- ): Pick<Partial<SceneNode>, 'fills' | 'strokes'> {
162
- // Most nodes bind nothing, so no copy is made until a paint binding is actually found.
163
- const changes: Pick<Partial<SceneNode>, 'fills' | 'strokes'> = {}
164
- for (const field in node.boundVariables) {
165
- if (!field.endsWith('/color')) continue
166
- const paint = paintBindingTarget(field)
167
- if (!paint) continue
168
- const color = graph.resolveColorVariableForNode(node.id, node.boundVariables[field], fallback)
169
- const { index } = paint
170
- if (!color) continue
171
- if (paint.list === 'fills') {
172
- if (!node.fills[index]) continue
173
- changes.fills ??= copyFills(node.fills)
174
- applyBoundColor(changes.fills[index], color)
175
- } else {
176
- if (!node.strokes[index]) continue
177
- changes.strokes ??= copyStrokes(node.strokes)
178
- applyBoundColor(changes.strokes[index], color)
179
- }
184
+ node: SceneNode
185
+ ): Pick<Partial<SceneNode>, 'effects'> {
186
+ // Most nodes bind nothing, so no copy is made until a value actually changes.
187
+ let effects: Effect[] | undefined
188
+ for (const [field, variableId] of Object.entries(node.boundVariables)) {
189
+ const target = effectBindingTarget(field)
190
+ if (!target) continue
191
+ const effect = (effects ?? node.effects).at(target.index)
192
+ if (!effect || !effectBindableProperties(effect.type).includes(target.property)) continue
193
+ const value =
194
+ target.property === 'color'
195
+ ? graph.resolveColorVariableForNode(node.id, variableId)
196
+ : scaledLength(graph.resolveNumberVariableForNode(node.id, variableId), node)
197
+ if (value == null || (typeof value === 'number' && !Number.isFinite(value))) continue
198
+ const next = withBoundEffectValue(effect, target.property, value)
199
+ if (isEqual(next, effect)) continue
200
+ effects ??= [...node.effects]
201
+ effects[target.index] = next
180
202
  }
181
- return changes
203
+ return effects ? { effects } : {}
182
204
  }
183
205
 
184
206
  /**
@@ -207,36 +229,12 @@ export function variablesResolvingThrough(graph: SceneGraph, ids: Iterable<strin
207
229
  return found
208
230
  }
209
231
 
210
- function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneNode[] {
211
- const nodes: SceneNode[] = []
212
- const pending = [...roots]
213
- for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
214
- const node = graph.getNode(id)
215
- if (!node) continue
216
- nodes.push(node)
217
- pending.push(...node.childIds)
218
- }
219
- return nodes
220
- }
221
-
222
232
  /**
223
233
  * The collections whose variables the layers in these subtrees resolve: those they bind, and
224
234
  * those the bound variables alias, so a mode set on the subtrees can change what they show.
225
235
  */
226
236
  export function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {
227
- const pending = subtreeNodes(graph, roots).flatMap(boundVariableIds)
228
- const seen = new Set<string>()
229
- const collections = new Set<string>()
230
- for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
231
- if (seen.has(id)) continue
232
- seen.add(id)
233
- const variable = graph.variables.get(id)
234
- if (!variable) continue
235
- collections.add(variable.collectionId)
236
- for (const value of Object.values(variable.valuesByMode))
237
- pending.push(...variableValueReferences(value))
238
- }
239
- return collections
237
+ return collectionsResolving(graph, subtreeNodes(graph, roots).flatMap(boundVariableIds))
240
238
  }
241
239
 
242
240
  /**
@@ -250,6 +248,27 @@ export function layersUsingVariables(graph: SceneGraph, ids: Iterable<string>):
250
248
  )
251
249
  }
252
250
 
251
+ /**
252
+ * The layers in these subtrees that resolve a variable of these collections, binding it or a
253
+ * variable that aliases it, so a mode change for the collections can change what they show.
254
+ */
255
+ export function layersResolvingCollections(
256
+ graph: SceneGraph,
257
+ roots: Iterable<string>,
258
+ collectionIds: ReadonlySet<string>
259
+ ): SceneNode[] {
260
+ const inCollections = [...graph.variables.values()].filter((variable) =>
261
+ collectionIds.has(variable.collectionId)
262
+ )
263
+ const variables = variablesResolvingThrough(
264
+ graph,
265
+ inCollections.map((variable) => variable.id)
266
+ )
267
+ return subtreeNodes(graph, roots).filter((node) =>
268
+ boundVariableIds(node).some((id) => variables.has(id))
269
+ )
270
+ }
271
+
253
272
  function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterable<SceneNode> {
254
273
  if (!scope) return graph.getAllNodes()
255
274
  if ('subtrees' in scope) return subtreeNodes(graph, scope.subtrees)
@@ -257,21 +276,66 @@ function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterab
257
276
  }
258
277
 
259
278
  /**
260
- * Resolve live numeric, text, and visibility bindings, numbers in scene units, without authoring
261
- * instance overrides. A scope limits it to the layers a change can reach, so layers whose saved
262
- * values differ from their bindings elsewhere in the document are left as saved.
279
+ * Resolve live numeric, text, visibility, and effect bindings, numbers in scene units, and refresh
280
+ * bound paints whose variable's color for the layer changed (`refreshBoundPaints`), without
281
+ * authoring instance overrides. A scope limits it to the layers a change can reach, so layers whose
282
+ * saved values differ from their bindings elsewhere in the document are left as saved. Returns the
283
+ * layers whose change needs a layout pass; effects and paints are not a layout input, so a change
284
+ * to them alone is written but not returned.
263
285
  */
264
286
  export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[] {
265
- const changed: string[] = []
266
- for (const node of scopedNodes(graph, scope)) {
267
- const updates = {
287
+ return [...reconcileVariableBindingUpdates(graph, scope).keys()]
288
+ }
289
+
290
+ /**
291
+ * As `reconcileVariableBindings`, with the layout fields each relaid layer received. Effects and
292
+ * paints are written too, but draw without layout, so a layer whose only change is one is left out.
293
+ */
294
+ export function reconcileVariableBindingUpdates(
295
+ graph: SceneGraph,
296
+ scope?: BindingScope
297
+ ): Map<string, Partial<SceneNode>> {
298
+ const relaid = new Map<string, Partial<SceneNode>>()
299
+ const nodes = [...scopedNodes(graph, scope)]
300
+ for (const node of nodes) refreshBoundPaints(graph, node)
301
+ for (const node of nodes) {
302
+ const layoutUpdates = {
268
303
  ...resolvedNumericBindings(graph, node),
269
304
  ...resolvedValueBindings(graph, node),
270
305
  ...resolvedRunBindings(graph, node)
271
306
  }
272
- if (Object.keys(updates).length === 0) continue
307
+ const updates = { ...layoutUpdates, ...resolvedEffectBindings(graph, node) }
308
+ if (isEmptyObject(updates)) continue
273
309
  graph.updateNode(node.id, updates)
274
- changed.push(node.id)
310
+ if (!isEmptyObject(layoutUpdates)) relaid.set(node.id, layoutUpdates)
311
+ }
312
+ return relaid
313
+ }
314
+
315
+ /** A layer list whose items carry their own bindings, keyed by position such as `fills/1/color`. */
316
+ export type BoundLayerList = 'fills' | 'strokes' | 'effects'
317
+
318
+ /**
319
+ * `boundVariables` once the `list` items are rebuilt from `sources`, where `sources[i]` is the
320
+ * position the new item `i` had before, or null for an added item. A binding moves with its item
321
+ * and goes with a removed one, as Figma keeps a binding on its paint or effect.
322
+ */
323
+ export function listBindingsAfterEdit(
324
+ boundVariables: Readonly<Record<string, string>>,
325
+ list: BoundLayerList,
326
+ sources: readonly (number | null)[]
327
+ ): Record<string, string> {
328
+ const prefix = `${list}/`
329
+ const moved: Record<string, string> = {}
330
+ for (const [field, variableId] of Object.entries(boundVariables)) {
331
+ if (!field.startsWith(prefix)) {
332
+ moved[field] = variableId
333
+ continue
334
+ }
335
+ const [index, ...property] = field.slice(prefix.length).split('/')
336
+ const next = sources.indexOf(Number(index))
337
+ if (next !== -1 && property.length > 0)
338
+ moved[`${prefix}${next}/${property.join('/')}`] = variableId
275
339
  }
276
- return changed
340
+ return moved
277
341
  }
@@ -1,4 +1,4 @@
1
- import type { VariableType } from '../types'
1
+ import type { Effect, VariableType } from '../types'
2
2
 
3
3
  export const NUMERIC_FIELDS = new Set([
4
4
  'opacity',
@@ -79,3 +79,48 @@ export function paintBindingTarget(
79
79
  if (!match) return null
80
80
  return { list: match[1] === 'fills' ? 'fills' : 'strokes', index: Number(match[2]) }
81
81
  }
82
+
83
+ /** The effect fields that bind to variables, in the order Figma's messages list them. */
84
+ export const EFFECT_BINDING_PROPERTIES = [
85
+ 'color',
86
+ 'radius',
87
+ 'spread',
88
+ 'offsetX',
89
+ 'offsetY'
90
+ ] as const
91
+
92
+ export type EffectBindingProperty = (typeof EFFECT_BINDING_PROPERTIES)[number]
93
+
94
+ const EFFECT_BINDING_FIELD = /^effects\/(\d+)\/(color|radius|spread|offsetX|offsetY)$/
95
+
96
+ const BLUR_BINDING_PROPERTIES = ['radius'] as const satisfies readonly EffectBindingProperty[]
97
+
98
+ /** The node binding field for one property of the effect at `index`, such as `effects/0/radius`. */
99
+ export function effectBindingField(index: number, property: EffectBindingProperty): string {
100
+ return `effects/${index}/${property}`
101
+ }
102
+
103
+ /**
104
+ * The effect and property a binding field names, such as the second effect's blur for
105
+ * `effects/1/radius`, or null for a field that binds anything else.
106
+ */
107
+ export function effectBindingTarget(
108
+ field: string
109
+ ): { index: number; property: EffectBindingProperty } | null {
110
+ if (!field.startsWith('effects/')) return null
111
+ const match = EFFECT_BINDING_FIELD.exec(field)
112
+ if (!match) return null
113
+ return { index: Number(match[1]), property: match[2] as EffectBindingProperty }
114
+ }
115
+
116
+ /** The properties of an effect that bind: every shadow property, else only the radius. */
117
+ export function effectBindableProperties(type: Effect['type']): readonly EffectBindingProperty[] {
118
+ return type === 'DROP_SHADOW' || type === 'INNER_SHADOW'
119
+ ? EFFECT_BINDING_PROPERTIES
120
+ : BLUR_BINDING_PROPERTIES
121
+ }
122
+
123
+ /** The variable type an effect property binds to: colour to COLOR, lengths to FLOAT. */
124
+ export function effectBindingVariableType(property: EffectBindingProperty): 'COLOR' | 'FLOAT' {
125
+ return property === 'color' ? 'COLOR' : 'FLOAT'
126
+ }
@@ -120,7 +120,9 @@ export function parseVariableFormula(
120
120
  graph,
121
121
  variableId,
122
122
  (collectionId) =>
123
- collectionId === variable.collectionId ? modeId : graph.getActiveModeId(collectionId),
123
+ collectionId === variable.collectionId
124
+ ? modeId
125
+ : (graph.variableCollections.get(collectionId)?.defaultModeId ?? ''),
124
126
  new Set(),
125
127
  new Map(),
126
128
  new Map([[variableId, value]])
@@ -1,4 +1,4 @@
1
- import { partition } from 'es-toolkit/array'
1
+ import { compact, partition, uniq } from 'es-toolkit/array'
2
2
  import { omit, omitBy } from 'es-toolkit/object'
3
3
 
4
4
  import { BLACK } from '../constants'
@@ -7,6 +7,8 @@ import { findInstanceAncestor } from '../instances'
7
7
  import { bindingOverrideField, setInstanceOverride } from '../instances/overrides'
8
8
  import { siblingOrderKeys } from '../order-keys'
9
9
  import type {
10
+ Effect,
11
+ SceneNode,
10
12
  Variable,
11
13
  VariableCollection,
12
14
  VariableCollectionMode,
@@ -20,18 +22,25 @@ import {
20
22
  } from './bindings'
21
23
  import {
22
24
  BOOLEAN_BINDING_FIELDS,
25
+ effectBindableProperties,
26
+ effectBindingTarget,
27
+ effectBindingVariableType,
28
+ type EffectBindingProperty,
23
29
  paintBindingTarget,
24
30
  STRING_BINDING_FIELDS,
25
31
  TEXT_CONTENT_VARIABLE_TYPES,
26
32
  VISIBILITY_VARIABLE_TYPES
27
33
  } from './fields'
34
+ import { applyBoundPaintColor } from './paints'
28
35
 
29
36
  export * from './resolution'
30
37
 
31
38
  export function addVariable(graph: SceneGraph, variable: Variable): void {
39
+ graph.willChangeVariable(variable.id)
32
40
  graph.variables.set(variable.id, variable)
33
41
  const collection = graph.variableCollections.get(variable.collectionId)
34
42
  if (collection && !collection.variableIds.includes(variable.id)) {
43
+ graph.willChangeCollection(collection.id)
35
44
  collection.variableIds.push(variable.id)
36
45
  }
37
46
  }
@@ -39,9 +48,11 @@ export function addVariable(graph: SceneGraph, variable: Variable): void {
39
48
  export function removeVariable(graph: SceneGraph, id: string): void {
40
49
  const variable = graph.variables.get(id)
41
50
  if (!variable) return
51
+ graph.willChangeVariable(id)
42
52
  graph.variables.delete(id)
43
53
  const collection = graph.variableCollections.get(variable.collectionId)
44
54
  if (collection) {
55
+ graph.willChangeCollection(collection.id)
45
56
  collection.variableIds = collection.variableIds.filter((vid) => vid !== id)
46
57
  }
47
58
  for (const node of graph.nodes.values()) {
@@ -65,10 +76,8 @@ export function removeVariable(graph: SceneGraph, id: string): void {
65
76
  }
66
77
 
67
78
  export function addCollection(graph: SceneGraph, collection: VariableCollection): void {
79
+ graph.willChangeCollection(collection.id)
68
80
  graph.variableCollections.set(collection.id, collection)
69
- if (!graph.activeMode.has(collection.id)) {
70
- graph.activeMode.set(collection.id, collection.defaultModeId)
71
- }
72
81
  }
73
82
 
74
83
  function defaultVariableValue(type: VariableType, value?: VariableValue): VariableValue {
@@ -133,12 +142,8 @@ export function removeCollection(graph: SceneGraph, id: string): void {
133
142
  removeVariable(graph, varId)
134
143
  }
135
144
  }
145
+ graph.willChangeCollection(id)
136
146
  graph.variableCollections.delete(id)
137
- graph.activeMode.delete(id)
138
- }
139
-
140
- export function setActiveMode(graph: SceneGraph, collectionId: string, modeId: string): void {
141
- graph.activeMode.set(collectionId, modeId)
142
147
  }
143
148
 
144
149
  export function addMode(
@@ -150,11 +155,13 @@ export function addMode(
150
155
  ): void {
151
156
  const collection = graph.variableCollections.get(collectionId)
152
157
  if (!collection) return
158
+ graph.willChangeCollection(collectionId)
153
159
  collection.modes.push({ modeId, name })
154
160
  const sourceModeId = sourceMode ?? collection.defaultModeId
155
161
  for (const varId of collection.variableIds) {
156
162
  const variable = graph.variables.get(varId)
157
163
  if (!variable) continue
164
+ graph.willChangeVariable(varId)
158
165
  variable.valuesByMode[modeId] = structuredClone(
159
166
  variable.valuesByMode[sourceModeId] ?? Object.values(variable.valuesByMode)[0]
160
167
  )
@@ -178,16 +185,16 @@ export function createMode(
178
185
  export function removeMode(graph: SceneGraph, collectionId: string, modeId: string): void {
179
186
  const collection = graph.variableCollections.get(collectionId)
180
187
  if (!collection || collection.modes.length <= 1) return
188
+ graph.willChangeCollection(collectionId)
181
189
  collection.modes = collection.modes.filter((m) => m.modeId !== modeId)
182
190
  if (collection.defaultModeId === modeId) {
183
191
  collection.defaultModeId = collection.modes[0].modeId
184
192
  }
185
193
  for (const varId of collection.variableIds) {
186
194
  const variable = graph.variables.get(varId)
187
- if (variable) variable.valuesByMode = omit(variable.valuesByMode, [modeId])
188
- }
189
- if (graph.activeMode.get(collectionId) === modeId) {
190
- graph.activeMode.set(collectionId, collection.defaultModeId)
195
+ if (!variable) continue
196
+ graph.willChangeVariable(varId)
197
+ variable.valuesByMode = omit(variable.valuesByMode, [modeId])
191
198
  }
192
199
  }
193
200
 
@@ -200,13 +207,16 @@ export function renameMode(
200
207
  const collection = graph.variableCollections.get(collectionId)
201
208
  if (!collection) return
202
209
  const mode = collection.modes.find((m) => m.modeId === modeId)
203
- if (mode) mode.name = name
210
+ if (!mode) return
211
+ graph.willChangeCollection(collectionId)
212
+ mode.name = name
204
213
  }
205
214
 
206
215
  export function setDefaultMode(graph: SceneGraph, collectionId: string, modeId: string): void {
207
216
  const collection = graph.variableCollections.get(collectionId)
208
217
  if (!collection) return
209
218
  if (!collection.modes.some((m) => m.modeId === modeId)) return
219
+ graph.willChangeCollection(collectionId)
210
220
  collection.defaultModeId = modeId
211
221
  collection.modes = withSortPositionsInOrder(modesDefaultFirst(collection))
212
222
  }
@@ -237,6 +247,49 @@ export function getVariablesByType(graph: SceneGraph, type: VariableType): Varia
237
247
  return [...graph.variables.values()].filter((v) => v.type === type)
238
248
  }
239
249
 
250
+ /** Checks that an effect binding names an effect the node has, a property it binds, and its type. */
251
+ function assertEffectBinding(
252
+ effects: readonly Effect[],
253
+ target: { index: number; property: EffectBindingProperty },
254
+ type: VariableType,
255
+ field: string
256
+ ): void {
257
+ const effect = effects.at(target.index)
258
+ if (!effect) {
259
+ throw new Error(`Index ${target.index} out of range for effects (length ${effects.length})`)
260
+ }
261
+ if (!effectBindableProperties(effect.type).includes(target.property)) {
262
+ throw new Error(`A ${effect.type} effect has no bindable ${target.property} ("${field}")`)
263
+ }
264
+ if (type !== effectBindingVariableType(target.property)) {
265
+ throw new Error(`Cannot bind ${type} variable to effect field "${field}"`)
266
+ }
267
+ }
268
+
269
+ /** Checks a paint binding and drops the list's dead top-level binding it replaces. */
270
+ function preparePaintBinding(
271
+ graph: SceneGraph,
272
+ node: SceneNode,
273
+ paint: { list: 'fills' | 'strokes'; index: number },
274
+ variable: Variable,
275
+ field: string
276
+ ): void {
277
+ // Color fields require COLOR variable type
278
+ if (variable.type !== 'COLOR') {
279
+ throw new Error(`Cannot bind ${variable.type} variable to color field "${field}"`)
280
+ }
281
+ // Validate index is within current array bounds
282
+ const currentLength = node[paint.list].length
283
+ if (paint.index >= currentLength) {
284
+ throw new Error(`Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`)
285
+ }
286
+ // Auto-remove top-level dead binding (e.g. 'fills') when setting indexed binding
287
+ if (paint.list in node.boundVariables) {
288
+ graph.willChangeNode(node)
289
+ node.boundVariables = omit(node.boundVariables, [paint.list])
290
+ }
291
+ }
292
+
240
293
  export function bindVariable(
241
294
  graph: SceneGraph,
242
295
  nodeId: string,
@@ -252,24 +305,11 @@ export function bindVariable(
252
305
  throw new Error(`Variable "${variableId}" not found`)
253
306
  }
254
307
 
255
- // Color fields require COLOR variable type
256
308
  const paint = paintBindingTarget(field)
257
- if (paint) {
258
- if (variable.type !== 'COLOR') {
259
- throw new Error(`Cannot bind ${variable.type} variable to color field "${field}"`)
260
- }
261
- // Validate index is within current array bounds
262
- const currentLength = node[paint.list].length
263
- if (paint.index >= currentLength) {
264
- throw new Error(
265
- `Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`
266
- )
267
- }
268
- // Auto-remove top-level dead binding (e.g. 'fills') when setting indexed binding
269
- if (paint.list in node.boundVariables) {
270
- node.boundVariables = omit(node.boundVariables, [paint.list])
271
- }
272
- }
309
+ if (paint) preparePaintBinding(graph, node, paint, variable, field)
310
+
311
+ const effect = effectBindingTarget(field)
312
+ if (effect) assertEffectBinding(node.effects, effect, variable.type, field)
273
313
 
274
314
  if (isNumericVariableBindingField(field) && variable.type !== 'FLOAT') {
275
315
  throw new Error(`Cannot bind ${variable.type} variable to scalar field "${field}"`)
@@ -292,7 +332,8 @@ export function bindVariable(
292
332
  isNumericVariableBindingField(field) ||
293
333
  STRING_BINDING_FIELDS.has(field) ||
294
334
  BOOLEAN_BINDING_FIELDS.has(field) ||
295
- paint !== null
335
+ paint !== null ||
336
+ effect !== null
296
337
 
297
338
  if (!isKnownField) {
298
339
  throw new Error(`Unknown binding field "${field}"`)
@@ -306,6 +347,8 @@ export function bindVariable(
306
347
  boundVariables: { ...node.boundVariables },
307
348
  variableBindingScales: { ...node.variableBindingScales }
308
349
  })
350
+ // A paint takes its variable's color, alpha as opacity, when it binds, as Figma's does.
351
+ if (paint) applyBoundPaintColor(graph, node, field)
309
352
  }
310
353
 
311
354
  export function unbindVariable(graph: SceneGraph, nodeId: string, field: string): void {
@@ -333,22 +376,30 @@ function markBoundVariablesOverrideOnInstance(
333
376
  const owner = variableBindingOwner(graph, node)
334
377
  if (owner.type !== 'INSTANCE') return
335
378
  const nearest = findInstanceAncestor(graph, nodeId)
379
+ const instances = uniq(compact([nearest, owner])).filter((x) => x.id !== nodeId)
380
+ for (const instance of instances) graph.willChangeNode(instance)
336
381
  if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, 'boundVariables')
337
382
  setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, 'boundVariables')
383
+ // Each instance claims the binding it changed alone, so the layer's other bindings keep
384
+ // following their components, as Figma claims only the paint or effect a script edits.
338
385
  if (field)
339
- setInstanceOverride(
340
- owner.instanceOverrides,
341
- owner.id,
342
- nodeId,
343
- bindingOverrideField(field),
344
- node.boundVariables[field] ?? null
345
- )
386
+ for (const instance of uniq(compact([nearest, owner])))
387
+ setInstanceOverride(
388
+ instance.instanceOverrides,
389
+ instance.id,
390
+ nodeId,
391
+ bindingOverrideField(field),
392
+ node.boundVariables[field] ?? null
393
+ )
394
+ for (const instance of instances)
395
+ graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })
346
396
  }
347
397
 
348
398
  export * from './bindings'
349
399
  export * from './corners'
350
400
  export * from './formula'
351
401
  export * from './formula/schema'
402
+ export * from './paints'
352
403
  export * from './schema'
353
404
  export * from './text'
354
405
  export * from './token'