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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (390) hide show
  1. package/dist/arrow-caps.d.ts.map +1 -1
  2. package/dist/arrow-caps.js +2 -5
  3. package/dist/arrow-caps.js.map +1 -1
  4. package/dist/asset-keys.d.ts +11 -1
  5. package/dist/asset-keys.d.ts.map +1 -1
  6. package/dist/asset-keys.js +16 -1
  7. package/dist/asset-keys.js.map +1 -1
  8. package/dist/behaviours/index.d.ts +2 -2
  9. package/dist/behaviours/index.js +2 -2
  10. package/dist/behaviours/kinds.d.ts +11 -5
  11. package/dist/behaviours/kinds.d.ts.map +1 -1
  12. package/dist/behaviours/kinds.js +76 -75
  13. package/dist/behaviours/kinds.js.map +1 -1
  14. package/dist/behaviours/layers.d.ts.map +1 -1
  15. package/dist/behaviours/layers.js +3 -2
  16. package/dist/behaviours/layers.js.map +1 -1
  17. package/dist/behaviours/model.d.ts +7 -4
  18. package/dist/behaviours/model.d.ts.map +1 -1
  19. package/dist/behaviours/model.js +16 -9
  20. package/dist/behaviours/model.js.map +1 -1
  21. package/dist/behaviours/schema.d.ts +118 -4
  22. package/dist/behaviours/schema.d.ts.map +1 -1
  23. package/dist/behaviours/schema.js +18 -3
  24. package/dist/behaviours/schema.js.map +1 -1
  25. package/dist/behaviours/spec.d.ts.map +1 -1
  26. package/dist/behaviours/spec.js +3 -2
  27. package/dist/behaviours/spec.js.map +1 -1
  28. package/dist/bindings.js +9 -1
  29. package/dist/bindings.js.map +1 -1
  30. package/dist/checkpoint.d.ts +31 -2
  31. package/dist/checkpoint.d.ts.map +1 -1
  32. package/dist/checkpoint.js +211 -10
  33. package/dist/checkpoint.js.map +1 -1
  34. package/dist/components/index.d.ts +3 -3
  35. package/dist/components/index.js +3 -3
  36. package/dist/components/properties.d.ts +18 -1
  37. package/dist/components/properties.d.ts.map +1 -1
  38. package/dist/components/properties.js +66 -3
  39. package/dist/components/properties.js.map +1 -1
  40. package/dist/components/variants.d.ts +16 -10
  41. package/dist/components/variants.d.ts.map +1 -1
  42. package/dist/components/variants.js +39 -13
  43. package/dist/components/variants.js.map +1 -1
  44. package/dist/connectors/hit.d.ts.map +1 -1
  45. package/dist/connectors/hit.js +1 -7
  46. package/dist/connectors/hit.js.map +1 -1
  47. package/dist/connectors/pieces.d.ts.map +1 -1
  48. package/dist/connectors/pieces.js +2 -5
  49. package/dist/connectors/pieces.js.map +1 -1
  50. package/dist/constants.d.ts +6 -1
  51. package/dist/constants.d.ts.map +1 -1
  52. package/dist/constants.js +17 -1
  53. package/dist/constants.js.map +1 -1
  54. package/dist/constraints.d.ts +25 -0
  55. package/dist/constraints.d.ts.map +1 -0
  56. package/dist/constraints.js +51 -0
  57. package/dist/constraints.js.map +1 -0
  58. package/dist/coordinate.d.ts +64 -1
  59. package/dist/coordinate.d.ts.map +1 -1
  60. package/dist/coordinate.js +217 -1
  61. package/dist/coordinate.js.map +1 -1
  62. package/dist/copy.d.ts.map +1 -1
  63. package/dist/copy.js +17 -16
  64. package/dist/copy.js.map +1 -1
  65. package/dist/css/blend.d.ts +11 -0
  66. package/dist/css/blend.d.ts.map +1 -0
  67. package/dist/css/blend.js +30 -0
  68. package/dist/css/blend.js.map +1 -0
  69. package/dist/css/index.d.ts +4 -2
  70. package/dist/css/index.js +4 -2
  71. package/dist/css/text.d.ts +28 -1
  72. package/dist/css/text.d.ts.map +1 -1
  73. package/dist/css/text.js +84 -17
  74. package/dist/css/text.js.map +1 -1
  75. package/dist/css/transform.d.ts +34 -0
  76. package/dist/css/transform.d.ts.map +1 -0
  77. package/dist/css/transform.js +320 -0
  78. package/dist/css/transform.js.map +1 -0
  79. package/dist/css/values.d.ts.map +1 -1
  80. package/dist/css/values.js +11 -1
  81. package/dist/css/values.js.map +1 -1
  82. package/dist/cubic.d.ts +7 -0
  83. package/dist/cubic.d.ts.map +1 -0
  84. package/dist/cubic.js +17 -0
  85. package/dist/cubic.js.map +1 -0
  86. package/dist/export-format.d.ts +14 -1
  87. package/dist/export-format.d.ts.map +1 -1
  88. package/dist/export-format.js +26 -1
  89. package/dist/export-format.js.map +1 -1
  90. package/dist/fields/text.d.ts +2 -2
  91. package/dist/fields/text.d.ts.map +1 -1
  92. package/dist/fields/text.js +2 -1
  93. package/dist/fields/text.js.map +1 -1
  94. package/dist/figjam/index.d.ts +2 -2
  95. package/dist/figjam/index.js +2 -2
  96. package/dist/figjam/palette.d.ts +13 -2
  97. package/dist/figjam/palette.d.ts.map +1 -1
  98. package/dist/figjam/palette.js +30 -2
  99. package/dist/figjam/palette.js.map +1 -1
  100. package/dist/generated/index.d.ts +2 -2
  101. package/dist/generated/index.js +2 -2
  102. package/dist/generated/shape-with-text.d.ts +7 -2
  103. package/dist/generated/shape-with-text.d.ts.map +1 -1
  104. package/dist/generated/shape-with-text.js +20 -3
  105. package/dist/generated/shape-with-text.js.map +1 -1
  106. package/dist/geometry.d.ts +37 -46
  107. package/dist/geometry.d.ts.map +1 -1
  108. package/dist/geometry.js +80 -137
  109. package/dist/geometry.js.map +1 -1
  110. package/dist/graph.d.ts +49 -12
  111. package/dist/graph.d.ts.map +1 -1
  112. package/dist/graph.js +125 -27
  113. package/dist/graph.js.map +1 -1
  114. package/dist/group-bounds.d.ts +14 -4
  115. package/dist/group-bounds.d.ts.map +1 -1
  116. package/dist/group-bounds.js +76 -11
  117. package/dist/group-bounds.js.map +1 -1
  118. package/dist/guides.d.ts +3 -1
  119. package/dist/guides.d.ts.map +1 -1
  120. package/dist/guides.js +5 -1
  121. package/dist/guides.js.map +1 -1
  122. package/dist/hit-test.d.ts +5 -0
  123. package/dist/hit-test.d.ts.map +1 -1
  124. package/dist/hit-test.js +57 -38
  125. package/dist/hit-test.js.map +1 -1
  126. package/dist/images.d.ts +27 -3
  127. package/dist/images.d.ts.map +1 -1
  128. package/dist/images.js +57 -3
  129. package/dist/images.js.map +1 -1
  130. package/dist/index.d.ts +2 -2
  131. package/dist/instances/fields.d.ts +2 -2
  132. package/dist/instances/fields.d.ts.map +1 -1
  133. package/dist/instances/fields.js +8 -0
  134. package/dist/instances/fields.js.map +1 -1
  135. package/dist/instances/index.d.ts +1 -3
  136. package/dist/instances/index.d.ts.map +1 -1
  137. package/dist/instances/index.js +39 -30
  138. package/dist/instances/index.js.map +1 -1
  139. package/dist/instances/kept-overrides.js +204 -0
  140. package/dist/instances/kept-overrides.js.map +1 -0
  141. package/dist/instances/main-component.d.ts +3 -1
  142. package/dist/instances/main-component.d.ts.map +1 -1
  143. package/dist/instances/main-component.js +10 -2
  144. package/dist/instances/main-component.js.map +1 -1
  145. package/dist/instances/overrides.d.ts +4 -1
  146. package/dist/instances/overrides.d.ts.map +1 -1
  147. package/dist/instances/overrides.js +18 -2
  148. package/dist/instances/overrides.js.map +1 -1
  149. package/dist/instances/placement.js +55 -12
  150. package/dist/instances/placement.js.map +1 -1
  151. package/dist/instances/swap.js +291 -0
  152. package/dist/instances/swap.js.map +1 -0
  153. package/dist/instances/sync.js +112 -39
  154. package/dist/instances/sync.js.map +1 -1
  155. package/dist/node-defaults.d.ts.map +1 -1
  156. package/dist/node-defaults.js +2 -1
  157. package/dist/node-defaults.js.map +1 -1
  158. package/dist/order-keys.d.ts +3 -1
  159. package/dist/order-keys.d.ts.map +1 -1
  160. package/dist/order-keys.js +6 -1
  161. package/dist/order-keys.js.map +1 -1
  162. package/dist/outline/flatten.d.ts +15 -0
  163. package/dist/outline/flatten.d.ts.map +1 -0
  164. package/dist/outline/flatten.js +189 -0
  165. package/dist/outline/flatten.js.map +1 -0
  166. package/dist/outline/heads.js +34 -0
  167. package/dist/outline/heads.js.map +1 -0
  168. package/dist/outline/hit.d.ts +21 -0
  169. package/dist/outline/hit.d.ts.map +1 -0
  170. package/dist/outline/hit.js +276 -0
  171. package/dist/outline/hit.js.map +1 -0
  172. package/dist/outline/index.d.ts +5 -0
  173. package/dist/outline/index.js +5 -0
  174. package/dist/outline/point.d.ts +16 -0
  175. package/dist/outline/point.d.ts.map +1 -0
  176. package/dist/outline/point.js +42 -0
  177. package/dist/outline/point.js.map +1 -0
  178. package/dist/outline/shapes.d.ts +35 -0
  179. package/dist/outline/shapes.d.ts.map +1 -0
  180. package/dist/outline/shapes.js +181 -0
  181. package/dist/outline/shapes.js.map +1 -0
  182. package/dist/plugin-data/fields.d.ts +4 -1
  183. package/dist/plugin-data/fields.d.ts.map +1 -1
  184. package/dist/plugin-data/fields.js +21 -4
  185. package/dist/plugin-data/fields.js.map +1 -1
  186. package/dist/resize.d.ts +22 -11
  187. package/dist/resize.d.ts.map +1 -1
  188. package/dist/resize.js +227 -93
  189. package/dist/resize.js.map +1 -1
  190. package/dist/scaling/index.d.ts +3 -2
  191. package/dist/scaling/index.js +3 -2
  192. package/dist/scaling/node.d.ts.map +1 -1
  193. package/dist/scaling/node.js +5 -1
  194. package/dist/scaling/node.js.map +1 -1
  195. package/dist/scaling/snapshot.d.ts +25 -0
  196. package/dist/scaling/snapshot.d.ts.map +1 -0
  197. package/dist/scaling/snapshot.js +39 -0
  198. package/dist/scaling/snapshot.js.map +1 -0
  199. package/dist/scaling/tree.d.ts +24 -2
  200. package/dist/scaling/tree.d.ts.map +1 -1
  201. package/dist/scaling/tree.js +38 -15
  202. package/dist/scaling/tree.js.map +1 -1
  203. package/dist/shaders/model.d.ts.map +1 -1
  204. package/dist/shaders/model.js +2 -1
  205. package/dist/shaders/model.js.map +1 -1
  206. package/dist/shared-styles.d.ts +16 -1
  207. package/dist/shared-styles.d.ts.map +1 -1
  208. package/dist/shared-styles.js +69 -2
  209. package/dist/shared-styles.js.map +1 -1
  210. package/dist/slots/authoring.d.ts +3 -2
  211. package/dist/slots/authoring.d.ts.map +1 -1
  212. package/dist/slots/authoring.js +5 -3
  213. package/dist/slots/authoring.js.map +1 -1
  214. package/dist/slots/content.d.ts.map +1 -1
  215. package/dist/slots/content.js +6 -1
  216. package/dist/slots/content.js.map +1 -1
  217. package/dist/slots/frames.d.ts +2 -0
  218. package/dist/slots/frames.d.ts.map +1 -1
  219. package/dist/slots/frames.js +7 -2
  220. package/dist/slots/frames.js.map +1 -1
  221. package/dist/text/direction.d.ts +3 -1
  222. package/dist/text/direction.d.ts.map +1 -1
  223. package/dist/text/direction.js +5 -1
  224. package/dist/text/direction.js.map +1 -1
  225. package/dist/text/index.d.ts +2 -2
  226. package/dist/text/index.js +3 -3
  227. package/dist/text/lengths.d.ts +6 -1
  228. package/dist/text/lengths.d.ts.map +1 -1
  229. package/dist/text/lengths.js +10 -1
  230. package/dist/text/lengths.js.map +1 -1
  231. package/dist/text/overflow.d.ts +2 -2
  232. package/dist/text/overflow.js.map +1 -1
  233. package/dist/text/picture.d.ts +1 -2
  234. package/dist/text/picture.d.ts.map +1 -1
  235. package/dist/text/picture.js +1 -4
  236. package/dist/text/picture.js.map +1 -1
  237. package/dist/transfer/apply.d.ts.map +1 -1
  238. package/dist/transfer/apply.js +1 -6
  239. package/dist/transfer/apply.js.map +1 -1
  240. package/dist/transfer/copied.d.ts +35 -3
  241. package/dist/transfer/copied.d.ts.map +1 -1
  242. package/dist/transfer/copied.js +44 -13
  243. package/dist/transfer/copied.js.map +1 -1
  244. package/dist/transfer/dependencies.d.ts +38 -0
  245. package/dist/transfer/dependencies.d.ts.map +1 -0
  246. package/dist/transfer/dependencies.js +100 -0
  247. package/dist/transfer/dependencies.js.map +1 -0
  248. package/dist/transfer/history.js +0 -1
  249. package/dist/transfer/history.js.map +1 -1
  250. package/dist/transfer/index.d.ts +5 -3
  251. package/dist/transfer/index.js +5 -3
  252. package/dist/transfer/nodes.d.ts.map +1 -1
  253. package/dist/transfer/nodes.js +16 -19
  254. package/dist/transfer/nodes.js.map +1 -1
  255. package/dist/transfer/plan.d.ts +0 -1
  256. package/dist/transfer/plan.js +3 -5
  257. package/dist/transfer/plan.js.map +1 -1
  258. package/dist/transfer/remove.js +5 -2
  259. package/dist/transfer/remove.js.map +1 -1
  260. package/dist/transfer/resources.d.ts +21 -0
  261. package/dist/transfer/resources.d.ts.map +1 -0
  262. package/dist/transfer/resources.js +38 -0
  263. package/dist/transfer/resources.js.map +1 -0
  264. package/dist/transfer/variables.d.ts +8 -1
  265. package/dist/transfer/variables.d.ts.map +1 -1
  266. package/dist/transfer/variables.js +24 -1
  267. package/dist/transfer/variables.js.map +1 -1
  268. package/dist/types.d.ts +73 -2
  269. package/dist/types.d.ts.map +1 -1
  270. package/dist/types.js.map +1 -1
  271. package/dist/undo.d.ts +28 -3
  272. package/dist/undo.d.ts.map +1 -1
  273. package/dist/undo.js +24 -7
  274. package/dist/undo.js.map +1 -1
  275. package/dist/variables/bindings.d.ts +37 -11
  276. package/dist/variables/bindings.d.ts.map +1 -1
  277. package/dist/variables/bindings.js +122 -74
  278. package/dist/variables/bindings.js.map +1 -1
  279. package/dist/variables/fields.d.ts +19 -1
  280. package/dist/variables/fields.d.ts.map +1 -1
  281. package/dist/variables/fields.js +36 -1
  282. package/dist/variables/fields.js.map +1 -1
  283. package/dist/variables/formula/check.d.ts +1 -1
  284. package/dist/variables/formula/index.d.ts +2 -2
  285. package/dist/variables/formula/index.js +1 -1
  286. package/dist/variables/formula/index.js.map +1 -1
  287. package/dist/variables/formula/print.d.ts +1 -1
  288. package/dist/variables/formula/signatures.js +1 -1
  289. package/dist/variables/index.d.ts +7 -7
  290. package/dist/variables/index.d.ts.map +1 -1
  291. package/dist/variables/index.js +57 -26
  292. package/dist/variables/index.js.map +1 -1
  293. package/dist/variables/paints.d.ts +57 -0
  294. package/dist/variables/paints.d.ts.map +1 -0
  295. package/dist/variables/paints.js +231 -0
  296. package/dist/variables/paints.js.map +1 -0
  297. package/dist/variables/resolution.d.ts +14 -11
  298. package/dist/variables/resolution.d.ts.map +1 -1
  299. package/dist/variables/resolution.js +36 -17
  300. package/dist/variables/resolution.js.map +1 -1
  301. package/dist/variables/subtree.js +17 -0
  302. package/dist/variables/subtree.js.map +1 -0
  303. package/dist/vector-network.d.ts.map +1 -1
  304. package/dist/vector-network.js +2 -8
  305. package/dist/vector-network.js.map +1 -1
  306. package/package.json +14 -1
  307. package/src/arrow-caps.ts +2 -5
  308. package/src/asset-keys.ts +20 -0
  309. package/src/behaviours/kinds.ts +51 -34
  310. package/src/behaviours/layers.ts +4 -2
  311. package/src/behaviours/model.ts +22 -11
  312. package/src/behaviours/schema.ts +20 -4
  313. package/src/behaviours/spec.ts +8 -2
  314. package/src/bindings.ts +11 -2
  315. package/src/checkpoint.ts +319 -17
  316. package/src/components/properties.ts +101 -2
  317. package/src/components/variants.ts +51 -30
  318. package/src/connectors/hit.ts +1 -12
  319. package/src/connectors/pieces.ts +2 -5
  320. package/src/constants.ts +17 -0
  321. package/src/constraints.ts +60 -0
  322. package/src/coordinate.ts +220 -0
  323. package/src/copy.ts +18 -19
  324. package/src/css/blend.ts +27 -0
  325. package/src/css/index.ts +18 -1
  326. package/src/css/text.ts +85 -17
  327. package/src/css/transform.ts +266 -0
  328. package/src/css/values.ts +8 -0
  329. package/src/cubic.ts +14 -0
  330. package/src/export-format.ts +29 -0
  331. package/src/fields/text.ts +3 -1
  332. package/src/figjam/palette.ts +29 -1
  333. package/src/generated/shape-with-text.ts +28 -2
  334. package/src/geometry.ts +97 -206
  335. package/src/graph.ts +155 -45
  336. package/src/group-bounds.ts +84 -17
  337. package/src/guides.ts +5 -0
  338. package/src/hit-test.ts +93 -64
  339. package/src/images.ts +71 -3
  340. package/src/instances/fields.ts +8 -0
  341. package/src/instances/index.ts +62 -31
  342. package/src/instances/kept-overrides.ts +280 -0
  343. package/src/instances/main-component.ts +12 -0
  344. package/src/instances/overrides.ts +27 -1
  345. package/src/instances/placement.ts +63 -10
  346. package/src/instances/swap.ts +456 -0
  347. package/src/instances/sync.ts +176 -50
  348. package/src/node-defaults.ts +3 -1
  349. package/src/order-keys.ts +6 -0
  350. package/src/outline/flatten.ts +190 -0
  351. package/src/outline/heads.ts +52 -0
  352. package/src/outline/hit.ts +369 -0
  353. package/src/outline/index.ts +4 -0
  354. package/src/outline/point.ts +59 -0
  355. package/src/outline/shapes.ts +207 -0
  356. package/src/plugin-data/fields.ts +21 -2
  357. package/src/resize.ts +267 -119
  358. package/src/scaling/index.ts +9 -1
  359. package/src/scaling/node.ts +9 -1
  360. package/src/scaling/snapshot.ts +51 -0
  361. package/src/scaling/tree.ts +63 -19
  362. package/src/shaders/model.ts +2 -3
  363. package/src/shared-styles.ts +90 -1
  364. package/src/slots/authoring.ts +5 -3
  365. package/src/slots/content.ts +6 -1
  366. package/src/slots/frames.ts +9 -7
  367. package/src/text/direction.ts +5 -0
  368. package/src/text/lengths.ts +10 -0
  369. package/src/text/overflow.ts +2 -2
  370. package/src/text/picture.ts +0 -4
  371. package/src/transfer/apply.ts +1 -13
  372. package/src/transfer/copied.ts +83 -15
  373. package/src/transfer/dependencies.ts +126 -0
  374. package/src/transfer/history.ts +0 -6
  375. package/src/transfer/index.ts +20 -2
  376. package/src/transfer/nodes.ts +17 -31
  377. package/src/transfer/plan.ts +3 -11
  378. package/src/transfer/remove.ts +5 -2
  379. package/src/transfer/resources.ts +54 -0
  380. package/src/transfer/variables.ts +34 -0
  381. package/src/types.ts +83 -1
  382. package/src/undo.ts +48 -10
  383. package/src/variables/bindings.ts +152 -88
  384. package/src/variables/fields.ts +46 -1
  385. package/src/variables/formula/index.ts +3 -1
  386. package/src/variables/index.ts +90 -39
  387. package/src/variables/paints.ts +296 -0
  388. package/src/variables/resolution.ts +39 -28
  389. package/src/variables/subtree.ts +15 -0
  390. package/src/vector-network.ts +4 -2
@@ -1,17 +1,24 @@
1
1
  import type { NodeCloneMode } from '../copy'
2
2
  import type { SceneGraph } from '../graph'
3
- import { detachOwnedSlotContent, restoreOwnedSlotContent } from '../slots/frames'
3
+ import {
4
+ detachOwnedSlotContent,
5
+ restoreOwnedSlotContent,
6
+ type ParkedSlotContent
7
+ } from '../slots/frames'
4
8
  import type { SceneNode } from '../types'
9
+ import { applyBoundPaintColorsIn, keepCopyBoundPaints } from '../variables/paints'
5
10
  import { keepOwnContentSize } from './content-size'
6
11
  import { createInstanceSyncContext, groupCopyRemovals, removeLeftoverCopies } from './copies'
7
12
  import { canCreateInstance } from './cycles'
8
13
  import { INSTANCE_OVERRIDE_FIELDS, INSTANCE_SYNC_PROPS } from './fields'
14
+ import { findInstanceAncestor, instanceMainComponent } from './main-component'
9
15
  import {
10
16
  clearInstanceOverrides,
11
17
  getInstanceOverride,
12
18
  hasInstanceOverride as hasNodeInstanceOverride,
13
19
  setInstanceOverride
14
20
  } from './overrides'
21
+ import { followComponentLayers } from './swap'
15
22
  import {
16
23
  bindingProtection,
17
24
  cloneChildrenWithMapping,
@@ -25,7 +32,7 @@ import {
25
32
  } from './sync'
26
33
 
27
34
  export { canCreateInstance, createsComponentCycle, cycleSafeParent } from './cycles'
28
- export { instanceMainComponent } from './main-component'
35
+ export { findInstanceAncestor, instanceMainComponent } from './main-component'
29
36
  export {
30
37
  INSTANCE_FOLLOWED_FIELDS,
31
38
  INSTANCE_OVERRIDE_FIELDS,
@@ -59,6 +66,8 @@ export function createInstance(
59
66
  const instance = graph.createNode('INSTANCE', parentId, { ...props, ...overrides, componentId })
60
67
 
61
68
  cloneChildrenWithMapping(graph, component.id, instance.id)
69
+ // A copy shows its bound paints in its own context, not as its component's paints hold them.
70
+ applyBoundPaintColorsIn(graph, instance.id)
62
71
 
63
72
  return instance
64
73
  }
@@ -111,6 +120,19 @@ export interface SwapInstanceOptions {
111
120
  keepName?: boolean
112
121
  }
113
122
 
123
+ function namedAfter(
124
+ graph: SceneGraph,
125
+ instance: SceneNode,
126
+ component: SceneNode
127
+ ): SceneNode | undefined {
128
+ const main = instanceMainComponent(graph, instance)
129
+ const set = main?.parentId ? graph.nodes.get(main.parentId) : undefined
130
+ if (set?.type === 'COMPONENT_SET' && component.parentId === set.id) {
131
+ return main
132
+ }
133
+ return instance.componentId ? graph.nodes.get(instance.componentId) : undefined
134
+ }
135
+
114
136
  export function swapInstanceComponent(
115
137
  graph: SceneGraph,
116
138
  instanceId: string,
@@ -122,26 +144,53 @@ export function swapInstanceComponent(
122
144
  if (!instance || component?.type !== 'COMPONENT' || instance.type !== 'INSTANCE') return
123
145
  if (instance.parentId && !canCreateInstance(graph, componentId, instance.parentId)) return
124
146
 
125
- const previousComponent = instance.componentId ? graph.nodes.get(instance.componentId) : undefined
147
+ const previousComponent = namedAfter(graph, instance, component)
126
148
  recordNestedSwap(graph, instance, componentId)
127
- const updates: Partial<SceneNode> = { componentId }
128
149
  const source = sourceInTargetCoordinates(component, instance.componentScale)
129
- for (const key of INSTANCE_SYNC_PROPS) {
130
- if (hasNodeInstanceOverride(instance.instanceOverrides, instance.id, instance.id, key)) continue
131
- copyProp(updates, source, key)
150
+ const updates: Partial<SceneNode> = {
151
+ componentId,
152
+ ...ownSyncedProps(graph, instance, source).updates
132
153
  }
133
154
 
134
155
  if (!keepName && (!previousComponent || instance.name === previousComponent.name))
135
156
  updates.name = component.name
136
157
 
137
- const childIds = Array.from(instance.childIds)
158
+ keepCopyBoundPaints(graph, instance, updates)
138
159
  const slotContent = detachOwnedSlotContent(graph, instance)
139
- for (const childId of childIds) graph.deleteNode(childId)
140
160
  graph.updateNode(instanceId, updates)
141
- cloneChildrenWithMapping(graph, componentId, instanceId)
161
+ followComponentLayers(graph, instance, component, parkedContent(slotContent))
142
162
  restoreOwnedSlotContent(graph, instance, slotContent)
143
163
  }
144
164
 
165
+ const NOTHING_PARKED: ReadonlySet<string> = new Set()
166
+
167
+ function parkedContent(parked: ParkedSlotContent): ReadonlySet<string> {
168
+ if (parked.slots.length === 0) {
169
+ return NOTHING_PARKED
170
+ }
171
+ return new Set(parked.slots.flatMap((slot) => slot.content))
172
+ }
173
+
174
+ /** What an instance takes from its component, and which fields it keeps as its own. */
175
+ function ownSyncedProps(
176
+ graph: SceneGraph,
177
+ instance: SceneNode,
178
+ source: SceneNode
179
+ ): { updates: Partial<SceneNode>; kept: (field: string) => boolean } {
180
+ const enclosing = enclosingInstanceOverrideFields(graph, instance)
181
+ enclosing.push(new Set(instance.instanceOverrides.self.keys()))
182
+ const protectedField = bindingProtection(enclosing)
183
+ const updates: Partial<SceneNode> = {}
184
+ syncBindingFields(instance, source, updates, protectedField)
185
+ const kept = (key: string) => isProtectedSyncField(instance, key, protectedField)
186
+ for (const key of INSTANCE_SYNC_PROPS) {
187
+ if (key !== 'boundVariables' && !kept(key)) {
188
+ copyProp(updates, source, key)
189
+ }
190
+ }
191
+ return { updates, kept }
192
+ }
193
+
145
194
  const syncingComponentsByGraph = new WeakMap<SceneGraph, Set<string>>()
146
195
 
147
196
  export function syncInstances(graph: SceneGraph, componentId: string): void {
@@ -182,20 +231,11 @@ function syncInstancesOf(
182
231
  try {
183
232
  groupCopyRemovals(graph, (removals) => {
184
233
  for (const instance of instances) {
185
- const enclosing = enclosingInstanceOverrideFields(graph, instance)
186
- enclosing.push(new Set(instance.instanceOverrides.self.keys()))
187
- const protectedField = bindingProtection(enclosing)
188
234
  const source = sourceInTargetCoordinates(component, instance.componentScale)
189
- const updates: Partial<SceneNode> = {}
190
- syncBindingFields(instance, source, updates, protectedField)
191
- for (const key of INSTANCE_SYNC_PROPS) {
192
- if (key === 'boundVariables') continue
193
- if (isProtectedSyncField(instance, key, protectedField)) continue
194
- copyProp(updates, source, key)
195
- }
235
+ const { updates, kept } = ownSyncedProps(graph, instance, source)
196
236
  // An instance that hugs its layers sizes to what it shows, not to the component.
197
237
  keepOwnContentSize(graph, updates, instance, source)
198
- updateSyncedProps(graph, instance, updates)
238
+ updateSyncedProps(graph, instance, updates, kept)
199
239
  const context = createInstanceSyncContext(instance.id, instance.instanceOverrides)
200
240
  syncChildren(graph, component.id, instance.id, instance.instanceOverrides, context)
201
241
  removeLeftoverCopies(graph, context, removals)
@@ -234,16 +274,6 @@ export function getInstances(graph: SceneGraph, componentId: string): SceneNode[
234
274
  return instances
235
275
  }
236
276
 
237
- /** Nearest INSTANCE at or above `nodeId` — self, parent, grandparent, etc. */
238
- export function findInstanceAncestor(graph: SceneGraph, nodeId: string): SceneNode | undefined {
239
- let current = graph.nodes.get(nodeId)
240
- while (current) {
241
- if (current.type === 'INSTANCE') return current
242
- current = current.parentId ? graph.nodes.get(current.parentId) : undefined
243
- }
244
- return undefined
245
- }
246
-
247
277
  /**
248
278
  * True when a node field is protected from instance synchronization.
249
279
  */
@@ -267,6 +297,7 @@ export function recordInstanceOverride(
267
297
 
268
298
  if (relevant.length === 0) return
269
299
 
300
+ graph.willChangeNode(instance)
270
301
  for (const field of relevant)
271
302
  setInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)
272
303
  graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })
@@ -0,0 +1,280 @@
1
+ // Overrides a variant switch keeps for the layers it removes, to give back to the next copies.
2
+ import { omit } from 'es-toolkit/object'
3
+ import { isEqual } from 'es-toolkit/predicate'
4
+
5
+ import {
6
+ overriddenAssignmentId,
7
+ reapplyComponentPropertyAssignments
8
+ } from '../components/properties'
9
+ import type { SceneGraph } from '../graph'
10
+ import type { SceneNode } from '../types'
11
+ import { instanceMainComponent } from './main-component'
12
+ import { overriddenBindingField } from './overrides'
13
+
14
+ /** Where a switch runs: the switched instance and the instances enclosing the layers it changes. */
15
+ export interface OverrideScope {
16
+ graph: SceneGraph
17
+ instance: SceneNode
18
+ owners: readonly SceneNode[]
19
+ /** Reports an owner before its override maps are written into. */
20
+ willChange(owner: SceneNode): void
21
+ }
22
+
23
+ /**
24
+ * An instance that holds overrides on a layer: one enclosing the switch, by ID, or one inside
25
+ * the removed copy, by its path, since a new copy of it gets a new ID.
26
+ */
27
+ type OwnerRef = { id: string } | { path: string }
28
+
29
+ /** What one owner overrides on a layer, with the values the layer showed. */
30
+ interface KeptOverrides {
31
+ owner: OwnerRef
32
+ /** The owner's own fields, for a nested instance's overrides on itself. */
33
+ self: boolean
34
+ fields: Map<string, unknown>
35
+ values: Partial<SceneNode>
36
+ /** Bindings the overrides claim, with their unit scales; an absent variable claims none. */
37
+ bindings: Record<string, { variableId: string | undefined; scale: number | undefined }>
38
+ /** Set when the overrides claim every binding, as removing a variable records. */
39
+ allBindings?: { boundVariables: SceneNode['boundVariables']; scales: Scales }
40
+ /** The component a nested instance was switched to, which its new copy is switched to again. */
41
+ swappedTo?: string
42
+ /** Property values the overrides claim on a nested instance. */
43
+ assignments: Record<string, string>
44
+ }
45
+
46
+ type Scales = SceneNode['variableBindingScales']
47
+
48
+ /** Override fields that link a copy to its layer, which a new copy gets from its own layer. */
49
+ const LINK_FIELDS: ReadonlySet<string> = new Set(['sourceComponentId', 'componentId'])
50
+
51
+ /**
52
+ * Overrides on layers a switch removed, by switched instance and then by the layer's path from
53
+ * it: each step's name and its order among the same-named layers beside it, the way a switch
54
+ * pairs layers. Figma brings them back when the instance switches to a variant that draws the
55
+ * layer again, though its saved records keep none of them while the layer is gone (desktop
56
+ * 126), so they live with the open document only.
57
+ */
58
+ const keptByGraph = new WeakMap<SceneGraph, Map<string, Map<string, KeptOverrides[]>>>()
59
+
60
+ function keptFor(scope: OverrideScope, create: true): Map<string, KeptOverrides[]>
61
+ function keptFor(scope: OverrideScope, create: false): Map<string, KeptOverrides[]> | undefined
62
+ function keptFor(scope: OverrideScope, create: boolean) {
63
+ let byInstance = keptByGraph.get(scope.graph)
64
+ if (!byInstance && create) {
65
+ byInstance = new Map()
66
+ keptByGraph.set(scope.graph, byInstance)
67
+ }
68
+ let byPath = byInstance?.get(scope.instance.id)
69
+ if (!byPath && byInstance && create) {
70
+ byPath = new Map()
71
+ byInstance.set(scope.instance.id, byPath)
72
+ }
73
+ return byPath
74
+ }
75
+
76
+ /** A layer's path from the switched instance, by names and their order among namesakes. */
77
+ function layerPath(scope: OverrideScope, node: SceneNode): string | undefined {
78
+ const steps: string[] = []
79
+ let current = node
80
+ while (current.id !== scope.instance.id) {
81
+ const parent: SceneNode | undefined = current.parentId
82
+ ? scope.graph.getNode(current.parentId)
83
+ : undefined
84
+ if (!parent) return undefined
85
+ let ordinal = 0
86
+ for (const id of parent.childIds) {
87
+ if (id === current.id) break
88
+ if (scope.graph.getNode(id)?.name === current.name) ordinal++
89
+ }
90
+ steps.push(`${current.name}\u0000${ordinal}`)
91
+ current = parent
92
+ }
93
+ return steps.reverse().join('\u0001')
94
+ }
95
+
96
+ function snapshot(
97
+ graph: SceneGraph,
98
+ node: SceneNode,
99
+ owner: OwnerRef,
100
+ self: boolean,
101
+ fields: ReadonlyMap<string, unknown>
102
+ ): KeptOverrides | undefined {
103
+ const kept = new Map([...fields].filter(([field]) => !LINK_FIELDS.has(field)))
104
+ // A swap is kept as the component it shows; the new copy records it again when switched.
105
+ const swappedTo =
106
+ fields.has('componentId') && node.type === 'INSTANCE'
107
+ ? instanceMainComponent(graph, node)?.id
108
+ : undefined
109
+ if (kept.size === 0 && !swappedTo) return undefined
110
+ const values: Partial<SceneNode> = {}
111
+ const bindings: KeptOverrides['bindings'] = {}
112
+ const assignments: Record<string, string> = {}
113
+ for (const field of kept.keys()) {
114
+ const binding = overriddenBindingField(field)
115
+ const assigned = overriddenAssignmentId(field)
116
+ if (assigned !== undefined) {
117
+ if (Object.hasOwn(node.componentPropertyAssignments, assigned))
118
+ assignments[assigned] = node.componentPropertyAssignments[assigned]
119
+ } else if (binding !== null) {
120
+ bindings[binding] = {
121
+ variableId: node.boundVariables[binding],
122
+ scale: node.variableBindingScales[binding]
123
+ }
124
+ } else if (field in node && field !== 'boundVariables') {
125
+ Object.assign(values, { [field]: structuredClone(node[field as keyof SceneNode]) })
126
+ }
127
+ }
128
+ const allBindings = kept.has('boundVariables')
129
+ ? {
130
+ boundVariables: { ...node.boundVariables },
131
+ scales: { ...node.variableBindingScales }
132
+ }
133
+ : undefined
134
+ return { owner, self, fields: kept, values, bindings, allBindings, swappedTo, assignments }
135
+ }
136
+
137
+ /** Takes the overrides on a layer off its owners, outer ones and those inside the removed copy. */
138
+ function takeOverrides(
139
+ scope: OverrideScope,
140
+ node: SceneNode,
141
+ inner: readonly SceneNode[]
142
+ ): KeptOverrides[] {
143
+ const taken: KeptOverrides[] = []
144
+ for (const owner of scope.owners) {
145
+ const fields = owner.instanceOverrides.descendants.get(node.id)
146
+ if (!fields) continue
147
+ scope.willChange(owner)
148
+ owner.instanceOverrides.descendants.delete(node.id)
149
+ const kept = snapshot(scope.graph, node, { id: owner.id }, false, fields)
150
+ if (kept) taken.push(kept)
151
+ }
152
+ for (const owner of inner) {
153
+ const fields = owner.instanceOverrides.descendants.get(node.id)
154
+ const path = layerPath(scope, owner)
155
+ const kept = fields && path ? snapshot(scope.graph, node, { path }, false, fields) : undefined
156
+ if (kept) taken.push(kept)
157
+ }
158
+ const ownPath = node.type === 'INSTANCE' ? layerPath(scope, node) : undefined
159
+ const own = ownPath
160
+ ? snapshot(scope.graph, node, { path: ownPath }, true, node.instanceOverrides.self)
161
+ : undefined
162
+ if (own) taken.push(own)
163
+ return taken
164
+ }
165
+
166
+ /**
167
+ * Keeps what is overridden on a copy and its layers before a switch removes it. A layer
168
+ * removed with nothing overridden clears what an earlier removal kept for its path, so a value
169
+ * the user reset does not come back.
170
+ */
171
+ export function keepOverrides(
172
+ scope: OverrideScope,
173
+ copy: SceneNode,
174
+ inner: readonly SceneNode[] = []
175
+ ): void {
176
+ const path = layerPath(scope, copy)
177
+ const taken = takeOverrides(scope, copy, inner)
178
+ if (path && taken.length > 0) keptFor(scope, true).set(path, taken)
179
+ else if (path) keptFor(scope, false)?.delete(path)
180
+ const owners = copy.type === 'INSTANCE' ? [...inner, copy] : inner
181
+ for (const child of scope.graph.getChildren(copy.id)) keepOverrides(scope, child, owners)
182
+ }
183
+
184
+ function ownerOf(scope: OverrideScope, ref: OwnerRef): SceneNode | undefined {
185
+ if ('id' in ref) return scope.owners.find((owner) => owner.id === ref.id)
186
+ let current: SceneNode | undefined = scope.instance
187
+ for (const step of ref.path.split('\u0001')) {
188
+ const [name, ordinal] = step.split('\u0000')
189
+ current = current
190
+ ? scope.graph.getChildren(current.id).filter((child) => child.name === name)[Number(ordinal)]
191
+ : undefined
192
+ }
193
+ return current?.type === 'INSTANCE' ? current : undefined
194
+ }
195
+
196
+ /**
197
+ * Switches a nested instance switched to another variant again first, so what is kept for it
198
+ * and its layers lands on what it then shows. Returns the node as it then stands.
199
+ */
200
+ function switchAsKept(scope: OverrideScope, copy: SceneNode, kept: readonly KeptOverrides[]) {
201
+ const swappedTo = kept.find((entry) => entry.swappedTo)?.swappedTo
202
+ if (
203
+ swappedTo &&
204
+ copy.type === 'INSTANCE' &&
205
+ scope.graph.getNode(swappedTo) &&
206
+ instanceMainComponent(scope.graph, copy)?.id !== swappedTo
207
+ ) {
208
+ scope.graph.swapInstanceComponent(copy.id, swappedTo)
209
+ }
210
+ return scope.graph.getNode(copy.id) ?? copy
211
+ }
212
+
213
+ /** The bindings and unit scales a layer shows with the kept bindings applied over its own. */
214
+ function keptBindings(node: SceneNode, kept: readonly KeptOverrides[]) {
215
+ let boundVariables: Record<string, string> = { ...node.boundVariables }
216
+ let scales: Scales = { ...node.variableBindingScales }
217
+ for (const { bindings, allBindings } of kept) {
218
+ if (allBindings) {
219
+ boundVariables = { ...allBindings.boundVariables }
220
+ scales = { ...allBindings.scales }
221
+ }
222
+ for (const [field, { variableId, scale }] of Object.entries(bindings)) {
223
+ boundVariables = variableId
224
+ ? { ...boundVariables, [field]: variableId }
225
+ : omit(boundVariables, [field])
226
+ scales = scale !== undefined ? { ...scales, [field]: scale } : omit(scales, [field])
227
+ }
228
+ }
229
+ return { boundVariables, scales }
230
+ }
231
+
232
+ function giveOverrides(scope: OverrideScope, copy: SceneNode, saved: readonly KeptOverrides[]) {
233
+ // What an owner no longer standing around the copy overrode is not given back at all.
234
+ const resolved = saved.flatMap((entry) => {
235
+ const owner = ownerOf(scope, entry.owner)
236
+ return owner ? [{ entry, owner }] : []
237
+ })
238
+ const kept = resolved.map(({ entry }) => entry)
239
+ const node = switchAsKept(scope, copy, kept)
240
+ const values: Partial<SceneNode> = {}
241
+ for (const { entry, owner } of resolved) {
242
+ const { self, fields, values: shown } = entry
243
+ scope.willChange(owner)
244
+ const own = self
245
+ ? owner.instanceOverrides.self
246
+ : owner.instanceOverrides.descendants.get(node.id)
247
+ const merged = new Map([...(own ?? []), ...fields])
248
+ if (self) owner.instanceOverrides.self = merged
249
+ else owner.instanceOverrides.descendants.set(node.id, merged)
250
+ Object.assign(values, structuredClone(shown))
251
+ }
252
+ const { boundVariables, scales } = keptBindings(node, kept)
253
+ if (!isEqual(boundVariables, node.boundVariables)) values.boundVariables = boundVariables
254
+ if (!isEqual(scales, node.variableBindingScales)) values.variableBindingScales = scales
255
+ const assignments: Record<string, string> = Object.assign(
256
+ {},
257
+ ...kept.map((entry) => entry.assignments)
258
+ )
259
+ const assigns = Object.keys(assignments).length > 0
260
+ if (assigns) {
261
+ values.componentPropertyAssignments = { ...node.componentPropertyAssignments, ...assignments }
262
+ }
263
+ if (Object.keys(values).length > 0) scope.graph.updateNode(node.id, values)
264
+ // The values a nested instance is given show in its layers, as a matched copy's do.
265
+ if (assigns) reapplyComponentPropertyAssignments(scope.graph, node.id)
266
+ }
267
+
268
+ /** Gives a copy just placed, and its layers, what an earlier switch kept for their paths. */
269
+ export function restoreOverrides(scope: OverrideScope, copy: SceneNode): void {
270
+ const kept = keptFor(scope, false)
271
+ if (!kept?.size) return
272
+ const restore = (node: SceneNode) => {
273
+ const path = layerPath(scope, node)
274
+ const found = path ? kept.get(path) : undefined
275
+ // Kept, not taken: undoing this switch and switching again gives them back once more.
276
+ if (found) giveOverrides(scope, node, found)
277
+ for (const child of scope.graph.getChildren(node.id)) restore(child)
278
+ }
279
+ restore(copy)
280
+ }
@@ -59,3 +59,15 @@ export function sourceWithEffectiveFill(
59
59
  )
60
60
  }
61
61
  }
62
+
63
+ /** Nearest INSTANCE at or above `nodeId` — self, parent, grandparent, etc. */
64
+ export function findInstanceAncestor(graph: SceneGraph, nodeId: string): SceneNode | undefined {
65
+ let current = graph.nodes.get(nodeId)
66
+ while (current) {
67
+ if (current.type === 'INSTANCE') {
68
+ return current
69
+ }
70
+ current = current.parentId ? graph.nodes.get(current.parentId) : undefined
71
+ }
72
+ return undefined
73
+ }
@@ -109,6 +109,19 @@ export function cloneInstanceOverrideState(state: InstanceOverrideState): Instan
109
109
  export interface InstanceOverrideReferenceMapper {
110
110
  node: (id: string) => string
111
111
  variable: (id: string) => string
112
+ collection?: (id: string) => string | undefined
113
+ }
114
+
115
+ const VARIABLE_MODE_FIELD_PREFIX = 'variableModes/'
116
+
117
+ export function variableModeOverrideField(collectionId: string): InstanceOverrideField {
118
+ return `${VARIABLE_MODE_FIELD_PREFIX}${collectionId}`
119
+ }
120
+
121
+ export function variableModeOverrideCollection(field: InstanceOverrideField): string | undefined {
122
+ return field.startsWith(VARIABLE_MODE_FIELD_PREFIX)
123
+ ? field.slice(VARIABLE_MODE_FIELD_PREFIX.length)
124
+ : undefined
112
125
  }
113
126
 
114
127
  /** Remap runtime identities only; ordinary strings and source-format payloads stay opaque. */
@@ -122,8 +135,21 @@ export function remapInstanceOverrideState(
122
135
  if (overriddenBindingField(field) !== null) return references.variable(value)
123
136
  return value
124
137
  }
138
+ const remapField = (field: string): string | undefined => {
139
+ const collection = variableModeOverrideCollection(field)
140
+ if (collection === undefined || !references.collection) {
141
+ return field
142
+ }
143
+ const mapped = references.collection(collection)
144
+ return mapped === undefined ? undefined : variableModeOverrideField(mapped)
145
+ }
125
146
  const fields = (entries: ReadonlyMap<string, unknown>) =>
126
- new Map([...entries].map(([field, value]) => [field, remapValue(field, value)]))
147
+ new Map(
148
+ [...entries].flatMap(([field, value]) => {
149
+ const mapped = remapField(field)
150
+ return mapped === undefined ? [] : [[mapped, remapValue(field, value)] as const]
151
+ })
152
+ )
127
153
  const descendants = new Map<string, Map<string, unknown>>()
128
154
  for (const [id, entries] of state.descendants) {
129
155
  const mapped = references.node(id)
@@ -1,29 +1,82 @@
1
+ import { getNodeLocalMatrix } from '../coordinate'
2
+ import type { SceneGraph } from '../graph'
3
+ import { groupFitChanges } from '../group-bounds'
1
4
  import { enforcedAspectRatio } from '../layout/aspect-ratio'
2
- import { layoutConstrainedChildRect } from '../resize'
5
+ import Matrix, { type Mat3 } from '../matrix'
6
+ import { FITTED_CONTAINER_TYPES } from '../node-defaults'
7
+ import { constrainedChildRect, flipChanges } from '../resize'
3
8
  import type { SceneNode } from '../types'
4
9
 
5
- /** The fields that say where a copy sits in its parent. */
6
- export const PLACEMENT_FIELDS: ReadonlySet<string> = new Set(['x', 'y', 'width', 'height'])
10
+ /** The fields that say where a copy sits in its parent, and which way its constraints turned it. */
11
+ export const PLACEMENT_FIELDS: ReadonlySet<string> = new Set([
12
+ 'x',
13
+ 'y',
14
+ 'width',
15
+ 'height',
16
+ 'flipX',
17
+ 'flipY'
18
+ ])
19
+
20
+ /** The frame around the groups and booleans holding a layer, and their space mapped into its. */
21
+ function frameAround(
22
+ graph: SceneGraph,
23
+ parent: SceneNode
24
+ ): { frame: SceneNode; toFrame: Mat3 } | null {
25
+ let toFrame = Matrix.identity()
26
+ const frame = graph.closest(parent.id, (node) => {
27
+ if (!FITTED_CONTAINER_TYPES.has(node.type)) return true
28
+ toFrame = Matrix.multiply(getNodeLocalMatrix(node), toFrame)
29
+ return false
30
+ })
31
+ return frame ? { frame, toFrame } : null
32
+ }
7
33
 
8
34
  /**
9
35
  * The component's layer placed in the copy's parent: where its constraints put it when the
10
- * instance's frame has another size than the component's, as Figma resizes an instance.
36
+ * instance's frame has another size than the component's, as Figma resizes an instance. A group
37
+ * or boolean owns no constraints: it keeps the component's place, and a layer in it is placed in
38
+ * the frame around it and written back in the copy's group, which `fitCopiedGroup` then fits.
39
+ * Returns `source` itself when the layer stays where the component's is.
11
40
  */
12
41
  export function placedByConstraints(
42
+ graph: SceneGraph,
13
43
  source: SceneNode,
14
44
  compParent: SceneNode,
15
45
  instParent: SceneNode,
16
46
  factor: number
17
47
  ): SceneNode {
18
- const before = { width: compParent.width * factor, height: compParent.height * factor }
19
- if (before.width === instParent.width && before.height === instParent.height) return source
20
- const rect = layoutConstrainedChildRect(
21
- source,
48
+ if (FITTED_CONTAINER_TYPES.has(source.type)) return source
49
+ const comp = frameAround(graph, compParent)
50
+ const inst = frameAround(graph, instParent)
51
+ if (!comp || !inst) return source
52
+ const before = { width: comp.frame.width * factor, height: comp.frame.height * factor }
53
+ if (before.width === inst.frame.width && before.height === inst.frame.height) return source
54
+ // `source` is at the copy's scale, so the component's groups map it at that scale too.
55
+ const toFrame = Matrix.multiply(
56
+ Matrix.scaled(factor, factor),
57
+ Matrix.multiply(comp.toFrame, Matrix.scaled(1 / factor, 1 / factor))
58
+ )
59
+ const { flipsX, flipsY, ...box } = constrainedChildRect(
60
+ { ...Matrix.mapPoint(toFrame, source), width: source.width, height: source.height },
22
61
  before,
23
- instParent,
62
+ inst.frame,
24
63
  source.horizontalConstraint,
25
64
  source.verticalConstraint,
26
65
  enforcedAspectRatio(source)
27
66
  )
28
- return { ...source, ...rect }
67
+ const back = Matrix.invert(inst.toFrame)
68
+ const position = back ? Matrix.mapPoint(back, box) : box
69
+ const placed = { ...source, ...box, ...position, ...flipChanges(source, source, flipsX, flipsY) }
70
+ const moved = [...PLACEMENT_FIELDS].some(
71
+ (key) => placed[key as keyof SceneNode] !== source[key as keyof SceneNode]
72
+ )
73
+ return moved ? placed : source
74
+ }
75
+
76
+ /** Fits a copied group or boolean to its layers after the sync placed them by their constraints. */
77
+ export function fitCopiedGroup(graph: SceneGraph, groupId: string): void {
78
+ const group = graph.getNode(groupId)
79
+ if (!group || !FITTED_CONTAINER_TYPES.has(group.type)) return
80
+ const changes = groupFitChanges(graph, group, graph.getChildren(groupId))
81
+ for (const [id, change] of changes ?? []) graph.updateNode(id, change)
29
82
  }