@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
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The geometry of a chart: plot area, axes, legend, marks and the points a
|
|
3
|
+
* keyboard user moves between. Pure and deterministic; `svg-renderer.js`
|
|
4
|
+
* writes it as SVG and the `<ui-chart>` element uses its points for the
|
|
5
|
+
* focus ring and the tooltip.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { ACTIVITY_LAYOUTS } from './activity-layout.js';
|
|
9
|
+
import { decorateSeries, roundedBar, emphasizeArcs } from './mark-options.js';
|
|
10
|
+
import {
|
|
11
|
+
arc as d3Arc,
|
|
12
|
+
area as d3Area,
|
|
13
|
+
curveLinear,
|
|
14
|
+
curveMonotoneX,
|
|
15
|
+
curveStepAfter,
|
|
16
|
+
line as d3Line,
|
|
17
|
+
pie as d3Pie,
|
|
18
|
+
} from 'd3-shape';
|
|
19
|
+
import {
|
|
20
|
+
colorScale,
|
|
21
|
+
continuousValue,
|
|
22
|
+
fieldTitle,
|
|
23
|
+
formatValue,
|
|
24
|
+
isDiscrete,
|
|
25
|
+
positionScale,
|
|
26
|
+
sizeScale,
|
|
27
|
+
} from './scales.js';
|
|
28
|
+
import { chartLabel } from './summary.js';
|
|
29
|
+
|
|
30
|
+
const CURVES = {
|
|
31
|
+
linear: curveLinear,
|
|
32
|
+
monotone: curveMonotoneX,
|
|
33
|
+
step: curveStepAfter,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** The default size of a chart, in pixels. */
|
|
37
|
+
export const DEFAULT_WIDTH = 640;
|
|
38
|
+
export const DEFAULT_HEIGHT = 360;
|
|
39
|
+
|
|
40
|
+
const CHAR_WIDTH = 7;
|
|
41
|
+
const TITLE_HEIGHT = 28;
|
|
42
|
+
const LEGEND_HEIGHT = 24;
|
|
43
|
+
|
|
44
|
+
/** A number rounded to hundredths, so the output has no float noise. */
|
|
45
|
+
export const round = (value) => Math.round(value * 100) / 100;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} ChartPoint a value a keyboard user can move to
|
|
49
|
+
* @property {number} series the series index
|
|
50
|
+
* @property {number} index the index of the point in its series
|
|
51
|
+
* @property {number} row the index of the data row
|
|
52
|
+
* @property {number} x the centre, in SVG pixels
|
|
53
|
+
* @property {number} y
|
|
54
|
+
* @property {string} text the tooltip and announcement
|
|
55
|
+
*
|
|
56
|
+
* @typedef {object} ChartSeries
|
|
57
|
+
* @property {string|null} key the colour value, or null for a single series
|
|
58
|
+
* @property {string} color
|
|
59
|
+
* @property {ChartPoint[]} points
|
|
60
|
+
* @property {object[]} shapes the marks: `{tag, attrs}`
|
|
61
|
+
*
|
|
62
|
+
* @typedef {object} ChartLayout
|
|
63
|
+
* @property {number} width
|
|
64
|
+
* @property {number} height
|
|
65
|
+
* @property {string|null} title
|
|
66
|
+
* @property {string} label the accessible name
|
|
67
|
+
* @property {{x: number, y: number, width: number, height: number}} plot
|
|
68
|
+
* @property {object[]} axes
|
|
69
|
+
* @property {{label: string, color: string, x: number, y: number}[]} legend
|
|
70
|
+
* @property {ChartSeries[]} series
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
/** The fields of the tooltip of a chart. */
|
|
74
|
+
export function tooltipFields(spec) {
|
|
75
|
+
const { encoding } = spec;
|
|
76
|
+
if (encoding.tooltip !== undefined) {
|
|
77
|
+
return encoding.tooltip;
|
|
78
|
+
}
|
|
79
|
+
const seen = new Set();
|
|
80
|
+
return ['color', 'x', 'theta', 'y', 'size']
|
|
81
|
+
.map((name) => encoding[name])
|
|
82
|
+
.filter((def) => {
|
|
83
|
+
if (def === undefined || seen.has(def.field)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
seen.add(def.field);
|
|
87
|
+
return true;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The tooltip text of a data row. */
|
|
92
|
+
export const rowText = (fields, row) =>
|
|
93
|
+
fields
|
|
94
|
+
.map((def) => `${fieldTitle(def)}: ${formatValue(def, row[def.field])}`)
|
|
95
|
+
.join(', ');
|
|
96
|
+
|
|
97
|
+
/** The rows of each series, in colour order, keeping each row index. */
|
|
98
|
+
function groupSeries(spec, colors) {
|
|
99
|
+
const { encoding, data } = spec;
|
|
100
|
+
const rows = data.values.map((row, index) => ({ row, index }));
|
|
101
|
+
if (encoding.color === undefined || spec.mark.type === 'arc') {
|
|
102
|
+
return [{ key: null, items: rows }];
|
|
103
|
+
}
|
|
104
|
+
const field = encoding.color.field;
|
|
105
|
+
return colors.domain.map((key) => ({
|
|
106
|
+
key,
|
|
107
|
+
items: rows.filter(({ row }) => String(row[field]) === key),
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Sort the items of a series along a continuous axis. */
|
|
112
|
+
function sortAlong(items, def) {
|
|
113
|
+
if (def === undefined || isDiscrete(def)) {
|
|
114
|
+
return items;
|
|
115
|
+
}
|
|
116
|
+
return [...items].sort(
|
|
117
|
+
(a, b) =>
|
|
118
|
+
continuousValue(def, a.row[def.field]) -
|
|
119
|
+
continuousValue(def, b.row[def.field])
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The longest tick label width estimate, for the left margin. */
|
|
124
|
+
const labelWidth = (ticks) =>
|
|
125
|
+
Math.max(0, ...ticks.map((tick) => tick.label.length)) * CHAR_WIDTH;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Stack the values of the series per category: each item gets the low and
|
|
129
|
+
* high value of its segment. Positive and negative values stack apart.
|
|
130
|
+
*/
|
|
131
|
+
function stack(groups, keyDef, valueDef) {
|
|
132
|
+
const positive = new Map();
|
|
133
|
+
const negative = new Map();
|
|
134
|
+
const totals = [];
|
|
135
|
+
for (const group of groups) {
|
|
136
|
+
for (const item of group.items) {
|
|
137
|
+
const key = String(item.row[keyDef.field]);
|
|
138
|
+
const value = Number(item.row[valueDef.field]);
|
|
139
|
+
const totalsMap = value < 0 ? negative : positive;
|
|
140
|
+
const low = totalsMap.get(key) ?? 0;
|
|
141
|
+
item.low = low;
|
|
142
|
+
item.high = low + value;
|
|
143
|
+
totalsMap.set(key, item.high);
|
|
144
|
+
totals.push(item.high);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return totals;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The orientation of a bar chart: bars along the discrete axis. */
|
|
151
|
+
const isHorizontal = (spec) =>
|
|
152
|
+
spec.mark.type === 'bar' &&
|
|
153
|
+
isDiscrete(spec.encoding.y) &&
|
|
154
|
+
!isDiscrete(spec.encoding.x);
|
|
155
|
+
|
|
156
|
+
/** Whether the values of a mark stack (bars and areas with colours). */
|
|
157
|
+
const isStacked = (spec) =>
|
|
158
|
+
(spec.mark.type === 'bar' || spec.mark.type === 'area') &&
|
|
159
|
+
spec.encoding.color !== undefined;
|
|
160
|
+
|
|
161
|
+
/** The scales of a Cartesian chart, sized to the plot. */
|
|
162
|
+
function cartesianScales(spec, groups, plot) {
|
|
163
|
+
const { x, y } = spec.encoding;
|
|
164
|
+
const rows = spec.data.values;
|
|
165
|
+
const horizontal = isHorizontal(spec);
|
|
166
|
+
const [keyDef, valueDef] = horizontal ? [y, x] : [x, y];
|
|
167
|
+
const filled = spec.mark.type === 'bar' || spec.mark.type === 'area';
|
|
168
|
+
let extent = [];
|
|
169
|
+
if (isStacked(spec)) {
|
|
170
|
+
extent = stack(groups, keyDef, valueDef);
|
|
171
|
+
}
|
|
172
|
+
const keyBand = spec.mark.type === 'bar';
|
|
173
|
+
const pad = spec.mark.type === 'point' ? 0.05 : 0;
|
|
174
|
+
const xScale = positionScale(x, rows, [0, plot.width], {
|
|
175
|
+
band: keyBand && !horizontal,
|
|
176
|
+
zero: filled && horizontal,
|
|
177
|
+
extent: horizontal ? extent : [],
|
|
178
|
+
pad,
|
|
179
|
+
});
|
|
180
|
+
const yScale = positionScale(y, rows, [plot.height, 0], {
|
|
181
|
+
band: keyBand && horizontal,
|
|
182
|
+
zero: filled && !horizontal,
|
|
183
|
+
extent: horizontal ? [] : extent,
|
|
184
|
+
pad,
|
|
185
|
+
});
|
|
186
|
+
return { xScale, yScale, horizontal, keyDef, valueDef };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The axes: ticks, grid lines and titles. */
|
|
190
|
+
function cartesianAxes(spec, scales, plot) {
|
|
191
|
+
const xTicks = scales.xScale.ticks(Math.max(2, Math.floor(plot.width / 90)));
|
|
192
|
+
const yTicks = scales.yScale.ticks(Math.max(2, Math.floor(plot.height / 50)));
|
|
193
|
+
return [
|
|
194
|
+
{
|
|
195
|
+
orient: 'bottom',
|
|
196
|
+
title: fieldTitle(spec.encoding.x),
|
|
197
|
+
ticks: xTicks,
|
|
198
|
+
grid: scales.xScale.kind === 'continuous' && scales.horizontal,
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
orient: 'left',
|
|
202
|
+
title: fieldTitle(spec.encoding.y),
|
|
203
|
+
ticks: yTicks,
|
|
204
|
+
grid: scales.yScale.kind === 'continuous' && !scales.horizontal,
|
|
205
|
+
},
|
|
206
|
+
];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** The plot area left after the title, legend and axes. */
|
|
210
|
+
function plotArea(spec, width, height, legendRows, yLabelWidth) {
|
|
211
|
+
const top = (spec.title ? TITLE_HEIGHT : 8) + legendRows * LEGEND_HEIGHT + 8;
|
|
212
|
+
const left = 20 + yLabelWidth + 12;
|
|
213
|
+
const bottom = 46;
|
|
214
|
+
const right = 20;
|
|
215
|
+
return {
|
|
216
|
+
x: left,
|
|
217
|
+
y: top,
|
|
218
|
+
width: Math.max(40, width - left - right),
|
|
219
|
+
height: Math.max(40, height - top - bottom),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The shapes and points of a bar series. */
|
|
224
|
+
function barShapes(group, scales, color, fields, spec) {
|
|
225
|
+
const { xScale, yScale, horizontal, keyDef, valueDef } = scales;
|
|
226
|
+
const keyScale = horizontal ? yScale : xScale;
|
|
227
|
+
const valueScale = horizontal ? xScale : yScale;
|
|
228
|
+
const shapes = [];
|
|
229
|
+
const points = [];
|
|
230
|
+
for (const item of group.items) {
|
|
231
|
+
const key = item.row[keyDef.field];
|
|
232
|
+
const value = Number(item.row[valueDef.field]);
|
|
233
|
+
const low = item.low ?? 0;
|
|
234
|
+
const high = item.high ?? value;
|
|
235
|
+
const center = keyScale.position(key);
|
|
236
|
+
const a = valueScale.continuous(low);
|
|
237
|
+
const b = valueScale.continuous(high);
|
|
238
|
+
const half = keyScale.bandwidth / 2;
|
|
239
|
+
const attrs = horizontal
|
|
240
|
+
? {
|
|
241
|
+
x: Math.min(a, b),
|
|
242
|
+
y: center - half,
|
|
243
|
+
width: Math.abs(b - a),
|
|
244
|
+
height: half * 2,
|
|
245
|
+
}
|
|
246
|
+
: {
|
|
247
|
+
x: center - half,
|
|
248
|
+
y: Math.min(a, b),
|
|
249
|
+
width: half * 2,
|
|
250
|
+
height: Math.abs(b - a),
|
|
251
|
+
};
|
|
252
|
+
shapes.push(
|
|
253
|
+
spec.cornerRadius
|
|
254
|
+
? roundedBar(
|
|
255
|
+
attrs,
|
|
256
|
+
spec.cornerRadius,
|
|
257
|
+
horizontal,
|
|
258
|
+
b < a,
|
|
259
|
+
color,
|
|
260
|
+
item.index
|
|
261
|
+
)
|
|
262
|
+
: { tag: 'rect', attrs: { ...attrs, fill: color }, row: item.index }
|
|
263
|
+
);
|
|
264
|
+
points.push({
|
|
265
|
+
row: item.index,
|
|
266
|
+
x: horizontal ? (a + b) / 2 : center,
|
|
267
|
+
y: horizontal ? center - half : Math.min(a, b),
|
|
268
|
+
text: rowText(fields, item.row),
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return { shapes, points };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** The shapes and points of a line, area or point series. */
|
|
275
|
+
function seriesShapes(spec, group, scales, color, fields) {
|
|
276
|
+
const { x, y } = spec.encoding;
|
|
277
|
+
const { xScale, yScale } = scales;
|
|
278
|
+
const mark = spec.mark;
|
|
279
|
+
const items = sortAlong(group.items, x);
|
|
280
|
+
const radius = sizeScale(spec.encoding.size, spec.data.values);
|
|
281
|
+
const points = items.map((item) => ({
|
|
282
|
+
row: item.index,
|
|
283
|
+
x: xScale.position(item.row[x.field]),
|
|
284
|
+
y:
|
|
285
|
+
item.high === undefined
|
|
286
|
+
? yScale.position(item.row[y.field])
|
|
287
|
+
: yScale.continuous(item.high),
|
|
288
|
+
low: item.low === undefined ? null : yScale.continuous(item.low),
|
|
289
|
+
radius: spec.encoding.size
|
|
290
|
+
? radius(item.row[spec.encoding.size.field])
|
|
291
|
+
: 3.5,
|
|
292
|
+
text: rowText(fields, item.row),
|
|
293
|
+
}));
|
|
294
|
+
const shapes = [];
|
|
295
|
+
const curve = CURVES[mark.interpolate] ?? curveLinear;
|
|
296
|
+
if (mark.type === 'area') {
|
|
297
|
+
const baseline = yScale.continuous(0);
|
|
298
|
+
const path = d3Area()
|
|
299
|
+
.x((p) => p.x)
|
|
300
|
+
.y0((p) => p.low ?? baseline)
|
|
301
|
+
.y1((p) => p.y)
|
|
302
|
+
.curve(curve)
|
|
303
|
+
.digits(2);
|
|
304
|
+
shapes.push({
|
|
305
|
+
tag: 'path',
|
|
306
|
+
attrs: {
|
|
307
|
+
d: path(points),
|
|
308
|
+
fill: color,
|
|
309
|
+
'fill-opacity': 0.35,
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
if (mark.type === 'line' || mark.type === 'area') {
|
|
314
|
+
const path = d3Line()
|
|
315
|
+
.x((p) => p.x)
|
|
316
|
+
.y((p) => p.y)
|
|
317
|
+
.curve(curve)
|
|
318
|
+
.digits(2);
|
|
319
|
+
shapes.push({
|
|
320
|
+
tag: 'path',
|
|
321
|
+
attrs: {
|
|
322
|
+
d: path(points),
|
|
323
|
+
fill: 'none',
|
|
324
|
+
stroke: color,
|
|
325
|
+
'stroke-width': 2,
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
if (mark.type === 'point' || mark.point) {
|
|
330
|
+
for (const p of points) {
|
|
331
|
+
shapes.push({
|
|
332
|
+
tag: 'circle',
|
|
333
|
+
attrs: {
|
|
334
|
+
cx: p.x,
|
|
335
|
+
cy: p.y,
|
|
336
|
+
r: p.radius,
|
|
337
|
+
fill: color,
|
|
338
|
+
'fill-opacity': mark.type === 'point' ? 0.75 : 1,
|
|
339
|
+
stroke: mark.type === 'point' ? color : 'none',
|
|
340
|
+
},
|
|
341
|
+
row: p.row,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return {
|
|
346
|
+
shapes,
|
|
347
|
+
points: points.map(({ row, x: px, y: py, text }) => ({
|
|
348
|
+
row,
|
|
349
|
+
x: px,
|
|
350
|
+
y: py,
|
|
351
|
+
text,
|
|
352
|
+
})),
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The legend entries of a colour scale, in rows across the top. */
|
|
357
|
+
function topLegend(colors, spec, width) {
|
|
358
|
+
const entries = [];
|
|
359
|
+
let x = 20;
|
|
360
|
+
let row = 0;
|
|
361
|
+
const top = spec.title ? TITLE_HEIGHT : 8;
|
|
362
|
+
colors.domain.forEach((label, i) => {
|
|
363
|
+
const entryWidth = 24 + label.length * CHAR_WIDTH + 16;
|
|
364
|
+
if (x + entryWidth > width - 20 && x > 20) {
|
|
365
|
+
x = 20;
|
|
366
|
+
row += 1;
|
|
367
|
+
}
|
|
368
|
+
entries.push({
|
|
369
|
+
label,
|
|
370
|
+
color: colors.colorAt(i),
|
|
371
|
+
x,
|
|
372
|
+
y: top + row * LEGEND_HEIGHT + 12,
|
|
373
|
+
});
|
|
374
|
+
x += entryWidth;
|
|
375
|
+
});
|
|
376
|
+
return { entries, rows: colors.domain.length === 0 ? 0 : row + 1 };
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** The layout of a Cartesian chart (line, area, bar, point). */
|
|
380
|
+
function cartesianLayout(spec, width, height, tokens) {
|
|
381
|
+
const rows = spec.data.values;
|
|
382
|
+
const colors = colorScale(spec.encoding.color, rows, tokens);
|
|
383
|
+
const legend = topLegend(colors, spec, width);
|
|
384
|
+
const groups = groupSeries(spec, colors);
|
|
385
|
+
const fields = tooltipFields(spec);
|
|
386
|
+
// Size the left margin with the y labels of a first pass.
|
|
387
|
+
const draft = plotArea(spec, width, height, legend.rows, 0);
|
|
388
|
+
const draftScales = cartesianScales(spec, groupSeries(spec, colors), draft);
|
|
389
|
+
const yLabels = labelWidth(cartesianAxes(spec, draftScales, draft)[1].ticks);
|
|
390
|
+
const plot = plotArea(spec, width, height, legend.rows, yLabels);
|
|
391
|
+
const scales = cartesianScales(spec, groups, plot);
|
|
392
|
+
const series = groups.map((group, i) => {
|
|
393
|
+
const color = group.key === null ? colors.colorAt(0) : colors.colorAt(i);
|
|
394
|
+
const { shapes, points } =
|
|
395
|
+
spec.mark.type === 'bar'
|
|
396
|
+
? barShapes(group, scales, color, fields, spec)
|
|
397
|
+
: seriesShapes(spec, group, scales, color, fields);
|
|
398
|
+
decorateSeries(spec, shapes, points);
|
|
399
|
+
return { key: group.key, color, shapes, points };
|
|
400
|
+
});
|
|
401
|
+
return {
|
|
402
|
+
plot,
|
|
403
|
+
axes: cartesianAxes(spec, scales, plot),
|
|
404
|
+
legend: legend.entries,
|
|
405
|
+
series,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** The layout of an arc (pie or donut) chart, legend on the right. */
|
|
410
|
+
function arcLayout(spec, width, height, tokens) {
|
|
411
|
+
const rows = spec.data.values;
|
|
412
|
+
const { theta, color } = spec.encoding;
|
|
413
|
+
const colors = colorScale(color, rows, tokens);
|
|
414
|
+
const fields = tooltipFields(spec);
|
|
415
|
+
const top = (spec.title ? TITLE_HEIGHT : 8) + 8;
|
|
416
|
+
const labels = rows.map((row, i) =>
|
|
417
|
+
color ? String(row[color.field]) : String(i + 1)
|
|
418
|
+
);
|
|
419
|
+
const legendWidth =
|
|
420
|
+
40 + Math.max(...labels.map((l) => l.length)) * CHAR_WIDTH;
|
|
421
|
+
const plot = {
|
|
422
|
+
x: 20,
|
|
423
|
+
y: top,
|
|
424
|
+
width: Math.max(40, width - 40 - legendWidth),
|
|
425
|
+
height: Math.max(40, height - top - 16),
|
|
426
|
+
};
|
|
427
|
+
const radius = Math.min(plot.width, plot.height) / 2;
|
|
428
|
+
const inner = Math.min(spec.mark.innerRadius, radius - 8);
|
|
429
|
+
const cx = plot.width / 2;
|
|
430
|
+
const cy = plot.height / 2;
|
|
431
|
+
const arcs = d3Pie()
|
|
432
|
+
.value((row) => Math.max(0, Number(row[theta.field])))
|
|
433
|
+
.sort(null)(rows);
|
|
434
|
+
const path = d3Arc()
|
|
435
|
+
.innerRadius(Math.max(0, inner))
|
|
436
|
+
.outerRadius(radius)
|
|
437
|
+
.digits(2);
|
|
438
|
+
const colorOf = (row, i) =>
|
|
439
|
+
color ? colors.color(row[color.field]) : colors.colorAt(i);
|
|
440
|
+
const shapes = arcs.map((slice, i) => ({
|
|
441
|
+
tag: 'path',
|
|
442
|
+
attrs: {
|
|
443
|
+
d: path(slice),
|
|
444
|
+
transform: `translate(${round(cx)},${round(cy)})`,
|
|
445
|
+
fill: colorOf(slice.data, i),
|
|
446
|
+
stroke: 'var(--color-chart-background, #ffffff)',
|
|
447
|
+
'stroke-width': 1,
|
|
448
|
+
},
|
|
449
|
+
row: i,
|
|
450
|
+
}));
|
|
451
|
+
const points = arcs.map((slice, i) => {
|
|
452
|
+
const [px, py] = path.centroid(slice);
|
|
453
|
+
return {
|
|
454
|
+
row: i,
|
|
455
|
+
x: cx + px,
|
|
456
|
+
y: cy + py,
|
|
457
|
+
text: rowText(fields, slice.data),
|
|
458
|
+
};
|
|
459
|
+
});
|
|
460
|
+
emphasizeArcs(spec, shapes, points);
|
|
461
|
+
const seen = new Set();
|
|
462
|
+
const legend = [];
|
|
463
|
+
labels.forEach((label, i) => {
|
|
464
|
+
if (!seen.has(label)) {
|
|
465
|
+
seen.add(label);
|
|
466
|
+
legend.push({
|
|
467
|
+
label,
|
|
468
|
+
color: colorOf(rows[i], i),
|
|
469
|
+
x: plot.x + plot.width + 20,
|
|
470
|
+
y: top + 12 + legend.length * LEGEND_HEIGHT,
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
return {
|
|
475
|
+
plot,
|
|
476
|
+
axes: [],
|
|
477
|
+
legend,
|
|
478
|
+
series: [{ key: null, color: colors.colorAt(0), shapes, points }],
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* The layout of a chart spec.
|
|
484
|
+
* @param {import('./chart-model.js').ChartSpec} spec
|
|
485
|
+
* @param {object} [options]
|
|
486
|
+
* @param {number} [options.width]
|
|
487
|
+
* @param {number} [options.height]
|
|
488
|
+
* @param {Record<string, string>|Map<string, string>|null} [options.tokens]
|
|
489
|
+
* @returns {ChartLayout}
|
|
490
|
+
*/
|
|
491
|
+
export function layoutChart(spec, options = {}) {
|
|
492
|
+
const width = options.width ?? spec.width ?? DEFAULT_WIDTH;
|
|
493
|
+
const height = options.height ?? spec.height ?? DEFAULT_HEIGHT;
|
|
494
|
+
const tokens = options.tokens ?? null;
|
|
495
|
+
const body = ACTIVITY_LAYOUTS[spec.mark.type]
|
|
496
|
+
? ACTIVITY_LAYOUTS[spec.mark.type](spec, width, height, tokens)
|
|
497
|
+
: spec.mark.type === 'arc'
|
|
498
|
+
? arcLayout(spec, width, height, tokens)
|
|
499
|
+
: cartesianLayout(spec, width, height, tokens);
|
|
500
|
+
const series = body.series.map((item, s) => ({
|
|
501
|
+
...item,
|
|
502
|
+
points: item.points.map((p, index) => ({
|
|
503
|
+
...p,
|
|
504
|
+
datum: p.datum ?? spec.data.values[p.row],
|
|
505
|
+
series: s,
|
|
506
|
+
index,
|
|
507
|
+
row: p.row,
|
|
508
|
+
x: round(body.plot.x + p.x),
|
|
509
|
+
y: round(body.plot.y + p.y),
|
|
510
|
+
text: p.text,
|
|
511
|
+
})),
|
|
512
|
+
}));
|
|
513
|
+
return {
|
|
514
|
+
width: body.width ?? width,
|
|
515
|
+
height: body.height ?? height,
|
|
516
|
+
weeks: body.weeks,
|
|
517
|
+
accessibleMarks: ['calendar', 'range'].includes(spec.mark.type),
|
|
518
|
+
diagnostics: Object.entries(spec.encoding).flatMap(([channel, def]) =>
|
|
519
|
+
def.scale?.type === 'log'
|
|
520
|
+
? spec.data.values.flatMap((row, index) =>
|
|
521
|
+
Number(row[def.field]) <= 0
|
|
522
|
+
? [
|
|
523
|
+
{
|
|
524
|
+
code: 'log-clamped',
|
|
525
|
+
channel,
|
|
526
|
+
row: index,
|
|
527
|
+
value: row[def.field],
|
|
528
|
+
message: `${def.field}: nonpositive value ${row[def.field]} clamped to the positive log domain`,
|
|
529
|
+
},
|
|
530
|
+
]
|
|
531
|
+
: []
|
|
532
|
+
)
|
|
533
|
+
: []
|
|
534
|
+
),
|
|
535
|
+
title: spec.title,
|
|
536
|
+
label: chartLabel(spec),
|
|
537
|
+
plot: body.plot,
|
|
538
|
+
axes: body.axes,
|
|
539
|
+
legend: body.legend,
|
|
540
|
+
series,
|
|
541
|
+
};
|
|
542
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Links Notation helpers of the chart dialect.
|
|
3
|
+
*
|
|
4
|
+
* Text is read only by the official `links-notation` parser. It returns a
|
|
5
|
+
* link written over several lines as path links: the multi-line form
|
|
6
|
+
* `(H\n a\n b)` gives `(H (H a) (H b))`, and the indented form gives `H`,
|
|
7
|
+
* `(H a)`, `(H b)` as separate top-level links. `unfoldLinks` turns every
|
|
8
|
+
* form back into the nested tree the author wrote, as ui-core path folding
|
|
9
|
+
* does for UI documents (docs/spec/projection.md), so the one-line,
|
|
10
|
+
* multi-line and indented forms of a chart give the same spec.
|
|
11
|
+
*
|
|
12
|
+
* Text is written only through links-notation `escapeReference` (atoms) and
|
|
13
|
+
* `Link#format` (one-line links), and through ui-core `formatLinks` (the
|
|
14
|
+
* nested form, the default for written charts).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { formatLinks, parseNested } from '@link-foundation/ui-core';
|
|
18
|
+
import { Link, escapeReference } from 'links-notation';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A link as the chart reader sees it: an atom has no values.
|
|
22
|
+
* @typedef {{ id: string|null, values: Node[] }} Node
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** Whether two links (or nodes) are the same tree. */
|
|
26
|
+
export function sameLink(a, b) {
|
|
27
|
+
if (a.id !== b.id || a.values.length !== b.values.length) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return a.values.every((value, i) => sameLink(value, b.values[i]));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A head of a multi-line or indented group: a link with values. */
|
|
34
|
+
const isHead = (link) => link.values.length > 0;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether `link` is a multi-line group `(H (H a) (H b) ...)`: no id, a head
|
|
38
|
+
* `H` that is a link (not an atom), and every other value `(H x)`.
|
|
39
|
+
*/
|
|
40
|
+
function isGroup(link) {
|
|
41
|
+
if (link.id !== null || link.values.length < 2 || !isHead(link.values[0])) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const [head, ...rest] = link.values;
|
|
45
|
+
return rest.every(
|
|
46
|
+
(value) =>
|
|
47
|
+
value.id === null &&
|
|
48
|
+
value.values.length === 2 &&
|
|
49
|
+
sameLink(value.values[0], head)
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The nested tree of one link: multi-line groups are folded into their head.
|
|
55
|
+
* @param {Link|Node} link
|
|
56
|
+
* @returns {Node}
|
|
57
|
+
*/
|
|
58
|
+
export function unfoldLink(link) {
|
|
59
|
+
const values = link.values ?? [];
|
|
60
|
+
if (isGroup(link)) {
|
|
61
|
+
const head = unfoldLink(values[0]);
|
|
62
|
+
return {
|
|
63
|
+
id: head.id,
|
|
64
|
+
values: [...head.values, ...values.slice(1).map((v) => unfold(v))],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return { id: link.id ?? null, values: values.map(unfoldLink) };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The payload of a group member `(H x)`. */
|
|
71
|
+
const unfold = (member) => unfoldLink(member.values[1]);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The nested trees of the top-level links. An indented child `(H x)` whose
|
|
75
|
+
* head `H` is an earlier top-level link is appended to that link.
|
|
76
|
+
* @param {readonly (Link|Node)[]} links
|
|
77
|
+
* @returns {Node[]}
|
|
78
|
+
*/
|
|
79
|
+
export function unfoldLinks(links) {
|
|
80
|
+
const roots = [];
|
|
81
|
+
const heads = [];
|
|
82
|
+
for (const link of links) {
|
|
83
|
+
const values = link.values ?? [];
|
|
84
|
+
const owner =
|
|
85
|
+
link.id === null && values.length === 2
|
|
86
|
+
? heads.find(({ source }) => sameLink(source, values[0]))
|
|
87
|
+
: undefined;
|
|
88
|
+
if (owner !== undefined) {
|
|
89
|
+
owner.node.values.push(unfoldLink(values[1]));
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const node = unfoldLink(link);
|
|
93
|
+
roots.push(node);
|
|
94
|
+
if (isHead(link)) {
|
|
95
|
+
heads.push({ source: link, node });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return roots;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Parse LiNo text with the official parser; indented lines are values of
|
|
103
|
+
* the line above them (the nested form).
|
|
104
|
+
* @param {string} text
|
|
105
|
+
* @returns {Link[]}
|
|
106
|
+
*/
|
|
107
|
+
export const parseLino = (text) => parseNested(text);
|
|
108
|
+
|
|
109
|
+
/** Whether a node is an atom (a reference without values). */
|
|
110
|
+
export const isAtom = (node) => node.values.length === 0 && node.id !== null;
|
|
111
|
+
|
|
112
|
+
/** The name of a property link `(name ...)`, or `null`. */
|
|
113
|
+
export function headName(node) {
|
|
114
|
+
if (node.id !== null || node.values.length === 0) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
return isAtom(node.values[0]) ? node.values[0].id : null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** A plain node of an atom. */
|
|
121
|
+
export const atomNode = (text) => ({ id: String(text), values: [] });
|
|
122
|
+
|
|
123
|
+
/** A property node `(name value...)`; values are nodes or atom texts. */
|
|
124
|
+
export const prop = (name, ...values) => ({
|
|
125
|
+
id: null,
|
|
126
|
+
values: [
|
|
127
|
+
atomNode(name),
|
|
128
|
+
...values.map((v) => (typeof v === 'object' ? v : atomNode(v))),
|
|
129
|
+
],
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
/** The links-notation `Link` of a node. */
|
|
133
|
+
export const toLink = (node) =>
|
|
134
|
+
isAtom(node)
|
|
135
|
+
? new Link(node.id)
|
|
136
|
+
: new Link(
|
|
137
|
+
node.id,
|
|
138
|
+
node.values.map((v) => toLink(v))
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
/** One-line LiNo text of a node (links-notation `Link#format`). */
|
|
142
|
+
export function formatNode(node) {
|
|
143
|
+
if (isAtom(node)) {
|
|
144
|
+
return escapeReference(node.id);
|
|
145
|
+
}
|
|
146
|
+
return toLink(node).format();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* LiNo text of a node in the nested form (#133): the head on the first line
|
|
151
|
+
* and every value that is itself a link on an indented line of its own,
|
|
152
|
+
* written by ui-core `formatLinks`.
|
|
153
|
+
* @param {Node} node
|
|
154
|
+
* @returns {string} the text, ending with a line break
|
|
155
|
+
*/
|
|
156
|
+
export function formatNested(node) {
|
|
157
|
+
return formatLinks([isAtom(node) ? node.id : toLink(node)]);
|
|
158
|
+
}
|