@k2b/editor 0.9.0 → 0.11.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 +3 -3
- package/src/BubbleMenu.tsx +11 -4
- package/src/Toolbar.tsx +25 -12
- package/src/chart-locale.ts +1 -1
- package/src/chart-schema.ts +178 -27
- package/src/charts.ts +225 -83
- 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 +16 -9
- 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 +129 -16
- 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 +10 -3
- package/src/markdown/tables.ts +15 -7
- package/src/slash-menu.ts +8 -0
- package/src/styles.css +8 -0
|
@@ -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);
|
|
@@ -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(
|
|
@@ -389,7 +395,7 @@ export function createBlockPreviews(options: BlockPreviewOptions): { extension:
|
|
|
389
395
|
}),
|
|
390
396
|
);
|
|
391
397
|
// A placeholder reserves the height its drawing will take; the note's charts are read once, and only for one.
|
|
392
|
-
let heights: Map<number, number | undefined> | undefined;
|
|
398
|
+
let heights: Map<number, number | string | undefined> | undefined;
|
|
393
399
|
const heightAt = (line: number) => {
|
|
394
400
|
heights ??= new Map(
|
|
395
401
|
parseNotebookChartBlocks(document, options.charts).blocks.map((entry) => [
|
|
@@ -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(
|