@link-foundation/ui-components 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 (402) hide show
  1. package/CONFIGURATIONS.md +74 -0
  2. package/ICONS.md +76 -0
  3. package/LICENSE +24 -0
  4. package/README.md +1499 -2
  5. package/bin/lino-network.mjs +71 -0
  6. package/dist/icons/ATTRIBUTION.md +602 -0
  7. package/dist/icons/NOTICE.lino +1261 -0
  8. package/dist/icons/brand.js +31 -0
  9. package/dist/icons/brand.svg +3 -0
  10. package/dist/icons/licenses/brand/DISCLAIMER.md +51 -0
  11. package/dist/icons/licenses/brand/LICENSE.md +30 -0
  12. package/dist/icons/licenses/lucide/LICENSE +43 -0
  13. package/dist/icons/licenses/material-symbols/LICENSE +202 -0
  14. package/dist/icons/licenses/material-symbols/NOTICE +12 -0
  15. package/dist/icons/licenses/tabler/LICENSE +21 -0
  16. package/dist/icons/lucide.js +681 -0
  17. package/dist/icons/lucide.svg +34 -0
  18. package/dist/icons/material-symbols.js +681 -0
  19. package/dist/icons/material-symbols.svg +34 -0
  20. package/dist/icons/own.js +521 -0
  21. package/dist/icons/own.svg +34 -0
  22. package/dist/icons/tabler.js +681 -0
  23. package/dist/icons/tabler.svg +34 -0
  24. package/dist/network-layout-NOTICE.md +6 -0
  25. package/dist/network-layout-d3-dispatch-LICENSE.txt +13 -0
  26. package/dist/network-layout-d3-force-LICENSE.txt +13 -0
  27. package/dist/network-layout-d3-quadtree-LICENSE.txt +13 -0
  28. package/dist/network-layout-d3-timer-LICENSE.txt +13 -0
  29. package/dist/network-layout-dagre-LICENSE.txt +19 -0
  30. package/dist/network-layout.worker.mjs +2 -0
  31. package/package.json +246 -4
  32. package/src/a11y/announce.js +88 -0
  33. package/src/a11y/bind-machine.js +181 -0
  34. package/src/a11y/focus.js +169 -0
  35. package/src/a11y/index.d.ts +89 -0
  36. package/src/a11y/index.js +19 -0
  37. package/src/a11y/keys.js +182 -0
  38. package/src/a11y/roving.js +167 -0
  39. package/src/a11y/typeahead.js +82 -0
  40. package/src/behavior/actions.js +235 -0
  41. package/src/behavior/diagnostics.js +73 -0
  42. package/src/behavior/dom.d.ts +38 -0
  43. package/src/behavior/dom.js +420 -0
  44. package/src/behavior/index.d.ts +211 -0
  45. package/src/behavior/index.js +29 -0
  46. package/src/behavior/interpret.js +454 -0
  47. package/src/behavior/machines/disclosure.lino +46 -0
  48. package/src/behavior/machines/index.js +91 -0
  49. package/src/behavior/machines/toggle.lino +25 -0
  50. package/src/behavior/parse-machine.js +367 -0
  51. package/src/charts/README.md +78 -0
  52. package/src/charts/activity-layout.js +350 -0
  53. package/src/charts/activity-model.js +130 -0
  54. package/src/charts/activity-validation.js +31 -0
  55. package/src/charts/chart-model.js +667 -0
  56. package/src/charts/chart.js +518 -0
  57. package/src/charts/echarts-adapter.d.ts +23 -0
  58. package/src/charts/echarts-adapter.js +240 -0
  59. package/src/charts/element.js +330 -0
  60. package/src/charts/errors.js +62 -0
  61. package/src/charts/gauge.js +27 -0
  62. package/src/charts/index.d.ts +344 -0
  63. package/src/charts/index.js +59 -0
  64. package/src/charts/layout.js +542 -0
  65. package/src/charts/lino.js +158 -0
  66. package/src/charts/mark-options.js +78 -0
  67. package/src/charts/read.js +101 -0
  68. package/src/charts/renderer-features.js +8 -0
  69. package/src/charts/scales.js +223 -0
  70. package/src/charts/small-model.js +223 -0
  71. package/src/charts/small-svg.js +269 -0
  72. package/src/charts/sparkline.js +27 -0
  73. package/src/charts/stat-tile.js +37 -0
  74. package/src/charts/summary.js +193 -0
  75. package/src/charts/svg-renderer.js +226 -0
  76. package/src/charts/svg.js +69 -0
  77. package/src/charts/tag-cloud.js +129 -0
  78. package/src/charts/tokens.js +171 -0
  79. package/src/charts/tooltip.js +47 -0
  80. package/src/charts/values.js +146 -0
  81. package/src/chat/README.md +142 -0
  82. package/src/chat/actions.js +75 -0
  83. package/src/chat/chat-contract.js +95 -0
  84. package/src/chat/chat-list.js +42 -0
  85. package/src/chat/chat-model.js +125 -0
  86. package/src/chat/chat.css +228 -0
  87. package/src/chat/composer.js +131 -0
  88. package/src/chat/day-separator.js +12 -0
  89. package/src/chat/dom.js +33 -0
  90. package/src/chat/follow.js +49 -0
  91. package/src/chat/index.d.ts +142 -0
  92. package/src/chat/index.js +13 -0
  93. package/src/chat/message-bubble.js +191 -0
  94. package/src/chat/message-stream.js +331 -0
  95. package/src/chat/reasoning-trace.js +70 -0
  96. package/src/chat/selection.js +39 -0
  97. package/src/chat/skins/bubbles.lino +2 -0
  98. package/src/chat/skins/cards.lino +4 -0
  99. package/src/chat/skins/compact.lino +4 -0
  100. package/src/chat/skins/signal.lino +25 -0
  101. package/src/chat/skins/t3.lino +25 -0
  102. package/src/chat/skins/telegram.lino +31 -0
  103. package/src/chat/skins/whatsapp.lino +25 -0
  104. package/src/chat/stream-keys.js +57 -0
  105. package/src/chat/stream-window.js +159 -0
  106. package/src/chat/vocabulary.js +19 -0
  107. package/src/controls/accordion.js +160 -0
  108. package/src/controls/avatar.js +80 -0
  109. package/src/controls/badge.js +48 -0
  110. package/src/controls/button-template.js +32 -0
  111. package/src/controls/button.js +97 -0
  112. package/src/controls/catalog.lino +84 -0
  113. package/src/controls/checkbox.js +112 -0
  114. package/src/controls/chip.js +140 -0
  115. package/src/controls/choices.js +45 -0
  116. package/src/controls/color-field.js +217 -0
  117. package/src/controls/color-popover.js +135 -0
  118. package/src/controls/color-rgb.js +80 -0
  119. package/src/controls/color.js +67 -0
  120. package/src/controls/combobox.js +192 -0
  121. package/src/controls/dom.js +261 -0
  122. package/src/controls/icon-button.js +33 -0
  123. package/src/controls/index.d.ts +163 -0
  124. package/src/controls/index.js +182 -0
  125. package/src/controls/kinds-text.js +358 -0
  126. package/src/controls/kinds.lino +350 -0
  127. package/src/controls/label.js +52 -0
  128. package/src/controls/link.js +101 -0
  129. package/src/controls/listbox.js +144 -0
  130. package/src/controls/machines/button.lino +20 -0
  131. package/src/controls/machines/checkbox.lino +17 -0
  132. package/src/controls/machines/chip.lino +20 -0
  133. package/src/controls/machines/color-field.lino +23 -0
  134. package/src/controls/machines/combobox.lino +82 -0
  135. package/src/controls/machines/icon-button.lino +20 -0
  136. package/src/controls/machines/index.js +586 -0
  137. package/src/controls/machines/number-field.lino +71 -0
  138. package/src/controls/machines/radio-group.lino +17 -0
  139. package/src/controls/machines/select.lino +122 -0
  140. package/src/controls/machines/slider.lino +99 -0
  141. package/src/controls/machines/switch.lino +19 -0
  142. package/src/controls/machines/text-area.lino +26 -0
  143. package/src/controls/machines/text-field.lino +26 -0
  144. package/src/controls/number-field.js +239 -0
  145. package/src/controls/numeric-keys.js +125 -0
  146. package/src/controls/option.js +54 -0
  147. package/src/controls/progress.js +76 -0
  148. package/src/controls/radio-group.js +234 -0
  149. package/src/controls/select.js +229 -0
  150. package/src/controls/slider.js +234 -0
  151. package/src/controls/stories.js +172 -0
  152. package/src/controls/styles.css +1060 -0
  153. package/src/controls/switch.js +95 -0
  154. package/src/controls/text-area.js +39 -0
  155. package/src/controls/text-field.js +131 -0
  156. package/src/controls/vocabulary.js +164 -0
  157. package/src/data/data.css +95 -0
  158. package/src/data/index.d.ts +83 -0
  159. package/src/data/index.js +10 -0
  160. package/src/data/keyboard.js +189 -0
  161. package/src/data/list.js +215 -0
  162. package/src/data/render-data.js +116 -0
  163. package/src/data/selection-dom.js +39 -0
  164. package/src/data/selection-model.js +43 -0
  165. package/src/data/sort-model.js +42 -0
  166. package/src/data/source.js +45 -0
  167. package/src/data/table.js +160 -0
  168. package/src/data/tree-model.js +41 -0
  169. package/src/data/tree.js +55 -0
  170. package/src/data/viewport.js +216 -0
  171. package/src/data/virtual-range.js +92 -0
  172. package/src/data/vocabulary.js +67 -0
  173. package/src/diagrams/diagram-model.js +511 -0
  174. package/src/diagrams/diagram.js +204 -0
  175. package/src/diagrams/index.d.ts +225 -0
  176. package/src/diagrams/index.js +48 -0
  177. package/src/diagrams/layout-dagre.js +164 -0
  178. package/src/diagrams/layout-plugins.js +131 -0
  179. package/src/diagrams/mermaid-export.js +232 -0
  180. package/src/diagrams/mermaid-view.js +229 -0
  181. package/src/diagrams/svg-diagram.js +215 -0
  182. package/src/editor/code-editor.js +206 -0
  183. package/src/editor/documents-model.js +170 -0
  184. package/src/editor/documents-persistence.js +85 -0
  185. package/src/editor/editor-engine.js +22 -0
  186. package/src/editor/editor-ui.js +203 -0
  187. package/src/editor/editor.css +106 -0
  188. package/src/editor/engines/codemirror.js +227 -0
  189. package/src/editor/engines/monaco.js +219 -0
  190. package/src/editor/index.d.ts +111 -0
  191. package/src/editor/index.js +14 -0
  192. package/src/editor/lino-highlight.js +106 -0
  193. package/src/editor/lino-structure.js +85 -0
  194. package/src/editor/validate-lino.js +43 -0
  195. package/src/embed/README.md +161 -0
  196. package/src/embed/controller.js +223 -0
  197. package/src/embed/element.js +141 -0
  198. package/src/embed/index.d.ts +75 -0
  199. package/src/embed/index.js +9 -0
  200. package/src/embed/providers/telegram.d.ts +3 -0
  201. package/src/embed/providers/telegram.js +38 -0
  202. package/src/embed/providers/vk.d.ts +3 -0
  203. package/src/embed/providers/vk.js +42 -0
  204. package/src/embed/providers/x.d.ts +3 -0
  205. package/src/embed/providers/x.js +31 -0
  206. package/src/embed/registry.js +35 -0
  207. package/src/embed/script.js +73 -0
  208. package/src/embed/view.js +122 -0
  209. package/src/icons/adapters/lucide.d.ts +15 -0
  210. package/src/icons/adapters/lucide.js +142 -0
  211. package/src/icons/adapters/material-symbols.d.ts +13 -0
  212. package/src/icons/adapters/material-symbols.js +113 -0
  213. package/src/icons/adapters/shared.d.ts +38 -0
  214. package/src/icons/adapters/shared.js +157 -0
  215. package/src/icons/adapters/simple-icons.d.ts +9 -0
  216. package/src/icons/adapters/simple-icons.js +38 -0
  217. package/src/icons/adapters/tabler.d.ts +9 -0
  218. package/src/icons/adapters/tabler.js +86 -0
  219. package/src/icons/icon-registry.js +215 -0
  220. package/src/icons/icon.js +356 -0
  221. package/src/icons/index.d.ts +123 -0
  222. package/src/icons/index.js +30 -0
  223. package/src/icons/names.js +51 -0
  224. package/src/icons/own-icons.js +521 -0
  225. package/src/icons/presentation.js +55 -0
  226. package/src/icons/svg.js +136 -0
  227. package/src/index.d.ts +14 -0
  228. package/src/index.js +40 -0
  229. package/src/layout/README.md +102 -0
  230. package/src/layout/app-shell.js +150 -0
  231. package/src/layout/catalog.lino +27 -0
  232. package/src/layout/dock-model.js +576 -0
  233. package/src/layout/dock-view.js +312 -0
  234. package/src/layout/dock.js +352 -0
  235. package/src/layout/dom.js +312 -0
  236. package/src/layout/grid.js +192 -0
  237. package/src/layout/index.d.ts +267 -0
  238. package/src/layout/index.js +60 -0
  239. package/src/layout/layer-manager.js +120 -0
  240. package/src/layout/layout.css +760 -0
  241. package/src/layout/menu.js +141 -0
  242. package/src/layout/nav-strip.js +174 -0
  243. package/src/layout/page-links.js +166 -0
  244. package/src/layout/panel.js +26 -0
  245. package/src/layout/pref-switches.js +209 -0
  246. package/src/layout/split-model.js +270 -0
  247. package/src/layout/split.js +430 -0
  248. package/src/layout/stack.js +86 -0
  249. package/src/layout/surfaces.js +119 -0
  250. package/src/layout/tab-group.js +151 -0
  251. package/src/layout/tab-strip.js +277 -0
  252. package/src/layout/vocabulary.js +229 -0
  253. package/src/layout/window-frame.js +222 -0
  254. package/src/layout/window.js +146 -0
  255. package/src/links-view/animator.js +144 -0
  256. package/src/links-view/configuration-classes.js +150 -0
  257. package/src/links-view/configuration-model.js +191 -0
  258. package/src/links-view/configuration-notations.js +230 -0
  259. package/src/links-view/configurations.d.ts +73 -0
  260. package/src/links-view/configurations.js +33 -0
  261. package/src/links-view/create-layout-runner.js +79 -0
  262. package/src/links-view/doublet-curves.js +239 -0
  263. package/src/links-view/doublet-layout.js +148 -0
  264. package/src/links-view/doublet-markers.js +109 -0
  265. package/src/links-view/doublet-options.js +369 -0
  266. package/src/links-view/doublet-svg.js +156 -0
  267. package/src/links-view/doublet.js +230 -0
  268. package/src/links-view/export.js +146 -0
  269. package/src/links-view/geometry.js +409 -0
  270. package/src/links-view/index.d.ts +1029 -0
  271. package/src/links-view/index.js +140 -0
  272. package/src/links-view/labels.js +28 -0
  273. package/src/links-view/layout-worker-entry.mjs +2 -0
  274. package/src/links-view/layout-worker.js +28 -0
  275. package/src/links-view/layouts/circle.js +7 -0
  276. package/src/links-view/layouts/force.js +76 -0
  277. package/src/links-view/layouts/grid.js +7 -0
  278. package/src/links-view/layouts/hex.js +8 -0
  279. package/src/links-view/layouts/index.js +15 -0
  280. package/src/links-view/layouts/layered.js +29 -0
  281. package/src/links-view/layouts/regular.js +27 -0
  282. package/src/links-view/layouts/spiral.js +10 -0
  283. package/src/links-view/link-view.js +494 -0
  284. package/src/links-view/links-gallery.js +306 -0
  285. package/src/links-view/links-network.js +496 -0
  286. package/src/links-view/lino-network-source.js +122 -0
  287. package/src/links-view/lino-network.js +233 -0
  288. package/src/links-view/lino-to-network.js +252 -0
  289. package/src/links-view/markers.js +192 -0
  290. package/src/links-view/model.js +233 -0
  291. package/src/links-view/network-editor/dom.js +36 -0
  292. package/src/links-view/network-editor/editor.js +202 -0
  293. package/src/links-view/network-editor/model.js +163 -0
  294. package/src/links-view/network-editor/options.js +265 -0
  295. package/src/links-view/network-editor/panels.js +423 -0
  296. package/src/links-view/network-editor/presets.js +76 -0
  297. package/src/links-view/network-editor/provider.js +93 -0
  298. package/src/links-view/network-editor/remap.js +45 -0
  299. package/src/links-view/network-editor/schema.js +224 -0
  300. package/src/links-view/network-editor/share.js +232 -0
  301. package/src/links-view/network-editor/styles.js +19 -0
  302. package/src/links-view/network-editor/url-sync.js +131 -0
  303. package/src/links-view/network-interaction.js +228 -0
  304. package/src/links-view/network-model.js +85 -0
  305. package/src/links-view/network-renderer.js +259 -0
  306. package/src/links-view/network-scene.js +245 -0
  307. package/src/links-view/network-source.js +82 -0
  308. package/src/links-view/network-vocabulary.js +18 -0
  309. package/src/links-view/presentation.js +199 -0
  310. package/src/links-view/render-canvas.js +102 -0
  311. package/src/links-view/render-svg.js +118 -0
  312. package/src/links-view/scene.js +233 -0
  313. package/src/links-view/solver-job.js +106 -0
  314. package/src/links-view/tokens.js +114 -0
  315. package/src/links-view/zoom-pan.js +203 -0
  316. package/src/overlays/catalog.lino +89 -0
  317. package/src/overlays/context-menu.js +110 -0
  318. package/src/overlays/dialog.js +117 -0
  319. package/src/overlays/dom.js +168 -0
  320. package/src/overlays/drawer.js +8 -0
  321. package/src/overlays/index.d.ts +80 -0
  322. package/src/overlays/index.js +25 -0
  323. package/src/overlays/kinds.lino +137 -0
  324. package/src/overlays/list-popover.js +129 -0
  325. package/src/overlays/machines/index.js +71 -0
  326. package/src/overlays/machines/surface.lino +11 -0
  327. package/src/overlays/machines/tabs.lino +7 -0
  328. package/src/overlays/machines/toast.lino +16 -0
  329. package/src/overlays/machines/tooltip.lino +29 -0
  330. package/src/overlays/menu.js +240 -0
  331. package/src/overlays/popover.js +194 -0
  332. package/src/overlays/position.js +84 -0
  333. package/src/overlays/radial-geometry.js +38 -0
  334. package/src/overlays/radial-menu.js +50 -0
  335. package/src/overlays/styles.css +155 -0
  336. package/src/overlays/tabs.js +125 -0
  337. package/src/overlays/toast-queue.js +105 -0
  338. package/src/overlays/toast.js +163 -0
  339. package/src/overlays/tooltip.js +110 -0
  340. package/src/overlays/vocabulary.js +3 -0
  341. package/src/settings/conditions.js +44 -0
  342. package/src/settings/control-for.js +62 -0
  343. package/src/settings/field-control.js +201 -0
  344. package/src/settings/index.d.ts +201 -0
  345. package/src/settings/index.js +3 -0
  346. package/src/settings/messages.js +112 -0
  347. package/src/settings/miniature.js +26 -0
  348. package/src/settings/panel-actions.js +234 -0
  349. package/src/settings/panel-model.js +49 -0
  350. package/src/settings/panel-refresh.js +56 -0
  351. package/src/settings/panel-rows.js +200 -0
  352. package/src/settings/preset-cards.js +43 -0
  353. package/src/settings/saved-looks.js +227 -0
  354. package/src/settings/section-layout.js +95 -0
  355. package/src/settings/section-tabs.js +175 -0
  356. package/src/settings/settings-panel.js +192 -0
  357. package/src/settings/settings.css +178 -0
  358. package/stories/accordion.lino +14 -0
  359. package/stories/alert-dialog.lino +4 -0
  360. package/stories/app-bar.lino +10 -0
  361. package/stories/avatar.lino +7 -0
  362. package/stories/badge.lino +12 -0
  363. package/stories/button.lino +17 -0
  364. package/stories/card.lino +10 -0
  365. package/stories/checkbox.lino +14 -0
  366. package/stories/chip.lino +19 -0
  367. package/stories/color-field.lino +9 -0
  368. package/stories/combobox.lino +10 -0
  369. package/stories/container.lino +11 -0
  370. package/stories/context-menu.lino +15 -0
  371. package/stories/dialog.lino +16 -0
  372. package/stories/drawer.lino +12 -0
  373. package/stories/icon-button.lino +13 -0
  374. package/stories/label.lino +7 -0
  375. package/stories/link-cards.lino +9 -0
  376. package/stories/link.lino +9 -0
  377. package/stories/menu-item.lino +6 -0
  378. package/stories/menu-separator.lino +7 -0
  379. package/stories/menu.lino +30 -0
  380. package/stories/nav-strip.lino +11 -0
  381. package/stories/number-field.lino +16 -0
  382. package/stories/option.lino +10 -0
  383. package/stories/paper.lino +11 -0
  384. package/stories/popover.lino +4 -0
  385. package/stories/pref-switches.lino +4 -0
  386. package/stories/progress.lino +14 -0
  387. package/stories/radial-menu.lino +19 -0
  388. package/stories/radio-group.lino +22 -0
  389. package/stories/radio.lino +10 -0
  390. package/stories/section-nav.lino +9 -0
  391. package/stories/select.lino +16 -0
  392. package/stories/slider.lino +14 -0
  393. package/stories/switch.lino +10 -0
  394. package/stories/tab-panel.lino +7 -0
  395. package/stories/tab.lino +7 -0
  396. package/stories/tabs.lino +25 -0
  397. package/stories/text-area.lino +12 -0
  398. package/stories/text-field.lino +23 -0
  399. package/stories/titled-section.lino +7 -0
  400. package/stories/toast.lino +3 -0
  401. package/stories/toaster.lino +2 -0
  402. package/stories/tooltip.lino +3 -0
@@ -0,0 +1,222 @@
1
+ /**
2
+ * The frame of a floating surface, shared by `window` and the floating
3
+ * panels of a dock: a header that is the drag handle, a body, a resize
4
+ * grip, and a layer of the layer manager.
5
+ *
6
+ * - Dragging the header (pointer events with `setPointerCapture`) moves
7
+ * the frame; dragging the grip resizes it.
8
+ * - Alt+Arrow moves it by 16px and Alt+Shift+Arrow resizes it, from
9
+ * anywhere inside the frame.
10
+ * - Escape closes it when it is closable.
11
+ * - The frame always stays inside the viewport: on drag, on resize, and
12
+ * when the browser window is resized.
13
+ * - A pointer down or focus inside raises it above the other windows.
14
+ */
15
+
16
+ import { bindKeys } from '../a11y/keys.js';
17
+ import { make, uid } from './dom.js';
18
+
19
+ const STEP = 16;
20
+ const MIN_WIDTH = 160;
21
+ const MIN_HEIGHT = 96;
22
+
23
+ const ARROWS = {
24
+ ArrowLeft: [-1, 0],
25
+ ArrowRight: [1, 0],
26
+ ArrowUp: [0, -1],
27
+ ArrowDown: [0, 1],
28
+ };
29
+
30
+ /**
31
+ * A rectangle moved and sized so it lies inside the viewport: never wider
32
+ * or taller than the viewport, and with no edge outside it.
33
+ * @param {{x: number, y: number, width: number, height: number}} rect
34
+ * @param {{width: number, height: number}} viewport
35
+ */
36
+ export function clampRect(rect, viewport) {
37
+ const width = Math.max(
38
+ Math.min(MIN_WIDTH, viewport.width),
39
+ Math.min(rect.width, viewport.width)
40
+ );
41
+ const height = Math.max(
42
+ Math.min(MIN_HEIGHT, viewport.height),
43
+ Math.min(rect.height, viewport.height)
44
+ );
45
+ const x = Math.min(Math.max(0, rect.x), Math.max(0, viewport.width - width));
46
+ const y = Math.min(
47
+ Math.max(0, rect.y),
48
+ Math.max(0, viewport.height - height)
49
+ );
50
+ return {
51
+ x: Math.round(x),
52
+ y: Math.round(y),
53
+ width: Math.round(width),
54
+ height: Math.round(height),
55
+ };
56
+ }
57
+
58
+ /** The size of the viewport (without scroll bars). */
59
+ export function viewportOf(document) {
60
+ const win = document.defaultView;
61
+ const root = document.documentElement;
62
+ return {
63
+ width: root?.clientWidth || win?.innerWidth || 0,
64
+ height: root?.clientHeight || win?.innerHeight || 0,
65
+ };
66
+ }
67
+
68
+ /**
69
+ * Build a frame in `root`.
70
+ * @param {HTMLElement} root
71
+ * @param {{title: string, closable?: boolean, layers: object,
72
+ * onClose?: () => void, onChange?: (rect: object) => void,
73
+ * tools?: HTMLElement[]}} spec
74
+ */
75
+ export function createWindowFrame(root, spec) {
76
+ const document = root.ownerDocument;
77
+ const win = document.defaultView;
78
+ const layerId = uid('ui-window');
79
+ const titleId = uid('ui-window-title');
80
+ root.classList.add('ui-window-frame');
81
+ root.setAttribute('role', 'dialog');
82
+ root.setAttribute('aria-labelledby', titleId);
83
+ root.tabIndex = -1;
84
+
85
+ const header = make(document, 'div', { class: 'ui-window-header' });
86
+ const title = make(document, 'span', {
87
+ id: titleId,
88
+ class: 'ui-window-title',
89
+ });
90
+ const close = make(
91
+ document,
92
+ 'button',
93
+ { type: 'button', class: 'ui-window-close' },
94
+ '×'
95
+ );
96
+ close.addEventListener('click', () => spec.onClose?.());
97
+ header.append(title, ...(spec.tools ?? []), close);
98
+ const body = make(document, 'div', { class: 'ui-window-body' });
99
+ const grip = make(document, 'div', {
100
+ class: 'ui-window-grip',
101
+ 'aria-hidden': 'true',
102
+ });
103
+ root.prepend(header);
104
+ root.append(body, grip);
105
+
106
+ let rect = { x: 0, y: 0, width: 320, height: 240 };
107
+ let closable = spec.closable !== false;
108
+
109
+ const draw = () => {
110
+ root.style.setProperty('left', `${rect.x}px`);
111
+ root.style.setProperty('top', `${rect.y}px`);
112
+ root.style.setProperty('width', `${rect.width}px`);
113
+ root.style.setProperty('height', `${rect.height}px`);
114
+ };
115
+
116
+ const setRect = (next, notify = true) => {
117
+ const clamped = clampRect({ ...rect, ...next }, viewportOf(document));
118
+ const same = Object.keys(clamped).every((k) => clamped[k] === rect[k]);
119
+ rect = clamped;
120
+ draw();
121
+ if (notify && !same) {
122
+ spec.onChange?.({ ...rect });
123
+ }
124
+ };
125
+
126
+ const layers = spec.layers;
127
+ layers.register(layerId, 'window');
128
+ const paint = () =>
129
+ root.style.setProperty('z-index', String(layers.zIndexOf(layerId)));
130
+ const unsubscribe = layers.subscribe(paint);
131
+ paint();
132
+ const raise = () => layers.raise(layerId);
133
+ root.addEventListener('pointerdown', raise, true);
134
+ root.addEventListener('focusin', raise);
135
+
136
+ /** A pointer drag of `handle` that changes the rect by `change`. */
137
+ const dragWith = (handle, change) => {
138
+ let start = null;
139
+ handle.addEventListener('pointerdown', (event) => {
140
+ if (event.button !== 0 || event.target.closest?.('button')) {
141
+ return;
142
+ }
143
+ handle.setPointerCapture?.(event.pointerId);
144
+ start = { id: event.pointerId, x: event.clientX, y: event.clientY, rect };
145
+ event.preventDefault();
146
+ });
147
+ handle.addEventListener('pointermove', (event) => {
148
+ if (start?.id === event.pointerId) {
149
+ const dx = event.clientX - start.x;
150
+ const dy = event.clientY - start.y;
151
+ setRect(change(start.rect, dx, dy), false);
152
+ }
153
+ });
154
+ const end = (event) => {
155
+ if (start?.id === event.pointerId) {
156
+ handle.releasePointerCapture?.(event.pointerId);
157
+ start = null;
158
+ spec.onChange?.({ ...rect });
159
+ }
160
+ };
161
+ handle.addEventListener('pointerup', end);
162
+ handle.addEventListener('pointercancel', end);
163
+ };
164
+ dragWith(header, (r, dx, dy) => ({ x: r.x + dx, y: r.y + dy }));
165
+ dragWith(grip, (r, dx, dy) => ({
166
+ width: r.width + dx,
167
+ height: r.height + dy,
168
+ }));
169
+
170
+ // The keys stay in this frame: a window inside a dock does not move it.
171
+ const own = (act) => (event) => {
172
+ event.stopPropagation();
173
+ act();
174
+ };
175
+ const keys = {
176
+ Escape: (event) => closable && own(() => spec.onClose?.())(event),
177
+ };
178
+ for (const [key, [dx, dy]] of Object.entries(ARROWS)) {
179
+ keys[`Alt+${key}`] = own(() =>
180
+ setRect({ x: rect.x + dx * STEP, y: rect.y + dy * STEP })
181
+ );
182
+ keys[`Alt+Shift+${key}`] = own(() =>
183
+ setRect({
184
+ width: rect.width + dx * STEP,
185
+ height: rect.height + dy * STEP,
186
+ })
187
+ );
188
+ }
189
+ const unbindKeys = bindKeys(root, keys);
190
+
191
+ const onResize = () => setRect({}, true);
192
+ win.addEventListener('resize', onResize);
193
+
194
+ const frame = {
195
+ root,
196
+ header,
197
+ body,
198
+ get rect() {
199
+ return { ...rect };
200
+ },
201
+ setRect,
202
+ raise,
203
+ layerId,
204
+ setTitle(text) {
205
+ title.textContent = text;
206
+ close.setAttribute('aria-label', `Close ${text}`);
207
+ },
208
+ setClosable(on) {
209
+ closable = on;
210
+ close.hidden = !on;
211
+ },
212
+ disconnect() {
213
+ win.removeEventListener('resize', onResize);
214
+ unbindKeys();
215
+ unsubscribe();
216
+ layers.unregister(layerId);
217
+ },
218
+ };
219
+ frame.setTitle(spec.title);
220
+ frame.setClosable(closable);
221
+ return frame;
222
+ }
@@ -0,0 +1,146 @@
1
+ /**
2
+ * `window`: a floating, movable and resizable surface (a non-modal
3
+ * `role="dialog"`).
4
+ *
5
+ * (inspector: window (title Inspector) (open false)
6
+ * (x 120) (y 96) (width 360) (height 280) ...)
7
+ *
8
+ * The header is the drag handle; Alt+Arrow moves the window and
9
+ * Alt+Shift+Arrow resizes it (see `window-frame.js`). The window is kept
10
+ * inside the viewport on drag and when the browser window is resized, and
11
+ * it is raised above the other windows by the layer manager of the render
12
+ * (`options.layout.layers`, else one shared by the document).
13
+ *
14
+ * `(open true)` shows the window and focuses it; the element that had the
15
+ * focus is its opener. The close button and Escape hide it, give the focus
16
+ * back to the opener and send `window-close` (`detail.id`); the
17
+ * application writes `(open false)` to its store then. Every move and
18
+ * resize sends `window-change` with `detail.rect`.
19
+ */
20
+
21
+ import { defineKind } from '@link-foundation/ui-dom';
22
+ import {
23
+ atomOf,
24
+ controllerOf,
25
+ dropController,
26
+ emit,
27
+ findController,
28
+ isLive,
29
+ layersOf,
30
+ make,
31
+ numberOf,
32
+ } from './dom.js';
33
+ import { createWindowFrame } from './window-frame.js';
34
+
35
+ const DEFAULTS = { x: 48, y: 48, width: 360, height: 240 };
36
+
37
+ const bodies = new WeakMap();
38
+
39
+ const FOCUSABLE =
40
+ 'button:not([disabled]), [href], input:not([disabled]), select, textarea, [tabindex]:not([tabindex="-1"])';
41
+
42
+ function createWindow(node, ctx) {
43
+ const state = { id: null, open: false, opener: null, started: false };
44
+
45
+ const hide = () => {
46
+ if (!state.open) {
47
+ return;
48
+ }
49
+ state.open = false;
50
+ node.hidden = true;
51
+ const opener = state.opener;
52
+ state.opener = null;
53
+ if (opener?.isConnected) {
54
+ opener.focus();
55
+ }
56
+ emit(node, 'window-close', { id: state.id });
57
+ };
58
+
59
+ const frame = createWindowFrame(node, {
60
+ title: '',
61
+ layers: layersOf(ctx),
62
+ onClose: hide,
63
+ onChange: (rect) => emit(node, 'window-change', { id: state.id, rect }),
64
+ });
65
+ bodies.set(node, frame.body);
66
+
67
+ const show = (focus) => {
68
+ if (state.open) {
69
+ return;
70
+ }
71
+ state.open = true;
72
+ const active = ctx.document.activeElement;
73
+ state.opener = active !== null && !node.contains(active) ? active : null;
74
+ node.hidden = false;
75
+ frame.raise();
76
+ frame.setRect({});
77
+ if (focus) {
78
+ const first = frame.body.querySelector(FOCUSABLE);
79
+ (first ?? node).focus();
80
+ }
81
+ };
82
+
83
+ return {
84
+ state,
85
+ frame,
86
+ show,
87
+ hide,
88
+ update(el, changed) {
89
+ state.id = el.id;
90
+ if (changed.includes('title') || !state.started) {
91
+ frame.setTitle(atomOf(el, 'title') ?? el.name ?? 'Window');
92
+ }
93
+ if (changed.includes('closable')) {
94
+ frame.setClosable(atomOf(el, 'closable') !== 'false');
95
+ }
96
+ const geometry = ['x', 'y', 'width', 'height'].filter((n) =>
97
+ changed.includes(n)
98
+ );
99
+ if (geometry.length > 0 || !state.started) {
100
+ const rect = {};
101
+ for (const name of ['x', 'y', 'width', 'height']) {
102
+ if (geometry.includes(name) || !state.started) {
103
+ rect[name] = numberOf(el, name, DEFAULTS[name]);
104
+ }
105
+ }
106
+ frame.setRect(rect, false);
107
+ }
108
+ if (changed.includes('open') || !state.started) {
109
+ if (atomOf(el, 'open') === 'false') {
110
+ node.hidden = true;
111
+ state.open = false;
112
+ } else {
113
+ // A window open from the start does not take the focus.
114
+ show(state.started);
115
+ }
116
+ }
117
+ state.started = true;
118
+ },
119
+ disconnect: () => frame.disconnect(),
120
+ };
121
+ }
122
+
123
+ defineKind('window', {
124
+ tag: 'ui-window',
125
+ childrenSlot: (node) => bodies.get(node) ?? node,
126
+ create(el, ctx) {
127
+ const node = ctx.document.createElement('ui-window');
128
+ if (!isLive(ctx)) {
129
+ const body = make(ctx.document, 'div', { class: 'ui-window-body' });
130
+ node.appendChild(body);
131
+ bodies.set(node, body);
132
+ }
133
+ return node;
134
+ },
135
+ update(node, el, changed, ctx) {
136
+ if (isLive(ctx)) {
137
+ controllerOf(node, () => createWindow(node, ctx)).update(el, changed);
138
+ } else if (atomOf(el, 'open') === 'false') {
139
+ node.setAttribute('hidden', '');
140
+ }
141
+ },
142
+ destroy: (node) => dropController(node),
143
+ });
144
+
145
+ /** The controller of a rendered window (for tests and tools). */
146
+ export const windowController = (node) => findController(node);
@@ -0,0 +1,144 @@
1
+ /**
2
+ * `createAnimator({now, raf})`: named animations on one frame loop. A frame
3
+ * is requested only while an animation is running, so a view where nothing
4
+ * moves costs no frames at all. With `prefers-reduced-motion: reduce` an
5
+ * animation is not played: its function is called once, at once, with
6
+ * `reduced: true`, and should jump to its final state.
7
+ *
8
+ * The clock and the frame scheduler are injected, so tests run without a
9
+ * browser and without real time.
10
+ */
11
+
12
+ /**
13
+ * @typedef {object} Frame
14
+ * @property {number} time `now()` of this frame
15
+ * @property {number} elapsed since the animation started
16
+ * @property {boolean} reduced reduced motion: the only call, go to the end
17
+ * @typedef {(frame: Frame) => boolean|void} Step return `true` to be
18
+ * called again on the next frame; anything else ends the animation
19
+ * @typedef {object} AnimatorOptions
20
+ * @property {() => number} [now]
21
+ * @property {(callback: (time: number) => void) => unknown} [raf]
22
+ * @property {(handle: unknown) => void} [caf]
23
+ * @property {() => boolean} [reducedMotion] whether motion is reduced
24
+ * (default: the `prefers-reduced-motion: reduce` media query)
25
+ * @property {(error: unknown, id: string) => void} [onError] a step that
26
+ * throws ends its animation and is reported here (default: rethrown in a
27
+ * task)
28
+ * @typedef {object} Animator
29
+ * @property {(id: string, step: Step) => void} start replaces a running
30
+ * animation of the same id
31
+ * @property {(id: string) => boolean} stop
32
+ * @property {(id?: string) => boolean} isRunning one animation, or any
33
+ * @property {() => void} destroy stops everything
34
+ */
35
+
36
+ const defaultNow = () => globalThis.performance?.now() ?? 0;
37
+
38
+ /** `requestAnimationFrame`, or a 16 ms timer where there is none. */
39
+ const defaultRaf = (callback) =>
40
+ typeof globalThis.requestAnimationFrame === 'function'
41
+ ? globalThis.requestAnimationFrame(callback)
42
+ : setTimeout(() => callback(defaultNow()), 16);
43
+
44
+ const defaultCaf = (handle) =>
45
+ typeof globalThis.cancelAnimationFrame === 'function'
46
+ ? globalThis.cancelAnimationFrame(handle)
47
+ : clearTimeout(handle);
48
+
49
+ /** Whether the user asked for reduced motion. */
50
+ export const prefersReducedMotion = (win = globalThis) =>
51
+ win?.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches === true;
52
+
53
+ /**
54
+ * Create an animator.
55
+ * @param {AnimatorOptions} [options]
56
+ * @returns {Animator}
57
+ */
58
+ export function createAnimator(options = {}) {
59
+ const now = options.now ?? defaultNow;
60
+ const raf = options.raf ?? defaultRaf;
61
+ const caf = options.caf ?? defaultCaf;
62
+ const reducedMotion = options.reducedMotion ?? (() => prefersReducedMotion());
63
+ const onError =
64
+ options.onError ??
65
+ ((error) =>
66
+ setTimeout(() => {
67
+ throw error;
68
+ }, 0));
69
+ /** @type {Map<string, {step: Step, started: number}>} */
70
+ const active = new Map();
71
+ let handle = null;
72
+ let destroyed = false;
73
+
74
+ const schedule = () => {
75
+ if (handle === null && active.size > 0 && !destroyed) {
76
+ handle = raf(tick);
77
+ }
78
+ };
79
+
80
+ function tick() {
81
+ handle = null;
82
+ const time = now();
83
+ for (const [id, entry] of [...active]) {
84
+ // An animation stopped by an earlier one in this frame is skipped.
85
+ if (active.get(id) !== entry) {
86
+ continue;
87
+ }
88
+ let more = false;
89
+ try {
90
+ more =
91
+ entry.step({
92
+ time,
93
+ elapsed: time - entry.started,
94
+ reduced: false,
95
+ }) === true;
96
+ } catch (error) {
97
+ // A failing animation ends; the others keep running.
98
+ onError(error, id);
99
+ }
100
+ if (!more && active.get(id) === entry) {
101
+ active.delete(id);
102
+ }
103
+ }
104
+ schedule();
105
+ }
106
+
107
+ const unschedule = () => {
108
+ if (handle !== null && active.size === 0) {
109
+ caf(handle);
110
+ handle = null;
111
+ }
112
+ };
113
+
114
+ return {
115
+ start(id, step) {
116
+ if (destroyed) {
117
+ return;
118
+ }
119
+ if (typeof step !== 'function') {
120
+ throw new TypeError('animator.start: the step must be a function');
121
+ }
122
+ if (reducedMotion()) {
123
+ active.delete(id);
124
+ unschedule();
125
+ const time = now();
126
+ step({ time, elapsed: 0, reduced: true });
127
+ return;
128
+ }
129
+ active.set(id, { step, started: now() });
130
+ schedule();
131
+ },
132
+ stop(id) {
133
+ const had = active.delete(id);
134
+ unschedule();
135
+ return had;
136
+ },
137
+ isRunning: (id) => (id === undefined ? active.size > 0 : active.has(id)),
138
+ destroy() {
139
+ active.clear();
140
+ unschedule();
141
+ destroyed = true;
142
+ },
143
+ };
144
+ }
@@ -0,0 +1,150 @@
1
+ import {
2
+ checked,
3
+ size,
4
+ count,
5
+ encode,
6
+ linksAt,
7
+ invalid,
8
+ } from './configuration-model.js';
9
+
10
+ function permutations(n) {
11
+ const result = [];
12
+ const visit = (prefix, rest) => {
13
+ if (!rest.length) {
14
+ result.push([0, ...prefix]);
15
+ }
16
+ for (let i = 0; i < rest.length; i++) {
17
+ visit(
18
+ [...prefix, rest[i]],
19
+ rest.filter((_, j) => i !== j)
20
+ );
21
+ }
22
+ };
23
+ visit(
24
+ [],
25
+ Array.from({ length: n }, (_, i) => i + 1)
26
+ );
27
+ return result;
28
+ }
29
+
30
+ function renamed(links, p) {
31
+ const result = Array(links.length);
32
+ links.forEach((pair, i) => {
33
+ result[p[i + 1] - 1] = pair.map((ref) => p[ref]);
34
+ });
35
+ return result;
36
+ }
37
+
38
+ /** permutation[i] is the new one-based name of old link i+1. */
39
+ export function relabel(links, permutation) {
40
+ checked(links);
41
+ if (
42
+ permutation.length !== links.length ||
43
+ new Set(permutation).size !== links.length ||
44
+ permutation.some((v) => !Number.isInteger(v) || v < 1 || v > links.length)
45
+ ) {
46
+ invalid('name');
47
+ }
48
+ return renamed(links, [0, ...permutation]);
49
+ }
50
+
51
+ function compare(a, b) {
52
+ for (let i = 0; i < a.length; i++) {
53
+ for (let j = 0; j < 2; j++) {
54
+ if (a[i][j] !== b[i][j]) {
55
+ return a[i][j] - b[i][j];
56
+ }
57
+ }
58
+ }
59
+ return 0;
60
+ }
61
+
62
+ /** Minimum in numeric counting order, practical through seven links. */
63
+ export function canonical(links) {
64
+ checked(links);
65
+ let best = links;
66
+ for (const p of permutations(links.length)) {
67
+ const candidate = renamed(links, p);
68
+ if (compare(candidate, best) < 0) {
69
+ best = candidate;
70
+ }
71
+ }
72
+ return encode(best);
73
+ }
74
+
75
+ /** Orbit sieve: only expand unvisited representatives, using one expansion per class. */
76
+ export function representatives(n, { real = false } = {}) {
77
+ size(n);
78
+ const total = count(n, { real });
79
+ // Arrays cannot represent more than 2^32-1 slots. Fail before rounding a count.
80
+ if (total > 0xffffffffn) {
81
+ throw new RangeError('Enumeration exceeds array capacity');
82
+ }
83
+ const visited = new Uint8Array(Number(total));
84
+ const ps = permutations(n);
85
+ const base = real ? n : n + 1;
86
+ const powers = Array.from(
87
+ { length: 2 * n },
88
+ (_, i) => base ** (2 * n - 1 - i)
89
+ );
90
+ const result = [];
91
+ for (let index = 0; index < visited.length; index++) {
92
+ if (visited[index]) {
93
+ continue;
94
+ }
95
+ const links = linksAt(n, index, { real });
96
+ result.push(links);
97
+ for (const p of ps) {
98
+ let renamedIndex = 0;
99
+ for (let i = 0; i < n; i++) {
100
+ const offset = 2 * (p[i + 1] - 1);
101
+ renamedIndex +=
102
+ (p[links[i][0]] - (real ? 1 : 0)) * powers[offset] +
103
+ (p[links[i][1]] - (real ? 1 : 0)) * powers[offset + 1];
104
+ }
105
+ visited[renamedIndex] = 1;
106
+ }
107
+ }
108
+ return result;
109
+ }
110
+
111
+ /** Burnside over integer partitions (cycle types), with exact integer weights. */
112
+ export function classCount(n, { real = false } = {}) {
113
+ size(n);
114
+ let factorial = 1n;
115
+ for (let i = 2; i <= n; i++) {
116
+ factorial *= BigInt(i);
117
+ }
118
+ let sum = 0n;
119
+ const visit = (remaining, min, cycles) => {
120
+ if (remaining) {
121
+ for (let length = min; length <= remaining; length++) {
122
+ visit(remaining - length, length, [...cycles, length]);
123
+ }
124
+ return;
125
+ }
126
+ const multiplicities = new Map();
127
+ for (const length of cycles) {
128
+ multiplicities.set(length, (multiplicities.get(length) ?? 0) + 1);
129
+ }
130
+ let z = 1n;
131
+ let fixed = 1n;
132
+ for (const [length, m] of multiplicities) {
133
+ z *= BigInt(length) ** BigInt(m);
134
+ for (let i = 2; i <= m; i++) {
135
+ z *= BigInt(i);
136
+ }
137
+ }
138
+ for (const length of cycles) {
139
+ const choices =
140
+ (real ? 0 : 1) +
141
+ cycles
142
+ .filter((other) => length % other === 0)
143
+ .reduce((a, b) => a + b, 0);
144
+ fixed *= BigInt(choices) ** 2n;
145
+ }
146
+ sum += (factorial / z) * fixed;
147
+ };
148
+ visit(n, 1, []);
149
+ return sum / factorial;
150
+ }