@kujoforall/paper-ui 0.0.0-stage → 0.1.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 (366) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +252 -2
  4. package/dist/components/editor/block-text.d.ts +8 -0
  5. package/dist/components/editor/block-text.d.ts.map +1 -0
  6. package/dist/components/editor/block-text.js +213 -0
  7. package/dist/components/editor/block-text.js.map +1 -0
  8. package/dist/components/editor/blocks.d.ts +33 -0
  9. package/dist/components/editor/blocks.d.ts.map +1 -0
  10. package/dist/components/editor/blocks.js +187 -0
  11. package/dist/components/editor/blocks.js.map +1 -0
  12. package/dist/components/editor/document-editor.d.ts +27 -0
  13. package/dist/components/editor/document-editor.d.ts.map +1 -0
  14. package/dist/components/editor/document-editor.js +40 -0
  15. package/dist/components/editor/document-editor.js.map +1 -0
  16. package/dist/components/editor/document-view.d.ts +22 -0
  17. package/dist/components/editor/document-view.d.ts.map +1 -0
  18. package/dist/components/editor/document-view.js +105 -0
  19. package/dist/components/editor/document-view.js.map +1 -0
  20. package/dist/components/editor/editor-state.d.ts +100 -0
  21. package/dist/components/editor/editor-state.d.ts.map +1 -0
  22. package/dist/components/editor/editor-state.js +192 -0
  23. package/dist/components/editor/editor-state.js.map +1 -0
  24. package/dist/components/editor/extensions/spoiler.d.ts +8 -0
  25. package/dist/components/editor/extensions/spoiler.d.ts.map +1 -0
  26. package/dist/components/editor/extensions/spoiler.js +36 -0
  27. package/dist/components/editor/extensions/spoiler.js.map +1 -0
  28. package/dist/components/editor/index.d.ts +9 -0
  29. package/dist/components/editor/index.d.ts.map +1 -0
  30. package/dist/components/editor/index.js +9 -0
  31. package/dist/components/editor/index.js.map +1 -0
  32. package/dist/components/editor/kinds.d.ts +99 -0
  33. package/dist/components/editor/kinds.d.ts.map +1 -0
  34. package/dist/components/editor/kinds.js +74 -0
  35. package/dist/components/editor/kinds.js.map +1 -0
  36. package/dist/components/sketch/rough.d.ts +38 -0
  37. package/dist/components/sketch/rough.d.ts.map +1 -0
  38. package/dist/components/sketch/rough.js +57 -0
  39. package/dist/components/sketch/rough.js.map +1 -0
  40. package/dist/components/ui/accent-picker.d.ts +2 -0
  41. package/dist/components/ui/accent-picker.d.ts.map +1 -0
  42. package/dist/components/ui/accent-picker.js +44 -0
  43. package/dist/components/ui/accent-picker.js.map +1 -0
  44. package/dist/components/ui/address-field.d.ts +10 -0
  45. package/dist/components/ui/address-field.d.ts.map +1 -0
  46. package/dist/components/ui/address-field.js +24 -0
  47. package/dist/components/ui/address-field.js.map +1 -0
  48. package/dist/components/ui/app-shell.d.ts +25 -0
  49. package/dist/components/ui/app-shell.d.ts.map +1 -0
  50. package/dist/components/ui/app-shell.js +69 -0
  51. package/dist/components/ui/app-shell.js.map +1 -0
  52. package/dist/components/ui/badge.d.ts +19 -0
  53. package/dist/components/ui/badge.d.ts.map +1 -0
  54. package/dist/components/ui/badge.js +62 -0
  55. package/dist/components/ui/badge.js.map +1 -0
  56. package/dist/components/ui/breadcrumbs.d.ts +10 -0
  57. package/dist/components/ui/breadcrumbs.d.ts.map +1 -0
  58. package/dist/components/ui/breadcrumbs.js +9 -0
  59. package/dist/components/ui/breadcrumbs.js.map +1 -0
  60. package/dist/components/ui/button.d.ts +13 -0
  61. package/dist/components/ui/button.d.ts.map +1 -0
  62. package/dist/components/ui/button.js +45 -0
  63. package/dist/components/ui/button.js.map +1 -0
  64. package/dist/components/ui/calendar.d.ts +5 -0
  65. package/dist/components/ui/calendar.d.ts.map +1 -0
  66. package/dist/components/ui/calendar.js +64 -0
  67. package/dist/components/ui/calendar.js.map +1 -0
  68. package/dist/components/ui/callout.d.ts +13 -0
  69. package/dist/components/ui/callout.d.ts.map +1 -0
  70. package/dist/components/ui/callout.js +27 -0
  71. package/dist/components/ui/callout.js.map +1 -0
  72. package/dist/components/ui/card.d.ts +13 -0
  73. package/dist/components/ui/card.d.ts.map +1 -0
  74. package/dist/components/ui/card.js +27 -0
  75. package/dist/components/ui/card.js.map +1 -0
  76. package/dist/components/ui/checkbox.d.ts +6 -0
  77. package/dist/components/ui/checkbox.d.ts.map +1 -0
  78. package/dist/components/ui/checkbox.js +10 -0
  79. package/dist/components/ui/checkbox.js.map +1 -0
  80. package/dist/components/ui/command-menu.d.ts +15 -0
  81. package/dist/components/ui/command-menu.d.ts.map +1 -0
  82. package/dist/components/ui/command-menu.js +24 -0
  83. package/dist/components/ui/command-menu.js.map +1 -0
  84. package/dist/components/ui/date-picker.d.ts +35 -0
  85. package/dist/components/ui/date-picker.d.ts.map +1 -0
  86. package/dist/components/ui/date-picker.js +56 -0
  87. package/dist/components/ui/date-picker.js.map +1 -0
  88. package/dist/components/ui/date-time-picker.d.ts +19 -0
  89. package/dist/components/ui/date-time-picker.d.ts.map +1 -0
  90. package/dist/components/ui/date-time-picker.js +32 -0
  91. package/dist/components/ui/date-time-picker.js.map +1 -0
  92. package/dist/components/ui/dialog.d.ts +17 -0
  93. package/dist/components/ui/dialog.d.ts.map +1 -0
  94. package/dist/components/ui/dialog.js +33 -0
  95. package/dist/components/ui/dialog.js.map +1 -0
  96. package/dist/components/ui/dropdown-menu.d.ts +20 -0
  97. package/dist/components/ui/dropdown-menu.d.ts.map +1 -0
  98. package/dist/components/ui/dropdown-menu.js +56 -0
  99. package/dist/components/ui/dropdown-menu.js.map +1 -0
  100. package/dist/components/ui/editable-text.d.ts +17 -0
  101. package/dist/components/ui/editable-text.d.ts.map +1 -0
  102. package/dist/components/ui/editable-text.js +68 -0
  103. package/dist/components/ui/editable-text.js.map +1 -0
  104. package/dist/components/ui/field-button.d.ts +14 -0
  105. package/dist/components/ui/field-button.d.ts.map +1 -0
  106. package/dist/components/ui/field-button.js +16 -0
  107. package/dist/components/ui/field-button.js.map +1 -0
  108. package/dist/components/ui/file-preview.d.ts +5 -0
  109. package/dist/components/ui/file-preview.d.ts.map +1 -0
  110. package/dist/components/ui/file-preview.js +66 -0
  111. package/dist/components/ui/file-preview.js.map +1 -0
  112. package/dist/components/ui/file-uploader.d.ts +39 -0
  113. package/dist/components/ui/file-uploader.d.ts.map +1 -0
  114. package/dist/components/ui/file-uploader.js +110 -0
  115. package/dist/components/ui/file-uploader.js.map +1 -0
  116. package/dist/components/ui/handle-row.d.ts +15 -0
  117. package/dist/components/ui/handle-row.d.ts.map +1 -0
  118. package/dist/components/ui/handle-row.js +41 -0
  119. package/dist/components/ui/handle-row.js.map +1 -0
  120. package/dist/components/ui/image-block.d.ts +31 -0
  121. package/dist/components/ui/image-block.d.ts.map +1 -0
  122. package/dist/components/ui/image-block.js +103 -0
  123. package/dist/components/ui/image-block.js.map +1 -0
  124. package/dist/components/ui/image-card.d.ts +17 -0
  125. package/dist/components/ui/image-card.d.ts.map +1 -0
  126. package/dist/components/ui/image-card.js +38 -0
  127. package/dist/components/ui/image-card.js.map +1 -0
  128. package/dist/components/ui/image-viewer.d.ts +31 -0
  129. package/dist/components/ui/image-viewer.d.ts.map +1 -0
  130. package/dist/components/ui/image-viewer.js +81 -0
  131. package/dist/components/ui/image-viewer.js.map +1 -0
  132. package/dist/components/ui/image.d.ts +11 -0
  133. package/dist/components/ui/image.d.ts.map +1 -0
  134. package/dist/components/ui/image.js +32 -0
  135. package/dist/components/ui/image.js.map +1 -0
  136. package/dist/components/ui/index.d.ts +49 -0
  137. package/dist/components/ui/index.d.ts.map +1 -0
  138. package/dist/components/ui/index.js +49 -0
  139. package/dist/components/ui/index.js.map +1 -0
  140. package/dist/components/ui/input.d.ts +16 -0
  141. package/dist/components/ui/input.d.ts.map +1 -0
  142. package/dist/components/ui/input.js +32 -0
  143. package/dist/components/ui/input.js.map +1 -0
  144. package/dist/components/ui/link-block.d.ts +13 -0
  145. package/dist/components/ui/link-block.d.ts.map +1 -0
  146. package/dist/components/ui/link-block.js +21 -0
  147. package/dist/components/ui/link-block.js.map +1 -0
  148. package/dist/components/ui/mac-card.d.ts +15 -0
  149. package/dist/components/ui/mac-card.d.ts.map +1 -0
  150. package/dist/components/ui/mac-card.js +9 -0
  151. package/dist/components/ui/mac-card.js.map +1 -0
  152. package/dist/components/ui/nav-tree.d.ts +16 -0
  153. package/dist/components/ui/nav-tree.d.ts.map +1 -0
  154. package/dist/components/ui/nav-tree.js +34 -0
  155. package/dist/components/ui/nav-tree.js.map +1 -0
  156. package/dist/components/ui/page.d.ts +12 -0
  157. package/dist/components/ui/page.d.ts.map +1 -0
  158. package/dist/components/ui/page.js +15 -0
  159. package/dist/components/ui/page.js.map +1 -0
  160. package/dist/components/ui/popover.d.ts +8 -0
  161. package/dist/components/ui/popover.d.ts.map +1 -0
  162. package/dist/components/ui/popover.js +18 -0
  163. package/dist/components/ui/popover.js.map +1 -0
  164. package/dist/components/ui/progress.d.ts +9 -0
  165. package/dist/components/ui/progress.d.ts.map +1 -0
  166. package/dist/components/ui/progress.js +9 -0
  167. package/dist/components/ui/progress.js.map +1 -0
  168. package/dist/components/ui/property.d.ts +15 -0
  169. package/dist/components/ui/property.d.ts.map +1 -0
  170. package/dist/components/ui/property.js +34 -0
  171. package/dist/components/ui/property.js.map +1 -0
  172. package/dist/components/ui/segmented.d.ts +13 -0
  173. package/dist/components/ui/segmented.d.ts.map +1 -0
  174. package/dist/components/ui/segmented.js +10 -0
  175. package/dist/components/ui/segmented.js.map +1 -0
  176. package/dist/components/ui/select-menu.d.ts +10 -0
  177. package/dist/components/ui/select-menu.d.ts.map +1 -0
  178. package/dist/components/ui/select-menu.js +10 -0
  179. package/dist/components/ui/select-menu.js.map +1 -0
  180. package/dist/components/ui/select.d.ts +11 -0
  181. package/dist/components/ui/select.d.ts.map +1 -0
  182. package/dist/components/ui/select.js +31 -0
  183. package/dist/components/ui/select.js.map +1 -0
  184. package/dist/components/ui/selection-bar.d.ts +12 -0
  185. package/dist/components/ui/selection-bar.d.ts.map +1 -0
  186. package/dist/components/ui/selection-bar.js +15 -0
  187. package/dist/components/ui/selection-bar.js.map +1 -0
  188. package/dist/components/ui/separator.d.ts +4 -0
  189. package/dist/components/ui/separator.d.ts.map +1 -0
  190. package/dist/components/ui/separator.js +12 -0
  191. package/dist/components/ui/separator.js.map +1 -0
  192. package/dist/components/ui/spotlight.d.ts +23 -0
  193. package/dist/components/ui/spotlight.d.ts.map +1 -0
  194. package/dist/components/ui/spotlight.js +107 -0
  195. package/dist/components/ui/spotlight.js.map +1 -0
  196. package/dist/components/ui/stat-tile.d.ts +8 -0
  197. package/dist/components/ui/stat-tile.d.ts.map +1 -0
  198. package/dist/components/ui/stat-tile.js +9 -0
  199. package/dist/components/ui/stat-tile.js.map +1 -0
  200. package/dist/components/ui/step-dialog.d.ts +20 -0
  201. package/dist/components/ui/step-dialog.d.ts.map +1 -0
  202. package/dist/components/ui/step-dialog.js +36 -0
  203. package/dist/components/ui/step-dialog.js.map +1 -0
  204. package/dist/components/ui/table.d.ts +8 -0
  205. package/dist/components/ui/table.d.ts.map +1 -0
  206. package/dist/components/ui/table.js +23 -0
  207. package/dist/components/ui/table.js.map +1 -0
  208. package/dist/components/ui/theme-toggle.d.ts +2 -0
  209. package/dist/components/ui/theme-toggle.d.ts.map +1 -0
  210. package/dist/components/ui/theme-toggle.js +49 -0
  211. package/dist/components/ui/theme-toggle.js.map +1 -0
  212. package/dist/components/ui/time-picker.d.ts +21 -0
  213. package/dist/components/ui/time-picker.d.ts.map +1 -0
  214. package/dist/components/ui/time-picker.js +63 -0
  215. package/dist/components/ui/time-picker.js.map +1 -0
  216. package/dist/components/ui/tooltip.d.ts +15 -0
  217. package/dist/components/ui/tooltip.d.ts.map +1 -0
  218. package/dist/components/ui/tooltip.js +21 -0
  219. package/dist/components/ui/tooltip.js.map +1 -0
  220. package/dist/components/ui/typography.d.ts +34 -0
  221. package/dist/components/ui/typography.d.ts.map +1 -0
  222. package/dist/components/ui/typography.js +72 -0
  223. package/dist/components/ui/typography.js.map +1 -0
  224. package/dist/components/ui/video-block.d.ts +14 -0
  225. package/dist/components/ui/video-block.d.ts.map +1 -0
  226. package/dist/components/ui/video-block.js +18 -0
  227. package/dist/components/ui/video-block.js.map +1 -0
  228. package/dist/hooks/use-save-queue.d.ts +13 -0
  229. package/dist/hooks/use-save-queue.d.ts.map +1 -0
  230. package/dist/hooks/use-save-queue.js +45 -0
  231. package/dist/hooks/use-save-queue.js.map +1 -0
  232. package/dist/hooks/use-selection.d.ts +10 -0
  233. package/dist/hooks/use-selection.d.ts.map +1 -0
  234. package/dist/hooks/use-selection.js +43 -0
  235. package/dist/hooks/use-selection.js.map +1 -0
  236. package/dist/hooks/use-shared-transition.d.ts +3 -0
  237. package/dist/hooks/use-shared-transition.d.ts.map +1 -0
  238. package/dist/hooks/use-shared-transition.js +38 -0
  239. package/dist/hooks/use-shared-transition.js.map +1 -0
  240. package/dist/hooks/use-upload-queue.d.ts +22 -0
  241. package/dist/hooks/use-upload-queue.d.ts.map +1 -0
  242. package/dist/hooks/use-upload-queue.js +78 -0
  243. package/dist/hooks/use-upload-queue.js.map +1 -0
  244. package/dist/lib/accent.d.ts +5 -0
  245. package/dist/lib/accent.d.ts.map +1 -0
  246. package/dist/lib/accent.js +10 -0
  247. package/dist/lib/accent.js.map +1 -0
  248. package/dist/lib/bento.d.ts +17 -0
  249. package/dist/lib/bento.d.ts.map +1 -0
  250. package/dist/lib/bento.js +92 -0
  251. package/dist/lib/bento.js.map +1 -0
  252. package/dist/lib/dates.d.ts +14 -0
  253. package/dist/lib/dates.d.ts.map +1 -0
  254. package/dist/lib/dates.js +34 -0
  255. package/dist/lib/dates.js.map +1 -0
  256. package/dist/lib/document.d.ts +79 -0
  257. package/dist/lib/document.d.ts.map +1 -0
  258. package/dist/lib/document.js +11 -0
  259. package/dist/lib/document.js.map +1 -0
  260. package/dist/lib/embed.d.ts +14 -0
  261. package/dist/lib/embed.d.ts.map +1 -0
  262. package/dist/lib/embed.js +63 -0
  263. package/dist/lib/embed.js.map +1 -0
  264. package/dist/lib/files.d.ts +23 -0
  265. package/dist/lib/files.d.ts.map +1 -0
  266. package/dist/lib/files.js +69 -0
  267. package/dist/lib/files.js.map +1 -0
  268. package/dist/lib/justified.d.ts +50 -0
  269. package/dist/lib/justified.d.ts.map +1 -0
  270. package/dist/lib/justified.js +78 -0
  271. package/dist/lib/justified.js.map +1 -0
  272. package/dist/lib/rich-text-dom.d.ts +14 -0
  273. package/dist/lib/rich-text-dom.d.ts.map +1 -0
  274. package/dist/lib/rich-text-dom.js +143 -0
  275. package/dist/lib/rich-text-dom.js.map +1 -0
  276. package/dist/lib/rich-text.d.ts +15 -0
  277. package/dist/lib/rich-text.d.ts.map +1 -0
  278. package/dist/lib/rich-text.js +35 -0
  279. package/dist/lib/rich-text.js.map +1 -0
  280. package/dist/lib/spotlight.d.ts +22 -0
  281. package/dist/lib/spotlight.d.ts.map +1 -0
  282. package/dist/lib/spotlight.js +66 -0
  283. package/dist/lib/spotlight.js.map +1 -0
  284. package/dist/lib/tree.d.ts +17 -0
  285. package/dist/lib/tree.d.ts.map +1 -0
  286. package/dist/lib/tree.js +35 -0
  287. package/dist/lib/tree.js.map +1 -0
  288. package/dist/lib/utils.d.ts +3 -0
  289. package/dist/lib/utils.d.ts.map +1 -0
  290. package/dist/lib/utils.js +6 -0
  291. package/dist/lib/utils.js.map +1 -0
  292. package/dist/styles/notion.css +526 -0
  293. package/package.json +106 -5
  294. package/src/components/editor/block-text.tsx +230 -0
  295. package/src/components/editor/blocks.tsx +368 -0
  296. package/src/components/editor/document-editor.tsx +101 -0
  297. package/src/components/editor/document-view.tsx +192 -0
  298. package/src/components/editor/editor-state.tsx +270 -0
  299. package/src/components/editor/extensions/spoiler.tsx +76 -0
  300. package/src/components/editor/index.ts +9 -0
  301. package/src/components/editor/kinds.ts +158 -0
  302. package/src/components/sketch/rough.tsx +90 -0
  303. package/src/components/ui/accent-picker.tsx +72 -0
  304. package/src/components/ui/address-field.tsx +57 -0
  305. package/src/components/ui/app-shell.tsx +138 -0
  306. package/src/components/ui/badge.tsx +81 -0
  307. package/src/components/ui/breadcrumbs.tsx +25 -0
  308. package/src/components/ui/button.tsx +73 -0
  309. package/src/components/ui/calendar.tsx +100 -0
  310. package/src/components/ui/callout.tsx +62 -0
  311. package/src/components/ui/card.tsx +90 -0
  312. package/src/components/ui/checkbox.tsx +32 -0
  313. package/src/components/ui/command-menu.tsx +72 -0
  314. package/src/components/ui/date-picker.tsx +170 -0
  315. package/src/components/ui/date-time-picker.tsx +117 -0
  316. package/src/components/ui/dialog.tsx +124 -0
  317. package/src/components/ui/dropdown-menu.tsx +183 -0
  318. package/src/components/ui/editable-text.tsx +109 -0
  319. package/src/components/ui/field-button.tsx +68 -0
  320. package/src/components/ui/file-preview.tsx +80 -0
  321. package/src/components/ui/file-uploader.tsx +376 -0
  322. package/src/components/ui/handle-row.tsx +116 -0
  323. package/src/components/ui/image-block.tsx +238 -0
  324. package/src/components/ui/image-card.tsx +102 -0
  325. package/src/components/ui/image-viewer.tsx +187 -0
  326. package/src/components/ui/image.tsx +96 -0
  327. package/src/components/ui/index.ts +83 -0
  328. package/src/components/ui/input.tsx +99 -0
  329. package/src/components/ui/link-block.tsx +82 -0
  330. package/src/components/ui/mac-card.tsx +46 -0
  331. package/src/components/ui/nav-tree.tsx +86 -0
  332. package/src/components/ui/page.tsx +25 -0
  333. package/src/components/ui/popover.tsx +42 -0
  334. package/src/components/ui/progress.tsx +36 -0
  335. package/src/components/ui/property.tsx +64 -0
  336. package/src/components/ui/segmented.tsx +42 -0
  337. package/src/components/ui/select-menu.tsx +46 -0
  338. package/src/components/ui/select.tsx +124 -0
  339. package/src/components/ui/selection-bar.tsx +50 -0
  340. package/src/components/ui/separator.tsx +30 -0
  341. package/src/components/ui/spotlight.tsx +222 -0
  342. package/src/components/ui/stat-tile.tsx +25 -0
  343. package/src/components/ui/step-dialog.tsx +104 -0
  344. package/src/components/ui/table.tsx +42 -0
  345. package/src/components/ui/theme-toggle.tsx +101 -0
  346. package/src/components/ui/time-picker.tsx +173 -0
  347. package/src/components/ui/tooltip.tsx +64 -0
  348. package/src/components/ui/typography.tsx +168 -0
  349. package/src/components/ui/video-block.tsx +60 -0
  350. package/src/hooks/use-save-queue.ts +66 -0
  351. package/src/hooks/use-selection.ts +44 -0
  352. package/src/hooks/use-shared-transition.ts +40 -0
  353. package/src/hooks/use-upload-queue.ts +111 -0
  354. package/src/lib/accent.ts +11 -0
  355. package/src/lib/bento.ts +87 -0
  356. package/src/lib/dates.ts +39 -0
  357. package/src/lib/document.ts +55 -0
  358. package/src/lib/embed.ts +62 -0
  359. package/src/lib/files.ts +73 -0
  360. package/src/lib/justified.ts +106 -0
  361. package/src/lib/rich-text-dom.ts +136 -0
  362. package/src/lib/rich-text.ts +50 -0
  363. package/src/lib/spotlight.ts +78 -0
  364. package/src/lib/tree.ts +41 -0
  365. package/src/lib/utils.ts +6 -0
  366. package/src/styles/notion.css +526 -0
@@ -0,0 +1,40 @@
1
+ "use client";
2
+
3
+ import { useSyncExternalStore } from "react";
4
+
5
+ /* Growing one element out of another with the browser's View Transitions: a
6
+ thumbnail into a full view and back.
7
+
8
+ The browser pairs two elements by a shared `view-transition-name`, and only
9
+ one element in the page may carry a given name at a time. With a grid of
10
+ hundreds, something has to say which thumbnail it is. This is that: the id
11
+ of the item last opened.
12
+
13
+ // on the thumbnail
14
+ const active = useActiveItem();
15
+ <img style={{ viewTransitionName: active === id && !viewerOpen ? "shared" : undefined }} />
16
+ // when it is pressed, before navigating
17
+ setActiveItem(id);
18
+ // on the full view, and whenever it steps to another item
19
+ <img style={{ viewTransitionName: "shared" }} />
20
+ useEffect(() => setActiveItem(id), [id]);
21
+
22
+ The thumbnail drops the name while the view is open, or the two would hold
23
+ it at once and the browser would skip the transition. Start the transition
24
+ with `document.startViewTransition`, or your router's own switch for it.
25
+ The stylesheet's `::view-transition-group(shared)` rules set the timing. */
26
+ let active: string | null = null;
27
+ const listeners = new Set<() => void>();
28
+
29
+ export function setActiveItem(id: string): void {
30
+ if (active === id) return;
31
+ active = id;
32
+ for (const listener of listeners) listener();
33
+ }
34
+
35
+ const subscribe = (listener: () => void): (() => void) => {
36
+ listeners.add(listener);
37
+ return () => listeners.delete(listener);
38
+ };
39
+
40
+ export const useActiveItem = (): string | null => useSyncExternalStore(subscribe, () => active);
@@ -0,0 +1,111 @@
1
+ "use client";
2
+
3
+ import { useCallback, useEffect, useRef, useState } from "react";
4
+
5
+ export type UploadState = "waiting" | "uploading" | "done" | "failed" | "rejected";
6
+
7
+ export type UploadItem = {
8
+ key: number;
9
+ file: File;
10
+ state: UploadState;
11
+ /** 0 to 1, while uploading. */
12
+ progress: number;
13
+ /** Why it failed or was turned away, or anything the upload wants said. */
14
+ message?: string;
15
+ };
16
+
17
+ /* A queue of files being sent, a few at a time.
18
+
19
+ You supply `upload`: send one file, call `report` with 0 to 1 as it goes,
20
+ resolve when it is done (optionally with a message to show), throw an Error
21
+ when it fails. The queue does the rest: holds the order, runs `parallel` at
22
+ once, keeps each file's progress, and lets a failed one be tried again.
23
+
24
+ `validate` turns a file away before anything is sent, by returning why.
25
+
26
+ The queue lives in the component that calls this. For uploads that must
27
+ survive navigation, keep the same logic in a store outside React instead. */
28
+ export function useUploadQueue({
29
+ upload,
30
+ validate,
31
+ parallel = 3,
32
+ }: {
33
+ upload: (file: File, report: (progress: number) => void) => Promise<string | void>;
34
+ validate?: (file: File) => string | undefined;
35
+ parallel?: number;
36
+ }) {
37
+ const items = useRef<UploadItem[]>([]);
38
+ const nextKey = useRef(1);
39
+ const running = useRef(0);
40
+ const [, render] = useState(0);
41
+ // The latest functions, so a queue started with one render's props finishes with the current ones.
42
+ const latest = useRef({ upload, validate });
43
+ latest.current = { upload, validate };
44
+
45
+ // Progress arrives many times a second per file; one render per frame is plenty.
46
+ const frame = useRef(0);
47
+ const changed = useCallback(() => {
48
+ if (frame.current !== 0) return;
49
+ frame.current = requestAnimationFrame(() => {
50
+ frame.current = 0;
51
+ render((n) => n + 1);
52
+ });
53
+ }, []);
54
+ useEffect(() => () => cancelAnimationFrame(frame.current), []);
55
+
56
+ const pump = useCallback(() => {
57
+ while (running.current < parallel) {
58
+ const item = items.current.find((i) => i.state === "waiting");
59
+ if (item === undefined) return;
60
+ running.current += 1;
61
+ Object.assign(item, { state: "uploading", progress: 0, message: undefined });
62
+ changed();
63
+ latest.current
64
+ .upload(item.file, (progress) => {
65
+ item.progress = progress;
66
+ changed();
67
+ })
68
+ .then(
69
+ (message) => Object.assign(item, { state: "done", progress: 1, message: message ?? undefined }),
70
+ (error: unknown) => Object.assign(item, { state: "failed", message: error instanceof Error ? error.message : "It could not be sent" }),
71
+ )
72
+ .finally(() => {
73
+ running.current -= 1;
74
+ changed();
75
+ pump();
76
+ });
77
+ }
78
+ }, [parallel, changed]);
79
+
80
+ const add = useCallback(
81
+ (files: File[]) => {
82
+ for (const file of files) {
83
+ const refused = latest.current.validate?.(file);
84
+ items.current.push({ key: nextKey.current++, file, state: refused === undefined ? "waiting" : "rejected", progress: 0, message: refused });
85
+ }
86
+ changed();
87
+ pump();
88
+ },
89
+ [changed, pump],
90
+ );
91
+
92
+ const retry = useCallback(
93
+ (key: number) => {
94
+ const item = items.current.find((i) => i.key === key);
95
+ if (item?.state !== "failed") return;
96
+ item.state = "waiting";
97
+ changed();
98
+ pump();
99
+ },
100
+ [changed, pump],
101
+ );
102
+
103
+ /** Drops everything that has reached an end; work in flight stays. */
104
+ const clear = useCallback(() => {
105
+ items.current = items.current.filter((i) => i.state === "waiting" || i.state === "uploading");
106
+ changed();
107
+ }, [changed]);
108
+
109
+ const list = [...items.current];
110
+ return { items: list, busy: list.some((i) => i.state === "waiting" || i.state === "uploading"), add, retry, clear };
111
+ }
@@ -0,0 +1,11 @@
1
+ // The colour themes, independent of light and dark. "paper" is the plain one: white
2
+ // and warm grey with the blue accent, and it is the absence of a theme, so nothing is
3
+ // set for it. Each of the others sets the accent and washes every surface with it;
4
+ // the colours live in notion.css under [data-accent]. This is only their names
5
+ // and where the choice is kept.
6
+ export const ACCENTS = ["paper", "blue", "green", "purple", "pink", "orange", "red", "brown"] as const;
7
+ export type Accent = (typeof ACCENTS)[number];
8
+ export const ACCENT_KEY = "accent";
9
+
10
+ // Runs in <head> before first paint, so the page never flashes the wrong theme.
11
+ export const ACCENT_INIT_SCRIPT = `try{var a=localStorage.getItem(${JSON.stringify(ACCENT_KEY)});if(a&&a!=="paper")document.documentElement.dataset.accent=a}catch(e){}`;
@@ -0,0 +1,87 @@
1
+ /* A bento: pictures packed into a grid of square-ish cells with no holes, so a
2
+ day's photos read as one composed block rather than a list that ran out.
3
+
4
+ Each picture asks for a span (wide ones two columns, tall ones two rows, a
5
+ chosen one both). They are placed in order at the first free cell, shrinking
6
+ to fit where the ask cannot be met. A grid packed that way can still leave
7
+ gaps, so afterwards each gap is filled by growing a neighbour into it, which
8
+ keeps every picture in the order it was placed. */
9
+
10
+ export type Tile<T> = { item: T; col: number; row: number; width: number; height: number };
11
+
12
+ export function bento<T>(
13
+ items: T[],
14
+ columns: number,
15
+ ask: (item: T, index: number) => [number, number],
16
+ /** Width over height of each picture, and of one cell, so a gap goes to the neighbour it crops least. */
17
+ shape?: { aspect: (item: T) => number; cell: number },
18
+ ): { tiles: Tile<T>[]; rows: number } {
19
+ const grid: (number | null)[][] = [];
20
+ const free = (r: number, c: number) => c >= 0 && c < columns && (grid[r]?.[c] ?? null) === null;
21
+ const claim = (r: number, c: number, who: number) => {
22
+ while (grid.length <= r) grid.push(Array.from({ length: columns }, () => null));
23
+ grid[r]![c] = who;
24
+ };
25
+ const fits = (r: number, c: number, w: number, h: number) => {
26
+ for (let y = r; y < r + h; y++) for (let x = c; x < c + w; x++) if (!free(y, x)) return false;
27
+ return true;
28
+ };
29
+
30
+ const tiles: Tile<T>[] = [];
31
+ items.forEach((item, index) => {
32
+ let [w, h] = ask(item, index);
33
+ w = Math.min(Math.max(w, 1), columns);
34
+ h = Math.max(h, 1);
35
+ let at = 0;
36
+ while (!free(Math.floor(at / columns), at % columns)) at++;
37
+ const row = Math.floor(at / columns);
38
+ const col = at % columns;
39
+ // The first free cell is free, so one of these always fits.
40
+ const size = ([[w, h], [1, h], [w, 1], [1, 1]] as const).find(([sw, sh]) => fits(row, col, sw, sh))!;
41
+ [w, h] = size;
42
+ for (let y = row; y < row + h; y++) for (let x = col; x < col + w; x++) claim(y, x, index);
43
+ tiles.push({ item, col, row, width: w, height: h });
44
+ });
45
+
46
+ const rows = grid.length;
47
+ const owner = (r: number, c: number) => (r >= 0 && r < rows && c >= 0 && c < columns ? (grid[r]![c] ?? null) : null);
48
+ const column = (t: Tile<T>, c: number) => Array.from({ length: t.height }, (_, k) => t.row + k).every((r) => owner(r, c) === null);
49
+ const line = (t: Tile<T>, r: number) => Array.from({ length: t.width }, (_, k) => t.col + k).every((c) => owner(r, c) === null);
50
+ const take = (t: Tile<T>, index: number) => {
51
+ for (let y = t.row; y < t.row + t.height; y++) for (let x = t.col; x < t.col + t.width; x++) grid[y]![x] = index;
52
+ };
53
+
54
+ // Each gap is filled by growing a neighbour whose whole edge borders it.
55
+ // Of those that can, the one whose new shape is nearest its picture's grows,
56
+ // so a portrait grows down and a landscape across.
57
+ const misfit = (t: Tile<T>, w: number, h: number) => (shape === undefined ? 0 : Math.abs(Math.log(((w / h) * shape.cell) / shape.aspect(t.item))));
58
+ for (let changed = true; changed; ) {
59
+ changed = false;
60
+ for (let r = 0; r < rows; r++) {
61
+ for (let c = 0; c < columns; c++) {
62
+ if (owner(r, c) !== null) continue;
63
+ const growths: { who: number; w: number; h: number; apply: (t: Tile<T>) => void }[] = [];
64
+ const left = owner(r, c - 1);
65
+ if (left !== null && tiles[left]!.col + tiles[left]!.width === c && column(tiles[left]!, c)) growths.push({ who: left, w: 1, h: 0, apply: (t) => t.width++ });
66
+ const right = owner(r, c + 1);
67
+ if (right !== null && tiles[right]!.col === c + 1 && column(tiles[right]!, c)) growths.push({ who: right, w: 1, h: 0, apply: (t) => (t.col--, t.width++) });
68
+ const above = owner(r - 1, c);
69
+ if (above !== null && tiles[above]!.row + tiles[above]!.height === r && line(tiles[above]!, r)) growths.push({ who: above, w: 0, h: 1, apply: (t) => t.height++ });
70
+ const below = owner(r + 1, c);
71
+ if (below !== null && tiles[below]!.row === r + 1 && line(tiles[below]!, r)) growths.push({ who: below, w: 0, h: 1, apply: (t) => (t.row--, t.height++) });
72
+ if (growths.length === 0) continue;
73
+ const best = growths.reduce((a, b) => {
74
+ const ta = tiles[a.who]!;
75
+ const tb = tiles[b.who]!;
76
+ return misfit(tb, tb.width + b.w, tb.height + b.h) < misfit(ta, ta.width + a.w, ta.height + a.h) ? b : a;
77
+ });
78
+ const tile = tiles[best.who]!;
79
+ best.apply(tile);
80
+ take(tile, best.who);
81
+ changed = true;
82
+ }
83
+ }
84
+ }
85
+
86
+ return { tiles, rows };
87
+ }
@@ -0,0 +1,39 @@
1
+ /* Dates and times as the strings a form holds: `YYYY-MM-DD`, `HH:MM`, and the two
2
+ joined by a `T`. They are calendar values, not instants, so nothing here goes
3
+ through UTC: `new Date("2027-01-12")` is midnight in Greenwich, which is the day
4
+ before for anyone west of it. */
5
+
6
+ const pad = (n: number): string => String(n).padStart(2, "0");
7
+
8
+ export function toISODate(date: Date): string {
9
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
10
+ }
11
+
12
+ /** The local day a `YYYY-MM-DD` names, or undefined if it names none (30 February). */
13
+ export function fromISODate(value: string | null | undefined): Date | undefined {
14
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value ?? "");
15
+ if (match === null) return undefined;
16
+ const date = new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
17
+ return toISODate(date) === value ? date : undefined;
18
+ }
19
+
20
+ export function isTime(value: string | null | undefined): value is string {
21
+ return /^([01]\d|2[0-3]):[0-5]\d$/.test(value ?? "");
22
+ }
23
+
24
+ /** Splits `YYYY-MM-DDTHH:MM`; either half is null when it is missing or wrong. */
25
+ export function splitDateTime(value: string | null | undefined): { date: string | null; time: string | null } {
26
+ const [date = "", time = ""] = (value ?? "").split("T");
27
+ return {
28
+ date: fromISODate(date) === undefined ? null : date,
29
+ time: isTime(time.slice(0, 5)) ? time.slice(0, 5) : null,
30
+ };
31
+ }
32
+
33
+ const DAY = new Intl.DateTimeFormat("en-GB", { weekday: "short", day: "numeric", month: "short", year: "numeric" });
34
+ const SHORT_DAY = new Intl.DateTimeFormat("en-GB", { day: "numeric", month: "short", year: "numeric" });
35
+
36
+ /** "Sun 27 Dec 2026" */
37
+ export const formatDay = (date: Date): string => DAY.format(date).replace(",", "");
38
+ /** "27 Dec 2026" */
39
+ export const formatShortDay = (date: Date): string => SHORT_DAY.format(date);
@@ -0,0 +1,55 @@
1
+ import type { Text } from "./rich-text.ts";
2
+
3
+ /*
4
+ * A document as data: Notion's blocks, nested where Notion nests them. Plain
5
+ * JSON, so an app stores it whole and validates it on its server; the editor in
6
+ * components/editor edits exactly this shape.
7
+ */
8
+
9
+ /** Every block has an id the editor chose, so one can be focused before it is saved. */
10
+ type Id = { id: number };
11
+
12
+ export type Block = Id &
13
+ (
14
+ | { type: "paragraph"; text: Text }
15
+ | { type: "heading"; level: 1 | 2 | 3; text: Text }
16
+ | { type: "bullet" | "numbered"; text: Text; children: Block[] }
17
+ | { type: "check"; checked: boolean; text: Text; children: Block[] }
18
+ | { type: "quote"; text: Text }
19
+ | { type: "divider" }
20
+ /**
21
+ * A picture. `src` is what the browser loads; `ref` is the owner's own name
22
+ * for an uploaded one (its id in the owner's store), kept beside it so the
23
+ * owner can tell which uploads a document still uses. `width` is the share
24
+ * of the column; `aspect` holds the shape so nothing moves while it loads.
25
+ */
26
+ | { type: "image"; src?: string; ref?: string; aspect?: number; caption: Text; width: number }
27
+ /** A YouTube or Vimeo video, played in place. Empty until a link is given. */
28
+ | { type: "video"; url: string; caption: Text }
29
+ /** A web page as a card, with a name the writer types. */
30
+ | { type: "bookmark"; url: string; title: string }
31
+ );
32
+
33
+ /**
34
+ * A block an app adds through a BlockExtension (components/editor): its own
35
+ * `type`, and whatever fields it needs. One with `text` is written in like a
36
+ * paragraph; one with `children` holds blocks; one with neither is picked whole.
37
+ */
38
+ export type CustomBlock = { id: number; type: string; text?: Text; children?: AnyBlock[]; [field: string]: unknown };
39
+
40
+ /** What a document holds: the kit's blocks and any an app adds. */
41
+ export type AnyBlock = Block | CustomBlock;
42
+
43
+ /** List items nest under one another and run together as one list. */
44
+ export const LISTS = ["bullet", "numbered", "check"] as const;
45
+ export const isListType = (type: string): boolean => (LISTS as readonly string[]).includes(type);
46
+
47
+ /** The kinds that hold children. */
48
+ export const nests = (type: string): boolean => isListType(type);
49
+
50
+ /** Whether `child` may sit inside `parent`. Lists hold list items. */
51
+ export const holds = (parent: string, child: string): boolean => isListType(parent) && isListType(child);
52
+
53
+ /** Kinds without words: picked as a whole in the editor rather than given a caret. */
54
+ export const WHOLE = ["divider", "image", "video", "bookmark"] as const;
55
+ export const isWhole = (type: string): boolean => (WHOLE as readonly string[]).includes(type);
@@ -0,0 +1,62 @@
1
+ /* Addresses for things embedded in a document: what counts as a web address, and
2
+ where a video link plays. Pure, so a server can check what a browser sends with
3
+ the same rules. */
4
+
5
+ /** A web address. Anything else, `javascript:` above all, is refused. */
6
+ export function isWebUrl(value: string): boolean {
7
+ if (!/^https?:\/\//i.test(value)) return false;
8
+ try {
9
+ new URL(value);
10
+ return true;
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
15
+
16
+ /** A link in text a reader may follow: a web or mail address. Never `javascript:` or `data:`. */
17
+ export function isSafeHref(value: string): boolean {
18
+ return /^mailto:/i.test(value) || isWebUrl(value);
19
+ }
20
+
21
+ /** The site a link is on, for showing in place of a missing name. */
22
+ export function hostOf(url: string): string {
23
+ try {
24
+ return new URL(url).hostname.replace(/^www\./, "");
25
+ } catch {
26
+ return url;
27
+ }
28
+ }
29
+
30
+ /** "1h2m3s", "90s" or "90" as seconds. */
31
+ function seconds(t: string | null): number | null {
32
+ const m = t?.match(/^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?$/);
33
+ if (!m || !t) return null;
34
+ return Number(m[1] ?? 0) * 3600 + Number(m[2] ?? 0) * 60 + Number(m[3] ?? 0);
35
+ }
36
+
37
+ /**
38
+ * Where a YouTube or Vimeo link plays, or null when it is not one. YouTube is
39
+ * embedded from its privacy-enhanced domain, which sets no cookie until the video
40
+ * is played, and Vimeo is asked not to track. A start time in a YouTube link
41
+ * (`t=`) is kept.
42
+ */
43
+ export function videoEmbed(url: string): string | null {
44
+ if (!isWebUrl(url)) return null;
45
+ const u = new URL(url);
46
+ const host = u.hostname.replace(/^(www|m|music)\./, "");
47
+ if (host === "vimeo.com") {
48
+ const id = u.pathname.match(/^\/(\d+)/)?.[1];
49
+ return id === undefined ? null : `https://player.vimeo.com/video/${id}?dnt=1`;
50
+ }
51
+ const id =
52
+ host === "youtu.be"
53
+ ? u.pathname.slice(1)
54
+ : host === "youtube.com"
55
+ ? u.pathname === "/watch"
56
+ ? u.searchParams.get("v")
57
+ : (u.pathname.match(/^\/(?:shorts|embed|live)\/([\w-]+)/)?.[1] ?? null)
58
+ : null;
59
+ if (!id || !/^[\w-]{6,}$/.test(id)) return null;
60
+ const start = seconds(u.searchParams.get("t") ?? u.searchParams.get("start"));
61
+ return `https://www.youtube-nocookie.com/embed/${id}${start ? `?start=${start}` : ""}`;
62
+ }
@@ -0,0 +1,73 @@
1
+ /* Getting files from a person: the system's chooser, a whole folder, or a
2
+ drop. And sending one with progress, which `fetch` cannot report. */
3
+
4
+ const byName = (files: File[]): File[] => files.sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true }));
5
+
6
+ /** The system dialog for files (or one, with `single`), or for a whole folder. Built on demand: nothing in the page needs to own an <input>. */
7
+ export function pickFiles(onPicked: (files: File[]) => void, options: { folder?: boolean; accept?: string; single?: boolean } = {}): void {
8
+ const input = document.createElement("input");
9
+ input.type = "file";
10
+ input.multiple = !options.single;
11
+ if (options.folder) input.webkitdirectory = true;
12
+ else if (options.accept !== undefined) input.accept = options.accept;
13
+ input.onchange = () => onPicked(byName([...(input.files ?? [])]));
14
+ input.click();
15
+ }
16
+
17
+ const fileOf = (entry: FileSystemFileEntry): Promise<File> => new Promise((resolve, reject) => entry.file(resolve, reject));
18
+
19
+ async function childrenOf(directory: FileSystemDirectoryEntry): Promise<FileSystemEntry[]> {
20
+ const reader = directory.createReader();
21
+ const all: FileSystemEntry[] = [];
22
+ // readEntries hands back a page at a time and signals the end with an empty one.
23
+ for (;;) {
24
+ const page = await new Promise<FileSystemEntry[]>((resolve, reject) => reader.readEntries(resolve, reject));
25
+ if (page.length === 0) return all;
26
+ all.push(...page);
27
+ }
28
+ }
29
+
30
+ async function walk(entry: FileSystemEntry, into: File[]): Promise<void> {
31
+ if (entry.isFile) into.push(await fileOf(entry as FileSystemFileEntry));
32
+ else if (entry.isDirectory) for (const child of await childrenOf(entry as FileSystemDirectoryEntry)) await walk(child, into);
33
+ }
34
+
35
+ /** Everything in a drop: loose files as they are, folders walked to their leaves. Call it in the drop handler itself. */
36
+ export async function filesFromDrop(data: DataTransfer): Promise<File[]> {
37
+ // The entries must be taken synchronously; the list is emptied once the event returns.
38
+ const dropped = [...data.items].map((item) => ({ entry: item.webkitGetAsEntry(), file: item.getAsFile() }));
39
+ const files: File[] = [];
40
+ for (const { entry, file } of dropped) {
41
+ if (entry?.isDirectory) await walk(entry, files);
42
+ else if (file !== null) files.push(file);
43
+ }
44
+ return byName(files);
45
+ }
46
+
47
+ /** True for a drag that is carrying files, as opposed to text or an element of the page. */
48
+ export const carriesFiles = (event: { dataTransfer: DataTransfer | null }): boolean => event.dataTransfer?.types.includes("Files") ?? false;
49
+
50
+ /**
51
+ * One request with upload progress, 0 to 1. Resolves with the status and the
52
+ * parsed JSON body whatever the status; rejects only when the connection
53
+ * fails, which is the case worth retrying. `headers` are sent as given: a
54
+ * custom one is how a server tells this from another site's form.
55
+ */
56
+ export function sendWithProgress(
57
+ method: string,
58
+ url: string,
59
+ body: Blob | FormData,
60
+ onProgress: (progress: number) => void,
61
+ headers: Record<string, string> = {},
62
+ ): Promise<{ status: number; body: unknown }> {
63
+ return new Promise((resolve, reject) => {
64
+ const xhr = new XMLHttpRequest();
65
+ xhr.open(method, url);
66
+ for (const [name, value] of Object.entries(headers)) xhr.setRequestHeader(name, value);
67
+ xhr.responseType = "json";
68
+ xhr.upload.onprogress = (event) => event.lengthComputable && onProgress(event.loaded / event.total);
69
+ xhr.onload = () => resolve({ status: xhr.status, body: xhr.response });
70
+ xhr.onerror = xhr.ontimeout = xhr.onabort = () => reject(new Error("The connection was lost"));
71
+ xhr.send(body);
72
+ });
73
+ }
@@ -0,0 +1,106 @@
1
+ /* Rows of pictures that each fill the width exactly, like a contact sheet:
2
+ every row is one height, scaled so its pictures and the gaps between them
3
+ add up to the width. The heights are known before anything is drawn, which
4
+ is what lets a long list be virtualised without measuring.
5
+
6
+ Works on anything with an aspect ratio. Give `group` to start a new section
7
+ (with a heading line) whenever its value changes: months, say. */
8
+
9
+ export type Cell<T> = { item: T; width: number };
10
+ export type Line<T> =
11
+ | { kind: "heading"; key: string; label: string; height: number }
12
+ | { kind: "row"; key: string; height: number; cells: Cell<T>[] };
13
+
14
+ /** One height for every row, or a rhythm: a height chosen by the row's number. */
15
+ export type RowHeight = number | ((row: number) => number);
16
+
17
+ /** A row height from the room there is: about one and a half pictures across a phone, three a tablet, four a wide screen. */
18
+ export const rowHeightFor = (width: number): number => Math.round(Math.min(300, Math.max(170, width / 4.6)));
19
+
20
+ /** A magazine's rhythm: every fourth row is a tall one of one or two pictures, and the rows between alternate taller and shorter. For a selection, not an archive. */
21
+ export const editorialRhythm =
22
+ (width: number): ((row: number) => number) =>
23
+ (row) => {
24
+ const base = rowHeightFor(width);
25
+ if (row % 4 === 0) return Math.round(Math.min(560, Math.max(base * 1.5, width * 0.4)));
26
+ return Math.round(base * (row % 2 === 1 ? 1.2 : 0.95));
27
+ };
28
+
29
+ export function justify<T>(
30
+ items: T[],
31
+ width: number,
32
+ options: {
33
+ id: (item: T) => string;
34
+ /** Width over height. */
35
+ aspect: (item: T) => number;
36
+ rowHeight: RowHeight;
37
+ gap?: number;
38
+ /** A value that starts a new section when it changes, and that section's heading. */
39
+ group?: (item: T) => { key: string; label: string };
40
+ headingHeight?: number;
41
+ },
42
+ ): Line<T>[] {
43
+ const { id, aspect, rowHeight, gap = 12, group, headingHeight = 64 } = options;
44
+ const lines: Line<T>[] = [];
45
+ if (width <= 0) return lines;
46
+
47
+ // A panorama or a sliver would otherwise set the height of its whole row.
48
+ const clamped = (item: T): number => Math.min(Math.max(aspect(item), 0.5), 3);
49
+ let section = "";
50
+ let pending: T[] = [];
51
+ let ratios = 0;
52
+ let row = 0;
53
+ const target = (): number => (typeof rowHeight === "number" ? rowHeight : rowHeight(row));
54
+
55
+ const flush = (full: boolean): void => {
56
+ if (pending.length === 0) return;
57
+ const room = width - gap * (pending.length - 1);
58
+ // A last row that is not full keeps the target height and stays ragged;
59
+ // stretching three pictures across the page would blow them up.
60
+ const height = full ? room / ratios : Math.min(target(), room / ratios);
61
+ lines.push({ kind: "row", key: id(pending[0]), height: Math.round(height) + gap, cells: pending.map((item) => ({ item, width: height * clamped(item) })) });
62
+ pending = [];
63
+ ratios = 0;
64
+ row += 1;
65
+ };
66
+
67
+ for (const item of items) {
68
+ const next = group?.(item);
69
+ if (next !== undefined && next.key !== section) {
70
+ flush(false);
71
+ section = next.key;
72
+ lines.push({ kind: "heading", key: next.key, label: next.label, height: headingHeight });
73
+ }
74
+ pending.push(item);
75
+ ratios += clamped(item);
76
+ if (ratios * target() + gap * (pending.length - 1) >= width) flush(true);
77
+ }
78
+ flush(false);
79
+ return lines;
80
+ }
81
+
82
+ /* Pinterest columns: each picture at its own shape, dealt across the columns
83
+ in order like cards, so the board reads left to right and a last row that
84
+ is not full starts at the left. (Putting each in the shortest column packs
85
+ tighter but drops the last one wherever there is room.) Fewer pictures than
86
+ columns are centred. `below` is room under each picture for a caption. */
87
+ export function masonry<T>(
88
+ items: T[],
89
+ width: number,
90
+ options: { aspect: (item: T) => number; columns: number; gap?: number; below?: number },
91
+ ): { places: { item: T; x: number; y: number; width: number; height: number }[]; height: number } {
92
+ const { aspect, columns, gap = 22, below = 0 } = options;
93
+ if (width <= 0 || columns <= 0) return { places: [], height: 0 };
94
+ const column = (width - gap * (columns - 1)) / columns;
95
+ const heights = Array.from({ length: columns }, () => 0);
96
+ const used = Math.min(columns, items.length);
97
+ const inset = (width - (used * column + gap * (used - 1))) / 2;
98
+ const places = items.map((item, index) => {
99
+ const at = index % columns;
100
+ const height = column / aspect(item);
101
+ const y = heights[at];
102
+ heights[at] = y + height + below + gap;
103
+ return { item, x: inset + at * (column + gap), y, width: column, height };
104
+ });
105
+ return { places, height: Math.max(0, Math.max(...heights) - gap) };
106
+ }