@k2b/editor 0.10.0 → 0.11.1
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 +3 -3
- package/src/BubbleMenu.tsx +11 -4
- package/src/chart-locale.ts +1 -1
- package/src/chart-schema.ts +39 -1
- package/src/charts.ts +100 -72
- package/src/collab/remote-cursors.ts +10 -2
- package/src/markdown/_lib/changed-blocks.ts +66 -0
- package/src/markdown/_lib/cursor-zone-field.ts +61 -5
- package/src/markdown/_lib/list-structure.ts +6 -1
- package/src/markdown/_lib/math-syntax.ts +5 -2
- package/src/markdown/block-previews.tsx +15 -8
- package/src/markdown/data-blocks.ts +13 -5
- package/src/markdown/images.ts +2 -1
- package/src/markdown/info-blocks.ts +4 -2
- package/src/markdown/katex.ts +11 -13
- package/src/markdown/lib/chart-blocks.ts +109 -7
- package/src/markdown/lib/query-blocks.ts +1 -0
- package/src/markdown/ligatures.ts +54 -45
- package/src/markdown/links.ts +2 -2
- package/src/markdown/lists.ts +53 -8
- package/src/markdown/markup.ts +110 -340
- package/src/markdown/mermaid.ts +4 -2
- package/src/markdown/named-blocks.ts +4 -2
- package/src/markdown/notebook-charts.tsx +3 -1
- package/src/markdown/tables.ts +15 -7
- package/src/slash-menu.ts +8 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k2b/editor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.1",
|
|
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": [
|
|
@@ -69,8 +69,8 @@
|
|
|
69
69
|
"@codemirror/search": "6.7.2",
|
|
70
70
|
"@codemirror/state": "6.7.1",
|
|
71
71
|
"@codemirror/view": "6.43.7",
|
|
72
|
-
"@k2b/stdlib": "0.
|
|
73
|
-
"@k2b/ui": "0.
|
|
72
|
+
"@k2b/stdlib": "0.32.0",
|
|
73
|
+
"@k2b/ui": "0.55.0",
|
|
74
74
|
"@lezer/common": "1.5.3",
|
|
75
75
|
"@lezer/highlight": "1.2.4",
|
|
76
76
|
"@lezer/markdown": "1.7.2",
|
package/src/BubbleMenu.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Prec } from "@codemirror/state";
|
|
1
|
+
import { Prec, type SelectionRange } from "@codemirror/state";
|
|
2
2
|
import { EditorView, keymap } from "@codemirror/view";
|
|
3
3
|
import { SelectionToolbar, Toolbar, useLocale } from "@k2b/ui";
|
|
4
4
|
import { createSignal, For, type JSX, Show } from "solid-js";
|
|
@@ -40,12 +40,12 @@ export function BubbleMenu(props: BubbleMenuProps) {
|
|
|
40
40
|
const locale = useLocale();
|
|
41
41
|
const t = () => editorMessages.resolve([locale()]).t;
|
|
42
42
|
const [dragging, setDragging] = createSignal(false);
|
|
43
|
-
const [dismissed, setDismissed] = createSignal<
|
|
43
|
+
const [dismissed, setDismissed] = createSignal<SelectionRange>();
|
|
44
44
|
const [geometry, setGeometry] = createSignal(0);
|
|
45
45
|
// Focus that moves from the text into the menu, such as Tab to a button or a host input, keeps it open.
|
|
46
46
|
const [menuFocused, setMenuFocused] = createSignal(false);
|
|
47
47
|
const dismiss = () => {
|
|
48
|
-
setDismissed(props.editor.state()?.selection);
|
|
48
|
+
setDismissed(props.editor.state()?.selection.main);
|
|
49
49
|
props.editor.view()?.focus();
|
|
50
50
|
};
|
|
51
51
|
|
|
@@ -65,6 +65,13 @@ export function BubbleMenu(props: BubbleMenuProps) {
|
|
|
65
65
|
}),
|
|
66
66
|
EditorView.updateListener.of((update) => {
|
|
67
67
|
if (update.geometryChanged || update.viewportChanged) setGeometry((value) => value + 1);
|
|
68
|
+
// An edit elsewhere, such as a collaborator typing, moves the dismissed selection along: it stays dismissed. A
|
|
69
|
+
// selection of the person's own ends the dismissal, also when they later select the same text again.
|
|
70
|
+
if (update.docChanged || update.selectionSet)
|
|
71
|
+
setDismissed((range) => {
|
|
72
|
+
const mapped = update.docChanged ? range?.map(update.changes) : range;
|
|
73
|
+
return mapped?.eq(update.state.selection.main) ? mapped : undefined;
|
|
74
|
+
});
|
|
68
75
|
}),
|
|
69
76
|
// Escape in the text closes an open menu first; with no menu, the key keeps its other meanings.
|
|
70
77
|
Prec.high(
|
|
@@ -88,7 +95,7 @@ export function BubbleMenu(props: BubbleMenuProps) {
|
|
|
88
95
|
if (!view || !state || dragging() || !(props.editor.focused() || menuFocused())) return null;
|
|
89
96
|
if (state.readOnly && !props.readOnly) return null;
|
|
90
97
|
const range = state.selection.main;
|
|
91
|
-
if (range.empty || dismissed()
|
|
98
|
+
if (range.empty || dismissed()?.eq(range)) return null;
|
|
92
99
|
return selectionBox(view, range.from, range.to);
|
|
93
100
|
};
|
|
94
101
|
const coarse = () => typeof window !== "undefined" && window.matchMedia("(pointer: coarse)").matches;
|
package/src/chart-locale.ts
CHANGED
|
@@ -14,6 +14,6 @@ export const chartLocaleSchema = () =>
|
|
|
14
14
|
}, "Use a valid BCP 47 locale, such as de-DE.");
|
|
15
15
|
|
|
16
16
|
/** The portable renderers which honour an explicit locale. Keep one policy for single charts and groups. */
|
|
17
|
-
const localeKinds = new Set(["treemap", "pareto", "bubble", "radar", "sankey"]);
|
|
17
|
+
const localeKinds = new Set(["treemap", "pareto", "bubble", "radar", "sankey", "balanceFlow"]);
|
|
18
18
|
export const chartAcceptsLocale = (chart: { kind?: string; layout?: string }): boolean =>
|
|
19
19
|
chart.kind === "heatmap" ? chart.layout === "calendar" : localeKinds.has(chart.kind ?? "");
|
package/src/chart-schema.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { ChartInput, ChartLimits } from "@k2b/ui/charts";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
BalanceFlowError,
|
|
4
|
+
CALENDAR_MAX_DAYS,
|
|
5
|
+
calendarDay,
|
|
6
|
+
lineDetailsEnabled,
|
|
7
|
+
nestedDotOptions,
|
|
8
|
+
sankeyGraph,
|
|
9
|
+
validateBalanceFlow,
|
|
10
|
+
} from "@k2b/ui/charts/core";
|
|
3
11
|
import { z } from "zod";
|
|
4
12
|
import { chartLocaleSchema } from "./chart-locale";
|
|
5
13
|
|
|
@@ -52,6 +60,7 @@ export const chartKindSchema = () =>
|
|
|
52
60
|
"bubble",
|
|
53
61
|
"radar",
|
|
54
62
|
"sankey",
|
|
63
|
+
"balanceFlow",
|
|
55
64
|
]);
|
|
56
65
|
|
|
57
66
|
export const chartParts = (limits?: ChartLimits) => {
|
|
@@ -530,6 +539,35 @@ export const chartKinds = (limits?: ChartLimits) => {
|
|
|
530
539
|
if (!Number.isFinite(spec.data.reduce((sum, d) => sum + d.value, 0)))
|
|
531
540
|
ctx.addIssue({ code: "custom", message: "Pareto total must be finite." });
|
|
532
541
|
}),
|
|
542
|
+
z
|
|
543
|
+
.object({
|
|
544
|
+
kind: z.literal("balanceFlow"),
|
|
545
|
+
...ChartKindCommon,
|
|
546
|
+
currency: z.string().regex(/^[A-Z]{3}$/),
|
|
547
|
+
locale: chartLocaleSchema().optional(),
|
|
548
|
+
periods: list(
|
|
549
|
+
z.object({
|
|
550
|
+
label: ChartLabel,
|
|
551
|
+
opening: z.number(),
|
|
552
|
+
inflows: list(z.object({ label: ChartLabel, value: z.number().min(0) }), 0, limits?.maxValues),
|
|
553
|
+
outflows: list(z.object({ label: ChartLabel, value: z.number().min(0) }), 0, limits?.maxValues),
|
|
554
|
+
closing: z.number(),
|
|
555
|
+
}),
|
|
556
|
+
0,
|
|
557
|
+
limits?.maxValues,
|
|
558
|
+
),
|
|
559
|
+
})
|
|
560
|
+
.superRefine((spec, ctx) => {
|
|
561
|
+
try {
|
|
562
|
+
validateBalanceFlow(spec.periods);
|
|
563
|
+
} catch (error) {
|
|
564
|
+
ctx.addIssue({
|
|
565
|
+
code: "custom",
|
|
566
|
+
path: error instanceof BalanceFlowError ? ["periods", error.period] : ["periods"],
|
|
567
|
+
message: error instanceof Error ? error.message : "Invalid balance flow.",
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
}),
|
|
533
571
|
z
|
|
534
572
|
.object({
|
|
535
573
|
kind: z.literal("sankey"),
|
package/src/charts.ts
CHANGED
|
@@ -3,13 +3,19 @@ import { i18n } from "@k2b/stdlib";
|
|
|
3
3
|
import {
|
|
4
4
|
type AreaOptions,
|
|
5
5
|
autoBin,
|
|
6
|
+
type BalanceFlowOptions,
|
|
6
7
|
type BarChartOptions,
|
|
7
8
|
type BubbleOptions,
|
|
9
|
+
balanceFlowDatums,
|
|
10
|
+
balanceFlowFields,
|
|
11
|
+
balanceFlowRenderOptions,
|
|
8
12
|
barChartLayout,
|
|
9
13
|
barGauge,
|
|
10
14
|
boxplot,
|
|
11
15
|
type ChartDatum,
|
|
16
|
+
type ChartRenderSettings,
|
|
12
17
|
calendarWords,
|
|
18
|
+
chartNumberFormatters,
|
|
13
19
|
type charts,
|
|
14
20
|
computeDomain,
|
|
15
21
|
donut,
|
|
@@ -29,6 +35,7 @@ import {
|
|
|
29
35
|
type RadarOptions,
|
|
30
36
|
rankedBarLayout,
|
|
31
37
|
renderAreaSvg,
|
|
38
|
+
renderBalanceFlowSvg,
|
|
32
39
|
renderBarChartSvg,
|
|
33
40
|
renderBubbleSvg,
|
|
34
41
|
renderFunnelSvg,
|
|
@@ -48,6 +55,9 @@ import {
|
|
|
48
55
|
type WaterfallOptions,
|
|
49
56
|
} from "@k2b/ui/charts/core";
|
|
50
57
|
import { z } from "zod";
|
|
58
|
+
|
|
59
|
+
export { balanceFlowRenderOptions, chartNumberFormatters } from "@k2b/ui/charts/core";
|
|
60
|
+
|
|
51
61
|
import { type ChartFormat, type ChartInput, type ChartNumberFormat, chartDateTime, chartKinds } from "./chart-schema";
|
|
52
62
|
|
|
53
63
|
export * from "./chart-schema";
|
|
@@ -55,30 +65,34 @@ export * from "./chart-schema";
|
|
|
55
65
|
type RenderOptions = {
|
|
56
66
|
[K in ChartInput["kind"]]: { kind: K } & (K extends "funnel"
|
|
57
67
|
? FunnelOptions
|
|
58
|
-
: K extends "
|
|
59
|
-
?
|
|
60
|
-
: K extends "
|
|
61
|
-
?
|
|
62
|
-
: K extends "
|
|
63
|
-
?
|
|
64
|
-
: K extends "
|
|
65
|
-
?
|
|
66
|
-
: K extends "
|
|
67
|
-
?
|
|
68
|
-
: K extends "
|
|
69
|
-
?
|
|
70
|
-
: K extends "
|
|
71
|
-
?
|
|
72
|
-
: K extends "
|
|
73
|
-
?
|
|
74
|
-
: K extends "
|
|
75
|
-
?
|
|
76
|
-
: K extends "
|
|
77
|
-
?
|
|
78
|
-
: K extends
|
|
79
|
-
?
|
|
80
|
-
:
|
|
68
|
+
: K extends "balanceFlow"
|
|
69
|
+
? BalanceFlowOptions
|
|
70
|
+
: K extends "hbar"
|
|
71
|
+
? HorizontalBarOptions
|
|
72
|
+
: K extends "waterfall"
|
|
73
|
+
? WaterfallOptions
|
|
74
|
+
: K extends "area"
|
|
75
|
+
? AreaOptions
|
|
76
|
+
: K extends "treemap"
|
|
77
|
+
? TreemapOptions
|
|
78
|
+
: K extends "pareto"
|
|
79
|
+
? ParetoOptions
|
|
80
|
+
: K extends "bubble"
|
|
81
|
+
? BubbleOptions
|
|
82
|
+
: K extends "radar"
|
|
83
|
+
? RadarOptions
|
|
84
|
+
: K extends "sankey"
|
|
85
|
+
? SankeyOptions
|
|
86
|
+
: K extends "heatmap"
|
|
87
|
+
? HeatmapChartOptions
|
|
88
|
+
: K extends "bar"
|
|
89
|
+
? BarChartOptions & { data: NonNullable<BarChartOptions["data"]> }
|
|
90
|
+
: K extends keyof typeof charts
|
|
91
|
+
? Omit<Parameters<(typeof charts)[K]>[0], "width" | "height" | "inspect">
|
|
92
|
+
: never);
|
|
81
93
|
}[ChartInput["kind"]];
|
|
94
|
+
type BalanceFlowInput = Extract<ChartInput, { kind: "balanceFlow" }>;
|
|
95
|
+
type BaseChartInput = Exclude<ChartInput, BalanceFlowInput>;
|
|
82
96
|
|
|
83
97
|
/**
|
|
84
98
|
* Plain-data chart options become the options of the shared
|
|
@@ -307,45 +321,9 @@ type Formats = {
|
|
|
307
321
|
x: { tick: (value: number) => string; value: (value: number) => string };
|
|
308
322
|
};
|
|
309
323
|
|
|
310
|
-
/**
|
|
311
|
-
* Value precision and tick precision share the same style and units; ticks must remain distinct. A currency without
|
|
312
|
-
* `decimals` or compact notation writes cents only when one of the formatted `values` has them: 418,000 € rather than
|
|
313
|
-
* 418,000.00 €.
|
|
314
|
-
*/
|
|
315
|
-
export const chartNumberFormatters = (format: ChartNumberFormat | undefined, locale: string, values?: readonly number[]) => {
|
|
316
|
-
const common: Intl.NumberFormatOptions = {
|
|
317
|
-
style: format?.style === "number" ? "decimal" : format?.style,
|
|
318
|
-
currency: format?.currency,
|
|
319
|
-
};
|
|
320
|
-
// Compact notation keeps its fraction: 1.2K and 1.4K must not both read 1K.
|
|
321
|
-
const decimals =
|
|
322
|
-
format?.decimals ??
|
|
323
|
-
(format?.style === "currency" && !format.compact && values?.length && values.every(Number.isInteger) ? 0 : undefined);
|
|
324
|
-
const value = new Intl.NumberFormat(
|
|
325
|
-
locale,
|
|
326
|
-
format
|
|
327
|
-
? {
|
|
328
|
-
...common,
|
|
329
|
-
...(decimals === undefined ? {} : { minimumFractionDigits: decimals, maximumFractionDigits: decimals }),
|
|
330
|
-
...(format.compact ? { notation: "compact" } : {}),
|
|
331
|
-
}
|
|
332
|
-
: { maximumSignificantDigits: 15 },
|
|
333
|
-
);
|
|
334
|
-
let tick: Intl.NumberFormat | undefined;
|
|
335
|
-
const tickValue = (v: number) => {
|
|
336
|
-
tick ??= new Intl.NumberFormat(locale, {
|
|
337
|
-
...common,
|
|
338
|
-
notation: !format || format.compact ? "compact" : "standard",
|
|
339
|
-
maximumSignificantDigits: 15,
|
|
340
|
-
});
|
|
341
|
-
return tick.format(v);
|
|
342
|
-
};
|
|
343
|
-
const unit = format?.unit ? ` ${format.unit}` : "";
|
|
344
|
-
return { value: (v: number) => `${value.format(v)}${unit}`, tick: (v: number) => `${tickValue(v)}${unit}` };
|
|
345
|
-
};
|
|
346
324
|
const numberFormat = (format: ChartFormat | undefined) => (format && !("date" in format) ? format : undefined);
|
|
347
325
|
/** The values a chart writes with its value format, for whole-number currency. */
|
|
348
|
-
const formattedValues = (input:
|
|
326
|
+
const formattedValues = (input: BaseChartInput): number[] => {
|
|
349
327
|
switch (input.kind) {
|
|
350
328
|
case "sankey":
|
|
351
329
|
return input.links.map((link) => link.value);
|
|
@@ -395,7 +373,7 @@ const formattedValues = (input: ChartInput): number[] => {
|
|
|
395
373
|
return [];
|
|
396
374
|
}
|
|
397
375
|
};
|
|
398
|
-
const formats = (input:
|
|
376
|
+
const formats = (input: BaseChartInput, locale: string): Formats => {
|
|
399
377
|
const yFormat = ("yAxis" in input ? numberFormat(input.yAxis?.format) : undefined) ?? ("format" in input ? input.format : undefined);
|
|
400
378
|
const y = chartNumberFormatters(yFormat, locale, formattedValues(input));
|
|
401
379
|
const xFormat = "xAxis" in input && input.kind !== "bar" ? input.xAxis?.format : undefined;
|
|
@@ -419,7 +397,8 @@ const formats = (input: ChartInput, locale: string): Formats => {
|
|
|
419
397
|
};
|
|
420
398
|
|
|
421
399
|
/** The table's x formatter also titles the shared cursor. */
|
|
422
|
-
export const chartXValueFormat = (input: ChartInput, locale: string): ((x: number) => string) =>
|
|
400
|
+
export const chartXValueFormat = (input: ChartInput, locale: string): ((x: number) => string) =>
|
|
401
|
+
input.kind === "balanceFlow" ? chartNumberFormatters(undefined, locale).value : formats(input, locale).x.value;
|
|
423
402
|
|
|
424
403
|
/** Room for the longest y tick label, so values like 1.2M never run past the left edge on a phone. */
|
|
425
404
|
const leftPadding = (values: number[], value: ChartAxisInput | undefined, tick: (value: number) => string, factor = NARROW) => {
|
|
@@ -469,15 +448,16 @@ export const chartReferenceMark = (input: ChartInput, datum: ChartDatum): boolea
|
|
|
469
448
|
input.kind === "scatter" && input.series[datum.seriesIndex ?? 0]?.role === "reference";
|
|
470
449
|
|
|
471
450
|
/** Options for the shared renderer; the chat block draws the title itself. */
|
|
472
|
-
export function chartRenderOptions(
|
|
473
|
-
input:
|
|
451
|
+
export function chartRenderOptions(input: ChartInput, locale: string, options: ChartRenderSettings = {}): RenderOptions {
|
|
452
|
+
return input.kind === "balanceFlow" ? balanceFlowRenderOptions(input, locale, options) : chartBaseRenderOptions(input, locale, options);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** Plain-chart mapping; hosts that defer the self-contained BalanceFlow frame keep its formatter lazy. */
|
|
456
|
+
export function chartBaseRenderOptions(
|
|
457
|
+
input: BaseChartInput,
|
|
474
458
|
locale: string,
|
|
475
|
-
options: {
|
|
476
|
-
|
|
477
|
-
legend?: "svg" | "outside";
|
|
478
|
-
formats?: { x?: (v: number) => string; y?: (v: number) => string; value?: (v: number) => string };
|
|
479
|
-
} = {},
|
|
480
|
-
): RenderOptions {
|
|
459
|
+
options: ChartRenderSettings = {},
|
|
460
|
+
): Exclude<RenderOptions, { kind: "balanceFlow" }> {
|
|
481
461
|
if ("locale" in input && input.locale) locale = input.locale;
|
|
482
462
|
const defaults = formats(input, locale);
|
|
483
463
|
// Function formats must be measured with the same text the renderer will print.
|
|
@@ -788,6 +768,8 @@ export const drawChart = (options: RenderOptions): string => {
|
|
|
788
768
|
return renderTreemapSvg({ ...options, ...common });
|
|
789
769
|
case "pareto":
|
|
790
770
|
return renderParetoSvg({ ...options, ...common });
|
|
771
|
+
case "balanceFlow":
|
|
772
|
+
return renderBalanceFlowSvg({ ...options, ...common });
|
|
791
773
|
case "sankey":
|
|
792
774
|
return renderSankeySvg({ ...options, ...common });
|
|
793
775
|
case "bubble":
|
|
@@ -888,6 +870,49 @@ export function chartRows(
|
|
|
888
870
|
input: ChartInput,
|
|
889
871
|
locale: string,
|
|
890
872
|
options: { formats?: { x?: (v: number) => string; y?: (v: number) => string; value?: (v: number) => string } } = {},
|
|
873
|
+
): ReturnType<typeof chartBaseRows> {
|
|
874
|
+
return input.kind === "balanceFlow" ? balanceFlowChartRows(input, locale) : chartBaseRows(input, locale, options);
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function balanceFlowChartRows(input: BalanceFlowInput, locale: string): ReturnType<typeof chartBaseRows> {
|
|
878
|
+
const words = balanceFlowFields(input.locale ?? locale);
|
|
879
|
+
const columns = new Map<string, ChartColumn>();
|
|
880
|
+
const rows: ChartRow[] = [];
|
|
881
|
+
return {
|
|
882
|
+
add(datum) {
|
|
883
|
+
columns.set("label", { id: "label", label: words.chartFields({ key: "detail" }), numeric: false });
|
|
884
|
+
const cells: Record<string, string> = { label: datum.label ?? "" };
|
|
885
|
+
const values: Record<string, string | number> = { label: datum.label ?? "" };
|
|
886
|
+
for (const field of datum.values) {
|
|
887
|
+
if (!columns.has(field.key))
|
|
888
|
+
columns.set(field.key, {
|
|
889
|
+
id: field.key,
|
|
890
|
+
label:
|
|
891
|
+
field.label ??
|
|
892
|
+
(field.key === "period"
|
|
893
|
+
? words.balancePeriod
|
|
894
|
+
: field.key === "direction"
|
|
895
|
+
? words.balanceDirection
|
|
896
|
+
: words.chartFields({ key: field.key })),
|
|
897
|
+
numeric: typeof field.value === "number",
|
|
898
|
+
});
|
|
899
|
+
cells[field.key] = field.formatted ?? String(field.value);
|
|
900
|
+
values[field.key] = field.value;
|
|
901
|
+
}
|
|
902
|
+
const row = { key: chartMarkKey(datum), cells, values };
|
|
903
|
+
rows.push(row);
|
|
904
|
+
return row;
|
|
905
|
+
},
|
|
906
|
+
columns: () => [...columns.values()],
|
|
907
|
+
table: () => ({ columns: [...columns.values()], rows }),
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/** Table builder for ordinary SVG charts; the BalanceFlow asset owns its complete table. */
|
|
912
|
+
export function chartBaseRows(
|
|
913
|
+
input: BaseChartInput,
|
|
914
|
+
locale: string,
|
|
915
|
+
options: { formats?: { x?: (v: number) => string; y?: (v: number) => string; value?: (v: number) => string } } = {},
|
|
891
916
|
) {
|
|
892
917
|
if ("locale" in input && input.locale) locale = input.locale;
|
|
893
918
|
const t = labels.resolve([locale]).t;
|
|
@@ -1120,7 +1145,7 @@ export function chartRows(
|
|
|
1120
1145
|
rows.some((row) => row.values.rank !== undefined)
|
|
1121
1146
|
) {
|
|
1122
1147
|
const source = { ...input, limit: undefined };
|
|
1123
|
-
const all =
|
|
1148
|
+
const all = chartBaseRows(source, locale, options);
|
|
1124
1149
|
const layout = rankedBarLayout({ data: source.data, title: source.title, subtitle: source.subtitle, valueFormat, locale });
|
|
1125
1150
|
for (const row of layout.rows) {
|
|
1126
1151
|
const value = Number.isFinite(row.value) ? row.value : null;
|
|
@@ -1167,7 +1192,8 @@ export function chartRows(
|
|
|
1167
1192
|
/** The data behind every mark of the chart as a table: the accessible alternative and what Copy data copies. */
|
|
1168
1193
|
export function chartTable(input: ChartInput, locale: string): ChartTable {
|
|
1169
1194
|
const rows = chartRows(input, locale);
|
|
1170
|
-
|
|
1195
|
+
const drawing = chartRenderOptions(input, locale);
|
|
1196
|
+
for (const datum of drawing.kind === "balanceFlow" ? balanceFlowDatums(drawing) : chartSvgMarks(drawChart(drawing))) rows.add(datum);
|
|
1171
1197
|
return rows.table();
|
|
1172
1198
|
}
|
|
1173
1199
|
|
|
@@ -1176,6 +1202,8 @@ export const chartMarks = (input: ChartInput): number => {
|
|
|
1176
1202
|
switch (input.kind) {
|
|
1177
1203
|
case "pareto":
|
|
1178
1204
|
return input.data.length * 3;
|
|
1205
|
+
case "balanceFlow":
|
|
1206
|
+
return input.periods.reduce((total, p) => total + p.inflows.length + p.outflows.length + 3, 0);
|
|
1179
1207
|
case "sankey": {
|
|
1180
1208
|
const graph = sankeyGraph(input);
|
|
1181
1209
|
return (
|
|
@@ -269,8 +269,16 @@ export const remoteCursors = (ytext: Y.Text, awareness: Awareness): Extension =>
|
|
|
269
269
|
) {
|
|
270
270
|
awareness.setLocalStateField("cursor", { anchor, head });
|
|
271
271
|
}
|
|
272
|
-
} else if (cursor != null &&
|
|
273
|
-
|
|
272
|
+
} else if (cursor != null && !update.view.dom.contains(update.view.dom.ownerDocument.activeElement)) {
|
|
273
|
+
// Focus moved to another field: the others no longer see a caret here until it returns. A table cell inside
|
|
274
|
+
// the editor keeps the cursor it publishes itself, and another window keeps the last place. The change is
|
|
275
|
+
// written after this update: other editors on the same awareness may be inside an update of their own,
|
|
276
|
+
// such as the one whose edit reached this view, and redraw on it.
|
|
277
|
+
const view = update.view;
|
|
278
|
+
queueMicrotask(() => {
|
|
279
|
+
if (view.hasFocus || view.dom.contains(view.dom.ownerDocument.activeElement)) return;
|
|
280
|
+
if (awareness.getLocalState()?.cursor != null) awareness.setLocalStateField("cursor", null);
|
|
281
|
+
});
|
|
274
282
|
}
|
|
275
283
|
}
|
|
276
284
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { syntaxTree } from "@codemirror/language";
|
|
2
|
+
import type { Transaction } from "@codemirror/state";
|
|
3
|
+
import { Tree, type TreeBuffer } from "@lezer/common";
|
|
4
|
+
|
|
5
|
+
export type MarkdownSpan = { from: number; to: number };
|
|
6
|
+
const changedBlocks = new WeakMap<Transaction, readonly MarkdownSpan[]>();
|
|
7
|
+
const blocks = (tree: Tree, offset = 0, result = new Map<Tree | TreeBuffer, number>()): Map<Tree | TreeBuffer, number> => {
|
|
8
|
+
tree.children.forEach((child, i) => {
|
|
9
|
+
const from = offset + tree.positions[i]!;
|
|
10
|
+
if (child instanceof Tree && child.type.isAnonymous) blocks(child, from, result);
|
|
11
|
+
else result.set(child, from);
|
|
12
|
+
});
|
|
13
|
+
return result;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Markdown reparses whole top-level blocks and reuses the other child trees.
|
|
17
|
+
* Compare those identities, including parser-only progress, before visiting nodes.
|
|
18
|
+
*/
|
|
19
|
+
export const changedMarkdownBlocks = (tr: Transaction): readonly MarkdownSpan[] => {
|
|
20
|
+
const cached = changedBlocks.get(tr);
|
|
21
|
+
if (cached) return cached;
|
|
22
|
+
const before = syntaxTree(tr.startState);
|
|
23
|
+
const after = syntaxTree(tr.state);
|
|
24
|
+
if (before === after) return [];
|
|
25
|
+
const old = blocks(before);
|
|
26
|
+
const spans: MarkdownSpan[] = [];
|
|
27
|
+
for (const [child, from] of blocks(after)) {
|
|
28
|
+
const previous = old.get(child);
|
|
29
|
+
if (previous !== undefined && tr.changes.mapPos(previous, 1) === from) old.delete(child);
|
|
30
|
+
else spans.push({ from, to: from + child.length });
|
|
31
|
+
}
|
|
32
|
+
for (const [child, from] of old) spans.push({ from: tr.changes.mapPos(from, -1), to: tr.changes.mapPos(from + child.length, 1) });
|
|
33
|
+
const merged = mergeMarkdownSpans(spans);
|
|
34
|
+
changedBlocks.set(tr, merged);
|
|
35
|
+
return merged;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const mergeMarkdownSpans = (spans: MarkdownSpan[]): MarkdownSpan[] => {
|
|
39
|
+
const merged: MarkdownSpan[] = [];
|
|
40
|
+
for (const span of spans.sort((a, b) => a.from - b.from)) {
|
|
41
|
+
const previous = merged.at(-1);
|
|
42
|
+
if (previous && span.from <= previous.to) previous.to = Math.max(previous.to, span.to);
|
|
43
|
+
else merged.push({ ...span });
|
|
44
|
+
}
|
|
45
|
+
return merged;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Sorted, merged spans allow bounded lookups even for a bulk transaction. */
|
|
49
|
+
export const intersectsMarkdownSpans = (spans: readonly MarkdownSpan[], from: number, to: number): boolean => {
|
|
50
|
+
let low = 0,
|
|
51
|
+
high = spans.length;
|
|
52
|
+
while (low < high) {
|
|
53
|
+
const middle = (low + high) >>> 1;
|
|
54
|
+
if (spans[middle]!.to <= from) low = middle + 1;
|
|
55
|
+
else high = middle;
|
|
56
|
+
}
|
|
57
|
+
return low < spans.length && spans[low]!.from < to;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const markdownBlockAt = (tree: Tree, position: number): MarkdownSpan => {
|
|
61
|
+
let node = tree.resolve(position, 1);
|
|
62
|
+
if (!node.parent) node = tree.resolve(position, -1);
|
|
63
|
+
if (!node.parent) return { from: position, to: position };
|
|
64
|
+
while (node.parent?.parent) node = node.parent;
|
|
65
|
+
return { from: node.from, to: node.to };
|
|
66
|
+
};
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
* the standard shape.
|
|
41
41
|
*/
|
|
42
42
|
import { forceParsing, syntaxTree } from "@codemirror/language";
|
|
43
|
-
import type { EditorState, Extension, SelectionRange, Transaction } from "@codemirror/state";
|
|
43
|
+
import type { EditorSelection, EditorState, Extension, SelectionRange, Transaction } from "@codemirror/state";
|
|
44
44
|
import { Prec, StateEffect, StateField } from "@codemirror/state";
|
|
45
45
|
import type { DecorationSet } from "@codemirror/view";
|
|
46
46
|
import { Decoration, EditorView, keymap, ViewPlugin } from "@codemirror/view";
|
|
@@ -74,7 +74,9 @@ export type IncrementalOptions = {
|
|
|
74
74
|
changesMightAffectSyntax: (tr: Transaction) => boolean;
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
/** No selection (an editor without focus) intersects nothing. */
|
|
78
|
+
export const selectionIntersectsRange = (selection: SelectionRange | undefined, from: number, to: number): boolean => {
|
|
79
|
+
if (!selection) return false;
|
|
78
80
|
if (selection.empty) return selection.from >= from && selection.to <= to;
|
|
79
81
|
return selection.from < to && selection.to > from;
|
|
80
82
|
};
|
|
@@ -93,6 +95,53 @@ export const refreshMarkdownDecorationsEffect = StateEffect.define<void>();
|
|
|
93
95
|
|
|
94
96
|
export const pointerSelectionHoldEffect = StateEffect.define<boolean>();
|
|
95
97
|
|
|
98
|
+
/** Marks the selection of its transaction as placed by a person, for a control that shows a block's source in an editor
|
|
99
|
+
* that cannot take focus, such as a read-only note. */
|
|
100
|
+
export const placeMarkdownCaret = StateEffect.define<void>();
|
|
101
|
+
|
|
102
|
+
const markdownBlurEffect = StateEffect.define<void>();
|
|
103
|
+
|
|
104
|
+
const pressHeld = (tr: Transaction) => tr.state.field(pointerSelectionHeld, false) === true;
|
|
105
|
+
|
|
106
|
+
/** Something the person did in the editor: set or moved the selection, typed, deleted, undid or redid. */
|
|
107
|
+
const personActs = (tr: Transaction) => ["select", "input", "delete", "undo", "redo", "move"].some((event) => tr.isUserEvent(event));
|
|
108
|
+
|
|
109
|
+
/** A press that acted while its button is still down: it counts from the refresh after the release, like its selection. */
|
|
110
|
+
const actAwaitingRelease = StateField.define<boolean>({
|
|
111
|
+
create: () => false,
|
|
112
|
+
update(value, tr) {
|
|
113
|
+
if (tr.effects.some((effect) => effect.is(markdownBlurEffect) || effect.is(refreshMarkdownDecorationsEffect))) value = false;
|
|
114
|
+
return value || (personActs(tr) && pressHeld(tr));
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/** Whether the person is editing in the editor: they acted in it (a press, a key that moves the caret, typing) since
|
|
119
|
+
* it last lost focus, or a control placed the caret in an editor that cannot take focus. Focus alone is not enough:
|
|
120
|
+
* a note that the app focuses on open, or that regains focus from a dialog or another window, shows every block
|
|
121
|
+
* formatted and no `#` before its title, the same as the server render. The block last edited renders again once
|
|
122
|
+
* focus leaves. Only blur is reported as a focus transaction, so focus moves between the editor and its own sheets and
|
|
123
|
+
* cells stay plain updates. Each decoration that reveals Markdown source brings this field along. */
|
|
124
|
+
export const markdownEditorFocused: StateField<boolean> = StateField.define<boolean>({
|
|
125
|
+
create: () => false,
|
|
126
|
+
update(value, tr) {
|
|
127
|
+
if (tr.effects.some((effect) => effect.is(markdownBlurEffect))) return false;
|
|
128
|
+
if (tr.effects.some((effect) => effect.is(placeMarkdownCaret))) return true;
|
|
129
|
+
if (tr.effects.some((effect) => effect.is(refreshMarkdownDecorationsEffect)) && tr.startState.field(actAwaitingRelease, false) === true)
|
|
130
|
+
return true;
|
|
131
|
+
return value || (personActs(tr) && !pressHeld(tr));
|
|
132
|
+
},
|
|
133
|
+
provide: (field) => [
|
|
134
|
+
actAwaitingRelease,
|
|
135
|
+
EditorView.focusChangeEffect.of((state, focusing) =>
|
|
136
|
+
!focusing && (state.field(field) || state.field(actAwaitingRelease)) ? markdownBlurEffect.of() : null,
|
|
137
|
+
),
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
/** The selection Markdown reveals its source around, or `null` while the person is not editing in the editor. */
|
|
142
|
+
export const revealedSelection = (state: EditorState): EditorSelection | null =>
|
|
143
|
+
state.field(markdownEditorFocused, false) === false ? null : state.selection;
|
|
144
|
+
|
|
96
145
|
/** True while a mouse or pen button is down in the editor. Revealing Markdown then would move the text under the
|
|
97
146
|
* pointer mid-drag, so selection-driven decorations wait for the refresh after release. */
|
|
98
147
|
export const pointerSelectionHeld = StateField.define<boolean>({
|
|
@@ -111,6 +160,12 @@ export const holdsDecorations = (tr: Transaction): boolean =>
|
|
|
111
160
|
tr.state.field(pointerSelectionHeld, false) === true &&
|
|
112
161
|
isPointerSelectionTransaction(tr);
|
|
113
162
|
|
|
163
|
+
/** True when decorations that follow the selection must rebuild although it may not have moved: an explicit refresh, or
|
|
164
|
+
* the person starting or stopping to edit. A press waits for the refresh after its release, like its selection. */
|
|
165
|
+
export const refreshesMarkdownDecorations = (tr: Transaction): boolean =>
|
|
166
|
+
tr.effects.some((effect) => effect.is(refreshMarkdownDecorationsEffect)) ||
|
|
167
|
+
(tr.startState.field(markdownEditorFocused, false) !== tr.state.field(markdownEditorFocused, false) && !pressHeld(tr));
|
|
168
|
+
|
|
114
169
|
export const initialMarkdownDecorationRefreshExtension = (): Extension =>
|
|
115
170
|
ViewPlugin.fromClass(
|
|
116
171
|
class {
|
|
@@ -243,8 +298,8 @@ export const blockWidgetLineNavigationExtension = <T>(
|
|
|
243
298
|
* `null` if it sits outside all ranges. A cursor-only transaction
|
|
244
299
|
* needs a rebuild only when this answer changes. */
|
|
245
300
|
const cursorKey = (state: EditorState, ranges: CursorZoneRange[]): string | null => {
|
|
246
|
-
|
|
247
|
-
|
|
301
|
+
const cursor = revealedSelection(state)?.main;
|
|
302
|
+
if (ranges.length === 0 || !cursor) return null;
|
|
248
303
|
const hits: number[] = [];
|
|
249
304
|
for (const r of ranges) {
|
|
250
305
|
if (selectionIntersectsRange(cursor, r.from, r.to)) hits.push(r.from);
|
|
@@ -293,7 +348,7 @@ export const cursorZoneStateField = (
|
|
|
293
348
|
return StateField.define<CursorZoneState>({
|
|
294
349
|
create: build,
|
|
295
350
|
update(value, tr) {
|
|
296
|
-
if (tr
|
|
351
|
+
if (refreshesMarkdownDecorations(tr)) {
|
|
297
352
|
return build(tr.state);
|
|
298
353
|
}
|
|
299
354
|
if (tr.docChanged) {
|
|
@@ -332,6 +387,7 @@ export const cursorZoneStateField = (
|
|
|
332
387
|
return [
|
|
333
388
|
EditorView.decorations.from(field, (v) => v.decorations),
|
|
334
389
|
EditorView.atomicRanges.of((view) => view.state.field(field).atomicDecorations ?? Decoration.none),
|
|
390
|
+
markdownEditorFocused,
|
|
335
391
|
];
|
|
336
392
|
},
|
|
337
393
|
});
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { syntaxTree } from "@codemirror/language";
|
|
7
7
|
import { type ChangeSet, type ChangeSpec, type EditorState, Facet } from "@codemirror/state";
|
|
8
8
|
import type { Tree } from "@lezer/common";
|
|
9
|
+
import { mergeMarkdownSpans } from "./changed-blocks";
|
|
9
10
|
|
|
10
11
|
export type ListItemKind = "bullet" | "ordered" | "task";
|
|
11
12
|
export type ListItem = {
|
|
@@ -59,6 +60,8 @@ export const listStructure = (
|
|
|
59
60
|
const items: ListItem[] = [];
|
|
60
61
|
const parents: Array<{ from: number; index: number }> = [];
|
|
61
62
|
const lists: Array<{ ordered: boolean; start: number | null; index: number }> = [];
|
|
63
|
+
let spans: readonly ExcludedSpan[] | undefined;
|
|
64
|
+
let exclusion = 0;
|
|
62
65
|
tree.iterate({
|
|
63
66
|
from: range?.from,
|
|
64
67
|
to: range?.to,
|
|
@@ -78,7 +81,9 @@ export const listStructure = (
|
|
|
78
81
|
const ordinal = list.start === null ? null : list.start + list.index;
|
|
79
82
|
list.index++;
|
|
80
83
|
const line = state.doc.lineAt(mark.from);
|
|
81
|
-
|
|
84
|
+
spans ??= mergeMarkdownSpans([...excludedSpans()]);
|
|
85
|
+
while (spans[exclusion] && spans[exclusion]!.to <= mark.from) exclusion++;
|
|
86
|
+
if (items.at(-1)?.line === line.number || (spans[exclusion] && mark.from >= spans[exclusion]!.from)) return false;
|
|
82
87
|
const task = node.getChild("Task")?.getChild("TaskMarker");
|
|
83
88
|
let contentFrom = task?.to ?? mark.to;
|
|
84
89
|
while (contentFrom < line.to && /[ \t]/.test(state.sliceDoc(contentFrom, contentFrom + 1))) contentFrom++;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
/** KaTeX source syntax, shared by the math widgets and every decoration that must leave math alone. */
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
// Delimiters cannot nest. Stop at a new opening delimiter so malformed pasted
|
|
3
|
+
// brackets cannot consume a later formula or repeatedly scan the document tail.
|
|
4
|
+
export const blockMathPattern = (): RegExp => /\$\$([^$]+)\$\$|\\\[((?:(?!\\\[).)*?)\\\]/gs;
|
|
5
|
+
export const inlineMathPattern = (): RegExp => /(?<!\$)\$(?!\$)([^\n$]+)\$(?!\$)|\\\(((?:(?!\\\()[^)\n])*?)\\\)/g;
|
|
6
|
+
export const hasMathMarker = (text: string): boolean => /\$|\\[()[\]]/.test(text);
|