basalt-ui 2.22.0 → 2.24.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/CHANGELOG.md +68 -0
- package/MIGRATING.md +400 -12
- package/agent/agents/basalt-design-reviewer.md +3 -3
- package/agent/skills/basalt-design/SKILL.md +2 -2
- package/agent/skills/basalt-design/references/catalogue.md +22 -19
- package/agent/skills/basalt-design/references/states.md +39 -0
- package/dist/charts/BandStrip.d.ts.map +1 -1
- package/dist/charts/BandStrip.js +51 -5
- package/dist/charts/Bars.d.ts.map +1 -1
- package/dist/charts/Bars.js +19 -3
- package/dist/charts/CartesianChart.d.ts +2 -0
- package/dist/charts/CartesianChart.d.ts.map +1 -1
- package/dist/charts/CartesianChart.js +7 -3
- package/dist/charts/ChartCard.d.ts +1 -1
- package/dist/charts/ChartCard.d.ts.map +1 -1
- package/dist/charts/ChartCard.js +25 -15
- package/dist/charts/Sparkline.d.ts +8 -2
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +95 -15
- package/dist/charts/StackedArea.d.ts.map +1 -1
- package/dist/charts/StackedArea.js +5 -1
- package/dist/charts/band-fold.d.ts +22 -4
- package/dist/charts/band-fold.d.ts.map +1 -1
- package/dist/charts/band-fold.js +10 -2
- package/dist/charts/chart-dates.d.ts +12 -0
- package/dist/charts/chart-dates.d.ts.map +1 -0
- package/dist/charts/chart-dates.js +16 -0
- package/dist/charts/chart-legend.module.css +16 -0
- package/dist/charts/compile.d.ts +10 -0
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +77 -24
- package/dist/charts/date-tick-format.d.ts +5 -5
- package/dist/charts/date-tick-format.d.ts.map +1 -1
- package/dist/charts/date-tick-format.js +3 -7
- package/dist/charts/labels.d.ts +7 -3
- package/dist/charts/labels.d.ts.map +1 -1
- package/dist/charts/labels.js +28 -17
- package/dist/charts/legend.d.ts +0 -6
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +87 -35
- package/dist/charts/ref-label.d.ts +17 -0
- package/dist/charts/ref-label.d.ts.map +1 -1
- package/dist/charts/ref-label.js +13 -1
- package/dist/charts/sparkline.module.css +62 -12
- package/dist/charts/sparkline.module.css.d.ts +1 -0
- package/dist/charts/state.d.ts.map +1 -1
- package/dist/charts/state.js +4 -2
- package/dist/charts/types.d.ts +15 -3
- package/dist/charts/types.d.ts.map +1 -1
- package/dist/cli/shot/capture.d.ts.map +1 -1
- package/dist/cli/shot/capture.js +9 -5
- package/dist/cli/shot/detect.js +8 -5
- package/dist/cli/shot/measure.js +19 -23
- package/dist/cli/shot/playwright.d.ts +8 -0
- package/dist/cli/shot/playwright.d.ts.map +1 -1
- package/dist/cli/shot/playwright.js +5 -1
- package/dist/cli/shot/types.d.ts +2 -0
- package/dist/cli/shot/types.d.ts.map +1 -1
- package/dist/cli/tokens-css.d.ts +5 -7
- package/dist/cli/tokens-css.d.ts.map +1 -1
- package/dist/cli/tokens-css.js +11 -12
- package/dist/dashboard/bar-list.d.ts +6 -1
- package/dist/dashboard/bar-list.d.ts.map +1 -1
- package/dist/dashboard/bar-list.js +8 -0
- package/dist/dashboard/bar-list.module.css +18 -0
- package/dist/dashboard/delta-badge.d.ts.map +1 -1
- package/dist/dashboard/delta-badge.js +2 -2
- package/dist/dashboard/empty-state.d.ts.map +1 -1
- package/dist/dashboard/meter-model.d.ts +55 -21
- package/dist/dashboard/meter-model.d.ts.map +1 -1
- package/dist/dashboard/meter-model.js +98 -17
- package/dist/dashboard/meter-parts.d.ts +9 -2
- package/dist/dashboard/meter-parts.d.ts.map +1 -1
- package/dist/dashboard/meter-parts.js +99 -21
- package/dist/dashboard/meter.d.ts +21 -8
- package/dist/dashboard/meter.d.ts.map +1 -1
- package/dist/dashboard/meter.js +14 -3
- package/dist/dashboard/meter.module.css +57 -2
- package/dist/dashboard/meter.module.css.d.ts +5 -0
- package/dist/dashboard/query-state.d.ts +7 -3
- package/dist/dashboard/query-state.d.ts.map +1 -1
- package/dist/dashboard/query-state.js +15 -7
- package/dist/dashboard/section.d.ts +2 -1
- package/dist/dashboard/section.d.ts.map +1 -1
- package/dist/dashboard/section.module.css +12 -0
- package/dist/dashboard/settings-section.d.ts +1 -1
- package/dist/dashboard/settings-section.d.ts.map +1 -1
- package/dist/dashboard/settings-section.js +3 -3
- package/dist/dashboard/stat-card-parts.js +1 -1
- package/dist/dashboard/stat-card.module.css +43 -10
- package/dist/dashboard/stat-group.module.css +15 -0
- package/dist/dashboard/stat-strip.d.ts +16 -5
- package/dist/dashboard/stat-strip.d.ts.map +1 -1
- package/dist/dashboard/stat-strip.js +100 -74
- package/dist/dashboard/stat-strip.module.css +68 -62
- package/dist/dashboard/stat-strip.module.css.d.ts +1 -0
- package/dist/dashboard/widget-header.d.ts.map +1 -1
- package/dist/dashboard/widget-header.js +19 -1
- package/dist/dashboard/widget-header.module.css +6 -0
- package/dist/guard/index.d.ts +2 -2
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +1 -17
- package/dist/styles.css +18 -11
- package/dist/table/data-table.js +1 -1
- package/dist/theme/controls.module.css +19 -0
- package/dist/theme/controls.module.css.d.ts +1 -0
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +12 -7
- package/dist/theme/status-verdict.d.ts.map +1 -1
- package/dist/theme/status-verdict.js +9 -1
- package/dist/tokens/palette.d.ts +7 -10
- package/dist/tokens/palette.d.ts.map +1 -1
- package/dist/tokens/palette.js +7 -10
- package/llms.txt +7 -4
- package/package.json +1 -1
- package/src/charts/BandStrip.tsx +55 -5
- package/src/charts/Bars.tsx +31 -3
- package/src/charts/CartesianChart.tsx +9 -3
- package/src/charts/ChartCard.tsx +32 -19
- package/src/charts/Sparkline.tsx +139 -21
- package/src/charts/StackedArea.tsx +12 -1
- package/src/charts/band-fold.ts +25 -6
- package/src/charts/chart-dates.ts +30 -0
- package/src/charts/chart-legend.module.css +16 -0
- package/src/charts/compile.ts +148 -25
- package/src/charts/date-tick-format.ts +10 -13
- package/src/charts/labels.ts +43 -22
- package/src/charts/legend.tsx +70 -3
- package/src/charts/ref-label.ts +27 -0
- package/src/charts/sparkline.module.css +62 -12
- package/src/charts/sparkline.module.css.d.ts +1 -0
- package/src/charts/state.tsx +7 -4
- package/src/charts/types.ts +13 -3
- package/src/cli/shot/capture.ts +15 -3
- package/src/cli/shot/detect.ts +9 -9
- package/src/cli/shot/measure.ts +27 -27
- package/src/cli/shot/playwright.ts +18 -2
- package/src/cli/shot/types.ts +2 -0
- package/src/cli/tokens-css.ts +18 -20
- package/src/dashboard/bar-list.module.css +18 -0
- package/src/dashboard/bar-list.tsx +29 -0
- package/src/dashboard/delta-badge.tsx +6 -2
- package/src/dashboard/empty-state.tsx +4 -2
- package/src/dashboard/meter-model.ts +181 -47
- package/src/dashboard/meter-parts.tsx +187 -30
- package/src/dashboard/meter.module.css +57 -2
- package/src/dashboard/meter.module.css.d.ts +5 -0
- package/src/dashboard/meter.tsx +33 -10
- package/src/dashboard/query-state.tsx +20 -9
- package/src/dashboard/section.module.css +12 -0
- package/src/dashboard/section.tsx +2 -1
- package/src/dashboard/settings-section.tsx +4 -4
- package/src/dashboard/stat-card-parts.tsx +1 -1
- package/src/dashboard/stat-card.module.css +43 -10
- package/src/dashboard/stat-group.module.css +15 -0
- package/src/dashboard/stat-strip.module.css +68 -62
- package/src/dashboard/stat-strip.module.css.d.ts +1 -0
- package/src/dashboard/stat-strip.tsx +118 -59
- package/src/dashboard/widget-header.module.css +6 -0
- package/src/dashboard/widget-header.tsx +28 -1
- package/src/guard/index.ts +3 -19
- package/src/styles.css +18 -11
- package/src/table/data-table.tsx +1 -1
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +14 -8
- package/src/theme/controls.module.css +19 -0
- package/src/theme/controls.module.css.d.ts +1 -0
- package/src/theme/index.ts +12 -7
- package/src/theme/status-verdict.tsx +5 -1
- package/src/tokens/palette.ts +7 -10
|
@@ -4,13 +4,15 @@
|
|
|
4
4
|
* generous vertical padding for a full-page empty state; `tier="section"` is compact, for an
|
|
5
5
|
* empty card/panel region.
|
|
6
6
|
*
|
|
7
|
+
* Its copy says the cause and the next step, never a bare "No data".
|
|
8
|
+
*
|
|
7
9
|
* @example
|
|
8
10
|
* import { EmptyState } from 'basalt-ui'
|
|
9
11
|
*
|
|
10
12
|
* <EmptyState
|
|
11
13
|
* icon={<IconInboxEmpty />}
|
|
12
|
-
* title="No
|
|
13
|
-
* description="
|
|
14
|
+
* title="No decisions match"
|
|
15
|
+
* description="The queue is filtered to Blocked; clear it to see all 12."
|
|
14
16
|
* action={<Button onClick={onReset}>Clear filters</Button>}
|
|
15
17
|
* tier="section"
|
|
16
18
|
* />
|
|
@@ -10,8 +10,8 @@ export type MeterSegment = {
|
|
|
10
10
|
label: string
|
|
11
11
|
color?: string
|
|
12
12
|
/**
|
|
13
|
-
* `'muted'`: the part takes
|
|
14
|
-
*
|
|
13
|
+
* `'muted'`: the part takes the one flat muted fill every muted part shares, a hairline seam
|
|
14
|
+
* between neighbours (never a lightness step). A `color` wins.
|
|
15
15
|
*/
|
|
16
16
|
tone?: 'muted'
|
|
17
17
|
forecast?: boolean
|
|
@@ -21,8 +21,8 @@ export type MeterZone = {
|
|
|
21
21
|
to: number
|
|
22
22
|
label: string
|
|
23
23
|
/**
|
|
24
|
-
* `'muted'`: the band takes
|
|
25
|
-
*
|
|
24
|
+
* `'muted'`: the band takes the one flat muted fill every muted zone shares, a hairline gap
|
|
25
|
+
* between neighbours (never a lightness step): a non-verdict band is not a value bar. A `color` wins.
|
|
26
26
|
*/
|
|
27
27
|
tone?: 'muted'
|
|
28
28
|
/**
|
|
@@ -32,22 +32,27 @@ export type MeterZone = {
|
|
|
32
32
|
short?: string
|
|
33
33
|
color?: string
|
|
34
34
|
}
|
|
35
|
-
/**
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* A mark on the track at `at` (in the reading's units). A `label` is captioned beside it, above the
|
|
37
|
+
* track; a `caption` sits under the track, centred on the mark and clamped inside the track.
|
|
38
|
+
*/
|
|
39
|
+
export type MeterTick = { at: number; label?: string; caption?: string }
|
|
37
40
|
|
|
38
41
|
/** A colour of a `stops` gradient at `at` (in the reading's units); the colour is the consumer's. */
|
|
39
42
|
type MeterStop = { at: number; color: string }
|
|
40
43
|
|
|
41
44
|
/** A ramp step's strength in percent of its neutral: from `lo`, spread over `step` across the steps. */
|
|
42
45
|
export const RAMP_ZONE = { lo: 15, step: 45, solo: 40 }
|
|
43
|
-
|
|
46
|
+
/** The flat muted fill, in percent of `--vx-muted`: a zone band (argo's alpha) and a segment (clears 3:1 on the track). */
|
|
47
|
+
const MUTED_ZONE = 35
|
|
48
|
+
const MUTED_PART = 65
|
|
44
49
|
/** Neighbouring steps stay at least this far apart (the contrast the zone ramp always had), up to `RAMP_MAX`. */
|
|
45
50
|
export const RAMP_MIN_STEP = 15
|
|
46
51
|
export const RAMP_MAX = 95
|
|
47
52
|
|
|
48
53
|
/**
|
|
49
54
|
* Step `index` of `count`. The spread is floored at `RAMP_MIN_STEP` per step, so a ramp stays
|
|
50
|
-
* distinguishable by lightness up to six zones
|
|
55
|
+
* distinguishable by lightness up to six zones; past that the last steps clamp at
|
|
51
56
|
* `RAMP_MAX` and merge, which is the cue to group parts.
|
|
52
57
|
*/
|
|
53
58
|
export function rampStrength(index: number, count: number, ramp: typeof RAMP_ZONE): number {
|
|
@@ -55,22 +60,26 @@ export function rampStrength(index: number, count: number, ramp: typeof RAMP_ZON
|
|
|
55
60
|
return Math.min(RAMP_MAX, ramp.lo + index * Math.max(RAMP_MIN_STEP, ramp.step / (count - 1)))
|
|
56
61
|
}
|
|
57
62
|
|
|
58
|
-
function rampFill(
|
|
59
|
-
base: 'line' | 'muted',
|
|
60
|
-
index: number,
|
|
61
|
-
count: number,
|
|
62
|
-
ramp: typeof RAMP_ZONE,
|
|
63
|
-
): string {
|
|
63
|
+
function rampFill(base: 'line', index: number, count: number, ramp: typeof RAMP_ZONE): string {
|
|
64
64
|
return `color-mix(in srgb, var(--vx-${base}) ${rampStrength(index, count, ramp)}%, transparent)`
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
const mutedFill = (pct: number) => `color-mix(in srgb, var(--vx-muted) ${pct}%, transparent)`
|
|
68
|
+
|
|
69
|
+
/** A zone's fill: its own colour, else the one flat muted fill (`tone="muted"`), else its step of the line ramp. */
|
|
68
70
|
export function zoneFill(zone: MeterZone, index: number, count: number): string {
|
|
69
|
-
|
|
71
|
+
if (zone.color !== undefined) return zone.color
|
|
72
|
+
return zone.tone === 'muted' ? mutedFill(MUTED_ZONE) : rampFill('line', index, count, RAMP_ZONE)
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
/** One tick on the track: its percent position and what it says (caption `label`, aria `text`). */
|
|
73
|
-
export type TickMark = {
|
|
76
|
+
export type TickMark = {
|
|
77
|
+
key: string
|
|
78
|
+
pct: number
|
|
79
|
+
label: string | undefined
|
|
80
|
+
caption: string | undefined
|
|
81
|
+
text: string
|
|
82
|
+
}
|
|
74
83
|
|
|
75
84
|
/** A fill on the track, as fractions (0–1) of the span, with the segment it was laid out from. */
|
|
76
85
|
export type Part = {
|
|
@@ -78,6 +87,8 @@ export type Part = {
|
|
|
78
87
|
readonly size: number
|
|
79
88
|
readonly color: string | undefined
|
|
80
89
|
readonly forecast: boolean
|
|
90
|
+
/** A muted part right after another muted part: a hairline seam keeps the shared fill apart. */
|
|
91
|
+
readonly seam: boolean
|
|
81
92
|
readonly label: string
|
|
82
93
|
/** The segment's own (normalised) amount, in the reading's units — what its caption states. */
|
|
83
94
|
readonly amount: number
|
|
@@ -130,6 +141,7 @@ export function tickMarks(
|
|
|
130
141
|
key: String(i),
|
|
131
142
|
pct: toPct((t.at - min) / span),
|
|
132
143
|
label: t.label,
|
|
144
|
+
caption: t.caption,
|
|
133
145
|
text: `${t.label ?? 'Tick'} ${format(t.at)}`,
|
|
134
146
|
},
|
|
135
147
|
]
|
|
@@ -197,8 +209,7 @@ export function normalizeSegments(segments: readonly MeterSegment[]): MeterSegme
|
|
|
197
209
|
export function layoutSegments(segments: readonly MeterSegment[], span: number): Part[] {
|
|
198
210
|
let cursor = 0
|
|
199
211
|
const isMuted = (s: MeterSegment) => s.tone === 'muted' && s.color === undefined && !s.forecast
|
|
200
|
-
|
|
201
|
-
let muted = 0
|
|
212
|
+
let prevMuted = false
|
|
202
213
|
return segments.map((s) => {
|
|
203
214
|
const size = Math.max(0, Math.min(clamp01(s.value / span), 1 - cursor))
|
|
204
215
|
const forecast = s.forecast === true
|
|
@@ -206,16 +217,14 @@ export function layoutSegments(segments: readonly MeterSegment[], span: number):
|
|
|
206
217
|
const part = {
|
|
207
218
|
start: cursor,
|
|
208
219
|
size,
|
|
209
|
-
color: forecast
|
|
210
|
-
? undefined
|
|
211
|
-
: isMuted(s)
|
|
212
|
-
? rampFill('muted', muted++, mutedCount, RAMP_PART)
|
|
213
|
-
: s.color,
|
|
220
|
+
color: forecast ? undefined : isMuted(s) ? mutedFill(MUTED_PART) : s.color,
|
|
214
221
|
forecast,
|
|
222
|
+
seam: isMuted(s) && prevMuted,
|
|
215
223
|
label: s.label,
|
|
216
224
|
amount: s.value,
|
|
217
225
|
}
|
|
218
226
|
cursor += size
|
|
227
|
+
prevMuted = isMuted(s)
|
|
219
228
|
return part
|
|
220
229
|
})
|
|
221
230
|
}
|
|
@@ -240,7 +249,7 @@ export type RowLabel = {
|
|
|
240
249
|
box: LabelBox
|
|
241
250
|
span?: LabelBox
|
|
242
251
|
holding?: boolean
|
|
243
|
-
/** A tick at the track's start or end: it
|
|
252
|
+
/** A tick at the track's start or end: it keeps its place over every zone name it would touch. */
|
|
244
253
|
end?: boolean
|
|
245
254
|
short?: LabelBox
|
|
246
255
|
}
|
|
@@ -257,11 +266,12 @@ export type RowPlacement = {
|
|
|
257
266
|
/**
|
|
258
267
|
* Which labels of the row show, in input order. The holding zone's name claims its place first (it
|
|
259
268
|
* is the reading's verdict, so it never hides for width: it may spill past its span, shifted inside
|
|
260
|
-
* the row); the other ticks go next
|
|
261
|
-
* when it lies inside the row and its own span and
|
|
262
|
-
* name
|
|
263
|
-
*
|
|
264
|
-
*
|
|
269
|
+
* the row); the other ticks go next, then the end ticks (at the track's start or end: a scale's
|
|
270
|
+
* figures); every other zone name then shows only when it lies inside the row and its own span and
|
|
271
|
+
* clears every label already kept, trying its full name, then its `short` box, then hiding. An end
|
|
272
|
+
* figure hides only when it cannot clear the holding name, an interior tick or the other end. A tick
|
|
273
|
+
* keeps at least `gap` px (one gap step of the host's spacing) from every other label; zone names
|
|
274
|
+
* among themselves only must not touch. A hidden name is still named in the described-by text.
|
|
265
275
|
*/
|
|
266
276
|
export function placeRowLabels(
|
|
267
277
|
labels: readonly RowLabel[],
|
|
@@ -272,7 +282,7 @@ export function placeRowLabels(
|
|
|
272
282
|
const short = labels.map(() => false)
|
|
273
283
|
const shift = labels.map(() => 0)
|
|
274
284
|
const kept: { box: LabelBox; tick: boolean }[] = []
|
|
275
|
-
const rank = (l: RowLabel) => (l.holding ? 0 : l.
|
|
285
|
+
const rank = (l: RowLabel) => (l.holding ? 0 : l.span ? 3 : l.end ? 2 : 1)
|
|
276
286
|
const order = labels
|
|
277
287
|
.map((_, i) => i)
|
|
278
288
|
.toSorted((a, b) => rank(labels[a] as RowLabel) - rank(labels[b] as RowLabel))
|
|
@@ -310,6 +320,14 @@ export function placeRowLabels(
|
|
|
310
320
|
short[i] = useShort
|
|
311
321
|
kept.push({ box, tick })
|
|
312
322
|
}
|
|
323
|
+
// One register: once any name falls back to its `short`, every other shown name that has one
|
|
324
|
+
// follows (the holding name, which never shortens, is the one exception). A shorter box only
|
|
325
|
+
// clears more, so the placement above stays valid.
|
|
326
|
+
if (short.some(Boolean)) {
|
|
327
|
+
for (const [i, label] of labels.entries()) {
|
|
328
|
+
if (shown[i] && !label.holding && label.short !== undefined) short[i] = true
|
|
329
|
+
}
|
|
330
|
+
}
|
|
313
331
|
return { shown, short, shift }
|
|
314
332
|
}
|
|
315
333
|
|
|
@@ -353,6 +371,8 @@ export type BarModel = {
|
|
|
353
371
|
zoneLabels: readonly ZoneLabel[]
|
|
354
372
|
ticks: readonly TickMark[]
|
|
355
373
|
markerPct: number | undefined
|
|
374
|
+
/** Percent where the committed fill ends: the reading caption's anchor. Absent on a marked track. */
|
|
375
|
+
fillEndPct: number | undefined
|
|
356
376
|
targetPct: number | undefined
|
|
357
377
|
/** The default start caption (the zone is named on the band, so it is the bare reading); `startCaption` decides whether it shows. */
|
|
358
378
|
startText: string
|
|
@@ -465,6 +485,12 @@ export function modelBar(input: BarInput): BarModel {
|
|
|
465
485
|
}),
|
|
466
486
|
ticks,
|
|
467
487
|
markerPct: zoned || striped ? toPct((reading - min) / span) : undefined,
|
|
488
|
+
fillEndPct:
|
|
489
|
+
zoned || striped
|
|
490
|
+
? undefined
|
|
491
|
+
: toPct(
|
|
492
|
+
parts.filter((p) => !p.forecast).reduce((end, p) => Math.max(end, p.start + p.size), 0),
|
|
493
|
+
),
|
|
468
494
|
targetPct: target === undefined ? undefined : toPct((target - min) / span),
|
|
469
495
|
startText: format(reading),
|
|
470
496
|
endText: endCaptionText(reading, target, format),
|
|
@@ -476,14 +502,34 @@ export function modelBar(input: BarInput): BarModel {
|
|
|
476
502
|
export const ringFraction = (reading: number, min: number, max: number): number =>
|
|
477
503
|
clamp01((reading - min) / spanOf(min, max).span)
|
|
478
504
|
|
|
479
|
-
/**
|
|
480
|
-
|
|
505
|
+
/**
|
|
506
|
+
* One caption on the marks row: its natural width and where it is anchored (`pct`), else its edge.
|
|
507
|
+
* `align` is which edge of the caption sits on the mark (default: its centre). `drop` ranks it for
|
|
508
|
+
* dropping when the row is too narrow: the lowest rank goes first, none never drops.
|
|
509
|
+
*/
|
|
510
|
+
export type MarkItem = {
|
|
511
|
+
width: number
|
|
512
|
+
pct?: number
|
|
513
|
+
edge?: 'end'
|
|
514
|
+
align?: 'start' | 'center' | 'end'
|
|
515
|
+
drop?: number
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** Where a caption starts before any caption is pushed off it: on its mark, else at its edge. */
|
|
519
|
+
function idealLeft(i: MarkItem, rowWidth: number): number {
|
|
520
|
+
if (i.pct === undefined) return i.edge === 'end' ? rowWidth - i.width : 0
|
|
521
|
+
const at = (rowWidth * i.pct) / 100
|
|
522
|
+
const left = i.align === 'end' ? at - i.width : i.align === 'start' ? at : at - i.width / 2
|
|
523
|
+
return Math.max(0, Math.min(left, rowWidth - i.width))
|
|
524
|
+
}
|
|
481
525
|
|
|
482
526
|
/**
|
|
483
527
|
* Where each caption of the marks row starts (px, logical start edge), index-aligned with `items`: an
|
|
484
528
|
* anchored caption is centred on its mark and clamped inside the row, the others sit at the start (or,
|
|
485
|
-
* `edge: 'end'`, the end). `
|
|
486
|
-
*
|
|
529
|
+
* `edge: 'end'`, the end). Two captions closer than `gap` are pushed apart, in order, the least that
|
|
530
|
+
* clears them, so a caption stays as near its mark as the row allows. `null` when nothing is anchored,
|
|
531
|
+
* the row is unmeasured, a caption is wider than the row, or the captions cannot all clear each other
|
|
532
|
+
* inside the row: the row then flows and wraps instead.
|
|
487
533
|
*/
|
|
488
534
|
export function placeMarkCaptions(
|
|
489
535
|
items: readonly MarkItem[],
|
|
@@ -492,19 +538,107 @@ export function placeMarkCaptions(
|
|
|
492
538
|
): number[] | null {
|
|
493
539
|
if (rowWidth <= 0 || !items.some((i) => i.pct !== undefined)) return null
|
|
494
540
|
if (items.some((i) => i.width > rowWidth)) return null
|
|
495
|
-
const lefts = items.map((i) =>
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
const order = lefts
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
541
|
+
const lefts = items.map((i) => idealLeft(i, rowWidth))
|
|
542
|
+
// Captions keep their order along the track: by their mark (an edge caption by its edge), not by
|
|
543
|
+
// where clamping left them, so a reading near the end never ends up right of the target.
|
|
544
|
+
const markAt = (i: MarkItem) => i.pct ?? (i.edge === 'end' ? 100 : 0)
|
|
545
|
+
const order = lefts
|
|
546
|
+
.map((_, i) => i)
|
|
547
|
+
.toSorted((a, b) => markAt(items[a] as MarkItem) - markAt(items[b] as MarkItem) || a - b)
|
|
548
|
+
const widthOf = (i: number) => items[i]?.width ?? 0
|
|
549
|
+
for (const [n, i] of order.entries()) {
|
|
550
|
+
const prev = order[n - 1]
|
|
551
|
+
if (prev !== undefined)
|
|
552
|
+
lefts[i] = Math.max(lefts[i] ?? 0, (lefts[prev] ?? 0) + widthOf(prev) + gap)
|
|
553
|
+
}
|
|
554
|
+
for (const [n, i] of order.toReversed().entries()) {
|
|
555
|
+
const next = order.toReversed()[n - 1]
|
|
556
|
+
const limit = next === undefined ? rowWidth : (lefts[next] ?? 0) - gap
|
|
557
|
+
lefts[i] = Math.min(lefts[i] ?? 0, limit - widthOf(i))
|
|
558
|
+
}
|
|
559
|
+
const first = order[0]
|
|
560
|
+
return first !== undefined && (lefts[first] ?? 0) < -LABEL_SUBPIXEL ? null : lefts
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** The furthest a droppable caption may sit from its own mark (Wave 37 r2's "not far"); past it, it drops. */
|
|
564
|
+
export const MAX_CAPTION_SHIFT = 24
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* `placeMarkCaptions` with dropping: when the captions cannot all clear each other on one line, the
|
|
568
|
+
* droppable ones go one at a time in `drop` rank order (a caption without a rank never goes) until
|
|
569
|
+
* the rest fit. A droppable caption stays while it sits within `MAX_CAPTION_SHIFT` of its mark and
|
|
570
|
+
* moves no undroppable caption (the reading, the target) further than those move with every droppable
|
|
571
|
+
* one dropped: two undroppables that collide on their own are not a reason to drop anything.
|
|
572
|
+
* `starts[i]` is the start of caption `i`, `undefined` when it dropped; `dropped[i]` marks the drop.
|
|
573
|
+
* `null` when even the undroppable ones do not fit, so the row flows instead.
|
|
574
|
+
*/
|
|
575
|
+
export function placeCaptionRow(
|
|
576
|
+
items: readonly MarkItem[],
|
|
577
|
+
rowWidth: number,
|
|
578
|
+
gap: number,
|
|
579
|
+
): { starts: (number | undefined)[]; dropped: boolean[] } | null {
|
|
580
|
+
const fixed = items.map((_, i) => i).filter((i) => items[i]?.drop === undefined)
|
|
581
|
+
const baseline = placeMarkCaptions(
|
|
582
|
+
fixed.map((i) => items[i] as MarkItem),
|
|
583
|
+
rowWidth,
|
|
584
|
+
gap,
|
|
585
|
+
)
|
|
586
|
+
if (baseline === null) return null
|
|
587
|
+
const allowed = new Map(
|
|
588
|
+
fixed.map((i, n) => [
|
|
589
|
+
i,
|
|
590
|
+
Math.abs((baseline[n] ?? 0) - idealLeft(items[i] as MarkItem, rowWidth)) + LABEL_SUBPIXEL,
|
|
591
|
+
]),
|
|
592
|
+
)
|
|
593
|
+
const dropped = items.map(() => false)
|
|
594
|
+
const order = items
|
|
595
|
+
.map((item, i) => ({ rank: item.drop, i }))
|
|
596
|
+
.filter((d): d is { rank: number; i: number } => d.rank !== undefined)
|
|
597
|
+
.toSorted((a, b) => a.rank - b.rank)
|
|
598
|
+
for (let step = 0; step <= order.length; step++) {
|
|
599
|
+
const kept = items.map((_, i) => i).filter((i) => !dropped[i])
|
|
600
|
+
const lefts = placeMarkCaptions(
|
|
601
|
+
kept.map((i) => items[i] as MarkItem),
|
|
602
|
+
rowWidth,
|
|
603
|
+
gap,
|
|
505
604
|
)
|
|
506
|
-
|
|
507
|
-
|
|
605
|
+
const settled =
|
|
606
|
+
lefts !== null &&
|
|
607
|
+
(step === order.length ||
|
|
608
|
+
kept.every((i, n) => {
|
|
609
|
+
const item = items[i] as MarkItem
|
|
610
|
+
const moved = Math.abs((lefts[n] ?? 0) - idealLeft(item, rowWidth))
|
|
611
|
+
if (item.drop === undefined) return moved <= (allowed.get(i) ?? 0)
|
|
612
|
+
return item.pct === undefined || moved <= MAX_CAPTION_SHIFT
|
|
613
|
+
}))
|
|
614
|
+
if (lefts !== null && settled) {
|
|
615
|
+
const starts = items.map((): number | undefined => undefined)
|
|
616
|
+
for (const [n, i] of kept.entries()) starts[i] = lefts[n]
|
|
617
|
+
return { starts, dropped }
|
|
618
|
+
}
|
|
619
|
+
const next = order[step]
|
|
620
|
+
if (next === undefined) return null
|
|
621
|
+
dropped[next.i] = true
|
|
622
|
+
}
|
|
623
|
+
return null
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* How many columns the categorical part captions take when they do not fit under their segments:
|
|
628
|
+
* the most columns whose widest-per-column entries fit the row, with no lone entry on the last row
|
|
629
|
+
* (`n % cols === 1`). All in one row when they fit; one column is always valid.
|
|
630
|
+
*/
|
|
631
|
+
export function partColumns(widths: readonly number[], rowWidth: number, gap: number): number {
|
|
632
|
+
const n = widths.length
|
|
633
|
+
for (let cols = n; cols > 1; cols--) {
|
|
634
|
+
if (n > cols && n % cols === 1) continue
|
|
635
|
+
const widest = Array.from({ length: cols }, (_, c) =>
|
|
636
|
+
Math.max(0, ...widths.filter((_w, i) => i % cols === c)),
|
|
637
|
+
)
|
|
638
|
+
const total = widest.reduce((a, b) => a + b, 0) + gap * (cols - 1)
|
|
639
|
+
if (total <= rowWidth + LABEL_SUBPIXEL) return cols
|
|
640
|
+
}
|
|
641
|
+
return 1
|
|
508
642
|
}
|
|
509
643
|
|
|
510
644
|
/**
|