@usememos/editor 26.10.21 → 26.10.22

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/README.md CHANGED
@@ -55,6 +55,7 @@ The same names work in `new Editor(options)` and as React props, except that Rea
55
55
  | `editable` | `boolean` | Default `true`. With `false`, no editing, and links open on click. |
56
56
  | `taskToggle` | `boolean` | Default `false`. While `editable` is `false`, lets the user tick and untick task items, for example for a memo's owner. Each tick emits `change` with Markdown in which only that item's `[ ]` or `[x]` changed. Ignored while `editable` is `true`. |
57
57
  | `mode` | `'rich' \| 'source'` | Default `'rich'`. `'source'` shows the whole document as its Markdown text, for people who prefer to edit the text itself. Switching changes no text, emits no `change` and keeps undo history. See [Source mode](#source-mode). |
58
+ | `smartLists` | `boolean` | Default `true`. In source mode, Enter continues a list or task item and Tab and Shift+Tab nest list items. `false` makes Enter insert a line break and Tab indent as in a code block. No effect in rich mode, where list items are blocks. Fixed for the editor's lifetime. |
58
59
  | `autofocus` | `boolean \| 'start' \| 'end'` | Focuses the editor on creation, with the caret at the start or the end. |
59
60
  | `attributes` | `Record<string, string>` | Attributes for the editable root, such as `autocorrect`, `autocapitalize` or `aria-label`. |
60
61
  | `resolveUrl` | `(url, kind) => string \| null` | The URL to use for a link (`kind` `'link'`) or an image (`'image'`), or `null` to drop it: the link is not clickable, the image has no `src`. The saved Markdown keeps the original. Default `defaultResolveUrl`, exported: relative URLs, `http`, `https`, `mailto` and `tel`. Call it from your own policy to extend it, for example to allow `sms:` or to block `http:` images. |
@@ -356,7 +357,7 @@ To style the editor entirely with your own classes, import `@usememos/editor/bas
356
357
 
357
358
  With blocks selected: ArrowUp and ArrowDown select the block above or below, Shift extends the selection, Mod+A selects every block, Backspace and Delete delete them, Mod+C, Mod+X and paste copy, cut and replace them as whole blocks, Mod+D duplicates them, and the block type keys, Tab and the mark keys apply to all of them. Enter, ArrowLeft and ArrowRight go back to editing text. Typing does nothing. See [Block selection](docs/architecture.md#block-selection).
358
359
 
359
- In source mode the formatting keys do nothing and Enter inserts a line break; see [Source mode](#source-mode).
360
+ In source mode the formatting keys do nothing, and Enter and Tab edit list items as text; see [Source mode](#source-mode).
360
361
 
361
362
  Markdown shortcuts at the start of a line: `#`–`######`, `-`, `*`, `+`, `1.`, `[ ]`, `[x]`, `>`, `[!note]` and the other callout types, `>>>` (collapsible section), ` ``` `, `---`. Inline: `**bold**`, `*italic*`, `_italic_`, `` `code` ``, `~~strike~~`.
362
363
 
@@ -409,13 +410,14 @@ With `mode: 'source'`, the editor shows the whole document as Markdown text, as
409
410
 
410
411
  **Switching** is a change of view, not an edit. The text shown in source mode is exactly `getMarkdown()`. Going back to rich mode loads the text as `setMarkdown` does, so `getMarkdown()` returns it byte for byte, including lines edited in source mode. A switch emits no `change`, adds no undo step and does not move focus. Undo and redo work across it: undoing an edit made in the other mode restores it in the current mode. The caret stays in the same block, and keeps its column when the text before it in that block has no Markdown syntax; otherwise it goes to the start of the block's text. A switch requested while an input method is composing waits for the composition to end.
411
412
 
412
- **What works in source mode.** Source mode is plain text editing, like a `<textarea>` or a code block. It has no autocomplete and no formatting keys.
413
+ **What works in source mode.** Source mode is plain text editing, like a `<textarea>` or a code block, that knows Markdown lists. It has no autocomplete and no formatting keys.
413
414
 
414
415
  | | |
415
416
  | --- | --- |
416
417
  | Typing, deleting, input methods, spellcheck | As in a code block |
417
- | Enter | Inserts a line break |
418
- | Tab, Shift+Tab | As in a code block: Tab inserts two spaces, or indents the selected lines by two spaces. Shift+Tab removes up to two spaces (or a tab) from the start of those lines |
418
+ | Enter | On a list item, after its marker, starts the next item: the same marker and indentation, the next number, and an unticked `[ ]` after a task. Text after the caret moves to the new item, and the items after it in an ordered list are numbered again. On an empty item, removes the marker, or outdents a nested item, as in rich mode. Elsewhere, inside fenced code and before an item's marker, inserts a line break |
419
+ | Shift+Enter | Inserts a line break |
420
+ | Tab, Shift+Tab | On list items: Tab nests the items the selection touches under the item before them, at that item's text column, and Shift+Tab moves them out to their parent's level. Lines nested under them move with them, ordered lists they leave and join are numbered again, and an item that cannot move stays. Elsewhere, as in a code block: Tab inserts two spaces, or indents the selected lines by two spaces. Shift+Tab removes up to two spaces (or a tab) from the start of those lines |
419
421
  | Paste | The clipboard's plain text (`text/plain`). Files go to `fileHandler` as in rich mode |
420
422
  | Copy and cut | The selected text, as `text/plain` |
421
423
  | Mod+Enter, undo, redo, Mod+A | As in rich mode |
@@ -424,7 +426,7 @@ With `mode: 'source'`, the editor shows the whole document as Markdown text, as
424
426
  | Markdown shortcuts, autolinks | Do not run: the text is the Markdown |
425
427
  | Autocomplete | Off. `suggestion()` popups, the slash menu and Ctrl+Space do not open |
426
428
  | `fileHandler`, `handlePaste`, `handleDrop`, extension key bindings | As in rich mode |
427
- | `commands.*` | `insertText` and `insertMarkdown` insert the text as written; `insertMarkdown` with `block: true` puts it on lines of its own. `indent`, `outdent`, `undo`, `redo`, `selectAll`, `focus`, `blur` and `scrollIntoView` work. The mark, block type, link, image, table, divider, callout, section and `selectBlocks` commands return `false` |
429
+ | `commands.*` | `insertText` and `insertMarkdown` insert the text as written; `insertMarkdown` with `block: true` puts it on lines of its own. `indent` and `outdent` work as Tab and Shift+Tab. `undo`, `undo`, `redo`, `selectAll`, `focus`, `blur` and `scrollIntoView` work. The mark, block type, link, image, table, divider, callout, section and `selectBlocks` commands return `false` |
428
430
  | Placeholder | Shown when the text is empty |
429
431
  | `addWidget` | Shown below the text. A widget remembers its place in the text, which moves with edits before it, and `insertMarkdown(md, { at: widget })` inserts there |
430
432
  | `editable: false` | The text is read-only and can be selected and copied. `taskToggle` has no effect |
package/dist/index.js CHANGED
@@ -3889,14 +3889,14 @@ var BlockParser = class {
3889
3889
  while (this.tip !== this.doc) this.finalize(this.tip, count);
3890
3890
  this.finalize(this.doc, count);
3891
3891
  const children = [];
3892
- const sourceLines = [];
3892
+ const sourceLines2 = [];
3893
3893
  for (const child of this.doc.children) {
3894
3894
  for (const node of this.toAst(child)) {
3895
3895
  children.push(node);
3896
- sourceLines.push({ start: child.startLine, end: child.endLine });
3896
+ sourceLines2.push({ start: child.startLine, end: child.endLine });
3897
3897
  }
3898
3898
  }
3899
- return { type: "root", children, lines: sourceLines, definitions: this.definitions };
3899
+ return { type: "root", children, lines: sourceLines2, definitions: this.definitions };
3900
3900
  }
3901
3901
  /** Link reference definitions at the start of a paragraph's content. Keeps their source. */
3902
3902
  extractReferences(content) {
@@ -3936,13 +3936,13 @@ var BlockParser = class {
3936
3936
  * Advances by `count` characters, or by `count` columns if `columns` is
3937
3937
  * set. A tab advanced by columns may be only partly consumed.
3938
3938
  */
3939
- advanceOffset(count, columns) {
3939
+ advanceOffset(count, columns2) {
3940
3940
  const line = this.line;
3941
3941
  let c;
3942
3942
  while (count > 0 && (c = line[this.offset]) !== void 0) {
3943
3943
  if (c === " ") {
3944
3944
  const toTab = 4 - this.column % 4;
3945
- if (columns) {
3945
+ if (columns2) {
3946
3946
  this.partiallyConsumedTab = toTab > count;
3947
3947
  const advance = toTab > count ? count : toTab;
3948
3948
  this.column += advance;
@@ -8810,6 +8810,260 @@ function sourceDeleteForward(state, next) {
8810
8810
  if (from === text.length) return null;
8811
8811
  return edit(state, from, next(text, from), "", from);
8812
8812
  }
8813
+ var QUOTE_MARKERS = /^(?: {0,3}>[ \t]?)*/;
8814
+ var LIST_MARKER = /^(?:([-*+])|(\d{1,9})([.)]))(?=[ \t]|$)/;
8815
+ var THEMATIC_BREAK = /^([-*_])[ \t]*(?:\1[ \t]*){2,}$/;
8816
+ var FENCE = /^(`{3,}|~{3,})(.*)$/;
8817
+ function columns(space, start = 0) {
8818
+ let col = start;
8819
+ for (const c of space) col = c === " " ? col + 4 - col % 4 : col + 1;
8820
+ return col - start;
8821
+ }
8822
+ function parseMarker(body, at, indent) {
8823
+ if (THEMATIC_BREAK.test(body)) return null;
8824
+ const m = LIST_MARKER.exec(body);
8825
+ if (!m) return null;
8826
+ let pos = m[0].length;
8827
+ const space = /^[ \t]*/.exec(body.slice(pos))[0];
8828
+ const width = columns(space, indent + pos);
8829
+ pos += space.length;
8830
+ let box = -1;
8831
+ if (space && /^\[[ xX]\](?=[ \t]|$)/.test(body.slice(pos))) {
8832
+ box = at + pos;
8833
+ pos += 3;
8834
+ pos += /^[ \t]*/.exec(body.slice(pos))[0].length;
8835
+ }
8836
+ return {
8837
+ ordered: !m[1],
8838
+ char: m[1] ?? m[3],
8839
+ number: m[2] ? Number(m[2]) : 0,
8840
+ from: at,
8841
+ to: at + m[0].length,
8842
+ spaced: space.length > 0,
8843
+ box,
8844
+ content: at + pos,
8845
+ // CommonMark: one to four spaces after the marker set the text's column; more start indented code.
8846
+ column: indent + m[0].length + (width >= 1 && width <= 4 ? width : 1)
8847
+ };
8848
+ }
8849
+ function sourceLines(text) {
8850
+ const lines = [];
8851
+ let fence = null;
8852
+ for (let from = 0; ; ) {
8853
+ const nl = text.indexOf("\n", from);
8854
+ const to = nl === -1 ? text.length : nl;
8855
+ const line = text.slice(from, to);
8856
+ const quote2 = QUOTE_MARKERS.exec(line)[0];
8857
+ const space = /^[ \t]*/.exec(line.slice(quote2.length))[0];
8858
+ const indentEnd = from + quote2.length + space.length;
8859
+ const body = line.slice(quote2.length + space.length);
8860
+ const indent = columns(space);
8861
+ let code = fence !== null;
8862
+ const f = FENCE.exec(body);
8863
+ if (fence) {
8864
+ if (f && f[1][0] === fence.char && f[1].length >= fence.length && f[2].trim() === "") fence = null;
8865
+ } else if (f && !(f[1][0] === "`" && f[2].includes("`"))) {
8866
+ fence = { char: f[1][0], length: f[1].length };
8867
+ code = true;
8868
+ }
8869
+ lines.push({
8870
+ from,
8871
+ to,
8872
+ depth: quote2.split(">").length - 1,
8873
+ quoteEnd: from + quote2.length,
8874
+ indent,
8875
+ indentEnd,
8876
+ blank: body.trim() === "",
8877
+ item: code ? null : parseMarker(body, indentEnd, indent)
8878
+ });
8879
+ if (nl === -1) return lines;
8880
+ from = nl + 1;
8881
+ }
8882
+ }
8883
+ function mayBeItem(text, offset) {
8884
+ const end = text.indexOf("\n", offset);
8885
+ const line = text.slice(text.lastIndexOf("\n", offset - 1) + 1, end === -1 ? text.length : end);
8886
+ return LIST_MARKER.test(line.replace(QUOTE_MARKERS, "").trimStart());
8887
+ }
8888
+ function lineAt(lines, offset) {
8889
+ let i = 0;
8890
+ while (i + 1 < lines.length && lines[i + 1].from <= offset) i++;
8891
+ return i;
8892
+ }
8893
+ function previousSibling(lines, i) {
8894
+ const line = lines[i];
8895
+ for (let j = i - 1; j >= 0; j--) {
8896
+ const l = lines[j];
8897
+ if (l.blank) continue;
8898
+ if (l.depth !== line.depth) return -1;
8899
+ if (l.indent > line.indent) continue;
8900
+ return l.item && l.indent === line.indent ? j : -1;
8901
+ }
8902
+ return -1;
8903
+ }
8904
+ function parentItem(lines, i) {
8905
+ const line = lines[i];
8906
+ for (let j = i - 1; j >= 0; j--) {
8907
+ const l = lines[j];
8908
+ if (l.blank) continue;
8909
+ if (l.depth !== line.depth) return -1;
8910
+ if (l.indent < line.indent) return l.item ? j : -1;
8911
+ }
8912
+ return -1;
8913
+ }
8914
+ function listRun(lines, i) {
8915
+ const line = lines[i];
8916
+ const item = line.item;
8917
+ const same = (l) => l.item !== null && l.indent === line.indent && l.item.ordered === item.ordered && l.item.char === item.char;
8918
+ const run = [i];
8919
+ for (const step of [-1, 1]) {
8920
+ for (let j = i + step; j >= 0 && j < lines.length; j += step) {
8921
+ const l = lines[j];
8922
+ if (l.blank) continue;
8923
+ if (l.depth !== line.depth) break;
8924
+ if (l.indent > line.indent) continue;
8925
+ if (!same(l)) break;
8926
+ if (step < 0) run.unshift(j);
8927
+ else run.push(j);
8928
+ }
8929
+ }
8930
+ return run;
8931
+ }
8932
+ function renumber(lines, targets, fresh) {
8933
+ const changes = [];
8934
+ const done = /* @__PURE__ */ new Set();
8935
+ for (const target of targets) {
8936
+ if (!lines[target]?.item?.ordered) continue;
8937
+ const run = listRun(lines, target);
8938
+ if (done.has(run[0])) continue;
8939
+ done.add(run[0]);
8940
+ const settled = run.filter((j) => !fresh.has(j)).map((j) => lines[j].item.number);
8941
+ const lazy = settled.length >= 2 && settled.every((n) => n === settled[0]);
8942
+ const start = fresh.has(run[0]) ? 1 : lines[run[0]].item.number;
8943
+ run.forEach((j, k) => {
8944
+ const item = lines[j].item;
8945
+ const number = lazy ? fresh.has(j) ? settled[0] : item.number : start + k;
8946
+ if (number !== item.number) changes.push({ from: item.from, to: item.to - 1, insert: String(number) });
8947
+ });
8948
+ }
8949
+ return changes;
8950
+ }
8951
+ function mapThrough(offset, changes) {
8952
+ let shift = 0;
8953
+ for (const c of changes) {
8954
+ if (c.from > offset || c.from === offset && c.to > c.from) break;
8955
+ if (offset >= c.to) shift += c.insert.length - (c.to - c.from);
8956
+ else return c.from + shift + Math.min(offset - c.from, c.insert.length);
8957
+ }
8958
+ return offset + shift;
8959
+ }
8960
+ function applyChanges(text, changes) {
8961
+ let out = "";
8962
+ let last = 0;
8963
+ for (const c of changes) {
8964
+ out += text.slice(last, c.from) + c.insert;
8965
+ last = c.to;
8966
+ }
8967
+ return out + text.slice(last);
8968
+ }
8969
+ function finish(text, changes, anchor, focus, targets, fresh, mapped = false) {
8970
+ changes.sort((a2, b) => a2.from - b.from);
8971
+ let next = applyChanges(text, changes);
8972
+ let a = mapped ? anchor : mapThrough(anchor, changes);
8973
+ let f = mapped ? focus : mapThrough(focus, changes);
8974
+ const lines = sourceLines(next);
8975
+ const numbers = renumber(
8976
+ lines,
8977
+ targets.map((o) => lineAt(lines, o)),
8978
+ new Set(fresh.map((o) => lineAt(lines, o)))
8979
+ );
8980
+ if (numbers.length) {
8981
+ next = applyChanges(next, numbers);
8982
+ a = mapThrough(a, numbers);
8983
+ f = mapThrough(f, numbers);
8984
+ }
8985
+ return sourceState(next, a, f);
8986
+ }
8987
+ function shiftItems(text, lines, first, last, delta2, anchor, focus) {
8988
+ const line = lines[first];
8989
+ let end = last;
8990
+ for (let j = last + 1; j < lines.length; j++) {
8991
+ const l = lines[j];
8992
+ if (l.blank) continue;
8993
+ if (l.depth !== line.depth || l.indent <= lines[last].indent) break;
8994
+ end = j;
8995
+ }
8996
+ const changes = [];
8997
+ for (let j = first; j <= end; j++) {
8998
+ const l = lines[j];
8999
+ if (l.blank || l.depth !== line.depth) continue;
9000
+ changes.push({ from: l.quoteEnd, to: l.indentEnd, insert: " ".repeat(Math.max(0, l.indent + delta2)) });
9001
+ }
9002
+ let after = -1;
9003
+ for (let j = end + 1; j < lines.length; j++) {
9004
+ const l = lines[j];
9005
+ if (l.blank) continue;
9006
+ if (l.depth === line.depth && l.indent === line.indent && l.item) after = j;
9007
+ break;
9008
+ }
9009
+ const moved = mapThrough(line.indentEnd, changes);
9010
+ const following = after === -1 ? [] : [mapThrough(lines[after].indentEnd, changes)];
9011
+ return finish(text, changes, anchor, focus, [moved, ...following], [moved, ...delta2 < 0 ? following : []]);
9012
+ }
9013
+ function sourceEnter(state) {
9014
+ const { from, to } = sourceRange(state);
9015
+ const whole = sourceText(state.doc);
9016
+ const text = whole.slice(0, from) + whole.slice(to);
9017
+ if (!mayBeItem(text, from)) return insertSourceText(state, "\n");
9018
+ const lines = sourceLines(text);
9019
+ const i = lineAt(lines, from);
9020
+ const line = lines[i];
9021
+ const item = line.item;
9022
+ if (!item || !item.spaced || from < item.content) return insertSourceText(state, "\n");
9023
+ if (text.slice(item.content, line.to).trim() === "") {
9024
+ const parent = parentItem(lines, i);
9025
+ if (line.indent > 0 && parent !== -1) return shiftItems(text, lines, i, i, lines[parent].indent - line.indent, line.to, line.to);
9026
+ const next = lines.findIndex((l, j) => j > i && !l.blank);
9027
+ const changes2 = [{ from: line.quoteEnd, to: line.to, insert: "" }];
9028
+ const targets = next === -1 || !lines[next].item ? [] : [mapThrough(lines[next].indentEnd, changes2)];
9029
+ return finish(text, changes2, line.quoteEnd, line.quoteEnd, targets, []);
9030
+ }
9031
+ const marker = item.ordered ? `${item.number + 1}${item.char}` : item.char;
9032
+ const space = text.slice(item.to, item.box === -1 ? item.content : item.box);
9033
+ const box = item.box === -1 ? "" : "[ ]" + (text.slice(item.box + 3, item.content) || " ");
9034
+ const head = text.slice(line.from, line.indentEnd) + marker + space + box;
9035
+ const restStart = from + /^[ \t]*/.exec(text.slice(from, line.to))[0].length;
9036
+ const changes = [{ from, to: restStart, insert: "\n" + head }];
9037
+ const caret2 = from + 1 + head.length;
9038
+ return finish(text, changes, caret2, caret2, [caret2], [caret2], true);
9039
+ }
9040
+ function sourceIndentList(state, delta2) {
9041
+ const { from, to } = sourceRange(state);
9042
+ const text = sourceText(state.doc);
9043
+ if (!mayBeItem(text, from)) return null;
9044
+ const lines = sourceLines(text);
9045
+ const first = lineAt(lines, from);
9046
+ let last = lineAt(lines, to);
9047
+ if (last > first && to === lines[last].from) last--;
9048
+ const line = lines[first];
9049
+ if (!line.item) return null;
9050
+ for (let j = first + 1; j <= last; j++) {
9051
+ const l = lines[j];
9052
+ if (!l.blank && (l.depth !== line.depth || !l.item && l.indent <= line.indent)) return null;
9053
+ }
9054
+ let target;
9055
+ if (delta2 === 1) {
9056
+ const sibling = previousSibling(lines, first);
9057
+ if (sibling === -1) return state;
9058
+ target = lines[sibling].item.column;
9059
+ } else {
9060
+ if (line.indent === 0) return state;
9061
+ const parent = parentItem(lines, first);
9062
+ target = parent === -1 ? 0 : lines[parent].indent;
9063
+ }
9064
+ if (target === line.indent) return state;
9065
+ return shiftItems(text, lines, first, last, target - line.indent, state.selection.anchor.offset, state.selection.focus.offset);
9066
+ }
8813
9067
  function mapOffset(offset, before, after) {
8814
9068
  if (before === after) return offset;
8815
9069
  let start = 0;
@@ -9005,6 +9259,7 @@ var Editor = class {
9005
9259
  wasReadOnly = false;
9006
9260
  /** Task checkboxes stay usable while read-only. */
9007
9261
  taskToggle;
9262
+ smartLists;
9008
9263
  options;
9009
9264
  /** The extensions in priority order, ending with the default keymap unless one was given. */
9010
9265
  extensions;
@@ -9071,6 +9326,7 @@ var Editor = class {
9071
9326
  this.labels = { ...DEFAULT_LABELS, ...options.labels };
9072
9327
  this.readOnly = options.editable === false;
9073
9328
  this.taskToggle = options.taskToggle === true;
9329
+ this.smartLists = options.smartLists !== false;
9074
9330
  const given = options.extensions ?? [];
9075
9331
  this.extensions = given.some((e) => e.name === "defaultKeymap") ? given : [...given, defaultKeymap()];
9076
9332
  this.bindings = this.extensions.flatMap(
@@ -9689,8 +9945,9 @@ var Editor = class {
9689
9945
  const richFn = rich[name];
9690
9946
  commands[name] = (...args) => this.modeValue === "source" ? fn ? fn(...args) : false : richFn(...args);
9691
9947
  };
9692
- inSource("indent", () => run((s) => indentCode(s, 1)));
9693
- inSource("outdent", () => run((s) => indentCode(s, -1)));
9948
+ const indentSource = (delta2) => run((s) => (this.smartLists ? sourceIndentList(s, delta2) : null) ?? indentCode(s, delta2));
9949
+ inSource("indent", () => indentSource(1));
9950
+ inSource("outdent", () => indentSource(-1));
9694
9951
  inSource("insertMarkdown", (markdown, options = {}) => this.insertSourceMarkdown(markdown, options));
9695
9952
  for (const name of [
9696
9953
  "toggleBold",
@@ -10413,6 +10670,8 @@ var Editor = class {
10413
10670
  onSourceInput(type) {
10414
10671
  switch (type) {
10415
10672
  case "insertParagraph":
10673
+ this.run((s) => this.smartLists ? sourceEnter(s) : insertSourceText(s, "\n"));
10674
+ return true;
10416
10675
  case "insertLineBreak":
10417
10676
  this.run((s) => insertSourceText(s, "\n"));
10418
10677
  return true;