@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,106 @@
|
|
|
1
|
+
/** Lexical colouring only. No links, validation or folding are built here. */
|
|
2
|
+
import { stripComments, unescapeReference } from 'links-notation';
|
|
3
|
+
|
|
4
|
+
const space = (char) => char !== undefined && char.trim() === '';
|
|
5
|
+
const quote = (char) => char === "'" || char === '"' || char === '`';
|
|
6
|
+
const boundary = (char) =>
|
|
7
|
+
char === undefined || space(char) || '():'.includes(char) || quote(char);
|
|
8
|
+
|
|
9
|
+
/** A quote-run token, including incomplete input while the user types. */
|
|
10
|
+
function quotedEnd(text, start) {
|
|
11
|
+
const delimiter = text[start];
|
|
12
|
+
let count = 1;
|
|
13
|
+
while (text[start + count] === delimiter) {
|
|
14
|
+
count++;
|
|
15
|
+
}
|
|
16
|
+
let at = start + count;
|
|
17
|
+
while (at < text.length) {
|
|
18
|
+
if (text[at] !== delimiter) {
|
|
19
|
+
at++;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
let run = 1;
|
|
23
|
+
while (text[at + run] === delimiter) {
|
|
24
|
+
run++;
|
|
25
|
+
}
|
|
26
|
+
if (Math.floor(run / count) % 2 === 1) {
|
|
27
|
+
if (count % 2 === 0) {
|
|
28
|
+
try {
|
|
29
|
+
unescapeReference(text.slice(start, at + run));
|
|
30
|
+
} catch {
|
|
31
|
+
return { to: start + count, closed: true };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { to: at + run, closed: true };
|
|
35
|
+
}
|
|
36
|
+
at += run;
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
to: count % 2 === 0 ? start + count : text.length,
|
|
40
|
+
closed: count % 2 === 0,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @param {string} text
|
|
45
|
+
* @returns {{type: 'paren'|'id'|'colon'|'string'|'comment'|'word', from: number, to: number}[]}
|
|
46
|
+
*/
|
|
47
|
+
export function highlightLino(text) {
|
|
48
|
+
return highlightLinoLine(text).tokens;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Internal line tokenizer; carries only an unfinished quote delimiter. */
|
|
52
|
+
export function highlightLinoLine(line, continuation = '') {
|
|
53
|
+
const text = continuation + line;
|
|
54
|
+
let next = '';
|
|
55
|
+
const clean = stripComments(text);
|
|
56
|
+
const tokens = [];
|
|
57
|
+
let at = 0;
|
|
58
|
+
while (at < text.length) {
|
|
59
|
+
const from = at;
|
|
60
|
+
const char = text[at];
|
|
61
|
+
let type;
|
|
62
|
+
if (char !== clean[at]) {
|
|
63
|
+
at = text.indexOf('\n', at);
|
|
64
|
+
if (at === -1) {
|
|
65
|
+
at = text.length;
|
|
66
|
+
}
|
|
67
|
+
type = 'comment';
|
|
68
|
+
} else if (space(char)) {
|
|
69
|
+
at++;
|
|
70
|
+
continue;
|
|
71
|
+
} else if (quote(char)) {
|
|
72
|
+
const quoted = quotedEnd(text, at);
|
|
73
|
+
at = quoted.to;
|
|
74
|
+
if (!quoted.closed) {
|
|
75
|
+
let end = from + 1;
|
|
76
|
+
while (text[end] === char) {
|
|
77
|
+
end++;
|
|
78
|
+
}
|
|
79
|
+
next = text.slice(from, end);
|
|
80
|
+
}
|
|
81
|
+
type = 'string';
|
|
82
|
+
} else if (char === '(' || char === ')') {
|
|
83
|
+
at++;
|
|
84
|
+
type = 'paren';
|
|
85
|
+
} else if (char === ':') {
|
|
86
|
+
at++;
|
|
87
|
+
type = 'colon';
|
|
88
|
+
} else {
|
|
89
|
+
do {
|
|
90
|
+
at++;
|
|
91
|
+
} while (!boundary(text[at]));
|
|
92
|
+
type = text[at] === ':' ? 'id' : 'word';
|
|
93
|
+
}
|
|
94
|
+
tokens.push({ type, from, to: at });
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
tokens: tokens
|
|
98
|
+
.filter((t) => t.to > continuation.length)
|
|
99
|
+
.map((t) => ({
|
|
100
|
+
...t,
|
|
101
|
+
from: Math.max(0, t.from - continuation.length),
|
|
102
|
+
to: t.to - continuation.length,
|
|
103
|
+
})),
|
|
104
|
+
continuation: next,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Parser-confirmed source ranges. Lexical candidates never establish validity. */
|
|
2
|
+
import { Parser } from 'links-notation';
|
|
3
|
+
import { highlightLino } from './lino-highlight.js';
|
|
4
|
+
import { EDITOR_PARSE_OPTIONS } from './validate-lino.js';
|
|
5
|
+
|
|
6
|
+
const accepts = (parser, text) => {
|
|
7
|
+
try {
|
|
8
|
+
return parser.parseGroups(text);
|
|
9
|
+
} catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The parser currently exposes groups without source ranges. Locate candidate
|
|
16
|
+
* delimiters/indented spans, then ask that same parser whether each is a link.
|
|
17
|
+
* Invalid input yields no structural affordances. No parallel LiNo grammar.
|
|
18
|
+
*/
|
|
19
|
+
export function linoStructure(text) {
|
|
20
|
+
const parser = new Parser(EDITOR_PARSE_OPTIONS);
|
|
21
|
+
if (!accepts(parser, text)) {
|
|
22
|
+
return { pairs: [], folds: [] };
|
|
23
|
+
}
|
|
24
|
+
const tokens = highlightLino(text);
|
|
25
|
+
const pairs = [];
|
|
26
|
+
const folds = [];
|
|
27
|
+
const opened = [];
|
|
28
|
+
for (const token of tokens.filter((t) => t.type === 'paren')) {
|
|
29
|
+
if (text[token.from] === '(') {
|
|
30
|
+
opened.push(token.from);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const from = opened.pop();
|
|
34
|
+
if (from !== undefined && accepts(parser, text.slice(from, token.to))) {
|
|
35
|
+
pairs.push({ from, to: token.to });
|
|
36
|
+
if (text.slice(from, token.to).includes('\n')) {
|
|
37
|
+
folds.push({ from: from + 1, to: token.from });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
folds.push(...indentedFolds(text, tokens, parser));
|
|
42
|
+
return { pairs, folds };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function indentedFolds(text, tokens, parser) {
|
|
46
|
+
const folds = [];
|
|
47
|
+
const lines = [];
|
|
48
|
+
let start = 0;
|
|
49
|
+
let tokenIndex = 0;
|
|
50
|
+
for (const line of text.split('\n')) {
|
|
51
|
+
while (tokenIndex < tokens.length && tokens[tokenIndex].to <= start) {
|
|
52
|
+
tokenIndex++;
|
|
53
|
+
}
|
|
54
|
+
const first = tokens[tokenIndex];
|
|
55
|
+
if (
|
|
56
|
+
first &&
|
|
57
|
+
first.from >= start &&
|
|
58
|
+
first.from < start + line.length &&
|
|
59
|
+
first.type !== 'comment'
|
|
60
|
+
) {
|
|
61
|
+
lines.push({
|
|
62
|
+
start,
|
|
63
|
+
end: start + line.length,
|
|
64
|
+
indent: first.from - start,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
start += line.length + 1;
|
|
68
|
+
}
|
|
69
|
+
for (let i = 0; i < lines.length; i++) {
|
|
70
|
+
const head = lines[i];
|
|
71
|
+
let end = i + 1;
|
|
72
|
+
while (end < lines.length && lines[end].indent > head.indent) {
|
|
73
|
+
end++;
|
|
74
|
+
}
|
|
75
|
+
if (end === i + 1) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const last = lines[end - 1];
|
|
79
|
+
const groups = accepts(parser, text.slice(head.start, last.end));
|
|
80
|
+
if (groups?.length === 1 && groups[0].children.length > 0) {
|
|
81
|
+
folds.push({ from: head.end, to: last.end });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return folds;
|
|
85
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Syntax diagnostics from the official LiNo parser; positions are one based. */
|
|
2
|
+
import { Parser } from 'links-notation';
|
|
3
|
+
|
|
4
|
+
export const EDITOR_PARSE_OPTIONS = Object.freeze({
|
|
5
|
+
maxDepth: 64,
|
|
6
|
+
maxInputSize: 1024 * 1024,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
/** @param {string} text
|
|
10
|
+
* @returns {{ok: boolean, diagnostics: {line: number, column: number, message: string, severity: 'error'|'warning'}[]}}
|
|
11
|
+
*/
|
|
12
|
+
export function validateLino(text) {
|
|
13
|
+
try {
|
|
14
|
+
new Parser(EDITOR_PARSE_OPTIONS).parseGroups(text);
|
|
15
|
+
return { ok: true, diagnostics: [] };
|
|
16
|
+
} catch (error) {
|
|
17
|
+
return {
|
|
18
|
+
ok: false,
|
|
19
|
+
diagnostics: [
|
|
20
|
+
{
|
|
21
|
+
line: error.line ?? 1,
|
|
22
|
+
column: error.column ?? 1,
|
|
23
|
+
message: error.message,
|
|
24
|
+
severity: 'error',
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Convert the parser's position to a clamped UTF-16 editor offset. */
|
|
32
|
+
export function diagnosticOffset(text, { line, column }) {
|
|
33
|
+
let start = 0;
|
|
34
|
+
for (let n = 1; n < line; n++) {
|
|
35
|
+
const end = text.indexOf('\n', start);
|
|
36
|
+
if (end === -1) {
|
|
37
|
+
return text.length;
|
|
38
|
+
}
|
|
39
|
+
start = end + 1;
|
|
40
|
+
}
|
|
41
|
+
const end = text.indexOf('\n', start);
|
|
42
|
+
return Math.min(end === -1 ? text.length : end, start + column - 1);
|
|
43
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# Deferred provider posts
|
|
2
|
+
|
|
3
|
+
Import `@link-foundation/ui-components/embed` to register the `embed` DOM kind
|
|
4
|
+
and define `<ui-embed>` in the current browser. Provider adapters are separate
|
|
5
|
+
opt-in modules; importing the main package does not allow a provider or load
|
|
6
|
+
its script. No code or archived content from the unlicensed consumer site is
|
|
7
|
+
included.
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import {
|
|
11
|
+
defineEmbedElement,
|
|
12
|
+
embedVocabulary,
|
|
13
|
+
} from '@link-foundation/ui-components/embed';
|
|
14
|
+
import '@link-foundation/ui-components/embed/providers/x';
|
|
15
|
+
import '@link-foundation/ui-components/embed/providers/telegram';
|
|
16
|
+
import '@link-foundation/ui-components/embed/providers/vk';
|
|
17
|
+
import { mountDocument } from '@link-foundation/ui-dom';
|
|
18
|
+
|
|
19
|
+
// Also call this before rendering in an explicit window, e.g. jsdom.
|
|
20
|
+
defineEmbedElement(window);
|
|
21
|
+
mountDocument(container, text, {
|
|
22
|
+
parseOptions: { vocabulary: embedVocabulary() },
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The opt-in vocabulary extends the core vocabulary; pass another base vocabulary
|
|
27
|
+
to compose extensions. Provider post `id` is independent of the element's stable
|
|
28
|
+
link ID (`post` below).
|
|
29
|
+
|
|
30
|
+
```lino
|
|
31
|
+
post: embed
|
|
32
|
+
provider x
|
|
33
|
+
id 20
|
|
34
|
+
toggle true
|
|
35
|
+
archived: text 'Example archive supplied by the host'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The equivalent custom element accepts one element with `slot="fallback"`:
|
|
39
|
+
|
|
40
|
+
```html
|
|
41
|
+
<ui-embed provider="x" id="20" toggle="true">
|
|
42
|
+
<article slot="fallback"><p>Example archived content</p></article>
|
|
43
|
+
</ui-embed>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Archive children must already be safe DOM: sanitize imported HTML with
|
|
47
|
+
`ui-convert` before inserting it. The embed never interprets an HTML string,
|
|
48
|
+
executes document-supplied code, or relaxes converted HTML's script/iframe
|
|
49
|
+
restrictions. Static HTML rendering includes the archive and no scripts.
|
|
50
|
+
|
|
51
|
+
## Options and lifecycle
|
|
52
|
+
|
|
53
|
+
Properties/attributes: `provider`, `id`, `hash`, `enabled` (default true),
|
|
54
|
+
`toggle` (default false), `root-margin` (default `400px`), `timeout` (default
|
|
55
|
+
12000 milliseconds), `script-timeout` (default 10000 milliseconds), `theme`
|
|
56
|
+
(`light`, `dark`, `system`), `locale` and `debug`. Boolean attributes accept
|
|
57
|
+
`true` and `false`; an empty boolean attribute means true. Prefer `enabled`
|
|
58
|
+
to defer a user's privacy choice indefinitely.
|
|
59
|
+
|
|
60
|
+
Validation happens before requesting a provider script or frame. A missing
|
|
61
|
+
provider, invalid identifier or unsafe adapter URL shows the archive directly.
|
|
62
|
+
Valid posts wait for IntersectionObserver; browsers without it load immediately.
|
|
63
|
+
Script promises are shared per document by URL. Failure, timeout or cancellation
|
|
64
|
+
of the last pending user removes the request from the cache. Successful SDKs
|
|
65
|
+
remain available for subsequent posts. A theme switch retains a shared pending
|
|
66
|
+
request while replacing the widget container.
|
|
67
|
+
|
|
68
|
+
`data-state` is `deferred`, `loading`, `ready`, `fallback` or `off`.
|
|
69
|
+
`data-loaded` appears only when ready. A provider's success callback or an
|
|
70
|
+
iframe's positive rendered height establishes readiness. Iframes receive a
|
|
71
|
+
localized title if they lack one. The readiness deadline covers script loading
|
|
72
|
+
and widget rendering. A failed generation emits one bubbling, composed
|
|
73
|
+
`ui-embed-error` event with `{provider, id, reason}`. Reasons are
|
|
74
|
+
`invalid-provider-or-id`, `timeout`, `provider-error` and `unavailable`.
|
|
75
|
+
|
|
76
|
+
Archived children appear inside a native `<details>` under the live widget.
|
|
77
|
+
Off/failed widgets show them directly. The optional button switches between
|
|
78
|
+
live and archive modes; choosing live starts a fresh generation and clears the
|
|
79
|
+
failure. Theme/locale attribute updates preserve the user's archive choice.
|
|
80
|
+
UI strings default to English or Russian based on the locale.
|
|
81
|
+
|
|
82
|
+
The resolved scheme follows the nearest `data-scheme` and the system's
|
|
83
|
+
`prefers-color-scheme`; locale follows the nearest `lang` or `data-locale`.
|
|
84
|
+
Explicit `theme` and `locale` override inheritance. Changes replace the whole
|
|
85
|
+
provider container. Old promises and callbacks retain their detached container
|
|
86
|
+
and cannot clear or mark the new generation ready. Disconnecting the custom
|
|
87
|
+
element destroys its controller; reconnecting starts another. Destruction
|
|
88
|
+
cancels timers, observers, media listeners, pending script ownership, provider
|
|
89
|
+
cleanup and the widget container. The archived DOM is preserved.
|
|
90
|
+
|
|
91
|
+
`createEmbed(connectedHost, options)` offers the same lifecycle without a custom
|
|
92
|
+
element. It returns `{update(options), retry(), destroy()}`. Its camelCase
|
|
93
|
+
options include `rootMargin`, `scriptTimeout` and optional `labels`
|
|
94
|
+
(`title`, `archive`, `showEmbed`, `showArchive`). `debug: true` logs each state
|
|
95
|
+
transition and fallback reason; an optional `log` callback receives those
|
|
96
|
+
messages. Logging is off by default. `loadEmbedScript(document, url, options)`
|
|
97
|
+
is a lower-level page-owned loader; widgets use disposable ownership internally.
|
|
98
|
+
|
|
99
|
+
## Trusted provider adapters
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
import { registerEmbedProvider } from '@link-foundation/ui-components/embed';
|
|
103
|
+
|
|
104
|
+
const unregister = registerEmbedProvider('example', {
|
|
105
|
+
script: 'https://provider.example/widget.js', // null for a standalone frame
|
|
106
|
+
validate: ({ id }) =>
|
|
107
|
+
typeof id === 'string' && /^[0-9]+$/.test(id.trim()) && id.trim() === id,
|
|
108
|
+
url: ({ id }) => `https://provider.example/post/${encodeURIComponent(id)}`,
|
|
109
|
+
render(container, context) {
|
|
110
|
+
// Trusted application code calls the official provider SDK here.
|
|
111
|
+
// context: id, hash, url, theme, locale, labels, window, signal,
|
|
112
|
+
// ready(), unavailable(). Return a cleanup function, optionally in a promise.
|
|
113
|
+
return context.window.provider.render(container, context);
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Registration is an application allow-list, not a document capability. It accepts
|
|
119
|
+
only HTTPS script URLs without credentials and function hooks. `url` runs only
|
|
120
|
+
after validation, and its result is checked for HTTPS before reaching `render`.
|
|
121
|
+
Adapters should honor `signal`, report unavailable posts, and release any
|
|
122
|
+
resources they create. Container-scoped asynchronous work remains isolated even
|
|
123
|
+
if a provider ignores cancellation. Third-party SDKs may retain their own global
|
|
124
|
+
page state; the component does not modify SDK internals.
|
|
125
|
+
|
|
126
|
+
- **X:** digits only; uses the official `widgets.createTweet` promise with
|
|
127
|
+
theme, language and do-not-track enabled. A missing result means unavailable.
|
|
128
|
+
- **Telegram:** public `channel/number` identifiers; uses the official standalone
|
|
129
|
+
`t.me` post iframe with theme/language parameters, no referrer, and a fixed
|
|
130
|
+
600px viewport after load. The provider frame owns its scripts. There is no
|
|
131
|
+
parent SDK or custom height-message listener. Browser load/error and the
|
|
132
|
+
readiness deadline handle transport failures; same-origin policy prevents
|
|
133
|
+
detecting an unavailable message rendered inside a successfully loaded frame.
|
|
134
|
+
- **VK:** `owner_post` (negative owners allowed), plus a required alphanumeric,
|
|
135
|
+
underscore or hyphen hash; uses the official `VK.Widgets.Post` API and its
|
|
136
|
+
iframe or `onReady` callback for readiness. The container sets `color-scheme`
|
|
137
|
+
and language and forwards theme/locale preferences to the SDK; the SDK controls
|
|
138
|
+
which presentation preferences it supports. The SDK owns resizing. Its
|
|
139
|
+
per-widget RPC endpoint is released on cleanup. As with any cross-origin widget,
|
|
140
|
+
unavailable content can only be reported when the provider exposes a signal.
|
|
141
|
+
|
|
142
|
+
The component never reads cross-origin documents or processes `postMessage`
|
|
143
|
+
height data. Frame title/size observation reads only the embedding DOM.
|
|
144
|
+
|
|
145
|
+
Provider references: [X factory](https://developer.x.com/en/docs/x-for-websites/embedded-tweets/javascript-factory-function),
|
|
146
|
+
[Telegram post widget](https://core.telegram.org/widgets/post),
|
|
147
|
+
[VK post widget](https://dev.vk.com/ru/widgets/post).
|
|
148
|
+
|
|
149
|
+
## Verification
|
|
150
|
+
|
|
151
|
+
Run `node --test packages/ui-components/tests/embed/*.test.js` for deterministic
|
|
152
|
+
manual-clock lifecycle and adapter tests. Run
|
|
153
|
+
`npx playwright test packages/ui-components/e2e/embed.e2e.mjs --workers=1` for
|
|
154
|
+
viewport loading, shared requests, theme races, malicious IDs, timeout/error,
|
|
155
|
+
keyboard controls, iframe titles and axe audits in live/archive/failure states.
|
|
156
|
+
The browser tests mock the provider SDK and do not contact a live social site.
|
|
157
|
+
|
|
158
|
+
Start `node scripts/serve-static.mjs` and open
|
|
159
|
+
`/packages/ui-components/examples/embed.html` for the runnable example; `?far`
|
|
160
|
+
places it below the fold. In a normal browser this example loads the official X
|
|
161
|
+
SDK and falls back if the example post is unavailable.
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { getEmbedProvider, isEmbedUrl } from './registry.js';
|
|
2
|
+
import { acquireEmbedScript } from './script.js';
|
|
3
|
+
import { embedView, embedContext, watchEmbedFrames } from './view.js';
|
|
4
|
+
|
|
5
|
+
const duration = (value, fallback) =>
|
|
6
|
+
Number.isFinite(Number(value)) && Number(value) > 0
|
|
7
|
+
? Number(value)
|
|
8
|
+
: fallback;
|
|
9
|
+
|
|
10
|
+
/** One disposable generation. All continuations retain only this container. */
|
|
11
|
+
function renderGeneration(host, view, options, context, transition) {
|
|
12
|
+
const win = host.ownerDocument.defaultView;
|
|
13
|
+
const container = host.ownerDocument.createElement('div');
|
|
14
|
+
view.live.append(container);
|
|
15
|
+
const abort = new win.AbortController();
|
|
16
|
+
let timer = null,
|
|
17
|
+
stopFrames = () => {},
|
|
18
|
+
release = () => {},
|
|
19
|
+
cleanup = () => {};
|
|
20
|
+
let finished = false;
|
|
21
|
+
const dispose = () => {
|
|
22
|
+
abort.abort();
|
|
23
|
+
win.clearTimeout(timer);
|
|
24
|
+
stopFrames();
|
|
25
|
+
release();
|
|
26
|
+
cleanup();
|
|
27
|
+
cleanup = () => {};
|
|
28
|
+
container.remove();
|
|
29
|
+
};
|
|
30
|
+
const fail = (reason) => {
|
|
31
|
+
if (abort.signal.aborted) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
finished = true;
|
|
35
|
+
dispose();
|
|
36
|
+
transition('fallback', reason);
|
|
37
|
+
host.dispatchEvent(
|
|
38
|
+
new win.CustomEvent('ui-embed-error', {
|
|
39
|
+
bubbles: true,
|
|
40
|
+
composed: true,
|
|
41
|
+
detail: Object.freeze({
|
|
42
|
+
provider: options.provider,
|
|
43
|
+
id: options.id,
|
|
44
|
+
reason,
|
|
45
|
+
}),
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
const ready = () => {
|
|
50
|
+
if (!abort.signal.aborted && !finished) {
|
|
51
|
+
finished = true;
|
|
52
|
+
win.clearTimeout(timer);
|
|
53
|
+
transition('ready');
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const provider = getEmbedProvider(options.provider);
|
|
57
|
+
const post = { id: options.id ?? '', hash: options.hash ?? '' };
|
|
58
|
+
let url;
|
|
59
|
+
try {
|
|
60
|
+
if (!provider || provider.validate(post) !== true) {
|
|
61
|
+
fail('invalid-provider-or-id');
|
|
62
|
+
return dispose;
|
|
63
|
+
}
|
|
64
|
+
url = provider.url(post, context);
|
|
65
|
+
if (!isEmbedUrl(url)) {
|
|
66
|
+
throw new TypeError('Unsafe embed URL');
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
fail('invalid-provider-or-id');
|
|
70
|
+
return dispose;
|
|
71
|
+
}
|
|
72
|
+
transition('loading');
|
|
73
|
+
timer = win.setTimeout(
|
|
74
|
+
() => fail('timeout'),
|
|
75
|
+
duration(options.timeout, 12000)
|
|
76
|
+
);
|
|
77
|
+
stopFrames = watchEmbedFrames(container, context.labels.title, ready);
|
|
78
|
+
const request =
|
|
79
|
+
provider.script === null
|
|
80
|
+
? { promise: Promise.resolve(), release: () => {} }
|
|
81
|
+
: acquireEmbedScript(host.ownerDocument, provider.script, {
|
|
82
|
+
timeout: duration(options.scriptTimeout, 10000),
|
|
83
|
+
});
|
|
84
|
+
release = request.release;
|
|
85
|
+
request.promise
|
|
86
|
+
.then(() => {
|
|
87
|
+
if (abort.signal.aborted) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
return provider.render(container, {
|
|
91
|
+
...post,
|
|
92
|
+
...context,
|
|
93
|
+
url,
|
|
94
|
+
window: win,
|
|
95
|
+
signal: abort.signal,
|
|
96
|
+
ready,
|
|
97
|
+
unavailable: () => fail('unavailable'),
|
|
98
|
+
});
|
|
99
|
+
})
|
|
100
|
+
.then((result) => {
|
|
101
|
+
const stop = typeof result === 'function' ? result : () => {};
|
|
102
|
+
if (abort.signal.aborted) {
|
|
103
|
+
stop();
|
|
104
|
+
} else {
|
|
105
|
+
cleanup = stop;
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
.catch(() => fail('provider-error'));
|
|
109
|
+
return dispose;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function validPost(options) {
|
|
113
|
+
try {
|
|
114
|
+
return (
|
|
115
|
+
getEmbedProvider(options.provider)?.validate({
|
|
116
|
+
id: options.id ?? '',
|
|
117
|
+
hash: options.hash ?? '',
|
|
118
|
+
}) === true
|
|
119
|
+
);
|
|
120
|
+
} catch {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Mount on a connected host. destroy() releases all instance-owned resources. */
|
|
126
|
+
export function createEmbed(host, initial = {}) {
|
|
127
|
+
const win = host.ownerDocument.defaultView;
|
|
128
|
+
const view = embedView(host);
|
|
129
|
+
const media = win.matchMedia?.('(prefers-color-scheme: dark)');
|
|
130
|
+
let options = { ...initial },
|
|
131
|
+
context,
|
|
132
|
+
current,
|
|
133
|
+
state = 'deferred',
|
|
134
|
+
near = false,
|
|
135
|
+
dead = false;
|
|
136
|
+
let intersection;
|
|
137
|
+
const transition = (next, reason) => {
|
|
138
|
+
state = next;
|
|
139
|
+
view.show(state, context.labels, options.toggle === true);
|
|
140
|
+
if (options.debug) {
|
|
141
|
+
(options.log ?? win.console.debug.bind(win.console))('[ui-embed]', {
|
|
142
|
+
state,
|
|
143
|
+
reason,
|
|
144
|
+
provider: options.provider,
|
|
145
|
+
id: options.id,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const refresh = () => {
|
|
150
|
+
const previous = current;
|
|
151
|
+
current = undefined;
|
|
152
|
+
context = embedContext(host, options, media);
|
|
153
|
+
if (options.enabled === false) {
|
|
154
|
+
transition('off');
|
|
155
|
+
} else if (near || !validPost(options)) {
|
|
156
|
+
current = renderGeneration(host, view, options, context, transition);
|
|
157
|
+
} else {
|
|
158
|
+
transition('deferred');
|
|
159
|
+
}
|
|
160
|
+
previous?.();
|
|
161
|
+
};
|
|
162
|
+
const onContext = () => {
|
|
163
|
+
const next = embedContext(host, options, media);
|
|
164
|
+
if (next.theme !== context.theme || next.locale !== context.locale) {
|
|
165
|
+
refresh();
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const observer = new win.MutationObserver(onContext);
|
|
169
|
+
// Watch attributes only: provider DOM mutations never restart a widget.
|
|
170
|
+
observer.observe(host.getRootNode(), {
|
|
171
|
+
subtree: true,
|
|
172
|
+
attributes: true,
|
|
173
|
+
attributeFilter: ['data-scheme', 'lang', 'data-locale'],
|
|
174
|
+
});
|
|
175
|
+
media?.addEventListener('change', onContext);
|
|
176
|
+
const onToggle = () => {
|
|
177
|
+
options.enabled = state === 'fallback' || state === 'off';
|
|
178
|
+
refresh();
|
|
179
|
+
};
|
|
180
|
+
view.button.addEventListener('click', onToggle);
|
|
181
|
+
if (win.IntersectionObserver) {
|
|
182
|
+
intersection = new win.IntersectionObserver(
|
|
183
|
+
(entries) => {
|
|
184
|
+
if (!dead && !near && entries.some((entry) => entry.isIntersecting)) {
|
|
185
|
+
near = true;
|
|
186
|
+
intersection.disconnect();
|
|
187
|
+
refresh();
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
{ rootMargin: options.rootMargin ?? '400px' }
|
|
191
|
+
);
|
|
192
|
+
intersection.observe(host);
|
|
193
|
+
} else {
|
|
194
|
+
near = true;
|
|
195
|
+
}
|
|
196
|
+
refresh();
|
|
197
|
+
return {
|
|
198
|
+
update(patch) {
|
|
199
|
+
if (!dead) {
|
|
200
|
+
options = { ...options, ...patch };
|
|
201
|
+
refresh();
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
retry() {
|
|
205
|
+
if (!dead) {
|
|
206
|
+
options.enabled = true;
|
|
207
|
+
refresh();
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
destroy() {
|
|
211
|
+
if (dead) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
dead = true;
|
|
215
|
+
current?.();
|
|
216
|
+
intersection?.disconnect();
|
|
217
|
+
observer.disconnect();
|
|
218
|
+
media?.removeEventListener('change', onContext);
|
|
219
|
+
view.button.removeEventListener('click', onToggle);
|
|
220
|
+
view.destroy();
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|