@usememos/editor 26.10.20 → 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 +25 -5
- package/dist/base.css +4 -0
- package/dist/index.js +328 -28
- package/dist/index.js.map +3 -3
- package/dist/react.js.map +2 -2
- package/dist/style.css +8 -0
- package/dist/types/editor.d.ts +7 -0
- package/dist/types/extension.d.ts +8 -0
- package/dist/types/model.d.ts +7 -0
- package/dist/types/react/index.d.ts +1 -1
- package/dist/types/sourceMode.d.ts +15 -0
- package/dist/types/view/dom.d.ts +16 -5
- package/package.json +1 -1
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. |
|
|
@@ -251,6 +252,8 @@ math({ render: (tex, element, options) => katex.render(tex, element, { ...option
|
|
|
251
252
|
|
|
252
253
|
`blockView` draws whole blocks the app wants to show its own way, such as a paragraph that is one bare link shown as a preview card with data from the app's server. `match(block)` picks the blocks, and `render(block, element, { editor })` draws one into `element`, a div the editor owns and keeps while the block is unchanged: typing elsewhere, a new selection, `redecorate()` and a switch to read-only never call `render` again, so app UI mounted into the element, such as a React portal, stays. `render` runs again for a block whose content changed and for a new block, and may return a function, which the editor calls when it lets the element go: the block was removed or changed, `refreshViews()` ran, or the editor was destroyed. Returning `false` leaves the block as the editor draws it. While editing, the block shows as the editor draws it, editable, when the selection is in it, and a click on the view puts the caret there, as for a formula; read-only always shows the view. `refreshViews(name)` with the extension's name draws its blocks again.
|
|
253
254
|
|
|
255
|
+
`placement: 'after'` keeps the block as the editor draws it, editable, and places the element after it instead, like a widget: for UI that adds to the text rather than standing in for it, such as a card for a link that sits on its own line of a paragraph. `inlineLines(content)` gives a block's lines, the spans between its line breaks, so `match` can look at one line. The element is still withheld while the selection is in the block, so a link being typed is not previewed on every keystroke, and clicks on it are the app's, not the editor's.
|
|
256
|
+
|
|
254
257
|
```tsx
|
|
255
258
|
const linkCards: Extension = {
|
|
256
259
|
name: 'linkCards',
|
|
@@ -263,6 +266,22 @@ const linkCards: Extension = {
|
|
|
263
266
|
},
|
|
264
267
|
},
|
|
265
268
|
};
|
|
269
|
+
|
|
270
|
+
// A card below a paragraph for each line of it that is one bare link (with `markdown: { breaks: true }`).
|
|
271
|
+
const bareLink = (line: Inline) => (line.length === 1 && line[0].link === line[0].text ? line[0].link : undefined);
|
|
272
|
+
const linkLines = (block: Block) => (block.type === 'paragraph' ? inlineLines(block.content).map(bareLink).filter((href) => href !== undefined) : []);
|
|
273
|
+
const linkCardsBelow: Extension = {
|
|
274
|
+
name: 'linkCardsBelow',
|
|
275
|
+
blockView: {
|
|
276
|
+
placement: 'after',
|
|
277
|
+
match: (block) => linkLines(block).length > 0,
|
|
278
|
+
render: (block, element) => {
|
|
279
|
+
const root = createRoot(element);
|
|
280
|
+
root.render(linkLines(block).map((href) => <LinkCard key={href} href={href} />));
|
|
281
|
+
return () => root.unmount();
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
};
|
|
266
285
|
```
|
|
267
286
|
|
|
268
287
|
### Theming
|
|
@@ -338,7 +357,7 @@ To style the editor entirely with your own classes, import `@usememos/editor/bas
|
|
|
338
357
|
|
|
339
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).
|
|
340
359
|
|
|
341
|
-
In source mode the formatting keys do nothing and Enter
|
|
360
|
+
In source mode the formatting keys do nothing, and Enter and Tab edit list items as text; see [Source mode](#source-mode).
|
|
342
361
|
|
|
343
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~~`.
|
|
344
363
|
|
|
@@ -391,13 +410,14 @@ With `mode: 'source'`, the editor shows the whole document as Markdown text, as
|
|
|
391
410
|
|
|
392
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.
|
|
393
412
|
|
|
394
|
-
**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.
|
|
395
414
|
|
|
396
415
|
| | |
|
|
397
416
|
| --- | --- |
|
|
398
417
|
| Typing, deleting, input methods, spellcheck | As in a code block |
|
|
399
|
-
| Enter |
|
|
400
|
-
|
|
|
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 |
|
|
401
421
|
| Paste | The clipboard's plain text (`text/plain`). Files go to `fileHandler` as in rich mode |
|
|
402
422
|
| Copy and cut | The selected text, as `text/plain` |
|
|
403
423
|
| Mod+Enter, undo, redo, Mod+A | As in rich mode |
|
|
@@ -406,7 +426,7 @@ With `mode: 'source'`, the editor shows the whole document as Markdown text, as
|
|
|
406
426
|
| Markdown shortcuts, autolinks | Do not run: the text is the Markdown |
|
|
407
427
|
| Autocomplete | Off. `suggestion()` popups, the slash menu and Ctrl+Space do not open |
|
|
408
428
|
| `fileHandler`, `handlePaste`, `handleDrop`, extension key bindings | As in rich mode |
|
|
409
|
-
| `commands.*` | `insertText` and `insertMarkdown` insert the text as written; `insertMarkdown` with `block: true` puts it on lines of its own. `indent
|
|
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` |
|
|
410
430
|
| Placeholder | Shown when the text is empty |
|
|
411
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 |
|
|
412
432
|
| `editable: false` | The text is read-only and can be selected and copied. `taskToggle` has no effect |
|
package/dist/base.css
CHANGED
|
@@ -221,6 +221,8 @@
|
|
|
221
221
|
cursor: pointer;
|
|
222
222
|
opacity: 0;
|
|
223
223
|
}
|
|
224
|
+
/* The icon matches the language label's 12px text, so the button is as tall as the label's line. */
|
|
225
|
+
.mbe-root .mbe-code-copy .mbe-icon { width: 12px; height: 12px; }
|
|
224
226
|
.mbe-root .mbe-code:hover .mbe-code-copy,
|
|
225
227
|
.mbe-root .mbe-code-copy:focus-visible,
|
|
226
228
|
.mbe-root .mbe-code-copy.mbe-copied { opacity: 1; }
|
|
@@ -251,6 +253,8 @@
|
|
|
251
253
|
:is(.mbe-root, .mbe-flow) > .mbe-view-block > * { margin-block: 0; }
|
|
252
254
|
/* A block an extension's block view draws (see Extension.blockView): the app owns what is inside, so nothing is clipped. */
|
|
253
255
|
.mbe-root > .mbe-block-view { overflow: visible; }
|
|
256
|
+
/* A block view placed after its block, which stays editable text: not text itself, like a widget. */
|
|
257
|
+
.mbe-root > .mbe-block-after { cursor: default; white-space: normal; }
|
|
254
258
|
/* Tags and mentions with an onClick, read-only: clickable, as links are. */
|
|
255
259
|
.mbe-root.mbe-readonly :is([data-mbe-tag], [data-mbe-mention]) { cursor: pointer; }
|
|
256
260
|
/* Rendered HTML: line breaks between its tags are not text. The Markdown
|
package/dist/index.js
CHANGED
|
@@ -233,6 +233,18 @@ function inlineLength(inline3) {
|
|
|
233
233
|
for (const s of inline3) n += s.text.length;
|
|
234
234
|
return n;
|
|
235
235
|
}
|
|
236
|
+
function inlineLines(inline3) {
|
|
237
|
+
const lines = [[]];
|
|
238
|
+
for (const span of inline3) {
|
|
239
|
+
const parts = span.text.split("\n");
|
|
240
|
+
for (let i = 0; i < parts.length; i++) {
|
|
241
|
+
if (i > 0) lines.push([]);
|
|
242
|
+
const text = parts[i];
|
|
243
|
+
if (text) lines[lines.length - 1].push(text === span.text ? span : { ...span, text });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return lines;
|
|
247
|
+
}
|
|
236
248
|
function normalizeInline(inline3) {
|
|
237
249
|
const out = [];
|
|
238
250
|
for (const span of inline3) {
|
|
@@ -1623,13 +1635,15 @@ function selectedBlocks(state) {
|
|
|
1623
1635
|
const { from, to } = orderedRange(state.doc, state.selection);
|
|
1624
1636
|
const start = blockIndex(state.doc, from.blockId);
|
|
1625
1637
|
const end = blockIndex(state.doc, to.blockId);
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
return cloneBlock(block3);
|
|
1638
|
+
if (start === end) {
|
|
1639
|
+
const block3 = state.doc.blocks[start];
|
|
1640
|
+
if (isTextBlock(block3)) return [paragraph(sliceInline(block3.content, from.offset, to.offset))];
|
|
1641
|
+
if (block3.type === "table" && from.cell && sameCell(from.cell, to.cell)) {
|
|
1642
|
+
return [paragraph(sliceInline(contentOf(block3, from.cell), from.offset, to.offset))];
|
|
1632
1643
|
}
|
|
1644
|
+
}
|
|
1645
|
+
return state.doc.blocks.slice(start, end + 1).map((block3) => {
|
|
1646
|
+
if (!isTextBlock(block3)) return cloneBlock(block3);
|
|
1633
1647
|
const a = block3.id === from.blockId ? from.offset : 0;
|
|
1634
1648
|
const b = block3.id === to.blockId ? to.offset : Infinity;
|
|
1635
1649
|
return cloneBlock(withContent(block3, sliceInline(block3.content, a, b)));
|
|
@@ -3875,14 +3889,14 @@ var BlockParser = class {
|
|
|
3875
3889
|
while (this.tip !== this.doc) this.finalize(this.tip, count);
|
|
3876
3890
|
this.finalize(this.doc, count);
|
|
3877
3891
|
const children = [];
|
|
3878
|
-
const
|
|
3892
|
+
const sourceLines2 = [];
|
|
3879
3893
|
for (const child of this.doc.children) {
|
|
3880
3894
|
for (const node of this.toAst(child)) {
|
|
3881
3895
|
children.push(node);
|
|
3882
|
-
|
|
3896
|
+
sourceLines2.push({ start: child.startLine, end: child.endLine });
|
|
3883
3897
|
}
|
|
3884
3898
|
}
|
|
3885
|
-
return { type: "root", children, lines:
|
|
3899
|
+
return { type: "root", children, lines: sourceLines2, definitions: this.definitions };
|
|
3886
3900
|
}
|
|
3887
3901
|
/** Link reference definitions at the start of a paragraph's content. Keeps their source. */
|
|
3888
3902
|
extractReferences(content) {
|
|
@@ -3922,13 +3936,13 @@ var BlockParser = class {
|
|
|
3922
3936
|
* Advances by `count` characters, or by `count` columns if `columns` is
|
|
3923
3937
|
* set. A tab advanced by columns may be only partly consumed.
|
|
3924
3938
|
*/
|
|
3925
|
-
advanceOffset(count,
|
|
3939
|
+
advanceOffset(count, columns2) {
|
|
3926
3940
|
const line = this.line;
|
|
3927
3941
|
let c;
|
|
3928
3942
|
while (count > 0 && (c = line[this.offset]) !== void 0) {
|
|
3929
3943
|
if (c === " ") {
|
|
3930
3944
|
const toTab = 4 - this.column % 4;
|
|
3931
|
-
if (
|
|
3945
|
+
if (columns2) {
|
|
3932
3946
|
this.partiallyConsumedTab = toTab > count;
|
|
3933
3947
|
const advance = toTab > count ? count : toTab;
|
|
3934
3948
|
this.column += advance;
|
|
@@ -7815,7 +7829,9 @@ var BlockRenderer = class {
|
|
|
7815
7829
|
/**
|
|
7816
7830
|
* Per block drawn by a block view, the element the view drew into (null
|
|
7817
7831
|
* when every matching view declined), kept while the block object is the
|
|
7818
|
-
* same, and the function to call when the element is let go.
|
|
7832
|
+
* same, and the function to call when the element is let go. A view placed
|
|
7833
|
+
* after its block has `after`: the editor's wrapper around that element,
|
|
7834
|
+
* placed after the block's element.
|
|
7819
7835
|
*/
|
|
7820
7836
|
hosted = /* @__PURE__ */ new Map();
|
|
7821
7837
|
/** The document's Markdown options, for the Markdown inside an HTML section. */
|
|
@@ -7909,13 +7925,15 @@ var BlockRenderer = class {
|
|
|
7909
7925
|
return this.blockViews.some(({ view }) => view.match(block3));
|
|
7910
7926
|
}
|
|
7911
7927
|
/**
|
|
7912
|
-
* The element a block view drew `block` into
|
|
7913
|
-
*
|
|
7914
|
-
* matching view that does not decline.
|
|
7928
|
+
* The element a block view drew `block` into, to show in place of the
|
|
7929
|
+
* block: the one from the last render while the block object is the same,
|
|
7930
|
+
* else a fresh one from the first matching view that does not decline.
|
|
7931
|
+
* Null when none draws it, and for a view placed after the block, whose
|
|
7932
|
+
* element `afterView` gives.
|
|
7915
7933
|
*/
|
|
7916
7934
|
renderBlockView(block3) {
|
|
7917
7935
|
const prev = this.hosted.get(block3.id);
|
|
7918
|
-
if (prev && prev.block === block3) return prev.el;
|
|
7936
|
+
if (prev && prev.block === block3) return prev.after ? null : prev.el;
|
|
7919
7937
|
if (prev) this.disposeHosted(block3.id);
|
|
7920
7938
|
const ctx = this.viewContext;
|
|
7921
7939
|
if (!ctx) return null;
|
|
@@ -7931,12 +7949,27 @@ var BlockRenderer = class {
|
|
|
7931
7949
|
result = false;
|
|
7932
7950
|
}
|
|
7933
7951
|
if (result === false) continue;
|
|
7934
|
-
|
|
7935
|
-
|
|
7952
|
+
const dispose = typeof result === "function" ? result : void 0;
|
|
7953
|
+
if (view.placement !== "after") {
|
|
7954
|
+
this.hosted.set(block3.id, { block: block3, name, el, dispose });
|
|
7955
|
+
return el;
|
|
7956
|
+
}
|
|
7957
|
+
const after = this.root.ownerDocument.createElement("div");
|
|
7958
|
+
after.className = "mbe-block-view mbe-block-after";
|
|
7959
|
+
after.contentEditable = "false";
|
|
7960
|
+
after.appendChild(el);
|
|
7961
|
+
this.hosted.set(block3.id, { block: block3, name, el, after, dispose });
|
|
7962
|
+
return null;
|
|
7936
7963
|
}
|
|
7937
7964
|
if (matched !== null) this.hosted.set(block3.id, { block: block3, name: matched, el: null });
|
|
7938
7965
|
return null;
|
|
7939
7966
|
}
|
|
7967
|
+
/** The element of a block view placed after `block`, while the block shows the view (not its editable form). */
|
|
7968
|
+
afterView(block3, reveal) {
|
|
7969
|
+
if (reveal?.has(REVEAL_BLOCK) || this.source) return void 0;
|
|
7970
|
+
const entry = this.hosted.get(block3.id);
|
|
7971
|
+
return entry?.block === block3 ? entry.after : void 0;
|
|
7972
|
+
}
|
|
7940
7973
|
disposeHosted(id) {
|
|
7941
7974
|
const entry = this.hosted.get(id);
|
|
7942
7975
|
if (!entry) return;
|
|
@@ -8041,8 +8074,10 @@ var BlockRenderer = class {
|
|
|
8041
8074
|
this.loadedImages.clear();
|
|
8042
8075
|
for (const [id, entry] of [...this.hosted]) if (next.get(id)?.block !== entry.block) this.disposeHosted(id);
|
|
8043
8076
|
const nodes = [];
|
|
8044
|
-
for (const [id, { el }] of next) {
|
|
8077
|
+
for (const [id, { block: block3, el, reveal }] of next) {
|
|
8045
8078
|
nodes.push(el);
|
|
8079
|
+
const after = this.afterView(block3, reveal);
|
|
8080
|
+
if (after) nodes.push(after);
|
|
8046
8081
|
const widgets = this.widgetsAfter?.(id);
|
|
8047
8082
|
if (widgets) nodes.push(...widgets);
|
|
8048
8083
|
}
|
|
@@ -8775,6 +8810,260 @@ function sourceDeleteForward(state, next) {
|
|
|
8775
8810
|
if (from === text.length) return null;
|
|
8776
8811
|
return edit(state, from, next(text, from), "", from);
|
|
8777
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
|
+
}
|
|
8778
9067
|
function mapOffset(offset, before, after) {
|
|
8779
9068
|
if (before === after) return offset;
|
|
8780
9069
|
let start = 0;
|
|
@@ -8927,6 +9216,7 @@ var ALIGN_CHOICES = [
|
|
|
8927
9216
|
{ id: "alignRight", align: "right", image: "imageAlignRight" }
|
|
8928
9217
|
];
|
|
8929
9218
|
var SLASH_CHARS = ["/", "\uFF0F"];
|
|
9219
|
+
var APP_UI = ".mbe-widget, .mbe-block-after";
|
|
8930
9220
|
var CARET_KEY = /^(Arrow(Up|Down|Left|Right)|Home|End|PageUp|PageDown)$/;
|
|
8931
9221
|
var COMPOSITION_INPUT = /* @__PURE__ */ new Set(["insertCompositionText", "deleteCompositionText", "insertFromComposition", "deleteByComposition"]);
|
|
8932
9222
|
var Editor = class {
|
|
@@ -8969,6 +9259,7 @@ var Editor = class {
|
|
|
8969
9259
|
wasReadOnly = false;
|
|
8970
9260
|
/** Task checkboxes stay usable while read-only. */
|
|
8971
9261
|
taskToggle;
|
|
9262
|
+
smartLists;
|
|
8972
9263
|
options;
|
|
8973
9264
|
/** The extensions in priority order, ending with the default keymap unless one was given. */
|
|
8974
9265
|
extensions;
|
|
@@ -9035,6 +9326,7 @@ var Editor = class {
|
|
|
9035
9326
|
this.labels = { ...DEFAULT_LABELS, ...options.labels };
|
|
9036
9327
|
this.readOnly = options.editable === false;
|
|
9037
9328
|
this.taskToggle = options.taskToggle === true;
|
|
9329
|
+
this.smartLists = options.smartLists !== false;
|
|
9038
9330
|
const given = options.extensions ?? [];
|
|
9039
9331
|
this.extensions = given.some((e) => e.name === "defaultKeymap") ? given : [...given, defaultKeymap()];
|
|
9040
9332
|
this.bindings = this.extensions.flatMap(
|
|
@@ -9322,7 +9614,7 @@ var Editor = class {
|
|
|
9322
9614
|
}
|
|
9323
9615
|
hasFocus() {
|
|
9324
9616
|
const active = this.root.ownerDocument.activeElement;
|
|
9325
|
-
return !!active && this.root.contains(active) && !(active instanceof HTMLInputElement) && !active.closest(
|
|
9617
|
+
return !!active && this.root.contains(active) && !(active instanceof HTMLInputElement) && !active.closest(APP_UI);
|
|
9326
9618
|
}
|
|
9327
9619
|
// -------------------------------------------------------------------------
|
|
9328
9620
|
// Widgets
|
|
@@ -9653,8 +9945,9 @@ var Editor = class {
|
|
|
9653
9945
|
const richFn = rich[name];
|
|
9654
9946
|
commands[name] = (...args) => this.modeValue === "source" ? fn ? fn(...args) : false : richFn(...args);
|
|
9655
9947
|
};
|
|
9656
|
-
|
|
9657
|
-
inSource("
|
|
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));
|
|
9658
9951
|
inSource("insertMarkdown", (markdown, options = {}) => this.insertSourceMarkdown(markdown, options));
|
|
9659
9952
|
for (const name of [
|
|
9660
9953
|
"toggleBold",
|
|
@@ -10285,7 +10578,7 @@ var Editor = class {
|
|
|
10285
10578
|
// Input
|
|
10286
10579
|
/** Events from the code-language input and other form controls inside the root are not editor input. */
|
|
10287
10580
|
fromControl(e) {
|
|
10288
|
-
return e.target instanceof HTMLInputElement || !!e.target?.closest?.(
|
|
10581
|
+
return e.target instanceof HTMLInputElement || !!e.target?.closest?.(APP_UI);
|
|
10289
10582
|
}
|
|
10290
10583
|
onBeforeInput(e) {
|
|
10291
10584
|
if (this.fromControl(e)) return;
|
|
@@ -10377,6 +10670,8 @@ var Editor = class {
|
|
|
10377
10670
|
onSourceInput(type) {
|
|
10378
10671
|
switch (type) {
|
|
10379
10672
|
case "insertParagraph":
|
|
10673
|
+
this.run((s) => this.smartLists ? sourceEnter(s) : insertSourceText(s, "\n"));
|
|
10674
|
+
return true;
|
|
10380
10675
|
case "insertLineBreak":
|
|
10381
10676
|
this.run((s) => insertSourceText(s, "\n"));
|
|
10382
10677
|
return true;
|
|
@@ -10941,8 +11236,12 @@ var Editor = class {
|
|
|
10941
11236
|
this.refreshSelection();
|
|
10942
11237
|
if (isCollapsed(this.state.selection) || !e.clipboardData) return;
|
|
10943
11238
|
e.preventDefault();
|
|
10944
|
-
const
|
|
10945
|
-
|
|
11239
|
+
const selected = selectedBlocks(this.state);
|
|
11240
|
+
const doc = { blocks: closeSections(selected) };
|
|
11241
|
+
const markdown = serializeMarkdown(doc, this.markdown).replace(/\n$/, "");
|
|
11242
|
+
const only = selected.length === 1 && selected[0].type === "paragraph" ? selected[0] : null;
|
|
11243
|
+
const code = only && isPlainTextBlock(getBlock(this.state.doc, this.state.selection.focus.blockId));
|
|
11244
|
+
writeClipboard(e.clipboardData, doc, markdown, code ? inlineText(only.content) : markdown);
|
|
10946
11245
|
if (cut && !this.readOnly) this.run(deleteSelection);
|
|
10947
11246
|
}
|
|
10948
11247
|
onPaste(e) {
|
|
@@ -11702,9 +12001,9 @@ function blockName(block3) {
|
|
|
11702
12001
|
return block3.type;
|
|
11703
12002
|
}
|
|
11704
12003
|
}
|
|
11705
|
-
function writeClipboard(data, doc,
|
|
11706
|
-
data.setData("text/plain",
|
|
11707
|
-
data.setData("text/html", `<meta charset="utf-8"><div data-mbe-markdown="${encodeURIComponent(
|
|
12004
|
+
function writeClipboard(data, doc, markdown, plain2 = markdown) {
|
|
12005
|
+
data.setData("text/plain", plain2);
|
|
12006
|
+
data.setData("text/html", `<meta charset="utf-8"><div data-mbe-markdown="${encodeURIComponent(markdown)}">${docToHtml(doc)}</div>`);
|
|
11708
12007
|
}
|
|
11709
12008
|
function sameBlocks(a, b) {
|
|
11710
12009
|
return a.blocks.length === b.blocks.length && a.blocks.every((block3, i) => block3 === b.blocks[i]);
|
|
@@ -12161,6 +12460,7 @@ export {
|
|
|
12161
12460
|
htmlToDoc,
|
|
12162
12461
|
imageBlockOffset,
|
|
12163
12462
|
inlineLength,
|
|
12463
|
+
inlineLines,
|
|
12164
12464
|
inlineText,
|
|
12165
12465
|
insertInline,
|
|
12166
12466
|
isCollapsed,
|