@mdbase-dev/ui 0.1.7 → 0.1.9

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.
@@ -1,6 +1,7 @@
1
1
  import { syntaxTree } from "@codemirror/language";
2
2
  import { EditorSelection, Prec } from "@codemirror/state";
3
3
  import { BlockType, keymap } from "@codemirror/view";
4
+ import { cellAt, tableAt } from "./table-model.js";
4
5
  const prefixMarks = new Set(["HeaderMark", "QuoteMark", "ListMark", "TaskMarker"]);
5
6
  /** Where a line's text starts after its quote, list, task and heading markers. */
6
7
  function textStart(state, position) {
@@ -52,13 +53,51 @@ function shownLineBetween(view, from, to, forward) {
52
53
  }
53
54
  return null;
54
55
  }
56
+ /**
57
+ * The next line the caret's table cell shows, going `forward` or back, at `x()`. A row is one line
58
+ * whose cells wrap side by side, and CodeMirror finds where it ends from its last cell's text: when
59
+ * that cell is shorter, Down left the row while the caret's own cell still showed lines below.
60
+ */
61
+ function lineInCell(view, range, forward, x) {
62
+ const table = tableAt(view.state, range.head);
63
+ const at = table && cellAt(table, range.head);
64
+ const cell = at && table.rows[at.row].cells[at.column];
65
+ const start = cell && view.coordsAtPos(range.head, range.assoc || (forward ? 1 : -1));
66
+ if (!cell || !start) return null;
67
+ const row = view.lineBlockAt(range.head);
68
+ const top = view.documentTop + row.top,
69
+ bottom = view.documentTop + row.bottom;
70
+ const from = forward ? start.bottom : start.top,
71
+ step = (forward ? 1 : -1) * view.defaultLineHeight / 2;
72
+ for (let y = from + step; forward ? y < bottom : y > top; y += step) {
73
+ const head = view.posAtCoords({
74
+ x: x(),
75
+ y
76
+ }, false);
77
+ const coords = head >= cell.contentFrom && head <= cell.contentTo ? view.coordsAtPos(head) : null;
78
+ if (coords && (forward ? coords.top >= from : coords.bottom <= from)) return head;
79
+ }
80
+ return null;
81
+ }
82
+ /** Where the caret stops on the first line that shows between `from` and `to`, at `x()`. */
83
+ function skippedLine(view, from, to, forward, x) {
84
+ const block = shownLineBetween(view, from, to, forward);
85
+ if (!block) return null;
86
+ if (block.type !== BlockType.Text) return forward ? block.from : view.state.doc.lineAt(block.to).from;
87
+ const at = view.posAtCoords({
88
+ x: x(),
89
+ y: view.documentTop + (block.top + block.bottom) / 2
90
+ }, false);
91
+ return Math.min(Math.max(at, block.from), block.to);
92
+ }
55
93
  /**
56
94
  * Up and Down reach every line that shows. CodeMirror's vertical motion steps
57
95
  * over rendered blocks and over lines whose end has no position on screen, such
58
96
  * as a table row whose closing pipe is hidden, so it jumped past tables, fences,
59
97
  * formulas and embeds. Where it would skip such a line, the caret stops there
60
98
  * instead: in a table row, in the cell under the caret; on a rendered block, at
61
- * its source, which then shows.
99
+ * its source, which then shows. In a table cell whose text wraps, the caret
100
+ * first visits each of the cell's lines.
62
101
  */
63
102
  function lineByLine(view, forward, extend) {
64
103
  const {
@@ -67,22 +106,16 @@ function lineByLine(view, forward, extend) {
67
106
  const range = state.selection.main;
68
107
  if (state.selection.ranges.length > 1 || !extend && !range.empty) return false;
69
108
  const moved = view.moveVertically(range, forward);
70
- const block = shownLineBetween(view, range.head, moved.head, forward);
71
- if (!block) return false;
109
+ const line = state.doc.lineAt(range.head);
110
+ if (moved.head >= line.from && moved.head <= line.to) return false;
72
111
  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
- }
112
+ let goal = moved.goalColumn ?? range.goalColumn;
113
+ const x = () => content.left + (goal ??= (view.coordsAtPos(range.head)?.left ?? content.left) - content.left);
114
+ const head = lineInCell(view, range, forward, x) ?? skippedLine(view, range.head, moved.head, forward, x);
115
+ if (head === null) return false;
116
+ const column = x() - content.left;
84
117
  view.dispatch({
85
- selection: extend ? EditorSelection.range(range.anchor, head, goal) : EditorSelection.cursor(head, 0, undefined, goal),
118
+ selection: extend ? EditorSelection.range(range.anchor, head, column) : EditorSelection.cursor(head, 0, undefined, column),
86
119
  scrollIntoView: true,
87
120
  userEvent: "select"
88
121
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdbase-dev/ui",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -250,6 +250,6 @@
250
250
  "typecheck": "node scripts/compile-catalogues.mjs && tsc -p tsconfig.json --noEmit"
251
251
  },
252
252
  "mdbaseRelease": {
253
- "commit": "e433a42fa6c09dbc44436afe0a01654c1d7d2bce"
253
+ "commit": "3ec51b33c969d57900bbb166eefccd65dc660cdf"
254
254
  }
255
255
  }