@link-foundation/ui-components 0.0.0-stage → 0.0.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,511 @@
1
+ /**
2
+ * The diagram dialect: a diagram written in Links Notation, read into a plain
3
+ * `DiagramModel` and written back. Pure: no DOM, no layout.
4
+ *
5
+ * ```lino
6
+ * (login-flow: diagram
7
+ * (kind flowchart)
8
+ * (direction down)
9
+ * (start: node (label 'Start'))
10
+ * (check: node (label 'Signed in?') (shape diamond))
11
+ * (edge (from start) (to check) (label 'yes')))
12
+ * ```
13
+ *
14
+ * Links come from the official `links-notation` parser; the multi-line form
15
+ * `(H (H a) (H b))` it returns is folded back into `(H a b)` here.
16
+ */
17
+
18
+ import { formatLinks, parseNested } from '@link-foundation/ui-core';
19
+ import { Link } from 'links-notation';
20
+
21
+ /** @typedef {'flowchart'|'sequence'|'state'|'class'} DiagramKind */
22
+ /** @typedef {'down'|'up'|'right'|'left'} DiagramDirection */
23
+
24
+ /**
25
+ * @typedef {object} DiagramNode
26
+ * @property {string} id
27
+ * @property {string} label the text shown (defaults to the id)
28
+ * @property {string} shape one of `NODE_SHAPES` (default `box`)
29
+ * @property {string|null} group the id of the group the node is in
30
+ *
31
+ * @typedef {object} DiagramEdge
32
+ * @property {string} id given, or `edge-N` (N counts the edges from 1)
33
+ * @property {string} from a node id
34
+ * @property {string} to a node id
35
+ * @property {string|null} label
36
+ *
37
+ * @typedef {object} DiagramGroup
38
+ * @property {string} id
39
+ * @property {string} label
40
+ * @property {string|null} group the id of the enclosing group
41
+ *
42
+ * @typedef {object} DiagramModel
43
+ * @property {string} id
44
+ * @property {DiagramKind} kind
45
+ * @property {DiagramDirection} direction
46
+ * @property {string|null} label an accessible title (optional)
47
+ * @property {'svg'|'mermaid'|null} renderer `null`: the default renderer
48
+ * @property {string|null} layout a registered layout name (`null`: dagre)
49
+ * @property {DiagramNode[]} nodes
50
+ * @property {DiagramEdge[]} edges
51
+ * @property {DiagramGroup[]} groups
52
+ */
53
+
54
+ export const DIAGRAM_KINDS = Object.freeze([
55
+ 'flowchart',
56
+ 'sequence',
57
+ 'state',
58
+ 'class',
59
+ ]);
60
+ export const DIAGRAM_DIRECTIONS = Object.freeze([
61
+ 'down',
62
+ 'up',
63
+ 'right',
64
+ 'left',
65
+ ]);
66
+ export const NODE_SHAPES = Object.freeze([
67
+ 'box',
68
+ 'round',
69
+ 'stadium',
70
+ 'circle',
71
+ 'diamond',
72
+ 'start',
73
+ 'end',
74
+ ]);
75
+ export const DIAGRAM_RENDERERS = Object.freeze(['svg', 'mermaid']);
76
+
77
+ const DEFAULTS = Object.freeze({
78
+ kind: 'flowchart',
79
+ direction: 'down',
80
+ shape: 'box',
81
+ });
82
+
83
+ /** A problem in a diagram, with the position of the offending link. */
84
+ export class DiagramError extends Error {
85
+ /**
86
+ * @param {string} code `no-diagram`, `unknown-node`, `unknown-group`,
87
+ * `duplicate-id`, `invalid-value`, `unknown-property`, `missing-value`
88
+ * @param {string} message
89
+ * @param {{diagram?: string, path: number[], link?: string}} at `path`:
90
+ * the index of the diagram link among the parsed links, then the index
91
+ * of the offending link inside the diagram (1 = the first child)
92
+ */
93
+ constructor(code, message, at) {
94
+ super(message);
95
+ this.name = 'DiagramError';
96
+ this.code = code;
97
+ this.at = at;
98
+ }
99
+ }
100
+
101
+ const keyOf = (link) => String(link);
102
+
103
+ /** Whether `link` is a reference (no values): `start`, `'Signed in?'`. */
104
+ const isRef = (link) => link.values.length === 0 && link.id !== null;
105
+
106
+ /**
107
+ * Fold the multi-line form `(H (H a) (H b))` into `(H a b)`, recursively.
108
+ * @param {Link} link
109
+ * @returns {Link}
110
+ */
111
+ export function foldLink(link) {
112
+ const [head, ...rest] = link.values;
113
+ const isWrapper =
114
+ link.id === null &&
115
+ rest.length > 0 &&
116
+ rest.every(
117
+ (value) =>
118
+ value.id === null &&
119
+ value.values.length >= 2 &&
120
+ keyOf(value.values[0]) === keyOf(head)
121
+ );
122
+ if (isWrapper) {
123
+ const payload = rest.flatMap((value) => value.values.slice(1));
124
+ return foldLink(new Link(head.id, [...head.values, ...payload]));
125
+ }
126
+ return new Link(link.id, link.values.map(foldLink));
127
+ }
128
+
129
+ /** The words of a sublink `(name word...)`, or null if it is not one. */
130
+ function sublink(link) {
131
+ if (link.id !== null || link.values.length === 0) {
132
+ return null;
133
+ }
134
+ const [name, ...values] = link.values;
135
+ if (!isRef(name) || !values.every(isRef)) {
136
+ return null;
137
+ }
138
+ return { name: name.id, words: values.map((value) => value.id) };
139
+ }
140
+
141
+ /** Reads the children of one diagram link and reports problems. */
142
+ class Reader {
143
+ constructor(diagramId, index) {
144
+ this.diagramId = diagramId;
145
+ this.index = index;
146
+ }
147
+
148
+ fail(code, message, child, link) {
149
+ const where =
150
+ child === undefined
151
+ ? `diagram "${this.diagramId}"`
152
+ : `link ${child} of diagram "${this.diagramId}"`;
153
+ throw new DiagramError(code, `${where}: ${message}`, {
154
+ diagram: this.diagramId,
155
+ path: child === undefined ? [this.index] : [this.index, child],
156
+ ...(link ? { link: String(link) } : {}),
157
+ });
158
+ }
159
+
160
+ /** The properties `(name value)` of an item link, checked by `allowed`. */
161
+ props(values, allowed, child, link) {
162
+ const props = {};
163
+ for (const value of values) {
164
+ const prop = sublink(value);
165
+ if (!prop || !allowed.includes(prop.name)) {
166
+ const what = prop ? `"${prop.name}"` : String(value);
167
+ this.fail(
168
+ 'unknown-property',
169
+ `unknown property ${what} (allowed: ${allowed.join(', ')})`,
170
+ child,
171
+ link
172
+ );
173
+ }
174
+ if (prop.words.length === 0) {
175
+ this.fail('missing-value', `"${prop.name}" needs a value`, child, link);
176
+ }
177
+ if (prop.name in props) {
178
+ this.fail('duplicate-id', `"${prop.name}" is set twice`, child, link);
179
+ }
180
+ props[prop.name] = prop.words.join(' ');
181
+ }
182
+ return props;
183
+ }
184
+
185
+ oneOf(value, allowed, what, child, link) {
186
+ if (!allowed.includes(value)) {
187
+ this.fail(
188
+ 'invalid-value',
189
+ `${what} "${value}" is not one of ${allowed.join(', ')}`,
190
+ child,
191
+ link
192
+ );
193
+ }
194
+ return value;
195
+ }
196
+ }
197
+
198
+ /** Split `(id: type props...)` / `(type props...)` into its parts. */
199
+ function itemOf(link) {
200
+ const [type, ...rest] = link.values;
201
+ if (!type || !isRef(type)) {
202
+ return null;
203
+ }
204
+ return { id: link.id, type: type.id, rest };
205
+ }
206
+
207
+ const DIAGRAM_PROPS = ['kind', 'direction', 'label', 'renderer', 'layout'];
208
+
209
+ /** Read the settings and items of a diagram link. */
210
+ function readItems(reader, items, model) {
211
+ const settings = [];
212
+ const pending = { nodes: [], edges: [], groups: [] };
213
+ items.forEach((link, i) => {
214
+ const child = i + 1;
215
+ const prop = sublink(link);
216
+ if (prop && DIAGRAM_PROPS.includes(prop.name)) {
217
+ settings.push([prop.name, reader.props([link], DIAGRAM_PROPS, child)]);
218
+ return;
219
+ }
220
+ const item = itemOf(link);
221
+ const bucket = { node: 'nodes', edge: 'edges', group: 'groups' }[
222
+ item?.type
223
+ ];
224
+ if (!bucket) {
225
+ reader.fail(
226
+ 'unknown-property',
227
+ `expected (kind …), (direction …), (label …), (renderer …), (layout …), a node, an edge or a group, got ${String(link)}`,
228
+ child,
229
+ link
230
+ );
231
+ }
232
+ if (bucket !== 'edges' && item.id === null) {
233
+ reader.fail('missing-value', `a ${item.type} needs an id`, child, link);
234
+ }
235
+ pending[bucket].push({ item, child, link });
236
+ });
237
+ for (const [, props] of settings) {
238
+ Object.assign(model, props);
239
+ }
240
+ return pending;
241
+ }
242
+
243
+ /** Check the settings of the model and fill in the defaults. */
244
+ function checkSettings(reader, model) {
245
+ model.kind = reader.oneOf(model.kind, DIAGRAM_KINDS, 'kind');
246
+ model.direction = reader.oneOf(
247
+ model.direction,
248
+ DIAGRAM_DIRECTIONS,
249
+ 'direction'
250
+ );
251
+ if (model.renderer !== null) {
252
+ reader.oneOf(model.renderer, DIAGRAM_RENDERERS, 'renderer');
253
+ }
254
+ }
255
+
256
+ function claimId(reader, ids, id, child, link) {
257
+ if (ids.has(id)) {
258
+ reader.fail('duplicate-id', `the id "${id}" is used twice`, child, link);
259
+ }
260
+ ids.add(id);
261
+ }
262
+
263
+ /** Build the nodes, groups and edges and check every reference. */
264
+ function buildItems(reader, pending, model) {
265
+ const ids = new Set([model.id]);
266
+ for (const { item, child, link } of pending.groups) {
267
+ claimId(reader, ids, item.id, child, link);
268
+ const props = reader.props(item.rest, ['label', 'group'], child, link);
269
+ model.groups.push({
270
+ id: item.id,
271
+ label: props.label ?? item.id,
272
+ group: props.group ?? null,
273
+ child,
274
+ link,
275
+ });
276
+ }
277
+ for (const { item, child, link } of pending.nodes) {
278
+ claimId(reader, ids, item.id, child, link);
279
+ const props = reader.props(
280
+ item.rest,
281
+ ['label', 'shape', 'group'],
282
+ child,
283
+ link
284
+ );
285
+ model.nodes.push({
286
+ id: item.id,
287
+ label: props.label ?? item.id,
288
+ shape: reader.oneOf(
289
+ props.shape ?? DEFAULTS.shape,
290
+ NODE_SHAPES,
291
+ 'shape',
292
+ child,
293
+ link
294
+ ),
295
+ group: props.group ?? null,
296
+ child,
297
+ link,
298
+ });
299
+ }
300
+ const groupIds = new Set(model.groups.map((group) => group.id));
301
+ for (const entry of [...model.groups, ...model.nodes]) {
302
+ if (entry.group !== null && !groupIds.has(entry.group)) {
303
+ reader.fail(
304
+ 'unknown-group',
305
+ `unknown group "${entry.group}" in ${String(entry.link)}`,
306
+ entry.child,
307
+ entry.link
308
+ );
309
+ }
310
+ }
311
+ checkGroupCycles(reader, model.groups);
312
+ const nodeIds = new Set(model.nodes.map((node) => node.id));
313
+ pending.edges.forEach(({ item, child, link }, i) => {
314
+ const id = item.id ?? `edge-${i + 1}`;
315
+ claimId(reader, ids, id, child, link);
316
+ const props = reader.props(item.rest, ['from', 'to', 'label'], child, link);
317
+ for (const end of ['from', 'to']) {
318
+ if (props[end] === undefined) {
319
+ reader.fail('missing-value', `an edge needs (${end} …)`, child, link);
320
+ }
321
+ if (!nodeIds.has(props[end])) {
322
+ reader.fail(
323
+ 'unknown-node',
324
+ `unknown node "${props[end]}" in ${String(link)}`,
325
+ child,
326
+ link
327
+ );
328
+ }
329
+ }
330
+ model.edges.push({
331
+ id,
332
+ from: props.from,
333
+ to: props.to,
334
+ label: props.label ?? null,
335
+ });
336
+ });
337
+ for (const list of [model.groups, model.nodes]) {
338
+ for (const entry of list) {
339
+ delete entry.child;
340
+ delete entry.link;
341
+ }
342
+ }
343
+ }
344
+
345
+ function checkGroupCycles(reader, groups) {
346
+ const parent = new Map(groups.map((group) => [group.id, group.group]));
347
+ for (const group of groups) {
348
+ const seen = new Set([group.id]);
349
+ for (let at = group.group; at !== null; at = parent.get(at)) {
350
+ if (seen.has(at)) {
351
+ reader.fail(
352
+ 'invalid-value',
353
+ `group "${group.id}" is inside itself`,
354
+ group.child,
355
+ group.link
356
+ );
357
+ }
358
+ seen.add(at);
359
+ }
360
+ }
361
+ }
362
+
363
+ /** Read one folded `(id: diagram ...)` link. */
364
+ function readDiagram(link, index) {
365
+ const reader = new Reader(link.id, index);
366
+ /** @type {DiagramModel} */
367
+ const model = {
368
+ id: link.id,
369
+ kind: DEFAULTS.kind,
370
+ direction: DEFAULTS.direction,
371
+ label: null,
372
+ renderer: null,
373
+ layout: null,
374
+ nodes: [],
375
+ edges: [],
376
+ groups: [],
377
+ };
378
+ const pending = readItems(reader, link.values.slice(1), model);
379
+ checkSettings(reader, model);
380
+ buildItems(reader, pending, model);
381
+ return model;
382
+ }
383
+
384
+ const isDiagramLink = (link) =>
385
+ link.id !== null && link.values[0]?.id === 'diagram' && isRef(link.values[0]);
386
+
387
+ /**
388
+ * Every diagram of a parsed document (`(id: diagram ...)` links).
389
+ * @param {Link[]} links the result of the `links-notation` parser
390
+ * @returns {DiagramModel[]}
391
+ */
392
+ export function diagramsFromLinks(links) {
393
+ const models = [];
394
+ links.forEach((raw, index) => {
395
+ const link = foldLink(raw);
396
+ if (isDiagramLink(link)) {
397
+ models.push(readDiagram(link, index));
398
+ }
399
+ });
400
+ return models;
401
+ }
402
+
403
+ /**
404
+ * The first diagram of a parsed document.
405
+ * @param {Link[]} links the result of the `links-notation` parser
406
+ * @returns {DiagramModel} `{ kind, direction, nodes, edges, groups, ... }`
407
+ * @throws {DiagramError} when there is no diagram or it is invalid (an edge
408
+ * to an unknown node id names the position of the edge link)
409
+ */
410
+ export function diagramFromLinks(links) {
411
+ const [model] = diagramsFromLinks(links);
412
+ if (!model) {
413
+ throw new DiagramError(
414
+ 'no-diagram',
415
+ 'no (id: diagram ...) link was found',
416
+ {
417
+ path: [],
418
+ }
419
+ );
420
+ }
421
+ return model;
422
+ }
423
+
424
+ /**
425
+ * Parse Links Notation text with `links-notation` and read its first diagram.
426
+ * @param {string} text
427
+ * @returns {DiagramModel}
428
+ */
429
+ export function parseDiagram(text) {
430
+ return diagramFromLinks(parseNested(text));
431
+ }
432
+
433
+ const ref = (id) => new Link(id);
434
+ const prop = (name, value) => new Link(null, [ref(name), ref(value)]);
435
+
436
+ /** The item links of a model, without defaults that equal the default. */
437
+ function itemLinks(model) {
438
+ const links = [];
439
+ const groups = model.groups.map((group) => {
440
+ const values = [ref('group')];
441
+ if (group.label !== group.id) {
442
+ values.push(prop('label', group.label));
443
+ }
444
+ if (group.group !== null) {
445
+ values.push(prop('group', group.group));
446
+ }
447
+ return new Link(group.id, values);
448
+ });
449
+ const nodes = model.nodes.map((node) => {
450
+ const values = [ref('node')];
451
+ if (node.label !== node.id) {
452
+ values.push(prop('label', node.label));
453
+ }
454
+ if (node.shape !== DEFAULTS.shape) {
455
+ values.push(prop('shape', node.shape));
456
+ }
457
+ if (node.group !== null) {
458
+ values.push(prop('group', node.group));
459
+ }
460
+ return new Link(node.id, values);
461
+ });
462
+ const edges = model.edges.map((edge, i) => {
463
+ const values = [ref('edge'), prop('from', edge.from), prop('to', edge.to)];
464
+ if (edge.label !== null) {
465
+ values.push(prop('label', edge.label));
466
+ }
467
+ return new Link(edge.id === `edge-${i + 1}` ? null : edge.id, values);
468
+ });
469
+ links.push(...groups, ...nodes, ...edges);
470
+ return links;
471
+ }
472
+
473
+ /**
474
+ * Write a diagram model as Links Notation in the nested form (#133): the
475
+ * `diagram` line, then one indented block per setting, group, node and edge.
476
+ * @param {DiagramModel} model
477
+ * @returns {string} LiNo text that `parseDiagram` reads back into `model`
478
+ */
479
+ export function diagramToLino(model) {
480
+ const settings = [
481
+ prop('kind', model.kind),
482
+ prop('direction', model.direction),
483
+ ];
484
+ for (const name of ['label', 'renderer', 'layout']) {
485
+ if (model[name] !== null && model[name] !== undefined) {
486
+ settings.push(prop(name, model[name]));
487
+ }
488
+ }
489
+ return formatLinks([
490
+ new Link(model.id, [ref('diagram'), ...settings, ...itemLinks(model)]),
491
+ ]);
492
+ }
493
+
494
+ /**
495
+ * The plain-English description of a diagram for screen readers: one line
496
+ * per node and per edge ("Start leads to Signed in?").
497
+ * @param {DiagramModel} model
498
+ * @returns {{nodes: string[], edges: string[]}}
499
+ */
500
+ export function describeDiagram(model) {
501
+ const labels = new Map(model.nodes.map((node) => [node.id, node.label]));
502
+ return {
503
+ nodes: model.nodes.map((node) => node.label),
504
+ edges: model.edges.map(
505
+ (edge) =>
506
+ `${labels.get(edge.from)} leads to ${labels.get(edge.to)}${
507
+ edge.label === null ? '' : ` (${edge.label})`
508
+ }`
509
+ ),
510
+ };
511
+ }
@@ -0,0 +1,204 @@
1
+ /**
2
+ * `<ui-diagram>`: a diagram written in Links Notation, drawn by our SVG
3
+ * renderer (flowchart and state kinds) after a registered layout (Dagre by
4
+ * default), or by the lazily loaded Mermaid adapter for `(renderer mermaid)`
5
+ * and for the kinds our renderer does not draw (sequence, class).
6
+ *
7
+ * The drawing has `role="img"` and an `aria-label`; a visually hidden list
8
+ * of the nodes and connections ("Start leads to Signed in?") follows it for
9
+ * screen readers. Like the ui-dom elements it is a plain custom element in
10
+ * the light DOM.
11
+ *
12
+ * ```html
13
+ * <ui-diagram>(f: diagram (a: node) (b: node) (edge (from a) (to b)))</ui-diagram>
14
+ * ```
15
+ */
16
+
17
+ import { describeDiagram, parseDiagram } from './diagram-model.js';
18
+ import { runLayout } from './layout-plugins.js';
19
+ import { toMermaid } from './mermaid-export.js';
20
+ import {
21
+ defineMermaidView,
22
+ setState,
23
+ showSource,
24
+ visuallyHiddenStyle,
25
+ } from './mermaid-view.js';
26
+ import { SVG_KINDS, diagramSummary, renderDiagramSvg } from './svg-diagram.js';
27
+
28
+ /** @typedef {import('./diagram-model.js').DiagramModel} DiagramModel */
29
+
30
+ /**
31
+ * Which renderer draws a model: the `renderer` attribute, then
32
+ * `(renderer …)`, then `svg` for the kinds our renderer draws.
33
+ * @param {DiagramModel} model
34
+ * @param {string|null} [override]
35
+ * @returns {'svg'|'mermaid'}
36
+ */
37
+ export function pickRenderer(model, override = null) {
38
+ const wanted = override ?? model.renderer;
39
+ if (wanted === 'svg' || wanted === 'mermaid') {
40
+ return wanted === 'svg' && !SVG_KINDS.includes(model.kind)
41
+ ? 'mermaid'
42
+ : wanted;
43
+ }
44
+ return SVG_KINDS.includes(model.kind) ? 'svg' : 'mermaid';
45
+ }
46
+
47
+ /** The visually hidden description: nodes, then connections. */
48
+ function descriptionOf(doc, model) {
49
+ const { nodes, edges } = describeDiagram(model);
50
+ const wrap = doc.createElement('div');
51
+ wrap.className = 'ui-diagram-description';
52
+ wrap.setAttribute('style', visuallyHiddenStyle());
53
+ const title = model.label ?? model.id;
54
+ for (const [name, items] of [
55
+ [`Nodes of ${title}`, nodes],
56
+ [`Connections of ${title}`, edges],
57
+ ]) {
58
+ if (items.length === 0) {
59
+ continue;
60
+ }
61
+ const list = doc.createElement('ul');
62
+ list.setAttribute('aria-label', name);
63
+ for (const text of items) {
64
+ const item = doc.createElement('li');
65
+ item.textContent = text;
66
+ list.appendChild(item);
67
+ }
68
+ wrap.appendChild(list);
69
+ }
70
+ return wrap;
71
+ }
72
+
73
+ let instances = 0;
74
+
75
+ /**
76
+ * The `<ui-diagram>` class for a window.
77
+ * @param {Window & typeof globalThis} win
78
+ */
79
+ function diagramClass(win) {
80
+ return class Diagram extends win.HTMLElement {
81
+ static get observedAttributes() {
82
+ return ['renderer', 'layout'];
83
+ }
84
+
85
+ constructor() {
86
+ super();
87
+ /** @type {string|null} */
88
+ this._source = null;
89
+ this._run = 0;
90
+ instances += 1;
91
+ this._idPrefix = `ui-diagram-${instances}`;
92
+ /** Options of the layout engine (`nodeSize`, `rankSep`, …). */
93
+ this.layoutOptions = {};
94
+ /** Tokens of the SVG renderer (`surface`, `onSurface`, …). */
95
+ this.tokens = {};
96
+ /** @type {DiagramModel|null} the model of the last render */
97
+ this.model = null;
98
+ }
99
+
100
+ /** The LiNo source (defaults to the text content at first connect). */
101
+ get source() {
102
+ return this._source ?? '';
103
+ }
104
+
105
+ set source(value) {
106
+ this._source = String(value);
107
+ if (this.isConnected) {
108
+ this.refresh();
109
+ }
110
+ }
111
+
112
+ connectedCallback() {
113
+ if (this._source === null) {
114
+ this._source = (this.textContent ?? '').trim();
115
+ }
116
+ this.refresh();
117
+ }
118
+
119
+ attributeChangedCallback(name, before, after) {
120
+ if (before !== after && this.isConnected && this._source !== null) {
121
+ this.refresh();
122
+ }
123
+ }
124
+
125
+ /** Render again; resolves to the state reached. */
126
+ async refresh() {
127
+ const run = ++this._run;
128
+ const source = this.source;
129
+ const doc = this.ownerDocument;
130
+ let model;
131
+ try {
132
+ model = parseDiagram(source);
133
+ } catch (error) {
134
+ showSource(this, source, `The diagram has an error: ${error.message}`);
135
+ this.model = null;
136
+ setState(this, 'error', { error });
137
+ return 'error';
138
+ }
139
+ this.model = model;
140
+ const renderer = pickRenderer(model, this.getAttribute('renderer'));
141
+ this.dataset.renderer = renderer;
142
+ if (renderer === 'mermaid') {
143
+ defineMermaidView(win);
144
+ const view = doc.createElement('ui-mermaid-view');
145
+ view.setAttribute('label', diagramSummary(model));
146
+ view.source = toMermaid(model);
147
+ this.replaceChildren(view, descriptionOf(doc, model));
148
+ setState(this, 'ready', { renderer });
149
+ return 'ready';
150
+ }
151
+ setState(this, 'loading', { renderer });
152
+ let svg;
153
+ try {
154
+ const layout = await runLayout(
155
+ model,
156
+ this.layoutOptions,
157
+ this.getAttribute('layout') ?? undefined
158
+ );
159
+ svg = renderDiagramSvg(model, layout, {
160
+ ...this.tokens,
161
+ idPrefix: this._idPrefix,
162
+ });
163
+ } catch (error) {
164
+ if (run !== this._run) {
165
+ return this.dataset.state;
166
+ }
167
+ showSource(
168
+ this,
169
+ source,
170
+ `The diagram could not be laid out: ${error.message}`
171
+ );
172
+ setState(this, 'error', { error });
173
+ return 'error';
174
+ }
175
+ if (run !== this._run) {
176
+ return this.dataset.state;
177
+ }
178
+ const figure = doc.createElement('div');
179
+ figure.className = 'ui-diagram-drawing';
180
+ // Our own SVG string: every label in it is escaped by renderDiagramSvg.
181
+ figure.innerHTML = svg;
182
+ this.replaceChildren(figure, descriptionOf(doc, model));
183
+ setState(this, 'ready', { renderer });
184
+ return 'ready';
185
+ }
186
+ };
187
+ }
188
+
189
+ /**
190
+ * Define `<ui-diagram>` (and `<ui-mermaid-view>`, which it uses) in a window,
191
+ * once.
192
+ * @param {Window & typeof globalThis} [win]
193
+ * @returns {CustomElementConstructor}
194
+ */
195
+ export function defineDiagram(win = globalThis) {
196
+ defineMermaidView(win);
197
+ const existing = win.customElements.get('ui-diagram');
198
+ if (existing) {
199
+ return existing;
200
+ }
201
+ const Diagram = diagramClass(win);
202
+ win.customElements.define('ui-diagram', Diagram);
203
+ return Diagram;
204
+ }