@open-take/compositor 0.5.2 → 0.6.0

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 (439) hide show
  1. package/dist/cdp-CzT5BKNY.js +3 -0
  2. package/dist/cdp-QXbQfXna.js +373 -0
  3. package/dist/cdp-QXbQfXna.js.map +1 -0
  4. package/dist/index.d.ts +4327 -195
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +26282 -160
  7. package/dist/index.js.map +1 -1
  8. package/dist/server-841PMF2H.js +163 -0
  9. package/dist/server-841PMF2H.js.map +1 -0
  10. package/dist/server-C2aVv4-t.js +3 -0
  11. package/kit/audio.mjs +2366 -0
  12. package/kit/camera.js +587 -0
  13. package/kit/cursor.js +264 -0
  14. package/kit/cut.js +427 -0
  15. package/kit/draw.js +346 -0
  16. package/kit/look.js +589 -0
  17. package/kit/map.js +170 -0
  18. package/kit/media.js +3568 -0
  19. package/kit/motion.js +1115 -0
  20. package/kit/seams.js +1006 -0
  21. package/kit/stage.js +923 -0
  22. package/kit/time.js +812 -0
  23. package/kit/world.js +1072 -0
  24. package/package.json +7 -3
  25. package/src/ffmpeg.ts +63 -0
  26. package/src/film/beats.ts +1285 -0
  27. package/src/film/carry.ts +221 -0
  28. package/src/film/changes.ts +330 -0
  29. package/src/film/check.ts +996 -0
  30. package/src/film/clock.ts +2530 -0
  31. package/src/film/components-probe.ts +809 -0
  32. package/src/film/components.ts +1566 -0
  33. package/src/film/frames.ts +389 -0
  34. package/src/film/gallery.ts +3276 -0
  35. package/src/film/kit.ts +515 -0
  36. package/src/film/layout.ts +701 -0
  37. package/src/film/listen.ts +1979 -0
  38. package/src/film/look.ts +3829 -0
  39. package/src/film/materials.ts +976 -0
  40. package/src/film/moving.ts +248 -0
  41. package/src/film/part-files.ts +729 -0
  42. package/src/film/picture.ts +74 -0
  43. package/src/film/probe-text.ts +843 -0
  44. package/src/film/probe.ts +3456 -0
  45. package/src/film/provenance.ts +596 -0
  46. package/src/film/render.ts +2447 -0
  47. package/src/film/report.ts +1631 -0
  48. package/src/film/sheet.ts +156 -0
  49. package/src/film/study-declared.ts +123 -0
  50. package/src/film/study-picture.ts +791 -0
  51. package/src/film/study-sound.ts +416 -0
  52. package/src/film/study.ts +864 -0
  53. package/src/film/tuning.ts +155 -0
  54. package/src/film/viewer.ts +174 -0
  55. package/src/html/cdp.ts +575 -0
  56. package/src/html/server.ts +208 -0
  57. package/src/index.ts +202 -0
  58. package/src/launch-render.ts +3 -2
  59. package/src/math.ts +3 -3
  60. package/src/mjpeg-matroska.ts +228 -0
  61. package/src/render.ts +6 -4
  62. package/src/state-diff.ts +215 -0
  63. package/src/state-index.ts +99 -0
  64. package/src/types.ts +59 -1
  65. package/starter/PLAN.md +84 -0
  66. package/starter/START.md +460 -0
  67. package/starter/lib/anim.js +760 -0
  68. package/starter/lib/devices.js +1402 -0
  69. package/starter/lib/handoff.js +421 -0
  70. package/starter/lib/hud.js +1879 -0
  71. package/starter/lib/measure.mjs +154 -0
  72. package/starter/lib/moves.js +1095 -0
  73. package/starter/lib/palette.mjs +803 -0
  74. package/starter/lib/physics-seeds.mjs +198 -0
  75. package/starter/lib/physics.js +738 -0
  76. package/starter/lib/sampler.mjs +242 -0
  77. package/starter/lib/solid.js +727 -0
  78. package/starter/lib/space.js +1227 -0
  79. package/starter/lib/swarm.js +1398 -0
  80. package/starter/reference/motion.md +276 -0
  81. package/starter/reference/structure.md +466 -0
  82. package/starter/reference/travel/motion.md +433 -0
  83. package/starter/reference/travel/sound.md +241 -0
  84. package/starter/reference/travel/structure.md +518 -0
  85. package/starter/samples/NOTICE.txt +55 -0
  86. package/starter/samples/celli/celli_24_1.opus +0 -0
  87. package/starter/samples/celli/celli_24_3.opus +0 -0
  88. package/starter/samples/celli/celli_28_1.opus +0 -0
  89. package/starter/samples/celli/celli_28_3.opus +0 -0
  90. package/starter/samples/celli/celli_31_1.opus +0 -0
  91. package/starter/samples/celli/celli_31_3.opus +0 -0
  92. package/starter/samples/celli/celli_35_1.opus +0 -0
  93. package/starter/samples/celli/celli_35_3.opus +0 -0
  94. package/starter/samples/celli/celli_38_1.opus +0 -0
  95. package/starter/samples/celli/celli_38_3.opus +0 -0
  96. package/starter/samples/celli/celli_41_1.opus +0 -0
  97. package/starter/samples/celli/celli_41_3.opus +0 -0
  98. package/starter/samples/celli/celli_45_1.opus +0 -0
  99. package/starter/samples/celli/celli_45_3.opus +0 -0
  100. package/starter/samples/celli/celli_48_1.opus +0 -0
  101. package/starter/samples/celli/celli_48_3.opus +0 -0
  102. package/starter/samples/celli/celli_52_1.opus +0 -0
  103. package/starter/samples/celli/celli_52_3.opus +0 -0
  104. package/starter/samples/celli/celli_55_1.opus +0 -0
  105. package/starter/samples/celli/celli_55_3.opus +0 -0
  106. package/starter/samples/celli/celli_59_1.opus +0 -0
  107. package/starter/samples/celli/celli_59_3.opus +0 -0
  108. package/starter/samples/celli/celli_62_1.opus +0 -0
  109. package/starter/samples/celli/celli_62_3.opus +0 -0
  110. package/starter/samples/celli/celli_65_1.opus +0 -0
  111. package/starter/samples/celli/celli_65_3.opus +0 -0
  112. package/starter/samples/crash/crash_f_1.flac +0 -0
  113. package/starter/samples/crash/crash_fff_1.flac +0 -0
  114. package/starter/samples/ebass/ebass_0_x.flac +0 -0
  115. package/starter/samples/ebass/ebass_10_x.flac +0 -0
  116. package/starter/samples/ebass/ebass_11_x.flac +0 -0
  117. package/starter/samples/ebass/ebass_1_x.flac +0 -0
  118. package/starter/samples/ebass/ebass_2_x.flac +0 -0
  119. package/starter/samples/ebass/ebass_3_x.flac +0 -0
  120. package/starter/samples/ebass/ebass_4_x.flac +0 -0
  121. package/starter/samples/ebass/ebass_5_x.flac +0 -0
  122. package/starter/samples/ebass/ebass_6_x.flac +0 -0
  123. package/starter/samples/ebass/ebass_7_x.flac +0 -0
  124. package/starter/samples/ebass/ebass_8_x.flac +0 -0
  125. package/starter/samples/ebass/ebass_9_x.flac +0 -0
  126. package/starter/samples/ek_clap/ek_clap_1_1.flac +0 -0
  127. package/starter/samples/ek_hat/ek_hat_1_1.flac +0 -0
  128. package/starter/samples/ek_hat/ek_hat_1_2.flac +0 -0
  129. package/starter/samples/ek_kick/ek_kick_1_1.flac +0 -0
  130. package/starter/samples/ek_kick/ek_kick_1_2.flac +0 -0
  131. package/starter/samples/glock/glock_67_x.flac +0 -0
  132. package/starter/samples/glock/glock_72_x.flac +0 -0
  133. package/starter/samples/glock/glock_79_x.flac +0 -0
  134. package/starter/samples/glock/glock_84_x.flac +0 -0
  135. package/starter/samples/glock/glock_91_x.flac +0 -0
  136. package/starter/samples/glock/glock_96_x.flac +0 -0
  137. package/starter/samples/harp/harp_101_f.opus +0 -0
  138. package/starter/samples/harp/harp_28_f.opus +0 -0
  139. package/starter/samples/harp/harp_31_mp.opus +0 -0
  140. package/starter/samples/harp/harp_35_mf.opus +0 -0
  141. package/starter/samples/harp/harp_38_mf.opus +0 -0
  142. package/starter/samples/harp/harp_41_mf.opus +0 -0
  143. package/starter/samples/harp/harp_45_mf.opus +0 -0
  144. package/starter/samples/harp/harp_48_mf.opus +0 -0
  145. package/starter/samples/harp/harp_52_mf.opus +0 -0
  146. package/starter/samples/harp/harp_55_mf.opus +0 -0
  147. package/starter/samples/harp/harp_59_mf.opus +0 -0
  148. package/starter/samples/harp/harp_62_mf.opus +0 -0
  149. package/starter/samples/harp/harp_65_mf.opus +0 -0
  150. package/starter/samples/harp/harp_69_mf.opus +0 -0
  151. package/starter/samples/harp/harp_72_mf.opus +0 -0
  152. package/starter/samples/harp/harp_76_mf.opus +0 -0
  153. package/starter/samples/harp/harp_79_mf.opus +0 -0
  154. package/starter/samples/harp/harp_83_mf.opus +0 -0
  155. package/starter/samples/harp/harp_86_mf.opus +0 -0
  156. package/starter/samples/harp/harp_89_mf.opus +0 -0
  157. package/starter/samples/harp/harp_93_mf.opus +0 -0
  158. package/starter/samples/harp/harp_95_mf.opus +0 -0
  159. package/starter/samples/harp/harp_98_f.opus +0 -0
  160. package/starter/samples/manifest.json +2951 -0
  161. package/starter/samples/marimba/marimba_29_x.flac +0 -0
  162. package/starter/samples/marimba/marimba_36_x.flac +0 -0
  163. package/starter/samples/marimba/marimba_43_x.flac +0 -0
  164. package/starter/samples/marimba/marimba_47_x.flac +0 -0
  165. package/starter/samples/marimba/marimba_53_x.flac +0 -0
  166. package/starter/samples/marimba/marimba_60_x.flac +0 -0
  167. package/starter/samples/marimba/marimba_67_x.flac +0 -0
  168. package/starter/samples/marimba/marimba_71_x.flac +0 -0
  169. package/starter/samples/marimba/marimba_77_x.flac +0 -0
  170. package/starter/samples/marimba/marimba_84_x.flac +0 -0
  171. package/starter/samples/piano/piano_102_12.opus +0 -0
  172. package/starter/samples/piano/piano_102_16.opus +0 -0
  173. package/starter/samples/piano/piano_102_4.opus +0 -0
  174. package/starter/samples/piano/piano_102_8.opus +0 -0
  175. package/starter/samples/piano/piano_105_12.opus +0 -0
  176. package/starter/samples/piano/piano_105_16.opus +0 -0
  177. package/starter/samples/piano/piano_105_4.opus +0 -0
  178. package/starter/samples/piano/piano_105_8.opus +0 -0
  179. package/starter/samples/piano/piano_108_12.opus +0 -0
  180. package/starter/samples/piano/piano_108_16.opus +0 -0
  181. package/starter/samples/piano/piano_108_4.opus +0 -0
  182. package/starter/samples/piano/piano_108_8.opus +0 -0
  183. package/starter/samples/piano/piano_21_12.opus +0 -0
  184. package/starter/samples/piano/piano_21_16.opus +0 -0
  185. package/starter/samples/piano/piano_21_4.opus +0 -0
  186. package/starter/samples/piano/piano_21_8.opus +0 -0
  187. package/starter/samples/piano/piano_24_12.opus +0 -0
  188. package/starter/samples/piano/piano_24_16.opus +0 -0
  189. package/starter/samples/piano/piano_24_4.opus +0 -0
  190. package/starter/samples/piano/piano_24_8.opus +0 -0
  191. package/starter/samples/piano/piano_27_12.opus +0 -0
  192. package/starter/samples/piano/piano_27_16.opus +0 -0
  193. package/starter/samples/piano/piano_27_4.opus +0 -0
  194. package/starter/samples/piano/piano_27_8.opus +0 -0
  195. package/starter/samples/piano/piano_30_12.opus +0 -0
  196. package/starter/samples/piano/piano_30_16.opus +0 -0
  197. package/starter/samples/piano/piano_30_4.opus +0 -0
  198. package/starter/samples/piano/piano_30_8.opus +0 -0
  199. package/starter/samples/piano/piano_33_12.opus +0 -0
  200. package/starter/samples/piano/piano_33_16.opus +0 -0
  201. package/starter/samples/piano/piano_33_4.opus +0 -0
  202. package/starter/samples/piano/piano_33_8.opus +0 -0
  203. package/starter/samples/piano/piano_36_12.opus +0 -0
  204. package/starter/samples/piano/piano_36_16.opus +0 -0
  205. package/starter/samples/piano/piano_36_4.opus +0 -0
  206. package/starter/samples/piano/piano_36_8.opus +0 -0
  207. package/starter/samples/piano/piano_39_12.opus +0 -0
  208. package/starter/samples/piano/piano_39_16.opus +0 -0
  209. package/starter/samples/piano/piano_39_4.opus +0 -0
  210. package/starter/samples/piano/piano_39_8.opus +0 -0
  211. package/starter/samples/piano/piano_42_12.opus +0 -0
  212. package/starter/samples/piano/piano_42_16.opus +0 -0
  213. package/starter/samples/piano/piano_42_4.opus +0 -0
  214. package/starter/samples/piano/piano_42_8.opus +0 -0
  215. package/starter/samples/piano/piano_45_12.opus +0 -0
  216. package/starter/samples/piano/piano_45_16.opus +0 -0
  217. package/starter/samples/piano/piano_45_4.opus +0 -0
  218. package/starter/samples/piano/piano_45_8.opus +0 -0
  219. package/starter/samples/piano/piano_48_12.opus +0 -0
  220. package/starter/samples/piano/piano_48_16.opus +0 -0
  221. package/starter/samples/piano/piano_48_4.opus +0 -0
  222. package/starter/samples/piano/piano_48_8.opus +0 -0
  223. package/starter/samples/piano/piano_51_12.opus +0 -0
  224. package/starter/samples/piano/piano_51_16.opus +0 -0
  225. package/starter/samples/piano/piano_51_4.opus +0 -0
  226. package/starter/samples/piano/piano_51_8.opus +0 -0
  227. package/starter/samples/piano/piano_54_12.opus +0 -0
  228. package/starter/samples/piano/piano_54_16.opus +0 -0
  229. package/starter/samples/piano/piano_54_4.opus +0 -0
  230. package/starter/samples/piano/piano_54_8.opus +0 -0
  231. package/starter/samples/piano/piano_57_12.opus +0 -0
  232. package/starter/samples/piano/piano_57_16.opus +0 -0
  233. package/starter/samples/piano/piano_57_4.opus +0 -0
  234. package/starter/samples/piano/piano_57_8.opus +0 -0
  235. package/starter/samples/piano/piano_60_12.opus +0 -0
  236. package/starter/samples/piano/piano_60_16.opus +0 -0
  237. package/starter/samples/piano/piano_60_4.opus +0 -0
  238. package/starter/samples/piano/piano_60_8.opus +0 -0
  239. package/starter/samples/piano/piano_63_12.opus +0 -0
  240. package/starter/samples/piano/piano_63_16.opus +0 -0
  241. package/starter/samples/piano/piano_63_4.opus +0 -0
  242. package/starter/samples/piano/piano_63_8.opus +0 -0
  243. package/starter/samples/piano/piano_66_12.opus +0 -0
  244. package/starter/samples/piano/piano_66_16.opus +0 -0
  245. package/starter/samples/piano/piano_66_4.opus +0 -0
  246. package/starter/samples/piano/piano_66_8.opus +0 -0
  247. package/starter/samples/piano/piano_69_12.opus +0 -0
  248. package/starter/samples/piano/piano_69_16.opus +0 -0
  249. package/starter/samples/piano/piano_69_4.opus +0 -0
  250. package/starter/samples/piano/piano_69_8.opus +0 -0
  251. package/starter/samples/piano/piano_72_12.opus +0 -0
  252. package/starter/samples/piano/piano_72_16.opus +0 -0
  253. package/starter/samples/piano/piano_72_4.opus +0 -0
  254. package/starter/samples/piano/piano_72_8.opus +0 -0
  255. package/starter/samples/piano/piano_75_12.opus +0 -0
  256. package/starter/samples/piano/piano_75_16.opus +0 -0
  257. package/starter/samples/piano/piano_75_4.opus +0 -0
  258. package/starter/samples/piano/piano_75_8.opus +0 -0
  259. package/starter/samples/piano/piano_78_12.opus +0 -0
  260. package/starter/samples/piano/piano_78_16.opus +0 -0
  261. package/starter/samples/piano/piano_78_4.opus +0 -0
  262. package/starter/samples/piano/piano_78_8.opus +0 -0
  263. package/starter/samples/piano/piano_81_12.opus +0 -0
  264. package/starter/samples/piano/piano_81_16.opus +0 -0
  265. package/starter/samples/piano/piano_81_4.opus +0 -0
  266. package/starter/samples/piano/piano_81_8.opus +0 -0
  267. package/starter/samples/piano/piano_84_12.opus +0 -0
  268. package/starter/samples/piano/piano_84_16.opus +0 -0
  269. package/starter/samples/piano/piano_84_4.opus +0 -0
  270. package/starter/samples/piano/piano_84_8.opus +0 -0
  271. package/starter/samples/piano/piano_87_12.opus +0 -0
  272. package/starter/samples/piano/piano_87_16.opus +0 -0
  273. package/starter/samples/piano/piano_87_4.opus +0 -0
  274. package/starter/samples/piano/piano_87_8.opus +0 -0
  275. package/starter/samples/piano/piano_90_12.opus +0 -0
  276. package/starter/samples/piano/piano_90_16.opus +0 -0
  277. package/starter/samples/piano/piano_90_4.opus +0 -0
  278. package/starter/samples/piano/piano_90_8.opus +0 -0
  279. package/starter/samples/piano/piano_93_12.opus +0 -0
  280. package/starter/samples/piano/piano_93_16.opus +0 -0
  281. package/starter/samples/piano/piano_93_4.opus +0 -0
  282. package/starter/samples/piano/piano_93_8.opus +0 -0
  283. package/starter/samples/piano/piano_96_12.opus +0 -0
  284. package/starter/samples/piano/piano_96_16.opus +0 -0
  285. package/starter/samples/piano/piano_96_4.opus +0 -0
  286. package/starter/samples/piano/piano_96_8.opus +0 -0
  287. package/starter/samples/piano/piano_99_12.opus +0 -0
  288. package/starter/samples/piano/piano_99_16.opus +0 -0
  289. package/starter/samples/piano/piano_99_4.opus +0 -0
  290. package/starter/samples/piano/piano_99_8.opus +0 -0
  291. package/starter/samples/piano/piano_rel_102_x.opus +0 -0
  292. package/starter/samples/piano/piano_rel_105_x.opus +0 -0
  293. package/starter/samples/piano/piano_rel_108_x.opus +0 -0
  294. package/starter/samples/piano/piano_rel_21_x.opus +0 -0
  295. package/starter/samples/piano/piano_rel_24_x.opus +0 -0
  296. package/starter/samples/piano/piano_rel_27_x.opus +0 -0
  297. package/starter/samples/piano/piano_rel_30_x.opus +0 -0
  298. package/starter/samples/piano/piano_rel_33_x.opus +0 -0
  299. package/starter/samples/piano/piano_rel_36_x.opus +0 -0
  300. package/starter/samples/piano/piano_rel_39_x.opus +0 -0
  301. package/starter/samples/piano/piano_rel_42_x.opus +0 -0
  302. package/starter/samples/piano/piano_rel_45_x.opus +0 -0
  303. package/starter/samples/piano/piano_rel_48_x.opus +0 -0
  304. package/starter/samples/piano/piano_rel_51_x.opus +0 -0
  305. package/starter/samples/piano/piano_rel_54_x.opus +0 -0
  306. package/starter/samples/piano/piano_rel_57_x.opus +0 -0
  307. package/starter/samples/piano/piano_rel_60_x.opus +0 -0
  308. package/starter/samples/piano/piano_rel_63_x.opus +0 -0
  309. package/starter/samples/piano/piano_rel_66_x.opus +0 -0
  310. package/starter/samples/piano/piano_rel_69_x.opus +0 -0
  311. package/starter/samples/piano/piano_rel_72_x.opus +0 -0
  312. package/starter/samples/piano/piano_rel_75_x.opus +0 -0
  313. package/starter/samples/piano/piano_rel_78_x.opus +0 -0
  314. package/starter/samples/piano/piano_rel_81_x.opus +0 -0
  315. package/starter/samples/piano/piano_rel_84_x.opus +0 -0
  316. package/starter/samples/piano/piano_rel_87_x.opus +0 -0
  317. package/starter/samples/piano/piano_rel_90_x.opus +0 -0
  318. package/starter/samples/piano/piano_rel_93_x.opus +0 -0
  319. package/starter/samples/piano/piano_rel_96_x.opus +0 -0
  320. package/starter/samples/piano/piano_rel_99_x.opus +0 -0
  321. package/starter/samples/pizz/pizz_24_2.flac +0 -0
  322. package/starter/samples/pizz/pizz_28_2.flac +0 -0
  323. package/starter/samples/pizz/pizz_31_2.flac +0 -0
  324. package/starter/samples/pizz/pizz_35_2.flac +0 -0
  325. package/starter/samples/pizz/pizz_38_2.flac +0 -0
  326. package/starter/samples/pizz/pizz_41_2.flac +0 -0
  327. package/starter/samples/pizz/pizz_45_2.flac +0 -0
  328. package/starter/samples/pizz/pizz_48_2.flac +0 -0
  329. package/starter/samples/pizz/pizz_52_2.flac +0 -0
  330. package/starter/samples/pizz/pizz_57_2.flac +0 -0
  331. package/starter/samples/pizz/pizz_60_2.flac +0 -0
  332. package/starter/samples/pizz/pizz_64_2.flac +0 -0
  333. package/starter/samples/pizz/pizz_67_2.flac +0 -0
  334. package/starter/samples/pizz/pizz_71_2.flac +0 -0
  335. package/starter/samples/pizz/pizz_74_2.flac +0 -0
  336. package/starter/samples/pk_hat/pk_hat_2_1.flac +0 -0
  337. package/starter/samples/pk_hat/pk_hat_2_2.flac +0 -0
  338. package/starter/samples/pk_hat/pk_hat_6_1.flac +0 -0
  339. package/starter/samples/pk_hat/pk_hat_6_2.flac +0 -0
  340. package/starter/samples/pk_kick/pk_kick_3_1.flac +0 -0
  341. package/starter/samples/pk_kick/pk_kick_3_2.flac +0 -0
  342. package/starter/samples/pk_kick/pk_kick_7_1.flac +0 -0
  343. package/starter/samples/pk_kick/pk_kick_7_2.flac +0 -0
  344. package/starter/samples/pk_ohat/pk_ohat_3_1.flac +0 -0
  345. package/starter/samples/pk_ohat/pk_ohat_3_2.flac +0 -0
  346. package/starter/samples/pk_ohat/pk_ohat_6_1.flac +0 -0
  347. package/starter/samples/pk_ohat/pk_ohat_6_2.flac +0 -0
  348. package/starter/samples/pk_rim/pk_rim_2_1.flac +0 -0
  349. package/starter/samples/pk_rim/pk_rim_2_2.flac +0 -0
  350. package/starter/samples/pk_rim/pk_rim_5_1.flac +0 -0
  351. package/starter/samples/pk_rim/pk_rim_5_2.flac +0 -0
  352. package/starter/samples/pk_snare/pk_snare_3_1.flac +0 -0
  353. package/starter/samples/pk_snare/pk_snare_3_2.flac +0 -0
  354. package/starter/samples/pk_snare/pk_snare_7_1.flac +0 -0
  355. package/starter/samples/pk_snare/pk_snare_7_2.flac +0 -0
  356. package/starter/samples/shaker/shaker_1_1.flac +0 -0
  357. package/starter/samples/shaker/shaker_2_1.flac +0 -0
  358. package/starter/samples/snap/snap_r_1.flac +0 -0
  359. package/starter/samples/snap/snap_r_2.flac +0 -0
  360. package/starter/samples/snap/snap_r_3.flac +0 -0
  361. package/starter/samples/snap/snap_r_4.flac +0 -0
  362. package/starter/samples/snap/snap_r_5.flac +0 -0
  363. package/starter/samples/snap/snap_r_6.flac +0 -0
  364. package/starter/samples/tom/tom_3_1.flac +0 -0
  365. package/starter/samples/tom/tom_3_2.flac +0 -0
  366. package/starter/samples/tom/tom_4_1.flac +0 -0
  367. package/starter/samples/tom/tom_4_2.flac +0 -0
  368. package/starter/samples/vibes/vibes_41_1.opus +0 -0
  369. package/starter/samples/vibes/vibes_41_2.opus +0 -0
  370. package/starter/samples/vibes/vibes_45_1.opus +0 -0
  371. package/starter/samples/vibes/vibes_45_2.opus +0 -0
  372. package/starter/samples/vibes/vibes_48_1.opus +0 -0
  373. package/starter/samples/vibes/vibes_48_2.opus +0 -0
  374. package/starter/samples/vibes/vibes_52_1.opus +0 -0
  375. package/starter/samples/vibes/vibes_52_2.opus +0 -0
  376. package/starter/samples/vibes/vibes_55_1.opus +0 -0
  377. package/starter/samples/vibes/vibes_55_2.opus +0 -0
  378. package/starter/samples/vibes/vibes_59_1.opus +0 -0
  379. package/starter/samples/vibes/vibes_59_2.opus +0 -0
  380. package/starter/samples/vibes/vibes_62_1.opus +0 -0
  381. package/starter/samples/vibes/vibes_62_2.opus +0 -0
  382. package/starter/samples/vibes/vibes_65_1.opus +0 -0
  383. package/starter/samples/vibes/vibes_65_2.opus +0 -0
  384. package/starter/samples/vibes/vibes_69_1.opus +0 -0
  385. package/starter/samples/vibes/vibes_69_2.opus +0 -0
  386. package/starter/samples/vibes/vibes_72_1.opus +0 -0
  387. package/starter/samples/vibes/vibes_72_2.opus +0 -0
  388. package/starter/samples/vibes/vibes_76_1.opus +0 -0
  389. package/starter/samples/vibes/vibes_76_2.opus +0 -0
  390. package/starter/samples/violas/violas_36_1.opus +0 -0
  391. package/starter/samples/violas/violas_36_2.opus +0 -0
  392. package/starter/samples/violas/violas_38_1.opus +0 -0
  393. package/starter/samples/violas/violas_38_2.opus +0 -0
  394. package/starter/samples/violas/violas_40_1.opus +0 -0
  395. package/starter/samples/violas/violas_40_2.opus +0 -0
  396. package/starter/samples/violas/violas_43_1.opus +0 -0
  397. package/starter/samples/violas/violas_43_2.opus +0 -0
  398. package/starter/samples/violas/violas_47_1.opus +0 -0
  399. package/starter/samples/violas/violas_47_2.opus +0 -0
  400. package/starter/samples/violas/violas_50_1.opus +0 -0
  401. package/starter/samples/violas/violas_50_2.opus +0 -0
  402. package/starter/samples/violas/violas_53_1.opus +0 -0
  403. package/starter/samples/violas/violas_53_2.opus +0 -0
  404. package/starter/samples/violas/violas_57_1.opus +0 -0
  405. package/starter/samples/violas/violas_57_2.opus +0 -0
  406. package/starter/samples/violas/violas_60_1.opus +0 -0
  407. package/starter/samples/violas/violas_60_2.opus +0 -0
  408. package/starter/samples/violas/violas_64_1.opus +0 -0
  409. package/starter/samples/violas/violas_64_2.opus +0 -0
  410. package/starter/samples/violas/violas_67_1.opus +0 -0
  411. package/starter/samples/violas/violas_67_2.opus +0 -0
  412. package/starter/samples/violas/violas_71_1.opus +0 -0
  413. package/starter/samples/violas/violas_71_2.opus +0 -0
  414. package/starter/samples/violas/violas_74_1.opus +0 -0
  415. package/starter/samples/violas/violas_74_2.opus +0 -0
  416. package/starter/samples/violins/violins_43_1.opus +0 -0
  417. package/starter/samples/violins/violins_43_2.opus +0 -0
  418. package/starter/samples/violins/violins_45_1.opus +0 -0
  419. package/starter/samples/violins/violins_45_2.opus +0 -0
  420. package/starter/samples/violins/violins_47_1.opus +0 -0
  421. package/starter/samples/violins/violins_47_2.opus +0 -0
  422. package/starter/samples/violins/violins_50_1.opus +0 -0
  423. package/starter/samples/violins/violins_50_2.opus +0 -0
  424. package/starter/samples/violins/violins_54_1.opus +0 -0
  425. package/starter/samples/violins/violins_54_2.opus +0 -0
  426. package/starter/samples/violins/violins_57_1.opus +0 -0
  427. package/starter/samples/violins/violins_57_2.opus +0 -0
  428. package/starter/samples/violins/violins_60_1.opus +0 -0
  429. package/starter/samples/violins/violins_60_2.opus +0 -0
  430. package/starter/samples/violins/violins_64_1.opus +0 -0
  431. package/starter/samples/violins/violins_64_2.opus +0 -0
  432. package/starter/samples/violins/violins_67_1.opus +0 -0
  433. package/starter/samples/violins/violins_67_2.opus +0 -0
  434. package/starter/samples/violins/violins_71_1.opus +0 -0
  435. package/starter/samples/violins/violins_71_2.opus +0 -0
  436. package/starter/samples/violins/violins_74_1.opus +0 -0
  437. package/starter/samples/violins/violins_74_2.opus +0 -0
  438. package/starter/samples/woodblock/woodblock_f_1.flac +0 -0
  439. package/starter/samples/woodblock/woodblock_f_2.flac +0 -0
@@ -0,0 +1,1566 @@
1
+ // The component inventory — a material index: every part the component probe
2
+ // measured in a shoot's or a take's saved screens (states/<id>/parts.json,
3
+ // written by the runtime in the fidelity page — components-probe.ts),
4
+ // gathered into one `components.json` beside the log, with what the log knows
5
+ // on top of what the page showed:
6
+ //
7
+ // - values: what each part holds, field by field, in full — its text, the
8
+ // number in it (with its unit), an input's value apart from its
9
+ // placeholder — each with where it was read (state, element, selector,
10
+ // the shoot's still), so a film can show the product's own values and
11
+ // say where they came from;
12
+ // - relations: what a control changed (the action before a screen acted on
13
+ // it and these parts changed or appeared — seen), what it controls or
14
+ // labels by the page's own markup, what holds what;
15
+ // - provenance: the page each part was on (its URL, whether that host is a
16
+ // local one) and when the screen was saved;
17
+ // - which part the action before a screen acted on, which changed (was →
18
+ // now, or its state: a tab now selected) and which appeared since the
19
+ // screen before it;
20
+ // - tracks: the same part followed through the screens (matchStates), with
21
+ // what it read at each step — the wizard card through its steps, a total
22
+ // rising as a slider moves, a tab turning selected;
23
+ // - where each part would sit fitted to the film's stage, and how big its
24
+ // text reads on a phone as part of the whole screen and fitted.
25
+ //
26
+ // Pure Node: no browser. Everything here is measurement or arithmetic on it.
27
+ // Parts are in the page's own order; nothing in the file says which part a
28
+ // film uses, and none is singled out.
29
+ //
30
+ // `exampleSource` writes a small module that lifts the part it is named and
31
+ // moves it from where it sat to its fit — `open-take components --snippet
32
+ // <ref>`, for a part the film's author names.
33
+
34
+ import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
35
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
36
+ import { contentOf, matchStates, subtree, visibleLabel } from "../state-diff";
37
+ import { readStateIndex, type StateElement, type StateIndex } from "../state-index";
38
+ import type { CaptureLog } from "../types";
39
+ import type { PartCandidate, PartFields, PartKind, PartsFile } from "./components-probe";
40
+ import { PHONE_W } from "./probe-text";
41
+
42
+ type Box = [number, number, number, number];
43
+
44
+ // --- the file -------------------------------------------------------------------
45
+
46
+ /** One value a part holds, as the page showed it. */
47
+ export type ComponentValue = {
48
+ /** as the page had it, in full */
49
+ text: string;
50
+ /** the number in it, read as written ("US$4,466" → 4466; "18.5個月" → 18.5) */
51
+ value?: number;
52
+ /** what stands with that number ("US$", "個月", "%") */
53
+ unit?: string;
54
+ /** read from a parts.json measured before the probe kept full text: the
55
+ * 80-character label it had — `open-take components` measures it again */
56
+ capped?: true;
57
+ /** where it was read: the saved screen, the element (index.json id), its
58
+ * CSS path in state.html, and the shoot's still of that moment (relative
59
+ * to components.json) */
60
+ source: { state: string; eid: string; selector?: string; shot?: string };
61
+ };
62
+
63
+ /** How one part bears on another (a ref, `<state>:<element id>`):
64
+ * - controls: its markup says so (aria-controls — a tab and its panel);
65
+ * - changes: the action before a screen acted on it and `to` changed,
66
+ * restyled or appeared on that screen (seen in the log and the diff);
67
+ * - labels: its text names `to` (a field's label and its input; the
68
+ * heading aria-labelledby points at);
69
+ * - contains: `to` is the next part inside it.
70
+ * `by` is how it is known: seen (the action log and the screens' diff),
71
+ * markup (the page's own attributes) or layout (what holds what). */
72
+ export type ComponentRelation = {
73
+ kind: "controls" | "changes" | "labels" | "contains";
74
+ to: string;
75
+ by: "seen" | "markup" | "layout";
76
+ };
77
+
78
+ /** Where a part was: the saved screen, the page's URL, when the screen was
79
+ * saved (index.json's file time) and on the capture's clock. `local`: the
80
+ * URL's host is this machine (localhost, 127.x, ::1, *.local, a file) — the
81
+ * data on it may be a local seed rather than the live product's. */
82
+ export type ComponentProvenance = {
83
+ state: string;
84
+ url?: string;
85
+ capturedAt?: string;
86
+ tMs?: number;
87
+ local?: true;
88
+ };
89
+
90
+ /** One part of one saved screen. Refs are `<state>:<element id>`, as
91
+ * `open-take states` names elements and as lift() takes them. */
92
+ export type ComponentEntry = {
93
+ ref: string;
94
+ state: string;
95
+ el: string;
96
+ kind: PartKind;
97
+ sub?: PartCandidate["sub"];
98
+ primary?: true;
99
+ /** what it reads, for a glance (≤ 80 characters); `values` has it in full */
100
+ label: string;
101
+ /** what it holds, by field: text (its visible text), number (the number in
102
+ * it), label (a stat's or a field's words beside its value), value /
103
+ * placeholder / min / max / checked (an input's own), name (an accessible
104
+ * name that is not its text — an icon-only button's) */
105
+ values: Record<string, ComponentValue>;
106
+ relations: ComponentRelation[];
107
+ provenance: ComponentProvenance;
108
+ selector: string;
109
+ /** layout box, the screen's CSS px */
110
+ box: Box;
111
+ /** the text's own extent (text kinds) */
112
+ ink?: Box;
113
+ /** the part it sits inside (a ref) */
114
+ inside?: string;
115
+ /** its content children in order (refs) — what an assemble acts on */
116
+ parts?: string[];
117
+ /** a group's members (refs) */
118
+ members?: string[];
119
+ axis?: PartCandidate["axis"];
120
+ field?: string;
121
+ input?: string;
122
+ /** the ARIA / form state it shows (selected, pressed, expanded, current,
123
+ * disabled, open), as the probe read it */
124
+ shows?: Record<string, string>;
125
+ look: PartCandidate["look"];
126
+ facts: PartCandidate["facts"] & {
127
+ /** the action before this screen acted on it (or on a control inside it) */
128
+ acted?: true;
129
+ /** its text since the screen before: "was → now" (each side ≤ 40
130
+ * characters; the steps of its track have them in full) */
131
+ changed?: string;
132
+ /** its state or colour since the screen before, its text the same:
133
+ * "selected: – → true", "fill #ffffff → #0a7f5a" */
134
+ restyled?: string;
135
+ /** its box moved or resized since the screen before (text the same) */
136
+ moved?: true;
137
+ /** not on the screen before (a result card, a toast) */
138
+ appeared?: true;
139
+ };
140
+ /** its text in px on a phone (PHONE_W wide): as part of the whole screen
141
+ * fitted to the stage, and fitted on its own (`fit`). Arithmetic on its
142
+ * computed font size (a stat, card, group or field: the largest of the
143
+ * parts inside it) — not measured on a phone */
144
+ phone?: { asScreen: number; atFit: number };
145
+ /** the largest box of its shape within 70 % × 60 % of the stage, centred —
146
+ * `k` its scale from the screen, `by` which of its boxes (with lift.pad) */
147
+ fit: { box: Box; k: number; by: "ink" | "box" };
148
+ /** the track it is a step of */
149
+ track?: string;
150
+ /** an earlier screen shows the same part (kind, text and place): its ref */
151
+ repeats?: string;
152
+ /** the same element on the screen before (matchStates, the same kind),
153
+ * whether it changed or not — a part that stayed but moved is linked
154
+ * here too */
155
+ before?: string;
156
+ /** its crisp picture (3×, see-through around it), relative to components.json */
157
+ picture?: string;
158
+ noPicture?: string;
159
+ /** the lift() options that reproduce the picture */
160
+ lift: PartCandidate["lift"];
161
+ };
162
+
163
+ export type ComponentState = {
164
+ id: string;
165
+ /** state.html, relative to components.json */
166
+ url: string;
167
+ /** the page it was: its URL, title, when it was saved */
168
+ page?: { url: string; title?: string; capturedAt?: string; tMs?: number; local?: true };
169
+ /** the shoot's still of this moment, relative to components.json */
170
+ shot?: string;
171
+ fidelity?: { ok: boolean; psnr?: number; diffFrac?: number };
172
+ /** the action that led here, as the log has it */
173
+ action?: ComponentAction;
174
+ /** the screen before it in the log (what `changed` / `appeared` compare with) */
175
+ since?: string;
176
+ /** most of the screen is new since the one before (another page): no part is
177
+ * marked `appeared` */
178
+ newPage?: true;
179
+ medianTextPx?: number;
180
+ /** this screen's components, in `order` */
181
+ components: string[];
182
+ /** why it has none (no parts.json, an unreadable index) */
183
+ unmeasured?: string;
184
+ };
185
+
186
+ export type ComponentAction = {
187
+ kind: string;
188
+ /** the shoot's still name / the plan's selector */
189
+ label?: string;
190
+ /** the element it acted on, a ref in this screen (when it is still there) */
191
+ acted?: string;
192
+ /** …or in the screen before, when this one no longer has it */
193
+ actedBefore?: string;
194
+ /** how that was found: the capture's own match of the action's box
195
+ * ("capture"), or the element under the action's point ("point") */
196
+ by?: "capture" | "point";
197
+ /** the parts that changed, restyled or appeared on this screen after it
198
+ * (seen: the log and the screens' diff), apart from the control itself
199
+ * and what changed only inside it — also when the element acted on is
200
+ * not a part of its own (a tab drawn as a bare link); in page order */
201
+ changes?: string[];
202
+ };
203
+
204
+ export type ComponentTrack = {
205
+ id: string;
206
+ kind: PartKind;
207
+ /** what it reads at its last step (≤ 60 characters) */
208
+ label: string;
209
+ /** steps whose text, number or state differs from the step before */
210
+ changes: number;
211
+ /** each step: its text in full, the number in it, and its state (ARIA /
212
+ * form state, fill) when it has one */
213
+ steps: {
214
+ state: string;
215
+ ref: string;
216
+ text: string;
217
+ value?: number;
218
+ unit?: string;
219
+ shows?: string;
220
+ }[];
221
+ };
222
+
223
+ /** `components.json` — version 2 (version 1 plus values, relations,
224
+ * provenance, restyled, track values; the order is the page's). */
225
+ export type ComponentsFile = {
226
+ version: 2;
227
+ about: string;
228
+ /** how `components` (and each screen's list) is ordered */
229
+ order: string;
230
+ source: {
231
+ kind: ComponentSourceKind;
232
+ /** the log it was read with, relative to components.json */
233
+ log?: string;
234
+ viewport: { w: number; h: number; dpr?: number };
235
+ /** px of what shows the screens — a shoot's stills (shots.json scale),
236
+ * a take's recording (its video's width over the viewport's) — per CSS
237
+ * px of the screen */
238
+ srcRatio?: number;
239
+ };
240
+ stage: { width: number; height: number; phoneWidth: number; phoneScale: number };
241
+ states: ComponentState[];
242
+ components: ComponentEntry[];
243
+ groups: { ref: string; kind: PartKind; axis?: string; members: string[] }[];
244
+ tracks: ComponentTrack[];
245
+ skipped: { ref: string; kind: PartKind; why: string; label?: string }[];
246
+ /** measured: the component probe's time in the page and its pictures, summed */
247
+ ms: { measure: number; pictures: number };
248
+ };
249
+
250
+ const ABOUT =
251
+ "A material index of this product's saved screens: the parts that stand on their own, as the component probe measured them in each restored page (states/<id>/parts.json); what each holds, field by field and in full, with where it was read (values); how parts bear on each other — what an action on one changed, what the markup says one controls or labels, what holds what (relations); the page each was on (provenance); and the same part followed through the screens (tracks). Measurements and arithmetic on them; nothing here says which part a film uses.";
252
+
253
+ /** The order, in words — printed by the CLI and written into the file. */
254
+ export const COMPONENT_ORDER =
255
+ "screens in log order; within a screen, the page's own order (document order) — where to find a part, not a ranking";
256
+
257
+ const ACTABLE = new Set<PartKind>(["button", "input", "control", "field"]);
258
+ /** kinds whose phone size is their largest text inside */
259
+ const BLOCK_TEXT = new Set<PartKind>(["stat", "card", "group", "field"]);
260
+
261
+ // --- the source -------------------------------------------------------------------
262
+
263
+ export type ComponentSourceKind = "shoot" | "take" | "states" | "state";
264
+
265
+ /** One saved screen to gather, with the action that led to it. */
266
+ export type ComponentSourceState = {
267
+ id: string;
268
+ /** the state's folder (absolute) */
269
+ dir: string;
270
+ fidelity?: ComponentState["fidelity"];
271
+ /** the shoot's still of this moment (absolute) */
272
+ shot?: string;
273
+ action?: {
274
+ kind: string;
275
+ label?: string;
276
+ /** the element acted on, by the capture's own match — in the screen before */
277
+ target?: string;
278
+ /** where the action was, viewport CSS px — of the screen before it
279
+ * (`before`: a take's anchor) or of this one (`after`: a shoot's still
280
+ * is taken where the mouse ended) */
281
+ point?: { x: number; y: number; in: "before" | "after" };
282
+ };
283
+ };
284
+
285
+ export type ComponentSource = {
286
+ kind: ComponentSourceKind;
287
+ /** where components.json goes (absolute) */
288
+ root: string;
289
+ /** the log, absolute */
290
+ log?: string;
291
+ /** px of the stills or the recording per CSS px of the screen */
292
+ srcRatio?: number;
293
+ states: ComponentSourceState[];
294
+ };
295
+
296
+ /** a shoot's log, as far as this reads it (runtime/src/shoot.ts) */
297
+ type ShotsLog = {
298
+ scale?: number;
299
+ stills?: {
300
+ file?: string;
301
+ name?: string;
302
+ cursor?: { x: number; y: number };
303
+ scroll?: { x: number; y: number };
304
+ action?: string;
305
+ state?: string;
306
+ }[];
307
+ states?: {
308
+ dir?: string;
309
+ items?: { id: string; still?: string; fidelity?: ComponentState["fidelity"] }[];
310
+ };
311
+ };
312
+
313
+ const isDir = (p: string): boolean => {
314
+ try {
315
+ return statSync(p).isDirectory();
316
+ } catch {
317
+ return false;
318
+ }
319
+ };
320
+ const readJson = <T>(p: string): T => JSON.parse(readFileSync(p, "utf8")) as T;
321
+ /** a take's or shoot's anchor kinds that sit on the thing acted on */
322
+ const POINT_KINDS = new Set(["click", "type", "drag", "hover", "move", "look", "dropFiles"]);
323
+
324
+ /**
325
+ * What `path` is and which saved screens it holds, in log order: a shoot's
326
+ * folder (shots.json), a take's working dir (capture.json — `<name>.take/`),
327
+ * one saved screen (index.json), or a folder of saved screens without a log
328
+ * (a film folder's states/, or that folder itself). Throws with what it
329
+ * found when it is none of these.
330
+ */
331
+ export function readComponentSource(path: string): ComponentSource {
332
+ let dir = resolve(path);
333
+ if (!isDir(dir) && /\.json$/i.test(dir)) dir = dirname(dir);
334
+ if (!isDir(dir) && /\.mp4$/i.test(dir) && isDir(dir.replace(/\.mp4$/i, ".take")))
335
+ dir = dir.replace(/\.mp4$/i, ".take");
336
+ if (!isDir(dir)) throw new Error(`components: no folder at ${dir}`);
337
+ if (existsSync(join(dir, "shots.json"))) return shootSource(dir);
338
+ if (existsSync(join(dir, "capture.json"))) return takeSource(dir);
339
+ if (existsSync(join(dir, "index.json")))
340
+ return { kind: "state", root: dir, states: [{ id: basename(dir), dir }] };
341
+ const states = isDir(join(dir, "states")) ? join(dir, "states") : dir;
342
+ const found = readdirSync(states, { withFileTypes: true })
343
+ .filter((d) => d.isDirectory() && existsSync(join(states, d.name, "index.json")))
344
+ .map((d) => {
345
+ const index = readStateIndex(join(states, d.name));
346
+ return {
347
+ id: d.name,
348
+ dir: join(states, d.name),
349
+ tMs: typeof index === "string" ? 0 : index.tMs,
350
+ };
351
+ })
352
+ .sort((a, b) => a.tMs - b.tMs || a.id.localeCompare(b.id));
353
+ if (!found.length)
354
+ throw new Error(
355
+ `components: ${dir} holds no saved screens — give it a shoot's folder (shots.json), a take (its .mp4 or <name>.take/), a saved screen (index.json) or a folder of them`,
356
+ );
357
+ return { kind: "states", root: dir, states: found.map(({ id, dir }) => ({ id, dir })) };
358
+ }
359
+
360
+ function shootSource(dir: string): ComponentSource {
361
+ const log = readJson<ShotsLog>(join(dir, "shots.json"));
362
+ const stills = Array.isArray(log.stills) ? log.stills : [];
363
+ const items = Array.isArray(log.states?.items) ? log.states.items : [];
364
+ const statesDir = join(dir, log.states?.dir || "states");
365
+ let last = -1;
366
+ const states: ComponentSourceState[] = [];
367
+ for (const it of items) {
368
+ const at = stills.findIndex((s) => s.state === it.id || (it.still && s.file === it.still));
369
+ // the action that led here: the last still with one since the screen before
370
+ let act: (typeof stills)[number] | undefined;
371
+ for (let i = last + 1; i <= at; i++) if (stills[i]?.action) act = stills[i];
372
+ if (at >= 0) last = at;
373
+ // the mouse is where the action left it: on what it acted on as the
374
+ // screen before showed it (the still itself is taken once the page has
375
+ // answered, and an answer can move things); the first screen has only itself
376
+ const point =
377
+ act?.cursor && act.action && POINT_KINDS.has(act.action)
378
+ ? {
379
+ x: act.cursor.x - (act.scroll?.x ?? 0),
380
+ y: act.cursor.y - (act.scroll?.y ?? 0),
381
+ in: states.length ? ("before" as const) : ("after" as const),
382
+ }
383
+ : undefined;
384
+ const still = it.still ?? (at >= 0 ? stills[at]?.file : undefined);
385
+ states.push({
386
+ id: it.id,
387
+ dir: join(statesDir, it.id),
388
+ ...(it.fidelity ? { fidelity: it.fidelity } : {}),
389
+ ...(still && existsSync(join(dir, still)) ? { shot: join(dir, still) } : {}),
390
+ ...(act?.action
391
+ ? {
392
+ action: {
393
+ kind: act.action,
394
+ ...(act.name ? { label: act.name } : {}),
395
+ ...(point ? { point } : {}),
396
+ },
397
+ }
398
+ : {}),
399
+ });
400
+ }
401
+ return {
402
+ kind: "shoot",
403
+ root: dir,
404
+ log: join(dir, "shots.json"),
405
+ ...(typeof log.scale === "number" && log.scale > 0 ? { srcRatio: log.scale } : {}),
406
+ states,
407
+ };
408
+ }
409
+
410
+ function takeSource(dir: string): ComponentSource {
411
+ const log = readJson<CaptureLog>(join(dir, "capture.json"));
412
+ const items = (log.states?.items ?? []).filter((it) => !it.moving);
413
+ const statesDir = join(dir, log.states?.dir || "states");
414
+ const states: ComponentSourceState[] = items.map((it) => {
415
+ const e = log.events.find((x) => x.state === it.id);
416
+ const kind = e?.kind ?? "click";
417
+ const point =
418
+ e && POINT_KINDS.has(kind)
419
+ ? e.kind === "dropFiles"
420
+ ? { x: e.to.x, y: e.to.y, in: "before" as const }
421
+ : { x: e.x, y: e.y, in: "before" as const }
422
+ : undefined;
423
+ return {
424
+ id: it.id,
425
+ dir: join(statesDir, it.id),
426
+ ...(it.fidelity ? { fidelity: it.fidelity } : {}),
427
+ ...(e
428
+ ? {
429
+ action: {
430
+ kind,
431
+ ...(e.sel ? { label: e.sel } : {}),
432
+ ...(e.target ? { target: e.target } : {}),
433
+ ...(point ? { point } : {}),
434
+ },
435
+ }
436
+ : {}),
437
+ };
438
+ });
439
+ const srcRatio = log.video?.width && log.viewport?.w ? log.video.width / log.viewport.w : 0;
440
+ return {
441
+ kind: "take",
442
+ root: dir,
443
+ log: join(dir, "capture.json"),
444
+ ...(srcRatio > 0 ? { srcRatio } : {}),
445
+ states,
446
+ };
447
+ }
448
+
449
+ // --- arithmetic ---------------------------------------------------------------------
450
+
451
+ export type Stage = { width: number; height: number };
452
+ export const FILM_STAGE: Stage = { width: 1920, height: 1080 };
453
+ /** the share of the stage a part's fit may take, each way */
454
+ const FIT_W = 0.7;
455
+ const FIT_H = 0.6;
456
+
457
+ const r1 = (v: number) => Math.round(v * 10) / 10;
458
+ const r2 = (v: number) => Math.round(v * 100) / 100;
459
+
460
+ /** The largest box of the part's shape (its ink or box, ± lift.pad) within
461
+ * 70 % × 60 % of the stage, centred. */
462
+ export function fitOf(
463
+ part: Pick<PartCandidate, "box" | "ink" | "lift">,
464
+ stage: Stage = FILM_STAGE,
465
+ ): ComponentEntry["fit"] {
466
+ const by = part.lift.box === "ink" && part.ink ? "ink" : "box";
467
+ const b = by === "ink" ? part.ink! : part.box;
468
+ const w = Math.max(1, b[2] + 2 * part.lift.pad);
469
+ const h = Math.max(1, b[3] + 2 * part.lift.pad);
470
+ const k = Math.min((FIT_W * stage.width) / w, (FIT_H * stage.height) / h);
471
+ const fw = Math.round(w * k);
472
+ const fh = Math.round(h * k);
473
+ return {
474
+ box: [Math.round((stage.width - fw) / 2), Math.round((stage.height - fh) / 2), fw, fh],
475
+ k: r2(k),
476
+ by,
477
+ };
478
+ }
479
+
480
+ /** Its text's px on a phone: the whole screen contained in the stage, and the
481
+ * part at its fit — the stage shown PHONE_W px wide. */
482
+ export function phoneOf(
483
+ fontPx: number,
484
+ fitK: number,
485
+ viewport: { w: number; h: number },
486
+ stage: Stage = FILM_STAGE,
487
+ ): { asScreen: number; atFit: number } {
488
+ const phone = PHONE_W / stage.width;
489
+ const screenK = Math.min(stage.width / viewport.w, stage.height / viewport.h);
490
+ return { asScreen: r1(fontPx * screenK * phone), atFit: r1(fontPx * fitK * phone) };
491
+ }
492
+
493
+ // --- gather -------------------------------------------------------------------------
494
+
495
+ type Loaded = {
496
+ src: ComponentSourceState;
497
+ index: StateIndex | null;
498
+ parts: PartsFile | null;
499
+ why?: string;
500
+ };
501
+
502
+ const cut = (s: string, n: number) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);
503
+ const textOf = (index: StateIndex, id: string): string =>
504
+ contentOf(index, id).join(" ").replace(/\s+/g, " ").trim();
505
+ const contains = (b: Box, x: number, y: number) =>
506
+ x >= b[0] && x <= b[0] + b[2] && y >= b[1] && y <= b[1] + b[3];
507
+
508
+ /** the smallest interactive element under a point — a <label> counts (a
509
+ * styled label standing for a hidden checkbox); null when none */
510
+ function interactiveAt(index: StateIndex, x: number, y: number): StateElement | null {
511
+ let best: StateElement | null = null;
512
+ for (const e of index.elements)
513
+ if (
514
+ (e.interactive || e.tag === "label") &&
515
+ contains(e.box, x, y) &&
516
+ (!best || e.box[2] * e.box[3] < best.box[2] * best.box[3])
517
+ )
518
+ best = e;
519
+ return best;
520
+ }
521
+
522
+ /** a matched pair is plausibly one element: the same text, or (text typed,
523
+ * a toggle's word swapped) much the same size — within 30 % each way */
524
+ function samePart(A: StateIndex, a: string, B: StateIndex, b: string): boolean {
525
+ if (textOf(A, a) === textOf(B, b)) return true;
526
+ const ba = A.elements.find((x) => x.id === a)?.box;
527
+ const bb = B.elements.find((x) => x.id === b)?.box;
528
+ if (!ba || !bb) return false;
529
+ const near = (x: number, y: number) => Math.abs(x - y) <= 0.3 * Math.max(x, y, 1);
530
+ return near(ba[2], bb[2]) && near(ba[3], bb[3]);
531
+ }
532
+
533
+ function load(src: ComponentSourceState): Loaded {
534
+ const index = readStateIndex(src.dir);
535
+ if (typeof index === "string") return { src, index: null, parts: null, why: index };
536
+ const p = join(src.dir, "parts.json");
537
+ if (!existsSync(p))
538
+ return {
539
+ src,
540
+ index,
541
+ parts: null,
542
+ why: "no parts.json (saved before the component probe — `open-take components` measures it)",
543
+ };
544
+ try {
545
+ const parts = readJson<PartsFile>(p);
546
+ if (parts.version !== 1 || !Array.isArray(parts.parts))
547
+ return { src, index, parts: null, why: "parts.json is not version 1" };
548
+ return { src, index, parts };
549
+ } catch (e) {
550
+ return { src, index, parts: null, why: `unreadable parts.json: ${(e as Error).message}` };
551
+ }
552
+ }
553
+
554
+ // --- values -------------------------------------------------------------------------
555
+
556
+ const NUMBER_PARTS_RE =
557
+ /(NT\$|US\$|HK\$|[$€£¥₩])?\s?([-−]?\d(?:[\d,]*\d)?(?:\.\d+)?)\s?(%|[kKM](?![A-Za-z])|\+|萬|億|ms(?![A-Za-z])|fps(?![A-Za-z])|×|x(?![A-Za-z])|[぀-ヿ㐀-鿿]{1,3})?/;
558
+
559
+ /** The first number in a text, as written, with what stands with it: "Save
560
+ * US$4,466" → { text: "US$4,466", value: 4466, unit: "US$" }; "18.5個月" →
561
+ * 18.5 "個月". Null when it holds no digit. */
562
+ export function readNumber(s: string): { text: string; value: number; unit?: string } | null {
563
+ const m = NUMBER_PARTS_RE.exec(s);
564
+ if (!m) return null;
565
+ const value = Number(m[2]!.replace(/,/g, "").replace("−", "-"));
566
+ if (!Number.isFinite(value)) return null;
567
+ const unit = m[1] && m[3] ? `${m[1]} ${m[3]}` : (m[1] ?? m[3]);
568
+ return { text: m[0].trim(), value, ...(unit ? { unit } : {}) };
569
+ }
570
+
571
+ /** a number that is the whole of a text ("1,288", "18.5"), else null */
572
+ const wholeNumber = (s: string) => {
573
+ const n = readNumber(s);
574
+ return n && n.text === s.trim() ? n : null;
575
+ };
576
+
577
+ const LOCAL_HOST = /^(localhost|127(\.\d+){3}|\[?::1\]?|0\.0\.0\.0|.*\.local|.*\.localhost)$/i;
578
+ /** the URL's host is this machine (or it is a file) */
579
+ function isLocal(url: string): boolean {
580
+ try {
581
+ const u = new URL(url);
582
+ return u.protocol === "file:" || LOCAL_HOST.test(u.hostname);
583
+ } catch {
584
+ return false;
585
+ }
586
+ }
587
+
588
+ /** what a part holds, field by field — see ComponentEntry.values */
589
+ function valuesOf(
590
+ p: PartCandidate,
591
+ byEl: Map<string, PartCandidate>,
592
+ at: (eid: string, selector?: string) => ComponentValue["source"],
593
+ ): Record<string, ComponentValue> {
594
+ const out: Record<string, ComponentValue> = {};
595
+ const capped = p.text == null && p.label.endsWith("…");
596
+ const val = (
597
+ text: string,
598
+ from: PartCandidate,
599
+ extra: { value?: number; unit?: string } = {},
600
+ ): ComponentValue => ({
601
+ text,
602
+ ...(extra.value != null ? { value: extra.value } : {}),
603
+ ...(extra.unit ? { unit: extra.unit } : {}),
604
+ ...(from.text == null && text.endsWith("…") ? { capped: true as const } : {}),
605
+ source: at(from.el, from.selector),
606
+ });
607
+ const full = p.text ?? p.label;
608
+ const f: PartFields | undefined = p.fields;
609
+ const own = p.kind === "input" || p.kind === "control";
610
+ if (full && !(own && f)) out.text = val(full, p);
611
+ if (p.kind === "number") {
612
+ const n = readNumber(full);
613
+ if (n) out.number = val(n.text, p, n);
614
+ } else if (p.kind === "stat") {
615
+ // its value: the largest number part inside it, else the number in its text
616
+ let num: PartCandidate | undefined;
617
+ for (const q of byEl.values()) {
618
+ if (q.kind !== "number") continue;
619
+ let up = q.inside;
620
+ while (up && up !== p.el) up = byEl.get(up)?.inside;
621
+ if (up === p.el && (!num || q.look.fontPx > num.look.fontPx)) num = q;
622
+ }
623
+ const n = readNumber(num ? (num.text ?? num.label) : full);
624
+ if (n) {
625
+ out.number = val(n.text, num ?? p, n);
626
+ const words = full.replace(n.text, " ").replace(/\s+/g, " ").trim();
627
+ if (words) out.label = val(words, p);
628
+ }
629
+ } else if (own && f) {
630
+ if (f.value != null) {
631
+ const n = wholeNumber(f.value);
632
+ out.value = val(f.value, p, n ?? {});
633
+ }
634
+ if (f.placeholder) out.placeholder = val(f.placeholder, p);
635
+ for (const k of ["min", "max", "step"] as const) {
636
+ const v = f[k];
637
+ if (v != null) out[k] = val(v, p, wholeNumber(v) ?? {});
638
+ }
639
+ if (f.checked != null) out.checked = val(String(f.checked), p, { value: f.checked ? 1 : 0 });
640
+ if (f.options?.length) out.options = val(f.options.join("\n"), p);
641
+ } else if (p.kind === "field") {
642
+ if (full) out.label = val(full, p);
643
+ const input = p.input ? byEl.get(p.input) : undefined;
644
+ const v = input?.fields?.value;
645
+ if (input && v != null) out.value = val(v, input, wholeNumber(v) ?? {});
646
+ }
647
+ if (f?.name) out.name = val(f.name, p);
648
+ if (capped && !Object.keys(out).length) out.text = val(p.label, p);
649
+ return out;
650
+ }
651
+
652
+ /** a part's state as one string: its ARIA / form state and its fill and
653
+ * text colour — what a tab or a toggle changes besides its text */
654
+ const showsOf = (e: ComponentEntry): Record<string, string> => ({
655
+ ...(e.shows ?? {}),
656
+ ...(e.values.checked ? { checked: e.values.checked.text } : {}),
657
+ fill: e.look.fill ?? "none",
658
+ fg: e.look.fg,
659
+ });
660
+ const showsKey = (e: ComponentEntry): string =>
661
+ Object.entries(showsOf(e))
662
+ .sort(([a], [b]) => a.localeCompare(b))
663
+ .map(([k, v]) => `${k}=${v}`)
664
+ .join(" ");
665
+ function restyledOf(a: ComponentEntry, b: ComponentEntry): string | undefined {
666
+ const A = showsOf(a);
667
+ const B = showsOf(b);
668
+ const keys = [...new Set([...Object.keys(A), ...Object.keys(B)])].sort();
669
+ const diff = keys
670
+ .filter((k) => A[k] !== B[k])
671
+ .map((k) => `${k}${k === "fill" || k === "fg" ? "" : ":"} ${A[k] ?? "–"} → ${B[k] ?? "–"}`);
672
+ return diff.length ? diff.join("; ") : undefined;
673
+ }
674
+
675
+ /** a step's text: its visible text in full, else an input's value */
676
+ const stepText = (e: ComponentEntry, index: StateIndex): string =>
677
+ e.values.text?.text ?? e.values.value?.text ?? textOf(index, e.el);
678
+ const stepNumber = (e: ComponentEntry) => e.values.number ?? e.values.value;
679
+
680
+ /**
681
+ * Gather the inventory of `source`'s saved screens from their parts.json.
682
+ * Screens without one are listed with why (`unmeasured`); the runtime's
683
+ * `inventoryComponents` measures them first.
684
+ */
685
+ export function gatherComponents(
686
+ source: ComponentSource,
687
+ opts: { stage?: Stage } = {},
688
+ ): ComponentsFile {
689
+ const stage = opts.stage ?? FILM_STAGE;
690
+ const rel = (abs: string) => relative(source.root, abs).split(sep).join("/") || ".";
691
+ const loaded = source.states.map(load);
692
+ const first = loaded.find((l) => l.index)?.index;
693
+ const viewport = first
694
+ ? { w: first.viewport.w, h: first.viewport.h, dpr: first.viewport.dpr }
695
+ : { w: stage.width, h: stage.height };
696
+
697
+ const states: ComponentState[] = [];
698
+ const all: ComponentEntry[] = [];
699
+ const skipped: ComponentsFile["skipped"] = [];
700
+ const byState = new Map<string, ComponentEntry[]>();
701
+ const ms = { measure: 0, pictures: 0 };
702
+ let prev: Loaded | null = null;
703
+ // matchStates once per pair of screens
704
+ const pairCache = new Map<string, Map<string, string>>();
705
+ const pairsOf = (a: string, A: StateIndex, b: string, B: StateIndex) => {
706
+ const key = `${a}\u0000${b}`;
707
+ let m = pairCache.get(key);
708
+ if (!m) {
709
+ m = matchStates(A, B);
710
+ pairCache.set(key, m);
711
+ }
712
+ return m;
713
+ };
714
+
715
+ for (const L of loaded) {
716
+ const id = L.src.id;
717
+ const state: ComponentState = {
718
+ id,
719
+ url: rel(join(L.src.dir, "state.html")),
720
+ ...(L.src.fidelity ? { fidelity: L.src.fidelity } : {}),
721
+ components: [],
722
+ };
723
+ if (L.src.shot) state.shot = rel(L.src.shot);
724
+ if (L.index) {
725
+ let capturedAt: string | undefined;
726
+ try {
727
+ capturedAt = statSync(join(L.src.dir, "index.json")).mtime.toISOString();
728
+ } catch {
729
+ /* the index was read a moment ago; no time is no time */
730
+ }
731
+ state.page = {
732
+ url: L.index.url,
733
+ ...(L.index.title ? { title: L.index.title } : {}),
734
+ ...(capturedAt ? { capturedAt } : {}),
735
+ tMs: L.index.tMs,
736
+ ...(isLocal(L.index.url) ? { local: true as const } : {}),
737
+ };
738
+ }
739
+ const fidelity = L.src.fidelity ?? L.index?.fidelity;
740
+ if (fidelity && !state.fidelity) state.fidelity = fidelity;
741
+ states.push(state);
742
+ if (!L.index || !L.parts) {
743
+ state.unmeasured = L.why ?? "not measured";
744
+ if (L.index) prev = { ...L, parts: null };
745
+ continue;
746
+ }
747
+ const index = L.index;
748
+ const P = L.parts;
749
+ ms.measure += P.ms?.measure ?? 0;
750
+ ms.pictures += P.ms?.pictures ?? 0;
751
+ state.medianTextPx = P.medianTextPx;
752
+ const ref = (el: string) => `${id}:${el}`;
753
+ const byEl = new Map(P.parts.map((p) => [p.el, p]));
754
+ const at = (eid: string, selector?: string): ComponentValue["source"] => ({
755
+ state: id,
756
+ eid,
757
+ ...(selector ? { selector } : {}),
758
+ ...(state.shot ? { shot: state.shot } : {}),
759
+ });
760
+ const provenance: ComponentProvenance = {
761
+ state: id,
762
+ ...(state.page?.url ? { url: state.page.url } : {}),
763
+ ...(state.page?.capturedAt ? { capturedAt: state.page.capturedAt } : {}),
764
+ ...(state.page ? { tMs: state.page.tMs } : {}),
765
+ ...(state.page?.local ? { local: true as const } : {}),
766
+ };
767
+ const entries: ComponentEntry[] = P.parts.map((p) => {
768
+ const fit = fitOf(p, stage);
769
+ const e: ComponentEntry = {
770
+ ref: ref(p.el),
771
+ state: id,
772
+ el: p.el,
773
+ kind: p.kind,
774
+ ...(p.sub ? { sub: p.sub } : {}),
775
+ ...(p.primary ? { primary: true as const } : {}),
776
+ label: p.label,
777
+ values: valuesOf(p, byEl, at),
778
+ relations: [],
779
+ provenance,
780
+ selector: p.selector,
781
+ box: p.box,
782
+ ...(p.ink ? { ink: p.ink } : {}),
783
+ ...(p.inside ? { inside: ref(p.inside) } : {}),
784
+ ...(p.parts?.length ? { parts: p.parts.map(ref) } : {}),
785
+ ...(p.members?.length ? { members: p.members.map(ref) } : {}),
786
+ ...(p.axis ? { axis: p.axis } : {}),
787
+ ...(p.field ? { field: ref(p.field) } : {}),
788
+ ...(p.input ? { input: ref(p.input) } : {}),
789
+ ...(p.state ? { shows: p.state } : {}),
790
+ look: p.look,
791
+ facts: { ...p.facts },
792
+ fit,
793
+ ...(p.picture ? { picture: rel(resolve(L.src.dir, p.picture)) } : {}),
794
+ ...(p.noPicture ? { noPicture: p.noPicture } : {}),
795
+ lift: p.lift,
796
+ };
797
+ return e;
798
+ });
799
+ const byRef = new Map(entries.map((e) => [e.ref, e]));
800
+ // relations the page itself states: what holds what, a field's label
801
+ // and its input, aria-controls
802
+ for (const e of entries) {
803
+ const up = e.inside ? byRef.get(e.inside) : undefined;
804
+ if (up) up.relations.push({ kind: "contains", to: e.ref, by: "layout" });
805
+ if (e.kind === "field" && e.input)
806
+ e.relations.push({ kind: "labels", to: e.input, by: "layout" });
807
+ for (const c of byEl.get(e.el)?.controls ?? [])
808
+ e.relations.push({ kind: "controls", to: ref(c), by: "markup" });
809
+ for (const l of byEl.get(e.el)?.labelledBy ?? [])
810
+ byRef.get(ref(l))?.relations.push({ kind: "labels", to: e.ref, by: "markup" });
811
+ }
812
+ // a block's text: the largest of the parts inside it
813
+ const font = new Map(entries.map((e) => [e.ref, e.look.fontPx]));
814
+ for (const e of entries)
815
+ for (let up = e.inside; up; up = byRef.get(up)?.inside)
816
+ if (BLOCK_TEXT.has(byRef.get(up)?.kind as PartKind))
817
+ font.set(up, Math.max(font.get(up) ?? 0, e.look.fontPx));
818
+ for (const e of entries)
819
+ if (e.kind !== "media" && e.kind !== "logo")
820
+ e.phone = phoneOf(font.get(e.ref)!, e.fit.k, P.viewport ?? index.viewport, stage);
821
+ for (const s of P.skipped ?? [])
822
+ skipped.push({
823
+ ref: ref(s.el),
824
+ kind: s.kind,
825
+ why: s.why,
826
+ ...(s.label ? { label: s.label } : {}),
827
+ });
828
+
829
+ // --- what the log adds: since the screen before ---------------------------
830
+ const A = prev?.index ?? null;
831
+ const prevBy = new Map((prev ? (byState.get(prev.src.id) ?? []) : []).map((e) => [e.el, e]));
832
+ /** each part's element in the screen before (when it is the same part) */
833
+ const wasOf = new Map<string, string>();
834
+ if (A && prev) {
835
+ state.since = prev.src.id;
836
+ const pairs = pairsOf(prev.src.id, A, id, index);
837
+ const back = new Map([...pairs].map(([a, b]) => [b, a]));
838
+ const kept = index.elements.filter((e) => back.has(e.id)).length;
839
+ if (kept < 0.4 * index.elements.length) state.newPage = true;
840
+ for (const e of entries) {
841
+ let was = back.get(e.el);
842
+ // a control in another control's slot is new, not that one changed
843
+ if (was != null && ACTABLE.has(e.kind) && !samePart(A, was, index, e.el)) was = undefined;
844
+ if (was == null) {
845
+ if (!state.newPage) e.facts.appeared = true;
846
+ continue;
847
+ }
848
+ wasOf.set(e.el, was);
849
+ const before = textOf(A, was);
850
+ const now = textOf(index, e.el);
851
+ if (before !== now) e.facts.changed = `${cut(before, 40)} → ${cut(now, 40)}`;
852
+ else {
853
+ const a = A.elements.find((x) => x.id === was)?.box;
854
+ if (a?.some((v, i) => Math.abs(v - e.box[i]!) > 2)) e.facts.moved = true;
855
+ const pe = prevBy.get(was);
856
+ const r = pe && pe.kind === e.kind ? restyledOf(pe, e) : undefined;
857
+ if (r) e.facts.restyled = r;
858
+ }
859
+ }
860
+ }
861
+ // the element acted on, in this screen
862
+ const act = L.src.action;
863
+ if (act) {
864
+ const action: ComponentAction = {
865
+ kind: act.kind,
866
+ ...(act.label ? { label: act.label } : {}),
867
+ };
868
+ let hit: string | undefined;
869
+ let by: "capture" | "point" | undefined;
870
+ let before: string | undefined;
871
+ if (A && (act.target || act.point?.in === "before")) {
872
+ const t =
873
+ act.target ?? (act.point ? interactiveAt(A, act.point.x, act.point.y)?.id : undefined);
874
+ by = act.target ? "capture" : "point";
875
+ if (t) {
876
+ hit = pairsOf(prev!.src.id, A, id, index).get(t);
877
+ // a pair whose text and size both changed is another element in
878
+ // its place (下一步 matched to the 上一步 that took its slot)
879
+ if (hit && !samePart(A, t, index, hit)) hit = undefined;
880
+ if (!hit) {
881
+ action.actedBefore = `${prev!.src.id}:${t}`;
882
+ action.by = by;
883
+ before = t;
884
+ }
885
+ }
886
+ } else if (act.point?.in === "after") {
887
+ hit = interactiveAt(index, act.point.x, act.point.y)?.id;
888
+ by = "point";
889
+ }
890
+ if (hit) {
891
+ action.acted = ref(hit);
892
+ action.by = by!;
893
+ // the element itself, the field it is the input of, or a control
894
+ // holding it (a span inside the button)
895
+ for (const e of entries)
896
+ if (
897
+ e.el === hit ||
898
+ e.input === ref(hit) ||
899
+ (ACTABLE.has(e.kind) && subtree(index, e.el).some((x) => x.id === hit))
900
+ )
901
+ e.facts.acted = true;
902
+ }
903
+ const holder = (list: ComponentEntry[], I: StateIndex, el: string) =>
904
+ list.find((e) => e.el === el) ??
905
+ list
906
+ .filter(
907
+ (e) =>
908
+ (ACTABLE.has(e.kind) || e.input?.endsWith(`:${el}`)) &&
909
+ subtree(I, e.el).some((x) => x.id === el),
910
+ )
911
+ .sort((a, b) => a.box[2] * a.box[3] - b.box[2] * b.box[3])[0];
912
+ const from = hit
913
+ ? holder(entries, index, hit)
914
+ : before && A
915
+ ? holder(byState.get(prev!.src.id) ?? [], A, before)
916
+ : undefined;
917
+ // what it changed: the parts that changed, restyled or appeared here,
918
+ // apart from the control itself and what sits inside it (a slider's own
919
+ // readout), and apart from a part around it whose text changed only
920
+ // inside the control (the form holding a slider reads its new value) —
921
+ // a part around it that changed elsewhere too (the wizard card a step's
922
+ // button turned) is kept. Listed on the action whether or not the
923
+ // element acted on is a part (a tab drawn as a bare link), and as the
924
+ // part's `changes` relations when it is one.
925
+ if ((hit || before) && A) {
926
+ // the control's own elements, here and in the screen before: the
927
+ // outermost acted part around the element (a field around its
928
+ // slider), else the element itself
929
+ const ownB = new Set<string>();
930
+ const ownA = new Set<string>();
931
+ if (hit) {
932
+ const outer = entries
933
+ .filter((e) => e.facts.acted && subtree(index, e.el).some((x) => x.id === hit))
934
+ .sort((a, b) => b.box[2] * b.box[3] - a.box[2] * a.box[3])[0];
935
+ for (const x of subtree(index, outer?.el ?? hit)) ownB.add(x.id);
936
+ for (const [a, b] of pairsOf(prev!.src.id, A, id, index)) if (ownB.has(b)) ownA.add(a);
937
+ }
938
+ const t =
939
+ before ?? [...pairsOf(prev!.src.id, A, id, index)].find(([, b]) => b === hit)?.[0];
940
+ if (t) for (const x of subtree(A, from && before ? from.el : t)) ownA.add(x.id);
941
+ /** a part's text without the control's own elements */
942
+ const rest = (I: StateIndex, el: string, own: Set<string>): string => {
943
+ const out: string[] = [];
944
+ for (const x of subtree(I, el)) {
945
+ if (own.has(x.id)) continue;
946
+ const l = visibleLabel(x);
947
+ if (l && !out.some((o) => o.includes(l))) out.push(l);
948
+ }
949
+ return out.join(" ").replace(/\s+/g, " ").trim();
950
+ };
951
+ const changes: string[] = [];
952
+ for (const e of entries) {
953
+ if (!(e.facts.changed || e.facts.restyled || e.facts.appeared)) continue;
954
+ if (e === from || ownB.has(e.el)) continue;
955
+ const was = wasOf.get(e.el);
956
+ if (e.facts.changed && was != null && rest(index, e.el, ownB) === rest(A, was, ownA))
957
+ continue;
958
+ changes.push(e.ref);
959
+ }
960
+ if (changes.length) action.changes = changes;
961
+ if (from)
962
+ for (const to of changes) from.relations.push({ kind: "changes", to, by: "seen" });
963
+ }
964
+ state.action = action;
965
+ }
966
+ state.components = entries.map((e) => e.ref);
967
+ byState.set(id, entries);
968
+ all.push(...entries);
969
+ prev = L;
970
+ }
971
+
972
+ // --- tracks: the same part, screen after screen --------------------------------
973
+ type Step = ComponentTrack["steps"][number];
974
+ const tracks: ComponentTrack[] = [];
975
+ const measured = loaded.filter((l) => l.index && l.parts);
976
+ const trackOf = new Map<string, ComponentTrack>();
977
+ const steps = new Map<string, Step[]>();
978
+ const stepOf = (e: ComponentEntry, I: StateIndex): Step => {
979
+ const n = stepNumber(e);
980
+ return {
981
+ state: e.state,
982
+ ref: e.ref,
983
+ text: stepText(e, I),
984
+ ...(n?.value != null ? { value: n.value } : {}),
985
+ ...(n?.unit ? { unit: n.unit } : {}),
986
+ shows: showsKey(e),
987
+ };
988
+ };
989
+ for (let i = 1; i < measured.length; i++) {
990
+ const A = measured[i - 1]!;
991
+ const B = measured[i]!;
992
+ const pairs = pairsOf(A.src.id, A.index!, B.src.id, B.index!);
993
+ const back = new Map([...pairs].map(([a, b]) => [b, a]));
994
+ const aBy = new Map((byState.get(A.src.id) ?? []).map((e) => [e.el, e]));
995
+ for (const eb of byState.get(B.src.id) ?? []) {
996
+ const was = back.get(eb.el);
997
+ const ea = was ? aBy.get(was) : undefined;
998
+ if (!ea || ea.kind !== eb.kind) continue;
999
+ // a control whose text and size both changed is another control in its
1000
+ // slot, not the same one (a card or a number may grow; a button does not)
1001
+ if (ACTABLE.has(eb.kind) && !samePart(A.index!, ea.el, B.index!, eb.el)) continue;
1002
+ eb.before = ea.ref;
1003
+ let chain = steps.get(ea.ref);
1004
+ if (!chain) {
1005
+ chain = [stepOf(ea, A.index!)];
1006
+ steps.set(ea.ref, chain);
1007
+ }
1008
+ chain.push(stepOf(eb, B.index!));
1009
+ steps.set(eb.ref, chain);
1010
+ }
1011
+ }
1012
+ const differs = (a: Step, b: Step) =>
1013
+ a.text !== b.text || a.value !== b.value || a.shows !== b.shows;
1014
+ const seen = new Set<unknown>();
1015
+ for (const e of all) {
1016
+ const chain = steps.get(e.ref);
1017
+ if (!chain || seen.has(chain)) continue;
1018
+ seen.add(chain);
1019
+ const changes = chain.filter((s, i) => i > 0 && differs(chain[i - 1]!, s)).length;
1020
+ if (!changes) continue; // the same part, unchanged throughout: a repeat, not a track
1021
+ const last = all.find((x) => x.ref === chain.at(-1)!.ref)!;
1022
+ // its state where it says more than its colours: a step that changed only
1023
+ // its state names it
1024
+ const stateful = chain.some((s, i) => i > 0 && s.shows !== chain[i - 1]!.shows);
1025
+ const t: ComponentTrack = {
1026
+ id: `t${tracks.length + 1}`,
1027
+ kind: e.kind,
1028
+ label: cut(last.label, 60),
1029
+ changes,
1030
+ steps: chain.map(({ shows, ...s }) => (stateful ? { ...s, shows } : s)),
1031
+ };
1032
+ tracks.push(t);
1033
+ for (const s of chain) trackOf.set(s.ref, t);
1034
+ }
1035
+ for (const e of all) {
1036
+ const t = trackOf.get(e.ref);
1037
+ if (t) e.track = t.id;
1038
+ }
1039
+ // repeats: the same kind, text and place on an earlier screen
1040
+ const firstOf = new Map<string, string>();
1041
+ for (const e of all) {
1042
+ if (e.track) continue;
1043
+ const key = `${e.kind}|${e.label}|${e.box.map((v) => Math.round(v / 8)).join(",")}`;
1044
+ const f = firstOf.get(key);
1045
+ if (f) e.repeats = f;
1046
+ else firstOf.set(key, e.ref);
1047
+ }
1048
+
1049
+ const groups = all
1050
+ .filter((e) => e.members?.length)
1051
+ .map((e) => ({
1052
+ ref: e.ref,
1053
+ kind: e.kind,
1054
+ ...(e.axis ? { axis: e.axis } : {}),
1055
+ members: e.members!,
1056
+ }));
1057
+ return {
1058
+ version: 2,
1059
+ about: ABOUT,
1060
+ order: COMPONENT_ORDER,
1061
+ source: {
1062
+ kind: source.kind,
1063
+ ...(source.log ? { log: rel(source.log) } : {}),
1064
+ viewport,
1065
+ ...(source.srcRatio ? { srcRatio: source.srcRatio } : {}),
1066
+ },
1067
+ stage: {
1068
+ width: stage.width,
1069
+ height: stage.height,
1070
+ phoneWidth: PHONE_W,
1071
+ phoneScale: Math.round((PHONE_W / stage.width) * 1e4) / 1e4,
1072
+ },
1073
+ states,
1074
+ components: all,
1075
+ groups,
1076
+ tracks,
1077
+ skipped,
1078
+ ms,
1079
+ };
1080
+ }
1081
+
1082
+ /** Write the inventory as `<root>/components.json`; returns its path. */
1083
+ export function writeComponents(file: ComponentsFile, root: string): string {
1084
+ const path = join(root, "components.json");
1085
+ writeFileSync(path, `${JSON.stringify(file, null, 2)}\n`);
1086
+ return path;
1087
+ }
1088
+
1089
+ // --- reading it -------------------------------------------------------------------
1090
+
1091
+ /** How many parts, on how many screens; how many of them are distinct (a
1092
+ * track counts once, a repeat not again). */
1093
+ export function componentCounts(file: ComponentsFile): {
1094
+ parts: number;
1095
+ distinct: number;
1096
+ screens: number;
1097
+ measured: number;
1098
+ tracks: number;
1099
+ } {
1100
+ const tracked = new Set<string>();
1101
+ let distinct = 0;
1102
+ for (const e of file.components) {
1103
+ if (e.repeats) continue;
1104
+ if (e.track) {
1105
+ if (tracked.has(e.track)) continue;
1106
+ tracked.add(e.track);
1107
+ }
1108
+ distinct++;
1109
+ }
1110
+ return {
1111
+ parts: file.components.length,
1112
+ distinct,
1113
+ screens: file.states.length,
1114
+ measured: file.states.filter((s) => !s.unmeasured).length,
1115
+ tracks: file.tracks.length,
1116
+ };
1117
+ }
1118
+
1119
+ const q = (s: string) => JSON.stringify(s);
1120
+ const posix = (p: string) => p.split(sep).join("/");
1121
+
1122
+ /** Where `abs` is as `film render` serves it when run in `servedFrom` (the
1123
+ * working directory, or --root): a URL from that folder ("/shots/…"),
1124
+ * which reads the same from any page under it ("/" is that folder) — or,
1125
+ * outside it, its path on this machine (`inside` false). */
1126
+ function servedUrl(abs: string, servedFrom: string): { url: string; inside: boolean } {
1127
+ const r = relative(resolve(servedFrom), abs);
1128
+ if (!r.startsWith("..") && !isAbsolute(r)) return { url: `/${posix(r)}`, inside: true };
1129
+ return { url: posix(abs), inside: false };
1130
+ }
1131
+
1132
+ /**
1133
+ * A module for the part named by `ref` (a part the film's author picked —
1134
+ * this never picks one): it lifts the part out of its saved screen with the
1135
+ * kit (screen() hidden + lift() with the inventory's options) and moves it
1136
+ * from where it sat to its fit, with one move of its own by kind, on the
1137
+ * product's own values — a number counts from the value its track held
1138
+ * first to the one it holds (and does not count when no screen before held
1139
+ * another: a count from a value the product never showed would be made up),
1140
+ * a button is pressed, an input types or runs to its own value, a card or
1141
+ * group assembles its parts.
1142
+ *
1143
+ * The saved screen's URL: with `root` (the folder components.json is in)
1144
+ * and `servedFrom` (the folder `film render` serves — the working directory,
1145
+ * or --root), a URL from that folder ("/shots/states/calc/state.html"),
1146
+ * which reads the same from any page under it; else `screens`, a path from
1147
+ * the page to the folder components.json is in.
1148
+ */
1149
+ export function exampleSource(
1150
+ file: ComponentsFile,
1151
+ ref: string,
1152
+ opts: { screens?: string; root?: string; servedFrom?: string } = {},
1153
+ ): string {
1154
+ const e = file.components.find((x) => x.ref === ref);
1155
+ if (!e)
1156
+ throw new Error(
1157
+ `components: no part ${ref} — refs are <state>:<element id>, as components.json lists them`,
1158
+ );
1159
+ const state = file.states.find((s) => s.id === e.state)!;
1160
+ let url: string;
1161
+ let where = "";
1162
+ if (opts.root && opts.servedFrom) {
1163
+ const served = servedUrl(resolve(opts.root, state.url), opts.servedFrom);
1164
+ url = served.url;
1165
+ where = served.inside
1166
+ ? `from the folder film render serves when run in ${posix(resolve(opts.servedFrom))} (or --root that folder)`
1167
+ : "the screen's path on this machine — it is outside the folder this was printed in, and film render serves one folder: render with --root on one that holds the page and this screen, and write the URL from there";
1168
+ } else {
1169
+ const base = (opts.screens ?? ".").replace(/\/+$/, "");
1170
+ url = base === "." ? state.url : `${base}/${state.url}`;
1171
+ where = "from the page";
1172
+ }
1173
+ const vp = file.source.viewport;
1174
+ const [fx, fy, fw, fh] = e.fit.box;
1175
+ const said = [
1176
+ e.facts.changed ? `changed since ${state.since}: ${e.facts.changed}` : "",
1177
+ e.facts.restyled ? `restyled since ${state.since}: ${e.facts.restyled}` : "",
1178
+ e.facts.appeared ? `appeared since ${state.since}` : "",
1179
+ e.facts.acted ? "the action acted on it" : "",
1180
+ ].filter(Boolean);
1181
+ const track = e.track ? file.tracks.find((t) => t.id === e.track) : undefined;
1182
+ const liftOpts: string[] = [
1183
+ `around: ${q(e.lift.around)}`,
1184
+ `box: ${q(e.lift.box === "ink" ? "ink" : "layout")}`,
1185
+ `pad: ${e.lift.pad}`,
1186
+ ];
1187
+ const motion = new Set(["morphRect"]);
1188
+ const before: string[] = [];
1189
+ const inner: string[] = [];
1190
+ /** the number its track's first step held, as that step's part wrote it
1191
+ * (a stat's own number, not the first digits in its text) — null when
1192
+ * that step held none or the same number as `now` */
1193
+ const firstOther = (tr: ComponentTrack | undefined, now: number) => {
1194
+ const s0 = tr?.steps[0];
1195
+ if (!s0 || s0.value == null || s0.value === now) return null;
1196
+ const p0 = file.components.find((x) => x.ref === s0.ref);
1197
+ const n0 = p0 ? (p0.values.number ?? p0.values.value) : undefined;
1198
+ return n0?.value === s0.value ? n0.text : null;
1199
+ };
1200
+ const num = e.values.number;
1201
+ if ((e.kind === "number" || e.kind === "stat") && num?.value != null) {
1202
+ const from = firstOther(track, num.value);
1203
+ // a stat's number is an element inside it: the text set is that one's
1204
+ const into = num.source.eid !== e.el ? `, ${q(num.source.eid)}` : "";
1205
+ if (from) {
1206
+ motion.add("count");
1207
+ inner.push(
1208
+ ` part.text(count(t, at + 0.2, ${q(from)}, ${q(num.text)}).text${into}); // from its first step (${track!.id}: ${track!.steps[0]!.state}) to ${num.source.state}, in the product's own format`,
1209
+ );
1210
+ } else
1211
+ inner.push(
1212
+ ` // ${q(num.text)} as the product showed it: no screen before held another value of it to count from`,
1213
+ );
1214
+ } else if (e.kind === "button") {
1215
+ motion.add("press");
1216
+ before.push(" const pressAt = at + 1.0;");
1217
+ inner.push(
1218
+ " const p = press(t, pressAt);",
1219
+ " const r = morphRect(t, at, at + 0.8, part.rect, fit);",
1220
+ " part.place({ x: r.x + (r.w * (1 - p.scale)) / 2, y: r.y + (r.h * (1 - p.scale)) / 2 + p.y, w: r.w * p.scale, h: r.h * p.scale, r: r.r });",
1221
+ );
1222
+ } else if (e.kind === "input" || e.kind === "field") {
1223
+ // its own value: the input's (a field's label is the words around it)
1224
+ const input =
1225
+ e.kind === "field" ? file.components.find((x) => x.ref === e.input) : (e as ComponentEntry);
1226
+ const inputEl = input?.el ?? (e.kind === "field" ? e.input?.split(":")[1] : e.el);
1227
+ const value = e.values.value ?? input?.values.value;
1228
+ const ownId = input?.track ?? e.track;
1229
+ const own = ownId ? file.tracks.find((t) => t.id === ownId) : undefined;
1230
+ if (inputEl && value?.text) {
1231
+ before.push(
1232
+ ` const field = part.find(${q(inputEl)}); // the input itself, in the lifted copy`,
1233
+ );
1234
+ if (value.value != null && wholeNumber(value.text)) {
1235
+ // a number (a slider, a quantity): it runs to its value rather than being typed
1236
+ const from = firstOther(own, value.value);
1237
+ if (from) {
1238
+ motion.add("count");
1239
+ inner.push(
1240
+ ` const v = count(t, at + 0.9, ${q(from)}, ${q(value.text)}).value; // from its first step (${own!.id})`,
1241
+ ` if ("value" in field) field.value = String(v); else field.textContent = String(v);`,
1242
+ );
1243
+ } else
1244
+ inner.push(
1245
+ ` // ${q(value.text)} as the product showed it: no screen before held another value of it to run from`,
1246
+ );
1247
+ } else {
1248
+ motion.add("type");
1249
+ inner.push(
1250
+ ` const typed = type(t, at + 0.9, ${q(value.text)}).text; // its own value`,
1251
+ ` if ("value" in field) field.value = typed; else field.textContent = typed;`,
1252
+ );
1253
+ }
1254
+ }
1255
+ } else if ((e.kind === "card" || e.kind === "group") && (e.members ?? e.parts)?.length) {
1256
+ motion.add("assemble");
1257
+ const kids = (e.members ?? e.parts)!.map((r) => q(r.split(":")[1]!));
1258
+ liftOpts.push(`parts: [${kids.join(", ")}]`);
1259
+ inner.push(" assemble(t, at + 0.5, part.parts()); // its parts arrive one after another");
1260
+ }
1261
+ const place =
1262
+ e.kind === "button" ? [] : [" part.place(morphRect(t, at, at + 0.8, part.rect, fit));"];
1263
+ const phone = e.phone
1264
+ ? ` — its text reads ~${Math.round(e.phone.atFit)} px on a phone there (${e.phone.asScreen} px as part of the whole screen)`
1265
+ : "";
1266
+ const lines = [
1267
+ `// ${e.ref} · ${e.kind}${e.sub ? `/${e.sub}` : ""} · ${q(cut(e.label, 50))}${said.length ? ` (${said.join("; ")})` : ""}`,
1268
+ "// The part you named, lifted out of its saved screen and moved from where it",
1269
+ "// sat to a size where it reads — the wiring, on the product's own values.",
1270
+ "// Where it goes, when and how it moves are placeholders:",
1271
+ `// import { liftPart } from "./<this file>.js";`,
1272
+ "// const p = await liftPart(stage, tl); // stage: a positioned element the film's size",
1273
+ "// p(t); // in window.__render(t)",
1274
+ `import { lift, screen } from "./kit/media.js";`,
1275
+ `import { ${[...motion].sort().join(", ")} } from "./kit/motion.js";`,
1276
+ "",
1277
+ "export async function liftPart(stage, tl) {",
1278
+ " // the saved screen it comes from: laid out (lift measures it) but not shown",
1279
+ ` const frame = document.createElement("iframe");`,
1280
+ ` Object.assign(frame.style, { position: "absolute", left: "0px", top: "0px", width: "${vp.w}px", height: "${vp.h}px", border: "0" });`,
1281
+ " stage.appendChild(frame);",
1282
+ ` // this URL: ${where}`,
1283
+ ` const src = screen(frame, ${q(url)}, { hidden: true });`,
1284
+ ` const part = lift(src, ${q(e.el)}, { ${liftOpts.join(", ")} });`,
1285
+ " await part.ready;",
1286
+ " const at = 0.3; // s",
1287
+ ` tl.event(at, "lift", { label: ${q(e.ref)} });`,
1288
+ ` // the largest box of its shape within 70 % × 60 % of the stage, centred${phone}`,
1289
+ ` const fit = { x: ${fx}, y: ${fy}, w: ${fw}, h: ${fh} };`,
1290
+ ...before,
1291
+ " return (t) => {",
1292
+ ...place,
1293
+ ...inner,
1294
+ " };",
1295
+ "}",
1296
+ "",
1297
+ ];
1298
+ return lines.join("\n");
1299
+ }
1300
+
1301
+ // --- the terminal -------------------------------------------------------------------
1302
+
1303
+ /** How many of a screen's changed, restyled or appeared parts its summary
1304
+ * names (the rest are in the file). */
1305
+ export const SUMMARY_PER_SCREEN = 12;
1306
+
1307
+ function entryLine(e: ComponentEntry): string {
1308
+ const said = [
1309
+ e.facts.acted ? "acted on" : "",
1310
+ e.facts.changed ? `changed ${e.facts.changed}` : "",
1311
+ e.facts.restyled ? `restyled ${e.facts.restyled}` : "",
1312
+ e.facts.appeared ? "appeared" : "",
1313
+ e.track ? `track ${e.track}` : "",
1314
+ ].filter(Boolean);
1315
+ return ` ${e.ref.padEnd(16)} ${`${e.kind}${e.sub ? `/${e.sub}` : ""}`.padEnd(12)} ${q(cut(e.label, 44))}${said.length ? ` · ${said.join(" · ")}` : ""}`;
1316
+ }
1317
+
1318
+ /** a count of each kind on a screen, in a fixed order: "3 number · 2 stat …" */
1319
+ function kindsLine(list: ComponentEntry[]): string {
1320
+ const n = new Map<string, number>();
1321
+ for (const e of list) n.set(e.kind, (n.get(e.kind) ?? 0) + 1);
1322
+ return [...n.entries()]
1323
+ .sort(([a], [b]) => a.localeCompare(b))
1324
+ .map(([k, c]) => `${c} ${k}`)
1325
+ .join(" · ");
1326
+ }
1327
+
1328
+ /** The summary `open-take components` prints — information only: per screen,
1329
+ * how many parts of each kind, what the action before it acted on, and
1330
+ * what changed, restyled or appeared since the screen before (in the
1331
+ * page's order); then where the rest is. */
1332
+ export function formatComponents(
1333
+ file: ComponentsFile,
1334
+ opts: {
1335
+ path: string;
1336
+ state?: string;
1337
+ dir?: string;
1338
+ /** the folder components.json is in, and the one film render serves
1339
+ * (the working directory): with them, the followed tracks and the
1340
+ * product() line */
1341
+ at?: string;
1342
+ servedFrom?: string;
1343
+ } = { path: "components.json" },
1344
+ ): string {
1345
+ const c = componentCounts(file);
1346
+ const byRef = new Map(file.components.map((e) => [e.ref, e]));
1347
+ const relations = file.components.reduce((s, e) => s + e.relations.length, 0);
1348
+ const out = [
1349
+ `components: ${c.parts} part${c.parts === 1 ? "" : "s"} in ${c.measured} saved screen${c.measured === 1 ? "" : "s"} (${c.distinct} distinct, ${c.tracks} track${c.tracks === 1 ? "" : "s"}, ${relations} relation${relations === 1 ? "" : "s"}) → ${opts.path}`,
1350
+ "each part: its values in full with where each was read, its relations, the page it was on (components.json)",
1351
+ ];
1352
+ for (const s of file.states) {
1353
+ if (opts.state && s.id !== opts.state) continue;
1354
+ const head = [
1355
+ s.action
1356
+ ? `after ${s.action.kind}${s.action.label ? ` ${q(cut(s.action.label, 30))}` : ""}`
1357
+ : "",
1358
+ s.page?.local ? `local page ${s.page.url}` : "",
1359
+ s.fidelity && !s.fidelity.ok ? "FIDELITY FAILED (its parts may not look as shot)" : "",
1360
+ s.newPage ? "a new page" : "",
1361
+ ].filter(Boolean);
1362
+ out.push("", `${s.id}${head.length ? ` — ${head.join(" · ")}` : ""}`);
1363
+ if (s.unmeasured) {
1364
+ out.push(` not measured: ${s.unmeasured}`);
1365
+ continue;
1366
+ }
1367
+ const list = s.components.map((r) => byRef.get(r)!);
1368
+ if (!list.length) {
1369
+ out.push(" no parts found (components.json `skipped` says why for each candidate)");
1370
+ continue;
1371
+ }
1372
+ out.push(` ${kindsLine(list)}`);
1373
+ const said = list.filter(
1374
+ (e) => e.facts.acted || e.facts.changed || e.facts.restyled || e.facts.appeared,
1375
+ );
1376
+ for (const e of said.slice(0, SUMMARY_PER_SCREEN)) out.push(entryLine(e));
1377
+ const more = said.length - SUMMARY_PER_SCREEN;
1378
+ if (more > 0) out.push(` … ${more} more changed in components.json`);
1379
+ }
1380
+ out.push("");
1381
+ const followed = opts.at ? followedLine(file, opts.at) : null;
1382
+ if (followed) out.push(followed);
1383
+ const changed = opts.at && opts.servedFrom ? changedLine(file, opts.at, opts.servedFrom) : null;
1384
+ if (changed) out.push(changed);
1385
+ if (opts.at && opts.servedFrom) out.push(partsLine(opts.at, opts.servedFrom));
1386
+ out.push(
1387
+ `skipped: ${file.skipped.length} candidate${file.skipped.length === 1 ? "" : "s"}, each with its reason, in components.json`,
1388
+ `snippet: open-take components ${opts.dir ?? "<dir>"} --snippet <ref> the wiring that lifts a part you name and moves it`,
1389
+ );
1390
+ return out.join("\n");
1391
+ }
1392
+
1393
+ /** How many followed tracks a summary names (the rest are in the file). */
1394
+ export const FOLLOWED_SHOWN = 4;
1395
+
1396
+ /** The tracks whose every step is a shoot still that kept the cursor
1397
+ * (shots.json) — a part followed through the screens with the mouse where
1398
+ * it was at each one — in the page's order: "followed: t1 input
1399
+ * "<label>" <a> → <b> over <first>…<last>, cursor kept at each step · …". Null when
1400
+ * there is none (a take's or a folder of screens' tracks have no stills).
1401
+ * `dir`: the folder components.json is in. Information only. */
1402
+ export function followedLine(file: ComponentsFile, dir: string): string | null {
1403
+ if (file.source.kind !== "shoot" || !file.source.log) return null;
1404
+ let log: ShotsLog;
1405
+ try {
1406
+ log = readJson<ShotsLog>(resolve(dir, file.source.log));
1407
+ } catch {
1408
+ return null;
1409
+ }
1410
+ // the states a still kept the cursor at
1411
+ const byFile = new Map((log.states?.items ?? []).map((it) => [it.still, it.id]));
1412
+ const cursorAt = new Set<string>();
1413
+ for (const s of log.stills ?? []) {
1414
+ const id = s.state ?? byFile.get(s.file);
1415
+ if (id && s.cursor) cursorAt.add(id);
1416
+ }
1417
+ const order = new Map(file.components.map((e, i) => [e.ref, i]));
1418
+ const followed = file.tracks
1419
+ .filter((t) => t.steps.length > 1 && t.steps.every((s) => cursorAt.has(s.state)))
1420
+ .sort((a, b) => (order.get(a.steps[0]!.ref) ?? 0) - (order.get(b.steps[0]!.ref) ?? 0));
1421
+ if (!followed.length) return null;
1422
+ const reads = (s: ComponentTrack["steps"][number]) =>
1423
+ s.value != null ? `${s.value}${s.unit ?? ""}` : q(cut(s.text, 20));
1424
+ const items = followed.slice(0, FOLLOWED_SHOWN).map((t) => {
1425
+ const a = t.steps[0]!;
1426
+ const b = t.steps[t.steps.length - 1]!;
1427
+ return `${t.id} ${t.kind} ${q(cut(t.label, 30))} ${reads(a)} → ${reads(b)} over ${a.state}…${b.state}, cursor kept at each step`;
1428
+ });
1429
+ const more = followed.length - FOLLOWED_SHOWN;
1430
+ if (more > 0) items.push(`… ${more} more in components.json`);
1431
+ return `followed: ${items.join(" · ")}`;
1432
+ }
1433
+
1434
+ /** What `now` gained over `was`, in whole words of `now`, compared without
1435
+ * whitespace (fact strings run words together); kit/media.js wordDiff. */
1436
+ export function addedWords(was: string, now: string): string {
1437
+ const cutShort = was.endsWith("…");
1438
+ const w = cutShort ? was.slice(0, -1) : was;
1439
+ const chars = (str: string) => {
1440
+ const out: { c: string; i: number; word: number }[] = [];
1441
+ let word = 0;
1442
+ let inWord = false;
1443
+ for (let i = 0; i < str.length; i++) {
1444
+ const c = str[i]!;
1445
+ if (/\s/.test(c)) {
1446
+ if (inWord) word++;
1447
+ inWord = false;
1448
+ } else {
1449
+ out.push({ c, i, word });
1450
+ inWord = true;
1451
+ }
1452
+ }
1453
+ return out;
1454
+ };
1455
+ const A = chars(w);
1456
+ const B = chars(now);
1457
+ let p = 0;
1458
+ while (p < A.length && p < B.length && A[p]!.c === B[p]!.c) p++;
1459
+ let k = 0;
1460
+ if (!cutShort)
1461
+ while (
1462
+ k < A.length - p &&
1463
+ k < B.length - p &&
1464
+ A[A.length - 1 - k]!.c === B[B.length - 1 - k]!.c
1465
+ )
1466
+ k++;
1467
+ while (p > 0 && p < B.length && B[p - 1]!.word === B[p]!.word) p--;
1468
+ while (k > 0 && k < B.length - p && B[B.length - k]!.word === B[B.length - 1 - k]!.word) k--;
1469
+ const from = B[p]?.i ?? now.length;
1470
+ const to = k > 0 ? (B[B.length - k]?.i ?? now.length) : now.length;
1471
+ return from < to ? now.slice(from, to).trim() : "";
1472
+ }
1473
+
1474
+ /** How many screens the changed line names (the rest: changes() on any). */
1475
+ export const CHANGED_SHOWN = 5;
1476
+
1477
+ /** One line on what the actions changed: per screen an action led to, the
1478
+ * innermost part that appeared or changed there and the words it gained —
1479
+ * and the kit's way to have them all on the material the film holds (a
1480
+ * shoot's stills(), a take's readTake()), at the URL film render serves
1481
+ * `dir` from when run in `servedFrom`. Null when no action changed a
1482
+ * measured part. Information only. */
1483
+ export function changedLine(file: ComponentsFile, dir: string, servedFrom: string): string | null {
1484
+ const byRef = new Map(file.components.map((e) => [e.ref, e]));
1485
+ const textOf = (e: ComponentEntry | undefined) => e?.values?.text?.text ?? e?.label ?? "";
1486
+ const inside = (a: Box, b: Box) =>
1487
+ a !== b &&
1488
+ a[0] >= b[0] - 0.5 &&
1489
+ a[1] >= b[1] - 0.5 &&
1490
+ a[0] + a[2] <= b[0] + b[2] + 0.5 &&
1491
+ a[1] + a[3] <= b[1] + b[3] + 0.5 &&
1492
+ a[2] * a[3] < b[2] * b[3];
1493
+ const items: string[] = [];
1494
+ let screens = 0;
1495
+ for (const st of file.states) {
1496
+ const refs = new Set(st.action?.changes ?? []);
1497
+ for (const ref of st.components) {
1498
+ const f = byRef.get(ref)?.facts;
1499
+ if (f?.changed || f?.appeared) refs.add(ref);
1500
+ }
1501
+ const parts = [...refs]
1502
+ .map((r) => byRef.get(r))
1503
+ .filter((e): e is ComponentEntry => !!e?.box && !!(e.facts.changed || e.facts.appeared));
1504
+ const inner = parts
1505
+ .filter((e) => !parts.some((o) => o !== e && inside(o.box, e.box)))
1506
+ .sort((a, b) => a.box[1] - b.box[1] || a.box[0] - b.box[0]);
1507
+ if (!inner.length) continue;
1508
+ screens++;
1509
+ if (items.length >= CHANGED_SHOWN) continue;
1510
+ const e = inner[0]!;
1511
+ const before = e.before ? byRef.get(e.before) : undefined;
1512
+ const now = textOf(e);
1513
+ const words = e.facts.appeared
1514
+ ? now
1515
+ : addedWords(before ? textOf(before) : ((e.facts.changed ?? "").split(" → ")[0] ?? ""), now);
1516
+ const more = inner.length > 1 ? ` (+${inner.length - 1})` : "";
1517
+ items.push(
1518
+ `${st.id} ${e.kind} ${e.facts.appeared ? "+" : ""}${q(cut(words || now, 30))}${more}`,
1519
+ );
1520
+ }
1521
+ if (!items.length) return null;
1522
+ const rest = screens - items.length;
1523
+ const served = servedUrl(resolve(dir), servedFrom);
1524
+ const url = served.url.endsWith("/") ? served.url : `${served.url}/`;
1525
+ const how =
1526
+ file.source.kind === "shoot"
1527
+ ? `(await stills(${q(`${url}${file.source.log ?? "shots.json"}`)}, img)).changes("<screen>")`
1528
+ : file.source.kind === "take"
1529
+ ? `(await readTake(${q(url)})).changes("<state>")`
1530
+ : `changesIn(components.json, "<screen>")`;
1531
+ return (
1532
+ `changed: ${items.join(" · ")}${rest > 0 ? ` · … ${rest} more screens` : ""}` +
1533
+ ` — ${how} in kit/media.js: each part the action before that screen changed, what it showed before and now, the words it gained and lost, its box`
1534
+ );
1535
+ }
1536
+
1537
+ /** The line that names the kit's way to use an inventory's parts:
1538
+ * product() on `dir` (the folder components.json is in) at the URL film
1539
+ * render serves it from when run in `servedFrom`. */
1540
+ export function partsLine(dir: string, servedFrom: string): string {
1541
+ const served = servedUrl(resolve(dir), servedFrom);
1542
+ const url = served.url.endsWith("/") ? served.url : `${served.url}/`;
1543
+ return (
1544
+ `parts: product(${q(url)}) in kit/media.js — each part by ref or track as one reference for cam.frame, cursor click/drag, lift, overlays; follows it through the screens and the camera` +
1545
+ (served.inside
1546
+ ? ""
1547
+ : " (its path on this machine: film render serves one folder — --root one that holds the page and this)")
1548
+ );
1549
+ }
1550
+
1551
+ /** The one line a shoot or a take prints about its inventory. */
1552
+ export function componentsLine(
1553
+ file: ComponentsFile,
1554
+ path: string,
1555
+ dir: string,
1556
+ measuredS?: number,
1557
+ ): string {
1558
+ const c = componentCounts(file);
1559
+ if (!c.measured)
1560
+ return `components: none measured — ${file.states[0]?.unmeasured ?? "no saved screens"}`;
1561
+ const secs = measuredS ?? (file.ms.measure + file.ms.pictures) / 1000;
1562
+ return (
1563
+ `components: ${c.parts} part${c.parts === 1 ? "" : "s"} in ${c.measured} saved screen${c.measured === 1 ? "" : "s"} (${c.distinct} distinct) → ${path}, measured in ${secs.toFixed(1)} s` +
1564
+ ` (open-take components ${dir}); --no-components skips it`
1565
+ );
1566
+ }