@mdbase-dev/ui 0.1.0-beta.218 → 0.1.0-beta.224

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/css/controls.css CHANGED
@@ -33,6 +33,10 @@
33
33
  position: fixed;
34
34
  inset: auto;
35
35
  margin: 0;
36
+ /* Measured before placement at its static position beside the trigger;
37
+ without an intrinsic width it shrinks near the window edge and breaks
38
+ its label mid-word. */
39
+ width: max-content;
36
40
  display: flex;
37
41
  align-items: center;
38
42
  gap: 12px;
package/css/feedback.css CHANGED
@@ -4,15 +4,16 @@
4
4
  .mdbase-feedback-bug { width: 22px; height: 22px; flex-shrink: 0; }
5
5
  @keyframes mdbase-feedback-wiggle { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-14deg); } 75% { transform: rotate(14deg); } }
6
6
  @media (prefers-reduced-motion: no-preference) { .mdbase-feedback-bug.is-wiggling { animation: mdbase-feedback-wiggle 280ms ease-in-out 2; } }
7
- .mdbase-feedback-dialog { position: fixed; inset: 0 0 0 auto; margin: 0; padding: 0; width: 440px; max-width: 100vw; height: 100dvh; max-height: 100dvh; border: 0; border-left: 1px solid var(--color-border-strong); background: var(--color-canvas); color: var(--color-text); font: var(--font-size-ui)/1.5 var(--sans); overflow: hidden; }
7
+ /* Both classes: the shared .mdbase-dialog rules may load after this file. */
8
+ .mdbase-dialog.mdbase-feedback-dialog { position: fixed; inset: 0 0 0 auto; margin: 0; padding: 0; width: 440px; max-width: 100vw; height: 100dvh; max-height: 100dvh; border: 0; border-left: 1px solid var(--color-border-strong); border-radius: 0; background: var(--color-canvas); color: var(--color-text); font: var(--font-size-ui)/1.5 var(--sans); overflow: hidden; }
8
9
  .mdbase-feedback-dialog[open] { display: flex; flex-direction: column; }
9
10
  .mdbase-feedback-dialog:not([open]) { display: none; }
10
11
  .mdbase-feedback-dialog::backdrop { background: var(--color-scrim); }
11
12
  .mdbase-feedback-dialog *, .mdbase-feedback-markup * { box-sizing: border-box; }
12
13
  .mdbase-feedback-dialog :is(button,input,textarea), .mdbase-feedback-markup button { font: inherit; }
13
14
  .mdbase-feedback-dialog :focus-visible, .mdbase-feedback-markup :focus-visible, .mdbase-feedback-trigger:focus-visible { outline: var(--focus-ring); outline-offset: var(--focus-offset); }
14
- .mdbase-feedback-dialog button, .mdbase-feedback-markup .mdbase-feedback-actions button, .mdbase-feedback-tools button { border: 1px solid var(--color-border-strong); border-radius: var(--radius-sm); padding: 7px 11px; background: var(--color-canvas); color: var(--color-text); cursor: pointer; }
15
- .mdbase-feedback-dialog button:hover, .mdbase-feedback-markup button:hover { background: var(--color-selected); }
15
+ .mdbase-feedback-dialog button:not(.mdbase-icon-button), .mdbase-feedback-markup .mdbase-feedback-actions button, .mdbase-feedback-tools button { border: 1px solid var(--color-border-strong); border-radius: var(--radius-sm); padding: 7px 11px; background: var(--color-canvas); color: var(--color-text); cursor: pointer; }
16
+ .mdbase-feedback-dialog button:not(.mdbase-icon-button):hover, .mdbase-feedback-markup button:not(.mdbase-icon-button):hover { background: var(--color-selected); }
16
17
  .mdbase-feedback-dialog button:disabled, .mdbase-feedback-markup button:disabled { opacity: .5; cursor: default; }
17
18
  .mdbase-feedback-dialog form { display: flex; flex-direction: column; flex: 1; overflow: hidden; margin: 0; }
18
19
  .mdbase-feedback-body { flex: 1; overflow-y: auto; padding: 23px 27px; }
@@ -48,7 +49,7 @@
48
49
  .mdbase-feedback-success { padding: 40px 27px; }
49
50
  .mdbase-feedback-success h3 { font-size: var(--font-size-heading); line-height: 1.3; margin: 0 0 17px; }
50
51
  .mdbase-feedback-success .mdbase-feedback-primary { margin-top: 20px; }
51
- .mdbase-feedback-markup { width: min(1100px,calc(100vw - 32px)); max-width: none; max-height: calc(100dvh - 32px); padding: 0; }
52
+ .mdbase-dialog.mdbase-feedback-markup { width: min(1100px,calc(100vw - 32px)); max-width: none; max-height: calc(100dvh - 32px); padding: 0; }
52
53
  .mdbase-feedback-markup .mdbase-dialog-body { max-height: calc(100dvh - 34px); display: flex; flex-direction: column; overflow: hidden; padding: 20px; }
53
54
  .mdbase-feedback-tools { display: flex; gap: 7px; flex-wrap: wrap; margin: 10px 0 15px; }
54
55
  .mdbase-feedback-tools button[aria-pressed=true] { background: var(--color-selected); border-color: var(--color-accent); }
@@ -58,4 +59,5 @@
58
59
  @media (max-width: 600px) { .mdbase-feedback-body { padding: 20px 23px; } .mdbase-feedback-actions { padding: 16px 23px; } .mdbase-feedback-tools { gap: 4px; } .mdbase-feedback-tools button { font-size: var(--font-size-caption); padding: 6px 8px; } }
59
60
 
60
61
  .mdbase-feedback-dialog.is-capturing { --motion-exit: 0ms; }
61
- .mdbase-feedback-dialog > .mdbase-dialog-body { padding: 16px; gap: 16px; }
62
+ /* A flex column that fills the panel, so the form body scrolls and its actions stay in view. */
63
+ .mdbase-feedback-dialog > .mdbase-dialog-body { display: flex; flex-direction: column; flex: 1; min-height: 0; padding: 16px; gap: 16px; }
@@ -1,3 +1,40 @@
1
1
  import { StateField } from "@codemirror/state";
2
2
  export declare function calloutHeader(text: string): RegExpExecArray | null;
3
- export declare function blockPreview(): (StateField<ReadonlyMap<number, boolean>> | import("@codemirror/state").Extension)[];
3
+ /** Code blocks, callouts and footnotes found in a note, kept until its text or parse changes. */
4
+ interface CodeBlock {
5
+ from: number;
6
+ to: number;
7
+ fenced: boolean;
8
+ language: string | undefined;
9
+ closed: boolean;
10
+ code: string;
11
+ diagram: boolean;
12
+ /** Start, end and indentation of each line, first to last. */
13
+ lines: Array<{
14
+ from: number;
15
+ to: number;
16
+ indent: number;
17
+ }>;
18
+ }
19
+ interface Callout {
20
+ from: number;
21
+ to: number;
22
+ type: string;
23
+ title: string | undefined;
24
+ fold: string | undefined;
25
+ lines: number[];
26
+ lastTo: number;
27
+ }
28
+ interface Structure {
29
+ codes: CodeBlock[];
30
+ callouts: Callout[];
31
+ definitions: Map<string, string>;
32
+ definitionLines: number[];
33
+ references: Array<{
34
+ from: number;
35
+ to: number;
36
+ key: string;
37
+ }>;
38
+ }
39
+ export declare function blockPreview(): (StateField<ReadonlyMap<number, boolean>> | StateField<Structure> | import("@codemirror/state").Extension)[];
40
+ export {};
@@ -14,6 +14,7 @@ const toggleFold = StateEffect.define({
14
14
  const folds = StateField.define({
15
15
  create: () => new Map(),
16
16
  update(value, tr) {
17
+ if (!tr.docChanged && !tr.effects.some(effect => effect.is(toggleFold))) return value;
17
18
  const next = new Map();
18
19
  for (const [pos, folded] of value) next.set(tr.changes.mapPos(pos), folded);
19
20
  for (const effect of tr.effects) if (effect.is(toggleFold)) next.set(effect.value.from, effect.value.folded);
@@ -245,146 +246,207 @@ class CalloutHeader extends WidgetType {
245
246
  return true;
246
247
  }
247
248
  }
248
- export function blockPreview() {
249
- return [folds, EditorView.decorations.compute(["doc", revealField, folds], build), keymap.of([{
250
- key: "Tab",
251
- run: view => inCode(view.state) && indentMore(view)
252
- }, {
253
- key: "Shift-Tab",
254
- run: view => inCode(view.state) && indentLess(view)
255
- }])];
256
- }
257
- function inCode(state) {
258
- const positions = state.selection.ranges.map(({
259
- head
260
- }) => head);
261
- const matched = positions.map(() => false);
262
- syntaxTree(state).iterate({
263
- enter(node) {
264
- if (node.name !== "FencedCode" && node.name !== "CodeBlock") return;
265
- positions.forEach((pos, index) => {
266
- if (pos > node.from && pos < node.to) matched[index] = true;
267
- });
268
- return false;
269
- }
270
- });
271
- return matched.every(Boolean);
272
- }
273
- function build(state) {
274
- const ranges = [];
275
- const skip = [];
276
- const foldState = state.field(folds);
277
- syntaxTree(state).iterate({
249
+ /** Blocks that hold only inline content, so they cannot contain code, quotes or callouts. */
250
+ const inlineBlocks = new Set(["Paragraph", "ATXHeading1", "ATXHeading2", "ATXHeading3", "ATXHeading4", "ATXHeading5", "ATXHeading6", "SetextHeading1", "SetextHeading2", "Table", "HTMLBlock", "LinkReference", "CommentBlock", "ProcessingInstructionBlock", "HorizontalRule", "Task"]);
251
+ const unreferenced = ["InlineCode", "FencedCode", "CodeBlock", "HTMLBlock", "InlineMath", "MathBlock"];
252
+ function analyze(state) {
253
+ const codes = [],
254
+ callouts = [];
255
+ const tree = syntaxTree(state);
256
+ tree.iterate({
278
257
  enter(node) {
258
+ if (inlineBlocks.has(node.name)) return false;
279
259
  if (node.name === "FencedCode" || node.name === "CodeBlock") {
280
260
  const first = state.doc.lineAt(node.from),
281
261
  last = state.doc.lineAt(node.to);
282
- skip.push({
283
- from: node.from,
284
- to: node.to
285
- });
286
262
  const fenced = node.name === "FencedCode";
287
263
  const opening = fenced ? /^\s*(`{3,}|~{3,})\s*([^\s`]*)/.exec(first.text) : null;
288
- const closed = opening && last.number > first.number && new RegExp(`^\\s*${opening[1][0]}{${opening[1].length},}\\s*$`).test(last.text);
264
+ const closed = !!opening && last.number > first.number && new RegExp(`^\\s*${opening[1][0]}{${opening[1].length},}\\s*$`).test(last.text);
289
265
  const codeFrom = fenced ? Math.min(first.to + 1, state.doc.length) : first.from;
290
266
  const codeTo = closed ? Math.max(codeFrom, last.from - 1) : last.to;
291
267
  const code = state.sliceDoc(codeFrom, codeTo);
292
268
  // Quote and list markers would be part of a nested diagram's source, so only top-level fences render.
293
- if (closed && isDiagramLanguage(opening[2]) && code.trim() && node.node.parent?.name === "Document" && !isRevealed(state, node.from, node.to)) {
294
- ranges.push(Decoration.replace({
295
- widget: new DiagramWidget(code),
296
- block: true
297
- }).range(first.from, last.to));
298
- return false;
299
- }
269
+ const diagram = closed && isDiagramLanguage(opening[2]) && !!code.trim() && node.node.parent?.name === "Document";
270
+ const lines = [];
300
271
  for (let n = first.number; n <= last.number; n++) {
301
272
  const line = state.doc.line(n);
302
- const edge = `${n === first.number ? "cm-lp-code-first" : ""} ${n === last.number ? "cm-lp-code-last" : ""}`;
303
- ranges.push(codeLine(edge).range(line.from));
304
- if (!fenced && !isRevealed(state, line.from, line.to)) {
305
- const indent = /^(?: {4}|\t)/.exec(line.text);
306
- if (indent) ranges.push(hide.range(line.from, line.from + indent[0].length));
307
- }
308
- }
309
- if (fenced && !isRevealed(state, first.from, first.to)) {
310
- ranges.push(Decoration.replace({
311
- widget: new CodeHeader(opening?.[2] ?? "", code)
312
- }).range(first.from, first.to));
313
- } else if (!fenced) {
314
- ranges.push(Decoration.widget({
315
- widget: new CodeHeader("", code.replace(/^(?: {4}|\t)/gm, "")),
316
- side: -1
317
- }).range(first.from));
318
- }
319
- if (closed && !isRevealed(state, last.from, last.to)) {
320
- ranges.push(Decoration.line({
321
- class: "cm-lp-code-fence-end"
322
- }).range(last.from));
323
- ranges.push(hide.range(last.from, last.to));
273
+ lines.push({
274
+ from: line.from,
275
+ to: line.to,
276
+ indent: fenced ? 0 : /^(?: {4}|\t)/.exec(line.text)?.[0].length ?? 0
277
+ });
324
278
  }
279
+ codes.push({
280
+ from: node.from,
281
+ to: node.to,
282
+ fenced,
283
+ language: opening?.[2],
284
+ closed,
285
+ code,
286
+ diagram,
287
+ lines
288
+ });
325
289
  return false;
326
290
  }
327
- if (node.name === "Blockquote") {
328
- if (node.node.parent?.name === "Blockquote") return;
291
+ if (node.name === "Blockquote" && node.node.parent?.name !== "Blockquote") {
329
292
  const first = state.doc.lineAt(node.from),
330
293
  last = state.doc.lineAt(node.to);
331
294
  const match = calloutHeader(first.text);
332
295
  if (!match) return;
296
+ const lines = [];
297
+ for (let n = first.number; n <= last.number; n++) lines.push(state.doc.line(n).from);
333
298
  const type = match[1].toLowerCase() in types ? match[1].toLowerCase() : "note";
334
- const title = match[3] || typeTitle(type);
335
- const folded = match[2] ? foldState.get(first.from) ?? match[2] === "-" : false;
336
- for (let n = first.number; n <= last.number; n++) {
337
- const line = state.doc.line(n);
338
- ranges.push(Decoration.line({
339
- class: `cm-lp-callout cm-lp-callout-${type}${n === first.number ? " cm-lp-callout-first" : ""}${n === last.number || folded && n === first.number ? " cm-lp-callout-last" : ""}`
340
- }).range(line.from));
341
- }
342
- if (!isRevealed(state, first.from, first.to)) ranges.push(Decoration.replace({
343
- widget: new CalloutHeader(type, title, first.from, !!match[2], folded)
344
- }).range(first.from, first.to));
345
- if (folded && first.number < last.number && !isRevealed(state, first.to + 1, last.to)) {
346
- ranges.push(Decoration.replace({
347
- block: true
348
- }).range(first.to + 1, last.to));
349
- }
299
+ callouts.push({
300
+ from: first.from,
301
+ to: first.to,
302
+ type,
303
+ title: match[3] || undefined,
304
+ fold: match[2],
305
+ lines,
306
+ lastTo: last.to
307
+ });
350
308
  }
351
309
  }
352
310
  });
353
- const definitions = new Map();
354
- for (let n = 1; n <= state.doc.lines; n++) {
355
- const line = state.doc.line(n);
356
- if (skip.some(part => line.from >= part.from && line.from <= part.to)) continue;
357
- const def = /^ {0,3}\[\^([^\]\s]+)\]:\s*(.*)$/.exec(line.text);
358
- if (def) {
359
- definitions.set(def[1].toLowerCase(), def[2]);
360
- ranges.push(Decoration.line({
361
- class: "cm-lp-footnote-definition"
362
- }).range(line.from));
311
+ // Footnotes are found line by line outside code; only lines with "[^" can hold one.
312
+ const definitions = new Map(),
313
+ definitionLines = [];
314
+ const references = [];
315
+ let from = 0,
316
+ code = 0;
317
+ for (const iterator = state.doc.iterLines(); !iterator.next().done; from += iterator.value.length + 1) {
318
+ const text = iterator.value;
319
+ if (!text.includes("[^")) continue;
320
+ while (code < codes.length && codes[code].to < from) code++;
321
+ if (code < codes.length && from >= codes[code].from) continue;
322
+ const definition = /^ {0,3}\[\^([^\]\s]+)\]:\s*(.*)$/.exec(text);
323
+ if (definition) {
324
+ definitions.set(definition[1].toLowerCase(), definition[2]);
325
+ definitionLines.push(from);
363
326
  continue;
364
327
  }
365
- }
366
- for (let n = 1; n <= state.doc.lines; n++) {
367
- const line = state.doc.line(n);
368
- if (skip.some(part => line.from >= part.from && line.from <= part.to) || /^ {0,3}\[\^[^\]]+\]:/.test(line.text)) continue;
369
- for (const match of line.text.matchAll(/(?<![\\!])\[\^([^\]\s]+)\]/g)) {
370
- const from = line.from + match.index,
371
- to = from + match[0].length;
372
- const node = syntaxTree(state).resolveInner(from, 1);
373
- if (["InlineCode", "FencedCode", "CodeBlock", "HTMLBlock", "InlineMath", "MathBlock"].some(name => {
328
+ if (/^ {0,3}\[\^[^\]]+\]:/.test(text)) continue;
329
+ for (const match of text.matchAll(/(?<![\\!])\[\^([^\]\s]+)\]/g)) {
330
+ const start = from + match.index;
331
+ const node = tree.resolveInner(start, 1);
332
+ if (unreferenced.some(name => {
374
333
  for (let p = node; p; p = p.parent) if (p.name === name) return true;
375
334
  return false;
376
335
  })) continue;
377
- if (!isRevealed(state, from, to)) {
378
- const text = definitions.get(match[1].toLowerCase());
379
- ranges.push((text ? Decoration.mark({
380
- class: "cm-lp-footnote-ref",
381
- attributes: {
382
- title: text
383
- }
384
- }) : footnote).range(from + 2, to - 1));
385
- ranges.push(hide.range(from, from + 2), hide.range(to - 1, to));
386
- }
336
+ references.push({
337
+ from: start,
338
+ to: start + match[0].length,
339
+ key: match[1].toLowerCase()
340
+ });
341
+ }
342
+ }
343
+ return {
344
+ codes,
345
+ callouts,
346
+ definitions,
347
+ definitionLines,
348
+ references
349
+ };
350
+ }
351
+ const structure = StateField.define({
352
+ create: analyze,
353
+ update: (value, transaction) => transaction.docChanged || syntaxTree(transaction.state) !== syntaxTree(transaction.startState) ? analyze(transaction.state) : value
354
+ });
355
+ export function blockPreview() {
356
+ return [folds, structure, EditorView.decorations.compute([structure, revealField, folds], build), keymap.of([{
357
+ key: "Tab",
358
+ run: view => inCode(view.state) && indentMore(view)
359
+ }, {
360
+ key: "Shift-Tab",
361
+ run: view => inCode(view.state) && indentLess(view)
362
+ }])];
363
+ }
364
+ function inCode(state) {
365
+ const {
366
+ codes
367
+ } = state.field(structure);
368
+ return state.selection.ranges.every(({
369
+ head
370
+ }) => codes.some(block => head > block.from && head < block.to));
371
+ }
372
+ function build(state) {
373
+ const ranges = [];
374
+ const {
375
+ codes,
376
+ callouts,
377
+ definitions,
378
+ definitionLines,
379
+ references
380
+ } = state.field(structure);
381
+ const foldState = state.field(folds);
382
+ for (const block of codes) {
383
+ const first = block.lines[0],
384
+ last = block.lines[block.lines.length - 1];
385
+ if (block.diagram && !isRevealed(state, block.from, block.to)) {
386
+ ranges.push(Decoration.replace({
387
+ widget: new DiagramWidget(block.code),
388
+ block: true
389
+ }).range(first.from, last.to));
390
+ continue;
391
+ }
392
+ block.lines.forEach((line, index) => {
393
+ const edge = `${index === 0 ? "cm-lp-code-first" : ""} ${index === block.lines.length - 1 ? "cm-lp-code-last" : ""}`;
394
+ ranges.push(codeLine(edge).range(line.from));
395
+ if (!block.fenced && line.indent && !isRevealed(state, line.from, line.to)) ranges.push(hide.range(line.from, line.from + line.indent));
396
+ });
397
+ if (block.fenced && !isRevealed(state, first.from, first.to)) {
398
+ ranges.push(Decoration.replace({
399
+ widget: new CodeHeader(block.language ?? "", block.code)
400
+ }).range(first.from, first.to));
401
+ } else if (!block.fenced) {
402
+ ranges.push(Decoration.widget({
403
+ widget: new CodeHeader("", block.code.replace(/^(?: {4}|\t)/gm, "")),
404
+ side: -1
405
+ }).range(first.from));
406
+ }
407
+ if (block.closed && !isRevealed(state, last.from, last.to)) {
408
+ ranges.push(Decoration.line({
409
+ class: "cm-lp-code-fence-end"
410
+ }).range(last.from));
411
+ ranges.push(hide.range(last.from, last.to));
412
+ }
413
+ }
414
+ for (const callout of callouts) {
415
+ const folded = callout.fold ? foldState.get(callout.from) ?? callout.fold === "-" : false;
416
+ callout.lines.forEach((from, index) => {
417
+ const last = index === callout.lines.length - 1;
418
+ ranges.push(Decoration.line({
419
+ class: `cm-lp-callout cm-lp-callout-${callout.type}${index === 0 ? " cm-lp-callout-first" : ""}${last || folded && index === 0 ? " cm-lp-callout-last" : ""}`
420
+ }).range(from));
421
+ });
422
+ if (!isRevealed(state, callout.from, callout.to)) {
423
+ ranges.push(Decoration.replace({
424
+ widget: new CalloutHeader(callout.type, callout.title ?? typeTitle(callout.type), callout.from, !!callout.fold, folded)
425
+ }).range(callout.from, callout.to));
426
+ }
427
+ if (folded && callout.lines.length > 1 && !isRevealed(state, callout.to + 1, callout.lastTo)) {
428
+ ranges.push(Decoration.replace({
429
+ block: true
430
+ }).range(callout.to + 1, callout.lastTo));
387
431
  }
388
432
  }
433
+ for (const from of definitionLines) ranges.push(Decoration.line({
434
+ class: "cm-lp-footnote-definition"
435
+ }).range(from));
436
+ for (const {
437
+ from,
438
+ to,
439
+ key
440
+ } of references) {
441
+ if (isRevealed(state, from, to)) continue;
442
+ const text = definitions.get(key);
443
+ ranges.push((text ? Decoration.mark({
444
+ class: "cm-lp-footnote-ref",
445
+ attributes: {
446
+ title: text
447
+ }
448
+ }) : footnote).range(from + 2, to - 1));
449
+ ranges.push(hide.range(from, from + 2), hide.range(to - 1, to));
450
+ }
389
451
  return Decoration.set(ranges, true);
390
452
  }
@@ -1,3 +1,4 @@
1
+ import { StateField } from "@codemirror/state";
1
2
  import { EditorView, WidgetType } from "@codemirror/view";
2
3
  import type { MarkdownConfig } from "@lezer/markdown";
3
4
  import "../i18n.js";
@@ -29,6 +30,16 @@ export declare class MathWidget extends WidgetType {
29
30
  toDOM(view: EditorView): HTMLDivElement | HTMLSpanElement;
30
31
  ignoreEvent(): boolean;
31
32
  }
33
+ interface Formula {
34
+ from: number;
35
+ to: number;
36
+ source: string;
37
+ first: number;
38
+ last: number;
39
+ lastTo: number;
40
+ topLevel: boolean;
41
+ display: boolean;
42
+ }
32
43
  /** Renders `markdownMath` formulas where the selection is not, and shows a rendered preview under a revealed display block. */
33
- export declare function mathPreview(): import("@codemirror/state").Extension;
44
+ export declare function mathPreview(): (StateField<Formula[]> | import("@codemirror/state").Extension)[];
34
45
  export {};
@@ -1,4 +1,5 @@
1
1
  import { syntaxTree } from "@codemirror/language";
2
+ import { StateField } from "@codemirror/state";
2
3
  import { Decoration, EditorView, WidgetType } from "@codemirror/view";
3
4
  import { i18n as _i18n } from "@lingui/core";
4
5
  import "../i18n.js";
@@ -160,42 +161,60 @@ function render(module, element, source, display) {
160
161
  const sourceMark = Decoration.mark({
161
162
  class: "cm-lp-math-source"
162
163
  });
163
- /** Renders `markdownMath` formulas where the selection is not, and shows a rendered preview under a revealed display block. */
164
- export function mathPreview() {
165
- return EditorView.decorations.compute(["doc", revealField], build);
166
- }
167
- function build(state) {
168
- const ranges = [];
164
+ /** The note's formulas, kept until its text or parse changes, so a caret move only checks what it reveals. */
165
+ function formulas(state) {
166
+ const found = [];
169
167
  syntaxTree(state).iterate({
170
168
  enter(node) {
171
169
  if (node.name !== "InlineMath" && node.name !== "MathBlock") return;
172
170
  const marks = node.node.getChildren("MathMark");
173
- const source = marks.length === 2 ? state.sliceDoc(marks[0].to, marks[1].from).trim() : "";
174
- const revealed = isRevealed(state, node.from, node.to);
175
171
  const first = state.doc.lineAt(node.from),
176
172
  last = state.doc.lineAt(node.to);
177
- const topLevel = node.name === "MathBlock" && node.node.parent?.name === "Document";
178
- // Quote and list markers would be part of a nested multi-line formula, so it stays source.
179
- if (!source || revealed || !topLevel && first.number !== last.number) {
180
- ranges.push(sourceMark.range(node.from, node.to));
181
- if (source && revealed && topLevel) ranges.push(Decoration.widget({
182
- widget: new MathWidget(source, "preview"),
183
- block: true,
184
- side: 1
185
- }).range(last.to));
186
- } else if (topLevel) {
187
- ranges.push(Decoration.replace({
188
- widget: new MathWidget(source, "block"),
189
- block: true
190
- }).range(first.from, last.to));
191
- } else {
192
- const display = node.name === "MathBlock" || marks[0].to - marks[0].from === 2;
193
- ranges.push(Decoration.replace({
194
- widget: new MathWidget(source, display ? "display" : "inline")
195
- }).range(node.from, node.to));
196
- }
173
+ found.push({
174
+ from: node.from,
175
+ to: node.to,
176
+ source: marks.length === 2 ? state.sliceDoc(marks[0].to, marks[1].from).trim() : "",
177
+ first: first.from,
178
+ last: last.number - first.number,
179
+ lastTo: last.to,
180
+ topLevel: node.name === "MathBlock" && node.node.parent?.name === "Document",
181
+ display: node.name === "MathBlock" || marks[0].to - marks[0].from === 2
182
+ });
197
183
  return false;
198
184
  }
199
185
  });
186
+ return found;
187
+ }
188
+ const formulaField = StateField.define({
189
+ create: formulas,
190
+ update: (value, transaction) => transaction.docChanged || syntaxTree(transaction.state) !== syntaxTree(transaction.startState) ? formulas(transaction.state) : value
191
+ });
192
+ /** Renders `markdownMath` formulas where the selection is not, and shows a rendered preview under a revealed display block. */
193
+ export function mathPreview() {
194
+ return [formulaField, EditorView.decorations.compute([formulaField, revealField], build)];
195
+ }
196
+ function build(state) {
197
+ const ranges = [];
198
+ for (const formula of state.field(formulaField)) {
199
+ const revealed = isRevealed(state, formula.from, formula.to);
200
+ // Quote and list markers would be part of a nested multi-line formula, so it stays source.
201
+ if (!formula.source || revealed || !formula.topLevel && formula.last > 0) {
202
+ ranges.push(sourceMark.range(formula.from, formula.to));
203
+ if (formula.source && revealed && formula.topLevel) ranges.push(Decoration.widget({
204
+ widget: new MathWidget(formula.source, "preview"),
205
+ block: true,
206
+ side: 1
207
+ }).range(formula.lastTo));
208
+ } else if (formula.topLevel) {
209
+ ranges.push(Decoration.replace({
210
+ widget: new MathWidget(formula.source, "block"),
211
+ block: true
212
+ }).range(formula.first, formula.lastTo));
213
+ } else {
214
+ ranges.push(Decoration.replace({
215
+ widget: new MathWidget(formula.source, formula.display ? "display" : "inline")
216
+ }).range(formula.from, formula.to));
217
+ }
218
+ }
200
219
  return Decoration.set(ranges, true);
201
220
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdbase-dev/ui",
3
- "version": "0.1.0-beta.218",
3
+ "version": "0.1.0-beta.224",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -242,6 +242,6 @@
242
242
  "typecheck": "node scripts/compile-catalogues.mjs && tsc -p tsconfig.json --noEmit"
243
243
  },
244
244
  "mdbaseRelease": {
245
- "commit": "17b79aec9b4ce01bcf7d07220c28e48ec16e6f14"
245
+ "commit": "be6d241b812f3dec252ce49f05b79b93cb7c21e3"
246
246
  }
247
247
  }