@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,57 @@
1
+ import { SceneGraph } from "../graph.js";
2
+ import { SceneNode } from "../types.js";
3
+ import { PaintBindingList } from "./fields.js";
4
+ //#region src/variables/paints.d.ts
5
+ /** Whether a layer has a paint whose color is bound to a variable. */
6
+ declare function hasBoundPaints(node: SceneNode): boolean;
7
+ /**
8
+ * A layer copied from another keeps the colors its bindings last applied there, so the copy's
9
+ * paints, edits included, stay as they are until their variables' colors for it change.
10
+ */
11
+ declare function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void;
12
+ type PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>;
13
+ /**
14
+ * Whether `node` is an instance, or a layer an instance copies from its component, with a paint
15
+ * list neither it nor an instance around it overrides: such paints follow the component, and
16
+ * refreshing them is no edit.
17
+ */
18
+ declare function copiesComponentPaints(graph: SceneGraph, node: SceneNode, list: PaintBindingList): boolean;
19
+ /**
20
+ * Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as
21
+ * opacity, once that color differs from the one last applied, discarding edits made since, as
22
+ * Figma refreshes them when a variable's value or a layer's mode changes.
23
+ */
24
+ declare function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void;
25
+ /** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */
26
+ declare function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field: string): void;
27
+ /**
28
+ * Gives every bound paint in this subtree its variable's color where it sits. A new instance
29
+ * or a layer an instance sync adds is a copy, which shows its variables in its own context rather
30
+ * than whatever its component's paints hold.
31
+ */
32
+ declare function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void;
33
+ /** Refreshes the bound paints in this subtree whose variables now give their layers another color. */
34
+ declare function refreshBoundPaintsIn(graph: SceneGraph, rootId: string): void;
35
+ /**
36
+ * Records, for the paint lists a write sets, the colors their variables give the layer now, so a
37
+ * paint written with another color or opacity keeps it until that color changes.
38
+ */
39
+ declare function recordWrittenBoundPaints(graph: SceneGraph, node: SceneNode, written: PaintLists): void;
40
+ /**
41
+ * Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose
42
+ * mode the move changed, as Figma does when a layer enters a frame or page in another mode. A
43
+ * move between parents in the same modes, or of layers that bind no paint, costs no more than
44
+ * comparing those modes.
45
+ */
46
+ declare function refreshMovedBoundPaints(graph: SceneGraph, nodeId: string, previousParentId: string | null | undefined): void;
47
+ /**
48
+ * A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit
49
+ * to its paint's color or opacity does not reach them. An instance sync writing a copy's paints
50
+ * keeps the color and opacity of each one still bound to the same variable, and gives it the
51
+ * variable's color for the copy once that differs from the one last applied, or when it binds
52
+ * anew. `updates` holds what the sync writes, whose paints this sets in place.
53
+ */
54
+ declare function keepCopyBoundPaints(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void;
55
+ //#endregion
56
+ export { applyBoundPaintColor, applyBoundPaintColorsIn, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints };
57
+ //# sourceMappingURL=paints.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paints.d.ts","names":[],"sources":["../../src/variables/paints.ts"],"mappings":";;;;;iBA4CgB,eAAe,MAAM;;;;;iBA+BrB,uBAAuB,QAAQ,WAAW,MAAM;KAyB3D,aAAa,KAAK,QAAQ;;;;;;iBAOf,sBACd,OAAO,YACP,MAAM,WACN,MAAM;;;;;;iBA4DQ,mBAAmB,OAAO,YAAY,MAAM;;iBAK5C,qBAAqB,OAAO,YAAY,MAAM,WAAW;;;;;;iBASzD,wBAAwB,OAAO,YAAY;;iBAM3C,qBAAqB,OAAO,YAAY;;;;;iBAQxC,yBACd,OAAO,YACP,MAAM,WACN,SAAS;;;;;;;iBAeK,wBACd,OAAO,YACP,gBACA;;;;;;;;iBA+Cc,oBACd,OAAO,YACP,QAAQ,WACR,SAAS,QAAQ"}
@@ -0,0 +1,231 @@
1
+ import { paintBindingTarget } from "./fields.js";
2
+ import { hasInstanceOverride } from "../instances/overrides.js";
3
+ import { copyFills, copyStrokes } from "../copy.js";
4
+ import { collectionsResolving, getNodeVariableModeId } from "./resolution.js";
5
+ import { subtreeNodes } from "./subtree.js";
6
+ //#region src/variables/paints.ts
7
+ const appliedColors = /* @__PURE__ */ new WeakMap();
8
+ const PAINT_LISTS = ["fills", "strokes"];
9
+ /** The paints of `node` whose color is bound to a variable, each with a paint there to color. */
10
+ function boundPaints(node) {
11
+ const paints = [];
12
+ for (const [field, variableId] of Object.entries(node.boundVariables)) {
13
+ const target = paintBindingTarget(field);
14
+ if (target && node[target.list].at(target.index)) paints.push({
15
+ field,
16
+ ...target,
17
+ variableId
18
+ });
19
+ }
20
+ return paints;
21
+ }
22
+ /** Whether a layer has a paint whose color is bound to a variable. */
23
+ function hasBoundPaints(node) {
24
+ for (const field in node.boundVariables) if (paintBindingTarget(field)) return true;
25
+ return false;
26
+ }
27
+ /** A paint's color with its opacity as alpha, as a variable's color carries it. */
28
+ function paintColor(paint) {
29
+ return {
30
+ r: paint.color.r,
31
+ g: paint.color.g,
32
+ b: paint.color.b,
33
+ a: paint.opacity
34
+ };
35
+ }
36
+ /** Saved colors are single precision, so a color read back from a file still matches its source. */
37
+ const COLOR_TOLERANCE = 1e-6;
38
+ function sameColor(a, b) {
39
+ return Math.abs(a.r - b.r) <= COLOR_TOLERANCE && Math.abs(a.g - b.g) <= COLOR_TOLERANCE && Math.abs(a.b - b.b) <= COLOR_TOLERANCE && Math.abs(a.a - b.a) <= COLOR_TOLERANCE;
40
+ }
41
+ function lastApplied(node, bound, paint) {
42
+ const entry = appliedColors.get(node)?.get(bound.field);
43
+ return entry?.variableId === bound.variableId ? entry.color : paintColor(paint);
44
+ }
45
+ /**
46
+ * A layer copied from another keeps the colors its bindings last applied there, so the copy's
47
+ * paints, edits included, stay as they are until their variables' colors for it change.
48
+ */
49
+ function copyAppliedBoundPaints(source, copy) {
50
+ const entries = appliedColors.get(source);
51
+ if (entries) appliedColors.set(copy, new Map(entries));
52
+ }
53
+ function recordApplied(node, bound, color) {
54
+ let entries = appliedColors.get(node);
55
+ if (!entries) {
56
+ entries = /* @__PURE__ */ new Map();
57
+ appliedColors.set(node, entries);
58
+ }
59
+ entries.set(bound.field, {
60
+ variableId: bound.variableId,
61
+ color: { ...color }
62
+ });
63
+ }
64
+ /** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */
65
+ function applyColor(paint, color) {
66
+ paint.color = {
67
+ ...color,
68
+ a: 1
69
+ };
70
+ paint.opacity = color.a;
71
+ }
72
+ /** Whether `paint` already shows `color` as a bound paint stores it. */
73
+ function showsColor(paint, color) {
74
+ return sameColor(paintColor(paint), color) && paint.color.a === 1;
75
+ }
76
+ /**
77
+ * Whether `node` is an instance, or a layer an instance copies from its component, with a paint
78
+ * list neither it nor an instance around it overrides: such paints follow the component, and
79
+ * refreshing them is no edit.
80
+ */
81
+ function copiesComponentPaints(graph, node, list) {
82
+ const walk = { copied: false };
83
+ const overriding = graph.closest(node.id, (current) => {
84
+ if (current.type !== "INSTANCE") return false;
85
+ walk.copied = true;
86
+ return hasInstanceOverride(current.instanceOverrides, current.id, node.id, list);
87
+ });
88
+ return walk.copied && !overriding;
89
+ }
90
+ /**
91
+ * Writes refreshed paints. A copy's paints that follow its component are derived values that a
92
+ * save leaves to the component, not edits written over it.
93
+ */
94
+ function writePaints(graph, node, changes) {
95
+ const derived = PAINT_LISTS.every((list) => !changes[list] || copiesComponentPaints(graph, node, list));
96
+ const write = () => graph.updateNode(node.id, changes);
97
+ if (derived) graph.applyComponentDerivedDuring(write);
98
+ else write();
99
+ }
100
+ /**
101
+ * Writes into each bound paint the color its variable gives the layer now, where that differs
102
+ * from the color last applied, or wherever `forced` says, and records what each binding gives.
103
+ */
104
+ function refresh(graph, node, forced) {
105
+ let fills;
106
+ let strokes;
107
+ for (const bound of boundPaints(node)) {
108
+ const color = graph.resolveColorVariableForNode(node.id, bound.variableId);
109
+ const paint = node[bound.list].at(bound.index);
110
+ if (!color || !paint) continue;
111
+ const last = lastApplied(node, bound, paint);
112
+ recordApplied(node, bound, color);
113
+ if (!forced(bound.field) && sameColor(last, color) || showsColor(paint, color)) continue;
114
+ if (bound.list === "fills") {
115
+ fills ??= copyFills(node.fills);
116
+ applyColor(fills[bound.index], color);
117
+ } else {
118
+ strokes ??= copyStrokes(node.strokes);
119
+ applyColor(strokes[bound.index], color);
120
+ }
121
+ }
122
+ const changes = {};
123
+ if (fills) changes.fills = fills;
124
+ if (strokes) changes.strokes = strokes;
125
+ if (fills || strokes) writePaints(graph, node, changes);
126
+ }
127
+ const NONE_FORCED = () => false;
128
+ const ALL_FORCED = () => true;
129
+ /**
130
+ * Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as
131
+ * opacity, once that color differs from the one last applied, discarding edits made since, as
132
+ * Figma refreshes them when a variable's value or a layer's mode changes.
133
+ */
134
+ function refreshBoundPaints(graph, node) {
135
+ if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED);
136
+ }
137
+ /** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */
138
+ function applyBoundPaintColor(graph, node, field) {
139
+ if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field);
140
+ }
141
+ /**
142
+ * Gives every bound paint in this subtree its variable's color where it sits. A new instance
143
+ * or a layer an instance sync adds is a copy, which shows its variables in its own context rather
144
+ * than whatever its component's paints hold.
145
+ */
146
+ function applyBoundPaintColorsIn(graph, rootId) {
147
+ for (const node of subtreeNodes(graph, [rootId])) if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED);
148
+ }
149
+ /** Refreshes the bound paints in this subtree whose variables now give their layers another color. */
150
+ function refreshBoundPaintsIn(graph, rootId) {
151
+ for (const node of subtreeNodes(graph, [rootId])) refreshBoundPaints(graph, node);
152
+ }
153
+ /**
154
+ * Records, for the paint lists a write sets, the colors their variables give the layer now, so a
155
+ * paint written with another color or opacity keeps it until that color changes.
156
+ */
157
+ function recordWrittenBoundPaints(graph, node, written) {
158
+ for (const bound of boundPaints(node)) {
159
+ if (!written[bound.list]) continue;
160
+ const color = graph.resolveColorVariableForNode(node.id, bound.variableId);
161
+ if (color) recordApplied(node, bound, color);
162
+ }
163
+ }
164
+ /**
165
+ * Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose
166
+ * mode the move changed, as Figma does when a layer enters a frame or page in another mode. A
167
+ * move between parents in the same modes, or of layers that bind no paint, costs no more than
168
+ * comparing those modes.
169
+ */
170
+ function refreshMovedBoundPaints(graph, nodeId, previousParentId) {
171
+ if (graph.isApplyingImportedState || graph.variableCollections.size === 0) return;
172
+ const parentId = graph.nodes.get(nodeId)?.parentId;
173
+ if (!parentId) return;
174
+ const previous = previousParentId && graph.nodes.has(previousParentId) ? previousParentId : null;
175
+ const changed = new Set([...graph.variableCollections.keys()].filter((id) => !previous || getNodeVariableModeId(graph, previous, id) !== getNodeVariableModeId(graph, parentId, id)));
176
+ if (changed.size === 0) return;
177
+ const reaches = /* @__PURE__ */ new Map();
178
+ const reachesChanged = (variableId) => {
179
+ let found = reaches.get(variableId);
180
+ if (found === void 0) {
181
+ found = [...collectionsResolving(graph, [variableId])].some((id) => changed.has(id));
182
+ reaches.set(variableId, found);
183
+ }
184
+ return found;
185
+ };
186
+ for (const node of subtreeNodes(graph, [nodeId])) if (boundPaints(node).some((bound) => reachesChanged(bound.variableId))) refresh(graph, node, NONE_FORCED);
187
+ }
188
+ function reboundPaint(target, bindings, list) {
189
+ return target[list].some((_, index) => {
190
+ const field = `${list}/${index}/color`;
191
+ return field in bindings && bindings[field] !== target.boundVariables[field];
192
+ });
193
+ }
194
+ /**
195
+ * A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit
196
+ * to its paint's color or opacity does not reach them. An instance sync writing a copy's paints
197
+ * keeps the color and opacity of each one still bound to the same variable, and gives it the
198
+ * variable's color for the copy once that differs from the one last applied, or when it binds
199
+ * anew. `updates` holds what the sync writes, whose paints this sets in place.
200
+ */
201
+ function keepCopyBoundPaints(graph, target, updates) {
202
+ const bindings = updates.boundVariables ?? target.boundVariables;
203
+ for (const list of PAINT_LISTS) {
204
+ if (!updates[list] && reboundPaint(target, bindings, list)) Object.assign(updates, { [list]: list === "fills" ? copyFills(target.fills) : copyStrokes(target.strokes) });
205
+ updates[list]?.forEach((paint, index) => {
206
+ const field = `${list}/${index}/color`;
207
+ const variableId = bindings[field];
208
+ if (!variableId) return;
209
+ const bound = {
210
+ field,
211
+ list,
212
+ index,
213
+ variableId
214
+ };
215
+ const own = target[list].at(index);
216
+ const keeps = own !== void 0 && target.boundVariables[field] === variableId;
217
+ if (keeps) {
218
+ paint.color = { ...own.color };
219
+ paint.opacity = own.opacity;
220
+ }
221
+ const color = graph.resolveColorVariableForNode(target.id, variableId);
222
+ if (!color) return;
223
+ if (!keeps || !sameColor(lastApplied(target, bound, own), color)) applyColor(paint, color);
224
+ recordApplied(target, bound, color);
225
+ });
226
+ }
227
+ }
228
+ //#endregion
229
+ export { applyBoundPaintColor, applyBoundPaintColorsIn, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints };
230
+
231
+ //# sourceMappingURL=paints.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paints.js","names":[],"sources":["../../src/variables/paints.ts"],"sourcesContent":["import { copyFills, copyStrokes } from '../copy'\nimport type { SceneGraph } from '../graph'\nimport { hasInstanceOverride } from '../instances/overrides'\nimport type { Color } from '../primitives'\nimport type { Fill, SceneNode, Stroke } from '../types'\nimport { paintBindingTarget, type PaintBindingList } from './fields'\nimport { collectionsResolving, getNodeVariableModeId } from './resolution'\nimport { subtreeNodes } from './subtree'\n\n/**\n * Figma keeps a bound paint's color and opacity as a cache of its variable: it draws and reads\n * what the paint holds, and writes the variable's color into it again only once the color the\n * variable gives that layer changes, so an edit to the paint sticks until then. The color each\n * binding last gave its paint lives here, beside the graph and never saved, by layer and binding\n * field. A layer without an entry, such as one just loaded, pasted, or restored, counts its\n * paint's own color as the last one applied.\n */\ninterface AppliedColor {\n readonly variableId: string\n readonly color: Color\n}\n\nconst appliedColors = new WeakMap<SceneNode, Map<string, AppliedColor>>()\n\nconst PAINT_LISTS = ['fills', 'strokes'] as const\n\ninterface BoundPaint {\n readonly field: string\n readonly list: PaintBindingList\n readonly index: number\n readonly variableId: string\n}\n\n/** The paints of `node` whose color is bound to a variable, each with a paint there to color. */\nfunction boundPaints(node: SceneNode): BoundPaint[] {\n const paints: BoundPaint[] = []\n for (const [field, variableId] of Object.entries(node.boundVariables)) {\n const target = paintBindingTarget(field)\n if (target && node[target.list].at(target.index)) paints.push({ field, ...target, variableId })\n }\n return paints\n}\n\n/** Whether a layer has a paint whose color is bound to a variable. */\nexport function hasBoundPaints(node: SceneNode): boolean {\n for (const field in node.boundVariables) if (paintBindingTarget(field)) return true\n return false\n}\n\n/** A paint's color with its opacity as alpha, as a variable's color carries it. */\nfunction paintColor(paint: Fill): Color {\n return { r: paint.color.r, g: paint.color.g, b: paint.color.b, a: paint.opacity }\n}\n\n/** Saved colors are single precision, so a color read back from a file still matches its source. */\nconst COLOR_TOLERANCE = 1e-6\n\nfunction sameColor(a: Color, b: Color): boolean {\n return (\n Math.abs(a.r - b.r) <= COLOR_TOLERANCE &&\n Math.abs(a.g - b.g) <= COLOR_TOLERANCE &&\n Math.abs(a.b - b.b) <= COLOR_TOLERANCE &&\n Math.abs(a.a - b.a) <= COLOR_TOLERANCE\n )\n}\n\nfunction lastApplied(node: SceneNode, bound: BoundPaint, paint: Fill): Color {\n const entry = appliedColors.get(node)?.get(bound.field)\n return entry?.variableId === bound.variableId ? entry.color : paintColor(paint)\n}\n\n/**\n * A layer copied from another keeps the colors its bindings last applied there, so the copy's\n * paints, edits included, stay as they are until their variables' colors for it change.\n */\nexport function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void {\n const entries = appliedColors.get(source)\n if (entries) appliedColors.set(copy, new Map(entries))\n}\n\nfunction recordApplied(node: SceneNode, bound: BoundPaint, color: Color): void {\n let entries = appliedColors.get(node)\n if (!entries) {\n entries = new Map()\n appliedColors.set(node, entries)\n }\n entries.set(bound.field, { variableId: bound.variableId, color: { ...color } })\n}\n\n/** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */\nfunction applyColor(paint: Fill, color: Color): void {\n paint.color = { ...color, a: 1 }\n paint.opacity = color.a\n}\n\n/** Whether `paint` already shows `color` as a bound paint stores it. */\nfunction showsColor(paint: Fill, color: Color): boolean {\n return sameColor(paintColor(paint), color) && paint.color.a === 1\n}\n\ntype PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>\n\n/**\n * Whether `node` is an instance, or a layer an instance copies from its component, with a paint\n * list neither it nor an instance around it overrides: such paints follow the component, and\n * refreshing them is no edit.\n */\nexport function copiesComponentPaints(\n graph: SceneGraph,\n node: SceneNode,\n list: PaintBindingList\n): boolean {\n const walk = { copied: false }\n const overriding = graph.closest(node.id, (current) => {\n if (current.type !== 'INSTANCE') return false\n walk.copied = true\n return hasInstanceOverride(current.instanceOverrides, current.id, node.id, list)\n })\n return walk.copied && !overriding\n}\n\n/**\n * Writes refreshed paints. A copy's paints that follow its component are derived values that a\n * save leaves to the component, not edits written over it.\n */\nfunction writePaints(graph: SceneGraph, node: SceneNode, changes: PaintLists): void {\n const derived = PAINT_LISTS.every(\n (list) => !changes[list] || copiesComponentPaints(graph, node, list)\n )\n const write = () => graph.updateNode(node.id, changes)\n if (derived) graph.applyComponentDerivedDuring(write)\n else write()\n}\n\n/**\n * Writes into each bound paint the color its variable gives the layer now, where that differs\n * from the color last applied, or wherever `forced` says, and records what each binding gives.\n */\nfunction refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) => boolean): void {\n let fills: Fill[] | undefined\n let strokes: Stroke[] | undefined\n for (const bound of boundPaints(node)) {\n const color = graph.resolveColorVariableForNode(node.id, bound.variableId)\n const paint = node[bound.list].at(bound.index)\n if (!color || !paint) continue\n const last = lastApplied(node, bound, paint)\n recordApplied(node, bound, color)\n if ((!forced(bound.field) && sameColor(last, color)) || showsColor(paint, color)) continue\n if (bound.list === 'fills') {\n fills ??= copyFills(node.fills)\n applyColor(fills[bound.index], color)\n } else {\n strokes ??= copyStrokes(node.strokes)\n applyColor(strokes[bound.index], color)\n }\n }\n const changes: PaintLists = {}\n if (fills) changes.fills = fills\n if (strokes) changes.strokes = strokes\n if (fills || strokes) writePaints(graph, node, changes)\n}\n\nconst NONE_FORCED = (): boolean => false\nconst ALL_FORCED = (): boolean => true\n\n/**\n * Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as\n * opacity, once that color differs from the one last applied, discarding edits made since, as\n * Figma refreshes them when a variable's value or a layer's mode changes.\n */\nexport function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void {\n if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED)\n}\n\n/** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */\nexport function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field: string): void {\n if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field)\n}\n\n/**\n * Gives every bound paint in this subtree its variable's color where it sits. A new instance\n * or a layer an instance sync adds is a copy, which shows its variables in its own context rather\n * than whatever its component's paints hold.\n */\nexport function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void {\n for (const node of subtreeNodes(graph, [rootId]))\n if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED)\n}\n\n/** Refreshes the bound paints in this subtree whose variables now give their layers another color. */\nexport function refreshBoundPaintsIn(graph: SceneGraph, rootId: string): void {\n for (const node of subtreeNodes(graph, [rootId])) refreshBoundPaints(graph, node)\n}\n\n/**\n * Records, for the paint lists a write sets, the colors their variables give the layer now, so a\n * paint written with another color or opacity keeps it until that color changes.\n */\nexport function recordWrittenBoundPaints(\n graph: SceneGraph,\n node: SceneNode,\n written: PaintLists\n): void {\n for (const bound of boundPaints(node)) {\n if (!written[bound.list]) continue\n const color = graph.resolveColorVariableForNode(node.id, bound.variableId)\n if (color) recordApplied(node, bound, color)\n }\n}\n\n/**\n * Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose\n * mode the move changed, as Figma does when a layer enters a frame or page in another mode. A\n * move between parents in the same modes, or of layers that bind no paint, costs no more than\n * comparing those modes.\n */\nexport function refreshMovedBoundPaints(\n graph: SceneGraph,\n nodeId: string,\n previousParentId: string | null | undefined\n): void {\n if (graph.isApplyingImportedState || graph.variableCollections.size === 0) return\n const parentId = graph.nodes.get(nodeId)?.parentId\n if (!parentId) return\n const previous = previousParentId && graph.nodes.has(previousParentId) ? previousParentId : null\n const changed = new Set(\n [...graph.variableCollections.keys()].filter(\n (id) =>\n !previous ||\n getNodeVariableModeId(graph, previous, id) !== getNodeVariableModeId(graph, parentId, id)\n )\n )\n if (changed.size === 0) return\n const reaches = new Map<string, boolean>()\n const reachesChanged = (variableId: string) => {\n let found = reaches.get(variableId)\n if (found === undefined) {\n found = [...collectionsResolving(graph, [variableId])].some((id) => changed.has(id))\n reaches.set(variableId, found)\n }\n return found\n }\n for (const node of subtreeNodes(graph, [nodeId])) {\n if (boundPaints(node).some((bound) => reachesChanged(bound.variableId)))\n refresh(graph, node, NONE_FORCED)\n }\n}\n\nfunction reboundPaint(\n target: SceneNode,\n bindings: Readonly<Record<string, string>>,\n list: PaintBindingList\n): boolean {\n return target[list].some((_, index) => {\n const field = `${list}/${index}/color`\n return field in bindings && bindings[field] !== target.boundVariables[field]\n })\n}\n\n/**\n * A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit\n * to its paint's color or opacity does not reach them. An instance sync writing a copy's paints\n * keeps the color and opacity of each one still bound to the same variable, and gives it the\n * variable's color for the copy once that differs from the one last applied, or when it binds\n * anew. `updates` holds what the sync writes, whose paints this sets in place.\n */\nexport function keepCopyBoundPaints(\n graph: SceneGraph,\n target: SceneNode,\n updates: Partial<SceneNode>\n): void {\n const bindings = updates.boundVariables ?? target.boundVariables\n for (const list of PAINT_LISTS) {\n // A paint the component binds anew keeps its own paint otherwise, but takes the new color.\n if (!updates[list] && reboundPaint(target, bindings, list))\n Object.assign(updates, {\n [list]: list === 'fills' ? copyFills(target.fills) : copyStrokes(target.strokes)\n })\n updates[list]?.forEach((paint, index) => {\n const field = `${list}/${index}/color`\n const variableId = bindings[field]\n if (!variableId) return\n const bound = { field, list, index, variableId }\n const own = target[list].at(index)\n const keeps = own !== undefined && target.boundVariables[field] === variableId\n if (keeps) {\n paint.color = { ...own.color }\n paint.opacity = own.opacity\n }\n const color = graph.resolveColorVariableForNode(target.id, variableId)\n if (!color) return\n if (!keeps || !sameColor(lastApplied(target, bound, own), color)) applyColor(paint, color)\n recordApplied(target, bound, color)\n })\n }\n}\n"],"mappings":";;;;;;AAsBA,MAAM,gCAAgB,IAAI,QAA8C;AAExE,MAAM,cAAc,CAAC,SAAS,SAAS;;AAUvC,SAAS,YAAY,MAA+B;CAClD,MAAM,SAAuB,CAAC;CAC9B,KAAK,MAAM,CAAC,OAAO,eAAe,OAAO,QAAQ,KAAK,cAAc,GAAG;EACrE,MAAM,SAAS,mBAAmB,KAAK;EACvC,IAAI,UAAU,KAAK,OAAO,KAAK,CAAC,GAAG,OAAO,KAAK,GAAG,OAAO,KAAK;GAAE;GAAO,GAAG;GAAQ;EAAW,CAAC;CAChG;CACA,OAAO;AACT;;AAGA,SAAgB,eAAe,MAA0B;CACvD,KAAK,MAAM,SAAS,KAAK,gBAAgB,IAAI,mBAAmB,KAAK,GAAG,OAAO;CAC/E,OAAO;AACT;;AAGA,SAAS,WAAW,OAAoB;CACtC,OAAO;EAAE,GAAG,MAAM,MAAM;EAAG,GAAG,MAAM,MAAM;EAAG,GAAG,MAAM,MAAM;EAAG,GAAG,MAAM;CAAQ;AAClF;;AAGA,MAAM,kBAAkB;AAExB,SAAS,UAAU,GAAU,GAAmB;CAC9C,OACE,KAAK,IAAI,EAAE,IAAI,EAAE,CAAC,KAAK,mBACvB,KAAK,IAAI,EAAE,IAAI,EAAE,CAAC,KAAK,mBACvB,KAAK,IAAI,EAAE,IAAI,EAAE,CAAC,KAAK,mBACvB,KAAK,IAAI,EAAE,IAAI,EAAE,CAAC,KAAK;AAE3B;AAEA,SAAS,YAAY,MAAiB,OAAmB,OAAoB;CAC3E,MAAM,QAAQ,cAAc,IAAI,IAAI,CAAC,EAAE,IAAI,MAAM,KAAK;CACtD,OAAO,OAAO,eAAe,MAAM,aAAa,MAAM,QAAQ,WAAW,KAAK;AAChF;;;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,MAAM,QAAQ,GAAG,KAAK,GAAG,MAAM;EAC/B,OAAO,SAAS,YAAY,SAAS,WAAW,OAAO,eAAe;CACxE,CAAC;AACH;;;;;;;;AASA,SAAgB,oBACd,OACA,QACA,SACM;CACN,MAAM,WAAW,QAAQ,kBAAkB,OAAO;CAClD,KAAK,MAAM,QAAQ,aAAa;EAE9B,IAAI,CAAC,QAAQ,SAAS,aAAa,QAAQ,UAAU,IAAI,GACvD,OAAO,OAAO,SAAS,GACpB,OAAO,SAAS,UAAU,UAAU,OAAO,KAAK,IAAI,YAAY,OAAO,OAAO,EACjF,CAAC;EACH,QAAQ,KAAK,EAAE,SAAS,OAAO,UAAU;GACvC,MAAM,QAAQ,GAAG,KAAK,GAAG,MAAM;GAC/B,MAAM,aAAa,SAAS;GAC5B,IAAI,CAAC,YAAY;GACjB,MAAM,QAAQ;IAAE;IAAO;IAAM;IAAO;GAAW;GAC/C,MAAM,MAAM,OAAO,KAAK,CAAC,GAAG,KAAK;GACjC,MAAM,QAAQ,QAAQ,KAAA,KAAa,OAAO,eAAe,WAAW;GACpE,IAAI,OAAO;IACT,MAAM,QAAQ,EAAE,GAAG,IAAI,MAAM;IAC7B,MAAM,UAAU,IAAI;GACtB;GACA,MAAM,QAAQ,MAAM,4BAA4B,OAAO,IAAI,UAAU;GACrE,IAAI,CAAC,OAAO;GACZ,IAAI,CAAC,SAAS,CAAC,UAAU,YAAY,QAAQ,OAAO,GAAG,GAAG,KAAK,GAAG,WAAW,OAAO,KAAK;GACzF,cAAc,QAAQ,OAAO,KAAK;EACpC,CAAC;CACH;AACF"}
@@ -1,15 +1,18 @@
1
1
  import { Color } from "../primitives/index.js";
2
- import { VariableFormulaError } from "./formula/tree.js";
3
2
  import { SceneGraph } from "../graph.js";
4
3
  import { Variable, VariableLiteral, VariableValue } from "../types.js";
4
+ import { VariableFormulaError } from "./formula/tree.js";
5
5
  //#region src/variables/resolution.d.ts
6
- declare function getActiveModeId(graph: SceneGraph, collectionId: string): string;
7
6
  /**
8
- * What a node without an explicit mode falls back to: the mode the editor shows (`active`), or
9
- * the collection's default (`default`), which is what a saved document means to other tools.
7
+ * The mode a node is in for a collection: the nearest explicit mode on it or an ancestor (the page
8
+ * included), else the collection's default, as Figma resolves it.
9
+ */
10
+ declare function getNodeVariableModeId(graph: SceneGraph, nodeId: string, collectionId: string): string;
11
+ /**
12
+ * The collections whose modes decide what these variables resolve to: their own, and those of the
13
+ * variables they alias, at any depth and in any mode.
10
14
  */
11
- type VariableModeFallback = 'active' | 'default';
12
- declare function getNodeVariableModeId(graph: SceneGraph, nodeId: string, collectionId: string, fallback?: VariableModeFallback): string;
15
+ declare function collectionsResolving(graph: SceneGraph, variableIds: Iterable<string>): Set<string>;
13
16
  declare function resolveVariable(graph: SceneGraph, variableId: string, modeId?: string, visited?: Set<string>): VariableLiteral | undefined;
14
17
  declare class VariableResolutionError extends VariableFormulaError {
15
18
  readonly code: 'cycle' | 'depth' | 'missing-variable' | 'missing-value' | 'type' | 'non-finite';
@@ -29,11 +32,11 @@ type NodeVariableResolver = (nodeId: string, variableId: string) => VariableLite
29
32
  declare function createBatchVariableResolver(graph: SceneGraph): NodeVariableResolver;
30
33
  declare function resolveColorVariable(graph: SceneGraph, variableId: string): Color | undefined;
31
34
  declare function resolveNumberVariable(graph: SceneGraph, variableId: string): number | undefined;
32
- declare function resolveColorVariableForNode(graph: SceneGraph, nodeId: string, variableId: string, fallback?: VariableModeFallback): Color | undefined;
33
- declare function resolveNumberVariableForNode(graph: SceneGraph, nodeId: string, variableId: string, fallback?: VariableModeFallback): number | undefined;
35
+ declare function resolveColorVariableForNode(graph: SceneGraph, nodeId: string, variableId: string): Color | undefined;
36
+ declare function resolveNumberVariableForNode(graph: SceneGraph, nodeId: string, variableId: string): number | undefined;
34
37
  /** A variable's value in the mode a node is in, aliases followed. */
35
- declare function resolveVariableForNode(graph: SceneGraph, nodeId: string, variableId: string, fallback?: VariableModeFallback): VariableLiteral | undefined;
36
- declare function resolveStringVariableForNode(graph: SceneGraph, nodeId: string, variableId: string, fallback?: VariableModeFallback): string | undefined;
38
+ declare function resolveVariableForNode(graph: SceneGraph, nodeId: string, variableId: string): VariableLiteral | undefined;
39
+ declare function resolveStringVariableForNode(graph: SceneGraph, nodeId: string, variableId: string): string | undefined;
37
40
  //#endregion
38
- export { NodeVariableResolver, VariableModeFallback, VariableResolutionError, createBatchVariableResolver, getActiveModeId, getNodeVariableModeId, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, variableValueInMode };
41
+ export { NodeVariableResolver, VariableResolutionError, collectionsResolving, createBatchVariableResolver, getNodeVariableModeId, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, variableValueInMode };
39
42
  //# sourceMappingURL=resolution.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolution.d.ts","names":[],"sources":["../../src/variables/resolution.ts"],"mappings":";;;;;iBAMgB,gBAAgB,OAAO,YAAY;;;;;KAWvC;iBAEI,sBACd,OAAO,YACP,gBACA,sBACA,WAAU;iBAaI,gBACd,OAAO,YACP,oBACA,iBACA,UAAU,cACT;cAaU,gCAAgC;WAGhC;WACA;EAHF;EAEE,YAAA,wFACA,oBACT;;;;;;iBAYY,uBACd,OAAO,YACP,oBACA,UAAU,iCACV,WAAQ,aACR,OAAO,YAAY,kBACnB,YAAY,oBAAoB,iBAC/B;;iBA2Ha,oBACd,OAAO,YACP,UAAU,UACV,iBACC;KAQS,wBACV,gBACA,uBACG;;iBAGW,4BAA4B,OAAO,aAAa;iBAqBhD,qBAAqB,OAAO,YAAY,qBAAqB;iBAM7D,sBAAsB,OAAO,YAAY;iBAKzC,4BACd,OAAO,YACP,gBACA,oBACA,WAAU,uBACT;iBAMa,6BACd,OAAO,YACP,gBACA,oBACA,WAAU;;iBAOI,uBACd,OAAO,YACP,gBACA,oBACA,WAAU,uBACT;iBAMa,6BACd,OAAO,YACP,gBACA,oBACA,WAAU"}
1
+ {"version":3,"file":"resolution.d.ts","names":[],"sources":["../../src/variables/resolution.ts"],"mappings":";;;;;;;;;iBAcgB,sBACd,OAAO,YACP,gBACA;;;;;iBAec,qBACd,OAAO,YACP,aAAa,mBACZ;iBAgBa,gBACd,OAAO,YACP,oBACA,iBACA,UAAU,cACT;cAaU,gCAAgC;WAGhC;WACA;EAHF;EAEE,YAAA,wFACA,oBACT;;;;;;iBAYY,uBACd,OAAO,YACP,oBACA,UAAU,iCACV,WAAQ,aACR,OAAO,YAAY,kBACnB,YAAY,oBAAoB,iBAC/B;;iBA2Ha,oBACd,OAAO,YACP,UAAU,UACV,iBACC;KAQS,wBACV,gBACA,uBACG;;iBAGW,4BAA4B,OAAO,aAAa;iBAqBhD,qBAAqB,OAAO,YAAY,qBAAqB;iBAM7D,sBAAsB,OAAO,YAAY;iBAKzC,4BACd,OAAO,YACP,gBACA,qBACC;iBAMa,6BACd,OAAO,YACP,gBACA;;iBAOc,uBACd,OAAO,YACP,gBACA,qBACC;iBAMa,6BACd,OAAO,YACP,gBACA"}
@@ -1,24 +1,43 @@
1
1
  import { VariableFormulaError } from "./formula/tree.js";
2
- import { resolveVariableValue } from "./value.js";
2
+ import { resolveVariableValue, variableValueReferences } from "./value.js";
3
3
  //#region src/variables/resolution.ts
4
- function getActiveModeId(graph, collectionId) {
5
- const mode = graph.activeMode.get(collectionId);
6
- if (mode) return mode;
4
+ function defaultModeId(graph, collectionId) {
7
5
  return graph.variableCollections.get(collectionId)?.defaultModeId ?? "";
8
6
  }
9
- function getNodeVariableModeId(graph, nodeId, collectionId, fallback = "active") {
7
+ /**
8
+ * The mode a node is in for a collection: the nearest explicit mode on it or an ancestor (the page
9
+ * included), else the collection's default, as Figma resolves it.
10
+ */
11
+ function getNodeVariableModeId(graph, nodeId, collectionId) {
10
12
  let node = graph.nodes.get(nodeId);
11
13
  while (node) {
12
14
  const modeId = node.variableModes[collectionId];
13
15
  if (modeId) return modeId;
14
16
  node = node.parentId ? graph.nodes.get(node.parentId) : void 0;
15
17
  }
16
- if (fallback === "default") return graph.variableCollections.get(collectionId)?.defaultModeId ?? "";
17
- return getActiveModeId(graph, collectionId);
18
+ return defaultModeId(graph, collectionId);
19
+ }
20
+ /**
21
+ * The collections whose modes decide what these variables resolve to: their own, and those of the
22
+ * variables they alias, at any depth and in any mode.
23
+ */
24
+ function collectionsResolving(graph, variableIds) {
25
+ const pending = [...variableIds];
26
+ const seen = /* @__PURE__ */ new Set();
27
+ const collections = /* @__PURE__ */ new Set();
28
+ for (let id = pending.pop(); id !== void 0; id = pending.pop()) {
29
+ if (seen.has(id)) continue;
30
+ seen.add(id);
31
+ const variable = graph.variables.get(id);
32
+ if (!variable) continue;
33
+ collections.add(variable.collectionId);
34
+ for (const value of Object.values(variable.valuesByMode)) pending.push(...variableValueReferences(value));
35
+ }
36
+ return collections;
18
37
  }
19
38
  function resolveVariable(graph, variableId, modeId, visited) {
20
39
  const initial = graph.variables.get(variableId);
21
- return resolveInModes(graph, variableId, (collectionId) => modeId && initial?.collectionId === collectionId ? modeId : getActiveModeId(graph, collectionId), visited);
40
+ return resolveInModes(graph, variableId, (collectionId) => modeId && initial?.collectionId === collectionId ? modeId : defaultModeId(graph, collectionId), visited);
22
41
  }
23
42
  var VariableResolutionError = class extends VariableFormulaError {
24
43
  code;
@@ -132,23 +151,23 @@ function resolveNumberVariable(graph, variableId) {
132
151
  const value = resolveVariable(graph, variableId);
133
152
  return typeof value === "number" ? value : void 0;
134
153
  }
135
- function resolveColorVariableForNode(graph, nodeId, variableId, fallback = "active") {
136
- const value = resolveVariableForNode(graph, nodeId, variableId, fallback);
154
+ function resolveColorVariableForNode(graph, nodeId, variableId) {
155
+ const value = resolveVariableForNode(graph, nodeId, variableId);
137
156
  if (value && typeof value === "object" && "r" in value) return value;
138
157
  }
139
- function resolveNumberVariableForNode(graph, nodeId, variableId, fallback = "active") {
140
- const value = resolveVariableForNode(graph, nodeId, variableId, fallback);
158
+ function resolveNumberVariableForNode(graph, nodeId, variableId) {
159
+ const value = resolveVariableForNode(graph, nodeId, variableId);
141
160
  return typeof value === "number" ? value : void 0;
142
161
  }
143
162
  /** A variable's value in the mode a node is in, aliases followed. */
144
- function resolveVariableForNode(graph, nodeId, variableId, fallback = "active") {
145
- return resolveInModes(graph, variableId, (collectionId) => getNodeVariableModeId(graph, nodeId, collectionId, fallback));
163
+ function resolveVariableForNode(graph, nodeId, variableId) {
164
+ return resolveInModes(graph, variableId, (collectionId) => getNodeVariableModeId(graph, nodeId, collectionId));
146
165
  }
147
- function resolveStringVariableForNode(graph, nodeId, variableId, fallback = "active") {
148
- const value = resolveVariableForNode(graph, nodeId, variableId, fallback);
166
+ function resolveStringVariableForNode(graph, nodeId, variableId) {
167
+ const value = resolveVariableForNode(graph, nodeId, variableId);
149
168
  return typeof value === "string" ? value : void 0;
150
169
  }
151
170
  //#endregion
152
- export { VariableResolutionError, createBatchVariableResolver, getActiveModeId, getNodeVariableModeId, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, variableValueInMode };
171
+ export { VariableResolutionError, collectionsResolving, createBatchVariableResolver, getNodeVariableModeId, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, variableValueInMode };
153
172
 
154
173
  //# sourceMappingURL=resolution.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolution.js","names":[],"sources":["../../src/variables/resolution.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { Color } from '../primitives'\nimport type { Variable, VariableLiteral, VariableValue } from '../types'\nimport { MAX_FORMULA_DEPTH, VariableFormulaError } from './formula/tree'\nimport { resolveVariableValue } from './value'\n\nexport function getActiveModeId(graph: SceneGraph, collectionId: string): string {\n const mode = graph.activeMode.get(collectionId)\n if (mode) return mode\n const collection = graph.variableCollections.get(collectionId)\n return collection?.defaultModeId ?? ''\n}\n\n/**\n * What a node without an explicit mode falls back to: the mode the editor shows (`active`), or\n * the collection's default (`default`), which is what a saved document means to other tools.\n */\nexport type VariableModeFallback = 'active' | 'default'\n\nexport function getNodeVariableModeId(\n graph: SceneGraph,\n nodeId: string,\n collectionId: string,\n fallback: VariableModeFallback = 'active'\n): string {\n let node = graph.nodes.get(nodeId)\n while (node) {\n const modeId = node.variableModes[collectionId]\n if (modeId) return modeId\n node = node.parentId ? graph.nodes.get(node.parentId) : undefined\n }\n if (fallback === 'default')\n return graph.variableCollections.get(collectionId)?.defaultModeId ?? ''\n return getActiveModeId(graph, collectionId)\n}\n\nexport function resolveVariable(\n graph: SceneGraph,\n variableId: string,\n modeId?: string,\n visited?: Set<string>\n): VariableLiteral | undefined {\n const initial = graph.variables.get(variableId)\n return resolveInModes(\n graph,\n variableId,\n (collectionId) =>\n modeId && initial?.collectionId === collectionId\n ? modeId\n : getActiveModeId(graph, collectionId),\n visited\n )\n}\n\nexport class VariableResolutionError extends VariableFormulaError {\n override name = 'VariableResolutionError'\n constructor(\n readonly code: 'cycle' | 'depth' | 'missing-variable' | 'missing-value' | 'type' | 'non-finite',\n readonly variableId: string,\n message: string\n ) {\n super(message)\n }\n}\n\nconst RESULT_TYPES = { FLOAT: 'number', BOOLEAN: 'boolean', STRING: 'string', COLOR: 'object' }\n\n/**\n * Resolve a draft or saved definition with identical mode, dependency and error semantics. A\n * memo shares values between lookups in one mode context; without one, nothing is cached.\n */\nexport function resolveVariableInModes(\n graph: SceneGraph,\n variableId: string,\n modeFor: (collectionId: string) => string,\n visiting = new Set<string>(),\n memo?: Map<string, VariableLiteral>,\n overrides?: ReadonlyMap<string, VariableValue>\n): VariableLiteral {\n const cached = memo?.get(variableId)\n if (cached !== undefined) return cached\n if (visiting.has(variableId))\n throw new VariableResolutionError('cycle', variableId, 'Circular variable reference')\n if (visiting.size >= MAX_FORMULA_DEPTH)\n throw new VariableResolutionError('depth', variableId, 'Too many linked variables')\n const variable = graph.variables.get(variableId)\n if (!variable)\n throw new VariableResolutionError(\n 'missing-variable',\n variableId,\n `Missing variable: ${variableId}`\n )\n const value =\n overrides?.get(variableId) ??\n variableValueInMode(graph, variable, modeFor(variable.collectionId))\n if (value === undefined)\n throw new VariableResolutionError(\n 'missing-value',\n variableId,\n `Missing value: ${variable.name}`\n )\n return evaluateDefinition(graph, variable, value, modeFor, visiting, memo, overrides)\n}\n\n/** Resolve one variable's definition in its mode context, its dependencies in theirs. */\nfunction evaluateDefinition(\n graph: SceneGraph,\n variable: Variable,\n value: VariableValue,\n modeFor: (collectionId: string) => string,\n visiting: Set<string>,\n memo?: Map<string, VariableLiteral>,\n overrides?: ReadonlyMap<string, VariableValue>\n): VariableLiteral {\n const variableId = variable.id\n visiting.add(variableId)\n try {\n const result = resolveVariableValue(value, (id) =>\n resolveVariableInModes(graph, id, modeFor, visiting, memo, overrides)\n )\n if (result === undefined || typeof result !== RESULT_TYPES[variable.type])\n throw new VariableResolutionError('type', variableId, `Wrong result type: ${variable.name}`)\n if (typeof result === 'number' && !Number.isFinite(result))\n throw new VariableResolutionError(\n 'non-finite',\n variableId,\n `Non-finite value: ${variable.name}`\n )\n // Failures can depend on the current path's depth. Only completed values are reusable.\n memo?.set(variableId, result)\n return result\n } finally {\n visiting.delete(variableId)\n }\n}\n\n/**\n * What the canvas draws for a variable, or nothing when it cannot be resolved. Literals and alias\n * chains, which most bindings are, resolve in a loop; an expression on the way takes the full\n * resolver, whose diagnostics the canvas does not need.\n */\nfunction resolveInModes(\n graph: SceneGraph,\n variableId: string,\n modeFor: (collectionId: string) => string,\n visiting?: Set<string>,\n memo?: Map<string, VariableLiteral>\n): VariableLiteral | undefined {\n const cached = memo?.get(variableId)\n if (cached !== undefined) return cached\n const initial = graph.variables.get(variableId)\n if (!initial) return undefined\n let id = variableId\n for (let depth = visiting?.size ?? 0; depth < MAX_FORMULA_DEPTH; depth++) {\n const variable = id === variableId ? initial : graph.variables.get(id)\n if (!variable || visiting?.has(id)) return undefined\n const value = variableValueInMode(graph, variable, modeFor(variable.collectionId))\n if (typeof value === 'object' && 'aliasId' in value) {\n id = value.aliasId\n continue\n }\n const result =\n typeof value === 'object' && 'expression' in value\n ? resolveExpression(graph, variable, value, modeFor, visiting, memo)\n : value\n return drawable(result, initial.type)\n }\n return undefined\n}\n\n/** A formula shares its dependencies' values for the length of one resolution. */\nfunction resolveExpression(\n graph: SceneGraph,\n variable: Variable,\n value: VariableValue,\n modeFor: (collectionId: string) => string,\n visiting?: Set<string>,\n memo?: Map<string, VariableLiteral>\n): VariableLiteral | undefined {\n try {\n return evaluateDefinition(\n graph,\n variable,\n value,\n modeFor,\n visiting ?? new Set(),\n memo ?? new Map()\n )\n } catch {\n return undefined\n }\n}\n\n/** A resolved value the canvas can draw for a variable of this type, or nothing. */\nfunction drawable(value: VariableValue | undefined, type: Variable['type']) {\n if (value === undefined || typeof value !== RESULT_TYPES[type]) return undefined\n if (typeof value === 'object' && !('r' in value)) return undefined\n return typeof value === 'number' && !Number.isFinite(value) ? undefined : value\n}\n\n/** Shared mode fallback for canvas resolution and formula draft validation. */\nexport function variableValueInMode(\n graph: SceneGraph,\n variable: Variable,\n modeId: string\n): VariableValue | undefined {\n const values = variable.valuesByMode\n if (Object.hasOwn(values, modeId)) return values[modeId]\n const defaultMode = graph.variableCollections.get(variable.collectionId)?.defaultModeId ?? ''\n if (Object.hasOwn(values, defaultMode)) return values[defaultMode]\n return Object.values(values).at(0)\n}\n\nexport type NodeVariableResolver = (\n nodeId: string,\n variableId: string\n) => VariableLiteral | undefined\n\n/** One read/reconcile operation only: equal mode contexts share results, edits start a new cache. */\nexport function createBatchVariableResolver(graph: SceneGraph): NodeVariableResolver {\n const collections = [...graph.variableCollections.keys()]\n const contexts = new Map<string, Map<string, VariableLiteral>>()\n return (nodeId, variableId) => {\n const modes = collections.map((id) => getNodeVariableModeId(graph, nodeId, id))\n const key = JSON.stringify(modes)\n let memo = contexts.get(key)\n if (!memo) {\n memo = new Map()\n contexts.set(key, memo)\n }\n return resolveInModes(\n graph,\n variableId,\n (id) => modes[collections.indexOf(id)] ?? '',\n undefined,\n memo\n )\n }\n}\n\nexport function resolveColorVariable(graph: SceneGraph, variableId: string): Color | undefined {\n const value = resolveVariable(graph, variableId)\n if (value && typeof value === 'object' && 'r' in value) return value\n return undefined\n}\n\nexport function resolveNumberVariable(graph: SceneGraph, variableId: string): number | undefined {\n const value = resolveVariable(graph, variableId)\n return typeof value === 'number' ? value : undefined\n}\n\nexport function resolveColorVariableForNode(\n graph: SceneGraph,\n nodeId: string,\n variableId: string,\n fallback: VariableModeFallback = 'active'\n): Color | undefined {\n const value = resolveVariableForNode(graph, nodeId, variableId, fallback)\n if (value && typeof value === 'object' && 'r' in value) return value\n return undefined\n}\n\nexport function resolveNumberVariableForNode(\n graph: SceneGraph,\n nodeId: string,\n variableId: string,\n fallback: VariableModeFallback = 'active'\n): number | undefined {\n const value = resolveVariableForNode(graph, nodeId, variableId, fallback)\n return typeof value === 'number' ? value : undefined\n}\n\n/** A variable's value in the mode a node is in, aliases followed. */\nexport function resolveVariableForNode(\n graph: SceneGraph,\n nodeId: string,\n variableId: string,\n fallback: VariableModeFallback = 'active'\n): VariableLiteral | undefined {\n return resolveInModes(graph, variableId, (collectionId) =>\n getNodeVariableModeId(graph, nodeId, collectionId, fallback)\n )\n}\n\nexport function resolveStringVariableForNode(\n graph: SceneGraph,\n nodeId: string,\n variableId: string,\n fallback: VariableModeFallback = 'active'\n): string | undefined {\n const value = resolveVariableForNode(graph, nodeId, variableId, fallback)\n return typeof value === 'string' ? value : undefined\n}\n"],"mappings":";;;AAMA,SAAgB,gBAAgB,OAAmB,cAA8B;CAC/E,MAAM,OAAO,MAAM,WAAW,IAAI,YAAY;CAC9C,IAAI,MAAM,OAAO;CAEjB,OADmB,MAAM,oBAAoB,IAAI,YACjC,CAAC,EAAE,iBAAi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EACA,OAAO,eACL,OACA,aACC,OAAO,MAAM,YAAY,QAAQ,EAAE,MAAM,IAC1C,KAAA,GACA,IACF;CACF;AACF;AAEA,SAAgB,qBAAqB,OAAmB,YAAuC;CAC7F,MAAM,QAAQ,gBAAgB,OAAO,UAAU;CAC/C,IAAI,SAAS,OAAO,UAAU,YAAY,OAAO,OAAO,OAAO;AAEjE;AAEA,SAAgB,sBAAsB,OAAmB,YAAwC;CAC/F,MAAM,QAAQ,gBAAgB,OAAO,UAAU;CAC/C,OAAO,OAAO,UAAU,WAAW,QAAQ,KAAA;AAC7C;AAEA,SAAgB,4BACd,OACA,QACA,YACA,WAAiC,UACd;CACnB,MAAM,QAAQ,uBAAuB,OAAO,QAAQ,YAAY,QAAQ;CACxE,IAAI,SAAS,OAAO,UAAU,YAAY,OAAO,OAAO,OAAO;AAEjE;AAEA,SAAgB,6BACd,OACA,QACA,YACA,WAAiC,UACb;CACpB,MAAM,QAAQ,uBAAuB,OAAO,QAAQ,YAAY,QAAQ;CACxE,OAAO,OAAO,UAAU,WAAW,QAAQ,KAAA;AAC7C;;AAGA,SAAgB,uBACd,OACA,QACA,YACA,WAAiC,UACJ;CAC7B,OAAO,eAAe,OAAO,aAAa,iBACxC,sBAAsB,OAAO,QAAQ,cAAc,QAAQ,CAC7D;AACF;AAEA,SAAgB,6BACd,OACA,QACA,YACA,WAAiC,UACb;CACpB,MAAM,QAAQ,uBAAuB,OAAO,QAAQ,YAAY,QAAQ;CACxE,OAAO,OAAO,UAAU,WAAW,QAAQ,KAAA;AAC7C"}
1
+ {"version":3,"file":"resolution.js","names":[],"sources":["../../src/variables/resolution.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { Color } from '../primitives'\nimport type { Variable, VariableLiteral, VariableValue } from '../types'\nimport { MAX_FORMULA_DEPTH, VariableFormulaError } from './formula/tree'\nimport { resolveVariableValue, variableValueReferences } from './value'\n\nfunction defaultModeId(graph: SceneGraph, collectionId: string): string {\n return graph.variableCollections.get(collectionId)?.defaultModeId ?? ''\n}\n\n/**\n * The mode a node is in for a collection: the nearest explicit mode on it or an ancestor (the page\n * included), else the collection's default, as Figma resolves it.\n */\nexport function getNodeVariableModeId(\n graph: SceneGraph,\n nodeId: string,\n collectionId: string\n): string {\n let node = graph.nodes.get(nodeId)\n while (node) {\n const modeId = node.variableModes[collectionId]\n if (modeId) return modeId\n node = node.parentId ? graph.nodes.get(node.parentId) : undefined\n }\n return defaultModeId(graph, collectionId)\n}\n\n/**\n * The collections whose modes decide what these variables resolve to: their own, and those of the\n * variables they alias, at any depth and in any mode.\n */\nexport function collectionsResolving(\n graph: SceneGraph,\n variableIds: Iterable<string>\n): Set<string> {\n const pending = [...variableIds]\n const seen = new Set<string>()\n const collections = new Set<string>()\n for (let id = pending.pop(); id !== undefined; id = pending.pop()) {\n if (seen.has(id)) continue\n seen.add(id)\n const variable = graph.variables.get(id)\n if (!variable) continue\n collections.add(variable.collectionId)\n for (const value of Object.values(variable.valuesByMode))\n pending.push(...variableValueReferences(value))\n }\n return collections\n}\n\nexport function resolveVariable(\n graph: SceneGraph,\n variableId: string,\n modeId?: string,\n visited?: Set<string>\n): VariableLiteral | undefined {\n const initial = graph.variables.get(variableId)\n return resolveInModes(\n graph,\n variableId,\n (collectionId) =>\n modeId && initial?.collectionId === collectionId\n ? modeId\n : defaultModeId(graph, collectionId),\n visited\n )\n}\n\nexport class VariableResolutionError extends VariableFormulaError {\n override name = 'VariableResolutionError'\n constructor(\n readonly code: 'cycle' | 'depth' | 'missing-variable' | 'missing-value' | 'type' | 'non-finite',\n readonly variableId: string,\n message: string\n ) {\n super(message)\n }\n}\n\nconst RESULT_TYPES = { FLOAT: 'number', BOOLEAN: 'boolean', STRING: 'string', COLOR: 'object' }\n\n/**\n * Resolve a draft or saved definition with identical mode, dependency and error semantics. A\n * memo shares values between lookups in one mode context; without one, nothing is cached.\n */\nexport function resolveVariableInModes(\n graph: SceneGraph,\n variableId: string,\n modeFor: (collectionId: string) => string,\n visiting = new Set<string>(),\n memo?: Map<string, VariableLiteral>,\n overrides?: ReadonlyMap<string, VariableValue>\n): VariableLiteral {\n const cached = memo?.get(variableId)\n if (cached !== undefined) return cached\n if (visiting.has(variableId))\n throw new VariableResolutionError('cycle', variableId, 'Circular variable reference')\n if (visiting.size >= MAX_FORMULA_DEPTH)\n throw new VariableResolutionError('depth', variableId, 'Too many linked variables')\n const variable = graph.variables.get(variableId)\n if (!variable)\n throw new VariableResolutionError(\n 'missing-variable',\n variableId,\n `Missing variable: ${variableId}`\n )\n const value =\n overrides?.get(variableId) ??\n variableValueInMode(graph, variable, modeFor(variable.collectionId))\n if (value === undefined)\n throw new VariableResolutionError(\n 'missing-value',\n variableId,\n `Missing value: ${variable.name}`\n )\n return evaluateDefinition(graph, variable, value, modeFor, visiting, memo, overrides)\n}\n\n/** Resolve one variable's definition in its mode context, its dependencies in theirs. */\nfunction evaluateDefinition(\n graph: SceneGraph,\n variable: Variable,\n value: VariableValue,\n modeFor: (collectionId: string) => string,\n visiting: Set<string>,\n memo?: Map<string, VariableLiteral>,\n overrides?: ReadonlyMap<string, VariableValue>\n): VariableLiteral {\n const variableId = variable.id\n visiting.add(variableId)\n try {\n const result = resolveVariableValue(value, (id) =>\n resolveVariableInModes(graph, id, modeFor, visiting, memo, overrides)\n )\n if (result === undefined || typeof result !== RESULT_TYPES[variable.type])\n throw new VariableResolutionError('type', variableId, `Wrong result type: ${variable.name}`)\n if (typeof result === 'number' && !Number.isFinite(result))\n throw new VariableResolutionError(\n 'non-finite',\n variableId,\n `Non-finite value: ${variable.name}`\n )\n // Failures can depend on the current path's depth. Only completed values are reusable.\n memo?.set(variableId, result)\n return result\n } finally {\n visiting.delete(variableId)\n }\n}\n\n/**\n * What the canvas draws for a variable, or nothing when it cannot be resolved. Literals and alias\n * chains, which most bindings are, resolve in a loop; an expression on the way takes the full\n * resolver, whose diagnostics the canvas does not need.\n */\nfunction resolveInModes(\n graph: SceneGraph,\n variableId: string,\n modeFor: (collectionId: string) => string,\n visiting?: Set<string>,\n memo?: Map<string, VariableLiteral>\n): VariableLiteral | undefined {\n const cached = memo?.get(variableId)\n if (cached !== undefined) return cached\n const initial = graph.variables.get(variableId)\n if (!initial) return undefined\n let id = variableId\n for (let depth = visiting?.size ?? 0; depth < MAX_FORMULA_DEPTH; depth++) {\n const variable = id === variableId ? initial : graph.variables.get(id)\n if (!variable || visiting?.has(id)) return undefined\n const value = variableValueInMode(graph, variable, modeFor(variable.collectionId))\n if (typeof value === 'object' && 'aliasId' in value) {\n id = value.aliasId\n continue\n }\n const result =\n typeof value === 'object' && 'expression' in value\n ? resolveExpression(graph, variable, value, modeFor, visiting, memo)\n : value\n return drawable(result, initial.type)\n }\n return undefined\n}\n\n/** A formula shares its dependencies' values for the length of one resolution. */\nfunction resolveExpression(\n graph: SceneGraph,\n variable: Variable,\n value: VariableValue,\n modeFor: (collectionId: string) => string,\n visiting?: Set<string>,\n memo?: Map<string, VariableLiteral>\n): VariableLiteral | undefined {\n try {\n return evaluateDefinition(\n graph,\n variable,\n value,\n modeFor,\n visiting ?? new Set(),\n memo ?? new Map()\n )\n } catch {\n return undefined\n }\n}\n\n/** A resolved value the canvas can draw for a variable of this type, or nothing. */\nfunction drawable(value: VariableValue | undefined, type: Variable['type']) {\n if (value === undefined || typeof value !== RESULT_TYPES[type]) return undefined\n if (typeof value === 'object' && !('r' in value)) return undefined\n return typeof value === 'number' && !Number.isFinite(value) ? undefined : value\n}\n\n/** Shared mode fallback for canvas resolution and formula draft validation. */\nexport function variableValueInMode(\n graph: SceneGraph,\n variable: Variable,\n modeId: string\n): VariableValue | undefined {\n const values = variable.valuesByMode\n if (Object.hasOwn(values, modeId)) return values[modeId]\n const defaultMode = graph.variableCollections.get(variable.collectionId)?.defaultModeId ?? ''\n if (Object.hasOwn(values, defaultMode)) return values[defaultMode]\n return Object.values(values).at(0)\n}\n\nexport type NodeVariableResolver = (\n nodeId: string,\n variableId: string\n) => VariableLiteral | undefined\n\n/** One read/reconcile operation only: equal mode contexts share results, edits start a new cache. */\nexport function createBatchVariableResolver(graph: SceneGraph): NodeVariableResolver {\n const collections = [...graph.variableCollections.keys()]\n const contexts = new Map<string, Map<string, VariableLiteral>>()\n return (nodeId, variableId) => {\n const modes = collections.map((id) => getNodeVariableModeId(graph, nodeId, id))\n const key = JSON.stringify(modes)\n let memo = contexts.get(key)\n if (!memo) {\n memo = new Map()\n contexts.set(key, memo)\n }\n return resolveInModes(\n graph,\n variableId,\n (id) => modes[collections.indexOf(id)] ?? '',\n undefined,\n memo\n )\n }\n}\n\nexport function resolveColorVariable(graph: SceneGraph, variableId: string): Color | undefined {\n const value = resolveVariable(graph, variableId)\n if (value && typeof value === 'object' && 'r' in value) return value\n return undefined\n}\n\nexport function resolveNumberVariable(graph: SceneGraph, variableId: string): number | undefined {\n const value = resolveVariable(graph, variableId)\n return typeof value === 'number' ? value : undefined\n}\n\nexport function resolveColorVariableForNode(\n graph: SceneGraph,\n nodeId: string,\n variableId: string\n): Color | undefined {\n const value = resolveVariableForNode(graph, nodeId, variableId)\n if (value && typeof value === 'object' && 'r' in value) return value\n return undefined\n}\n\nexport function resolveNumberVariableForNode(\n graph: SceneGraph,\n nodeId: string,\n variableId: string\n): number | undefined {\n const value = resolveVariableForNode(graph, nodeId, variableId)\n return typeof value === 'number' ? value : undefined\n}\n\n/** A variable's value in the mode a node is in, aliases followed. */\nexport function resolveVariableForNode(\n graph: SceneGraph,\n nodeId: string,\n variableId: string\n): VariableLiteral | undefined {\n return resolveInModes(graph, variableId, (collectionId) =>\n getNodeVariableModeId(graph, nodeId, collectionId)\n )\n}\n\nexport function resolveStringVariableForNode(\n graph: SceneGraph,\n nodeId: string,\n variableId: string\n): string | undefined {\n const value = resolveVariableForNode(graph, nodeId, variableId)\n return typeof value === 'string' ? value : undefined\n}\n"],"mappings":";;;AAMA,SAAS,cAAc,OAAmB,cAA8B;CACtE,OAAO,MAAM,oBAAoB,IAAI,YAAY,CAAC,EAAE,iBAAiB;AACvE;;;;;AA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AAO,OAAO,UAAU,WAAW,QAAQ,KAAA;AAC7C"}
@@ -0,0 +1,17 @@
1
+ //#region src/variables/subtree.ts
2
+ /** The layers in these subtrees, the roots included, that a binding change can reach. */
3
+ function subtreeNodes(graph, roots) {
4
+ const nodes = [];
5
+ const pending = [...roots];
6
+ for (let id = pending.pop(); id !== void 0; id = pending.pop()) {
7
+ const node = graph.getNode(id);
8
+ if (!node) continue;
9
+ nodes.push(node);
10
+ pending.push(...node.childIds);
11
+ }
12
+ return nodes;
13
+ }
14
+ //#endregion
15
+ export { subtreeNodes };
16
+
17
+ //# sourceMappingURL=subtree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subtree.js","names":[],"sources":["../../src/variables/subtree.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { SceneNode } from '../types'\n\n/** The layers in these subtrees, the roots included, that a binding change can reach. */\nexport function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneNode[] {\n const nodes: SceneNode[] = []\n const pending = [...roots]\n for (let id = pending.pop(); id !== undefined; id = pending.pop()) {\n const node = graph.getNode(id)\n if (!node) continue\n nodes.push(node)\n pending.push(...node.childIds)\n }\n return nodes\n}\n"],"mappings":";;AAIA,SAAgB,aAAa,OAAmB,OAAsC;CACpF,MAAM,QAAqB,CAAC;CAC5B,MAAM,UAAU,CAAC,GAAG,KAAK;CACzB,KAAK,IAAI,KAAK,QAAQ,IAAI,GAAG,OAAO,KAAA,GAAW,KAAK,QAAQ,IAAI,GAAG;EACjE,MAAM,OAAO,MAAM,QAAQ,EAAE;EAC7B,IAAI,CAAC,MAAM;EACX,MAAM,KAAK,IAAI;EACf,QAAQ,KAAK,GAAG,KAAK,QAAQ;CAC/B;CACA,OAAO;AACT"}
@@ -1 +1 @@
1
- {"version":3,"file":"vector-network.d.ts","names":[],"sources":["../src/vector-network.ts"],"mappings":";;;;;iBAgBgB,oBAAoB,mBAAmB,kBAAkB;;;;;;iBAuCzD,uBAAuB,GAAG,MAAM,IAAI,gBAAgB;;iBAqBpD,eAAe,SAAS;;;;;iBAQxB,2BACd,uBAAuB,gBACvB,SAAS;;;;;iBASK,kBACd,SAAS,sBACT,qBAAqB;;;;;iBAmBP,kBACd,MAAM,WACN,SAAS,QAAQ,aAChB,KAAK,QAAQ;;iBAQA,YAAY,SAAS,uBAAuB;;iBAK5C,oBAAoB,GAAG,eAAe,GAAG;;;;;iBAYzC,sBAAsB;KA6K1B,4BAA4B,KAAK;EAC3C,UAAU;;;;;;;iBAQI,uBAAuB,IAAI,4BAA4B;iBAwJvD,wBACd,SAAS,eACT;;iBAkBc,eAAe,SAAS,eAAe;;iBAQvC,gBAAgB,UAAU,WAAW,gBAAgB;;UAMpD;EACf,aAAa;EACb;;;;;;iBAOc,mBAAmB,SAAS,gBAAgB"}
1
+ {"version":3,"file":"vector-network.d.ts","names":[],"sources":["../src/vector-network.ts"],"mappings":";;;;;iBAgBgB,oBAAoB,mBAAmB,kBAAkB;;;;;;iBAuCzD,uBAAuB,GAAG,MAAM,IAAI,gBAAgB;;iBAqBpD,eAAe,SAAS;;;;;iBAQxB,2BACd,uBAAuB,gBACvB,SAAS;;;;;iBASK,kBACd,SAAS,sBACT,qBAAqB;;;;;iBAmBP,kBACd,MAAM,WACN,SAAS,QAAQ,aAChB,KAAK,QAAQ;;iBAQA,YAAY,SAAS,uBAAuB;;iBAK5C,oBAAoB,GAAG,eAAe,GAAG;;;;;iBAYzC,sBAAsB;KA6K1B,4BAA4B,KAAK;EAC3C,UAAU;;;;;;;iBAQI,uBAAuB,IAAI,4BAA4B;iBA0JvD,wBACd,SAAS,eACT;;iBAkBc,eAAe,SAAS,eAAe;;iBAQvC,gBAAgB,UAAU,WAAW,gBAAgB;;UAMpD;EACf,aAAa;EACb;;;;;;iBAOc,mBAAmB,SAAS,gBAAgB"}
@@ -239,14 +239,8 @@ function coordinate(value, decimals) {
239
239
  function segmentToSVG(seg, vertices, forward, decimals) {
240
240
  const start = forward ? vertices[seg.start] : vertices[seg.end];
241
241
  const end = forward ? vertices[seg.end] : vertices[seg.start];
242
- const ts = forward ? seg.tangentStart : {
243
- x: -seg.tangentEnd.x,
244
- y: -seg.tangentEnd.y
245
- };
246
- const te = forward ? seg.tangentEnd : {
247
- x: -seg.tangentStart.x,
248
- y: -seg.tangentStart.y
249
- };
242
+ const ts = forward ? seg.tangentStart : seg.tangentEnd;
243
+ const te = forward ? seg.tangentEnd : seg.tangentStart;
250
244
  if (Math.abs(ts.x) < .001 && Math.abs(ts.y) < .001 && Math.abs(te.x) < .001 && Math.abs(te.y) < .001) return `L${coordinate(end.x, decimals)} ${coordinate(end.y, decimals)}`;
251
245
  return `C${coordinate(start.x + ts.x, decimals)} ${coordinate(start.y + ts.y, decimals)} ${coordinate(end.x + te.x, decimals)} ${coordinate(end.y + te.y, decimals)} ${coordinate(end.x, decimals)} ${coordinate(end.y, decimals)}`;
252
246
  }