paseo-plugin-genui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/OVERVIEW.md +21 -0
- package/README.md +77 -2
- package/assets/fonts/Excalifont-OFL.txt +109 -0
- package/assets/fonts/Excalifont-Regular-latin.woff2 +0 -0
- package/assets/icon.png +0 -0
- package/assets/icon.svg +1 -0
- package/assets/screenshot-chat.png +0 -0
- package/assets/screenshot-gallery.png +0 -0
- package/assets/screenshot-playground.png +0 -0
- package/client/components/content.tsx +468 -0
- package/client/components/data.tsx +967 -0
- package/client/components/dev.tsx +998 -0
- package/client/components/diagram.tsx +513 -0
- package/client/components/inputs.tsx +994 -0
- package/client/components/layout.tsx +239 -0
- package/client/components/media.tsx +428 -0
- package/client/components/project.tsx +641 -0
- package/client/components/widgets.tsx +836 -0
- package/client/gallery.tsx +449 -0
- package/client/live-ui.tsx +525 -0
- package/client/loaders.ts +342 -0
- package/client/markdown.tsx +172 -0
- package/client/node-renderer.tsx +149 -0
- package/client/render.tsx +186 -0
- package/client/runtime.ts +103 -0
- package/client/settings.tsx +57 -0
- package/client/web.ts +186 -0
- package/gallery/Acceptance-baseline.ui +1 -0
- package/gallery/Acceptance-checklist.ui +14 -0
- package/gallery/Acceptance-evidence.ui +9 -0
- package/gallery/Acceptance.ui +1 -0
- package/gallery/Badge.ui +3 -0
- package/gallery/Benchmark-context.ui +8 -0
- package/gallery/Benchmark-hyperfine.ui +1 -0
- package/gallery/Benchmark-runs-list.ui +9 -0
- package/gallery/Benchmark-runs.ui +1 -0
- package/gallery/Benchmark-vitest.ui +1 -0
- package/gallery/Benchmark.ui +9 -0
- package/gallery/Board-bb-tasks.ui +1 -0
- package/gallery/Board-gh-issues.ui +1 -0
- package/gallery/Board-gh-prs.ui +1 -0
- package/gallery/Board.ui +12 -0
- package/gallery/Button-destructive.ui +4 -0
- package/gallery/Button.ui +5 -0
- package/gallery/Calendar.ui +15 -0
- package/gallery/CalendarHeatmap-short.ui +11 -0
- package/gallery/CalendarHeatmap.ui +4 -0
- package/gallery/Callout.ui +4 -0
- package/gallery/Card.ui +4 -0
- package/gallery/Carousel.ui +8 -0
- package/gallery/Changes-paths.ui +1 -0
- package/gallery/Changes-uncommitted.ui +1 -0
- package/gallery/Changes.ui +1 -0
- package/gallery/Chart-area.ui +3 -0
- package/gallery/Chart-funnel.ui +3 -0
- package/gallery/Chart-heatmap.ui +11 -0
- package/gallery/Chart-pie.ui +3 -0
- package/gallery/Chart-radar.ui +3 -0
- package/gallery/Chart-scatter.ui +4 -0
- package/gallery/Chart-stacked.ui +5 -0
- package/gallery/Chart.ui +3 -0
- package/gallery/Checklist.ui +6 -0
- package/gallery/Choice.ui +8 -0
- package/gallery/Code.ui +3 -0
- package/gallery/Commits-limit.ui +1 -0
- package/gallery/Commits.ui +1 -0
- package/gallery/Compare-conditions.ui +4 -0
- package/gallery/Compare-three.ui +7 -0
- package/gallery/Compare.ui +4 -0
- package/gallery/Coverage-go.ui +1 -0
- package/gallery/Coverage-lcov.ui +1 -0
- package/gallery/Coverage-python.ui +1 -0
- package/gallery/Coverage.ui +1 -0
- package/gallery/DateInput.ui +5 -0
- package/gallery/Decision-percent.ui +10 -0
- package/gallery/Decision.ui +12 -0
- package/gallery/Dependencies-audit.ui +1 -0
- package/gallery/Dependencies-both.ui +1 -0
- package/gallery/Dependencies-list.ui +7 -0
- package/gallery/Dependencies-pip.ui +1 -0
- package/gallery/Dependencies-pnpm.ui +1 -0
- package/gallery/Dependencies.ui +1 -0
- package/gallery/Details.ui +4 -0
- package/gallery/Diagram-flow.ui +16 -0
- package/gallery/Diagram-sequence.ui +15 -0
- package/gallery/Diagram.ui +16 -0
- package/gallery/Diff.ui +3 -0
- package/gallery/Divider.ui +5 -0
- package/gallery/EditableTable.ui +9 -0
- package/gallery/Entity-list.ui +6 -0
- package/gallery/Entity.ui +8 -0
- package/gallery/Environments.ui +1 -0
- package/gallery/Estimate-points.ui +6 -0
- package/gallery/Estimate.ui +8 -0
- package/gallery/File.ui +4 -0
- package/gallery/Findings-biome.ui +1 -0
- package/gallery/Findings-eslint.ui +1 -0
- package/gallery/Findings-oxlint.ui +1 -0
- package/gallery/Findings-review.ui +1 -0
- package/gallery/Findings-ruff.ui +1 -0
- package/gallery/Findings-sarif.ui +1 -0
- package/gallery/Findings-ship.ui +11 -0
- package/gallery/Findings-tsc.ui +1 -0
- package/gallery/Findings.ui +7 -0
- package/gallery/Flashcards.ui +3 -0
- package/gallery/FollowUps.ui +4 -0
- package/gallery/Form.ui +8 -0
- package/gallery/Gallery-links.ui +8 -0
- package/gallery/Gallery-strip.ui +14 -0
- package/gallery/Gallery.ui +10 -0
- package/gallery/Gauge.ui +7 -0
- package/gallery/Grid.ui +8 -0
- package/gallery/Hotspots-photo.ui +8 -0
- package/gallery/Hotspots.ui +11 -0
- package/gallery/Icon.ui +5 -0
- package/gallery/Image.ui +4 -0
- package/gallery/ImageSearch-aspects.ui +6 -0
- package/gallery/ImageSearch.ui +4 -0
- package/gallery/Input.ui +5 -0
- package/gallery/Json-api.ui +1 -0
- package/gallery/Json.ui +8 -0
- package/gallery/KeyValue.ui +3 -0
- package/gallery/List-dots.ui +8 -0
- package/gallery/List.ui +8 -0
- package/gallery/ListEditor.ui +5 -0
- package/gallery/Logs-jsonl.ui +1 -0
- package/gallery/Logs-lines.ui +11 -0
- package/gallery/Logs-python.ui +1 -0
- package/gallery/Logs.ui +1 -0
- package/gallery/Map-city.ui +8 -0
- package/gallery/Map.ui +8 -0
- package/gallery/Metric.ui +3 -0
- package/gallery/OptionCards-multi.ui +10 -0
- package/gallery/OptionCards.ui +10 -0
- package/gallery/Pipeline-checks.ui +1 -0
- package/gallery/Pipeline-passed.ui +1 -0
- package/gallery/Pipeline-running.ui +1 -0
- package/gallery/Pipeline-stages.ui +13 -0
- package/gallery/Pipeline.ui +1 -0
- package/gallery/Plan.ui +9 -0
- package/gallery/Progress.ui +4 -0
- package/gallery/Provenance-deploy.ui +6 -0
- package/gallery/Provenance-no-receipt.ui +1 -0
- package/gallery/Provenance.ui +1 -0
- package/gallery/PullRequest.ui +1 -0
- package/gallery/Quiz.ui +6 -0
- package/gallery/Radio.ui +5 -0
- package/gallery/Rating.ui +5 -0
- package/gallery/Roadmap-sprint.ui +8 -0
- package/gallery/Roadmap-year.ui +9 -0
- package/gallery/Roadmap.ui +12 -0
- package/gallery/Row.ui +3 -0
- package/gallery/SegmentedBar-status.ui +8 -0
- package/gallery/SegmentedBar.ui +8 -0
- package/gallery/Select.ui +5 -0
- package/gallery/Slider.ui +7 -0
- package/gallery/Snippet.ui +3 -0
- package/gallery/Sources-chips.ui +4 -0
- package/gallery/Sources.ui +8 -0
- package/gallery/Sparkline.ui +6 -0
- package/gallery/Stack.ui +7 -0
- package/gallery/StackTrace-aggregate.ui +1 -0
- package/gallery/StackTrace-browser.ui +1 -0
- package/gallery/StackTrace-go.ui +1 -0
- package/gallery/StackTrace-java.ui +1 -0
- package/gallery/StackTrace-python.ui +1 -0
- package/gallery/StackTrace-rust.ui +1 -0
- package/gallery/StackTrace.ui +1 -0
- package/gallery/Status.ui +9 -0
- package/gallery/Stepper.ui +5 -0
- package/gallery/Steps.ui +8 -0
- package/gallery/Swatches-long-names.ui +4 -0
- package/gallery/Swatches-picker.ui +5 -0
- package/gallery/Swatches.ui +10 -0
- package/gallery/Table.ui +7 -0
- package/gallery/Tabs.ui +8 -0
- package/gallery/Tests-junit.ui +1 -0
- package/gallery/Tests-vitest-junit.ui +1 -0
- package/gallery/Tests-vitest.ui +1 -0
- package/gallery/Tests.ui +9 -0
- package/gallery/Text.ui +4 -0
- package/gallery/TextArea.ui +5 -0
- package/gallery/Threads-ids.ui +1 -0
- package/gallery/Threads.ui +1 -0
- package/gallery/Timeline.ui +8 -0
- package/gallery/Timer.ui +4 -0
- package/gallery/Toggle.ui +5 -0
- package/gallery/Transcript-text.ui +1 -0
- package/gallery/Transcript-untimed.ui +1 -0
- package/gallery/Transcript-whisper.ui +1 -0
- package/gallery/Transcript.ui +1 -0
- package/gallery/Tree.ui +3 -0
- package/gallery/Treemap-du.ui +1 -0
- package/gallery/Treemap-esbuild.ui +1 -0
- package/gallery/Treemap-tree.ui +7 -0
- package/gallery/Treemap.ui +18 -0
- package/gallery/Walkthrough.ui +9 -0
- package/gallery/Waterfall-har.ui +1 -0
- package/gallery/Waterfall-trace.ui +1 -0
- package/gallery/Waterfall.ui +11 -0
- package/gallery/data/acceptance/coil/viewport-lifecycle.json +25 -0
- package/gallery/data/acceptance/coil-gate.json +153 -0
- package/gallery/data/acceptance/orbit-baseline.json +163 -0
- package/gallery/data/acceptance/shots/gate-cell-opened.png +0 -0
- package/gallery/data/acceptance/shots/gate-light.png +0 -0
- package/gallery/data/acceptance/shots/gate-phone-dark.png +0 -0
- package/gallery/data/activity.json +1 -0
- package/gallery/data/bb-tasks-orb.json +420 -0
- package/gallery/data/biome.json +229 -0
- package/gallery/data/compare/status-after.png +0 -0
- package/gallery/data/compare/status-before.png +0 -0
- package/gallery/data/compare/status-dark-2x.png +0 -0
- package/gallery/data/compare/status-rerender.png +0 -0
- package/gallery/data/coverage-py.json +1 -0
- package/gallery/data/coverage-summary.json +1 -0
- package/gallery/data/coverage.out +23 -0
- package/gallery/data/deploys.json +1 -0
- package/gallery/data/device-runs.json +285 -0
- package/gallery/data/du-node-modules.txt +359 -0
- package/gallery/data/esbuild-meta.json +292 -0
- package/gallery/data/eslint.json +266 -0
- package/gallery/data/eslint.sarif +343 -0
- package/gallery/data/findings-review.json +76 -0
- package/gallery/data/gh-issues-nextjs.json +293 -0
- package/gallery/data/gh-pr-checks.json +272 -0
- package/gallery/data/gh-prs-nextjs.json +419 -0
- package/gallery/data/gh-run-failed.json +1 -0
- package/gallery/data/gh-run-passed.json +1 -0
- package/gallery/data/gh-run-running.json +1 -0
- package/gallery/data/github-release.json +57 -0
- package/gallery/data/hyperfine-startup.json +187 -0
- package/gallery/data/lcov.info +234 -0
- package/gallery/data/logs/media-sync.log +127 -0
- package/gallery/data/logs/thumbnailer.jsonl +17 -0
- package/gallery/data/logs/vitest-run.log +89 -0
- package/gallery/data/npm-audit.json +672 -0
- package/gallery/data/npm-outdated.json +179 -0
- package/gallery/data/oxlint.json +156 -0
- package/gallery/data/pip-outdated.json +68 -0
- package/gallery/data/pnpm-outdated.json +58 -0
- package/gallery/data/provenance/devicectl-install.json +35 -0
- package/gallery/data/provenance/install-timeout.log +4 -0
- package/gallery/data/provenance/iphone-install.json +72 -0
- package/gallery/data/provenance/iphone-no-receipt.json +57 -0
- package/gallery/data/provenance/live-home.jpg +0 -0
- package/gallery/data/provenance/live-probe.txt +10 -0
- package/gallery/data/provenance/xcodebuild-2811.log +19 -0
- package/gallery/data/provenance/xcodebuild-4712.log +19 -0
- package/gallery/data/pytest-junit.xml +64 -0
- package/gallery/data/python-org-trace.json +35 -0
- package/gallery/data/python-org.har +31 -0
- package/gallery/data/retry.ts +13 -0
- package/gallery/data/ruff.json +261 -0
- package/gallery/data/src/durations.py +63 -0
- package/gallery/data/src/json-logic.ts +156 -0
- package/gallery/data/src/mapMath.ts +135 -0
- package/gallery/data/src/semver/bump.go +73 -0
- package/gallery/data/traces/browser-error.json +5 -0
- package/gallery/data/traces/go/main.go +27 -0
- package/gallery/data/traces/go-panic.txt +16 -0
- package/gallery/data/traces/java-caused-by.txt +15 -0
- package/gallery/data/traces/node-aggregate.txt +30 -0
- package/gallery/data/traces/node-cause.txt +21 -0
- package/gallery/data/traces/py/deploy.json +5 -0
- package/gallery/data/traces/py/deploy.py +17 -0
- package/gallery/data/traces/py/settings.py +19 -0
- package/gallery/data/traces/python-chain.txt +30 -0
- package/gallery/data/traces/rust/main.rs +11 -0
- package/gallery/data/traces/rust-panic.txt +16 -0
- package/gallery/data/traces/shop/cart.mjs +19 -0
- package/gallery/data/traces/shop/pricing.mjs +12 -0
- package/gallery/data/traces/shop/status.mjs +9 -0
- package/gallery/data/traces/web/app.js +11 -0
- package/gallery/data/traces/web/index.html +1 -0
- package/gallery/data/traces/web/search.js +10 -0
- package/gallery/data/transcript/call-noisy.wav +0 -0
- package/gallery/data/transcript/call.json +362 -0
- package/gallery/data/transcript/call.wav +0 -0
- package/gallery/data/transcript/mlx-whisper-tiny.en-noisy.json +1 -0
- package/gallery/data/transcript/whisper-cpp-base.en-noisy.json +658 -0
- package/gallery/data/transcript/whisper-cpp-base.en.json +658 -0
- package/gallery/data/transcript/whisper-cpp-base.en.srt +16 -0
- package/gallery/data/transcript/whisper-cpp-tiny.en-noisy.json +619 -0
- package/gallery/data/tsc.txt +4 -0
- package/gallery/data/vitest-bench.json +119 -0
- package/gallery/data/vitest-junit.xml +133 -0
- package/gallery/data/vitest-report.json +612 -0
- package/gallery/images/bell.svg +10 -0
- package/gallery/images/bike.svg +47 -0
- package/gallery/images/footer-planet.webp +0 -0
- package/gallery/images/front-light.svg +13 -0
- package/gallery/images/golden-hour.webp +0 -0
- package/gallery/images/option-flowers.webp +0 -0
- package/gallery/images/option-hilltop.webp +0 -0
- package/gallery/images/option-meadow.webp +0 -0
- package/gallery/images/option-sky.webp +0 -0
- package/gallery/images/u-lock.svg +10 -0
- package/index.client.tsx +164 -0
- package/index.server.ts +216 -0
- package/package.json +50 -4
- package/paseo-plugin.json +14 -0
- package/server/data-source.ts +407 -0
- package/server/project.ts +981 -0
- package/server/web/address.ts +57 -0
- package/server/web/cache.ts +85 -0
- package/server/web/commons.ts +126 -0
- package/server/web/geocode.ts +40 -0
- package/server/web/image.ts +35 -0
- package/server/web/lookups.ts +248 -0
- package/server/web/net.ts +210 -0
- package/shared/core/acceptance.ts +744 -0
- package/shared/core/ast.ts +76 -0
- package/shared/core/benchmark.ts +743 -0
- package/shared/core/catalog.ts +733 -0
- package/shared/core/compare.ts +225 -0
- package/shared/core/coverage.ts +456 -0
- package/shared/core/data.ts +334 -0
- package/shared/core/decisions.ts +527 -0
- package/shared/core/dependencies.ts +604 -0
- package/shared/core/diagnostics.ts +24 -0
- package/shared/core/environments.ts +212 -0
- package/shared/core/evaluate.ts +737 -0
- package/shared/core/fields.ts +66 -0
- package/shared/core/findings.ts +1172 -0
- package/shared/core/form.ts +69 -0
- package/shared/core/index.ts +44 -0
- package/shared/core/junit.ts +252 -0
- package/shared/core/lexer.ts +275 -0
- package/shared/core/logs.ts +496 -0
- package/shared/core/markdown.ts +2374 -0
- package/shared/core/parser.ts +355 -0
- package/shared/core/perf-units.ts +228 -0
- package/shared/core/pipeline.ts +679 -0
- package/shared/core/project.ts +472 -0
- package/shared/core/provenance.ts +725 -0
- package/shared/core/stack-trace.ts +718 -0
- package/shared/core/streaming.ts +136 -0
- package/shared/core/test-results.ts +562 -0
- package/shared/core/thread-activity.ts +203 -0
- package/shared/core/tracking.ts +887 -0
- package/shared/core/transcript.ts +1364 -0
- package/shared/core/treemap.ts +446 -0
- package/shared/core/types.ts +70 -0
- package/shared/core/validate.ts +574 -0
- package/shared/core/waterfall.ts +536 -0
- package/shared/diagram/flowchart.ts +205 -0
- package/shared/diagram/layout.ts +440 -0
- package/shared/diagram/markdown.ts +135 -0
- package/shared/diagram/sequence.ts +122 -0
- package/shared/diagram/zoom.ts +114 -0
- package/shared/fallback.ts +120 -0
- package/shared/genui.test.ts +122 -0
- package/shared/instructions.ts +144 -0
- package/shared/logic/calendarMath.ts +130 -0
- package/shared/logic/chartMath.ts +289 -0
- package/shared/logic/choose-logic.ts +196 -0
- package/shared/logic/compare-logic.ts +270 -0
- package/shared/logic/content-model.ts +227 -0
- package/shared/logic/environments-logic.ts +53 -0
- package/shared/logic/explain-logic.ts +315 -0
- package/shared/logic/fieldLogic.ts +138 -0
- package/shared/logic/gallery-search.ts +71 -0
- package/shared/logic/icons.ts +322 -0
- package/shared/logic/json-logic.ts +153 -0
- package/shared/logic/mapMath.ts +133 -0
- package/shared/logic/project-logic.ts +39 -0
- package/shared/logic/references-model.ts +201 -0
- package/shared/logic/sketch.ts +165 -0
- package/shared/logic/supersession.ts +77 -0
- package/shared/logic/values.ts +133 -0
- package/shared/logic/widget-logic.ts +163 -0
- package/shared/rpc.ts +252 -0
- package/shared/segment.ts +81 -0
- package/shared/settings.ts +12 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// Pure rules behind the choose-and-edit components (choose.tsx,
|
|
2
|
+
// EditableTable.tsx): option, colour, column and prompt shapes, picking from
|
|
3
|
+
// a list, editing rows, reading typed numbers. No React, no DOM.
|
|
4
|
+
import type { Scalar, Value } from "../core/types";
|
|
5
|
+
import { isObject, isScalar, list, text } from "./values";
|
|
6
|
+
|
|
7
|
+
// ── OptionCards ──────────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
export interface CardOption {
|
|
10
|
+
readonly value: Scalar;
|
|
11
|
+
readonly title: string;
|
|
12
|
+
readonly detail: string | null;
|
|
13
|
+
readonly image: string | null;
|
|
14
|
+
readonly icon: string | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A non-empty trimmed string field, else null. */
|
|
18
|
+
function field(value: unknown): string | null {
|
|
19
|
+
const shown = text(value).trim();
|
|
20
|
+
return shown === "" ? null : shown;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* OptionCards options: scalars, or `{value, title?, detail?, image?, icon?}`.
|
|
25
|
+
* An object without a scalar `value` uses its title as the value; one with
|
|
26
|
+
* neither is skipped (a streaming spec shows `{}` before its fields arrive).
|
|
27
|
+
* Null when `options` is not a list.
|
|
28
|
+
*/
|
|
29
|
+
export function cardOptions(options: Value | undefined): CardOption[] | null {
|
|
30
|
+
if (!Array.isArray(options)) return null;
|
|
31
|
+
return list(options).flatMap((item): CardOption[] => {
|
|
32
|
+
if (isScalar(item)) {
|
|
33
|
+
if (item === null) return [];
|
|
34
|
+
return [{ value: item, title: String(item), detail: null, image: null, icon: null }];
|
|
35
|
+
}
|
|
36
|
+
if (!isObject(item)) return [];
|
|
37
|
+
// Read by key: narrowing kept the Action shapes, which name no options fields.
|
|
38
|
+
const entry: Readonly<Record<string, unknown>> = item;
|
|
39
|
+
const title = field(entry.title);
|
|
40
|
+
const value = Object.hasOwn(entry, "value") && isScalar(entry.value) && entry.value !== null ? entry.value : title;
|
|
41
|
+
if (value === null) return [];
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
value,
|
|
45
|
+
title: title ?? String(value),
|
|
46
|
+
detail: field(entry.detail),
|
|
47
|
+
image: field(entry.image),
|
|
48
|
+
icon: field(entry.icon),
|
|
49
|
+
},
|
|
50
|
+
];
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The list state after toggling `option`: unticking removes it, ticking adds
|
|
56
|
+
* it in option order. Values the spec preset that are not options stay, after
|
|
57
|
+
* the options.
|
|
58
|
+
*/
|
|
59
|
+
export function togglePick(picked: readonly Value[], option: Scalar, options: readonly Scalar[]): Value[] {
|
|
60
|
+
if (picked.includes(option)) return picked.filter((value) => value !== option);
|
|
61
|
+
return [
|
|
62
|
+
...options.filter((candidate, index) => (candidate === option || picked.includes(candidate)) && options.indexOf(candidate) === index),
|
|
63
|
+
...picked.filter((value) => !options.includes(value as Scalar)),
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ── Swatches ─────────────────────────────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
export interface Swatch {
|
|
70
|
+
readonly value: string;
|
|
71
|
+
readonly name: string | null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Swatches colours: CSS colour strings or `{value, name?}`. `isColor` decides
|
|
76
|
+
* what counts (the renderer passes `CSS.supports("color", …)`); refused
|
|
77
|
+
* entries are listed in `invalid`. `{}` and objects still missing their value
|
|
78
|
+
* are skipped while a spec streams. Null when `colors` is not a list.
|
|
79
|
+
*/
|
|
80
|
+
export function swatchColors(
|
|
81
|
+
colors: Value | undefined,
|
|
82
|
+
isColor: (value: string) => boolean,
|
|
83
|
+
): { readonly colors: Swatch[]; readonly invalid: string[] } | null {
|
|
84
|
+
if (!Array.isArray(colors)) return null;
|
|
85
|
+
const valid: Swatch[] = [];
|
|
86
|
+
const invalid: string[] = [];
|
|
87
|
+
for (const item of list(colors)) {
|
|
88
|
+
const entry: Readonly<Record<string, unknown>> = isObject(item) ? item : { value: item };
|
|
89
|
+
if (entry.value === undefined || entry.value === null) continue;
|
|
90
|
+
const value = typeof entry.value === "string" ? entry.value.trim() : "";
|
|
91
|
+
if (value !== "" && isColor(value)) valid.push({ value, name: field(entry.name) });
|
|
92
|
+
else invalid.push(text(entry.value) || JSON.stringify(entry.value));
|
|
93
|
+
}
|
|
94
|
+
return { colors: valid, invalid };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Colour strings compare without case or surrounding space: `#FFF` picks `#fff`. */
|
|
98
|
+
export function sameColor(a: Value, b: string): boolean {
|
|
99
|
+
return typeof a === "string" && a.trim().toLowerCase() === b.toLowerCase();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ── EditableTable ────────────────────────────────────────────────────────────
|
|
103
|
+
|
|
104
|
+
export type ColumnType = "text" | "number" | "boolean";
|
|
105
|
+
|
|
106
|
+
export interface TableColumn {
|
|
107
|
+
readonly key: string;
|
|
108
|
+
readonly label: string;
|
|
109
|
+
readonly type: ColumnType;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type TableRow = Readonly<Record<string, Value>>;
|
|
113
|
+
|
|
114
|
+
/** A key as a header: first letter capitalized (`price` → `Price`). */
|
|
115
|
+
export function columnLabel(key: string): string {
|
|
116
|
+
return key.charAt(0).toUpperCase() + key.slice(1);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** What a column without a `type` holds: the kind of its first filled cell, else text. */
|
|
120
|
+
function inferType(key: string, rows: readonly TableRow[]): ColumnType {
|
|
121
|
+
for (const row of rows) {
|
|
122
|
+
const cell = Object.hasOwn(row, key) ? row[key] : null;
|
|
123
|
+
if (typeof cell === "number") return "number";
|
|
124
|
+
if (typeof cell === "boolean") return "boolean";
|
|
125
|
+
if (cell !== null) return "text";
|
|
126
|
+
}
|
|
127
|
+
return "text";
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* EditableTable columns: keys, or `{key, label?, type?: text|number|boolean}`.
|
|
132
|
+
* A column without a valid type takes the kind of its first filled cell.
|
|
133
|
+
* Columns without a key (still streaming) and repeated keys are skipped.
|
|
134
|
+
*/
|
|
135
|
+
export function tableColumns(columns: Value | undefined, rows: readonly TableRow[]): TableColumn[] {
|
|
136
|
+
const seen = new Set<string>();
|
|
137
|
+
return list(columns).flatMap((item): TableColumn[] => {
|
|
138
|
+
const entry: Readonly<Record<string, unknown>> = isObject(item) ? item : { key: item };
|
|
139
|
+
const key = typeof entry.key === "string" || typeof entry.key === "number" ? String(entry.key).trim() : "";
|
|
140
|
+
if (key === "" || seen.has(key)) return [];
|
|
141
|
+
seen.add(key);
|
|
142
|
+
const type = entry.type === "text" || entry.type === "number" || entry.type === "boolean" ? entry.type : inferType(key, rows);
|
|
143
|
+
return [{ key, label: field(entry.label) ?? columnLabel(key), type }];
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The rows a list state holds; an empty state is no rows. Null when a row is not an object. */
|
|
148
|
+
export function tableRows(value: Value): TableRow[] | null {
|
|
149
|
+
if (value === null) return [];
|
|
150
|
+
if (!Array.isArray(value)) return null;
|
|
151
|
+
const rows = list(value);
|
|
152
|
+
return rows.every(isObject) ? (rows as TableRow[]) : null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A new row: every column present, so the spec's `row.key` reads never miss. Numbers start empty (null). */
|
|
156
|
+
export function blankRow(columns: readonly TableColumn[]): TableRow {
|
|
157
|
+
return Object.fromEntries(columns.map((column) => [column.key, column.type === "boolean" ? false : column.type === "number" ? null : ""]));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** `rows` with one cell replaced; the row's other keys (shown or not) are kept. */
|
|
161
|
+
export function setCell(rows: readonly TableRow[], index: number, key: string, value: Value): TableRow[] {
|
|
162
|
+
return rows.map((row, position) => (position === index ? { ...row, [key]: value } : row));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A typed number: `12`, `-3.5`, `1,234.5` (comma grouping) or `3,5` (a
|
|
167
|
+
* decimal comma, as decimal keyboards type in many locales). "" is null
|
|
168
|
+
* (empty cell); anything unreadable, including a half-typed `-` or `1.`,
|
|
169
|
+
* is undefined so the field keeps the draft without writing it.
|
|
170
|
+
*/
|
|
171
|
+
export function parseNumberInput(input: string): number | null | undefined {
|
|
172
|
+
const compact = input.trim().replace(/[\s\u00a0\u202f']/g, "");
|
|
173
|
+
if (compact === "") return null;
|
|
174
|
+
const normalized = compact.includes(".")
|
|
175
|
+
? compact.replace(/,/g, "")
|
|
176
|
+
: /^-?\d{1,3}(,\d{3})+$/.test(compact) && !/^-?0,/.test(compact)
|
|
177
|
+
? // 1,234 is grouping; 3,5 or 0,250 is a decimal comma.
|
|
178
|
+
compact.replace(/,/g, "")
|
|
179
|
+
: compact.replace(",", ".");
|
|
180
|
+
if (!/^[-+]?(\d+\.?\d*|\.\d+)$/.test(normalized) || normalized.endsWith(".")) return undefined;
|
|
181
|
+
const number = Number(normalized);
|
|
182
|
+
return Number.isFinite(number) ? number : undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ── FollowUps ────────────────────────────────────────────────────────────────
|
|
186
|
+
|
|
187
|
+
/** FollowUps prompts: non-empty strings (numbers read as text), trimmed; repeats dropped. */
|
|
188
|
+
export function followUpPrompts(prompts: Value | undefined): string[] {
|
|
189
|
+
const out: string[] = [];
|
|
190
|
+
for (const item of list(prompts)) {
|
|
191
|
+
if (typeof item !== "string" && typeof item !== "number") continue;
|
|
192
|
+
const prompt = String(item).trim();
|
|
193
|
+
if (prompt !== "" && !out.includes(prompt)) out.push(prompt);
|
|
194
|
+
}
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// Compare's pixel work and view geometry, kept free of the DOM so it can be
|
|
2
|
+
// tested: which pixels differ beyond a tolerance, the boxes around changed
|
|
3
|
+
// regions, and the shared zoom/pan every pane draws from.
|
|
4
|
+
import type { Size } from "../core/compare";
|
|
5
|
+
|
|
6
|
+
/** Per-channel tolerance (0-255) a fresh Compare starts with: anti-aliasing and JPEG noise stay quiet, a recoloured or moved element does not. */
|
|
7
|
+
export const DEFAULT_TOLERANCE = 8;
|
|
8
|
+
export const MAX_TOLERANCE = 64;
|
|
9
|
+
|
|
10
|
+
export interface PixelDiff {
|
|
11
|
+
/** 1 where the pixel differs beyond the tolerance, row-major. */
|
|
12
|
+
readonly mask: Uint8Array;
|
|
13
|
+
readonly changed: number;
|
|
14
|
+
readonly total: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Compares two RGBA buffers of the same `width`×`height`. A pixel changed
|
|
19
|
+
* when any premultiplied channel (or alpha) differs by more than `tolerance`
|
|
20
|
+
* of 255, so two fully transparent pixels match whatever colour they carry.
|
|
21
|
+
*/
|
|
22
|
+
export function pixelDiff(
|
|
23
|
+
a: ArrayLike<number>,
|
|
24
|
+
b: ArrayLike<number>,
|
|
25
|
+
width: number,
|
|
26
|
+
height: number,
|
|
27
|
+
tolerance: number,
|
|
28
|
+
): PixelDiff {
|
|
29
|
+
const total = Math.max(0, width * height);
|
|
30
|
+
const mask = new Uint8Array(total);
|
|
31
|
+
let changed = 0;
|
|
32
|
+
for (let pixel = 0, offset = 0; pixel < total; pixel++, offset += 4) {
|
|
33
|
+
const alphaA = a[offset + 3] ?? 0;
|
|
34
|
+
const alphaB = b[offset + 3] ?? 0;
|
|
35
|
+
let delta = Math.abs(alphaA - alphaB);
|
|
36
|
+
if (delta <= tolerance) {
|
|
37
|
+
for (let channel = 0; channel < 3 && delta <= tolerance; channel++) {
|
|
38
|
+
const d = Math.abs(((a[offset + channel] ?? 0) * alphaA - (b[offset + channel] ?? 0) * alphaB) / 255);
|
|
39
|
+
if (d > delta) delta = d;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (delta > tolerance) {
|
|
43
|
+
mask[pixel] = 1;
|
|
44
|
+
changed++;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { mask, changed, total };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface Region {
|
|
51
|
+
readonly x: number;
|
|
52
|
+
readonly y: number;
|
|
53
|
+
readonly width: number;
|
|
54
|
+
readonly height: number;
|
|
55
|
+
/** Changed pixels inside. */
|
|
56
|
+
readonly pixels: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Boxes around the changed pixels of `mask`: pixels are binned into `cell`-px
|
|
61
|
+
* cells, touching cells and cells with up to `gap` empty cells between them join one region (so a changed
|
|
62
|
+
* word is one box, not one per letter), each box is fitted tightly to its
|
|
63
|
+
* pixels, and boxes that overlap merge. Largest first; past `max`, `more`
|
|
64
|
+
* counts the regions left out.
|
|
65
|
+
*/
|
|
66
|
+
export function changedRegions(
|
|
67
|
+
mask: Uint8Array,
|
|
68
|
+
width: number,
|
|
69
|
+
height: number,
|
|
70
|
+
{ cell = 8, gap = 2, max = 40 }: { cell?: number; gap?: number; max?: number } = {},
|
|
71
|
+
): { regions: Region[]; more: number } {
|
|
72
|
+
const columns = Math.ceil(width / cell);
|
|
73
|
+
const rows = Math.ceil(height / cell);
|
|
74
|
+
const cells = columns * rows;
|
|
75
|
+
if (cells === 0) return { regions: [], more: 0 };
|
|
76
|
+
// Per cell: changed count and the tight bounds of its changed pixels.
|
|
77
|
+
const count = new Uint32Array(cells);
|
|
78
|
+
const bounds = new Int32Array(cells * 4);
|
|
79
|
+
for (let y = 0; y < height; y++) {
|
|
80
|
+
const row = y * width;
|
|
81
|
+
const cellRow = Math.floor(y / cell) * columns;
|
|
82
|
+
for (let x = 0; x < width; x++) {
|
|
83
|
+
if (mask[row + x] !== 1) continue;
|
|
84
|
+
const index = cellRow + Math.floor(x / cell);
|
|
85
|
+
const at = index * 4;
|
|
86
|
+
if (count[index] === 0) {
|
|
87
|
+
bounds[at] = x;
|
|
88
|
+
bounds[at + 1] = y;
|
|
89
|
+
bounds[at + 2] = x;
|
|
90
|
+
bounds[at + 3] = y;
|
|
91
|
+
} else {
|
|
92
|
+
if (x < bounds[at]!) bounds[at] = x;
|
|
93
|
+
if (x > bounds[at + 2]!) bounds[at + 2] = x;
|
|
94
|
+
bounds[at + 3] = y;
|
|
95
|
+
}
|
|
96
|
+
count[index]!++;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let boxes: { x0: number; y0: number; x1: number; y1: number; pixels: number }[] = [];
|
|
101
|
+
const seen = new Uint8Array(cells);
|
|
102
|
+
const queue: number[] = [];
|
|
103
|
+
for (let start = 0; start < cells; start++) {
|
|
104
|
+
if (count[start] === 0 || seen[start] === 1) continue;
|
|
105
|
+
seen[start] = 1;
|
|
106
|
+
queue.length = 0;
|
|
107
|
+
queue.push(start);
|
|
108
|
+
const box = { x0: Infinity, y0: Infinity, x1: -Infinity, y1: -Infinity, pixels: 0 };
|
|
109
|
+
for (let head = 0; head < queue.length; head++) {
|
|
110
|
+
const index = queue[head]!;
|
|
111
|
+
const at = index * 4;
|
|
112
|
+
box.x0 = Math.min(box.x0, bounds[at]!);
|
|
113
|
+
box.y0 = Math.min(box.y0, bounds[at + 1]!);
|
|
114
|
+
box.x1 = Math.max(box.x1, bounds[at + 2]!);
|
|
115
|
+
box.y1 = Math.max(box.y1, bounds[at + 3]!);
|
|
116
|
+
box.pixels += count[index]!;
|
|
117
|
+
const cx = index % columns;
|
|
118
|
+
const cy = (index - cx) / columns;
|
|
119
|
+
// Neighbours touch at distance 1; `gap` empty cells may sit between two that still join.
|
|
120
|
+
const reach = gap + 1;
|
|
121
|
+
for (let ny = Math.max(0, cy - reach); ny <= Math.min(rows - 1, cy + reach); ny++) {
|
|
122
|
+
for (let nx = Math.max(0, cx - reach); nx <= Math.min(columns - 1, cx + reach); nx++) {
|
|
123
|
+
const next = ny * columns + nx;
|
|
124
|
+
if (count[next] === 0 || seen[next] === 1) continue;
|
|
125
|
+
seen[next] = 1;
|
|
126
|
+
queue.push(next);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
boxes.push(box);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Tight boxes of neighbouring components can still overlap: merge until none do.
|
|
134
|
+
for (let merged = true; merged; ) {
|
|
135
|
+
merged = false;
|
|
136
|
+
const next: typeof boxes = [];
|
|
137
|
+
for (const box of boxes) {
|
|
138
|
+
const hit = next.find((other) => box.x0 <= other.x1 && other.x0 <= box.x1 && box.y0 <= other.y1 && other.y0 <= box.y1);
|
|
139
|
+
if (hit === undefined) {
|
|
140
|
+
next.push({ ...box });
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
hit.x0 = Math.min(hit.x0, box.x0);
|
|
144
|
+
hit.y0 = Math.min(hit.y0, box.y0);
|
|
145
|
+
hit.x1 = Math.max(hit.x1, box.x1);
|
|
146
|
+
hit.y1 = Math.max(hit.y1, box.y1);
|
|
147
|
+
hit.pixels += box.pixels;
|
|
148
|
+
merged = true;
|
|
149
|
+
}
|
|
150
|
+
boxes = next;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
boxes.sort((a, b) => b.pixels - a.pixels || a.y0 - b.y0 || a.x0 - b.x0);
|
|
154
|
+
return {
|
|
155
|
+
regions: boxes.slice(0, max).map((box) => ({ x: box.x0, y: box.y0, width: box.x1 - box.x0 + 1, height: box.y1 - box.y0 + 1, pixels: box.pixels })),
|
|
156
|
+
more: Math.max(0, boxes.length - max),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A share of pixels as people read it: whole from 10%, one decimal from 1%, two significant digits below; never "0%" for a change. */
|
|
161
|
+
export function formatPixelShare(changed: number, total: number): string {
|
|
162
|
+
if (!(total > 0) || changed <= 0) return "0%";
|
|
163
|
+
const percent = (changed / total) * 100;
|
|
164
|
+
if (percent >= 10) return `${Math.round(percent)}%`;
|
|
165
|
+
if (percent >= 1) return `${Number(percent.toFixed(1))}%`;
|
|
166
|
+
return `${Number(percent.toPrecision(2))}%`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
// View geometry
|
|
171
|
+
|
|
172
|
+
export interface Point {
|
|
173
|
+
readonly x: number;
|
|
174
|
+
readonly y: number;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The shared zoom and pan: `zoom` relative to fitting the image in its pane
|
|
179
|
+
* (1 = fit), and the image point at the pane's centre as a fraction of the
|
|
180
|
+
* image (0.5, 0.5 = centred). Fractions, not pixels, so panes of any size
|
|
181
|
+
* show the same part of their image.
|
|
182
|
+
*/
|
|
183
|
+
export interface View {
|
|
184
|
+
readonly zoom: number;
|
|
185
|
+
readonly cx: number;
|
|
186
|
+
readonly cy: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export const FIT_VIEW: View = { zoom: 1, cx: 0.5, cy: 0.5 };
|
|
190
|
+
export const MAX_ZOOM = 32;
|
|
191
|
+
|
|
192
|
+
/** Pixels on screen per image pixel when the image is contained in the pane; never above 1, so a small image is not blown up. */
|
|
193
|
+
export function fitScale(pane: Size, image: Size): number {
|
|
194
|
+
if (!(image.width > 0 && image.height > 0 && pane.width > 0 && pane.height > 0)) return 1;
|
|
195
|
+
return Math.min(1, pane.width / image.width, pane.height / image.height);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Where the image's top-left corner sits in the pane and its scale, for `view`; an image smaller than the pane is centred, a larger one never leaves a gap at an edge. */
|
|
199
|
+
export function placeImage(view: View, pane: Size, image: Size): { readonly scale: number; readonly x: number; readonly y: number } {
|
|
200
|
+
const scale = fitScale(pane, image) * view.zoom;
|
|
201
|
+
const along = (paneLength: number, imageLength: number, centre: number) => {
|
|
202
|
+
const shown = imageLength * scale;
|
|
203
|
+
if (shown <= paneLength) return (paneLength - shown) / 2;
|
|
204
|
+
return Math.min(0, Math.max(paneLength - shown, paneLength / 2 - centre * shown));
|
|
205
|
+
};
|
|
206
|
+
return { scale, x: along(pane.width, image.width, view.cx), y: along(pane.height, image.height, view.cy) };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** `view` with its zoom inside 1…MAX_ZOOM and its centre where `placeImage` would actually put it. */
|
|
210
|
+
export function clampView(view: View, pane: Size, image: Size): View {
|
|
211
|
+
const zoom = Math.min(MAX_ZOOM, Math.max(1, Number.isFinite(view.zoom) ? view.zoom : 1));
|
|
212
|
+
const placed = placeImage({ zoom, cx: view.cx, cy: view.cy }, pane, image);
|
|
213
|
+
const shownWidth = image.width * placed.scale;
|
|
214
|
+
const shownHeight = image.height * placed.scale;
|
|
215
|
+
return {
|
|
216
|
+
zoom,
|
|
217
|
+
cx: shownWidth > 0 ? (pane.width / 2 - placed.x) / shownWidth : 0.5,
|
|
218
|
+
cy: shownHeight > 0 ? (pane.height / 2 - placed.y) / shownHeight : 0.5,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Zooms by `factor` keeping the image point under `at` (pane coordinates) where it is. */
|
|
223
|
+
export function zoomAround(view: View, factor: number, at: Point, pane: Size, image: Size): View {
|
|
224
|
+
const before = placeImage(view, pane, image);
|
|
225
|
+
const imageX = (at.x - before.x) / before.scale;
|
|
226
|
+
const imageY = (at.y - before.y) / before.scale;
|
|
227
|
+
const zoom = Math.min(MAX_ZOOM, Math.max(1, view.zoom * factor));
|
|
228
|
+
const scale = fitScale(pane, image) * zoom;
|
|
229
|
+
const x = at.x - imageX * scale;
|
|
230
|
+
const y = at.y - imageY * scale;
|
|
231
|
+
return clampView(
|
|
232
|
+
{ zoom, cx: (pane.width / 2 - x) / (image.width * scale), cy: (pane.height / 2 - y) / (image.height * scale) },
|
|
233
|
+
pane,
|
|
234
|
+
image,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Moves the image by `dx`, `dy` screen pixels (a drag), as far as its edges allow. */
|
|
239
|
+
export function panBy(view: View, dx: number, dy: number, pane: Size, image: Size): View {
|
|
240
|
+
const scale = fitScale(pane, image) * view.zoom;
|
|
241
|
+
if (!(scale > 0)) return view;
|
|
242
|
+
return clampView({ zoom: view.zoom, cx: view.cx - dx / (image.width * scale), cy: view.cy - dy / (image.height * scale) }, pane, image);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The view that shows `region` (image pixels) as large as it fits with a
|
|
247
|
+
* margin of half its size around it, and never less than 96×64 image pixels,
|
|
248
|
+
* so a changed letter keeps the words around it.
|
|
249
|
+
*/
|
|
250
|
+
export function viewOfRegion(region: Region, pane: Size, image: Size): View {
|
|
251
|
+
const fit = fitScale(pane, image);
|
|
252
|
+
const width = Math.max(region.width * 2, 96);
|
|
253
|
+
const height = Math.max(region.height * 2, 64);
|
|
254
|
+
const zoom = Math.min(pane.width / width, pane.height / height) / fit;
|
|
255
|
+
return clampView(
|
|
256
|
+
{ zoom, cx: (region.x + region.width / 2) / image.width, cy: (region.y + region.height / 2) / image.height },
|
|
257
|
+
pane,
|
|
258
|
+
image,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** The largest pixel count the difference is computed at; a bigger reference is compared scaled down, and says so. */
|
|
263
|
+
export const MAX_DIFF_PIXELS = 8_000_000;
|
|
264
|
+
|
|
265
|
+
/** The grid the difference is computed on: the reference's own size, or scaled down to MAX_DIFF_PIXELS. */
|
|
266
|
+
export function diffGrid(reference: Size): Size & { readonly scale: number } {
|
|
267
|
+
const pixels = reference.width * reference.height;
|
|
268
|
+
const scale = pixels > MAX_DIFF_PIXELS ? Math.sqrt(MAX_DIFF_PIXELS / pixels) : 1;
|
|
269
|
+
return { width: Math.max(1, Math.round(reference.width * scale)), height: Math.max(1, Math.round(reference.height * scale)), scale };
|
|
270
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// Pure readers behind the content renderers (content.tsx): they turn the
|
|
2
|
+
// loose shapes models write into the exact structures the components draw,
|
|
3
|
+
// and drop what they cannot read instead of throwing.
|
|
4
|
+
import type { Value } from "../core/types";
|
|
5
|
+
import { isNode, isObject, optionalText, text, tone, type Tone } from "./values";
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// KeyValue
|
|
9
|
+
|
|
10
|
+
export type KeyValuePair = readonly [label: string, value: Value];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* `{label: value}` or `[[label, value], …]` (also `{label|key, value}` rows).
|
|
14
|
+
* Absent/null pairs are empty; any other shape is null (malformed).
|
|
15
|
+
*/
|
|
16
|
+
export function keyValuePairs(pairs: unknown): KeyValuePair[] | null {
|
|
17
|
+
if (pairs === undefined || pairs === null) return [];
|
|
18
|
+
if (Array.isArray(pairs)) {
|
|
19
|
+
const result: KeyValuePair[] = [];
|
|
20
|
+
for (const entry of pairs as readonly unknown[]) {
|
|
21
|
+
if (Array.isArray(entry)) {
|
|
22
|
+
// A streaming `[label]` shows its label before its value arrives.
|
|
23
|
+
if (entry.length > 0) result.push([text(entry[0]), (entry[1] ?? null) as Value]);
|
|
24
|
+
} else if (isObject(entry) && !isNode(entry) && ("label" in entry || "key" in entry)) {
|
|
25
|
+
result.push([text(entry.label ?? entry.key), (entry.value ?? null) as Value]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result.length > 0 || pairs.length === 0 ? result : null;
|
|
29
|
+
}
|
|
30
|
+
// Evaluated props are Values, so a plain object's fields are Values too.
|
|
31
|
+
if (isObject(pairs) && !isNode(pairs)) return Object.entries(pairs) as KeyValuePair[];
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Timeline
|
|
37
|
+
|
|
38
|
+
export interface TimelineEntry {
|
|
39
|
+
readonly time: string | null;
|
|
40
|
+
readonly title: string;
|
|
41
|
+
readonly detail: string | null;
|
|
42
|
+
readonly tone: Tone;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** `{time, title, detail?, tone?}` items; a bare string or number is a title. */
|
|
46
|
+
export function timelineEntries(items: readonly unknown[]): TimelineEntry[] {
|
|
47
|
+
const entries: TimelineEntry[] = [];
|
|
48
|
+
for (const item of items) {
|
|
49
|
+
if (typeof item === "string" || typeof item === "number") {
|
|
50
|
+
const title = text(item);
|
|
51
|
+
if (title !== "") entries.push({ time: null, title, detail: null, tone: "neutral" });
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (!isObject(item) || isNode(item)) continue;
|
|
55
|
+
const time = optionalText(item.time ?? item.date ?? null);
|
|
56
|
+
const title = text(item.title ?? item.label ?? null);
|
|
57
|
+
const detail = optionalText(item.detail ?? null);
|
|
58
|
+
if (title === "" && !time && !detail) continue;
|
|
59
|
+
entries.push({ time: time || null, title, detail: detail || null, tone: tone(item.tone as Value) });
|
|
60
|
+
}
|
|
61
|
+
return entries;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
// Tree
|
|
66
|
+
|
|
67
|
+
export interface TreeItem {
|
|
68
|
+
/** Unique in its tree and stable while more items stream in. */
|
|
69
|
+
readonly id: string;
|
|
70
|
+
readonly label: string;
|
|
71
|
+
/** The full path for items built from path strings; null for `{label}` items. */
|
|
72
|
+
readonly path: string | null;
|
|
73
|
+
/** A short status shown at the right: text or a node such as a Badge. */
|
|
74
|
+
readonly note: Value | null;
|
|
75
|
+
readonly folder: boolean;
|
|
76
|
+
readonly children: readonly TreeItem[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface Draft {
|
|
80
|
+
id: string;
|
|
81
|
+
label: string;
|
|
82
|
+
path: string | null;
|
|
83
|
+
note: Value | null;
|
|
84
|
+
folder: boolean;
|
|
85
|
+
/** Built from a path string: merged by name, ordered folders first. */
|
|
86
|
+
fromPath: boolean;
|
|
87
|
+
children: Draft[];
|
|
88
|
+
/** Path-built children by segment, for merging. */
|
|
89
|
+
byName: Map<string, Draft>;
|
|
90
|
+
/** `{label}` children per label, for unique ids. */
|
|
91
|
+
labelCounts: Map<string, number>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const ID_SEPARATOR = "\u001f";
|
|
95
|
+
|
|
96
|
+
function draft(id: string, label: string, path: string | null, fromPath: boolean): Draft {
|
|
97
|
+
return {
|
|
98
|
+
id,
|
|
99
|
+
label,
|
|
100
|
+
path,
|
|
101
|
+
note: null,
|
|
102
|
+
folder: false,
|
|
103
|
+
fromPath,
|
|
104
|
+
children: [],
|
|
105
|
+
byName: new Map(),
|
|
106
|
+
labelCounts: new Map(),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Adds `raw` under `parent`, creating or reusing one folder per segment.
|
|
112
|
+
* Inside a `{label}` item the location is unknown, so the new items get no path.
|
|
113
|
+
*/
|
|
114
|
+
function addPath(parent: Draft, raw: string, withPaths: boolean): void {
|
|
115
|
+
const trimmed = raw.trim();
|
|
116
|
+
const segments = trimmed.split("/").filter((segment) => segment !== "" && segment !== ".");
|
|
117
|
+
const endsAsFolder = trimmed.endsWith("/");
|
|
118
|
+
// An absolute path keeps its leading slash.
|
|
119
|
+
const lead = trimmed.startsWith("/") ? "/" : "";
|
|
120
|
+
let node = parent;
|
|
121
|
+
segments.forEach((segment, index) => {
|
|
122
|
+
let child = node.byName.get(segment);
|
|
123
|
+
if (child === undefined) {
|
|
124
|
+
const path = withPaths ? lead + segments.slice(0, index + 1).join("/") : null;
|
|
125
|
+
child = draft(`${node.id}${ID_SEPARATOR}p${segment}`, segment, path, true);
|
|
126
|
+
node.byName.set(segment, child);
|
|
127
|
+
node.children.push(child);
|
|
128
|
+
}
|
|
129
|
+
// A segment with something below it is a folder, whatever came first.
|
|
130
|
+
if (index < segments.length - 1 || endsAsFolder) child.folder = true;
|
|
131
|
+
node = child;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function addItems(parent: Draft, items: readonly unknown[], withPaths: boolean): void {
|
|
136
|
+
for (const item of items) {
|
|
137
|
+
if (typeof item === "string") {
|
|
138
|
+
addPath(parent, item, withPaths);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (!isObject(item) || isNode(item)) continue;
|
|
142
|
+
const label = text(item.label ?? item.name ?? null).trim();
|
|
143
|
+
if (label === "") continue;
|
|
144
|
+
const seen = parent.labelCounts.get(label) ?? 0;
|
|
145
|
+
parent.labelCounts.set(label, seen + 1);
|
|
146
|
+
const child = draft(`${parent.id}${ID_SEPARATOR}o${label}${seen === 0 ? "" : `#${seen}`}`, label, null, false);
|
|
147
|
+
child.note = (item.note ?? null) as Value | null;
|
|
148
|
+
if (Array.isArray(item.children)) {
|
|
149
|
+
child.folder = true;
|
|
150
|
+
addItems(child, item.children, false);
|
|
151
|
+
}
|
|
152
|
+
parent.children.push(child);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Path-built siblings go folders first, each keeping first-seen order; `{label}` items keep their place. */
|
|
157
|
+
function finish(children: readonly Draft[]): TreeItem[] {
|
|
158
|
+
const fromPaths = children.filter((child) => child.fromPath);
|
|
159
|
+
const ordered = [...fromPaths.filter((child) => child.folder), ...fromPaths.filter((child) => !child.folder)];
|
|
160
|
+
let next = 0;
|
|
161
|
+
return children.map((child) => {
|
|
162
|
+
const item = child.fromPath ? ordered[next++] : child;
|
|
163
|
+
return {
|
|
164
|
+
id: item.id,
|
|
165
|
+
label: item.label,
|
|
166
|
+
path: item.path,
|
|
167
|
+
note: item.note,
|
|
168
|
+
folder: item.folder,
|
|
169
|
+
children: finish(item.children),
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Builds a tree from path strings ("src/core/a.ts") and `{label, children?,
|
|
176
|
+
* note?}` items (children the same shape). Paths sharing folders merge,
|
|
177
|
+
* duplicates collapse, and empty or unreadable entries are dropped.
|
|
178
|
+
*/
|
|
179
|
+
export function buildTree(items: readonly unknown[]): TreeItem[] {
|
|
180
|
+
const root = draft("", "", null, false);
|
|
181
|
+
addItems(root, items, true);
|
|
182
|
+
return finish(root.children);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Folders open by default, breadth first: each level opens while the
|
|
187
|
+
* visible rows stay within `maxRows`, down to `maxDepth` levels of folders.
|
|
188
|
+
*/
|
|
189
|
+
export function defaultExpanded(roots: readonly TreeItem[], maxDepth = 3, maxRows = 50): Set<string> {
|
|
190
|
+
const open = new Set<string>();
|
|
191
|
+
let rows = roots.length;
|
|
192
|
+
let level = roots;
|
|
193
|
+
for (let depth = 0; depth < maxDepth && level.length > 0; depth++) {
|
|
194
|
+
const next: TreeItem[] = [];
|
|
195
|
+
for (const item of level) {
|
|
196
|
+
if (!item.folder || item.children.length === 0 || rows + item.children.length > maxRows) continue;
|
|
197
|
+
open.add(item.id);
|
|
198
|
+
rows += item.children.length;
|
|
199
|
+
next.push(...item.children);
|
|
200
|
+
}
|
|
201
|
+
level = next;
|
|
202
|
+
}
|
|
203
|
+
return open;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export interface TreeRow {
|
|
207
|
+
readonly item: TreeItem;
|
|
208
|
+
/** 0 for roots. */
|
|
209
|
+
readonly depth: number;
|
|
210
|
+
/** 1-based position among its siblings. */
|
|
211
|
+
readonly position: number;
|
|
212
|
+
readonly siblings: number;
|
|
213
|
+
readonly parent: string | null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The rows on screen, in order, given which folders are open. */
|
|
217
|
+
export function visibleRows(roots: readonly TreeItem[], isOpen: (id: string) => boolean): TreeRow[] {
|
|
218
|
+
const rows: TreeRow[] = [];
|
|
219
|
+
const walk = (items: readonly TreeItem[], depth: number, parent: string | null) => {
|
|
220
|
+
items.forEach((item, index) => {
|
|
221
|
+
rows.push({ item, depth, position: index + 1, siblings: items.length, parent });
|
|
222
|
+
if (item.folder && isOpen(item.id)) walk(item.children, depth + 1, item.id);
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
walk(roots, 0, null);
|
|
226
|
+
return rows;
|
|
227
|
+
}
|