@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,265 @@
1
+ import { parseNested, formatLinks } from '@link-foundation/ui-core';
2
+ import { networkOptionSchema } from './schema.js';
3
+ export { networkOptionSchema, optionAvailable } from './schema.js';
4
+
5
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
6
+ const clamp = (value, min, max, fallback) => {
7
+ const n = value === '' || value === null ? NaN : Number(value);
8
+ return Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : fallback;
9
+ };
10
+ function scalar(field, value) {
11
+ if (field.type === 'number') {
12
+ const n = clamp(value, field.min, field.max, field.default);
13
+ return field.step === 1 ? Math.round(n) : n;
14
+ }
15
+ if (field.type === 'boolean') {
16
+ return value === true || value === 'true'
17
+ ? true
18
+ : value === false || value === 'false'
19
+ ? false
20
+ : field.default;
21
+ }
22
+ if (field.choices) {
23
+ return field.choices.includes(value) ? value : field.default;
24
+ }
25
+ if (field.type === 'color') {
26
+ return typeof value === 'string' && /^#[\da-f]{6}$/iu.test(value)
27
+ ? value.toLowerCase()
28
+ : field.default;
29
+ }
30
+ return typeof value === 'string'
31
+ ? [...value].slice(0, 80).join('')
32
+ : field.default;
33
+ }
34
+
35
+ function normalizedFrame(frame) {
36
+ return Array.isArray(frame) &&
37
+ frame.length === 3 &&
38
+ frame.every(
39
+ (v) => typeof v === 'number' && Number.isFinite(v) && Math.abs(v) <= 1e6
40
+ ) &&
41
+ frame[0] > 1e-9 &&
42
+ frame[0] <= 60
43
+ ? [...frame]
44
+ : null;
45
+ }
46
+
47
+ /** Clamp invalid scalar values and detach every per-link override. */
48
+ export function normalize(input = {}) {
49
+ const source = { ...input };
50
+ if (source.gap !== undefined) {
51
+ source.startGap ??= source.gap;
52
+ source.endGap ??= source.gap;
53
+ }
54
+ if (
55
+ networkOptionSchema.some(
56
+ (f) =>
57
+ f.type === 'color' &&
58
+ Object.hasOwn(source, f.key) &&
59
+ (!source.palette ||
60
+ (source.palette === 'default' && source[f.key] !== f.default))
61
+ )
62
+ ) {
63
+ source.palette = 'custom';
64
+ }
65
+ const output = Object.fromEntries(
66
+ networkOptionSchema.map((field) => [
67
+ field.key,
68
+ scalar(field, source[field.key]),
69
+ ])
70
+ );
71
+ const vector = (raw, ranges, inherit = false) =>
72
+ Array.isArray(raw)
73
+ ? ranges.map(([min, max], i) =>
74
+ inherit && (raw[i] === null || raw[i] === undefined)
75
+ ? null
76
+ : clamp(raw[i], min, max, 0)
77
+ )
78
+ : null;
79
+ output.turns = Array.isArray(source.turns)
80
+ ? source.turns.map((v) =>
81
+ (
82
+ vector(v, [
83
+ [-180, 180],
84
+ [-180, 180],
85
+ ]) ?? [0, 0]
86
+ ).map(Math.round)
87
+ )
88
+ : [];
89
+ output.linkGaps = Array.isArray(source.linkGaps)
90
+ ? source.linkGaps.map(
91
+ (v) =>
92
+ vector(
93
+ v,
94
+ [
95
+ [0, 1],
96
+ [0, 1],
97
+ [0, 30],
98
+ [0, 30],
99
+ ],
100
+ true
101
+ ) ?? [null, null, null, null]
102
+ )
103
+ : [];
104
+ output.linkColors = Array.isArray(source.linkColors)
105
+ ? source.linkColors.map((v) =>
106
+ typeof v === 'string' && /^#[\da-f]{6}$/iu.test(v)
107
+ ? v.toLowerCase()
108
+ : null
109
+ )
110
+ : [];
111
+ output.order =
112
+ Array.isArray(source.order) &&
113
+ source.order.length > 0 &&
114
+ source.order.every(
115
+ (v) => Number.isInteger(v) && v > 0 && v <= source.order.length
116
+ ) &&
117
+ new Set(source.order).size === source.order.length
118
+ ? [...source.order]
119
+ : null;
120
+ output.frame = normalizedFrame(source.frame);
121
+ return output;
122
+ }
123
+
124
+ const specialKeys = Object.freeze([
125
+ 'turns',
126
+ 'linkGaps',
127
+ 'linkColors',
128
+ 'order',
129
+ 'frame',
130
+ ]);
131
+ /** Only nondefault values enter URLSearchParams. Complex values use LiNo. */
132
+ export function toParams(input) {
133
+ const options = normalize(input);
134
+ const defaults = normalize({});
135
+ const params = new globalThis.URLSearchParams();
136
+ for (const key of [
137
+ ...networkOptionSchema.map((field) => field.key),
138
+ ...specialKeys,
139
+ ]) {
140
+ if (!same(options[key], defaults[key])) {
141
+ params.set(
142
+ key,
143
+ specialKeys.includes(key)
144
+ ? formatValue(key, options[key])
145
+ : String(options[key])
146
+ );
147
+ }
148
+ }
149
+ return params;
150
+ }
151
+ function formatValue(key, value) {
152
+ if (value === null) {
153
+ return '';
154
+ }
155
+ if (['turns', 'linkGaps'].includes(key)) {
156
+ return formatLinks(
157
+ value.map((row, i) => ({
158
+ id: String(i + 1),
159
+ values: row.map((v) => (v === null ? '~' : String(v))),
160
+ }))
161
+ );
162
+ }
163
+ return formatLinks([value.map((v) => (v === null ? '~' : String(v)))]);
164
+ }
165
+ function parseValue(key, text) {
166
+ const links = parseNested(text);
167
+ const atom = (v) =>
168
+ v.id === '~' ? null : key === 'linkColors' ? v.id : Number(v.id);
169
+ if (['turns', 'linkGaps'].includes(key)) {
170
+ if (!links.every((v, i) => v.id === String(i + 1))) {
171
+ return [];
172
+ }
173
+ return links.map((v) => v.values.map(atom));
174
+ }
175
+ return links.length === 1
176
+ ? links[0].values.length
177
+ ? links[0].values.map(atom)
178
+ : [atom(links[0])]
179
+ : [];
180
+ }
181
+ export function fromParams(value) {
182
+ const params = new globalThis.URLSearchParams(value);
183
+ const raw = {};
184
+ for (const key of [
185
+ ...networkOptionSchema.map((field) => field.key),
186
+ 'gap',
187
+ ...specialKeys,
188
+ ]) {
189
+ if (!params.has(key)) {
190
+ continue;
191
+ }
192
+ try {
193
+ raw[key] = specialKeys.includes(key)
194
+ ? parseValue(key, params.get(key))
195
+ : params.get(key);
196
+ } catch {
197
+ /* Ignore malformed optional URL values. */
198
+ }
199
+ }
200
+ // Defaults must not become explicit colours, which imply a custom palette.
201
+ return normalize(raw);
202
+ }
203
+
204
+ /** Canonical editor state for saved styles and presentation adapters. */
205
+ export function toOptions(input) {
206
+ return formatLinks([...toParams(input)].map(([key, value]) => [key, value]));
207
+ }
208
+ export function fromOptions(text = '') {
209
+ const params = new globalThis.URLSearchParams();
210
+ for (const link of parseNested(text)) {
211
+ if (link.id === null && link.values.length === 2) {
212
+ params.set(link.values[0].id, link.values[1].id);
213
+ }
214
+ }
215
+ return fromParams(params);
216
+ }
217
+
218
+ export function editOptions(input, patch, { countChanged = false } = {}) {
219
+ // Selecting the default palette restores its colours; otherwise inherited
220
+ // custom colours would immediately normalize the selection back to custom.
221
+ const palette =
222
+ patch.palette === 'default'
223
+ ? Object.fromEntries(
224
+ networkOptionSchema
225
+ .filter((field) => field.type === 'color')
226
+ .map((field) => [field.key, field.default])
227
+ )
228
+ : {};
229
+ const geometry = new Set([
230
+ 'layout',
231
+ 'geometry',
232
+ 'gaps',
233
+ 'start',
234
+ 'end',
235
+ 'middle',
236
+ 'size',
237
+ 'caption',
238
+ ]);
239
+ const clearsFrame = Object.keys(patch).some(
240
+ (key) =>
241
+ [
242
+ 'turns',
243
+ 'linkGaps',
244
+ 'order',
245
+ 'strokeWidth',
246
+ 'glow',
247
+ 'card.inset',
248
+ 'card.width',
249
+ 'card.radius',
250
+ ].includes(key) ||
251
+ networkOptionSchema.some(
252
+ (field) =>
253
+ field.key === key &&
254
+ field.type !== 'color' &&
255
+ geometry.has(field.section)
256
+ )
257
+ );
258
+ return normalize({
259
+ ...input,
260
+ ...patch,
261
+ ...palette,
262
+ ...(clearsFrame ? { frame: null } : {}),
263
+ ...(countChanged ? { frame: null, order: null } : {}),
264
+ });
265
+ }
@@ -0,0 +1,423 @@
1
+ import {
2
+ networkOptionSchema,
3
+ normalize,
4
+ optionAvailable,
5
+ toOptions,
6
+ fromOptions,
7
+ } from './options.js';
8
+ import { networkLooks, networkSizePresets } from './presets.js';
9
+ import { parseNested, formatLinks } from '@link-foundation/ui-core';
10
+ import { element, button, field, choices } from './dom.js';
11
+
12
+ export function notationPanel(parent, context) {
13
+ const { doc, config, applyLinks, getState, run } = context;
14
+ const panel = element(doc, 'fieldset', undefined, parent);
15
+ element(doc, 'legend', 'Notations', panel);
16
+ const summary = element(doc, 'p', undefined, panel);
17
+ const inputs = new Map();
18
+ const definitions = [
19
+ ['theory', config.parseTheory, config.formatTheory],
20
+ ['pairs', config.parseCode, config.encode],
21
+ ['number', config.parseNumber, config.formatNumber],
22
+ [
23
+ 'decimal',
24
+ (text) => config.parseDecimal(text, getState().links.length),
25
+ config.formatDecimal,
26
+ ],
27
+ [
28
+ 'links',
29
+ config.parseLinks,
30
+ (links) =>
31
+ formatLinks(
32
+ links.map(([s, t], i) => ({
33
+ id: String(i + 1),
34
+ values: [String(s), String(t)],
35
+ })),
36
+ { style: 'inline' }
37
+ )
38
+ .trim()
39
+ .split('\n')
40
+ .join(' '),
41
+ ],
42
+ ];
43
+ for (const [name, parse, format] of definitions) {
44
+ const input = field(doc, panel, name);
45
+ input.autocomplete = 'off';
46
+ input.spellcheck = false;
47
+ const error = element(doc, 'span', undefined, input.parentNode);
48
+ error.setAttribute('role', 'alert');
49
+ error.id = `${context.id}-${name}-error`;
50
+ input.setAttribute('aria-describedby', error.id);
51
+ input.addEventListener('input', () => {
52
+ context.draft = { name, code: getState().code };
53
+ const result = parse(input.value);
54
+ if (result.error) {
55
+ input.setAttribute('aria-invalid', 'true');
56
+ error.textContent =
57
+ config.configurationMessages?.[context.locale]?.[result.error] ??
58
+ result.error;
59
+ } else {
60
+ input.removeAttribute('aria-invalid');
61
+ error.textContent = '';
62
+ context.draft = { name, code: config.encode(result.links) };
63
+ run(() => applyLinks(result.links));
64
+ }
65
+ });
66
+ inputs.set(name, { input, error, format });
67
+ }
68
+ return {
69
+ refresh(external = false) {
70
+ const state = getState();
71
+ if (external || (context.draft && context.draft.code !== state.code)) {
72
+ context.draft = null;
73
+ }
74
+ for (const [name, record] of inputs) {
75
+ if (!external && context.draft?.name === name) {
76
+ continue;
77
+ }
78
+ record.input.value = record.format(state.links);
79
+ record.input.removeAttribute('aria-invalid');
80
+ record.error.textContent = '';
81
+ }
82
+ summary.textContent = `n = ${state.links.length} · base ${state.links.length + 1} · ${2 * state.links.length} digits · one of ${config.count(state.links.length)} networks`;
83
+ },
84
+ };
85
+ }
86
+
87
+ export function linksPanel(parent, context) {
88
+ const { doc, config, getState, applyLinks, changeOptions, run } = context;
89
+ const panel = element(doc, 'fieldset', undefined, parent);
90
+ element(doc, 'legend', 'Links', panel);
91
+ const canonical = linkActions(panel, context);
92
+ const cards = element(doc, 'div', undefined, panel);
93
+ let count = -1;
94
+ let controls = [];
95
+ function build() {
96
+ cards.replaceChildren();
97
+ controls = [];
98
+ const state = getState();
99
+ count = state.links.length;
100
+ for (let i = 0; i < count; i++) {
101
+ const card = element(doc, 'fieldset', undefined, cards);
102
+ element(doc, 'legend', `Link ${i + 1}`, card);
103
+ const reference = ['source', 'target'].map((end, e) => {
104
+ const input = field(
105
+ doc,
106
+ card,
107
+ `${end} ${i + 1}`,
108
+ count > 50 ? 'number' : 'select'
109
+ );
110
+ if (count <= 50) {
111
+ choices(
112
+ doc,
113
+ input,
114
+ Array.from({ length: count + 1 }, (_, k) => [
115
+ String(k),
116
+ k === 0 ? '0 — nothing' : k === i + 1 ? `${k} — self` : String(k),
117
+ ])
118
+ );
119
+ } else {
120
+ input.min = '0';
121
+ input.max = String(count);
122
+ input.step = '1';
123
+ }
124
+ input.addEventListener('input', () =>
125
+ run(() =>
126
+ applyLinks(
127
+ config.setReference(
128
+ getState().links,
129
+ i + 1,
130
+ e,
131
+ Number(input.value)
132
+ )
133
+ )
134
+ )
135
+ );
136
+ return input;
137
+ });
138
+ const color = field(doc, card, `colour ${i + 1}`, 'color');
139
+ color.addEventListener('input', () =>
140
+ run(() => {
141
+ const values = [...getState().options.linkColors];
142
+ while (values.length <= i) {
143
+ values.push(null);
144
+ }
145
+ values[i] = color.value;
146
+ return changeOptions({ linkColors: values, palette: 'custom' });
147
+ })
148
+ );
149
+ const turns = ['start', 'end'].map((end, e) => {
150
+ const input = field(doc, card, `${end} turn ${i + 1}`, 'number');
151
+ input.min = '-180';
152
+ input.max = '180';
153
+ input.step = '1';
154
+ const change = (value) => {
155
+ const values = getState().links.map((_, k) => [
156
+ ...(getState().options.turns[k] ?? [0, 0]),
157
+ ]);
158
+ values[i][e] = value;
159
+ return changeOptions({ turns: values });
160
+ };
161
+ input.addEventListener('input', () =>
162
+ run(() => change(Number(input.value)))
163
+ );
164
+ button(doc, card, `Reset ${end} turn ${i + 1}`, () =>
165
+ run(() => change(0))
166
+ );
167
+ return input;
168
+ });
169
+ const gaps = [
170
+ 'start gap',
171
+ 'end gap',
172
+ 'cut before middle',
173
+ 'cut after middle',
174
+ ].map((name, g) => {
175
+ const input = field(doc, card, `${name} ${i + 1}`, 'number');
176
+ input.min = '0';
177
+ input.max = g < 2 ? '1' : '30';
178
+ input.step = '0.01';
179
+ input.placeholder = 'inherit';
180
+ input.addEventListener('input', () =>
181
+ run(() => {
182
+ const values = getState().links.map((_, k) => [
183
+ ...(getState().options.linkGaps[k] ?? [null, null, null, null]),
184
+ ]);
185
+ values[i][g] = input.value === '' ? null : Number(input.value);
186
+ return changeOptions({ linkGaps: values });
187
+ })
188
+ );
189
+ return input;
190
+ });
191
+ const remove = button(doc, card, `Remove link ${i + 1}`, () =>
192
+ run(() => context.removeLink(i + 1))
193
+ );
194
+ remove.disabled = count === 1;
195
+ controls.push({ reference, color, turns, gaps });
196
+ }
197
+ }
198
+ return {
199
+ refresh() {
200
+ const state = getState();
201
+ if (count !== state.links.length) {
202
+ build();
203
+ }
204
+ canonical.disabled = state.links.length > 7;
205
+ controls.forEach((record, i) => {
206
+ const set = (input, value) => {
207
+ if (
208
+ doc.activeElement !== input &&
209
+ input.getRootNode().activeElement !== input
210
+ ) {
211
+ input.value = value;
212
+ }
213
+ };
214
+ record.reference.forEach((input, e) => set(input, state.links[i][e]));
215
+ set(record.color, state.options.linkColors[i] ?? state.options.color);
216
+ record.turns.forEach((input, e) =>
217
+ set(input, state.options.turns[i]?.[e] ?? 0)
218
+ );
219
+ record.gaps.forEach((input, g) =>
220
+ set(input, state.options.linkGaps[i]?.[g] ?? '')
221
+ );
222
+ });
223
+ },
224
+ };
225
+ }
226
+
227
+ export function stylePanel(parent, context) {
228
+ const { doc, getState, changeOptions, applyStyle, run } = context;
229
+ const panel = element(doc, 'fieldset', undefined, parent);
230
+ element(doc, 'legend', 'Style', panel);
231
+ const looks = field(doc, panel, 'Look', 'select');
232
+ choices(doc, looks, [
233
+ ['', 'Choose a look'],
234
+ ...Object.keys(networkLooks).map((key) => [
235
+ key,
236
+ key === 'social' ? 'social 1200×630' : key,
237
+ ]),
238
+ ]);
239
+ looks.addEventListener('change', () => {
240
+ if (looks.value) {
241
+ run(() => applyStyle(networkLooks[looks.value]));
242
+ looks.value = '';
243
+ }
244
+ });
245
+ const sizes = field(doc, panel, 'Size', 'select');
246
+ choices(doc, sizes, [
247
+ ['', 'Choose a size'],
248
+ ...networkSizePresets.map(([w, h]) => [`${w}x${h}`, `${w}×${h}`]),
249
+ ]);
250
+ sizes.addEventListener('change', () => {
251
+ if (sizes.value) {
252
+ const [width, height] = sizes.value.split('x').map(Number);
253
+ run(() => changeOptions({ width, height }));
254
+ sizes.value = '';
255
+ }
256
+ });
257
+ const sections = new Map(),
258
+ controls = [];
259
+ for (const definition of networkOptionSchema) {
260
+ if (!sections.has(definition.section)) {
261
+ const group = element(doc, 'fieldset', undefined, panel);
262
+ element(doc, 'legend', definition.section, group);
263
+ const reset = button(doc, group, `Reset ${definition.section}`, () => {
264
+ const defaults = normalize({});
265
+ run(() =>
266
+ changeOptions(
267
+ Object.fromEntries(
268
+ networkOptionSchema
269
+ .filter((f) => f.section === definition.section)
270
+ .map((f) => [f.key, defaults[f.key]])
271
+ )
272
+ )
273
+ );
274
+ });
275
+ sections.set(definition.section, { group, reset });
276
+ }
277
+ const group = sections.get(definition.section).group;
278
+ const type = {
279
+ choice: 'select',
280
+ boolean: 'checkbox',
281
+ number: 'range',
282
+ color: 'color',
283
+ text: 'text',
284
+ }[definition.type];
285
+ const input = field(doc, group, definition.key, type);
286
+ if (definition.choices) {
287
+ choices(
288
+ doc,
289
+ input,
290
+ definition.choices.map((v) => [v, v])
291
+ );
292
+ }
293
+ if (type === 'range') {
294
+ input.min = definition.min;
295
+ input.max = definition.max;
296
+ input.step = definition.step;
297
+ }
298
+ const output =
299
+ type === 'range'
300
+ ? element(doc, 'span', undefined, input.parentNode)
301
+ : null;
302
+ input.addEventListener('input', () =>
303
+ run(() =>
304
+ changeOptions({
305
+ [definition.key]:
306
+ type === 'checkbox'
307
+ ? input.checked
308
+ : type === 'range'
309
+ ? Number(input.value)
310
+ : input.value,
311
+ ...(type === 'color' ? { palette: 'custom' } : {}),
312
+ })
313
+ )
314
+ );
315
+ controls.push({ definition, input, output });
316
+ }
317
+ const storage = savedStyles(panel, context);
318
+ return {
319
+ refresh() {
320
+ const { links, options } = getState();
321
+ const defaults = normalize({});
322
+ for (const { definition: f, input, output } of controls) {
323
+ input.parentNode.hidden = !optionAvailable(f, links, options);
324
+ if (f.type === 'boolean') {
325
+ input.checked = options[f.key];
326
+ } else {
327
+ input.value = options[f.key];
328
+ }
329
+ if (output) {
330
+ output.textContent = String(options[f.key]);
331
+ }
332
+ }
333
+ for (const [section, { reset }] of sections) {
334
+ reset.disabled = networkOptionSchema
335
+ .filter((f) => f.section === section)
336
+ .every((f) => options[f.key] === defaults[f.key]);
337
+ }
338
+ },
339
+ dispose: storage.dispose,
340
+ };
341
+ }
342
+
343
+ function savedStyles(parent, context) {
344
+ const { doc, win, run, getState, applyStyle, announce } = context;
345
+ const name = field(doc, parent, 'Style name');
346
+ name.maxLength = 80;
347
+ const select = field(doc, parent, 'Saved styles', 'select');
348
+ const storageKey = 'links-network-editor-styles';
349
+ function read() {
350
+ const text = win.localStorage.getItem(storageKey) ?? '';
351
+ return new Map(
352
+ parseNested(text)
353
+ .filter((v) => v.id !== null && v.values.length === 1)
354
+ .map((v) => [v.id, v.values[0].id])
355
+ );
356
+ }
357
+ function refresh() {
358
+ select.replaceChildren();
359
+ choices(
360
+ doc,
361
+ select,
362
+ [...read()].map(([key]) => [key, key])
363
+ );
364
+ }
365
+ button(doc, parent, 'Save style', () =>
366
+ run(() => {
367
+ const key = name.value.trim();
368
+ if (!key) {
369
+ throw new Error('Enter a style name');
370
+ }
371
+ const styles = read();
372
+ styles.set(key, toOptions(getState().options));
373
+ win.localStorage.setItem(
374
+ storageKey,
375
+ formatLinks([...styles].map(([id, text]) => ({ id, values: [text] })))
376
+ );
377
+ refresh();
378
+ announce('Style saved');
379
+ })
380
+ );
381
+ button(doc, parent, 'Load style', () =>
382
+ run(() => {
383
+ const text = read().get(select.value);
384
+ if (text !== undefined) {
385
+ return applyStyle(fromOptions(text));
386
+ }
387
+ })
388
+ );
389
+ try {
390
+ refresh();
391
+ } catch (error) {
392
+ announce(error.message, true);
393
+ }
394
+ return { dispose() {} };
395
+ }
396
+
397
+ function linkActions(panel, context) {
398
+ const { doc, config, getState, applyLinks, run } = context;
399
+ const toolbar = element(doc, 'div', undefined, panel);
400
+ button(doc, toolbar, 'Add link', () =>
401
+ run(() => applyLinks(config.addLink(getState().links)))
402
+ );
403
+ for (const [label, direction] of [
404
+ ['Previous', -1],
405
+ ['Next', 1],
406
+ ]) {
407
+ button(doc, toolbar, label, () =>
408
+ run(() => applyLinks(config.step(getState().links, direction)))
409
+ );
410
+ }
411
+ for (const real of [false, true]) {
412
+ button(doc, toolbar, real ? 'Random real links' : 'Random', () =>
413
+ run(() =>
414
+ applyLinks(
415
+ config.decode(config.randomCode(getState().links.length, { real }))
416
+ )
417
+ )
418
+ );
419
+ }
420
+ return button(doc, toolbar, 'Canonical renaming', () =>
421
+ run(() => context.canonicalize())
422
+ );
423
+ }