@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
@@ -2,6 +2,7 @@ import { sha1 } from '@noble/hashes/legacy.js'
2
2
  import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js'
3
3
 
4
4
  import { TRANSPARENT } from '../constants'
5
+ import { imagePaintHashes } from '../images'
5
6
  import { readAllPluginData, withAllPluginData } from '../plugin-data/field'
6
7
  import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'
7
8
  import { randomHex } from '../random'
@@ -43,9 +44,7 @@ export function withShaderPaints(
43
44
  paints: readonly Fill[],
44
45
  shaders: readonly ShaderPaint[] = []
45
46
  ): SceneNode['pluginData'] {
46
- const shown = new Set(
47
- paints.flatMap((paint) => (paint.type === 'IMAGE' && paint.imageHash ? [paint.imageHash] : []))
48
- )
47
+ const shown = new Set(imagePaintHashes(paints))
49
48
  const byImage = new Map(readShaderPaints(node).map((shader) => [shader.image, shader]))
50
49
  for (const shader of shaders) byImage.set(shader.image, shader)
51
50
  return withAllPluginData(
@@ -1,6 +1,10 @@
1
- import { compact, isEqual, omit } from 'es-toolkit'
1
+ import { compact, isEqual, omit, pick } from 'es-toolkit'
2
2
 
3
+ import { BLACK } from './constants'
4
+ import { copyEffects, copyFills, copyLayoutGrids } from './copy'
3
5
  import type { SceneGraph } from './graph'
6
+ import { newStrokeGeometry } from './node-defaults'
7
+ import { randomHex } from './random'
4
8
  import { styleRunReferences } from './text/references'
5
9
  import type {
6
10
  CharacterStyleOverride,
@@ -99,6 +103,14 @@ export function sharedStyleTypeForKind(kind: SharedStyleKind): SharedStyleType {
99
103
  return STYLE_TYPES[kind]
100
104
  }
101
105
 
106
+ /** The document's shared style layers, by the source ID layers link them by. */
107
+ export function sharedStyleLayers(graph: SceneGraph): Map<string, SceneNode> {
108
+ const styles = new Map<string, SceneNode>()
109
+ for (const node of graph.getAllNodes())
110
+ if (node.sharedStyleType !== null && node.source.id) styles.set(node.source.id, node)
111
+ return styles
112
+ }
113
+
102
114
  export function getSharedStyles(graph: SceneGraph, kind: SharedStyleKind): SharedStyle[] {
103
115
  const type = sharedStyleTypeForKind(kind)
104
116
  const styles: SharedStyle[] = []
@@ -145,3 +157,80 @@ export function styleDetachmentChanges(
145
157
  }
146
158
  return next
147
159
  }
160
+
161
+ /** The node of the shared style with `id`, when the document holds one of `type`. */
162
+ export function findSharedStyle(
163
+ graph: SceneGraph,
164
+ id: string,
165
+ type: SharedStyleType
166
+ ): SceneNode | undefined {
167
+ if (!id) return undefined
168
+ for (const node of graph.getAllNodes()) {
169
+ if (node.sharedStyleType === type && node.source.id === id) return node
170
+ }
171
+ return undefined
172
+ }
173
+
174
+ /** A new local style's ID, in the shape Figma gives one: `S:` and a 40-digit hex key. */
175
+ export function newSharedStyleId(): string {
176
+ return `S:${randomHex(20)},`
177
+ }
178
+
179
+ const INTERNAL_CANVAS_NAME = 'Internal Only Canvas'
180
+
181
+ /** The page that holds the document's styles out of view, as a `.fig` file keeps them; made once. */
182
+ export function internalCanvas(graph: SceneGraph): SceneNode {
183
+ const existing = graph.getPages(true).find((page) => page.internalOnly)
184
+ if (existing) return existing
185
+ return graph.createNode('CANVAS', graph.rootId, {
186
+ name: INTERNAL_CANVAS_NAME,
187
+ width: 0,
188
+ height: 0,
189
+ internalOnly: true
190
+ })
191
+ }
192
+
193
+ /**
194
+ * A paint style's paints as a layer's strokes: every paint, each keeping the weight, alignment,
195
+ * and line style of the stroke it replaces, or of the layer's first; no paints clear the strokes
196
+ * (Figma desktop 126).
197
+ */
198
+ function strokesFromStyle(target: SceneNode, style: SceneNode): SceneNode['strokes'] {
199
+ // Without strokes, the layer keeps the weight and alignment a new stroke takes.
200
+ const fallback: Stroke = target.strokes[0] ?? {
201
+ type: 'SOLID',
202
+ color: BLACK,
203
+ opacity: 1,
204
+ visible: true,
205
+ ...newStrokeGeometry(target)
206
+ }
207
+ return copyFills(style.fills).map((paint, index) => ({
208
+ ...paint,
209
+ ...pick(target.strokes[index] ?? fallback, STROKE_LINE_KEYS)
210
+ }))
211
+ }
212
+
213
+ /**
214
+ * What a layer takes from a shared style it starts using: the style's fields and the reference
215
+ * that keeps it attached. Without the style's node, the reference alone.
216
+ */
217
+ export function sharedStylePatch(
218
+ kind: SharedStyleKind,
219
+ target: SceneNode,
220
+ styleId: string,
221
+ style: SceneNode | null
222
+ ): Partial<SceneNode> {
223
+ const patch: Partial<SceneNode> = { [STYLE_REF_KEYS[kind]]: styleId }
224
+ if (!style) return patch
225
+ if (kind === 'fill') patch.fills = copyFills(style.fills)
226
+ else if (kind === 'stroke') patch.strokes = strokesFromStyle(target, style)
227
+ else if (kind === 'effect') patch.effects = copyEffects(style.effects)
228
+ else if (kind === 'grid') patch.layoutGrids = copyLayoutGrids(style.layoutGrids)
229
+ else Object.assign(patch, structuredClone(pick(style, SHARED_STYLE_FIELDS.text)))
230
+ return patch
231
+ }
232
+
233
+ /** A layer leaving a shared style: it keeps the style's values, without the reference. */
234
+ export function sharedStyleDetachPatch(kind: SharedStyleKind): Partial<SceneNode> {
235
+ return { [STYLE_REF_KEYS[kind]]: null }
236
+ }
@@ -1,3 +1,4 @@
1
+ import { propertyOwner } from '../components/properties'
1
2
  import type { SceneGraph } from '../graph'
2
3
  import type { ComponentPropertyDefinition, SceneNode, SlotSettings } from '../types'
3
4
  import { slotPropertyId } from './frames'
@@ -10,13 +11,14 @@ export const DEFAULT_SLOT_SETTINGS: SlotSettings = {
10
11
  }
11
12
 
12
13
  /**
13
- * The main component a layer belongs to, which defines the slots inside it. Layers of an
14
- * instance are not authored there, so a layer under an instance has none.
14
+ * The main component or set that defines the slots inside a layer: the component the layer
15
+ * belongs to, or that component's set. Layers of an instance are not authored there, so a
16
+ * layer under an instance has none.
15
17
  */
16
18
  export function slotOwner(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
17
19
  let current = node.parentId ? graph.getNode(node.parentId) : undefined
18
20
  while (current && current.type !== 'CANVAS') {
19
- if (current.type === 'COMPONENT') return current
21
+ if (current.type === 'COMPONENT') return propertyOwner(graph, current)
20
22
  if (current.type === 'INSTANCE') return undefined
21
23
  current = current.parentId ? graph.getNode(current.parentId) : undefined
22
24
  }
@@ -3,6 +3,7 @@ import type { SceneGraph } from '../graph'
3
3
  import { getInstanceOverride, setInstanceOverride } from '../instances/overrides'
4
4
  import { cloneChildrenWithMapping } from '../instances/sync'
5
5
  import type { SceneNode } from '../types'
6
+ import { applyBoundPaintColorsIn } from '../variables/paints'
6
7
  import { ownsSlotContent, slotPropertyId } from './frames'
7
8
 
8
9
  /**
@@ -68,6 +69,7 @@ export function claimSlotContent(
68
69
  ): void {
69
70
  const { frame, instance, propertyId } = scope
70
71
  if (ownsSlotContent(graph, frame, propertyId)) return
72
+ graph.willChangeNode(instance)
71
73
  const overrides = instance.instanceOverrides
72
74
  for (const node of descendants(graph, frame, false)) {
73
75
  overrides.descendants.delete(node.id)
@@ -76,6 +78,7 @@ export function claimSlotContent(
76
78
  continue
77
79
  }
78
80
  // A nested instance keeps the overrides its former owner applied inside it.
81
+ graph.willChangeNode(node)
79
82
  for (const inner of descendants(graph, node, true)) {
80
83
  const fields = overrides.descendants.get(inner.id)
81
84
  if (!fields) continue
@@ -114,7 +117,9 @@ export function resetSlotContent(
114
117
  Reflect.deleteProperty(assignments, propertyId)
115
118
  graph.updateNode(instance.id, { componentPropertyAssignments: assignments })
116
119
  const source = componentSource(instance, frame)
117
- if (source && graph.nodes.has(source)) cloneChildrenWithMapping(graph, source, frame.id)
120
+ if (!source || !graph.nodes.has(source)) return
121
+ cloneChildrenWithMapping(graph, source, frame.id)
122
+ for (const childId of frame.childIds) applyBoundPaintColorsIn(graph, childId)
118
123
  }
119
124
 
120
125
  /** Remove everything from the slot; the instance keeps owning it, now empty. */
@@ -56,6 +56,8 @@ interface ParkedSlot {
56
56
  name: string
57
57
  /** Content layer ids, in layer order. */
58
58
  content: string[]
59
+ ownerId: string
60
+ propertyId: string
59
61
  }
60
62
 
61
63
  /** Owned slot content parked under an instance while its component changes. */
@@ -112,13 +114,18 @@ export function detachOwnedSlotContent(graph: SceneGraph, instance: SceneNode):
112
114
  continue
113
115
  const content = [...frame.childIds]
114
116
  for (const id of content) graph.insertChildAt(id, instance.id, instance.childIds.length)
115
- parked.slots.push({ path, name, content })
117
+ parked.slots.push({ path, name, content, ownerId: owner.id, propertyId })
116
118
  }
117
119
  }
118
120
  visit(instance, [])
119
121
  return parked
120
122
  }
121
123
 
124
+ function assignsOtherContent(owner: SceneNode, propertyId: string, slot: ParkedSlot): boolean {
125
+ const parkedFromHere = owner.id === slot.ownerId && propertyId === slot.propertyId
126
+ return !parkedFromHere && Object.hasOwn(owner.componentPropertyAssignments, propertyId)
127
+ }
128
+
122
129
  /** Move parked content into the slots of the same names and paths, and reassign them. */
123
130
  export function restoreOwnedSlotContent(
124
131
  graph: SceneGraph,
@@ -142,12 +149,7 @@ export function restoreOwnedSlotContent(
142
149
  return !!propertyId && slotName(graph, owner, propertyId) === slot.name
143
150
  })
144
151
  const propertyId = frame && slotPropertyId(frame)
145
- if (
146
- !owner ||
147
- !frame ||
148
- !propertyId ||
149
- Object.hasOwn(owner.componentPropertyAssignments, propertyId)
150
- ) {
152
+ if (!owner || !frame || !propertyId || assignsOtherContent(owner, propertyId, slot)) {
151
153
  for (const id of slot.content) graph.deleteNode(id)
152
154
  continue
153
155
  }
@@ -4,6 +4,11 @@ const RTL_CHAR_RE =
4
4
  /\p{Script=Arabic}|\p{Script=Hebrew}|\p{Script=Syriac}|\p{Script=Thaana}|\p{Script=Nko}|\p{Script=Adlam}/u
5
5
  const LTR_CHAR_RE = /\p{Script=Latin}|\p{Script=Cyrillic}|\p{Script=Greek}/u
6
6
 
7
+ /** Whether the text has a letter of a right-to-left script, such as Arabic or Hebrew. */
8
+ export function containsRTLText(text: string): boolean {
9
+ return RTL_CHAR_RE.test(text)
10
+ }
11
+
7
12
  export function detectTextDirection(text: string): Exclude<TextDirection, 'AUTO'> {
8
13
  for (const char of text) {
9
14
  if (RTL_CHAR_RE.test(char)) return 'RTL'
@@ -13,6 +13,16 @@ export function lineHeightPixels(lineHeight: TextLength | null, fontSize: number
13
13
  return lineHeight ? textLengthPixels(lineHeight, fontSize) : null
14
14
  }
15
15
 
16
+ /**
17
+ * How far apart a line height sets lines at `fontSize`, as Figma steps them: a height in pixels
18
+ * as it is, a percent one to whole pixels (Figma desktop 126); null when it is automatic.
19
+ */
20
+ export function lineStepPixels(lineHeight: TextLength | null, fontSize: number): number | null {
21
+ if (!lineHeight) return null
22
+ const pixels = textLengthPixels(lineHeight, fontSize)
23
+ return lineHeight.unit === 'PIXELS' ? pixels : Math.round(pixels)
24
+ }
25
+
16
26
  /** A length in pixels, as a bound number variable and most authored formats give one. */
17
27
  export function pixelLength(value: number): TextLength {
18
28
  return { value, unit: 'PIXELS' }
@@ -1,6 +1,6 @@
1
- import type { VisualBoundsNode } from '../geometry'
1
+ import type { SceneNode } from '../types'
2
2
 
3
- type TextBoundsNode = Pick<VisualBoundsNode, 'type' | 'height' | 'derivedTextHeight'>
3
+ type TextBoundsNode = Pick<SceneNode, 'type' | 'height' | 'derivedTextHeight'>
4
4
 
5
5
  /**
6
6
  * How far a text's lines reach below its box: a fixed-size box does not clip them, as in Figma,
@@ -51,7 +51,3 @@ export function textCacheInvalidationChanges(
51
51
  }
52
52
  return invalidated
53
53
  }
54
-
55
- export function invalidateTextCaches(node: SceneNode, changes: Partial<SceneNode>): void {
56
- Object.assign(node, textCacheInvalidationChanges(node, changes))
57
- }
@@ -24,14 +24,6 @@ function validate(target: SceneGraph, plan: GraphTransferPlan): void {
24
24
  plan.collections,
25
25
  variableIdentityReferences(plan.variables, plan.collections)
26
26
  )
27
- for (const [id, mode] of plan.activeModes) {
28
- if (
29
- !plan.collections
30
- .find((collection) => collection.id === id)
31
- ?.modes.some((entry) => entry.modeId === mode)
32
- )
33
- throw new Error(`Invalid transferred active mode ${id}:${mode}`)
34
- }
35
27
  for (const collection of plan.collections) claim(collection.id)
36
28
  for (const variable of plan.variables) claim(variable.id)
37
29
  const parents = new Set(target.nodes.keys())
@@ -64,7 +56,6 @@ export function applyGraphTransfer(target: SceneGraph, input: GraphTransferPlan)
64
56
  try {
65
57
  for (const collection of plan.collections) target.addCollection(collection)
66
58
  for (const variable of plan.variables) target.addVariable(variable)
67
- for (const [id, mode] of plan.activeModes) target.activeMode.set(id, mode)
68
59
  for (const [hash, bytes] of plan.images)
69
60
  if (!target.images.has(hash)) target.images.set(hash, bytes)
70
61
  for (const node of plan.nodes)
@@ -73,10 +64,7 @@ export function applyGraphTransfer(target: SceneGraph, input: GraphTransferPlan)
73
64
  for (const node of plan.nodes.toReversed())
74
65
  if (target.getNode(node.id)) target.deleteNode(node.id)
75
66
  for (const variable of plan.variables) target.variables.delete(variable.id)
76
- for (const collection of plan.collections) {
77
- target.variableCollections.delete(collection.id)
78
- target.activeMode.delete(collection.id)
79
- }
67
+ for (const collection of plan.collections) target.variableCollections.delete(collection.id)
80
68
  for (const hash of imageIds) target.images.delete(hash)
81
69
  throw error
82
70
  }
@@ -3,44 +3,103 @@ import { isEqual, mapValues } from 'es-toolkit'
3
3
  import { componentPropertyDefinitions } from '../components/properties'
4
4
  import type { SceneGraph } from '../graph'
5
5
  import { remapInstanceOverrideState } from '../instances/overrides'
6
+ import { remapStyleRunReferences } from '../text/references'
6
7
  import type { ComponentPropertyDefinition, SceneNode } from '../types'
7
8
 
9
+ /** How a layer's references to other layers are resolved anew. */
10
+ export interface ReferenceResolvers {
11
+ /** The component a reference names now; `previous` is the one the layer named, if any. */
12
+ component: (id: string, previous: string | null) => string
13
+ /** The layer an override names now; `component` is the one its instance now draws. */
14
+ layer: (id: string, component: string | null) => string
15
+ /** The layer a connector's end follows now. */
16
+ endpoint: (id: string) => string
17
+ /**
18
+ * The layer a text link names now. Without it links keep naming their layers, as Figma's
19
+ * duplicate and paste do (desktop 126); copies into another document, which lacks those
20
+ * layers, pass it.
21
+ */
22
+ link?: (id: string) => string
23
+ variable: (id: string) => string
24
+ }
25
+
26
+ /** Copies placed in another document, whose text links must follow the copy. */
27
+ export interface CopiedReferenceOptions {
28
+ followTextLinks?: boolean
29
+ }
30
+
8
31
  function swapPropertyIds(definitions: readonly ComponentPropertyDefinition[]): string[] {
9
32
  return definitions
10
33
  .filter((definition) => definition.type === 'INSTANCE_SWAP')
11
34
  .map((definition) => definition.id)
12
35
  }
13
36
 
14
- function remappedReferences(
37
+ /** The instance-swap properties `nodes` define, whose values name components. */
38
+ export function instanceSwapPropertyIds(nodes: Iterable<SceneNode>): Set<string> {
39
+ return new Set([...nodes].flatMap((node) => swapPropertyIds(node.componentPropertyDefinitions)))
40
+ }
41
+
42
+ /**
43
+ * `node`'s references to other layers, resolved anew: its main component, the layers its
44
+ * overrides change and the components they swap in, the components its instance-swap properties
45
+ * default to or are set to, and the layers its connector ends follow. `previous` is the layer
46
+ * whose references these replace, so a resolver can keep what it already pointed at. Only the
47
+ * fields whose references change are returned: every write is an update collaboration sends.
48
+ */
49
+ export function resolvedReferences(
15
50
  node: SceneNode,
16
- mapped: (id: string) => string,
51
+ previous: SceneNode,
52
+ resolve: ReferenceResolvers,
17
53
  swapProperties: ReadonlySet<string>
18
54
  ): Partial<SceneNode> {
19
55
  const changes: Partial<SceneNode> = {}
20
- if (node.componentId && mapped(node.componentId) !== node.componentId)
21
- changes.componentId = mapped(node.componentId)
22
- // Only what a mapping changes: every write is an update collaboration sends.
56
+ const componentId = node.componentId
57
+ ? resolve.component(node.componentId, previous.componentId)
58
+ : null
59
+ if (componentId !== node.componentId) changes.componentId = componentId
23
60
  const overrides = remapInstanceOverrideState(node.instanceOverrides, {
24
- node: mapped,
25
- variable: (id) => id
61
+ node: (id) => resolve.layer(id, componentId),
62
+ variable: resolve.variable
26
63
  })
27
64
  if (!isEqual(overrides, node.instanceOverrides)) changes.instanceOverrides = overrides
65
+ const previousDefaults = new Map(
66
+ previous.componentPropertyDefinitions.map((definition) => [
67
+ definition.id,
68
+ definition.defaultValue
69
+ ])
70
+ )
28
71
  const definitions = node.componentPropertyDefinitions.map((definition) =>
29
72
  definition.type === 'INSTANCE_SWAP' && definition.defaultValue
30
- ? { ...definition, defaultValue: mapped(definition.defaultValue) }
73
+ ? {
74
+ ...definition,
75
+ defaultValue: resolve.component(
76
+ definition.defaultValue,
77
+ previousDefaults.get(definition.id) ?? null
78
+ )
79
+ }
31
80
  : definition
32
81
  )
33
82
  if (!isEqual(definitions, node.componentPropertyDefinitions))
34
83
  changes.componentPropertyDefinitions = definitions
35
84
  const assignments = mapValues(node.componentPropertyAssignments, (value, id) =>
36
- swapProperties.has(id) ? mapped(value) : value
85
+ swapProperties.has(id)
86
+ ? resolve.component(value, previous.componentPropertyAssignments[id] ?? null)
87
+ : value
37
88
  )
38
89
  if (!isEqual(assignments, node.componentPropertyAssignments))
39
90
  changes.componentPropertyAssignments = assignments
40
91
  for (const field of ['connectorStart', 'connectorEnd'] as const) {
41
92
  const endpoint = node[field]
42
- if (endpoint?.nodeId && mapped(endpoint.nodeId) !== endpoint.nodeId)
43
- changes[field] = { ...endpoint, nodeId: mapped(endpoint.nodeId) }
93
+ const nodeId = endpoint?.nodeId ? resolve.endpoint(endpoint.nodeId) : null
94
+ if (endpoint && nodeId && nodeId !== endpoint.nodeId) changes[field] = { ...endpoint, nodeId }
95
+ }
96
+ if (resolve.link) {
97
+ const styleRuns = remapStyleRunReferences(node.styleRuns, {
98
+ variable: (id) => id,
99
+ style: (id) => id,
100
+ node: resolve.link
101
+ })
102
+ if (!isEqual(styleRuns, node.styleRuns)) changes.styleRuns = styleRuns
44
103
  }
45
104
  return changes
46
105
  }
@@ -51,22 +110,31 @@ function remappedReferences(
51
110
  * components an instance-swap property defaults to or is set to, and the layers a connector's
52
111
  * ends follow. `mappedIds` maps each copied layer to its copy, such as a library copy or a design
53
112
  * JSX render folded into the layers it replaces; a reference outside the copy stays as it is, and
54
- * so do variable IDs, since the copies keep their source's variables.
113
+ * so do variable IDs, since the copies keep their source's variables. Text links follow the copy
114
+ * only with `followTextLinks`.
55
115
  */
56
116
  export function remapCopiedReferences(
57
117
  graph: SceneGraph,
58
- mappedIds: ReadonlyMap<string, string>
118
+ mappedIds: ReadonlyMap<string, string>,
119
+ options: CopiedReferenceOptions = {}
59
120
  ): void {
60
121
  const mapped = (id: string) => mappedIds.get(id) ?? id
122
+ const resolve: ReferenceResolvers = {
123
+ component: mapped,
124
+ layer: mapped,
125
+ endpoint: mapped,
126
+ link: options.followTextLinks ? mapped : undefined,
127
+ variable: (id) => id
128
+ }
61
129
  const copies = [...mappedIds.values()].flatMap((id) => graph.getNode(id) ?? [])
62
- const copiedSwaps = copies.flatMap((node) => swapPropertyIds(node.componentPropertyDefinitions))
130
+ const copiedSwaps = instanceSwapPropertyIds(copies)
63
131
  for (const node of copies) {
64
132
  // An instance's swap property may be defined on a component the copy leaves behind.
65
133
  const swapProperties = new Set([
66
134
  ...copiedSwaps,
67
135
  ...swapPropertyIds(componentPropertyDefinitions(graph, node))
68
136
  ])
69
- const changes = remappedReferences(node, mapped, swapProperties)
137
+ const changes = resolvedReferences(node, node, resolve, swapProperties)
70
138
  if (Object.keys(changes).length > 0) graph.updateNode(node.id, changes)
71
139
  }
72
140
  }
@@ -0,0 +1,126 @@
1
+ import type { SceneGraph } from '../graph'
2
+ import { imagePaintHashes } from '../images'
3
+ import { overriddenBindingField } from '../instances/overrides'
4
+ import { referencedStyleIds, sharedStyleLayers } from '../shared-styles'
5
+ import { styleRunReferences } from '../text/references'
6
+ import type { SceneNode } from '../types'
7
+ import { isUnsupportedVariableValue, variableValueReferences } from '../variables/value'
8
+ import { regionFills } from '../vector-network'
9
+
10
+ /** What layers need from their document to draw the same in another one. */
11
+ export interface LayerDependencies {
12
+ /** The variables the layers and their styles bind, and every variable those alias. */
13
+ variables: Set<string>
14
+ /**
15
+ * The collections of those variables, those the layers set a mode of, and those a kept
16
+ * expression names a mode of.
17
+ */
18
+ collections: Set<string>
19
+ /** The shared styles the layers use, as the style layers that define them. */
20
+ styles: SceneNode[]
21
+ /** The images the layers and their styles paint with. */
22
+ images: Set<string>
23
+ }
24
+
25
+ /** The variables a layer names: its own bindings, its characters', and its overrides'. */
26
+ export function layerVariableIds(node: SceneNode): string[] {
27
+ const { self, descendants } = node.instanceOverrides
28
+ const overridden = [...self, ...[...descendants.values()].flatMap((fields) => [...fields])]
29
+ return [
30
+ ...Object.values(node.boundVariables),
31
+ ...styleRunReferences(node.styleRuns).variables,
32
+ ...overridden.flatMap(([field, value]) =>
33
+ typeof value === 'string' && overriddenBindingField(field) !== null ? [value] : []
34
+ )
35
+ ]
36
+ }
37
+
38
+ /**
39
+ * The images a layer paints with: its fills, strokes, vector regions, text decorations, and
40
+ * characters.
41
+ */
42
+ export function layerImageHashes(node: SceneNode): string[] {
43
+ return imagePaintHashes([
44
+ ...node.fills,
45
+ ...node.strokes,
46
+ ...regionFills(node.vectorNetwork),
47
+ ...node.textDecorationFills,
48
+ ...node.styleRuns.flatMap((run) => [
49
+ ...(run.style.fills ?? []),
50
+ ...(run.style.textDecorationFills ?? [])
51
+ ])
52
+ ])
53
+ }
54
+
55
+ /**
56
+ * The variables, collections, styles, and images `layers` depend on, as a copy into another
57
+ * document must carry them: a style brings what it binds and paints with in turn, and a variable
58
+ * brings the variables it aliases.
59
+ */
60
+ export function layerDependencies(
61
+ graph: SceneGraph,
62
+ layers: Iterable<SceneNode>
63
+ ): LayerDependencies {
64
+ const collections = new Set<string>()
65
+ const images = new Set<string>()
66
+ const styles = new Map<string, SceneNode>()
67
+ const pending: string[] = []
68
+ let styleLayers: Map<string, SceneNode> | undefined
69
+ const styleLayer = (id: string): SceneNode | undefined =>
70
+ (styleLayers ??= sharedStyleLayers(graph)).get(id)
71
+ const visit = (node: SceneNode): void => {
72
+ for (const hash of layerImageHashes(node)) images.add(hash)
73
+ pending.push(...layerVariableIds(node))
74
+ for (const id of Object.keys(node.variableModes))
75
+ if (graph.variableCollections.has(id)) collections.add(id)
76
+ for (const id of referencedStyleIds(node)) {
77
+ const style = styles.has(id) ? undefined : styleLayer(id)
78
+ if (!style) continue
79
+ styles.set(id, style)
80
+ visit(style)
81
+ }
82
+ }
83
+ for (const node of layers) visit(node)
84
+ const resolved = variableDependencies(graph, pending)
85
+ for (const id of resolved.collections) collections.add(id)
86
+ return { variables: resolved.variables, collections, styles: [...styles.values()], images }
87
+ }
88
+
89
+ /**
90
+ * These variables and every variable they alias, at any depth, with their collections and the
91
+ * collections a kept expression names a mode of.
92
+ */
93
+ export function variableDependencies(
94
+ graph: SceneGraph,
95
+ ids: Iterable<string>
96
+ ): Pick<LayerDependencies, 'variables' | 'collections'> {
97
+ const pending = [...ids]
98
+ const variables = new Set<string>()
99
+ const collections = new Set<string>()
100
+ let modeCollections: Map<string, string> | undefined
101
+ const collectionOfMode = (modeId: string): string | undefined => {
102
+ modeCollections ??= new Map(
103
+ [...graph.variableCollections.values()].flatMap((collection) =>
104
+ collection.modes.map((mode) => [mode.modeId, collection.id] as const)
105
+ )
106
+ )
107
+ return modeCollections.get(modeId)
108
+ }
109
+ for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
110
+ if (variables.has(id)) continue
111
+ const variable = graph.variables.get(id)
112
+ if (!variable) continue
113
+ variables.add(id)
114
+ collections.add(variable.collectionId)
115
+ for (const value of Object.values(variable.valuesByMode)) {
116
+ pending.push(...variableValueReferences(value))
117
+ // A kept expression can name a mode of a collection none of its aliases reach.
118
+ if (!isUnsupportedVariableValue(value)) continue
119
+ for (const modeId of value.modes) {
120
+ const collection = collectionOfMode(modeId)
121
+ if (collection) collections.add(collection)
122
+ }
123
+ }
124
+ }
125
+ return { variables, collections }
126
+ }
@@ -33,12 +33,6 @@ export function captureTransferredState(
33
33
  requireValue(graph.variableCollections.get(collection.id), 'collection', collection.id)
34
34
  )
35
35
  ),
36
- activeModes: new Map(
37
- original.collections.map((collection) => [
38
- collection.id,
39
- requireValue(graph.activeMode.get(collection.id), 'active mode', collection.id)
40
- ])
41
- ),
42
36
  images: new Map(
43
37
  [...original.images.keys()].map((hash) => [
44
38
  hash,
@@ -3,5 +3,23 @@ export { captureTransferredState } from './history'
3
3
  export { applyGraphTransfer, type GraphTransferPlan } from './apply'
4
4
  export { prepareGraphTransfer, type GraphTransferInput } from './plan'
5
5
  export { prepareNodeTransfer, type NodeTransferReferences } from './nodes'
6
- export { prepareVariableTransfer, type VariableTransferReferences } from './variables'
7
- export { remapCopiedReferences } from './copied'
6
+ export {
7
+ copyVariableTables,
8
+ prepareVariableTransfer,
9
+ type VariableTransferReferences
10
+ } from './variables'
11
+ export {
12
+ instanceSwapPropertyIds,
13
+ remapCopiedReferences,
14
+ resolvedReferences,
15
+ type CopiedReferenceOptions,
16
+ type ReferenceResolvers
17
+ } from './copied'
18
+ export {
19
+ layerDependencies,
20
+ layerImageHashes,
21
+ layerVariableIds,
22
+ variableDependencies,
23
+ type LayerDependencies
24
+ } from './dependencies'
25
+ export { remappedResourceReferences, type ResourceReferences } from './resources'