@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
|
@@ -8,7 +8,10 @@ import { render } from "solid-js/web";
|
|
|
8
8
|
import {
|
|
9
9
|
blockWidgetLineNavigationExtension,
|
|
10
10
|
holdsDecorations,
|
|
11
|
-
|
|
11
|
+
markdownEditorFocused,
|
|
12
|
+
placeMarkdownCaret,
|
|
13
|
+
refreshesMarkdownDecorations,
|
|
14
|
+
revealedSelection,
|
|
12
15
|
selectionIntersectsRange,
|
|
13
16
|
} from "./_lib/cursor-zone-field";
|
|
14
17
|
import {
|
|
@@ -105,7 +108,7 @@ class SourceToggleWidget extends WidgetType {
|
|
|
105
108
|
label={this.label}
|
|
106
109
|
pressed
|
|
107
110
|
onClick={() => {
|
|
108
|
-
view.dispatch({ selection: { anchor: this.exit } });
|
|
111
|
+
view.dispatch({ selection: { anchor: this.exit }, effects: placeMarkdownCaret.of() });
|
|
109
112
|
view.focus();
|
|
110
113
|
}}
|
|
111
114
|
/>
|
|
@@ -192,7 +195,9 @@ class QueryBlockWidget extends WidgetType {
|
|
|
192
195
|
status.className = "notebook-book-chart__status text-sm text-dimmed";
|
|
193
196
|
status.setAttribute("role", "status");
|
|
194
197
|
status.textContent = this.snapshot.failed ? t.failed : this.staleReadOnly ? t.savedChanged : (this.diagnostic ?? t.loading);
|
|
195
|
-
content.innerHTML = notebookChartFrameHtml(this.chart, "");
|
|
198
|
+
content.innerHTML = notebookChartFrameHtml(this.chart, "", { locale: this.snapshot.locale });
|
|
199
|
+
const balanceStatus = content.querySelector(".k2b-balance-flow__status p");
|
|
200
|
+
if (balanceStatus) balanceStatus.textContent = status.textContent;
|
|
196
201
|
content.querySelector(".notebook-book-chart__viewport")?.append(status);
|
|
197
202
|
} else {
|
|
198
203
|
content.className += " text-sm text-dimmed";
|
|
@@ -227,7 +232,8 @@ class QueryBlockWidget extends WidgetType {
|
|
|
227
232
|
});
|
|
228
233
|
}
|
|
229
234
|
const reveal = () => {
|
|
230
|
-
|
|
235
|
+
// A read-only note cannot take focus; the press itself places the caret.
|
|
236
|
+
view.dispatch({ selection: { anchor: this.block.from }, scrollIntoView: true, effects: placeMarkdownCaret.of() });
|
|
231
237
|
view.focus();
|
|
232
238
|
};
|
|
233
239
|
this.dispose = render(
|
|
@@ -401,7 +407,7 @@ export function createBlockPreviews(options: BlockPreviewOptions): { extension:
|
|
|
401
407
|
};
|
|
402
408
|
const sourceToggles: Range<Decoration>[] = [];
|
|
403
409
|
for (const block of blocks) {
|
|
404
|
-
if (selectionIntersectsRange(state
|
|
410
|
+
if (selectionIntersectsRange(revealedSelection(state)?.main, block.from, block.to)) {
|
|
405
411
|
const exit = blockExit(block, blocks, state.doc.length);
|
|
406
412
|
if (exit !== undefined) {
|
|
407
413
|
const line = state.doc.lineAt(block.from);
|
|
@@ -453,20 +459,21 @@ export function createBlockPreviews(options: BlockPreviewOptions): { extension:
|
|
|
453
459
|
update(value, tr) {
|
|
454
460
|
const effect = tr.effects.find((effect) => effect.is(previewEffect));
|
|
455
461
|
if (effect?.is(previewEffect)) return build(tr.state, effect.value);
|
|
456
|
-
if (tr.docChanged || tr
|
|
462
|
+
if (tr.docChanged || refreshesMarkdownDecorations(tr)) return build(tr.state, value.snapshot);
|
|
457
463
|
if (holdsDecorations(tr)) return value;
|
|
458
464
|
if (
|
|
459
465
|
tr.selection &&
|
|
460
466
|
value.ranges.some(
|
|
461
467
|
(range) =>
|
|
462
|
-
selectionIntersectsRange(tr.startState
|
|
463
|
-
selectionIntersectsRange(tr.state
|
|
468
|
+
selectionIntersectsRange(revealedSelection(tr.startState)?.main, range.from, range.to) !==
|
|
469
|
+
selectionIntersectsRange(revealedSelection(tr.state)?.main, range.from, range.to),
|
|
464
470
|
)
|
|
465
471
|
)
|
|
466
472
|
return build(tr.state, value.snapshot);
|
|
467
473
|
return value;
|
|
468
474
|
},
|
|
469
475
|
provide: (field) => [
|
|
476
|
+
markdownEditorFocused,
|
|
470
477
|
EditorView.decorations.from(field, (value) => value.decorations),
|
|
471
478
|
EditorView.decorations.from(field, (value) => value.sourceToggles),
|
|
472
479
|
EditorView.atomicRanges.of((view) => view.state.field(field).decorations),
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { type EditorState, type Extension, Prec, type Range, RangeSet, StateField } from "@codemirror/state";
|
|
2
2
|
import { Decoration, type DecorationSet, EditorView, keymap, WidgetType } from "@codemirror/view";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
holdsDecorations,
|
|
5
|
+
markdownEditorFocused,
|
|
6
|
+
placeMarkdownCaret,
|
|
7
|
+
refreshesMarkdownDecorations,
|
|
8
|
+
revealedSelection,
|
|
9
|
+
selectionIntersectsRange,
|
|
10
|
+
} from "./_lib/cursor-zone-field";
|
|
4
11
|
import { type DataBlock, extractDataBlocks, namedBlockBody, renderDataBlockHtml } from "./lib/named-blocks";
|
|
5
12
|
import { blockAnchorsDrawnElsewhere } from "./named-blocks";
|
|
6
13
|
|
|
@@ -33,7 +40,7 @@ class DataBlockWidget extends WidgetType {
|
|
|
33
40
|
el.onmousedown = (event) => {
|
|
34
41
|
event.preventDefault();
|
|
35
42
|
event.stopPropagation();
|
|
36
|
-
view.dispatch({ selection: { anchor: this.block.blockStart }, scrollIntoView: true });
|
|
43
|
+
view.dispatch({ selection: { anchor: this.block.blockStart }, scrollIntoView: true, effects: placeMarkdownCaret.of() });
|
|
37
44
|
view.focus();
|
|
38
45
|
};
|
|
39
46
|
el.ondblclick = (event) => {
|
|
@@ -68,7 +75,7 @@ export const dataBlocksExtension = (): Extension => {
|
|
|
68
75
|
const stateField = StateField.define<DataBlocksState>({
|
|
69
76
|
create: scanDataBlocks,
|
|
70
77
|
update(value, tr) {
|
|
71
|
-
if (tr
|
|
78
|
+
if (refreshesMarkdownDecorations(tr)) return scanDataBlocks(tr.state);
|
|
72
79
|
if (tr.docChanged) return scanDataBlocks(tr.state);
|
|
73
80
|
if (holdsDecorations(tr)) return value;
|
|
74
81
|
if (!tr.selection) return value;
|
|
@@ -97,6 +104,7 @@ export const dataBlocksExtension = (): Extension => {
|
|
|
97
104
|
);
|
|
98
105
|
|
|
99
106
|
return [
|
|
107
|
+
markdownEditorFocused,
|
|
100
108
|
stateField,
|
|
101
109
|
navKeymap,
|
|
102
110
|
EditorView.theme({
|
|
@@ -116,7 +124,7 @@ const scanDataBlocks = (state: EditorState): DataBlocksState => {
|
|
|
116
124
|
const decorations: Range<Decoration>[] = [];
|
|
117
125
|
const blockWidgetDecorations: Range<Decoration>[] = [];
|
|
118
126
|
const ranges: DataBlockRange[] = [];
|
|
119
|
-
const cursor = state
|
|
127
|
+
const cursor = revealedSelection(state)?.main;
|
|
120
128
|
const text = state.doc.toString();
|
|
121
129
|
|
|
122
130
|
for (const block of extractDataBlocks(text)) {
|
|
@@ -146,7 +154,7 @@ const scanDataBlocks = (state: EditorState): DataBlocksState => {
|
|
|
146
154
|
};
|
|
147
155
|
|
|
148
156
|
const cursorDataBlockKey = (state: EditorState, ranges: DataBlockRange[]): string | null => {
|
|
149
|
-
const cursor = state
|
|
157
|
+
const cursor = revealedSelection(state)?.main;
|
|
150
158
|
const hits: number[] = [];
|
|
151
159
|
for (const range of ranges) {
|
|
152
160
|
if (selectionIntersectsRange(cursor, range.from, range.to)) hits.push(range.widgetFrom);
|
package/src/markdown/images.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
blockWidgetLineNavigationExtension,
|
|
7
7
|
type CursorZoneState,
|
|
8
8
|
cursorZoneStateField,
|
|
9
|
+
revealedSelection,
|
|
9
10
|
selectionIntersectsRange,
|
|
10
11
|
} from "./_lib/cursor-zone-field";
|
|
11
12
|
import { parseSizedMarkdownImage } from "./lib/markdown-image";
|
|
@@ -127,7 +128,7 @@ const findMarkdownImages = (state: EditorState, options: MarkdownUrlOptions): Cu
|
|
|
127
128
|
const decorations: Range<Decoration>[] = [];
|
|
128
129
|
const atomicDecorations: Range<Decoration>[] = [];
|
|
129
130
|
const ranges: { from: number; to: number }[] = [];
|
|
130
|
-
const cursor = state
|
|
131
|
+
const cursor = revealedSelection(state)?.main;
|
|
131
132
|
|
|
132
133
|
syntaxTree(state).iterate({
|
|
133
134
|
enter: (node) => {
|
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
blockWidgetLineNavigationExtension,
|
|
7
7
|
type CursorZoneState,
|
|
8
8
|
cursorZoneStateField,
|
|
9
|
+
placeMarkdownCaret,
|
|
10
|
+
revealedSelection,
|
|
9
11
|
selectionIntersectsRange,
|
|
10
12
|
} from "./_lib/cursor-zone-field";
|
|
11
13
|
import { literalMarkdownLines, NOTEBOOK_DIRECTIVE_START } from "./lib/markdown-context";
|
|
@@ -49,7 +51,7 @@ class InfoBlockWidget extends WidgetType {
|
|
|
49
51
|
container.onmousedown = (event) => {
|
|
50
52
|
event.preventDefault();
|
|
51
53
|
event.stopPropagation();
|
|
52
|
-
view.dispatch({ selection: { anchor: this.fromPos }, scrollIntoView: true });
|
|
54
|
+
view.dispatch({ selection: { anchor: this.fromPos }, scrollIntoView: true, effects: placeMarkdownCaret.of() });
|
|
53
55
|
view.focus();
|
|
54
56
|
};
|
|
55
57
|
container.ondblclick = (event) => {
|
|
@@ -96,7 +98,7 @@ class InfoBlockWidget extends WidgetType {
|
|
|
96
98
|
const findInfoBlocks = (state: EditorState, locale: string): CursorZoneState => {
|
|
97
99
|
const decorations: Range<Decoration>[] = [];
|
|
98
100
|
const ranges: { from: number; to: number }[] = [];
|
|
99
|
-
const cursor = state
|
|
101
|
+
const cursor = revealedSelection(state)?.main;
|
|
100
102
|
const text = state.doc.toString();
|
|
101
103
|
if (!text.includes(":::")) return { decorations: Decoration.none, atomicDecorations: Decoration.none, ranges };
|
|
102
104
|
const literal = literalMarkdownLines(text);
|
package/src/markdown/katex.ts
CHANGED
|
@@ -3,13 +3,16 @@ import type { EditorState, Extension, Range, Transaction } from "@codemirror/sta
|
|
|
3
3
|
import { RangeSet } from "@codemirror/state";
|
|
4
4
|
import { Decoration, EditorView, WidgetType } from "@codemirror/view";
|
|
5
5
|
import katex from "katex";
|
|
6
|
+
import { intersectsMarkdownSpans, mergeMarkdownSpans } from "./_lib/changed-blocks";
|
|
6
7
|
import {
|
|
7
8
|
blockWidgetLineNavigationExtension,
|
|
8
9
|
type CursorZoneState,
|
|
9
10
|
cursorZoneStateField,
|
|
11
|
+
placeMarkdownCaret,
|
|
12
|
+
revealedSelection,
|
|
10
13
|
selectionIntersectsRange,
|
|
11
14
|
} from "./_lib/cursor-zone-field";
|
|
12
|
-
import { blockMathPattern, inlineMathPattern } from "./_lib/math-syntax";
|
|
15
|
+
import { blockMathPattern, hasMathMarker, inlineMathPattern } from "./_lib/math-syntax";
|
|
13
16
|
import { codeFenceLanguage } from "./lib/markdown-fences";
|
|
14
17
|
|
|
15
18
|
// =============================================================================
|
|
@@ -112,7 +115,7 @@ class BlockMathWidget extends WidgetType {
|
|
|
112
115
|
container.onmousedown = (event) => {
|
|
113
116
|
event.preventDefault();
|
|
114
117
|
event.stopPropagation();
|
|
115
|
-
view.dispatch({ selection: { anchor: this.fromPos }, scrollIntoView: true });
|
|
118
|
+
view.dispatch({ selection: { anchor: this.fromPos }, scrollIntoView: true, effects: placeMarkdownCaret.of() });
|
|
116
119
|
view.focus();
|
|
117
120
|
};
|
|
118
121
|
container.ondblclick = (event) => {
|
|
@@ -147,11 +150,9 @@ class BlockMathWidget extends WidgetType {
|
|
|
147
150
|
}
|
|
148
151
|
}
|
|
149
152
|
|
|
150
|
-
const hasMathMarker = (text: string): boolean => text.includes("$") || text.includes("\\");
|
|
151
|
-
|
|
152
153
|
/** Predicate for the incremental cursor-zone mode. Cheap O(insert + 4)
|
|
153
154
|
* scan to decide whether a doc change might introduce or remove a
|
|
154
|
-
* math marker — `$` or
|
|
155
|
+
* math marker — `$` or a backslash delimiter in the inserted text or the ±2 char
|
|
155
156
|
* window around it. False positives (e.g. typing `$` inside code)
|
|
156
157
|
* fall back to a baseline rebuild; false negatives would leave
|
|
157
158
|
* stale decorations, so the predicate is intentionally generous. */
|
|
@@ -170,15 +171,10 @@ const changesMightAffectMath = (tr: Transaction): boolean => {
|
|
|
170
171
|
return might;
|
|
171
172
|
};
|
|
172
173
|
|
|
173
|
-
const intersectsAnyRange = (ranges: { from: number; to: number }[], from: number, to: number): boolean =>
|
|
174
|
-
ranges.some(
|
|
175
|
-
(range) => (from >= range.from && from < range.to) || (to > range.from && to <= range.to) || (from <= range.from && to >= range.to),
|
|
176
|
-
);
|
|
177
|
-
|
|
178
174
|
const buildKatexDecorations = (state: EditorState): CursorZoneState => {
|
|
179
175
|
const decorations: Range<Decoration>[] = [];
|
|
180
176
|
const atomicDecorations: Range<Decoration>[] = [];
|
|
181
|
-
const cursor = state
|
|
177
|
+
const cursor = revealedSelection(state)?.main;
|
|
182
178
|
const doc = state.doc.toString();
|
|
183
179
|
let hasSyntax = false;
|
|
184
180
|
const ranges: { from: number; to: number }[] = [];
|
|
@@ -209,12 +205,14 @@ const buildKatexDecorations = (state: EditorState): CursorZoneState => {
|
|
|
209
205
|
atomicDecorations.push(blockDecoration);
|
|
210
206
|
}
|
|
211
207
|
}
|
|
212
|
-
if (node.type.name === "InlineCode") {
|
|
208
|
+
if (node.type.name === "InlineCode" || node.type.name === "CodeBlock") {
|
|
213
209
|
codeRanges.push({ from: node.from, to: node.to });
|
|
214
210
|
}
|
|
215
211
|
},
|
|
216
212
|
});
|
|
217
213
|
|
|
214
|
+
const code = mergeMarkdownSpans(codeRanges);
|
|
215
|
+
|
|
218
216
|
/**
|
|
219
217
|
* Walk a global regex over the doc, push a decoration per match
|
|
220
218
|
* UNLESS the match falls inside a code range (handled separately)
|
|
@@ -251,7 +249,7 @@ const buildKatexDecorations = (state: EditorState): CursorZoneState => {
|
|
|
251
249
|
ranges.push({ from, to });
|
|
252
250
|
}
|
|
253
251
|
const cursorInside = selectionIntersectsRange(cursor, sourceVisibleFrom, sourceVisibleTo);
|
|
254
|
-
const skip =
|
|
252
|
+
const skip = intersectsMarkdownSpans(code, from, to) || cursorInside;
|
|
255
253
|
match = re.exec(doc);
|
|
256
254
|
if (skip) continue;
|
|
257
255
|
const decoration = Decoration.replace(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { highlight, text } from "@k2b/stdlib";
|
|
2
2
|
import { prepareChartSnapshot } from "@k2b/ui";
|
|
3
3
|
import { barChartLayout, calendarLayout, calendarViewportHeight, hbarLayout } from "@k2b/ui/charts";
|
|
4
|
-
import { chartSvgWithUiPalette, paretoLayout, sankeyFrame } from "@k2b/ui/charts/core";
|
|
4
|
+
import { balanceFlowHeight, chartSvgWithUiPalette, paretoLayout, renderBalanceFlowHtml, sankeyFrame } from "@k2b/ui/charts/core";
|
|
5
5
|
import { Lexer, type Tokens } from "marked";
|
|
6
6
|
import type { z } from "zod";
|
|
7
7
|
import {
|
|
@@ -32,11 +32,18 @@ export type MarkdownChartOptions = {
|
|
|
32
32
|
};
|
|
33
33
|
export type ChartBlock = { line: number; input: ChartInput };
|
|
34
34
|
/** `height` is the drawing's own height in CSS pixels when it needs more than the kind's frame (app.css). */
|
|
35
|
-
type Frame = {
|
|
35
|
+
type Frame = {
|
|
36
|
+
kind: ChartInput["kind"];
|
|
37
|
+
area?: boolean;
|
|
38
|
+
title: string;
|
|
39
|
+
subtitle?: string;
|
|
40
|
+
height?: number | string;
|
|
41
|
+
balanceFlow?: Extract<ChartInput, { kind: "balanceFlow" }>;
|
|
42
|
+
};
|
|
36
43
|
type Entry = { value: string; line: number };
|
|
37
44
|
type Cell = { raw: string; result: EvalResult };
|
|
38
45
|
type SourceData = { columns: string[]; rows: Cell[][]; implicit: boolean; entries?: NamedDataEntry[]; invalid?: boolean };
|
|
39
|
-
const TYPES = new Set(["line", "area", "bar", "pie", "donut", "scatter", "stat"]);
|
|
46
|
+
const TYPES = new Set(["line", "area", "bar", "pie", "donut", "scatter", "stat", "balanceFlow"]);
|
|
40
47
|
const escape = highlight.escape;
|
|
41
48
|
const scalarCell = (value: string | number | boolean): Cell => {
|
|
42
49
|
const raw = String(value);
|
|
@@ -134,7 +141,25 @@ const parseChart = (
|
|
|
134
141
|
const key = entry[1]!;
|
|
135
142
|
section = "";
|
|
136
143
|
if (
|
|
137
|
-
![
|
|
144
|
+
![
|
|
145
|
+
"type",
|
|
146
|
+
"title",
|
|
147
|
+
"subtitle",
|
|
148
|
+
"data",
|
|
149
|
+
"columns",
|
|
150
|
+
"rows",
|
|
151
|
+
"x",
|
|
152
|
+
"y",
|
|
153
|
+
"label",
|
|
154
|
+
"value",
|
|
155
|
+
"format",
|
|
156
|
+
"unit",
|
|
157
|
+
"tooltip",
|
|
158
|
+
"periods",
|
|
159
|
+
"currency",
|
|
160
|
+
"locale",
|
|
161
|
+
"period",
|
|
162
|
+
].includes(key)
|
|
138
163
|
) {
|
|
139
164
|
add("unknown-key", key, line);
|
|
140
165
|
continue;
|
|
@@ -181,6 +206,43 @@ const parseChart = (
|
|
|
181
206
|
const title = string("title");
|
|
182
207
|
const subtitle = string("subtitle");
|
|
183
208
|
if (type && !TYPES.has(type)) add("invalid-type", "type");
|
|
209
|
+
if (type === "balanceFlow") {
|
|
210
|
+
let periods: unknown;
|
|
211
|
+
try {
|
|
212
|
+
periods = JSON.parse(top.get("periods")?.value ?? "");
|
|
213
|
+
} catch {
|
|
214
|
+
add("invalid-type", "periods");
|
|
215
|
+
}
|
|
216
|
+
for (const key of top.keys())
|
|
217
|
+
if (!["type", "title", "subtitle", "periods", "currency", "locale", "period"].includes(key)) add("unknown-key", key);
|
|
218
|
+
const parsed = options.schema.safeParse({
|
|
219
|
+
kind: "balanceFlow",
|
|
220
|
+
title,
|
|
221
|
+
subtitle,
|
|
222
|
+
periods,
|
|
223
|
+
currency: string("currency"),
|
|
224
|
+
locale: string("locale"),
|
|
225
|
+
});
|
|
226
|
+
if (!parsed.success)
|
|
227
|
+
for (const issue of parsed.error.issues) {
|
|
228
|
+
const index = issue.path[0] === "periods" && typeof issue.path[1] === "number" ? issue.path[1] : undefined;
|
|
229
|
+
const period: unknown = index !== undefined && Array.isArray(periods) ? periods[index] : undefined;
|
|
230
|
+
if (period && typeof period === "object" && "label" in period && typeof period.label === "string")
|
|
231
|
+
add("chart-balance-period", issue.path.join("."), top.get("periods")?.line, { name: period.label });
|
|
232
|
+
else add("invalid-type", String(issue.path[0] ?? "periods"));
|
|
233
|
+
}
|
|
234
|
+
if (diagnostics.length || !parsed.success) return { diagnostics };
|
|
235
|
+
if (
|
|
236
|
+
parsed.data.kind === "balanceFlow" &&
|
|
237
|
+
parsed.data.periods.reduce((sum, p) => sum + p.inflows.length + p.outflows.length + 3, 0) > options.maxSeries * options.maxValues
|
|
238
|
+
) {
|
|
239
|
+
add("too-many-items", "periods", undefined, { limit: options.maxSeries * options.maxValues });
|
|
240
|
+
return { diagnostics };
|
|
241
|
+
}
|
|
242
|
+
return { diagnostics, block: { line: directive.line, input: parsed.data } };
|
|
243
|
+
}
|
|
244
|
+
for (const key of ["periods", "currency", "locale", "period"]) if (top.has(key)) add("unknown-key", key);
|
|
245
|
+
|
|
184
246
|
let data: SourceData = { columns: [], rows: [], implicit: false };
|
|
185
247
|
if (top.has("data")) {
|
|
186
248
|
if (top.has("columns") || top.has("rows")) add("invalid-type", "data");
|
|
@@ -423,7 +485,7 @@ export const parseNotebookChartBlocks = (
|
|
|
423
485
|
export const chartBlockFrame = (body: string[], options: Pick<MarkdownChartOptions, "schema">): Frame | null => {
|
|
424
486
|
const entries = new Map<string, string>();
|
|
425
487
|
for (const line of body) {
|
|
426
|
-
const pair = /^(type|title|subtitle):\s*(.*)$/.exec(line);
|
|
488
|
+
const pair = /^(type|title|subtitle|periods|currency|locale|period):\s*(.*)$/.exec(line);
|
|
427
489
|
if (!pair) continue;
|
|
428
490
|
if (entries.has(pair[1]!)) return null;
|
|
429
491
|
entries.set(pair[1]!, pair[2]!);
|
|
@@ -439,6 +501,24 @@ export const chartBlockFrame = (body: string[], options: Pick<MarkdownChartOptio
|
|
|
439
501
|
switch (type) {
|
|
440
502
|
case "area":
|
|
441
503
|
return { kind: "line", area: true, title: header.data.title, ...(header.data.subtitle ? { subtitle: header.data.subtitle } : {}) };
|
|
504
|
+
case "balanceFlow": {
|
|
505
|
+
let periods: unknown;
|
|
506
|
+
try {
|
|
507
|
+
periods = JSON.parse(entries.get("periods") ?? "");
|
|
508
|
+
} catch {
|
|
509
|
+
return null;
|
|
510
|
+
}
|
|
511
|
+
const parsed = options.schema.safeParse({
|
|
512
|
+
kind: type,
|
|
513
|
+
title,
|
|
514
|
+
subtitle,
|
|
515
|
+
periods,
|
|
516
|
+
currency: parseScalar(entries.get("currency") ?? ""),
|
|
517
|
+
...(entries.has("locale") ? { locale: parseScalar(entries.get("locale")!) } : {}),
|
|
518
|
+
});
|
|
519
|
+
if (!parsed.success || parsed.data.kind !== "balanceFlow") return null;
|
|
520
|
+
return { kind: type, title, subtitle, balanceFlow: parsed.data };
|
|
521
|
+
}
|
|
442
522
|
case "line":
|
|
443
523
|
case "bar":
|
|
444
524
|
case "scatter":
|
|
@@ -458,6 +538,7 @@ export const chartBlockFrame = (body: string[], options: Pick<MarkdownChartOptio
|
|
|
458
538
|
*/
|
|
459
539
|
export const notebookChartHeight = (input: ChartInput, locale: string): number | string | undefined => {
|
|
460
540
|
const options = chartRenderOptions(input, locale, { legend: "outside" });
|
|
541
|
+
if (options.kind === "balanceFlow") return balanceFlowHeight(options.periods);
|
|
461
542
|
if (options.kind === "treemap") return 280;
|
|
462
543
|
if (options.kind === "pareto") return paretoLayout(options).height;
|
|
463
544
|
if (options.kind === "sankey") return sankeyFrame(options).height;
|
|
@@ -479,9 +560,18 @@ export const notebookChartHeight = (input: ChartInput, locale: string): number |
|
|
|
479
560
|
export const notebookChartFrameHtml = (
|
|
480
561
|
frame: Frame,
|
|
481
562
|
viewportHtml: string,
|
|
482
|
-
options: { legend?: boolean; input?: ChartInput; print?: boolean } = {},
|
|
563
|
+
options: { legend?: boolean; input?: ChartInput; print?: boolean; locale?: string } = {},
|
|
483
564
|
): string =>
|
|
484
|
-
|
|
565
|
+
frame.balanceFlow
|
|
566
|
+
? renderBalanceFlowHtml(
|
|
567
|
+
{ ...frame.balanceFlow, locale: frame.balanceFlow.locale ?? options.locale },
|
|
568
|
+
"loading",
|
|
569
|
+
undefined,
|
|
570
|
+
"chart",
|
|
571
|
+
false,
|
|
572
|
+
Boolean(options.print),
|
|
573
|
+
)
|
|
574
|
+
: `<figure class="notebook-book-chart"${typeof frame.height === "string" ? ' data-calendar="true" style="container-type:inline-size"' : ""} data-kind="${frame.kind === "line" && frame.area ? "area" : frame.kind}"${options.input ? ` data-chart="${escape(JSON.stringify(options.input))}"` : ""}><figcaption class="notebook-book-chart__header"><span class="notebook-book-chart__title">${escape(frame.title)}</span>${frame.subtitle ? `<span class="notebook-book-chart__subtitle">${escape(frame.subtitle)}</span>` : ""}</figcaption>${options.print ? "" : '<div class="notebook-book-chart__actions"></div>'}<div class="notebook-book-chart__viewport"${options.legend ? ' data-legend="true"' : ""}${frame.height ? ` style="--notebook-chart-height:${typeof frame.height === "number" ? `${frame.height}px` : frame.height}"` : ""}>${viewportHtml}</div></figure>`;
|
|
485
575
|
|
|
486
576
|
/** A chart's data as a note table: the PDF prints it below the figure, and the table view shows it in the frame. */
|
|
487
577
|
export const notebookChartTableHtml = (table: ChartTable, options: { label?: string; className?: string } = {}): string =>
|
|
@@ -490,6 +580,18 @@ export const notebookChartTableHtml = (table: ChartTable, options: { label?: str
|
|
|
490
580
|
/** Only the shared renderer supplies SVG. A failed drawing leaves the caller's source visible. */
|
|
491
581
|
export const renderNotebookChartFigure = (input: ChartInput, locale: string, options: { print?: boolean } = {}): string | null => {
|
|
492
582
|
try {
|
|
583
|
+
if (input.kind === "balanceFlow") {
|
|
584
|
+
const drawing = chartRenderOptions(input, locale);
|
|
585
|
+
if (drawing.kind === "balanceFlow")
|
|
586
|
+
return renderBalanceFlowHtml(
|
|
587
|
+
{ ...drawing, title: input.title, subtitle: input.subtitle },
|
|
588
|
+
"data",
|
|
589
|
+
undefined,
|
|
590
|
+
"chart",
|
|
591
|
+
false,
|
|
592
|
+
Boolean(options.print),
|
|
593
|
+
);
|
|
594
|
+
}
|
|
493
595
|
const rows = chartRows(input, locale);
|
|
494
596
|
const snapshot = prepareChartSnapshot(chartRenderOptions(input, locale, { legend: "outside" }), {
|
|
495
597
|
key: ({ datum }) => chartMarkKey(datum),
|
|
@@ -13,9 +13,15 @@
|
|
|
13
13
|
import { syntaxTree } from "@codemirror/language";
|
|
14
14
|
import { type Text as DocText, type EditorSelection, type Extension, Prec, type Range } from "@codemirror/state";
|
|
15
15
|
import { Decoration, type DecorationSet, type EditorView, ViewPlugin, type ViewUpdate, WidgetType } from "@codemirror/view";
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
holdsDecorations,
|
|
18
|
+
markdownEditorFocused,
|
|
19
|
+
pointerSelectionHeld,
|
|
20
|
+
refreshesMarkdownDecorations,
|
|
21
|
+
revealedSelection,
|
|
22
|
+
} from "./_lib/cursor-zone-field";
|
|
17
23
|
import type { MarkdownRangeOptions } from "./_lib/list-structure";
|
|
18
|
-
import { blockMathPattern, inlineMathPattern } from "./_lib/math-syntax";
|
|
24
|
+
import { blockMathPattern, hasMathMarker, inlineMathPattern } from "./_lib/math-syntax";
|
|
19
25
|
import { findLigatures, frontMatterLength, type LigatureMatch } from "./lib/ligatures";
|
|
20
26
|
|
|
21
27
|
type Span = { from: number; to: number };
|
|
@@ -57,7 +63,7 @@ const notebookSyntax = (doc: DocText, options: MarkdownRangeOptions, cache: Weak
|
|
|
57
63
|
const spans: Span[] = [];
|
|
58
64
|
const frontMatter = frontMatterLength(markdown);
|
|
59
65
|
if (frontMatter) spans.push({ from: 0, to: frontMatter });
|
|
60
|
-
if (
|
|
66
|
+
if (hasMathMarker(markdown)) {
|
|
61
67
|
for (const pattern of [blockMathPattern(), inlineMathPattern()]) {
|
|
62
68
|
for (const match of markdown.matchAll(pattern)) spans.push({ from: match.index, to: match.index + match[0].length });
|
|
63
69
|
}
|
|
@@ -99,10 +105,10 @@ const findVisibleLigatures = (view: EditorView, options: MarkdownRangeOptions, c
|
|
|
99
105
|
};
|
|
100
106
|
|
|
101
107
|
/** Same rule as hidden Markdown marks: a selection that touches the sequence reveals it. */
|
|
102
|
-
const touched = (selection: EditorSelection, match: LigatureMatch): boolean =>
|
|
103
|
-
selection
|
|
108
|
+
const touched = (selection: EditorSelection | null, match: LigatureMatch): boolean =>
|
|
109
|
+
selection?.ranges.some((range) => range.from <= match.to && range.to >= match.from) ?? false;
|
|
104
110
|
|
|
105
|
-
const touchedKey = (selection: EditorSelection, matches: readonly LigatureMatch[]): string =>
|
|
111
|
+
const touchedKey = (selection: EditorSelection | null, matches: readonly LigatureMatch[]): string =>
|
|
106
112
|
matches.flatMap((match, index) => (touched(selection, match) ? [index] : [])).join(",");
|
|
107
113
|
|
|
108
114
|
class LigatureWidget extends WidgetType {
|
|
@@ -137,7 +143,7 @@ const ligatureDecoration = (symbol: string): Decoration => {
|
|
|
137
143
|
return decoration;
|
|
138
144
|
};
|
|
139
145
|
|
|
140
|
-
const decorate = (selection: EditorSelection, matches: readonly LigatureMatch[]): DecorationSet => {
|
|
146
|
+
const decorate = (selection: EditorSelection | null, matches: readonly LigatureMatch[]): DecorationSet => {
|
|
141
147
|
const ranges: Range<Decoration>[] = [];
|
|
142
148
|
for (const match of matches) {
|
|
143
149
|
if (!touched(selection, match)) ranges.push(ligatureDecoration(match.symbol).range(match.from, match.to));
|
|
@@ -148,46 +154,49 @@ const decorate = (selection: EditorSelection, matches: readonly LigatureMatch[])
|
|
|
148
154
|
/** Highest precedence: only marks of lower precedence, such as syntax highlighting, wrap a replaced widget, so the symbol keeps heading and emphasis styling. */
|
|
149
155
|
export const ligaturesExtension = (options: MarkdownRangeOptions = {}): Extension => {
|
|
150
156
|
const cache = new WeakMap<DocText, Span[]>();
|
|
151
|
-
return
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
update(update: ViewUpdate) {
|
|
167
|
-
const refresh = update.transactions.some((tr) => tr.effects.some((effect) => effect.is(refreshMarkdownDecorationsEffect)));
|
|
168
|
-
// While the button is down only pointer selections wait for the release; scrolling and edits keep the press-time selection.
|
|
169
|
-
const held =
|
|
170
|
-
update.state.field(pointerSelectionHeld, false) === true &&
|
|
171
|
-
!refresh &&
|
|
172
|
-
update.transactions.every((tr) => !tr.selection || holdsDecorations(tr));
|
|
173
|
-
if (held) {
|
|
174
|
-
if (update.docChanged) this.selection = this.selection.map(update.changes);
|
|
175
|
-
} else {
|
|
176
|
-
this.selection = update.state.selection;
|
|
157
|
+
return [
|
|
158
|
+
markdownEditorFocused,
|
|
159
|
+
Prec.highest(
|
|
160
|
+
ViewPlugin.fromClass(
|
|
161
|
+
class {
|
|
162
|
+
matches: LigatureMatch[];
|
|
163
|
+
selection: EditorSelection | null;
|
|
164
|
+
key: string;
|
|
165
|
+
decorations: DecorationSet;
|
|
166
|
+
|
|
167
|
+
constructor(view: EditorView) {
|
|
168
|
+
this.matches = findVisibleLigatures(view, options, cache);
|
|
169
|
+
this.selection = revealedSelection(view.state);
|
|
170
|
+
this.key = touchedKey(this.selection, this.matches);
|
|
171
|
+
this.decorations = decorate(this.selection, this.matches);
|
|
177
172
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
173
|
+
|
|
174
|
+
update(update: ViewUpdate) {
|
|
175
|
+
const refresh = update.transactions.some(refreshesMarkdownDecorations);
|
|
176
|
+
// While the button is down only pointer selections wait for the release; scrolling and edits keep the press-time selection.
|
|
177
|
+
const held =
|
|
178
|
+
update.state.field(pointerSelectionHeld, false) === true &&
|
|
179
|
+
!refresh &&
|
|
180
|
+
update.transactions.every((tr) => !tr.selection || holdsDecorations(tr));
|
|
181
|
+
if (held) {
|
|
182
|
+
if (update.docChanged) this.selection = this.selection?.map(update.changes) ?? null;
|
|
183
|
+
} else {
|
|
184
|
+
this.selection = revealedSelection(update.state);
|
|
185
|
+
}
|
|
186
|
+
const rescan =
|
|
187
|
+
update.docChanged || update.viewportChanged || syntaxTree(update.startState) !== syntaxTree(update.state) || refresh;
|
|
188
|
+
if (rescan) this.matches = findVisibleLigatures(update.view, options, cache);
|
|
189
|
+
else if (!update.selectionSet || update.transactions.every(holdsDecorations)) return;
|
|
190
|
+
const key = touchedKey(this.selection, this.matches);
|
|
191
|
+
if (!rescan && key === this.key) return;
|
|
192
|
+
this.key = key;
|
|
193
|
+
this.decorations = decorate(this.selection, this.matches);
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
{ decorations: (plugin) => plugin.decorations },
|
|
197
|
+
),
|
|
189
198
|
),
|
|
190
|
-
|
|
199
|
+
];
|
|
191
200
|
};
|
|
192
201
|
|
|
193
202
|
const LIGATURE_SKIP = "code, pre, a, time, .katex, .md-formula-ok, .md-formula-error";
|
package/src/markdown/links.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
markdownReferenceType,
|
|
10
10
|
markdownReferenceTypeLabel,
|
|
11
11
|
} from "@k2b/ui";
|
|
12
|
-
import { type CursorZoneState, cursorZoneStateField, selectionIntersectsRange } from "./_lib/cursor-zone-field";
|
|
12
|
+
import { type CursorZoneState, cursorZoneStateField, revealedSelection, selectionIntersectsRange } from "./_lib/cursor-zone-field";
|
|
13
13
|
import { makeOpener } from "./_lib/link-opener";
|
|
14
14
|
import { parseNoteLink } from "./lib/heading-anchors";
|
|
15
15
|
import { extractAttachmentId, isSafeMarkdownUrl, type MarkdownUrlOptions } from "./urls";
|
|
@@ -122,7 +122,7 @@ const parseLinkSyntax = (text: string, options: MarkdownUrlOptions): LinkData |
|
|
|
122
122
|
const findLinks = (state: EditorState, options: MarkdownUrlOptions): CursorZoneState => {
|
|
123
123
|
const decorations: Range<Decoration>[] = [];
|
|
124
124
|
const ranges: { from: number; to: number }[] = [];
|
|
125
|
-
const cursor = state
|
|
125
|
+
const cursor = revealedSelection(state)?.main;
|
|
126
126
|
|
|
127
127
|
syntaxTree(state).iterate({
|
|
128
128
|
enter: ({ type, from, to }) => {
|