@k2b/editor 0.12.2 → 0.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k2b/editor",
3
- "version": "0.12.2",
3
+ "version": "0.13.0",
4
4
  "description": "Configurable Markdown editor for SolidJS: editor, toolbar, bubble menu, and slash menu on one shared state.",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -20,6 +20,7 @@
20
20
  "./collab": "./src/collab/index.ts",
21
21
  "./formula": "./src/formula.ts",
22
22
  "./charts": "./src/charts.ts",
23
+ "./chart-schema": "./src/chart-schema.ts",
23
24
  "./markdown/pretty-table": "./src/markdown/pretty-table.ts",
24
25
  "./markdown/shared-tables": "./src/markdown/shared-tables.ts",
25
26
  "./markdown/pretty-table-messages": "./src/markdown/pretty-table-messages.ts",
@@ -69,8 +70,8 @@
69
70
  "@codemirror/search": "6.7.2",
70
71
  "@codemirror/state": "6.7.6",
71
72
  "@codemirror/view": "6.43.15",
72
- "@k2b/stdlib": "0.34.1",
73
- "@k2b/ui": "0.62.0",
73
+ "@k2b/stdlib": "0.36.0",
74
+ "@k2b/ui": "0.63.0",
74
75
  "@lezer/common": "1.5.5",
75
76
  "@lezer/highlight": "1.2.5",
76
77
  "@lezer/markdown": "1.8.1",
@@ -1,10 +1,13 @@
1
- import type { ChartInput, ChartLimits } from "@k2b/ui/charts";
1
+ import type { ChartFormat, ChartInput, ChartLimits } from "@k2b/ui/charts";
2
2
  import {
3
+ autoBin,
3
4
  BalanceFlowError,
4
5
  CALENDAR_MAX_DAYS,
5
6
  calendarDay,
7
+ computeDomain,
6
8
  lineDetailsEnabled,
7
9
  nestedDotOptions,
10
+ niceStep,
8
11
  sankeyGraph,
9
12
  validateBalanceFlow,
10
13
  } from "@k2b/ui/charts/core";
@@ -338,6 +341,8 @@ export const chartKinds = (limits?: ChartLimits) => {
338
341
  legend: z.boolean().optional(),
339
342
  area: z.boolean().optional(),
340
343
  smooth: z.boolean().optional(),
344
+ /** A hollow point on every value; off by default. */
345
+ points: z.boolean().optional(),
341
346
  step: z.enum(["after", "before", "middle"]).optional(),
342
347
  zero: z.boolean().optional(),
343
348
  legendValues: z.boolean().optional(),
@@ -477,7 +482,8 @@ export const chartKinds = (limits?: ChartLimits) => {
477
482
  z.object({
478
483
  kind: z.literal("heatmap"),
479
484
  ...ChartKindCommon,
480
- layout: z.undefined().optional(),
485
+ // An empty enum accepts no layout, exports as never, and lets optional() enumerate the absent discriminator.
486
+ layout: z.enum([]).optional(),
481
487
  data: list(ChartCell, 1, limits?.maxMarks),
482
488
  format: numberFormat.optional(),
483
489
  showValues: z.boolean().optional(),
@@ -663,6 +669,93 @@ export const chartDateTime = (value: string): number | undefined => {
663
669
  return date.toISOString().startsWith(`${year}-${month}-${day}T${hour}:${minute}:${second}`) ? date.getTime() : undefined;
664
670
  };
665
671
 
672
+ export type ChartAxisInput = { label?: string; domain?: number[]; ticks?: number; scale?: "linear" | "log"; format?: ChartFormat };
673
+
674
+ const dateTime = (value: number | string) => (typeof value === "number" ? value : (chartDateTime(value) ?? Number.NaN));
675
+ /** The renderer's default tick count. */
676
+ const TICKS = 5;
677
+
678
+ export const chartReferenceValues = (input: ChartInput, name: "x" | "y") => {
679
+ const values =
680
+ "references" in input ? (input.references ?? []).filter((ref) => (ref.axis ?? "y") === name).map((ref) => dateTime(ref.value)) : [];
681
+ if (input.kind === "line") {
682
+ if (name === "x") {
683
+ if (input.forecast) values.push(dateTime(input.forecast.from));
684
+ if (input.marker) values.push(dateTime(input.marker.at));
685
+ } else {
686
+ if (input.threshold) values.push(input.threshold.value);
687
+ if (input.zero) values.push(0);
688
+ for (const bounds of input.forecast?.band ?? []) values.push(...bounds);
689
+ }
690
+ }
691
+ return values;
692
+ };
693
+
694
+ /** The value axes of a chart with what they show: the data checks and the label room read them alike. */
695
+ export const chartValueAxes = (input: ChartInput): { values: number[]; axis?: ChartAxisInput; zero?: boolean; name: "x" | "y" }[] => {
696
+ if (input.kind === "bar")
697
+ return [
698
+ {
699
+ values: [
700
+ ...input.data.map((item) => item.value),
701
+ ...(input.series?.flatMap((s) => s.data.filter((v): v is number => v !== null)) ?? []),
702
+ ...(input.compare?.data.filter((v): v is number => v !== null) ?? []),
703
+ ],
704
+ axis: input.yAxis,
705
+ zero: true,
706
+ name: "y",
707
+ },
708
+ ];
709
+ if (input.kind === "boxplot") return [{ values: input.data.flatMap((item) => item.values), axis: input.yAxis, name: "y" }];
710
+ if (input.kind === "histogram") {
711
+ if (!input.references?.length) return [{ values: input.data, name: "x" }];
712
+ const { edges, counts } = autoBin(input.data, input.bins);
713
+ return [
714
+ { values: edges, name: "x" },
715
+ { values: counts, zero: true, name: "y" },
716
+ ];
717
+ }
718
+ if (input.kind !== "line" && input.kind !== "scatter") return [];
719
+ const points = input.series.flatMap((series) => series.data);
720
+ return [
721
+ { values: points.map((point) => dateTime(point.x)), axis: input.xAxis, name: "x" },
722
+ { values: points.map((point) => point.y), axis: input.yAxis, name: "y" },
723
+ ];
724
+ };
725
+
726
+ /**
727
+ * Whether the renderer can step from the lowest to the highest tick. Values that differ by less than the float
728
+ * precision of their size, such as 1e17 and 1e17 + 16, would make it count forever.
729
+ */
730
+ const steppable = (values: number[], ticks = TICKS) => {
731
+ const [min, max] = computeDomain(values);
732
+ const step = niceStep(max - min, ticks);
733
+ return [Math.floor(min / step) * step, Math.ceil(max / step) * step].every((value) => value + step !== value && value - step !== value);
734
+ };
735
+
736
+ /** Rejects only precision ranges that would make the renderer count ticks forever. */
737
+ export function chartSteppingIssue(input: ChartInput): string | undefined {
738
+ // Check raw observations before autoBin computes histogram edges.
739
+ if (input.kind === "histogram" && !steppable(input.data))
740
+ return "The values differ too little for their size to label an axis; subtract a common offset first.";
741
+ for (const { values: data, axis, zero, name } of chartValueAxes(input)) {
742
+ const values = [...data, ...chartReferenceValues(input, name)];
743
+ if (axis?.scale !== "log" && !axis?.domain && !steppable(zero ? [...values, 0] : values, axis?.ticks))
744
+ return "The values differ too little for their size to label an axis; subtract a common offset first.";
745
+ }
746
+ return undefined;
747
+ }
748
+
749
+ /** Application chart schema: permissive observations, with the renderer's tick-safety guard. */
750
+ export const chartSchema = (limits?: ChartLimits) =>
751
+ chartKinds(limits).superRefine(
752
+ (chart, context) => {
753
+ const issue = chartSteppingIssue(chart);
754
+ if (issue) context.addIssue({ code: "custom", message: issue });
755
+ },
756
+ { when: (payload) => payload.issues.length === 0 },
757
+ );
758
+
666
759
  // Both directions are checked: a validator change must update the ui-owned description too.
667
760
  type Assert<T extends true> = T;
668
761
  type Same<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
package/src/charts.ts CHANGED
@@ -2,7 +2,6 @@
2
2
  import { i18n } from "@k2b/stdlib";
3
3
  import {
4
4
  type AreaOptions,
5
- autoBin,
6
5
  type BalanceFlowOptions,
7
6
  type BarChartOptions,
8
7
  type BubbleOptions,
@@ -58,7 +57,15 @@ import { z } from "zod";
58
57
 
59
58
  export { balanceFlowRenderOptions, chartNumberFormatters } from "@k2b/ui/charts/core";
60
59
 
61
- import { type ChartFormat, type ChartInput, type ChartNumberFormat, chartDateTime, chartKinds } from "./chart-schema";
60
+ import {
61
+ type ChartAxisInput,
62
+ type ChartFormat,
63
+ type ChartInput,
64
+ type ChartNumberFormat,
65
+ chartDateTime,
66
+ chartReferenceValues,
67
+ chartValueAxes,
68
+ } from "./chart-schema";
62
69
 
63
70
  export * from "./chart-schema";
64
71
 
@@ -209,8 +216,6 @@ const dateTime = (value: number | string) => (typeof value === "number" ? value
209
216
 
210
217
  export const chartSeries = (input: ChartInput) => (input.kind === "line" || input.kind === "scatter" ? input.series : []);
211
218
 
212
- type ChartAxisInput = { label?: string; domain?: number[]; ticks?: number; scale?: "linear" | "log"; format?: ChartFormat };
213
-
214
219
  export const chartBounds = (domain: number[] | undefined): [number, number] | undefined => {
215
220
  const [min, max] = domain ?? [];
216
221
  return min === undefined || max === undefined ? undefined : [min, max];
@@ -222,54 +227,6 @@ const axis = ({ domain, format: _format, ...value }: ChartAxisInput = {}, format
222
227
  format,
223
228
  });
224
229
 
225
- export const chartReferenceValues = (input: ChartInput, name: "x" | "y") => {
226
- const values =
227
- "references" in input ? (input.references ?? []).filter((ref) => (ref.axis ?? "y") === name).map((ref) => dateTime(ref.value)) : [];
228
- if (input.kind === "line") {
229
- if (name === "x") {
230
- if (input.forecast) values.push(dateTime(input.forecast.from));
231
- if (input.marker) values.push(dateTime(input.marker.at));
232
- } else {
233
- if (input.threshold) values.push(input.threshold.value);
234
- if (input.zero) values.push(0);
235
- for (const bounds of input.forecast?.band ?? []) values.push(...bounds);
236
- }
237
- }
238
- return values;
239
- };
240
-
241
- /** The value axes of a chart with what they show: the data checks and the label room read them alike. */
242
- export const chartValueAxes = (input: ChartInput): { values: number[]; axis?: ChartAxisInput; zero?: boolean; name: "x" | "y" }[] => {
243
- if (input.kind === "bar")
244
- return [
245
- {
246
- values: [
247
- ...input.data.map((item) => item.value),
248
- ...(input.series?.flatMap((s) => s.data.filter((v): v is number => v !== null)) ?? []),
249
- ...(input.compare?.data.filter((v): v is number => v !== null) ?? []),
250
- ],
251
- axis: input.yAxis,
252
- zero: true,
253
- name: "y",
254
- },
255
- ];
256
- if (input.kind === "boxplot") return [{ values: input.data.flatMap((item) => item.values), axis: input.yAxis, name: "y" }];
257
- if (input.kind === "histogram") {
258
- if (!input.references?.length) return [{ values: input.data, name: "x" }];
259
- const { edges, counts } = autoBin(input.data, input.bins);
260
- return [
261
- { values: edges, name: "x" },
262
- { values: counts, zero: true, name: "y" },
263
- ];
264
- }
265
- if (input.kind !== "line" && input.kind !== "scatter") return [];
266
- const points = input.series.flatMap((series) => series.data);
267
- return [
268
- { values: points.map((point) => dateTime(point.x)), axis: input.xAxis, name: "x" },
269
- { values: points.map((point) => point.y), axis: input.yAxis, name: "y" },
270
- ];
271
- };
272
-
273
230
  /** Only references add a fixed domain; charts without them keep the renderer's existing defaults. */
274
231
  const referencedAxis = (input: ChartInput, name: "x" | "y"): ChartAxisInput | undefined => {
275
232
  const entry = chartValueAxes(input).find((entry) => entry.name === name);
@@ -287,16 +244,6 @@ const referencedAxis = (input: ChartInput, name: "x" | "y"): ChartAxisInput | un
287
244
  const renderReferences = (input: ChartInput) =>
288
245
  "references" in input ? input.references?.map((ref) => ({ ...ref, value: dateTime(ref.value) })) : undefined;
289
246
 
290
- /**
291
- * Whether the renderer can step from the lowest to the highest tick. Values that differ by less than the float
292
- * precision of their size, such as 1e17 and 1e17 + 16, would make it count forever.
293
- */
294
- const steppable = (values: number[], ticks = TICKS) => {
295
- const [min, max] = computeDomain(values);
296
- const step = niceStep(max - min, ticks);
297
- return [Math.floor(min / step) * step, Math.ceil(max / step) * step].every((value) => value + step !== value && value - step !== value);
298
- };
299
-
300
247
  /**
301
248
  * The values whose labels are the widest on a value axis: its ends and the ticks next to them. The renderer puts ticks
302
249
  * on multiples of a nice step, or on powers of ten on a log axis, and adds the bounds of a fixed domain.
@@ -658,6 +605,7 @@ export function chartBaseRenderOptions(
658
605
  ...common,
659
606
  area: input.area,
660
607
  smooth: input.smooth,
608
+ points: input.points,
661
609
  step: input.step,
662
610
  zero: input.zero,
663
611
  legendValues: input.legendValues,
@@ -1251,32 +1199,9 @@ export const chartMarks = (input: ChartInput): number => {
1251
1199
  }
1252
1200
  };
1253
1201
 
1254
- /** Rejects only precision ranges that would make the renderer count ticks forever. */
1255
- export function chartSteppingIssue(input: ChartInput): string | undefined {
1256
- // Check raw observations before autoBin computes histogram edges.
1257
- if (input.kind === "histogram" && !steppable(input.data))
1258
- return "The values differ too little for their size to label an axis; subtract a common offset first.";
1259
- for (const { values: data, axis, zero, name } of chartValueAxes(input)) {
1260
- const values = [...data, ...chartReferenceValues(input, name)];
1261
- if (axis?.scale !== "log" && !axis?.domain && !steppable(zero ? [...values, 0] : values, axis?.ticks))
1262
- return "The values differ too little for their size to label an axis; subtract a common offset first.";
1263
- }
1264
- return undefined;
1265
- }
1266
-
1267
1202
  /** Both dimensions must fit before suggesting a heatmap orientation change. */
1268
1203
  export const chartHeatmapLabelFit = (columns: string[], rows: string[]) => {
1269
1204
  const left = Math.max(40, Math.ceil((Math.max(0, ...rows.map((label) => label.length)) * CHAR + 8) * NARROW));
1270
1205
  const characters = Math.floor((WIDTH - left - 16) / NARROW / columns.length / CHAR) - 1;
1271
1206
  return { characters, columns: columns.every((label) => label.length <= characters), rows: rows.length <= HEATMAP_MAX_ROWS };
1272
1207
  };
1273
-
1274
- /** Application chart schema: permissive observations, with the renderer's tick-safety guard. */
1275
- export const chartSchema = () =>
1276
- chartKinds().superRefine(
1277
- (chart, context) => {
1278
- const issue = chartSteppingIssue(chart);
1279
- if (issue) context.addIssue({ code: "custom", message: issue });
1280
- },
1281
- { when: (payload) => payload.issues.length === 0 },
1282
- );