@mdbase-dev/ui 0.1.0-beta.230 → 0.1.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/css/controls.css CHANGED
@@ -735,11 +735,12 @@ textarea.mdbase-field {
735
735
  }
736
736
 
737
737
  @media (pointer: coarse) {
738
- /* Expand the hit target, not the visual control or its containing row. */
738
+ /* Expand the hit target, not the visual control or its containing row. The inset
739
+ counts from inside the border: the checkbox's 16px plus 2 × 14px is 44px. */
739
740
  .mdbase-checkbox::before,
740
741
  .mdbase-switch::before {
741
742
  position: absolute;
742
- inset: -13px;
743
+ inset: -14px;
743
744
  content: "";
744
745
  }
745
746
  }
@@ -352,6 +352,12 @@
352
352
  .cm-lp-table-grip-column { width: 32px; height: 20px; }
353
353
  .cm-lp-table-grip-row { width: 20px; height: 32px; }
354
354
  .cm-lp-table-add { opacity: 1; }
355
+ /* Fingertip targets grow away from the table, so they cover no more of its cells than the controls do. */
356
+ .cm-lp-table-controls > button::before { position: absolute; content: ""; }
357
+ .cm-lp-table-grip-column::before { bottom: -1px; left: 50%; width: 44px; height: 44px; translate: -50% 0; }
358
+ .cm-lp-table-grip-row::before { top: 50%; right: -1px; width: 44px; height: 44px; translate: 0 -50%; }
359
+ .cm-lp-table-add-row::before { inset: -1px -1px auto; height: 44px; }
360
+ .cm-lp-table-add-column::before { inset: -1px auto -1px -1px; width: 44px; }
355
361
  }
356
362
 
357
363
  /* Code blocks retain real editable lines, including nested syntax highlighting. */
@@ -367,7 +373,8 @@
367
373
  .cm-lp-code-first { border-radius: var(--radius-md) var(--radius-md) 0 0; padding-top: 0.5em !important; }
368
374
  .cm-lp-code-last { border-radius: 0 0 var(--radius-md) var(--radius-md); padding-bottom: 0.5em !important; }
369
375
  .cm-lp-code-first.cm-lp-code-last { border-radius: var(--radius-md); }
370
- .cm-lp-code-fence-end { line-height: 0; min-height: 0; }
376
+ /* An opening fence shown as source is as tall as the header it replaces, so the code below stays put. */
377
+ .cm-lp-code-first::before { content: ""; display: inline-block; height: 28px; vertical-align: middle; }
371
378
  .cm-lp-code-header { display: inline-flex; width: 100%; vertical-align: middle; align-items: center; justify-content: space-between; gap: 1em; min-height: 28px; color: var(--color-text-muted); font-family: var(--sans); font-size: var(--font-size-caption); }
372
379
  .cm-lp-code-header button, .cm-lp-callout-header button { border: 0; border-radius: var(--radius-sm); background: transparent; color: inherit; cursor: pointer; min-width: 28px; min-height: 28px; font: inherit; }
373
380
  .cm-lp-code-header button:hover, .cm-lp-callout-header button:hover { background: var(--color-surface); color: var(--color-text); }
@@ -424,6 +431,13 @@
424
431
  .cm-lp-callout-danger { background: color-mix(in srgb, var(--color-danger) 8%, var(--color-surface)); }
425
432
  .cm-lp-callout-header { display: inline-flex; align-items: center; gap: 0.55em; width: 100%; }
426
433
  .cm-lp-callout-header button { margin-left: auto; }
434
+
435
+ @media (pointer: coarse) {
436
+ /* Fingertip targets around the block buttons. Copy's grows upward, off the code it sits on. */
437
+ .cm-lp-code-header button, .cm-lp-callout-header button { position: relative; }
438
+ .cm-lp-code-header button::before, .cm-lp-callout-header button::before, .cm-lp-diagram-edit::before { position: absolute; inset: calc(50% - 22px); content: ""; }
439
+ .cm-lp-code-header button::before { inset: auto calc(50% - 22px) 0; height: 44px; }
440
+ }
427
441
  .cm-lp-callout-icon { width: 1em; text-align: center; color: var(--color-text-soft); }
428
442
  .cm-lp-footnote-ref { font-size: 0.75em; vertical-align: super; color: var(--color-text-soft); }
429
443
  .cm-lp-footnote-definition { color: var(--color-text-muted); font-size: 0.9em; }
@@ -4,6 +4,7 @@ import { StateEffect, StateField } from "@codemirror/state";
4
4
  import { Decoration, EditorView, WidgetType, keymap } from "@codemirror/view";
5
5
  import { i18n as _i18n } from "@lingui/core";
6
6
  import { DiagramWidget, isDiagramLanguage } from "./diagram.js";
7
+ import { proseEdit } from "./prose-edit.js";
7
8
  import { isRevealed, revealField } from "./reveal.js";
8
9
  const toggleFold = StateEffect.define({
9
10
  map: (value, changes) => ({
@@ -350,7 +351,39 @@ function analyze(state) {
350
351
  }
351
352
  const structure = StateField.define({
352
353
  create: analyze,
353
- update: (value, transaction) => transaction.docChanged || syntaxTree(transaction.state) !== syntaxTree(transaction.startState) ? analyze(transaction.state) : value
354
+ update(value, transaction) {
355
+ // Typing in prose only moves the blocks and footnotes on other lines.
356
+ if (proseEdit(transaction)) {
357
+ const map = pos => transaction.changes.mapPos(pos);
358
+ return {
359
+ codes: value.codes.map(block => ({
360
+ ...block,
361
+ from: map(block.from),
362
+ to: map(block.to),
363
+ lines: block.lines.map(line => ({
364
+ ...line,
365
+ from: map(line.from),
366
+ to: map(line.to)
367
+ }))
368
+ })),
369
+ callouts: value.callouts.map(callout => ({
370
+ ...callout,
371
+ from: map(callout.from),
372
+ to: map(callout.to),
373
+ lines: callout.lines.map(map),
374
+ lastTo: map(callout.lastTo)
375
+ })),
376
+ definitions: value.definitions,
377
+ definitionLines: value.definitionLines.map(map),
378
+ references: value.references.map(reference => ({
379
+ ...reference,
380
+ from: map(reference.from),
381
+ to: map(reference.to)
382
+ }))
383
+ };
384
+ }
385
+ return transaction.docChanged || syntaxTree(transaction.state) !== syntaxTree(transaction.startState) ? analyze(transaction.state) : value;
386
+ }
354
387
  });
355
388
  export function blockPreview() {
356
389
  return [folds, structure, EditorView.decorations.compute([structure, revealField, folds], build), keymap.of([{
@@ -404,12 +437,8 @@ function build(state) {
404
437
  side: -1
405
438
  }).range(first.from));
406
439
  }
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
- }
440
+ // The hidden closing fence keeps its line, so showing it moves nothing below.
441
+ if (block.closed && !isRevealed(state, last.from, last.to)) ranges.push(hide.range(last.from, last.to));
413
442
  }
414
443
  for (const callout of callouts) {
415
444
  const folded = callout.fold ? foldState.get(callout.from) ?? callout.fold === "-" : false;
@@ -159,11 +159,12 @@ function buildRanges(state, options, visibleRanges) {
159
159
  ranges.push(headingLines[Number(heading[1]) - 1].range(line(node.from).from));
160
160
  const revealed = lineRevealed(node.from);
161
161
  for (const mark of children(node, "HeaderMark")) {
162
+ // The opening mark takes its space with it; a closing mark the space before it.
163
+ const opening = mark.from === node.from;
162
164
  if (revealed) {
163
- ranges.push((mark.from === node.from ? headingMark : muted).range(mark.from, mark.to));
165
+ // The opening mark hangs in the margin with its space, so the text stays put.
166
+ ranges.push(opening ? headingMark.range(mark.from, skipSpaces(state, mark.to, node.to)) : muted.range(mark.from, mark.to));
164
167
  } else {
165
- // The opening mark takes its space with it; a closing mark the space before it.
166
- const opening = mark.from === node.from;
167
168
  const from = opening ? mark.from : trimStart(state, node.from, mark.from);
168
169
  const to = opening ? skipSpaces(state, mark.to, node.to) : mark.to;
169
170
  conceal(from, to);
@@ -202,7 +203,8 @@ function buildRanges(state, options, visibleRanges) {
202
203
  if (name === "QuoteMark") {
203
204
  const current = line(node.from);
204
205
  if (calloutHeader(current.text)) return;
205
- if (isRevealed(state, current.from, current.to)) ranges.push(quoteMark.range(node.from, node.to));else conceal(node.from, skipSpaces(state, node.to, current.to, 1));
206
+ const end = skipSpaces(state, node.to, current.to, 1);
207
+ if (isRevealed(state, current.from, current.to)) ranges.push(quoteMark.range(node.from, end));else conceal(node.from, end);
206
208
  return;
207
209
  }
208
210
  if (name === "ListMark") {
@@ -1,6 +1,6 @@
1
1
  import { syntaxTree } from "@codemirror/language";
2
2
  import { EditorSelection, Prec } from "@codemirror/state";
3
- import { keymap } from "@codemirror/view";
3
+ import { BlockType, keymap } from "@codemirror/view";
4
4
  const prefixMarks = new Set(["HeaderMark", "QuoteMark", "ListMark", "TaskMarker"]);
5
5
  /** Where a line's text starts after its quote, list, task and heading markers. */
6
6
  function textStart(state, position) {
@@ -34,10 +34,72 @@ function home(view, extend) {
34
34
  }));
35
35
  return true;
36
36
  }
37
+ /**
38
+ * The first line between `from` and `to`, going `forward` or back, that shows:
39
+ * one with height. Hidden lines, such as a table's delimiter row or a folded
40
+ * callout's body, have none.
41
+ */
42
+ function shownLineBetween(view, from, to, forward) {
43
+ const {
44
+ doc
45
+ } = view.state;
46
+ const start = doc.lineAt(from).number,
47
+ end = doc.lineAt(to).number,
48
+ step = forward ? 1 : -1;
49
+ for (let number = start + step; forward ? number < end : number > end; number += step) {
50
+ const block = view.lineBlockAt(doc.line(number).from);
51
+ if (block.height > 0) return block;
52
+ }
53
+ return null;
54
+ }
55
+ /**
56
+ * Up and Down reach every line that shows. CodeMirror's vertical motion steps
57
+ * over rendered blocks and over lines whose end has no position on screen, such
58
+ * as a table row whose closing pipe is hidden, so it jumped past tables, fences,
59
+ * formulas and embeds. Where it would skip such a line, the caret stops there
60
+ * instead: in a table row, in the cell under the caret; on a rendered block, at
61
+ * its source, which then shows.
62
+ */
63
+ function lineByLine(view, forward, extend) {
64
+ const {
65
+ state
66
+ } = view;
67
+ const range = state.selection.main;
68
+ if (state.selection.ranges.length > 1 || !extend && !range.empty) return false;
69
+ const moved = view.moveVertically(range, forward);
70
+ const block = shownLineBetween(view, range.head, moved.head, forward);
71
+ if (!block) return false;
72
+ const content = view.contentDOM.getBoundingClientRect();
73
+ const goal = moved.goalColumn ?? range.goalColumn ?? (view.coordsAtPos(range.head)?.left ?? content.left) - content.left;
74
+ let head;
75
+ if (block.type === BlockType.Text) {
76
+ const at = view.posAtCoords({
77
+ x: content.left + goal,
78
+ y: view.documentTop + (block.top + block.bottom) / 2
79
+ }, false);
80
+ head = Math.min(Math.max(at, block.from), block.to);
81
+ } else {
82
+ head = forward ? block.from : state.doc.lineAt(block.to).from;
83
+ }
84
+ view.dispatch({
85
+ selection: extend ? EditorSelection.range(range.anchor, head, goal) : EditorSelection.cursor(head, 0, undefined, goal),
86
+ scrollIntoView: true,
87
+ userEvent: "select"
88
+ });
89
+ return true;
90
+ }
37
91
  export const livePreviewKeys = Prec.high(keymap.of([{
38
92
  key: "Home",
39
93
  run: view => home(view, false),
40
94
  shift: view => home(view, true)
95
+ }, {
96
+ key: "ArrowDown",
97
+ run: view => lineByLine(view, true, false),
98
+ shift: view => lineByLine(view, true, true)
99
+ }, {
100
+ key: "ArrowUp",
101
+ run: view => lineByLine(view, false, false),
102
+ shift: view => lineByLine(view, false, true)
41
103
  }]));
42
104
  /** The list item whose marker starts the line at `position`, if any. */
43
105
  function itemAt(state, position) {
@@ -4,6 +4,7 @@ import { Decoration, EditorView, WidgetType } from "@codemirror/view";
4
4
  import { i18n as _i18n } from "@lingui/core";
5
5
  import "../i18n.js";
6
6
  import { lazily } from "./lazy.js";
7
+ import { proseEdit } from "./prose-edit.js";
7
8
  import { isRevealed, revealField } from "./reveal.js";
8
9
  const DOLLAR = 36,
9
10
  BACKSLASH = 92,
@@ -187,7 +188,22 @@ function formulas(state) {
187
188
  }
188
189
  const formulaField = StateField.define({
189
190
  create: formulas,
190
- update: (value, transaction) => transaction.docChanged || syntaxTree(transaction.state) !== syntaxTree(transaction.startState) ? formulas(transaction.state) : value
191
+ update(value, transaction) {
192
+ // Typing in prose only moves the formulas on other lines.
193
+ if (proseEdit(transaction)) {
194
+ const {
195
+ changes
196
+ } = transaction;
197
+ return value.map(formula => ({
198
+ ...formula,
199
+ from: changes.mapPos(formula.from),
200
+ to: changes.mapPos(formula.to),
201
+ first: changes.mapPos(formula.first),
202
+ lastTo: changes.mapPos(formula.lastTo)
203
+ }));
204
+ }
205
+ return transaction.docChanged || syntaxTree(transaction.state) !== syntaxTree(transaction.startState) ? formulas(transaction.state) : value;
206
+ }
191
207
  });
192
208
  /** Renders `markdownMath` formulas where the selection is not, and shows a rendered preview under a revealed display block. */
193
209
  export function mathPreview() {
@@ -0,0 +1,9 @@
1
+ import type { Transaction } from "@codemirror/state";
2
+ /**
3
+ * Whether `transaction` only edits prose. Each change stays on one line of a
4
+ * paragraph or heading and starts after the first character of that line's
5
+ * text, so the line keeps its kind of block, and neither the old nor the new
6
+ * line holds code, quote, table, formula or footnote syntax. Structure found
7
+ * in the syntax tree outside such a line is then unchanged apart from moving.
8
+ */
9
+ export declare function proseEdit(transaction: Transaction): boolean;
@@ -0,0 +1,39 @@
1
+ import { syntaxTree } from "@codemirror/language";
2
+ /** Text blocks: an edit inside one changes inline content only. */
3
+ const proseBlocks = new Set(["Paragraph", "ATXHeading1", "ATXHeading2", "ATXHeading3", "ATXHeading4", "ATXHeading5", "ATXHeading6"]);
4
+ /** Characters that open or close code, quotes and callouts, tables, formulas and footnotes. */
5
+ const structural = /[`~>|$]|\[\^/;
6
+ /** A line's leading list, quote and heading markers. */
7
+ const leadingMarkers = /^[\s\-+*>#\d.)]*/;
8
+ const known = new WeakMap();
9
+ /**
10
+ * Whether `transaction` only edits prose. Each change stays on one line of a
11
+ * paragraph or heading and starts after the first character of that line's
12
+ * text, so the line keeps its kind of block, and neither the old nor the new
13
+ * line holds code, quote, table, formula or footnote syntax. Structure found
14
+ * in the syntax tree outside such a line is then unchanged apart from moving.
15
+ */
16
+ export function proseEdit(transaction) {
17
+ let prose = known.get(transaction);
18
+ if (prose === undefined) known.set(transaction, prose = transaction.docChanged && everyChangeIsProse(transaction));
19
+ return prose;
20
+ }
21
+ function everyChangeIsProse({
22
+ startState,
23
+ state,
24
+ changes
25
+ }) {
26
+ const tree = syntaxTree(startState);
27
+ let prose = true;
28
+ changes.iterChanges((fromA, toA, fromB, toB) => {
29
+ if (!prose) return;
30
+ const before = startState.doc.lineAt(fromA),
31
+ after = state.doc.lineAt(fromB);
32
+ prose = toA <= before.to && toB <= after.to && fromA - before.from > leadingMarkers.exec(before.text)[0].length && !structural.test(before.text) && !structural.test(after.text);
33
+ if (!prose) return;
34
+ let node = tree.resolveInner(fromA, 1);
35
+ while (node.parent && !proseBlocks.has(node.name)) node = node.parent;
36
+ prose = proseBlocks.has(node.name) && node.to >= toA;
37
+ });
38
+ return prose;
39
+ }
@@ -626,6 +626,15 @@ function caretSide(transaction) {
626
626
  const at = table && main.head >= table.rows[0].lineFrom && main.head <= table.to ? cellAt(table, main.head) : null;
627
627
  const cell = at && table.rows[at.row].cells[at.column];
628
628
  if (!cell) return transaction;
629
+ // Before the first cell's text is the row's hidden leading pipe. A caret arriving there from
630
+ // another line goes into the cell; one leaving the row to the left keeps that stop.
631
+ const arriving = state.doc.lineAt(main.head).number !== transaction.startState.doc.lineAt(transaction.startState.selection.main.head).number;
632
+ if (at.column === 0 && main.head < cell.contentFrom && arriving) {
633
+ return [transaction, {
634
+ selection: EditorSelection.create([EditorSelection.cursor(cell.contentFrom, 1)]),
635
+ sequential: true
636
+ }];
637
+ }
629
638
  // An empty cell's caret stop is the widget after it; text ends before the caret.
630
639
  const side = cell.contentFrom === cell.contentTo && main.head === cell.contentFrom ? 1 : main.head >= cell.contentTo ? -1 : main.head <= cell.contentFrom ? 1 : 0;
631
640
  if (!side || main.assoc === side) return transaction;
@@ -751,6 +760,11 @@ function keepCaretInCell(update) {
751
760
  view
752
761
  } = update;
753
762
  if (!(update.selectionSet || update.docChanged) || !view.hasFocus) return;
763
+ // Reading the DOM selection lays out the page: do it only for a caret in a table.
764
+ const {
765
+ main
766
+ } = update.state.selection;
767
+ if (!main.empty || !tableAt(update.state, main.head)) return;
754
768
  const selection = view.root.getSelection?.();
755
769
  const line = selection?.isCollapsed ? selection.anchorNode : null;
756
770
  if (!(line instanceof HTMLElement) || !line.classList.contains("cm-lp-tr")) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdbase-dev/ui",
3
- "version": "0.1.0-beta.230",
3
+ "version": "0.1.0",
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": "4712fff2cdc84f950bd292cc476e3011f7a7602a"
245
+ "commit": "f1c417117b2cefd5bad52f1f5aaa60260c752c11"
246
246
  }
247
247
  }