@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.
- package/CONFIGURATIONS.md +74 -0
- package/ICONS.md +76 -0
- package/LICENSE +24 -0
- package/README.md +1499 -2
- package/bin/lino-network.mjs +71 -0
- package/dist/icons/ATTRIBUTION.md +602 -0
- package/dist/icons/NOTICE.lino +1261 -0
- package/dist/icons/brand.js +31 -0
- package/dist/icons/brand.svg +3 -0
- package/dist/icons/licenses/brand/DISCLAIMER.md +51 -0
- package/dist/icons/licenses/brand/LICENSE.md +30 -0
- package/dist/icons/licenses/lucide/LICENSE +43 -0
- package/dist/icons/licenses/material-symbols/LICENSE +202 -0
- package/dist/icons/licenses/material-symbols/NOTICE +12 -0
- package/dist/icons/licenses/tabler/LICENSE +21 -0
- package/dist/icons/lucide.js +681 -0
- package/dist/icons/lucide.svg +34 -0
- package/dist/icons/material-symbols.js +681 -0
- package/dist/icons/material-symbols.svg +34 -0
- package/dist/icons/own.js +521 -0
- package/dist/icons/own.svg +34 -0
- package/dist/icons/tabler.js +681 -0
- package/dist/icons/tabler.svg +34 -0
- package/dist/network-layout-NOTICE.md +6 -0
- package/dist/network-layout-d3-dispatch-LICENSE.txt +13 -0
- package/dist/network-layout-d3-force-LICENSE.txt +13 -0
- package/dist/network-layout-d3-quadtree-LICENSE.txt +13 -0
- package/dist/network-layout-d3-timer-LICENSE.txt +13 -0
- package/dist/network-layout-dagre-LICENSE.txt +19 -0
- package/dist/network-layout.worker.mjs +2 -0
- package/package.json +246 -4
- package/src/a11y/announce.js +88 -0
- package/src/a11y/bind-machine.js +181 -0
- package/src/a11y/focus.js +169 -0
- package/src/a11y/index.d.ts +89 -0
- package/src/a11y/index.js +19 -0
- package/src/a11y/keys.js +182 -0
- package/src/a11y/roving.js +167 -0
- package/src/a11y/typeahead.js +82 -0
- package/src/behavior/actions.js +235 -0
- package/src/behavior/diagnostics.js +73 -0
- package/src/behavior/dom.d.ts +38 -0
- package/src/behavior/dom.js +420 -0
- package/src/behavior/index.d.ts +211 -0
- package/src/behavior/index.js +29 -0
- package/src/behavior/interpret.js +454 -0
- package/src/behavior/machines/disclosure.lino +46 -0
- package/src/behavior/machines/index.js +91 -0
- package/src/behavior/machines/toggle.lino +25 -0
- package/src/behavior/parse-machine.js +367 -0
- package/src/charts/README.md +78 -0
- package/src/charts/activity-layout.js +350 -0
- package/src/charts/activity-model.js +130 -0
- package/src/charts/activity-validation.js +31 -0
- package/src/charts/chart-model.js +667 -0
- package/src/charts/chart.js +518 -0
- package/src/charts/echarts-adapter.d.ts +23 -0
- package/src/charts/echarts-adapter.js +240 -0
- package/src/charts/element.js +330 -0
- package/src/charts/errors.js +62 -0
- package/src/charts/gauge.js +27 -0
- package/src/charts/index.d.ts +344 -0
- package/src/charts/index.js +59 -0
- package/src/charts/layout.js +542 -0
- package/src/charts/lino.js +158 -0
- package/src/charts/mark-options.js +78 -0
- package/src/charts/read.js +101 -0
- package/src/charts/renderer-features.js +8 -0
- package/src/charts/scales.js +223 -0
- package/src/charts/small-model.js +223 -0
- package/src/charts/small-svg.js +269 -0
- package/src/charts/sparkline.js +27 -0
- package/src/charts/stat-tile.js +37 -0
- package/src/charts/summary.js +193 -0
- package/src/charts/svg-renderer.js +226 -0
- package/src/charts/svg.js +69 -0
- package/src/charts/tag-cloud.js +129 -0
- package/src/charts/tokens.js +171 -0
- package/src/charts/tooltip.js +47 -0
- package/src/charts/values.js +146 -0
- package/src/chat/README.md +142 -0
- package/src/chat/actions.js +75 -0
- package/src/chat/chat-contract.js +95 -0
- package/src/chat/chat-list.js +42 -0
- package/src/chat/chat-model.js +125 -0
- package/src/chat/chat.css +228 -0
- package/src/chat/composer.js +131 -0
- package/src/chat/day-separator.js +12 -0
- package/src/chat/dom.js +33 -0
- package/src/chat/follow.js +49 -0
- package/src/chat/index.d.ts +142 -0
- package/src/chat/index.js +13 -0
- package/src/chat/message-bubble.js +191 -0
- package/src/chat/message-stream.js +331 -0
- package/src/chat/reasoning-trace.js +70 -0
- package/src/chat/selection.js +39 -0
- package/src/chat/skins/bubbles.lino +2 -0
- package/src/chat/skins/cards.lino +4 -0
- package/src/chat/skins/compact.lino +4 -0
- package/src/chat/skins/signal.lino +25 -0
- package/src/chat/skins/t3.lino +25 -0
- package/src/chat/skins/telegram.lino +31 -0
- package/src/chat/skins/whatsapp.lino +25 -0
- package/src/chat/stream-keys.js +57 -0
- package/src/chat/stream-window.js +159 -0
- package/src/chat/vocabulary.js +19 -0
- package/src/controls/accordion.js +160 -0
- package/src/controls/avatar.js +80 -0
- package/src/controls/badge.js +48 -0
- package/src/controls/button-template.js +32 -0
- package/src/controls/button.js +97 -0
- package/src/controls/catalog.lino +84 -0
- package/src/controls/checkbox.js +112 -0
- package/src/controls/chip.js +140 -0
- package/src/controls/choices.js +45 -0
- package/src/controls/color-field.js +217 -0
- package/src/controls/color-popover.js +135 -0
- package/src/controls/color-rgb.js +80 -0
- package/src/controls/color.js +67 -0
- package/src/controls/combobox.js +192 -0
- package/src/controls/dom.js +261 -0
- package/src/controls/icon-button.js +33 -0
- package/src/controls/index.d.ts +163 -0
- package/src/controls/index.js +182 -0
- package/src/controls/kinds-text.js +358 -0
- package/src/controls/kinds.lino +350 -0
- package/src/controls/label.js +52 -0
- package/src/controls/link.js +101 -0
- package/src/controls/listbox.js +144 -0
- package/src/controls/machines/button.lino +20 -0
- package/src/controls/machines/checkbox.lino +17 -0
- package/src/controls/machines/chip.lino +20 -0
- package/src/controls/machines/color-field.lino +23 -0
- package/src/controls/machines/combobox.lino +82 -0
- package/src/controls/machines/icon-button.lino +20 -0
- package/src/controls/machines/index.js +586 -0
- package/src/controls/machines/number-field.lino +71 -0
- package/src/controls/machines/radio-group.lino +17 -0
- package/src/controls/machines/select.lino +122 -0
- package/src/controls/machines/slider.lino +99 -0
- package/src/controls/machines/switch.lino +19 -0
- package/src/controls/machines/text-area.lino +26 -0
- package/src/controls/machines/text-field.lino +26 -0
- package/src/controls/number-field.js +239 -0
- package/src/controls/numeric-keys.js +125 -0
- package/src/controls/option.js +54 -0
- package/src/controls/progress.js +76 -0
- package/src/controls/radio-group.js +234 -0
- package/src/controls/select.js +229 -0
- package/src/controls/slider.js +234 -0
- package/src/controls/stories.js +172 -0
- package/src/controls/styles.css +1060 -0
- package/src/controls/switch.js +95 -0
- package/src/controls/text-area.js +39 -0
- package/src/controls/text-field.js +131 -0
- package/src/controls/vocabulary.js +164 -0
- package/src/data/data.css +95 -0
- package/src/data/index.d.ts +83 -0
- package/src/data/index.js +10 -0
- package/src/data/keyboard.js +189 -0
- package/src/data/list.js +215 -0
- package/src/data/render-data.js +116 -0
- package/src/data/selection-dom.js +39 -0
- package/src/data/selection-model.js +43 -0
- package/src/data/sort-model.js +42 -0
- package/src/data/source.js +45 -0
- package/src/data/table.js +160 -0
- package/src/data/tree-model.js +41 -0
- package/src/data/tree.js +55 -0
- package/src/data/viewport.js +216 -0
- package/src/data/virtual-range.js +92 -0
- package/src/data/vocabulary.js +67 -0
- package/src/diagrams/diagram-model.js +511 -0
- package/src/diagrams/diagram.js +204 -0
- package/src/diagrams/index.d.ts +225 -0
- package/src/diagrams/index.js +48 -0
- package/src/diagrams/layout-dagre.js +164 -0
- package/src/diagrams/layout-plugins.js +131 -0
- package/src/diagrams/mermaid-export.js +232 -0
- package/src/diagrams/mermaid-view.js +229 -0
- package/src/diagrams/svg-diagram.js +215 -0
- package/src/editor/code-editor.js +206 -0
- package/src/editor/documents-model.js +170 -0
- package/src/editor/documents-persistence.js +85 -0
- package/src/editor/editor-engine.js +22 -0
- package/src/editor/editor-ui.js +203 -0
- package/src/editor/editor.css +106 -0
- package/src/editor/engines/codemirror.js +227 -0
- package/src/editor/engines/monaco.js +219 -0
- package/src/editor/index.d.ts +111 -0
- package/src/editor/index.js +14 -0
- package/src/editor/lino-highlight.js +106 -0
- package/src/editor/lino-structure.js +85 -0
- package/src/editor/validate-lino.js +43 -0
- package/src/embed/README.md +161 -0
- package/src/embed/controller.js +223 -0
- package/src/embed/element.js +141 -0
- package/src/embed/index.d.ts +75 -0
- package/src/embed/index.js +9 -0
- package/src/embed/providers/telegram.d.ts +3 -0
- package/src/embed/providers/telegram.js +38 -0
- package/src/embed/providers/vk.d.ts +3 -0
- package/src/embed/providers/vk.js +42 -0
- package/src/embed/providers/x.d.ts +3 -0
- package/src/embed/providers/x.js +31 -0
- package/src/embed/registry.js +35 -0
- package/src/embed/script.js +73 -0
- package/src/embed/view.js +122 -0
- package/src/icons/adapters/lucide.d.ts +15 -0
- package/src/icons/adapters/lucide.js +142 -0
- package/src/icons/adapters/material-symbols.d.ts +13 -0
- package/src/icons/adapters/material-symbols.js +113 -0
- package/src/icons/adapters/shared.d.ts +38 -0
- package/src/icons/adapters/shared.js +157 -0
- package/src/icons/adapters/simple-icons.d.ts +9 -0
- package/src/icons/adapters/simple-icons.js +38 -0
- package/src/icons/adapters/tabler.d.ts +9 -0
- package/src/icons/adapters/tabler.js +86 -0
- package/src/icons/icon-registry.js +215 -0
- package/src/icons/icon.js +356 -0
- package/src/icons/index.d.ts +123 -0
- package/src/icons/index.js +30 -0
- package/src/icons/names.js +51 -0
- package/src/icons/own-icons.js +521 -0
- package/src/icons/presentation.js +55 -0
- package/src/icons/svg.js +136 -0
- package/src/index.d.ts +14 -0
- package/src/index.js +40 -0
- package/src/layout/README.md +102 -0
- package/src/layout/app-shell.js +150 -0
- package/src/layout/catalog.lino +27 -0
- package/src/layout/dock-model.js +576 -0
- package/src/layout/dock-view.js +312 -0
- package/src/layout/dock.js +352 -0
- package/src/layout/dom.js +312 -0
- package/src/layout/grid.js +192 -0
- package/src/layout/index.d.ts +267 -0
- package/src/layout/index.js +60 -0
- package/src/layout/layer-manager.js +120 -0
- package/src/layout/layout.css +760 -0
- package/src/layout/menu.js +141 -0
- package/src/layout/nav-strip.js +174 -0
- package/src/layout/page-links.js +166 -0
- package/src/layout/panel.js +26 -0
- package/src/layout/pref-switches.js +209 -0
- package/src/layout/split-model.js +270 -0
- package/src/layout/split.js +430 -0
- package/src/layout/stack.js +86 -0
- package/src/layout/surfaces.js +119 -0
- package/src/layout/tab-group.js +151 -0
- package/src/layout/tab-strip.js +277 -0
- package/src/layout/vocabulary.js +229 -0
- package/src/layout/window-frame.js +222 -0
- package/src/layout/window.js +146 -0
- package/src/links-view/animator.js +144 -0
- package/src/links-view/configuration-classes.js +150 -0
- package/src/links-view/configuration-model.js +191 -0
- package/src/links-view/configuration-notations.js +230 -0
- package/src/links-view/configurations.d.ts +73 -0
- package/src/links-view/configurations.js +33 -0
- package/src/links-view/create-layout-runner.js +79 -0
- package/src/links-view/doublet-curves.js +239 -0
- package/src/links-view/doublet-layout.js +148 -0
- package/src/links-view/doublet-markers.js +109 -0
- package/src/links-view/doublet-options.js +369 -0
- package/src/links-view/doublet-svg.js +156 -0
- package/src/links-view/doublet.js +230 -0
- package/src/links-view/export.js +146 -0
- package/src/links-view/geometry.js +409 -0
- package/src/links-view/index.d.ts +1029 -0
- package/src/links-view/index.js +140 -0
- package/src/links-view/labels.js +28 -0
- package/src/links-view/layout-worker-entry.mjs +2 -0
- package/src/links-view/layout-worker.js +28 -0
- package/src/links-view/layouts/circle.js +7 -0
- package/src/links-view/layouts/force.js +76 -0
- package/src/links-view/layouts/grid.js +7 -0
- package/src/links-view/layouts/hex.js +8 -0
- package/src/links-view/layouts/index.js +15 -0
- package/src/links-view/layouts/layered.js +29 -0
- package/src/links-view/layouts/regular.js +27 -0
- package/src/links-view/layouts/spiral.js +10 -0
- package/src/links-view/link-view.js +494 -0
- package/src/links-view/links-gallery.js +306 -0
- package/src/links-view/links-network.js +496 -0
- package/src/links-view/lino-network-source.js +122 -0
- package/src/links-view/lino-network.js +233 -0
- package/src/links-view/lino-to-network.js +252 -0
- package/src/links-view/markers.js +192 -0
- package/src/links-view/model.js +233 -0
- package/src/links-view/network-editor/dom.js +36 -0
- package/src/links-view/network-editor/editor.js +202 -0
- package/src/links-view/network-editor/model.js +163 -0
- package/src/links-view/network-editor/options.js +265 -0
- package/src/links-view/network-editor/panels.js +423 -0
- package/src/links-view/network-editor/presets.js +76 -0
- package/src/links-view/network-editor/provider.js +93 -0
- package/src/links-view/network-editor/remap.js +45 -0
- package/src/links-view/network-editor/schema.js +224 -0
- package/src/links-view/network-editor/share.js +232 -0
- package/src/links-view/network-editor/styles.js +19 -0
- package/src/links-view/network-editor/url-sync.js +131 -0
- package/src/links-view/network-interaction.js +228 -0
- package/src/links-view/network-model.js +85 -0
- package/src/links-view/network-renderer.js +259 -0
- package/src/links-view/network-scene.js +245 -0
- package/src/links-view/network-source.js +82 -0
- package/src/links-view/network-vocabulary.js +18 -0
- package/src/links-view/presentation.js +199 -0
- package/src/links-view/render-canvas.js +102 -0
- package/src/links-view/render-svg.js +118 -0
- package/src/links-view/scene.js +233 -0
- package/src/links-view/solver-job.js +106 -0
- package/src/links-view/tokens.js +114 -0
- package/src/links-view/zoom-pan.js +203 -0
- package/src/overlays/catalog.lino +89 -0
- package/src/overlays/context-menu.js +110 -0
- package/src/overlays/dialog.js +117 -0
- package/src/overlays/dom.js +168 -0
- package/src/overlays/drawer.js +8 -0
- package/src/overlays/index.d.ts +80 -0
- package/src/overlays/index.js +25 -0
- package/src/overlays/kinds.lino +137 -0
- package/src/overlays/list-popover.js +129 -0
- package/src/overlays/machines/index.js +71 -0
- package/src/overlays/machines/surface.lino +11 -0
- package/src/overlays/machines/tabs.lino +7 -0
- package/src/overlays/machines/toast.lino +16 -0
- package/src/overlays/machines/tooltip.lino +29 -0
- package/src/overlays/menu.js +240 -0
- package/src/overlays/popover.js +194 -0
- package/src/overlays/position.js +84 -0
- package/src/overlays/radial-geometry.js +38 -0
- package/src/overlays/radial-menu.js +50 -0
- package/src/overlays/styles.css +155 -0
- package/src/overlays/tabs.js +125 -0
- package/src/overlays/toast-queue.js +105 -0
- package/src/overlays/toast.js +163 -0
- package/src/overlays/tooltip.js +110 -0
- package/src/overlays/vocabulary.js +3 -0
- package/src/settings/conditions.js +44 -0
- package/src/settings/control-for.js +62 -0
- package/src/settings/field-control.js +201 -0
- package/src/settings/index.d.ts +201 -0
- package/src/settings/index.js +3 -0
- package/src/settings/messages.js +112 -0
- package/src/settings/miniature.js +26 -0
- package/src/settings/panel-actions.js +234 -0
- package/src/settings/panel-model.js +49 -0
- package/src/settings/panel-refresh.js +56 -0
- package/src/settings/panel-rows.js +200 -0
- package/src/settings/preset-cards.js +43 -0
- package/src/settings/saved-looks.js +227 -0
- package/src/settings/section-layout.js +95 -0
- package/src/settings/section-tabs.js +175 -0
- package/src/settings/settings-panel.js +192 -0
- package/src/settings/settings.css +178 -0
- package/stories/accordion.lino +14 -0
- package/stories/alert-dialog.lino +4 -0
- package/stories/app-bar.lino +10 -0
- package/stories/avatar.lino +7 -0
- package/stories/badge.lino +12 -0
- package/stories/button.lino +17 -0
- package/stories/card.lino +10 -0
- package/stories/checkbox.lino +14 -0
- package/stories/chip.lino +19 -0
- package/stories/color-field.lino +9 -0
- package/stories/combobox.lino +10 -0
- package/stories/container.lino +11 -0
- package/stories/context-menu.lino +15 -0
- package/stories/dialog.lino +16 -0
- package/stories/drawer.lino +12 -0
- package/stories/icon-button.lino +13 -0
- package/stories/label.lino +7 -0
- package/stories/link-cards.lino +9 -0
- package/stories/link.lino +9 -0
- package/stories/menu-item.lino +6 -0
- package/stories/menu-separator.lino +7 -0
- package/stories/menu.lino +30 -0
- package/stories/nav-strip.lino +11 -0
- package/stories/number-field.lino +16 -0
- package/stories/option.lino +10 -0
- package/stories/paper.lino +11 -0
- package/stories/popover.lino +4 -0
- package/stories/pref-switches.lino +4 -0
- package/stories/progress.lino +14 -0
- package/stories/radial-menu.lino +19 -0
- package/stories/radio-group.lino +22 -0
- package/stories/radio.lino +10 -0
- package/stories/section-nav.lino +9 -0
- package/stories/select.lino +16 -0
- package/stories/slider.lino +14 -0
- package/stories/switch.lino +10 -0
- package/stories/tab-panel.lino +7 -0
- package/stories/tab.lino +7 -0
- package/stories/tabs.lino +25 -0
- package/stories/text-area.lino +12 -0
- package/stories/text-field.lino +23 -0
- package/stories/titled-section.lino +7 -0
- package/stories/toast.lino +3 -0
- package/stories/toaster.lino +2 -0
- package/stories/tooltip.lino +3 -0
package/src/a11y/keys.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key bindings: `bindKeys(element, { Enter: open, 'Shift+Tab': back })`.
|
|
3
|
+
* A key combination is written as the modifiers `Ctrl`, `Alt`, `Shift`,
|
|
4
|
+
* `Meta` (in that order) and the `KeyboardEvent.key`, joined by `+`; the
|
|
5
|
+
* space bar is `Space`. Shift is part of the character for printable keys
|
|
6
|
+
* (`?`, `A`), so it is not written for them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const MODIFIERS = [
|
|
10
|
+
['ctrlKey', 'Ctrl'],
|
|
11
|
+
['altKey', 'Alt'],
|
|
12
|
+
['shiftKey', 'Shift'],
|
|
13
|
+
['metaKey', 'Meta'],
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
/** Modifier names as written (lower case) → their canonical names. */
|
|
17
|
+
const MODIFIER_ALIASES = {
|
|
18
|
+
ctrl: 'Ctrl',
|
|
19
|
+
control: 'Ctrl',
|
|
20
|
+
alt: 'Alt',
|
|
21
|
+
option: 'Alt',
|
|
22
|
+
shift: 'Shift',
|
|
23
|
+
meta: 'Meta',
|
|
24
|
+
cmd: 'Meta',
|
|
25
|
+
command: 'Meta',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Key names as written (lower case) → `KeyboardEvent.key` names. */
|
|
29
|
+
const KEY_ALIASES = {
|
|
30
|
+
space: 'Space',
|
|
31
|
+
spacebar: 'Space',
|
|
32
|
+
esc: 'Escape',
|
|
33
|
+
escape: 'Escape',
|
|
34
|
+
enter: 'Enter',
|
|
35
|
+
return: 'Enter',
|
|
36
|
+
tab: 'Tab',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Whether a key is a printable character (one code point, not a space). */
|
|
40
|
+
const isCharacter = (key) => [...key].length === 1 && key !== ' ';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The combination of a keyboard event: `Enter`, `Space`, `Shift+Tab`,
|
|
44
|
+
* `Ctrl+k`, `a`, `?`.
|
|
45
|
+
* @param {{key: string, ctrlKey?: boolean, altKey?: boolean,
|
|
46
|
+
* shiftKey?: boolean, metaKey?: boolean}} event
|
|
47
|
+
*/
|
|
48
|
+
export function keyOf(event) {
|
|
49
|
+
const key = event.key === ' ' ? 'Space' : event.key;
|
|
50
|
+
const parts = MODIFIERS.filter(
|
|
51
|
+
([flag, name]) => event[flag] && !(name === 'Shift' && isCharacter(key))
|
|
52
|
+
).map(([, name]) => name);
|
|
53
|
+
return [...parts, key].join('+');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A key combination written by hand in its canonical form: modifier order,
|
|
58
|
+
* `Control` → `Ctrl`, `Cmd` → `Meta`, `space` and `' '` → `Space`, `esc` →
|
|
59
|
+
* `Escape` (modifier and key names in any case).
|
|
60
|
+
* @param {string} combo
|
|
61
|
+
*/
|
|
62
|
+
export function normalizeKey(combo) {
|
|
63
|
+
if (combo === ' ' || combo === '+') {
|
|
64
|
+
return combo === ' ' ? 'Space' : '+';
|
|
65
|
+
}
|
|
66
|
+
const names = combo.split('+');
|
|
67
|
+
const key = names.pop();
|
|
68
|
+
const flags = new Set(
|
|
69
|
+
names.map((name) => MODIFIER_ALIASES[name.toLowerCase()] ?? name)
|
|
70
|
+
);
|
|
71
|
+
return keyOf({
|
|
72
|
+
key: key === '' ? '+' : (KEY_ALIASES[key.toLowerCase()] ?? key),
|
|
73
|
+
ctrlKey: flags.has('Ctrl'),
|
|
74
|
+
altKey: flags.has('Alt'),
|
|
75
|
+
shiftKey: flags.has('Shift'),
|
|
76
|
+
metaKey: flags.has('Meta'),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const TEXT_INPUTS = new Set([
|
|
81
|
+
'text',
|
|
82
|
+
'search',
|
|
83
|
+
'email',
|
|
84
|
+
'url',
|
|
85
|
+
'tel',
|
|
86
|
+
'password',
|
|
87
|
+
'number',
|
|
88
|
+
'date',
|
|
89
|
+
'datetime-local',
|
|
90
|
+
'month',
|
|
91
|
+
'time',
|
|
92
|
+
'week',
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Whether an element is editable content: `isContentEditable`, or (where
|
|
97
|
+
* the DOM has no such property, as in jsdom) the closest `contenteditable`
|
|
98
|
+
* attribute.
|
|
99
|
+
*/
|
|
100
|
+
function isEditable(element) {
|
|
101
|
+
if (typeof element.isContentEditable === 'boolean') {
|
|
102
|
+
return element.isContentEditable;
|
|
103
|
+
}
|
|
104
|
+
const value = element
|
|
105
|
+
.closest?.('[contenteditable]')
|
|
106
|
+
?.getAttribute('contenteditable');
|
|
107
|
+
return value !== undefined && value !== null && value !== 'false';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Whether an element takes typed text: a text-like `<input>`, a
|
|
112
|
+
* `<textarea>`, a `<select>` or an editable element.
|
|
113
|
+
* @param {EventTarget|null|undefined} element
|
|
114
|
+
*/
|
|
115
|
+
export function isTextField(element) {
|
|
116
|
+
if (!element || element.nodeType !== 1) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
const tag = element.localName;
|
|
120
|
+
if (tag === 'input') {
|
|
121
|
+
return TEXT_INPUTS.has(
|
|
122
|
+
(element.getAttribute('type') ?? 'text').toLowerCase()
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return tag === 'textarea' || tag === 'select' || isEditable(element);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Keys a text field does not use for typing: they fire there too. */
|
|
129
|
+
const FIELD_SAFE = /^(Escape|F\d{1,2})$/;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Whether a combination may fire while focus is in a text field: only
|
|
133
|
+
* combinations with Ctrl, Alt or Meta, and Escape and the function keys.
|
|
134
|
+
* A plain letter, Space, Enter or an arrow key belongs to the field.
|
|
135
|
+
* @param {string} combo a canonical combination (`keyOf`)
|
|
136
|
+
*/
|
|
137
|
+
export function firesInField(combo) {
|
|
138
|
+
const names = combo === '+' ? ['+'] : combo.split('+');
|
|
139
|
+
const key = names.at(-1) === '' ? '+' : names.at(-1);
|
|
140
|
+
return (
|
|
141
|
+
names.some(
|
|
142
|
+
(name) => name === 'Ctrl' || name === 'Alt' || name === 'Meta'
|
|
143
|
+
) || FIELD_SAFE.test(key)
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Listen for key combinations on an element (and its descendants, as
|
|
149
|
+
* `keydown` bubbles). A handler that returns `false` leaves the event
|
|
150
|
+
* alone; otherwise its default action is prevented.
|
|
151
|
+
* @param {EventTarget} element
|
|
152
|
+
* @param {Record<string, (event: KeyboardEvent) => unknown>} map handlers
|
|
153
|
+
* by combination (`Enter`, `Shift+Tab`, `Ctrl+k`, `Space`)
|
|
154
|
+
* @param {{ignoreInFields?: boolean, type?: 'keydown'|'keyup'}} [options]
|
|
155
|
+
* `ignoreInFields` (default `true`): in a text field only `firesInField`
|
|
156
|
+
* combinations fire, so typing never triggers a binding
|
|
157
|
+
* @returns {() => void} unbind
|
|
158
|
+
*/
|
|
159
|
+
export function bindKeys(element, map, options = {}) {
|
|
160
|
+
const { ignoreInFields = true, type = 'keydown' } = options;
|
|
161
|
+
const handlers = new Map(
|
|
162
|
+
Object.entries(map).map(([combo, fn]) => [normalizeKey(combo), fn])
|
|
163
|
+
);
|
|
164
|
+
const listener = (event) => {
|
|
165
|
+
if (event.defaultPrevented || event.isComposing) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const combo = keyOf(event);
|
|
169
|
+
const handler = handlers.get(combo);
|
|
170
|
+
if (handler === undefined) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (ignoreInFields && isTextField(event.target) && !firesInField(combo)) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (handler(event) !== false) {
|
|
177
|
+
event.preventDefault();
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
element.addEventListener(type, listener);
|
|
181
|
+
return () => element.removeEventListener(type, listener);
|
|
182
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A roving tabindex (WAI-ARIA APG): one item of a composite widget (a
|
|
3
|
+
* toolbar, a tab list, a radio group, a menu) is in the Tab order with
|
|
4
|
+
* `tabindex="0"`, the others have `tabindex="-1"`, and the arrow keys,
|
|
5
|
+
* Home and End move focus between them.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const NEXT = {
|
|
9
|
+
horizontal: ['ArrowRight'],
|
|
10
|
+
vertical: ['ArrowDown'],
|
|
11
|
+
both: ['ArrowRight', 'ArrowDown'],
|
|
12
|
+
};
|
|
13
|
+
const PREVIOUS = {
|
|
14
|
+
horizontal: ['ArrowLeft'],
|
|
15
|
+
vertical: ['ArrowUp'],
|
|
16
|
+
both: ['ArrowLeft', 'ArrowUp'],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Whether an item is skipped by the arrow keys. */
|
|
20
|
+
const isDisabled = (item) =>
|
|
21
|
+
item.disabled === true ||
|
|
22
|
+
item.getAttribute('aria-disabled') === 'true' ||
|
|
23
|
+
item.hidden === true;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {HTMLElement[] | (() => HTMLElement[])} items the items, or a
|
|
27
|
+
* function that returns the current items (for lists that change)
|
|
28
|
+
* @param {{orientation?: 'horizontal'|'vertical'|'both', loop?: boolean,
|
|
29
|
+
* initial?: number, rtl?: boolean,
|
|
30
|
+
* onChange?: (index: number, item: HTMLElement) => void}} [options]
|
|
31
|
+
* `orientation` (default `horizontal`), `loop` (default `true`): moving
|
|
32
|
+
* past the last item wraps to the first; `rtl` swaps Left and Right
|
|
33
|
+
*/
|
|
34
|
+
export function createRovingTabindex(items, options = {}) {
|
|
35
|
+
const { orientation = 'horizontal', loop = true, rtl = false } = options;
|
|
36
|
+
if (!Object.hasOwn(NEXT, orientation)) {
|
|
37
|
+
throw new TypeError(
|
|
38
|
+
`createRovingTabindex: unknown orientation ${JSON.stringify(orientation)}`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
const list = typeof items === 'function' ? items : () => items;
|
|
42
|
+
let index = Math.max(0, options.initial ?? 0);
|
|
43
|
+
const swap = (key) =>
|
|
44
|
+
rtl
|
|
45
|
+
? ({ ArrowRight: 'ArrowLeft', ArrowLeft: 'ArrowRight' }[key] ?? key)
|
|
46
|
+
: key;
|
|
47
|
+
|
|
48
|
+
/** Give the current item tabindex 0 and the others -1. */
|
|
49
|
+
const update = () => {
|
|
50
|
+
const all = list();
|
|
51
|
+
if (index >= all.length) {
|
|
52
|
+
index = Math.max(0, all.length - 1);
|
|
53
|
+
}
|
|
54
|
+
if (all[index] && isDisabled(all[index])) {
|
|
55
|
+
const first = all.findIndex((item) => !isDisabled(item));
|
|
56
|
+
index = first === -1 ? index : first;
|
|
57
|
+
}
|
|
58
|
+
all.forEach((item, i) =>
|
|
59
|
+
item.setAttribute('tabindex', i === index ? '0' : '-1')
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** The next enabled index from `from` in `step` direction, or -1. */
|
|
64
|
+
const seek = (from, step) => {
|
|
65
|
+
const all = list();
|
|
66
|
+
for (let n = 1; n <= all.length; n++) {
|
|
67
|
+
let i = from + step * n;
|
|
68
|
+
if (loop) {
|
|
69
|
+
i = ((i % all.length) + all.length) % all.length;
|
|
70
|
+
} else if (i < 0 || i >= all.length) {
|
|
71
|
+
return -1;
|
|
72
|
+
}
|
|
73
|
+
if (!isDisabled(all[i])) {
|
|
74
|
+
return i;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return -1;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const focus = (next) => {
|
|
81
|
+
const all = list();
|
|
82
|
+
if (next < 0 || next >= all.length) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
index = next;
|
|
86
|
+
update();
|
|
87
|
+
all[index].focus();
|
|
88
|
+
options.onChange?.(index, all[index]);
|
|
89
|
+
return true;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** Handle a keydown; returns whether the key moved focus. */
|
|
93
|
+
const handleKeyDown = (event) => {
|
|
94
|
+
if (event.ctrlKey || event.altKey || event.metaKey) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const key = swap(event.key);
|
|
98
|
+
const all = list();
|
|
99
|
+
let next;
|
|
100
|
+
if (NEXT[orientation].includes(key)) {
|
|
101
|
+
next = seek(index, 1);
|
|
102
|
+
} else if (PREVIOUS[orientation].includes(key)) {
|
|
103
|
+
next = seek(index, -1);
|
|
104
|
+
} else if (key === 'Home') {
|
|
105
|
+
next = all.findIndex((item) => !isDisabled(item));
|
|
106
|
+
} else if (key === 'End') {
|
|
107
|
+
next = all.findLastIndex((item) => !isDisabled(item));
|
|
108
|
+
} else {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
event.preventDefault?.();
|
|
112
|
+
return next !== -1 && focus(next);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** Follow focus that reaches an item another way (a click). */
|
|
116
|
+
const onFocusIn = (event) => {
|
|
117
|
+
const i = list().indexOf(event.target);
|
|
118
|
+
if (i !== -1 && i !== index) {
|
|
119
|
+
index = i;
|
|
120
|
+
update();
|
|
121
|
+
options.onChange?.(index, event.target);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
update();
|
|
126
|
+
return {
|
|
127
|
+
get index() {
|
|
128
|
+
return index;
|
|
129
|
+
},
|
|
130
|
+
get item() {
|
|
131
|
+
return list()[index] ?? null;
|
|
132
|
+
},
|
|
133
|
+
focus,
|
|
134
|
+
/**
|
|
135
|
+
* Make item `i` the tab stop without focusing it (a selection made
|
|
136
|
+
* another way); returns whether `i` is an item.
|
|
137
|
+
*/
|
|
138
|
+
select(i) {
|
|
139
|
+
if (i < 0 || i >= list().length) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
index = i;
|
|
143
|
+
update();
|
|
144
|
+
return true;
|
|
145
|
+
},
|
|
146
|
+
update,
|
|
147
|
+
handleKeyDown,
|
|
148
|
+
/**
|
|
149
|
+
* Listen on a container for keydown and focusin.
|
|
150
|
+
* @param {HTMLElement} container
|
|
151
|
+
* @returns {() => void} detach
|
|
152
|
+
*/
|
|
153
|
+
attach(container) {
|
|
154
|
+
const onKeyDown = (event) => {
|
|
155
|
+
if (list().includes(event.target)) {
|
|
156
|
+
handleKeyDown(event);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
container.addEventListener('keydown', onKeyDown);
|
|
160
|
+
container.addEventListener('focusin', onFocusIn);
|
|
161
|
+
return () => {
|
|
162
|
+
container.removeEventListener('keydown', onKeyDown);
|
|
163
|
+
container.removeEventListener('focusin', onFocusIn);
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typeahead (WAI-ARIA APG): typing characters in a list, a menu or a tree
|
|
3
|
+
* moves to the next item whose label starts with them. Characters typed
|
|
4
|
+
* within `timeout` ms of each other form one query; typing the same
|
|
5
|
+
* character again cycles through the items that start with it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** A clock on the global timers. */
|
|
9
|
+
const globalClock = {
|
|
10
|
+
setTimeout: (fn, ms) => globalThis.setTimeout(fn, ms),
|
|
11
|
+
clearTimeout: (handle) => globalThis.clearTimeout(handle),
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** Whether a key is a character to search for. */
|
|
15
|
+
const isSearchKey = (key) =>
|
|
16
|
+
typeof key === 'string' && [...key].length === 1 && key.trim() !== '';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {() => readonly string[]} getLabels the labels of the items, in
|
|
20
|
+
* order (read at every key, so the list may change)
|
|
21
|
+
* @param {{timeout?: number, clock?: {setTimeout: Function,
|
|
22
|
+
* clearTimeout: Function}, locale?: string}} [options] `timeout`
|
|
23
|
+
* (default 500 ms) ends the query; `clock` is injected in tests
|
|
24
|
+
*/
|
|
25
|
+
export function createTypeahead(getLabels, options = {}) {
|
|
26
|
+
const { timeout = 500, clock = globalClock, locale } = options;
|
|
27
|
+
let query = '';
|
|
28
|
+
let timer = null;
|
|
29
|
+
const fold = (text) => String(text).toLocaleLowerCase(locale).trim();
|
|
30
|
+
|
|
31
|
+
const reset = () => {
|
|
32
|
+
query = '';
|
|
33
|
+
if (timer !== null) {
|
|
34
|
+
clock.clearTimeout(timer);
|
|
35
|
+
timer = null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Add a typed key to the query and find the item to move to.
|
|
41
|
+
* @param {string} key `KeyboardEvent.key`
|
|
42
|
+
* @param {number} [current] the index of the current item (-1: none)
|
|
43
|
+
* @returns {number} the index to move to, or -1 (no match, or the key
|
|
44
|
+
* is not a character: then the query is unchanged)
|
|
45
|
+
*/
|
|
46
|
+
const handleKey = (key, current = -1) => {
|
|
47
|
+
if (!isSearchKey(key)) {
|
|
48
|
+
return -1;
|
|
49
|
+
}
|
|
50
|
+
query += fold(key);
|
|
51
|
+
if (timer !== null) {
|
|
52
|
+
clock.clearTimeout(timer);
|
|
53
|
+
}
|
|
54
|
+
timer = clock.setTimeout(() => {
|
|
55
|
+
timer = null;
|
|
56
|
+
query = '';
|
|
57
|
+
}, timeout);
|
|
58
|
+
|
|
59
|
+
const labels = getLabels().map(fold);
|
|
60
|
+
const repeated = [...query].every((c) => c === query[0]);
|
|
61
|
+
// One character (or one repeated): search after the current item, so
|
|
62
|
+
// pressing it again moves on; a longer query may stay on the current
|
|
63
|
+
// item when it still matches.
|
|
64
|
+
const search = repeated ? query[0] : query;
|
|
65
|
+
const start = repeated ? current + 1 : Math.max(current, 0);
|
|
66
|
+
for (let n = 0; n < labels.length; n++) {
|
|
67
|
+
const i = (start + n) % labels.length;
|
|
68
|
+
if (labels[i].startsWith(search)) {
|
|
69
|
+
return i;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return -1;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
get query() {
|
|
77
|
+
return query;
|
|
78
|
+
},
|
|
79
|
+
handleKey,
|
|
80
|
+
reset,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The actions registry: action names map to host functions. A handler such
|
|
3
|
+
* as `(on press (navigate home))` or a machine step `(do focus-content)`
|
|
4
|
+
* is looked up by name and dispatched with its arguments as data. An
|
|
5
|
+
* unknown name is a diagnostic, never code: nothing is evaluated.
|
|
6
|
+
*
|
|
7
|
+
* The vocabulary actions (docs/spec/vocabulary.lino) have default
|
|
8
|
+
* implementations that validate their arguments and call the `host`
|
|
9
|
+
* function of the same name, so a renderer decides what "navigate" means:
|
|
10
|
+
*
|
|
11
|
+
* | Action | Default |
|
|
12
|
+
* | ------------------------- | --------------------------------------------- |
|
|
13
|
+
* | `(navigate target)` | rejects `javascript:`, `data:`, `vbscript:`; `host.navigate(target)` |
|
|
14
|
+
* | `(emit event)` | `host.emit(event)` |
|
|
15
|
+
* | `(set target prop value)` | `host.set(target, prop, value)` |
|
|
16
|
+
* | `(toggle target prop)` | `host.toggle(target, prop)` |
|
|
17
|
+
* | `(focus target)` | `meta.effects.focus(target)`, else `host.focus(target)` |
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { diagnostic, isUnsafeUrl } from './diagnostics.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {import('./diagnostics.js').diagnostic} DiagnosticFn
|
|
24
|
+
* @typedef {ReturnType<DiagnosticFn>} Diagnostic
|
|
25
|
+
*
|
|
26
|
+
* @typedef {object} ActionMeta what a handler learns besides its
|
|
27
|
+
* arguments; every field is optional
|
|
28
|
+
* @property {string|null} [source] the element (store ID) of the event
|
|
29
|
+
* @property {string} [event] the UI or machine event
|
|
30
|
+
* @property {string} [state] the machine state
|
|
31
|
+
* @property {Readonly<Record<string, unknown>>} [context] the machine
|
|
32
|
+
* context
|
|
33
|
+
* @property {(patch: Record<string, unknown>) => void} [assign] update
|
|
34
|
+
* the machine context
|
|
35
|
+
* @property {Record<string, Function>} [effects] host effects of the
|
|
36
|
+
* bound machine (`focus` from `bindMachine`)
|
|
37
|
+
*
|
|
38
|
+
* @typedef {(args: readonly unknown[], meta: ActionMeta) => unknown} ActionFn
|
|
39
|
+
*
|
|
40
|
+
* @typedef {object} DispatchResult
|
|
41
|
+
* @property {boolean} ok
|
|
42
|
+
* @property {unknown} [value] what the handler returned
|
|
43
|
+
* @property {Diagnostic} [diagnostic] why nothing (or not all) ran
|
|
44
|
+
*
|
|
45
|
+
* @typedef {object} ActionRegistry
|
|
46
|
+
* @property {(name: string, fn: ActionFn) => () => void} register add or
|
|
47
|
+
* replace an action; returns a function that removes it again
|
|
48
|
+
* @property {(name: unknown) => boolean} has
|
|
49
|
+
* @property {() => string[]} names
|
|
50
|
+
* @property {(name: unknown, args?: readonly unknown[],
|
|
51
|
+
* meta?: ActionMeta) => DispatchResult} dispatch
|
|
52
|
+
*
|
|
53
|
+
* @typedef {object} HostActions what the default vocabulary actions call
|
|
54
|
+
* @property {(target: string, meta: ActionMeta) => unknown} [navigate]
|
|
55
|
+
* @property {(event: string, args: readonly unknown[], meta: ActionMeta) => unknown} [emit]
|
|
56
|
+
* @property {(target: string, property: string, value: unknown, meta: ActionMeta) => unknown} [set]
|
|
57
|
+
* @property {(target: string, property: string, meta: ActionMeta) => unknown} [toggle]
|
|
58
|
+
* @property {(target: string, meta: ActionMeta) => unknown} [focus]
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/** A failed dispatch: the diagnostic is also reported. */
|
|
62
|
+
class ActionFailure extends Error {
|
|
63
|
+
constructor(code, at) {
|
|
64
|
+
super(code);
|
|
65
|
+
this.code = code;
|
|
66
|
+
this.at = at;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Fail the current action with a diagnostic (for action handlers). */
|
|
71
|
+
export const fail = (code, at = {}) => {
|
|
72
|
+
throw new ActionFailure(code, at);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const requireText = (value, reason) =>
|
|
76
|
+
typeof value === 'string' && value !== ''
|
|
77
|
+
? value
|
|
78
|
+
: fail('invalid-argument', { reason });
|
|
79
|
+
|
|
80
|
+
/** Call the host function `name`, or report that there is none. */
|
|
81
|
+
function callHost(host, name, args) {
|
|
82
|
+
const fn = host[name];
|
|
83
|
+
return typeof fn === 'function'
|
|
84
|
+
? fn(...args)
|
|
85
|
+
: fail('unhandled-action', { action: name });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The default implementations of the vocabulary actions. */
|
|
89
|
+
function vocabularyActions(host) {
|
|
90
|
+
return {
|
|
91
|
+
navigate([target], meta) {
|
|
92
|
+
requireText(target, 'navigate needs a target');
|
|
93
|
+
if (isUnsafeUrl(target)) {
|
|
94
|
+
fail('unsafe-url', { url: target });
|
|
95
|
+
}
|
|
96
|
+
return callHost(host, 'navigate', [target, meta]);
|
|
97
|
+
},
|
|
98
|
+
emit([event, ...args], meta) {
|
|
99
|
+
requireText(event, 'emit needs an event name');
|
|
100
|
+
return callHost(host, 'emit', [event, args, meta]);
|
|
101
|
+
},
|
|
102
|
+
set([target, property, value], meta) {
|
|
103
|
+
requireText(target, 'set needs a target');
|
|
104
|
+
requireText(property, 'set needs a property');
|
|
105
|
+
return callHost(host, 'set', [target, property, value, meta]);
|
|
106
|
+
},
|
|
107
|
+
toggle([target, property], meta) {
|
|
108
|
+
requireText(target, 'toggle needs a target');
|
|
109
|
+
requireText(property, 'toggle needs a property');
|
|
110
|
+
return callHost(host, 'toggle', [target, property, meta]);
|
|
111
|
+
},
|
|
112
|
+
focus([target], meta) {
|
|
113
|
+
requireText(target, 'focus needs a target');
|
|
114
|
+
const effect = meta.effects?.focus;
|
|
115
|
+
return typeof effect === 'function'
|
|
116
|
+
? effect(target, meta)
|
|
117
|
+
: callHost(host, 'focus', [target, meta]);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The names of the vocabulary actions, which have defaults. */
|
|
123
|
+
export const VOCABULARY_ACTIONS = Object.freeze([
|
|
124
|
+
'navigate',
|
|
125
|
+
'emit',
|
|
126
|
+
'set',
|
|
127
|
+
'toggle',
|
|
128
|
+
'focus',
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Create an actions registry.
|
|
133
|
+
* @param {{onUnknown?: (d: Diagnostic) => void,
|
|
134
|
+
* onDiagnostic?: (d: Diagnostic) => void, host?: HostActions,
|
|
135
|
+
* defaults?: boolean}} [options] `onUnknown` gets `unknown-action`
|
|
136
|
+
* diagnostics, `onDiagnostic` every diagnostic; `defaults: false`
|
|
137
|
+
* leaves out the vocabulary actions
|
|
138
|
+
* @returns {ActionRegistry}
|
|
139
|
+
*/
|
|
140
|
+
export function createActionRegistry(options = {}) {
|
|
141
|
+
/** @type {Map<string, ActionFn>} */
|
|
142
|
+
const actions = new Map();
|
|
143
|
+
if (options.defaults !== false) {
|
|
144
|
+
for (const [name, fn] of Object.entries(
|
|
145
|
+
vocabularyActions(options.host ?? {})
|
|
146
|
+
)) {
|
|
147
|
+
actions.set(name, fn);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const report = (code, severity, at) => {
|
|
152
|
+
const d = diagnostic(code, severity, at);
|
|
153
|
+
if (code === 'unknown-action') {
|
|
154
|
+
options.onUnknown?.(d);
|
|
155
|
+
}
|
|
156
|
+
options.onDiagnostic?.(d);
|
|
157
|
+
return { ok: false, diagnostic: d };
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
return Object.freeze({
|
|
161
|
+
register(name, fn) {
|
|
162
|
+
if (typeof name !== 'string' || name === '') {
|
|
163
|
+
throw new TypeError('register: the name must be a non-empty string');
|
|
164
|
+
}
|
|
165
|
+
if (typeof fn !== 'function') {
|
|
166
|
+
throw new TypeError(`register(${name}): the action must be a function`);
|
|
167
|
+
}
|
|
168
|
+
actions.set(name, fn);
|
|
169
|
+
return () => {
|
|
170
|
+
if (actions.get(name) === fn) {
|
|
171
|
+
actions.delete(name);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
has: (name) => typeof name === 'string' && actions.has(name),
|
|
176
|
+
names: () => [...actions.keys()],
|
|
177
|
+
dispatch(name, args = [], meta = {}) {
|
|
178
|
+
if (typeof name !== 'string' || !actions.has(name)) {
|
|
179
|
+
return report('unknown-action', 'warning', { action: String(name) });
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
return {
|
|
183
|
+
ok: true,
|
|
184
|
+
value: actions.get(name)(Object.freeze([...args]), meta),
|
|
185
|
+
};
|
|
186
|
+
} catch (error) {
|
|
187
|
+
if (error instanceof ActionFailure) {
|
|
188
|
+
return report(error.code, 'error', { action: name, ...error.at });
|
|
189
|
+
}
|
|
190
|
+
return report('action-error', 'error', {
|
|
191
|
+
action: name,
|
|
192
|
+
error: String(error?.message ?? error),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Dispatch the actions of a handler `(on EVENT ACTION...)` given as its
|
|
201
|
+
* values (`['press', ['navigate', 'home']]`). An action that is not a
|
|
202
|
+
* link, such as the string `'alert(1)'`, is looked up as a name too, so it
|
|
203
|
+
* is reported as `unknown-action` and never run.
|
|
204
|
+
* @param {ActionRegistry} registry
|
|
205
|
+
* @param {readonly unknown[]} values the values of the `on` property
|
|
206
|
+
* @param {ActionMeta} [meta]
|
|
207
|
+
* @returns {DispatchResult[]}
|
|
208
|
+
*/
|
|
209
|
+
export function dispatchHandler(registry, values, meta = {}) {
|
|
210
|
+
const [event, ...actions] = values;
|
|
211
|
+
return actions.map((action) =>
|
|
212
|
+
Array.isArray(action)
|
|
213
|
+
? registry.dispatch(action[0], action.slice(1), { event, ...meta })
|
|
214
|
+
: registry.dispatch(action, [], { event, ...meta })
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Check the actions of a handler `(on EVENT ACTION...)` without running
|
|
220
|
+
* any: every action whose name is not registered (a string such as
|
|
221
|
+
* `'alert(1)'` included) is an `unknown-action` diagnostic.
|
|
222
|
+
* @param {ActionRegistry} registry
|
|
223
|
+
* @param {readonly unknown[]} values the values of the `on` property
|
|
224
|
+
* @param {Record<string, unknown>} [at] where the handler is
|
|
225
|
+
* @returns {Diagnostic[]}
|
|
226
|
+
*/
|
|
227
|
+
export function checkHandler(registry, values, at = {}) {
|
|
228
|
+
const [, ...actions] = values;
|
|
229
|
+
return actions
|
|
230
|
+
.map((action) => (Array.isArray(action) ? action[0] : action))
|
|
231
|
+
.filter((name) => !registry.has(name))
|
|
232
|
+
.map((name) =>
|
|
233
|
+
diagnostic('unknown-action', 'warning', { ...at, action: String(name) })
|
|
234
|
+
);
|
|
235
|
+
}
|