@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
package/src/geometry.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { arrowCapOverflow } from './arrow-caps'
2
2
  import type { Rect, Vector } from './primitives'
3
- import { textLinesOverflow } from './text/overflow'
4
- import type { Effect, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'
3
+ import type { Effect, SceneNode, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'
4
+
5
+ export { cubicPoint } from './cubic'
5
6
 
6
7
  export function degToRad(degrees: number): number {
7
8
  return (degrees * Math.PI) / 180
@@ -94,15 +95,18 @@ function includeRect(bounds: BoundsAccumulator, rect: Rect): void {
94
95
  includePoint(bounds, rect.x + rect.width, rect.y + rect.height)
95
96
  }
96
97
 
98
+ /** `bounds` as a rect at its top-left corner. */
99
+ export function visualBoundsToRect(bounds: VisualBounds): Rect {
100
+ return {
101
+ x: bounds.minX,
102
+ y: bounds.minY,
103
+ width: bounds.maxX - bounds.minX,
104
+ height: bounds.maxY - bounds.minY
105
+ }
106
+ }
107
+
97
108
  function boundsToRect(bounds: BoundsAccumulator): Rect {
98
- return bounds.minX === Infinity
99
- ? { x: 0, y: 0, width: 0, height: 0 }
100
- : {
101
- x: bounds.minX,
102
- y: bounds.minY,
103
- width: bounds.maxX - bounds.minX,
104
- height: bounds.maxY - bounds.minY
105
- }
109
+ return bounds.minX === Infinity ? { x: 0, y: 0, width: 0, height: 0 } : visualBoundsToRect(bounds)
106
110
  }
107
111
 
108
112
  export function computeBounds(items: Iterable<Rect>): Rect {
@@ -216,6 +220,38 @@ function miterRatio(network: VectorNetwork, join: StrokeJoin | undefined, limit
216
220
  return ratio
217
221
  }
218
222
 
223
+ /**
224
+ * The blur radius Figma draws: a negative one, which only a bound variable can give, draws as no
225
+ * blur, though the layer keeps the value.
226
+ */
227
+ export function drawnBlurRadius(radius: number): number {
228
+ return Math.max(0, radius)
229
+ }
230
+
231
+ /**
232
+ * The widest radius an effect blurs by: its radius, or the larger end of a progressive blur,
233
+ * which can start wider than it ends. Bounds and padding that must hold the whole blur use it.
234
+ */
235
+ export function widestBlurRadius(effect: Pick<Effect, 'radius' | 'progressive'>): number {
236
+ return Math.max(effect.radius, effect.progressive?.startRadius ?? effect.radius)
237
+ }
238
+
239
+ const BLUR_SIGMA_PER_RADIUS = 0.43
240
+
241
+ /**
242
+ * The Gaussian sigma that draws a Figma blur radius as Figma does. Measured on Figma 126 exports,
243
+ * layer blur, both shadows, and background blur fall off as a Gaussian of 0.43 × radius; Figma's
244
+ * SVG export writes radius / 2, which is wider (`packages/core/tests/canvas/blur.test.ts`).
245
+ */
246
+ export function blurSigma(radius: number): number {
247
+ return drawnBlurRadius(radius) * BLUR_SIGMA_PER_RADIUS
248
+ }
249
+
250
+ /** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */
251
+ export function blurRadiusForSigma(sigma: number): number {
252
+ return Math.max(0, sigma) / BLUR_SIGMA_PER_RADIUS
253
+ }
254
+
219
255
  export function effectOverflow(effects?: Effect[]) {
220
256
  let left = 0
221
257
  let right = 0
@@ -231,7 +267,7 @@ export function effectOverflow(effects?: Effect[]) {
231
267
  ) {
232
268
  continue
233
269
  }
234
- const blurSpread = effect.radius + effect.spread
270
+ const blurSpread = drawnBlurRadius(widestBlurRadius(effect)) + effect.spread
235
271
  left = Math.max(left, blurSpread + Math.max(0, -effect.offset.x))
236
272
  right = Math.max(right, blurSpread + Math.max(0, effect.offset.x))
237
273
  top = Math.max(top, blurSpread + Math.max(0, -effect.offset.y))
@@ -253,59 +289,6 @@ export function computeAbsoluteBounds(
253
289
  return boundsToRect(bounds)
254
290
  }
255
291
 
256
- export function computeVisualBounds(
257
- nodes: Iterable<
258
- {
259
- id: string
260
- width: number
261
- height: number
262
- rotation?: number
263
- strokes?: Stroke[]
264
- strokeCap?: StrokeCap
265
- vectorNetwork?: VectorNetwork | null
266
- effects?: Effect[]
267
- } & StrokeShape
268
- >,
269
- getAbsolutePosition: (id: string) => Vector
270
- ): Rect {
271
- const bounds = createBoundsAccumulator()
272
-
273
- for (const n of nodes) {
274
- const abs = getAbsolutePosition(n.id)
275
- const bbox = rotatedBBox(abs.x, abs.y, n.width, n.height, n.rotation ?? 0)
276
- const stroke = strokeOverflow(n.strokes, n.strokeCap, n.vectorNetwork, n)
277
- const effects = effectOverflow(n.effects)
278
- includePoint(bounds, bbox.left - stroke - effects.left, bbox.top - stroke - effects.top)
279
- includePoint(bounds, bbox.right + stroke + effects.right, bbox.bottom + stroke + effects.bottom)
280
- }
281
-
282
- return boundsToRect(bounds)
283
- }
284
-
285
- export interface VisualBoundsNode {
286
- id: string
287
- width: number
288
- height: number
289
- rotation?: number
290
- flipX?: boolean
291
- flipY?: boolean
292
- strokes?: Stroke[]
293
- strokeCap?: StrokeCap
294
- vectorNetwork?: VectorNetwork | null
295
- effects?: Effect[]
296
- fillGeometry?: Array<{ commandsBlob: Uint8Array }>
297
- strokeGeometry?: Array<{ commandsBlob: Uint8Array }>
298
- childIds?: string[]
299
- visible?: boolean
300
- type?: string
301
- clipsContent?: boolean
302
- fontSize?: number
303
- textDecoration?: string
304
- textUnderlineOffset?: number | null
305
- textDecorationThickness?: number | null
306
- derivedTextHeight?: number | null
307
- }
308
-
309
292
  export function unionVisualBounds(
310
293
  a: VisualBounds | null,
311
294
  b: VisualBounds | null
@@ -328,163 +311,71 @@ export function intersectVisualBounds(a: VisualBounds, b: VisualBounds): VisualB
328
311
  return minX < maxX && minY < maxY ? { minX, minY, maxX, maxY } : null
329
312
  }
330
313
 
314
+ /** The path commands of a Figma geometry blob, by their byte code. */
315
+ export const GeometryCommand = { CLOSE: 0, MOVE: 1, LINE: 2, QUAD: 3, CUBIC: 4 } as const
316
+ export type GeometryCommand = (typeof GeometryCommand)[keyof typeof GeometryCommand]
317
+
331
318
  /**
332
- * Coordinate pairs per path blob command: 0=close, 1=move, 2=line,
333
- * 3=quad (glyph-derived blobs are quad-heavy), 4=cubic. Unknown → null so
334
- * walkers stop instead of desyncing into coordinate bytes.
319
+ * Coordinate pairs per command: glyph-derived blobs are quad-heavy, so every code needs its
320
+ * count. Unknown codes are null, so readers stop instead of desyncing into coordinate bytes.
335
321
  */
336
- export function geometryCommandCoordCount(command: number): number | null {
337
- if (command === 0) return 0
338
- if (command === 1 || command === 2) return 1
339
- if (command === 3) return 2
340
- if (command === 4) return 3
322
+ function commandCoordCount(command: number): number | null {
323
+ if (command === GeometryCommand.CLOSE) return 0
324
+ if (command === GeometryCommand.MOVE || command === GeometryCommand.LINE) return 1
325
+ if (command === GeometryCommand.QUAD) return 2
326
+ if (command === GeometryCommand.CUBIC) return 3
341
327
  return null
342
328
  }
343
329
 
344
- export function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>): Rect | null {
345
- const bounds = createBoundsAccumulator()
346
-
347
- for (const path of paths) {
348
- const blob = path.commandsBlob
349
- const dv = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)
350
- let offset = 0
351
- while (offset < blob.length) {
352
- const command = blob[offset++]
353
- const coords = geometryCommandCoordCount(command)
354
- if (coords == null) break
355
- for (let i = 0; i < coords; i++) {
356
- if (offset + 8 > blob.length) break
357
- const x = dv.getFloat32(offset, true)
358
- const y = dv.getFloat32(offset + 4, true)
359
- includePoint(bounds, x, y)
360
- offset += 8
361
- }
330
+ /**
331
+ * Walks a Figma geometry blob: `visit` gets each command, its coordinates as x, y pairs in one
332
+ * array reused between calls, and the byte offset of the first coordinate. It stops at an
333
+ * unknown command or one cut short.
334
+ */
335
+ export function readGeometryBlob(
336
+ blob: Uint8Array,
337
+ visit: (command: GeometryCommand, coords: readonly number[], offset: number) => void
338
+ ): void {
339
+ const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)
340
+ const coords: number[] = []
341
+ let offset = 0
342
+ while (offset < blob.length) {
343
+ const command = blob[offset]
344
+ const count = commandCoordCount(command)
345
+ if (count === null || offset + 1 + count * 8 > blob.length) return
346
+ offset += 1
347
+ coords.length = 0
348
+ for (let i = 0; i < count; i++) {
349
+ coords.push(view.getFloat32(offset + i * 8, true), view.getFloat32(offset + i * 8 + 4, true))
362
350
  }
363
- }
364
-
365
- return bounds.minX === Infinity ? null : boundsToRect(bounds)
366
- }
367
-
368
- function transformLocalPoint(node: VisualBoundsNode, point: Vector): Vector {
369
- let x = node.flipX ? node.width - point.x : point.x
370
- let y = node.flipY ? node.height - point.y : point.y
371
- const rotation = node.rotation ?? 0
372
- if (rotation !== 0) {
373
- const rotated = rotatePoint(x, y, node.width / 2, node.height / 2, degToRad(rotation))
374
- x = rotated.x
375
- y = rotated.y
376
- }
377
- return { x, y }
378
- }
379
-
380
- function transformedLocalBounds(node: VisualBoundsNode, local: Rect, abs: Vector): VisualBounds {
381
- const points = [
382
- { x: local.x, y: local.y },
383
- { x: local.x + local.width, y: local.y },
384
- { x: local.x + local.width, y: local.y + local.height },
385
- { x: local.x, y: local.y + local.height }
386
- ].map((point) => transformLocalPoint(node, point))
387
-
388
- return {
389
- minX: abs.x + Math.min(...points.map((point) => point.x)),
390
- minY: abs.y + Math.min(...points.map((point) => point.y)),
391
- maxX: abs.x + Math.max(...points.map((point) => point.x)),
392
- maxY: abs.y + Math.max(...points.map((point) => point.y))
351
+ visit(command as GeometryCommand, coords, offset)
352
+ offset += count * 8
393
353
  }
394
354
  }
395
355
 
396
- export function nodeVisualBounds(
397
- node: VisualBoundsNode,
398
- getAbsolutePosition: (id: string) => Vector
399
- ): VisualBounds {
400
- const abs = getAbsolutePosition(node.id)
401
- const base = computeVisualBounds([node], getAbsolutePosition)
402
- let bounds: VisualBounds = {
403
- minX: base.x,
404
- minY: base.y,
405
- maxX: base.x + base.width,
406
- maxY: base.y + base.height
407
- }
408
-
409
- const hasNonInsideStroke = node.strokes?.some(
410
- (stroke) => stroke.visible && stroke.align !== 'INSIDE'
411
- )
412
- const localGeometry = geometryBlobBounds([
413
- ...(node.fillGeometry ?? []),
414
- ...(hasNonInsideStroke ? (node.strokeGeometry ?? []) : [])
415
- ])
416
- if (localGeometry) {
417
- bounds = unionVisualBounds(bounds, transformedLocalBounds(node, localGeometry, abs)) ?? bounds
418
- }
419
-
420
- const linesOverflow = textLinesOverflow(node)
421
- if (linesOverflow > 0) {
422
- const lines = { x: 0, y: 0, width: node.width, height: node.height + linesOverflow }
423
- bounds = unionVisualBounds(bounds, transformedLocalBounds(node, lines, abs)) ?? bounds
356
+ export function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>): Rect | null {
357
+ const bounds = createBoundsAccumulator()
358
+ for (const path of paths) {
359
+ readGeometryBlob(path.commandsBlob, (_, coords) => {
360
+ for (let i = 0; i < coords.length; i += 2) includePoint(bounds, coords[i], coords[i + 1])
361
+ })
424
362
  }
425
363
 
426
- bounds.maxY += textDecorationOverflow(node)
427
-
428
- return bounds
364
+ return bounds.minX === Infinity ? null : boundsToRect(bounds)
429
365
  }
430
366
 
431
367
  /** How far a text node's underline or strikethrough reaches below its box. */
432
- export function textDecorationOverflow(node: VisualBoundsNode): number {
433
- if (node.type !== 'TEXT' || !node.textDecoration || node.textDecoration === 'NONE') return 0
434
- const fontSize = node.fontSize ?? 14
368
+ export function textDecorationOverflow(
369
+ node: Pick<
370
+ SceneNode,
371
+ 'type' | 'fontSize' | 'textDecoration' | 'textUnderlineOffset' | 'textDecorationThickness'
372
+ >
373
+ ): number {
374
+ if (node.type !== 'TEXT' || node.textDecoration === 'NONE') {
375
+ return 0
376
+ }
377
+ const { fontSize } = node
435
378
  const underlineOffset = node.textUnderlineOffset ?? fontSize * 0.18
436
379
  const thickness = node.textDecorationThickness ?? Math.max(1, fontSize / 16)
437
380
  return underlineOffset + thickness + fontSize * 0.35
438
381
  }
439
-
440
- function collectDescendantVisualBounds(
441
- nodeId: string,
442
- getNode: (id: string) => VisualBoundsNode | undefined,
443
- getAbsolutePosition: (id: string) => Vector,
444
- clip: VisualBounds | null = null
445
- ): VisualBounds | null {
446
- const node = getNode(nodeId)
447
- if (!node?.visible) return null
448
-
449
- const own = nodeVisualBounds(node, getAbsolutePosition)
450
- let bounds = clip ? intersectVisualBounds(own, clip) : own
451
-
452
- const isClippableContainer =
453
- node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE'
454
- let childClip = clip
455
- if (isClippableContainer && node.clipsContent) {
456
- const abs = getAbsolutePosition(node.id)
457
- const nodeClip = {
458
- minX: abs.x,
459
- minY: abs.y,
460
- maxX: abs.x + node.width,
461
- maxY: abs.y + node.height
462
- }
463
- childClip = childClip ? intersectVisualBounds(childClip, nodeClip) : nodeClip
464
- if (!childClip) return bounds
465
- }
466
-
467
- for (const childId of node.childIds ?? []) {
468
- bounds = unionVisualBounds(
469
- bounds,
470
- collectDescendantVisualBounds(childId, getNode, getAbsolutePosition, childClip)
471
- )
472
- }
473
-
474
- return bounds
475
- }
476
-
477
- export function computeDescendantVisualBounds(
478
- nodeIds: string[],
479
- getNode: (id: string) => VisualBoundsNode | undefined,
480
- getAbsolutePosition: (id: string) => Vector
481
- ): VisualBounds | null {
482
- let bounds: VisualBounds | null = null
483
- for (const nodeId of nodeIds) {
484
- bounds = unionVisualBounds(
485
- bounds,
486
- collectDescendantVisualBounds(nodeId, getNode, getAbsolutePosition)
487
- )
488
- }
489
- return bounds
490
- }
package/src/graph.ts CHANGED
@@ -25,7 +25,8 @@ import { CONTAINER_TYPES, createDefaultNode } from './node-defaults'
25
25
  import { updateNodePreview, type NodePreviewObserver } from './preview'
26
26
  import type { Color, Rect, Vector } from './primitives'
27
27
  import { markSourceFieldsEdited } from './source-metadata'
28
- import { invalidateTextCaches } from './text/picture'
28
+ import { textCacheInvalidationChanges } from './text/picture'
29
+ import { remapCopiedReferences } from './transfer/copied'
29
30
  import type {
30
31
  DocumentColorSpace,
31
32
  DocumentKind,
@@ -41,18 +42,22 @@ import type {
41
42
  VariableLiteral
42
43
  } from './types'
43
44
  import * as Variables from './variables'
44
- import type { VariableModeFallback } from './variables'
45
45
  import { normalizeVectorNetwork } from './vector-network'
46
46
 
47
47
  /** Scopes that graph methods check while a caller's callback runs; scopes nest. */
48
- type GraphScope = 'preview' | 'sourceMetadata' | 'importedState' | 'layout' | 'derivedLayout'
48
+ type GraphScope =
49
+ | 'preview'
50
+ | 'sourceMetadata'
51
+ | 'importedState'
52
+ | 'layout'
53
+ | 'derivedLayout'
54
+ | 'componentDerived'
49
55
 
50
56
  export class SceneGraph {
51
57
  nodes = new Map<string, SceneNode>()
52
58
  images = new Map<string, Uint8Array>()
53
59
  variables = new Map<string, Variable>()
54
60
  variableCollections = new Map<string, VariableCollection>()
55
- activeMode = new Map<string, string>()
56
61
  rootId: string
57
62
  figKiwiVersion: number | null = null
58
63
  /** Deflated kiwi schema bytes from the original .fig file, preserved for roundtrip fidelity. */
@@ -67,10 +72,14 @@ export class SceneGraph {
67
72
  sourceMetadata: 0,
68
73
  importedState: 0,
69
74
  layout: 0,
70
- derivedLayout: 0
75
+ derivedLayout: 0,
76
+ componentDerived: 0
71
77
  }
72
78
  private previewObservers: NodePreviewObserver[] = []
73
79
  private readonly changeObservers = new Set<NodeChangeObserver>()
80
+ /** How many waits outside the edit each recorder is paused for. */
81
+ private readonly outsideWaits = new Map<NodeChangeObserver, number>()
82
+ private outsideWrites = 0
74
83
  private importedIdGenerator: (() => string) | undefined
75
84
  private layoutWrites: LayoutWrites | null = null
76
85
  positionPreviewVersion = 0
@@ -109,22 +118,96 @@ export class SceneGraph {
109
118
  }
110
119
 
111
120
  /**
112
- * Tells `observer` about each node an edit adds and, before it changes, each node it is about
113
- * to change, so a recorder can keep the node as it was. Unlike node events these are never
114
- * buffered. Graph methods report the nodes they change; code that writes into a node directly
115
- * reports it with `willChangeNode` first.
121
+ * Tells `observer` about each node an edit adds and, before it changes, each node, variable,
122
+ * and collection it is about to change, so a recorder can keep it as it was. Unlike node events
123
+ * these are never buffered. Graph methods report what they change; code that writes into a
124
+ * node, variable, or collection directly reports it with `willChangeNode`,
125
+ * `willChangeVariable`, or `willChangeCollection` first.
116
126
  */
117
127
  observeNodeChanges(observer: NodeChangeObserver): () => void {
118
128
  this.changeObservers.add(observer)
119
129
  return () => this.changeObservers.delete(observer)
120
130
  }
121
131
 
132
+ /**
133
+ * Waits for `promise`, such as a font or an image download an edit needs, outside the edit in
134
+ * progress: what changes meanwhile, such as the user's or a collaborator's edit, is not recorded
135
+ * as the waiting edit's. Only for code that writes nothing itself until the wait ends.
136
+ */
137
+ async waitOutsideEdit<T>(promise: Promise<T>): Promise<T> {
138
+ const resume = this.pauseOpenEdits()
139
+ try {
140
+ return await promise
141
+ } finally {
142
+ resume()
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Runs `write`, a change someone else makes, such as a collaborator's arriving or an undo,
148
+ * outside every edit in progress, so none records it as its own.
149
+ */
150
+ runOutsideEdits<T>(write: () => T): T {
151
+ const resume = this.pauseOpenEdits()
152
+ this.outsideWrites++
153
+ try {
154
+ return write()
155
+ } finally {
156
+ this.outsideWrites--
157
+ resume()
158
+ }
159
+ }
160
+
161
+ /** Whether a write run with `runOutsideEdits`, such as a collaborator's change, is in progress. */
162
+ get isWritingOutsideEdits(): boolean {
163
+ return this.outsideWrites > 0
164
+ }
165
+
166
+ /**
167
+ * Pauses the recorders open now, and only those, until the returned function resumes them: a
168
+ * recorder that opens while an earlier pause is still pending, such as during a script's load
169
+ * left unawaited, pauses for its own waits and is not resumed by the earlier one.
170
+ */
171
+ private pauseOpenEdits(): () => void {
172
+ const paused = [...this.changeObservers]
173
+ for (const observer of paused) {
174
+ const waits = this.outsideWaits.get(observer) ?? 0
175
+ if (waits === 0) observer.pause?.()
176
+ this.outsideWaits.set(observer, waits + 1)
177
+ }
178
+ return () => {
179
+ for (const observer of paused) {
180
+ const waits = (this.outsideWaits.get(observer) ?? 1) - 1
181
+ if (waits > 0) {
182
+ this.outsideWaits.set(observer, waits)
183
+ continue
184
+ }
185
+ this.outsideWaits.delete(observer)
186
+ if (this.changeObservers.has(observer)) observer.resume?.()
187
+ }
188
+ }
189
+ }
190
+
122
191
  /** Tells `observeNodeChanges` observers that `node` is about to change. */
123
192
  willChangeNode(node: SceneNode | undefined): void {
124
193
  if (!node) return
125
194
  for (const observer of this.changeObservers) observer.before(node)
126
195
  }
127
196
 
197
+ /**
198
+ * Tells `observeNodeChanges` observers that the variable `id` is about to change, be added, or
199
+ * be removed. Graph methods report the variables they change; code that writes into one
200
+ * directly reports it first.
201
+ */
202
+ willChangeVariable(id: string): void {
203
+ for (const observer of this.changeObservers) observer.beforeRecord?.('variable', id)
204
+ }
205
+
206
+ /** As `willChangeVariable`, for the collection `id`. */
207
+ willChangeCollection(id: string): void {
208
+ for (const observer of this.changeObservers) observer.beforeRecord?.('collection', id)
209
+ }
210
+
128
211
  countDescendants(nodeId: string): number {
129
212
  const node = this.nodes.get(nodeId)
130
213
  if (!node) return 0
@@ -188,18 +271,10 @@ export class SceneGraph {
188
271
  Variables.removeCollection(this, id)
189
272
  }
190
273
 
191
- getActiveModeId(collectionId: string): string {
192
- return Variables.getActiveModeId(this, collectionId)
193
- }
194
-
195
274
  getNodeVariableModeId(nodeId: string, collectionId: string): string {
196
275
  return Variables.getNodeVariableModeId(this, nodeId, collectionId)
197
276
  }
198
277
 
199
- setActiveMode(collectionId: string, modeId: string): void {
200
- Variables.setActiveMode(this, collectionId, modeId)
201
- }
202
-
203
278
  addMode(collectionId: string, modeId: string, name: string, sourceMode?: string): void {
204
279
  Variables.addMode(this, collectionId, modeId, name, sourceMode)
205
280
  }
@@ -232,36 +307,20 @@ export class SceneGraph {
232
307
  return Variables.resolveNumberVariable(this, variableId)
233
308
  }
234
309
 
235
- resolveColorVariableForNode(
236
- nodeId: string,
237
- variableId: string,
238
- fallback?: VariableModeFallback
239
- ): Color | undefined {
240
- return Variables.resolveColorVariableForNode(this, nodeId, variableId, fallback)
310
+ resolveColorVariableForNode(nodeId: string, variableId: string): Color | undefined {
311
+ return Variables.resolveColorVariableForNode(this, nodeId, variableId)
241
312
  }
242
313
 
243
- resolveNumberVariableForNode(
244
- nodeId: string,
245
- variableId: string,
246
- fallback?: VariableModeFallback
247
- ): number | undefined {
248
- return Variables.resolveNumberVariableForNode(this, nodeId, variableId, fallback)
314
+ resolveNumberVariableForNode(nodeId: string, variableId: string): number | undefined {
315
+ return Variables.resolveNumberVariableForNode(this, nodeId, variableId)
249
316
  }
250
317
 
251
- resolveVariableForNode(
252
- nodeId: string,
253
- variableId: string,
254
- fallback?: VariableModeFallback
255
- ): VariableLiteral | undefined {
256
- return Variables.resolveVariableForNode(this, nodeId, variableId, fallback)
318
+ resolveVariableForNode(nodeId: string, variableId: string): VariableLiteral | undefined {
319
+ return Variables.resolveVariableForNode(this, nodeId, variableId)
257
320
  }
258
321
 
259
- resolveStringVariableForNode(
260
- nodeId: string,
261
- variableId: string,
262
- fallback?: VariableModeFallback
263
- ): string | undefined {
264
- return Variables.resolveStringVariableForNode(this, nodeId, variableId, fallback)
322
+ resolveStringVariableForNode(nodeId: string, variableId: string): string | undefined {
323
+ return Variables.resolveStringVariableForNode(this, nodeId, variableId)
265
324
  }
266
325
 
267
326
  getVariablesForCollection(collectionId: string): Variable[] {
@@ -473,6 +532,19 @@ export class SceneGraph {
473
532
  this.withScope('derivedLayout', fn)
474
533
  }
475
534
 
535
+ /**
536
+ * Runs `fn` as values a copy takes from its component, such as the bound paints of a layer an
537
+ * instance copies, refreshed for it. Source metadata is preserved, and a save leaves them to the
538
+ * component rather than writing them as edits of the copy.
539
+ */
540
+ applyComponentDerivedDuring(fn: () => void): void {
541
+ this.withScope('componentDerived', () => this.preserveSourceMetadataDuring(fn))
542
+ }
543
+
544
+ get isApplyingComponentDerived(): boolean {
545
+ return this.scopeDepths.componentDerived > 0
546
+ }
547
+
476
548
  get isApplyingDerivedLayout(): boolean {
477
549
  return this.scopeDepths.derivedLayout > 0
478
550
  }
@@ -576,8 +648,15 @@ export class SceneGraph {
576
648
  if (node.componentId) this.unindexInstance(node.componentId, id)
577
649
  if (changes.componentId) this.indexInstance(changes.componentId, id)
578
650
  }
579
- if (node.type === 'TEXT') invalidateTextCaches(node, changes)
580
651
  this.markEdited(node, Object.keys(changes))
652
+ // Cache resets that change the node travel with the change, under the caller's own values,
653
+ // so a layout put-back cannot drop their event.
654
+ if (node.type === 'TEXT') {
655
+ const resets = omitBy(textCacheInvalidationChanges(node, changes), (value, key) =>
656
+ Object.is(node[key], value)
657
+ )
658
+ changes = { ...resets, ...changes }
659
+ }
581
660
  if (changes.vectorNetwork) {
582
661
  changes = { ...changes, vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork) }
583
662
  }
@@ -587,12 +666,21 @@ export class SceneGraph {
587
666
  this.scopeDepths.sourceMetadata === 0 && !this.isApplyingDerivedLayout
588
667
  )
589
668
  Object.assign(node, changes)
590
- if (changes.fills) removeStaleBindings(node, 'fills', changes)
591
- if (changes.strokes) removeStaleBindings(node, 'strokes', changes)
669
+ this.followListBindings(node, changes)
592
670
  for (const key of absent) Reflect.deleteProperty(node, key)
593
671
  this.publishNodeUpdate(id, changes)
594
672
  }
595
673
 
674
+ /** Bindings of paints and effects a write sets follow the lists it wrote. */
675
+ private followListBindings(node: SceneNode, changes: Partial<SceneNode>): void {
676
+ if (changes.fills) removeStaleBindings(node, 'fills', changes)
677
+ if (changes.strokes) removeStaleBindings(node, 'strokes', changes)
678
+ if (changes.effects) removeStaleBindings(node, 'effects', changes)
679
+ // A bound paint written keeps what it was given until its variable's color changes.
680
+ if ((changes.fills || changes.strokes) && !this.isApplyingImportedState)
681
+ Variables.recordWrittenBoundPaints(this, node, changes)
682
+ }
683
+
596
684
  /** Marks fields an edit changed, unless metadata is being restored or layout is writing them. */
597
685
  private markEdited(node: SceneNode, keys: string[]): void {
598
686
  if (this.scopeDepths.sourceMetadata === 0 && !this.isApplyingLayout) {
@@ -647,6 +735,8 @@ export class SceneGraph {
647
735
  )
648
736
 
649
737
  this.emitter.emit('node:reparented', nodeId, oldParentId, newParentId)
738
+ // Bound paints show their variables in the modes the layer now sits in.
739
+ Variables.refreshMovedBoundPaints(this, nodeId, oldParentId)
650
740
  }
651
741
 
652
742
  /**
@@ -672,6 +762,8 @@ export class SceneGraph {
672
762
  node.parentId = parentId
673
763
  this.absPosCache.clear()
674
764
  this.emitter.emit('node:reordered', childId, parentId, at, previousParentId)
765
+ if (previousParentId !== parentId)
766
+ Variables.refreshMovedBoundPaints(this, childId, previousParentId)
675
767
  }
676
768
 
677
769
  deleteNode(id: string): void {
@@ -742,6 +834,22 @@ export class SceneGraph {
742
834
  sourceId: string,
743
835
  parentId: string,
744
836
  overrides: Partial<SceneNode> = {}
837
+ ): SceneNode | null {
838
+ const ids = new Map<string, string>()
839
+ const clone = this.cloneSubtree(sourceId, parentId, overrides, ids)
840
+ // Copies point at one another, as a paste's do: an instance at a component cloned with it,
841
+ // as Figma's clone does (desktop 126), and its override records at its copies' new ids.
842
+ remapCopiedReferences(this, ids)
843
+ // A copy placed in other modes than its source shows its bound paints there, as a move does.
844
+ if (clone) Variables.refreshMovedBoundPaints(this, clone.id, this.nodes.get(sourceId)?.parentId)
845
+ return clone
846
+ }
847
+
848
+ private cloneSubtree(
849
+ sourceId: string,
850
+ parentId: string,
851
+ overrides: Partial<SceneNode>,
852
+ ids: Map<string, string>
745
853
  ): SceneNode | null {
746
854
  const src = this.nodes.get(sourceId)
747
855
  if (!src) return null
@@ -752,10 +860,12 @@ export class SceneGraph {
752
860
  // A copy is a new design nobody has reviewed: Figma's duplicate and clone() drop the status.
753
861
  props.status = null
754
862
  const clone = this.createNode(src.type, parentId, { ...props, ...overrides })
863
+ Variables.copyAppliedBoundPaints(src, clone)
755
864
  if (src.status) markSourceFieldsEdited(clone, ['status'])
865
+ ids.set(src.id, clone.id)
756
866
 
757
867
  for (const childId of src.childIds) {
758
- this.cloneTree(childId, clone.id)
868
+ this.cloneSubtree(childId, clone.id, {}, ids)
759
869
  }
760
870
 
761
871
  return clone