@copilotkit/shared 1.84.2-canary.openbot-intel-fixes → 1.86.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/dist/event-transforms/recorded-events.cjs +7 -1
- package/dist/event-transforms/recorded-events.cjs.map +1 -1
- package/dist/event-transforms/recorded-events.mjs +8 -2
- package/dist/event-transforms/recorded-events.mjs.map +1 -1
- package/dist/generative-ui/clone-value.cjs +364 -0
- package/dist/generative-ui/clone-value.cjs.map +1 -0
- package/dist/generative-ui/clone-value.d.cts +51 -0
- package/dist/generative-ui/clone-value.d.cts.map +1 -0
- package/dist/generative-ui/clone-value.d.mts +51 -0
- package/dist/generative-ui/clone-value.d.mts.map +1 -0
- package/dist/generative-ui/clone-value.mjs +363 -0
- package/dist/generative-ui/clone-value.mjs.map +1 -0
- package/dist/generative-ui/index.cjs +39 -0
- package/dist/generative-ui/index.d.cts +8 -2
- package/dist/generative-ui/index.d.mts +8 -2
- package/dist/generative-ui/index.mjs +8 -2
- package/dist/generative-ui/is-record.d.cts +3 -1
- package/dist/generative-ui/is-record.d.cts.map +1 -1
- package/dist/generative-ui/is-record.d.mts +3 -1
- package/dist/generative-ui/is-record.d.mts.map +1 -1
- package/dist/generative-ui/schema-render.cjs +2 -1
- package/dist/generative-ui/schema-render.cjs.map +1 -1
- package/dist/generative-ui/schema-render.d.cts.map +1 -1
- package/dist/generative-ui/schema-render.d.mts.map +1 -1
- package/dist/generative-ui/schema-render.mjs +2 -1
- package/dist/generative-ui/schema-render.mjs.map +1 -1
- package/dist/generative-ui/standard/chart-config.cjs +347 -0
- package/dist/generative-ui/standard/chart-config.cjs.map +1 -0
- package/dist/generative-ui/standard/chart-config.d.cts +97 -0
- package/dist/generative-ui/standard/chart-config.d.cts.map +1 -0
- package/dist/generative-ui/standard/chart-config.d.mts +97 -0
- package/dist/generative-ui/standard/chart-config.d.mts.map +1 -0
- package/dist/generative-ui/standard/chart-config.mjs +342 -0
- package/dist/generative-ui/standard/chart-config.mjs.map +1 -0
- package/dist/generative-ui/standard/chart.d.cts +10 -10
- package/dist/generative-ui/standard/chart.d.mts +10 -10
- package/dist/generative-ui/standard/composites.d.cts +46 -46
- package/dist/generative-ui/standard/composites.d.mts +46 -46
- package/dist/generative-ui/standard/data.d.cts +42 -42
- package/dist/generative-ui/standard/data.d.mts +42 -42
- package/dist/generative-ui/standard/declarations.d.cts +226 -226
- package/dist/generative-ui/standard/declarations.d.mts +226 -226
- package/dist/generative-ui/standard/feedback.d.cts +4 -4
- package/dist/generative-ui/standard/feedback.d.mts +4 -4
- package/dist/generative-ui/standard/index.d.mts +5 -1
- package/dist/generative-ui/standard/inputs.d.cts +72 -72
- package/dist/generative-ui/standard/inputs.d.mts +72 -72
- package/dist/generative-ui/standard/layout.d.cts +156 -156
- package/dist/generative-ui/standard/layout.d.mts +156 -156
- package/dist/generative-ui/standard/map-helpers.cjs +104 -0
- package/dist/generative-ui/standard/map-helpers.cjs.map +1 -0
- package/dist/generative-ui/standard/map-helpers.d.cts +88 -0
- package/dist/generative-ui/standard/map-helpers.d.cts.map +1 -0
- package/dist/generative-ui/standard/map-helpers.d.mts +88 -0
- package/dist/generative-ui/standard/map-helpers.d.mts.map +1 -0
- package/dist/generative-ui/standard/map-helpers.mjs +95 -0
- package/dist/generative-ui/standard/map-helpers.mjs.map +1 -0
- package/dist/generative-ui/standard/map.d.cts +10 -10
- package/dist/generative-ui/standard/map.d.mts +10 -10
- package/dist/generative-ui/standard/media.d.cts +76 -76
- package/dist/generative-ui/standard/media.d.mts +76 -76
- package/dist/generative-ui/standard/style-helpers.cjs +151 -0
- package/dist/generative-ui/standard/style-helpers.cjs.map +1 -0
- package/dist/generative-ui/standard/style-helpers.d.cts +56 -0
- package/dist/generative-ui/standard/style-helpers.d.cts.map +1 -0
- package/dist/generative-ui/standard/style-helpers.d.mts +56 -0
- package/dist/generative-ui/standard/style-helpers.d.mts.map +1 -0
- package/dist/generative-ui/standard/style-helpers.mjs +142 -0
- package/dist/generative-ui/standard/style-helpers.mjs.map +1 -0
- package/dist/generative-ui/standard/text.d.cts +28 -28
- package/dist/generative-ui/standard/text.d.mts +28 -28
- package/dist/generative-ui/standard/url.cjs +54 -0
- package/dist/generative-ui/standard/url.cjs.map +1 -0
- package/dist/generative-ui/standard/url.d.cts +32 -0
- package/dist/generative-ui/standard/url.d.cts.map +1 -0
- package/dist/generative-ui/standard/url.d.mts +32 -0
- package/dist/generative-ui/standard/url.d.mts.map +1 -0
- package/dist/generative-ui/standard/url.mjs +50 -0
- package/dist/generative-ui/standard/url.mjs.map +1 -0
- package/dist/generative-ui/thrown.cjs +69 -0
- package/dist/generative-ui/thrown.cjs.map +1 -0
- package/dist/generative-ui/thrown.d.cts +21 -0
- package/dist/generative-ui/thrown.d.cts.map +1 -0
- package/dist/generative-ui/thrown.d.mts +21 -0
- package/dist/generative-ui/thrown.d.mts.map +1 -0
- package/dist/generative-ui/thrown.mjs +67 -0
- package/dist/generative-ui/thrown.mjs.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +368 -2
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/package.json +2 -2
- package/src/__tests__/schema-render.test.ts +78 -0
- package/src/event-transforms/recorded-events.test.ts +35 -3
- package/src/event-transforms/recorded-events.ts +15 -2
- package/src/generative-ui/__tests__/clone-value-differential.test.ts +480 -0
- package/src/generative-ui/__tests__/clone-value.test.ts +226 -0
- package/src/generative-ui/clone-value.ts +511 -0
- package/src/generative-ui/index.ts +6 -2
- package/src/generative-ui/schema-render.ts +2 -1
- package/src/generative-ui/standard/__tests__/standard-helpers.test.ts +407 -0
- package/src/generative-ui/standard/chart-config.ts +434 -0
- package/src/generative-ui/standard/index.ts +36 -0
- package/src/generative-ui/standard/map-helpers.ts +155 -0
- package/src/generative-ui/standard/style-helpers.ts +170 -0
- package/src/generative-ui/standard/url.ts +51 -0
- package/src/generative-ui/thrown.ts +90 -0
- package/src/index.ts +5 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
// Framework-free chart helpers of the standard Chart component. They build a
|
|
2
|
+
// chart.js config without importing chart.js: the chart.js parts are typed
|
|
3
|
+
// structurally, so chart.js stays an optional peer of each framework package.
|
|
4
|
+
import { CHART_PALETTE, borderColor, textColor } from "./tokens";
|
|
5
|
+
import type { ChartProps, ChartSeries } from "./chart";
|
|
6
|
+
|
|
7
|
+
type ChartData = NonNullable<ChartProps["data"]>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The series of a chart, each with a key, a label and a color. Without
|
|
11
|
+
* series, the chart shows every number field of the first row, except the
|
|
12
|
+
* x key.
|
|
13
|
+
*
|
|
14
|
+
* @param series - The `series` prop of the chart.
|
|
15
|
+
* @param data - The rows of the chart.
|
|
16
|
+
* @param xKey - The field of the x axis (or of the slice names).
|
|
17
|
+
*/
|
|
18
|
+
export function normalizeSeries(
|
|
19
|
+
series: ChartSeries[] | undefined,
|
|
20
|
+
data: ChartData,
|
|
21
|
+
xKey: string | undefined,
|
|
22
|
+
) {
|
|
23
|
+
const first = data[0];
|
|
24
|
+
const isEmpty = series === undefined || series.length === 0;
|
|
25
|
+
const list: ChartSeries[] =
|
|
26
|
+
isEmpty && first !== undefined
|
|
27
|
+
? Object.keys(first).filter(
|
|
28
|
+
(key) => key !== xKey && typeof first[key] === "number",
|
|
29
|
+
)
|
|
30
|
+
: (series ?? []);
|
|
31
|
+
return list.map((item, index) => {
|
|
32
|
+
const entry = typeof item === "string" ? { key: item } : item;
|
|
33
|
+
const color: string =
|
|
34
|
+
textColor("color" in entry ? entry.color : undefined) ??
|
|
35
|
+
CHART_PALETTE[index % CHART_PALETTE.length]!;
|
|
36
|
+
return {
|
|
37
|
+
key: entry.key,
|
|
38
|
+
label: ("label" in entry && entry.label) || entry.key,
|
|
39
|
+
color,
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function numberStyle(format: NonNullable<ChartProps["format"]>) {
|
|
45
|
+
switch (format) {
|
|
46
|
+
case "currency":
|
|
47
|
+
return "currency";
|
|
48
|
+
case "percent":
|
|
49
|
+
return "percent";
|
|
50
|
+
case "number":
|
|
51
|
+
return "decimal";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The value formatter of the axis, the tooltip, the value labels and the
|
|
57
|
+
* data table. `undefined` when no `format` is set. Axis ticks must stay
|
|
58
|
+
* short ("$1.6K", "25%"): en-US, no decimals, and compact from 10,000.
|
|
59
|
+
*
|
|
60
|
+
* @param format - The `format` prop of the chart.
|
|
61
|
+
* @param currency - The ISO currency code for `"currency"`. Defaults to USD.
|
|
62
|
+
*/
|
|
63
|
+
export function makeFormatter(
|
|
64
|
+
format: ChartProps["format"],
|
|
65
|
+
currency: string | undefined,
|
|
66
|
+
) {
|
|
67
|
+
if (!format) return undefined;
|
|
68
|
+
const build = (isCompact: boolean) => {
|
|
69
|
+
try {
|
|
70
|
+
return new Intl.NumberFormat("en-US", {
|
|
71
|
+
style: numberStyle(format),
|
|
72
|
+
currency: format === "currency" ? (currency ?? "USD") : undefined,
|
|
73
|
+
maximumFractionDigits: format === "percent" ? 1 : 0,
|
|
74
|
+
notation: isCompact ? "compact" : undefined,
|
|
75
|
+
});
|
|
76
|
+
} catch {
|
|
77
|
+
// An unknown currency code throws. Plain numbers are a safe fallback.
|
|
78
|
+
return new Intl.NumberFormat("en-US");
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const plain = build(false);
|
|
82
|
+
const compact = build(true);
|
|
83
|
+
return (value: unknown) =>
|
|
84
|
+
typeof value === "number"
|
|
85
|
+
? (Math.abs(value) >= 10_000 ? compact : plain).format(value)
|
|
86
|
+
: String(value ?? "");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A CSS color that a canvas can paint. The color tokens are
|
|
91
|
+
* `var(--name, fallback)` strings: the custom property comes from the
|
|
92
|
+
* computed style of `element`, else the fallback.
|
|
93
|
+
*
|
|
94
|
+
* @param value - A CSS color or a `var(...)` token.
|
|
95
|
+
* @param element - The element whose computed style holds the custom properties.
|
|
96
|
+
*/
|
|
97
|
+
export function resolveCssColor(value: string, element: Element) {
|
|
98
|
+
const match = /^var\((--[\w-]+)(?:,\s*(.*))?\)$/.exec(value.trim());
|
|
99
|
+
if (!match) return value;
|
|
100
|
+
const fallback = match[2]?.trim() ?? value;
|
|
101
|
+
if (typeof getComputedStyle !== "function") return fallback;
|
|
102
|
+
try {
|
|
103
|
+
const resolved = getComputedStyle(element)
|
|
104
|
+
.getPropertyValue(match[1]!)
|
|
105
|
+
.trim();
|
|
106
|
+
return resolved || fallback;
|
|
107
|
+
} catch {
|
|
108
|
+
return fallback;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A `#rrggbb` color with an alpha channel added. Any other color comes back
|
|
114
|
+
* as it is.
|
|
115
|
+
*
|
|
116
|
+
* @example withAlpha("#ff0000", 0.2) // "#ff000033"
|
|
117
|
+
*/
|
|
118
|
+
export function withAlpha(color: string, alpha: number) {
|
|
119
|
+
const match = /^#([0-9a-f]{6})$/i.exec(color);
|
|
120
|
+
if (!match) return color;
|
|
121
|
+
const hex = Math.round(alpha * 255)
|
|
122
|
+
.toString(16)
|
|
123
|
+
.padStart(2, "0");
|
|
124
|
+
return `#${match[1]}${hex}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The `key` of a chart.js point (`{ x, y }`), or the value itself when it is not a point. */
|
|
128
|
+
function pointValue(value: unknown, key: "x" | "y") {
|
|
129
|
+
if (typeof value !== "object" || value === null) return value;
|
|
130
|
+
if (key === "x") return "x" in value ? value.x : value;
|
|
131
|
+
return "y" in value ? value.y : value;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* A chart.js `Chart` constructor, as `chart.js/auto` exports it. Use it to
|
|
136
|
+
* type the module that a chart loader resolves.
|
|
137
|
+
*/
|
|
138
|
+
export type ChartConstructor = new (
|
|
139
|
+
canvas: HTMLCanvasElement,
|
|
140
|
+
config: Record<string, unknown>,
|
|
141
|
+
) => { destroy(): void };
|
|
142
|
+
|
|
143
|
+
/** The part of a chart.js chart that the value labels plugin reads and draws on. */
|
|
144
|
+
interface ChartForLabels {
|
|
145
|
+
ctx: CanvasRenderingContext2D;
|
|
146
|
+
data: { datasets: { data: unknown[] }[] };
|
|
147
|
+
options: { indexAxis?: string };
|
|
148
|
+
getDatasetMeta(index: number): {
|
|
149
|
+
hidden: boolean | null;
|
|
150
|
+
type: string;
|
|
151
|
+
data: {
|
|
152
|
+
x: number;
|
|
153
|
+
y: number;
|
|
154
|
+
getProps?: (keys: string[], final?: boolean) => Record<string, number>;
|
|
155
|
+
}[];
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* A chart.js plugin that draws the value of each bar, point and slice, for
|
|
161
|
+
* `showValueLabels`: chart.js has no value labels of its own. Bar and point
|
|
162
|
+
* labels sit above the mark (next to it in a horizontal layout); slice
|
|
163
|
+
* labels sit outside the arc, with a line unless `showLabelLines` is false.
|
|
164
|
+
*
|
|
165
|
+
* @param props - The chart props.
|
|
166
|
+
* @param format - The value formatter, from `makeFormatter`.
|
|
167
|
+
* @param textFill - The canvas color of the label text.
|
|
168
|
+
* @param lineStroke - The canvas color of the slice label lines.
|
|
169
|
+
*/
|
|
170
|
+
export function valueLabelsPlugin(
|
|
171
|
+
props: ChartProps,
|
|
172
|
+
format: ((value: unknown) => string) | undefined,
|
|
173
|
+
textFill: string | undefined,
|
|
174
|
+
lineStroke: string | undefined,
|
|
175
|
+
) {
|
|
176
|
+
const showLines = props.showLabelLines ?? true;
|
|
177
|
+
return {
|
|
178
|
+
id: "a2uiValueLabels",
|
|
179
|
+
afterDatasetsDraw(chart: ChartForLabels) {
|
|
180
|
+
const ctx = chart.ctx;
|
|
181
|
+
ctx.save();
|
|
182
|
+
ctx.font = "12px system-ui, sans-serif";
|
|
183
|
+
if (textFill) ctx.fillStyle = textFill;
|
|
184
|
+
if (lineStroke) ctx.strokeStyle = lineStroke;
|
|
185
|
+
ctx.lineWidth = 1;
|
|
186
|
+
const horizontal = chart.options.indexAxis === "y";
|
|
187
|
+
chart.data.datasets.forEach((dataset, datasetIndex) => {
|
|
188
|
+
const meta = chart.getDatasetMeta(datasetIndex);
|
|
189
|
+
if (meta.hidden) return;
|
|
190
|
+
meta.data.forEach((element, index) => {
|
|
191
|
+
const value = pointValue(dataset.data[index], "y");
|
|
192
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return;
|
|
193
|
+
const label = format ? format(value) : String(value);
|
|
194
|
+
if (meta.type === "pie" || meta.type === "doughnut") {
|
|
195
|
+
const arc = element.getProps?.(
|
|
196
|
+
["x", "y", "startAngle", "endAngle", "outerRadius"],
|
|
197
|
+
true,
|
|
198
|
+
);
|
|
199
|
+
if (!arc) return;
|
|
200
|
+
const angle = (arc.startAngle! + arc.endAngle!) / 2;
|
|
201
|
+
const cos = Math.cos(angle);
|
|
202
|
+
const sin = Math.sin(angle);
|
|
203
|
+
const edge = arc.outerRadius!;
|
|
204
|
+
const fromX = arc.x! + cos * edge;
|
|
205
|
+
const fromY = arc.y! + sin * edge;
|
|
206
|
+
const toX = arc.x! + cos * (edge + 12);
|
|
207
|
+
const toY = arc.y! + sin * (edge + 12);
|
|
208
|
+
if (showLines) {
|
|
209
|
+
ctx.beginPath();
|
|
210
|
+
ctx.moveTo(fromX, fromY);
|
|
211
|
+
ctx.lineTo(toX, toY);
|
|
212
|
+
ctx.stroke();
|
|
213
|
+
}
|
|
214
|
+
ctx.textAlign = cos >= 0 ? "left" : "right";
|
|
215
|
+
ctx.textBaseline = "middle";
|
|
216
|
+
ctx.fillText(label, toX + (cos >= 0 ? 3 : -3), toY);
|
|
217
|
+
} else if (horizontal) {
|
|
218
|
+
ctx.textAlign = "left";
|
|
219
|
+
ctx.textBaseline = "middle";
|
|
220
|
+
ctx.fillText(label, element.x + 4, element.y);
|
|
221
|
+
} else {
|
|
222
|
+
ctx.textAlign = "center";
|
|
223
|
+
ctx.textBaseline = "bottom";
|
|
224
|
+
ctx.fillText(label, element.x, element.y - 4);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
ctx.restore();
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The chart.js config of the chart props: the marks, colors, axes, legend,
|
|
235
|
+
* tooltip and value labels. Pass it to a chart.js `Chart` constructor.
|
|
236
|
+
*
|
|
237
|
+
* @param props - The chart props.
|
|
238
|
+
* @param element - The chart element. Its computed style resolves the color tokens.
|
|
239
|
+
*/
|
|
240
|
+
export function chartConfig(
|
|
241
|
+
props: ChartProps,
|
|
242
|
+
element: Element,
|
|
243
|
+
): Record<string, unknown> {
|
|
244
|
+
const color = (value: string | undefined) =>
|
|
245
|
+
value === undefined ? undefined : resolveCssColor(value, element);
|
|
246
|
+
const data = props.data ?? [];
|
|
247
|
+
const chartType = props.chartType ?? "bar";
|
|
248
|
+
const series = normalizeSeries(props.series, data, props.xKey);
|
|
249
|
+
const format = makeFormatter(props.format, props.currency);
|
|
250
|
+
const xKey = props.xKey;
|
|
251
|
+
const secondary = color(textColor("secondary"));
|
|
252
|
+
const plugins = props.showValueLabels
|
|
253
|
+
? [valueLabelsPlugin(props, format, color(textColor("default")), secondary)]
|
|
254
|
+
: undefined;
|
|
255
|
+
const legend = {
|
|
256
|
+
display: (props.showLegend ?? true) && series.length > 0,
|
|
257
|
+
labels: {
|
|
258
|
+
usePointStyle: true,
|
|
259
|
+
pointStyle: "circle",
|
|
260
|
+
boxWidth: 8,
|
|
261
|
+
boxHeight: 8,
|
|
262
|
+
color: color(textColor("default")),
|
|
263
|
+
font: { size: 13 },
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
const tooltip = {
|
|
267
|
+
enabled: props.showTooltip ?? true,
|
|
268
|
+
callbacks: format
|
|
269
|
+
? {
|
|
270
|
+
label: (context: {
|
|
271
|
+
dataset: { label?: string };
|
|
272
|
+
parsed: unknown;
|
|
273
|
+
raw: unknown;
|
|
274
|
+
}) => {
|
|
275
|
+
const parsed = context.parsed;
|
|
276
|
+
// A horizontal layout puts the value on the x axis. A scatter
|
|
277
|
+
// point keeps its value in y whatever the layout.
|
|
278
|
+
const valueKey =
|
|
279
|
+
props.layout === "horizontal" && chartType !== "scatter"
|
|
280
|
+
? "x"
|
|
281
|
+
: "y";
|
|
282
|
+
const value = pointValue(parsed, valueKey);
|
|
283
|
+
const label = context.dataset.label;
|
|
284
|
+
return `${label ? `${label}: ` : ""}${format(value)}`;
|
|
285
|
+
},
|
|
286
|
+
}
|
|
287
|
+
: undefined,
|
|
288
|
+
};
|
|
289
|
+
const common = {
|
|
290
|
+
responsive: true,
|
|
291
|
+
maintainAspectRatio: false,
|
|
292
|
+
animation: false,
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
if (chartType === "pie" || chartType === "donut") {
|
|
296
|
+
const first = series[0];
|
|
297
|
+
const labels = data.map((row) => String(row[xKey ?? "name"] ?? ""));
|
|
298
|
+
return {
|
|
299
|
+
type: chartType === "pie" ? "pie" : "doughnut",
|
|
300
|
+
plugins,
|
|
301
|
+
data: {
|
|
302
|
+
labels,
|
|
303
|
+
datasets: first
|
|
304
|
+
? [
|
|
305
|
+
{
|
|
306
|
+
label: first.label,
|
|
307
|
+
data: data.map((row) => Number(row[first.key] ?? 0)),
|
|
308
|
+
backgroundColor: data.map((_, index) =>
|
|
309
|
+
color(CHART_PALETTE[index % CHART_PALETTE.length]!),
|
|
310
|
+
),
|
|
311
|
+
borderWidth: 0,
|
|
312
|
+
borderRadius: props.cornerRadius ?? 0,
|
|
313
|
+
spacing: props.paddingAngle ?? 0,
|
|
314
|
+
},
|
|
315
|
+
]
|
|
316
|
+
: [],
|
|
317
|
+
},
|
|
318
|
+
options: {
|
|
319
|
+
...common,
|
|
320
|
+
cutout: props.innerRadius ?? (chartType === "donut" ? "55%" : 0),
|
|
321
|
+
radius: props.outerRadius ?? "80%",
|
|
322
|
+
rotation: props.startAngle === undefined ? 0 : 90 - props.startAngle,
|
|
323
|
+
circumference:
|
|
324
|
+
props.startAngle !== undefined && props.endAngle !== undefined
|
|
325
|
+
? Math.abs(props.startAngle - props.endAngle)
|
|
326
|
+
: 360,
|
|
327
|
+
plugins: { legend, tooltip },
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const isHorizontal = props.layout === "horizontal";
|
|
333
|
+
const stacked = props.stacked === true;
|
|
334
|
+
// Scatter uses numeric distances when all x values are numbers. Preserve
|
|
335
|
+
// category spacing for string/mixed data and for the other chart types.
|
|
336
|
+
const isNumericScatter =
|
|
337
|
+
chartType === "scatter" &&
|
|
338
|
+
xKey !== undefined &&
|
|
339
|
+
data.length > 0 &&
|
|
340
|
+
data.every(
|
|
341
|
+
(row) => typeof row[xKey] === "number" && Number.isFinite(row[xKey]),
|
|
342
|
+
);
|
|
343
|
+
const labels = data.map((row) => String((xKey ? row[xKey] : "") ?? ""));
|
|
344
|
+
const pointRadius = props.showDots ? 3 : 0;
|
|
345
|
+
const datasets = series.map((item) => {
|
|
346
|
+
const paint = color(item.color);
|
|
347
|
+
const values = data.map((row) => row[item.key]);
|
|
348
|
+
switch (chartType) {
|
|
349
|
+
case "line":
|
|
350
|
+
return {
|
|
351
|
+
label: item.label,
|
|
352
|
+
data: values,
|
|
353
|
+
borderColor: paint,
|
|
354
|
+
backgroundColor: paint,
|
|
355
|
+
borderWidth: 2,
|
|
356
|
+
tension: 0.4,
|
|
357
|
+
pointRadius,
|
|
358
|
+
fill: false,
|
|
359
|
+
};
|
|
360
|
+
case "area":
|
|
361
|
+
return {
|
|
362
|
+
label: item.label,
|
|
363
|
+
data: values,
|
|
364
|
+
borderColor: paint,
|
|
365
|
+
backgroundColor: withAlpha(paint ?? "", 0.2),
|
|
366
|
+
borderWidth: 2,
|
|
367
|
+
tension: 0.4,
|
|
368
|
+
pointRadius,
|
|
369
|
+
fill: stacked ? "stack" : "origin",
|
|
370
|
+
};
|
|
371
|
+
case "scatter":
|
|
372
|
+
return {
|
|
373
|
+
label: item.label,
|
|
374
|
+
data: data.map((row, index) => ({
|
|
375
|
+
x: isNumericScatter ? row[xKey!] : index,
|
|
376
|
+
y: row[item.key],
|
|
377
|
+
})),
|
|
378
|
+
backgroundColor: paint,
|
|
379
|
+
pointRadius: 4,
|
|
380
|
+
};
|
|
381
|
+
default:
|
|
382
|
+
return {
|
|
383
|
+
label: item.label,
|
|
384
|
+
data: values,
|
|
385
|
+
backgroundColor: paint,
|
|
386
|
+
borderRadius: stacked ? 0 : (props.barRadius ?? 4),
|
|
387
|
+
borderSkipped: stacked ? false : undefined,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
const valueAxis = {
|
|
392
|
+
stacked,
|
|
393
|
+
min: props.yAxis?.min,
|
|
394
|
+
max: props.yAxis?.max,
|
|
395
|
+
grid: {
|
|
396
|
+
display: props.showGrid ?? true,
|
|
397
|
+
color: color(borderColor("subtle")),
|
|
398
|
+
},
|
|
399
|
+
border: { display: false },
|
|
400
|
+
ticks: {
|
|
401
|
+
color: secondary,
|
|
402
|
+
font: { size: 12 },
|
|
403
|
+
callback: format ? (value: unknown) => format(value) : undefined,
|
|
404
|
+
},
|
|
405
|
+
title: props.yAxis?.label
|
|
406
|
+
? {
|
|
407
|
+
display: true,
|
|
408
|
+
text: props.yAxis.label,
|
|
409
|
+
color: secondary,
|
|
410
|
+
font: { size: 12 },
|
|
411
|
+
}
|
|
412
|
+
: undefined,
|
|
413
|
+
};
|
|
414
|
+
const categoryAxis = {
|
|
415
|
+
stacked,
|
|
416
|
+
type: isNumericScatter ? "linear" : "category",
|
|
417
|
+
grid: { display: false },
|
|
418
|
+
border: { color: color(borderColor("default")) },
|
|
419
|
+
ticks: { color: secondary, font: { size: 12 } },
|
|
420
|
+
};
|
|
421
|
+
return {
|
|
422
|
+
type: chartType === "area" ? "line" : chartType,
|
|
423
|
+
plugins,
|
|
424
|
+
data: { labels: isNumericScatter ? undefined : labels, datasets },
|
|
425
|
+
options: {
|
|
426
|
+
...common,
|
|
427
|
+
indexAxis: isHorizontal ? "y" : "x",
|
|
428
|
+
plugins: { legend, tooltip },
|
|
429
|
+
scales: isHorizontal
|
|
430
|
+
? { x: valueAxis, y: categoryAxis }
|
|
431
|
+
: { x: categoryAxis, y: valueAxis },
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
}
|
|
@@ -17,3 +17,39 @@ export * from "./map";
|
|
|
17
17
|
export * from "./media";
|
|
18
18
|
export * from "./composites";
|
|
19
19
|
export * from "./declarations";
|
|
20
|
+
// Framework-free helpers that the standard components of each framework use.
|
|
21
|
+
export {
|
|
22
|
+
ALIGN_CSS,
|
|
23
|
+
FOCUS_RING,
|
|
24
|
+
JUSTIFY_CSS,
|
|
25
|
+
VISUALLY_HIDDEN,
|
|
26
|
+
defined,
|
|
27
|
+
jsonEqual,
|
|
28
|
+
layoutStyle,
|
|
29
|
+
lineClamp,
|
|
30
|
+
px,
|
|
31
|
+
radiusStyle,
|
|
32
|
+
} from "./style-helpers";
|
|
33
|
+
export type { StyleMap } from "./style-helpers";
|
|
34
|
+
export { faviconSrc, hostOf, openUrl, safeHref } from "./url";
|
|
35
|
+
export {
|
|
36
|
+
chartConfig,
|
|
37
|
+
makeFormatter,
|
|
38
|
+
normalizeSeries,
|
|
39
|
+
resolveCssColor,
|
|
40
|
+
valueLabelsPlugin,
|
|
41
|
+
withAlpha,
|
|
42
|
+
} from "./chart-config";
|
|
43
|
+
export type { ChartConstructor } from "./chart-config";
|
|
44
|
+
export {
|
|
45
|
+
DEFAULT_DARK_MAP_STYLE,
|
|
46
|
+
DEFAULT_MAP_STYLE,
|
|
47
|
+
MAP_CSS,
|
|
48
|
+
MAP_STYLE_ID,
|
|
49
|
+
ROUTE_SOURCE,
|
|
50
|
+
guardNumericFilter,
|
|
51
|
+
guardStyleFilters,
|
|
52
|
+
isMapLibre,
|
|
53
|
+
isValidMarker,
|
|
54
|
+
} from "./map-helpers";
|
|
55
|
+
export type { MapLibre, MapLibreMap, MapLibreMarker } from "./map-helpers";
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// Framework-free map helpers of the standard Map component. The maplibre-gl
|
|
2
|
+
// parts are typed structurally, so maplibre-gl stays an optional peer of
|
|
3
|
+
// each framework package.
|
|
4
|
+
import type { MapMarker } from "./map";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The default basemap: the Positron style of OpenFreeMap. It is a keyless
|
|
8
|
+
* OpenStreetMap vector style with streets and labels at city zoom. A host
|
|
9
|
+
* sets another style with `theme: { mapStyle: "<style url>" }`, and a
|
|
10
|
+
* dark one with `theme: { dark: { mapStyle: "<style url>" } }`.
|
|
11
|
+
*/
|
|
12
|
+
export const DEFAULT_MAP_STYLE =
|
|
13
|
+
"https://tiles.openfreemap.org/styles/positron";
|
|
14
|
+
|
|
15
|
+
/** The default basemap in dark mode: the Dark style of OpenFreeMap. */
|
|
16
|
+
export const DEFAULT_DARK_MAP_STYLE =
|
|
17
|
+
"https://tiles.openfreemap.org/styles/dark";
|
|
18
|
+
|
|
19
|
+
const ORDERING_OPS = new Set(["<", "<=", ">", ">="]);
|
|
20
|
+
|
|
21
|
+
// guardNumericFilter keeps its return type: the function calls itself, so
|
|
22
|
+
// TypeScript cannot infer it.
|
|
23
|
+
/**
|
|
24
|
+
* A filter expression with every numeric comparison of a feature property
|
|
25
|
+
* guarded by `["has", property]`.
|
|
26
|
+
*
|
|
27
|
+
* MapLibre adds a number assertion to `["get", key]` in a comparison with a
|
|
28
|
+
* number. The assertion logs a warning for each feature without the
|
|
29
|
+
* property. OpenFreeMap styles compare `ref_length` on roads that mostly do
|
|
30
|
+
* not have it. The comparison is false for a missing property in both
|
|
31
|
+
* cases, so the guard changes no result.
|
|
32
|
+
*
|
|
33
|
+
* @param filter - A MapLibre filter expression, at any depth.
|
|
34
|
+
*/
|
|
35
|
+
export function guardNumericFilter(filter: unknown): unknown {
|
|
36
|
+
if (!Array.isArray(filter)) return filter;
|
|
37
|
+
const [op, left, right] = filter;
|
|
38
|
+
const isPropertyComparison =
|
|
39
|
+
typeof op === "string" &&
|
|
40
|
+
ORDERING_OPS.has(op) &&
|
|
41
|
+
Array.isArray(left) &&
|
|
42
|
+
left[0] === "get" &&
|
|
43
|
+
typeof left[1] === "string" &&
|
|
44
|
+
typeof right === "number";
|
|
45
|
+
if (isPropertyComparison) return ["all", ["has", left[1]], filter];
|
|
46
|
+
return filter.map((part) => guardNumericFilter(part));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The style with `guardNumericFilter` applied to the filter of each layer.
|
|
51
|
+
* Pass it as the `transformStyle` of MapLibre's `setStyle`.
|
|
52
|
+
*
|
|
53
|
+
* @param style - A MapLibre style object.
|
|
54
|
+
*/
|
|
55
|
+
export function guardStyleFilters<Style extends { layers?: readonly object[] }>(
|
|
56
|
+
style: Style,
|
|
57
|
+
) {
|
|
58
|
+
const layers = style.layers;
|
|
59
|
+
if (layers === undefined) return style;
|
|
60
|
+
const guarded = layers.map((layer) =>
|
|
61
|
+
"filter" in layer
|
|
62
|
+
? { ...layer, filter: guardNumericFilter(layer.filter) }
|
|
63
|
+
: layer,
|
|
64
|
+
);
|
|
65
|
+
// The guard only wraps a comparison in ["all", ["has", key], comparison],
|
|
66
|
+
// which is a valid filter, so each layer keeps its type.
|
|
67
|
+
return { ...style, layers: guarded } as Style;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The id of the map stylesheet (`MAP_CSS`) in the document. */
|
|
71
|
+
export const MAP_STYLE_ID = "a2ui-standard-map";
|
|
72
|
+
|
|
73
|
+
/** The source and layer id of the route line between the markers. */
|
|
74
|
+
export const ROUTE_SOURCE = "a2ui-route";
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The few maplibre-gl rules that markers and popups need, so a host does not
|
|
78
|
+
* have to import maplibre-gl.css. The list of places is hidden while the map
|
|
79
|
+
* shows. It shows when a keyboard user moves focus into it.
|
|
80
|
+
*/
|
|
81
|
+
export const MAP_CSS = [
|
|
82
|
+
".maplibregl-map{position:relative;overflow:hidden}",
|
|
83
|
+
".maplibregl-canvas{position:absolute;left:0;top:0}",
|
|
84
|
+
".maplibregl-canvas-container{width:100%;height:100%}",
|
|
85
|
+
".maplibregl-marker{position:absolute;top:0;left:0;will-change:transform;cursor:pointer}",
|
|
86
|
+
".maplibregl-popup{position:absolute;top:0;left:0;display:flex;will-change:transform;pointer-events:none}",
|
|
87
|
+
".maplibregl-popup-content{position:relative;background:var(--a2ui-color-surface-elevated, #fff);color:var(--a2ui-color-text-default, #0d0d0d);border-radius:6px;padding:6px 10px;font:12px/1.4 system-ui,sans-serif;box-shadow:var(--a2ui-shadow-sm, 0 1px 4px rgba(0,0,0,.2));pointer-events:auto}",
|
|
88
|
+
".maplibregl-popup-tip{display:none}",
|
|
89
|
+
".maplibregl-popup-close-button{display:none}",
|
|
90
|
+
".maplibregl-ctrl-attrib{font:10px system-ui,sans-serif;position:absolute;right:0;bottom:0;background:color-mix(in srgb, var(--a2ui-color-surface, #fff) 70%, transparent);color:var(--a2ui-color-text-secondary, #5d5d5d);padding:0 4px}",
|
|
91
|
+
".maplibregl-ctrl-attrib a{color:inherit}",
|
|
92
|
+
// The cooperative-gesture hint is hidden until MapLibre adds `maplibregl-show`.
|
|
93
|
+
".maplibregl-cooperative-gesture-screen{align-items:center;background:rgba(0,0,0,.4);color:#fff;display:flex;font:14px/1.3 system-ui,sans-serif;inset:0;justify-content:center;opacity:0;padding:1rem;pointer-events:none;position:absolute;text-align:center;transition:opacity 1s ease 1s;z-index:2}",
|
|
94
|
+
".maplibregl-cooperative-gesture-screen.maplibregl-show{opacity:1;transition:opacity .05s}",
|
|
95
|
+
".maplibregl-cooperative-gesture-screen .maplibregl-mobile-message{display:none}",
|
|
96
|
+
"@media (hover:none){.maplibregl-cooperative-gesture-screen .maplibregl-desktop-message{display:none}.maplibregl-cooperative-gesture-screen .maplibregl-mobile-message{display:block}}",
|
|
97
|
+
".a2ui-std-map-places[data-collapsed]:not(:focus-within){position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0}",
|
|
98
|
+
].join("\n");
|
|
99
|
+
|
|
100
|
+
/** The part of a maplibre-gl `Map` that the standard Map component uses. */
|
|
101
|
+
export interface MapLibreMap {
|
|
102
|
+
setStyle(
|
|
103
|
+
style: string,
|
|
104
|
+
options: {
|
|
105
|
+
transformStyle: (
|
|
106
|
+
previous: unknown,
|
|
107
|
+
next: { layers?: readonly object[] },
|
|
108
|
+
) => unknown;
|
|
109
|
+
},
|
|
110
|
+
): void;
|
|
111
|
+
on(
|
|
112
|
+
event: string,
|
|
113
|
+
handler: (event: { originalEvent?: unknown }) => void,
|
|
114
|
+
): void;
|
|
115
|
+
fitBounds(bounds: unknown, options: Record<string, unknown>): void;
|
|
116
|
+
getSource(id: string): unknown;
|
|
117
|
+
addSource(id: string, source: Record<string, unknown>): void;
|
|
118
|
+
addLayer(layer: Record<string, unknown>): void;
|
|
119
|
+
remove(): void;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The part of a maplibre-gl `Marker` that the standard Map component uses. */
|
|
123
|
+
export interface MapLibreMarker {
|
|
124
|
+
setLngLat(position: [number, number]): MapLibreMarker;
|
|
125
|
+
setPopup(popup: unknown): MapLibreMarker;
|
|
126
|
+
addTo(map: MapLibreMap): MapLibreMarker;
|
|
127
|
+
getElement(): HTMLElement;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The part of the maplibre-gl module that the standard Map component uses. */
|
|
131
|
+
export interface MapLibre {
|
|
132
|
+
Map: new (options: Record<string, unknown>) => MapLibreMap;
|
|
133
|
+
Marker: new () => MapLibreMarker;
|
|
134
|
+
Popup: new (options: Record<string, unknown>) => {
|
|
135
|
+
setText(text: string): unknown;
|
|
136
|
+
};
|
|
137
|
+
LngLatBounds: new () => { extend(position: [number, number]): void };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* True when `value` looks like the maplibre-gl module: it has a `Map`
|
|
142
|
+
* constructor. Use it on the module that a map loader resolves.
|
|
143
|
+
*/
|
|
144
|
+
export function isMapLibre(value: unknown): value is MapLibre {
|
|
145
|
+
return (
|
|
146
|
+
typeof value === "object" &&
|
|
147
|
+
value !== null &&
|
|
148
|
+
"Map" in value &&
|
|
149
|
+
typeof value.Map === "function"
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** True when the marker has finite coordinates, so the map can place it. */
|
|
154
|
+
export const isValidMarker = (marker: MapMarker) =>
|
|
155
|
+
Number.isFinite(marker.lat) && Number.isFinite(marker.lng);
|