@usememos/editor 26.10.18 → 26.10.19
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 -6
- package/dist/base.css +4 -0
- package/dist/index.js +207 -30
- package/dist/index.js.map +3 -3
- package/dist/style.css +9 -0
- package/dist/theme.css +2 -0
- package/dist/types/commands.d.ts +9 -0
- package/dist/types/editor.d.ts +25 -2
- package/dist/types/extension.d.ts +35 -3
- package/dist/types/extensions/tags.d.ts +15 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/theme.d.ts +2 -0
- package/dist/types/view/dom.d.ts +37 -1
- package/dist/types/view/reveal.d.ts +10 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -79,9 +79,9 @@ For note-style Markdown, as in the playground, use `markdown: { breaks: true, se
|
|
|
79
79
|
|
|
80
80
|
`editor.setOptions({ placeholder, editable, taskToggle, mode, attributes, resolveUrl, onLinkClick, theme })` changes those options while the editor runs. `setOptions({ extensions })` takes new callbacks for `suggestion()` extensions of the same names. The other options are fixed for the editor's lifetime.
|
|
81
81
|
|
|
82
|
-
In React, `placeholder`, `editable`, `taskToggle`, `mode`, `attributes`, `resolveUrl`, `onLinkClick` and `theme` update live. A change to the names in `extensions` recreates the editor and keeps its Markdown, so an inline array is fine; with the same names, the callbacks of `suggestion()` extensions update live, so `items` may read React state. Other props are read on mount; change the component's `key` to apply new values. A `ref` gives the core `Editor` instance. A `value` that arrives while an input method (IME) is composing text is applied when the composition ends.
|
|
82
|
+
In React, `placeholder`, `editable`, `taskToggle`, `mode`, `attributes`, `resolveUrl`, `onLinkClick` and `theme` update live. `onChange`, `onSubmit`, `onFocus` and `onBlur` always call the latest prop, so they may be inline functions that close over state; no ref is needed. A change to the names in `extensions` recreates the editor and keeps its Markdown, so an inline array is fine; with the same names, the callbacks of `suggestion()` extensions update live, so `items` may read React state. Other props are read on mount; change the component's `key` to apply new values. A `ref` gives the core `Editor` instance. A `value` that arrives while an input method (IME) is composing text is applied when the composition ends.
|
|
83
83
|
|
|
84
|
-
For apps that manage the editor instance themselves, `useEditor(options, deps)` creates one and `<EditorContent editor={editor} />` shows it:
|
|
84
|
+
For apps that manage the editor instance themselves, `useEditor(options, deps)` creates one and `<EditorContent editor={editor} />` shows it. Its `onChange`, `onSubmit`, `onFocus`, `onBlur` and `onLinkClick` call the latest options too, and so do `suggestion()` callbacks:
|
|
85
85
|
|
|
86
86
|
```tsx
|
|
87
87
|
import { EditorContent, useEditor } from '@usememos/editor/react';
|
|
@@ -100,7 +100,7 @@ function Composer() {
|
|
|
100
100
|
| `setMarkdown(markdown, { history? })` | Replaces the document. `history: 'clear'` (default) clears undo history; `'keep'` makes the replacement one undo step. Does not emit `change`. |
|
|
101
101
|
| `isEmpty` | True when the document holds only empty paragraphs. |
|
|
102
102
|
| `state` | The document and selection, immutable. |
|
|
103
|
-
| `commands.*` | Edits. Each applies one change, records one undo step, and returns `true` if it did something: `toggleBold`, `toggleItalic`, `toggleStrike`, `toggleCode`, `setParagraph`, `toggleHeading({ level })`, `toggleBulletList`, `toggleOrderedList`, `toggleTaskList`, `toggleBlockquote`, `toggleCallout({ type? })`, `setCallout({ type })`, `toggleDetails`, `toggleCodeBlock({ language? })`, `setLink({ href })`, `unsetLink`, `updateImage({ alt?, title?, align?, width? })`, `insertText(text, { range? })`, `insertMarkdown(markdown, options?)`, `insertTable({ rows?, cols? })`, `insertDivider`, `indent`, `outdent`, `undo`, `redo`, `selectAll`, `selectBlocks`, `focus(position?)`, `blur`, `scrollIntoView`. `selectBlocks` selects the blocks the selection touches as whole blocks, as Escape does; it returns `false` when read-only. `updateImage` changes the selected image; `null` removes its title, alignment or width, and a new width drops a height so the image keeps its aspect ratio. `toggleCallout` turns the selected blocks into a callout (`note` unless `type` says otherwise: `note`, `tip`, `important`, `warning` or `caution`), or back into paragraphs. `setCallout` changes the type of the whole callout or quote with the caret; `type: null` makes it a plain quote. `toggleDetails` makes the block with the caret the title of a collapsible section, or, on a title, removes the section and keeps its blocks. `toggleCallout`, `setCallout` and `toggleDetails` return `false` when the `markdown` option turns that syntax off. `scrollIntoView` changes nothing in the document: it scrolls the caret into view in the editor's scrolling containers and the window, for example after an `insertMarkdown` from code. |
|
|
103
|
+
| `commands.*` | Edits. Each applies one change, records one undo step, and returns `true` if it did something: `toggleBold`, `toggleItalic`, `toggleStrike`, `toggleCode`, `setParagraph`, `toggleHeading({ level })`, `toggleBulletList`, `toggleOrderedList`, `toggleTaskList`, `toggleBlockquote`, `toggleCallout({ type? })`, `setCallout({ type })`, `toggleDetails`, `toggleCodeBlock({ language? })`, `setLink({ href })`, `unsetLink`, `updateImage({ alt?, title?, align?, width? })`, `insertText(text, { range? })`, `insertToken(text)`, `insertMarkdown(markdown, options?)`, `insertTable({ rows?, cols? })`, `insertDivider`, `indent`, `outdent`, `undo`, `redo`, `selectAll`, `selectBlocks`, `focus(position?)`, `blur`, `scrollIntoView`. `selectBlocks` selects the blocks the selection touches as whole blocks, as Escape does; it returns `false` when read-only. `updateImage` changes the selected image; `null` removes its title, alignment or width, and a new width drops a height so the image keeps its aspect ratio. `toggleCallout` turns the selected blocks into a callout (`note` unless `type` says otherwise: `note`, `tip`, `important`, `warning` or `caution`), or back into paragraphs. `setCallout` changes the type of the whole callout or quote with the caret; `type: null` makes it a plain quote. `toggleDetails` makes the block with the caret the title of a collapsible section, or, on a title, removes the section and keeps its blocks. `toggleCallout`, `setCallout` and `toggleDetails` return `false` when the `markdown` option turns that syntax off. `scrollIntoView` changes nothing in the document: it scrolls the caret into view in the editor's scrolling containers and the window, for example after an `insertMarkdown` from code. |
|
|
104
104
|
| `run(fn)` | Applies a custom command: a function from state to a new state, or `null`. The pure commands are exported as `commands` from the package. |
|
|
105
105
|
| `isActive(name, attrs?)` | `bold`, `italic`, `strike`, `code`, `link`, `image` (the selection is exactly one image), or the type of the block with the caret: `paragraph`, `heading`, `bulletList`, `orderedList`, `taskList`, `blockquote` (also true in a list inside a quote), `details` (a collapsible section's title or end), `codeBlock`, `table`, `divider`, `raw`. `attrs` narrows it, for example `isActive('heading', { level: 2 })`. |
|
|
106
106
|
| `getAttributes(name)` | `src`, `alt`, `title`, `align`, `width`, `height` and `block` (true for an image block) of the selected image, `href` of a link, `level` of a heading, `language` of a code block, `checked` of a task, `syntax` of a raw block, `callout` of a callout (`getAttributes('blockquote')`, also in a list item inside a callout), `open` (saved open) and `collapsed` (shown collapsed) of a collapsible section title (`getAttributes('details')`). |
|
|
@@ -113,6 +113,7 @@ function Composer() {
|
|
|
113
113
|
| `addWidget({ at, render })` | Shows app UI after the block that contains `at` (a position or `'end'`), such as an upload placeholder. Returns a widget with `update(render?)`, `remove()` and `isRemoved`. A widget is never saved and is not part of undo history. It moves with its block; if the block is deleted, it moves to the nearest remaining block. `insertMarkdown(md, { at: widget })` inserts after the widget's block. |
|
|
114
114
|
| `positionAtPoint(x, y)` | The document position at a viewport point, or `null`. |
|
|
115
115
|
| `redecorate()` | Computes decorations again for every block, for when what they show depends on something outside the document: which mentioned people exist, a tag's color, a highlighter's language that finished loading. Waits for an input method to finish composing. |
|
|
116
|
+
| `refreshViews(name?)` | Renders every view (`views` and `blockView` in extensions) again, or those of one syntax (`'math'`, `'code:mermaid'`) or of the extension with that name (its `blockView`), for when what a view shows depends on something besides its source that changed: a renderer that finished loading, the font a diagram measured its text with, the page's color scheme a diagram was drawn in. A view that failed is tried again. Only the blocks that show those views render again; the document, selection and history stay. Waits for an input method to finish composing. |
|
|
116
117
|
| `submit()` | Emits a pending `change`, then `submit` with the current Markdown, as Mod+Enter does. Returns `false` when nothing listens for `submit`. |
|
|
117
118
|
| `root` | The editable element: `element`, or for `useEditor`, the element `<EditorContent>` shows. |
|
|
118
119
|
| `isComposing` | True while an input method (IME) is composing text. Replacing the document then would break the composition; the React wrapper waits for it to end. |
|
|
@@ -120,7 +121,9 @@ function Composer() {
|
|
|
120
121
|
| `mode` | `'rich'` or `'source'`: the mode the editor shows now. See [Source mode](#source-mode). |
|
|
121
122
|
| `hasFocus()`, `focus()`, `blur()`, `isEditable`, `setOptions(options)` | |
|
|
122
123
|
|
|
123
|
-
`insertMarkdown(markdown, { at, block, select })`: `at` is `'selection'` (default; replaces the selection, like a paste), `'end'`, a position, or a widget; the last three replace nothing. A single paragraph is inserted inline unless `block: true`, which inserts it as its own block and splits the block at the target. `select: 'keep'` keeps the current selection instead of moving the caret after the inserted content.
|
|
124
|
+
`insertMarkdown(markdown, { at, block, select })`: `at` is `'selection'` (default; replaces the selection, like a paste), `'end'`, a position, or a widget; the last three replace nothing, so `at: editor.getSelection().focus` inserts at the caret and keeps selected text. A single paragraph is inserted inline unless `block: true`, which inserts it as its own block and splits the block at the target; an empty paragraph at the target is replaced, as a paste would, so no empty line is left above the inserted blocks. `select: 'keep'` keeps the current selection instead of moving the caret after the inserted content.
|
|
125
|
+
|
|
126
|
+
`insertToken(text)` inserts a token such as `#tag` at the caret as a word of its own: a space before it unless the caret is at the start of the text or after whitespace, and a space after it, reusing one that is already there; the caret ends after that space. Selected text is kept, the token takes the format at the caret as typed text would (plain text in a code block), and it returns `false` without inserting while an input method is composing.
|
|
124
127
|
|
|
125
128
|
### Extensions
|
|
126
129
|
|
|
@@ -143,7 +146,7 @@ const tags: Extension = {
|
|
|
143
146
|
textDecorator({ pattern: /\bTODO\b/, class: 'todo' })
|
|
144
147
|
```
|
|
145
148
|
|
|
146
|
-
`tags()` and `mentions()` highlight `#tag` and `@name` (classes `mbe-tag` and `mbe-mention`) and keep `\#` and `\@` as written, so an escaped trigger survives edits. Their `class` and `attributes` can be functions of the match as written (`#work/planning`), for a color per tag or a `data-` attribute per person; call `editor.redecorate()` when what they read changes. Tags and mentions stay plain text and are saved exactly as typed. `scan` replaces the default grammar (`scanTags`, `scanMentions`); see [docs/syntax.md](docs/syntax.md#tags-and-mentions). For autocomplete, add a `suggestion()` next to them.
|
|
149
|
+
`tags()` and `mentions()` highlight `#tag` and `@name` (classes `mbe-tag` and `mbe-mention`) and keep `\#` and `\@` as written, so an escaped trigger survives edits. Their `class` and `attributes` can be functions of the match as written (`#work/planning`), for a color per tag or a `data-` attribute per person; call `editor.redecorate()` when what they read changes. `onClick({ editor, match, event })` is called when a tag or mention is clicked to be followed, with the same contract as `onLinkClick`: a plain click while read-only, Mod+click while editing; return `true` to handle it, and the click goes no further. With `onClick`, read-only tags and mentions show a pointer cursor and mentions underline on hover, so an app needs no selectors of its own; whether a mention names someone who exists is the handler's call. Tags and mentions stay plain text and are saved exactly as typed. `scan` replaces the default grammar (`scanTags`, `scanMentions`); see [docs/syntax.md](docs/syntax.md#tags-and-mentions). For autocomplete, add a `suggestion()` next to them.
|
|
147
150
|
|
|
148
151
|
#### Autocomplete
|
|
149
152
|
|
|
@@ -237,7 +240,7 @@ Options: `limit` (default 8), `minLength`, the letters typed before the popup op
|
|
|
237
240
|
|
|
238
241
|
`markdown` adds syntax; see `math()`, `footnotes()`, [docs/syntax.md](docs/syntax.md) and [docs/architecture.md](docs/architecture.md).
|
|
239
242
|
|
|
240
|
-
`views` shows a syntax rendered instead of as source, keyed by syntax name. A key of `code:` and a language (`code:mermaid`) renders code blocks of that language, such as diagrams. The key `html` renders raw HTML: HTML blocks (`kind` `'block'`), and inline HTML (`'inline'`) one run at a time, an opening tag through its closing tag (`<kbd>**Ctrl**</kbd>`), or a single tag (`<br>`). The view gets HTML only: the editor renders the Markdown between the tags itself, inline text and the blocks inside an HTML section such as `<details>` (see [HTML blocks](docs/syntax.md#html-blocks)), and puts it in place of a marker it leaves in the HTML, so parse the source as HTML and keep its text. The editor never renders HTML itself, so sanitize it in the view. While editing, a token shows its source when the caret touches it, and a block when the selection is in it; clicking either does the same. Read-only always shows the rendered view. A view that finishes later, such as a diagram library that loads on demand, fills in a placeholder and returns a promise: the editor shows the placeholder, then the finished element; a promise that rejects or resolves to `false` shows the source. `math()` uses this when given a render function; the core has no math renderer of its own, so pass one such as KaTeX. A render function may return a promise, for a renderer loaded on demand; the formula's source shows until it resolves:
|
|
243
|
+
`views` shows a syntax rendered instead of as source, keyed by syntax name. A key of `code:` and a language (`code:mermaid`) renders code blocks of that language, such as diagrams. The key `html` renders raw HTML: HTML blocks (`kind` `'block'`), and inline HTML (`'inline'`) one run at a time, an opening tag through its closing tag (`<kbd>**Ctrl**</kbd>`), or a single tag (`<br>`). The view gets HTML only: the editor renders the Markdown between the tags itself, inline text and the blocks inside an HTML section such as `<details>` (see [HTML blocks](docs/syntax.md#html-blocks)), and puts it in place of a marker it leaves in the HTML, so parse the source as HTML and keep its text. The editor never renders HTML itself, so sanitize it in the view. While editing, a token shows its source when the caret touches it, and a block when the selection is in it; clicking either does the same. Read-only always shows the rendered view. A view that finishes later, such as a diagram library that loads on demand, fills in a placeholder and returns a promise: the editor shows the placeholder, then the finished element; a promise that rejects or resolves to `false` shows the source. A view's element is not in the page yet, so a view that lays out text, such as a diagram, measures it with its fourth argument, the editor: `getComputedStyle(ctx.editor.root)` gives the font, for a canvas `measureText`. Rendered views are cached by source; `editor.refreshViews(syntax?)` renders them again when what they show depends on something else that changed, such as a renderer that finished loading, and the editor calls it itself when the theme's font tokens change. `math()` uses this when given a render function; the core has no math renderer of its own, so pass one such as KaTeX. A render function may return a promise, for a renderer loaded on demand; the formula's source shows until it resolves:
|
|
241
244
|
|
|
242
245
|
```ts
|
|
243
246
|
import katex from 'katex';
|
|
@@ -246,6 +249,22 @@ import 'katex/dist/katex.min.css';
|
|
|
246
249
|
math({ render: (tex, element, options) => katex.render(tex, element, { ...options, throwOnError: false }) });
|
|
247
250
|
```
|
|
248
251
|
|
|
252
|
+
`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
|
+
```tsx
|
|
255
|
+
const linkCards: Extension = {
|
|
256
|
+
name: 'linkCards',
|
|
257
|
+
blockView: {
|
|
258
|
+
match: (block) => block.type === 'paragraph' && block.content.length === 1 && block.content[0].link === inlineText(block.content),
|
|
259
|
+
render: (block, element) => {
|
|
260
|
+
const root = createRoot(element);
|
|
261
|
+
root.render(<LinkCard href={block.content[0].link} />);
|
|
262
|
+
return () => root.unmount();
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
```
|
|
267
|
+
|
|
249
268
|
### Theming
|
|
250
269
|
|
|
251
270
|
`style.css` is two stylesheets: `base.css` holds the layout the editor needs and all spacing between blocks, and `theme.css` the default look. The default look is quiet and compact, like a rendered Markdown view: a 16px body with 24px lines, 8px between blocks, headings close to the body size, muted quotes and list markers, code blocks on a light panel with no border, and tables as a light grid with a shaded header row. Editing and read-only look the same; only the cursor on links and checkboxes changes. It has no border or background of its own, so it fits inside your own card, comment box or chat input.
|
package/dist/base.css
CHANGED
|
@@ -248,6 +248,10 @@
|
|
|
248
248
|
:is(.mbe-root, .mbe-flow) > .mbe-view-block { overflow-x: auto; overflow-y: hidden; }
|
|
249
249
|
/* A renderer's own block margins (KaTeX's display math has 1em) would add to the block gap. */
|
|
250
250
|
:is(.mbe-root, .mbe-flow) > .mbe-view-block > * { margin-block: 0; }
|
|
251
|
+
/* A block an extension's block view draws (see Extension.blockView): the app owns what is inside, so nothing is clipped. */
|
|
252
|
+
.mbe-root > .mbe-block-view { overflow: visible; }
|
|
253
|
+
/* Tags and mentions with an onClick, read-only: clickable, as links are. */
|
|
254
|
+
.mbe-root.mbe-readonly :is([data-mbe-tag], [data-mbe-mention]) { cursor: pointer; }
|
|
251
255
|
/* Rendered HTML: line breaks between its tags are not text. The Markdown
|
|
252
256
|
between the tags of an HTML section (.mbe-flow) is blocks again, one
|
|
253
257
|
block gap from the HTML around it. */
|
package/dist/index.js
CHANGED
|
@@ -37,6 +37,7 @@ __export(commands_exports, {
|
|
|
37
37
|
insertFragment: () => insertFragment,
|
|
38
38
|
insertLineBreak: () => insertLineBreak,
|
|
39
39
|
insertText: () => insertText,
|
|
40
|
+
insertToken: () => insertToken,
|
|
40
41
|
isMarkActive: () => isMarkActive,
|
|
41
42
|
moveCell: () => moveCell,
|
|
42
43
|
nextGrapheme: () => nextGrapheme,
|
|
@@ -762,6 +763,18 @@ function insertText(state, text) {
|
|
|
762
763
|
const next = insertInline(content, pos.offset, value, format);
|
|
763
764
|
return replaceBlock(s, withContainerContent(block3, pos.cell, next), caret(block3.id, pos.offset + value.length, pos.cell));
|
|
764
765
|
}
|
|
766
|
+
function insertToken(state, token) {
|
|
767
|
+
const pos = state.selection.focus;
|
|
768
|
+
const content = containerContent(getBlock(state.doc, pos.blockId), pos.cell);
|
|
769
|
+
if (content === null) return state;
|
|
770
|
+
const text = inlineText(content);
|
|
771
|
+
const before = pos.offset > 0 && !/\s/.test(text[pos.offset - 1]) ? " " : "";
|
|
772
|
+
const spaced = text[pos.offset] === " " || text[pos.offset] === " ";
|
|
773
|
+
const next = insertText({ ...state, selection: { anchor: pos, focus: pos } }, before + token + (spaced ? "" : " "));
|
|
774
|
+
if (!spaced) return next;
|
|
775
|
+
const after = { ...next.selection.focus, offset: next.selection.focus.offset + 1 };
|
|
776
|
+
return { ...next, selection: { anchor: after, focus: after } };
|
|
777
|
+
}
|
|
765
778
|
function exitBlock(state, dir) {
|
|
766
779
|
const image = selectedImageBlock(state);
|
|
767
780
|
if (!isCollapsed(state.selection) && !image) return state;
|
|
@@ -7235,7 +7248,12 @@ function renderBlock(block3, ctx, reveal) {
|
|
|
7235
7248
|
return e;
|
|
7236
7249
|
};
|
|
7237
7250
|
let root;
|
|
7238
|
-
|
|
7251
|
+
const hosted = reveal?.has(REVEAL_BLOCK) || ctx.source ? null : ctx.blockView?.(block3);
|
|
7252
|
+
if (hosted) {
|
|
7253
|
+
root = el("div", "mbe-view-block mbe-block-view");
|
|
7254
|
+
root.contentEditable = "false";
|
|
7255
|
+
root.appendChild(hosted);
|
|
7256
|
+
} else switch (block3.type) {
|
|
7239
7257
|
case "paragraph":
|
|
7240
7258
|
case "heading":
|
|
7241
7259
|
case "quote":
|
|
@@ -7456,8 +7474,9 @@ function renderFlow(blocks, ctx) {
|
|
|
7456
7474
|
}
|
|
7457
7475
|
function appendBlocks(flow, blocks, ctx) {
|
|
7458
7476
|
const elements = /* @__PURE__ */ new Map();
|
|
7477
|
+
const inner = ctx.blockView ? { ...ctx, blockView: void 0 } : ctx;
|
|
7459
7478
|
for (const block3 of blocks) {
|
|
7460
|
-
const el = renderBlock(block3,
|
|
7479
|
+
const el = renderBlock(block3, inner);
|
|
7461
7480
|
elements.set(block3.id, el);
|
|
7462
7481
|
flow.appendChild(el);
|
|
7463
7482
|
}
|
|
@@ -7769,6 +7788,16 @@ var BlockRenderer = class {
|
|
|
7769
7788
|
widgetsAfter;
|
|
7770
7789
|
/** Views of extension syntaxes, by syntax name. */
|
|
7771
7790
|
views;
|
|
7791
|
+
/** What a view gets besides its source: the editor. Set with `views`. */
|
|
7792
|
+
viewContext;
|
|
7793
|
+
/** Block views of extensions, with the extension's name, in extension order. */
|
|
7794
|
+
blockViews = [];
|
|
7795
|
+
/**
|
|
7796
|
+
* Per block drawn by a block view, the element the view drew into (null
|
|
7797
|
+
* when every matching view declined), kept while the block object is the
|
|
7798
|
+
* same, and the function to call when the element is let go.
|
|
7799
|
+
*/
|
|
7800
|
+
hosted = /* @__PURE__ */ new Map();
|
|
7772
7801
|
/** The document's Markdown options, for the Markdown inside an HTML section. */
|
|
7773
7802
|
markdown;
|
|
7774
7803
|
/** Theme classes for rendered elements. Set before a full render. */
|
|
@@ -7794,6 +7823,8 @@ var BlockRenderer = class {
|
|
|
7794
7823
|
pendingViews = /* @__PURE__ */ new Map();
|
|
7795
7824
|
/** Views whose promise rejected or resolved to false: their source shows. */
|
|
7796
7825
|
failedViews = /* @__PURE__ */ new Set();
|
|
7826
|
+
/** Per cached or failed view, the blocks that showed it, to render again when `dropViews` forgets it. */
|
|
7827
|
+
viewBlocks = /* @__PURE__ */ new Map();
|
|
7797
7828
|
/** The block being rendered, so a pending view knows which blocks to render again. */
|
|
7798
7829
|
renderingBlock = "";
|
|
7799
7830
|
/** During a render, the loaded images of the block elements it replaces, by `src`. */
|
|
@@ -7808,6 +7839,7 @@ var BlockRenderer = class {
|
|
|
7808
7839
|
decorateCode: this.decorateCode,
|
|
7809
7840
|
views: this.views,
|
|
7810
7841
|
view: this.views && ((syntax, source, kind) => this.renderView(syntax, source, kind)),
|
|
7842
|
+
blockView: this.blockViews.length > 0 ? (block3) => this.renderBlockView(block3) : void 0,
|
|
7811
7843
|
markdown: this.markdown,
|
|
7812
7844
|
classes: this.classes,
|
|
7813
7845
|
icon: this.icon,
|
|
@@ -7819,9 +7851,12 @@ var BlockRenderer = class {
|
|
|
7819
7851
|
}
|
|
7820
7852
|
renderView(syntax, source, kind) {
|
|
7821
7853
|
const view = this.views?.get(syntax);
|
|
7822
|
-
if (!view) return null;
|
|
7854
|
+
if (!view || !this.viewContext) return null;
|
|
7823
7855
|
const key = `${kind}\0${syntax}\0${source}`;
|
|
7824
|
-
if (this.failedViews.has(key))
|
|
7856
|
+
if (this.failedViews.has(key)) {
|
|
7857
|
+
this.noteViewBlock(key);
|
|
7858
|
+
return null;
|
|
7859
|
+
}
|
|
7825
7860
|
let el = this.viewCache.get(key);
|
|
7826
7861
|
if (el) {
|
|
7827
7862
|
this.viewCache.delete(key);
|
|
@@ -7829,38 +7864,125 @@ var BlockRenderer = class {
|
|
|
7829
7864
|
el = this.root.ownerDocument.createElement(kind === "inline" ? "span" : "div");
|
|
7830
7865
|
let result;
|
|
7831
7866
|
try {
|
|
7832
|
-
result = view(source, el, kind);
|
|
7867
|
+
result = view(source, el, kind, this.viewContext);
|
|
7833
7868
|
} catch {
|
|
7869
|
+
result = false;
|
|
7870
|
+
}
|
|
7871
|
+
if (result === false) {
|
|
7872
|
+
this.noteViewBlock(key);
|
|
7834
7873
|
return null;
|
|
7835
7874
|
}
|
|
7836
|
-
if (result === false) return null;
|
|
7837
7875
|
if (result && typeof result.then === "function") this.awaitView(key, result);
|
|
7838
|
-
if (this.viewCache.size >= 500)
|
|
7876
|
+
if (this.viewCache.size >= 500) {
|
|
7877
|
+
const oldest = this.viewCache.keys().next().value;
|
|
7878
|
+
this.viewCache.delete(oldest);
|
|
7879
|
+
this.viewBlocks.delete(oldest);
|
|
7880
|
+
}
|
|
7839
7881
|
}
|
|
7840
7882
|
this.pendingViews.get(key)?.add(this.renderingBlock);
|
|
7883
|
+
this.noteViewBlock(key);
|
|
7841
7884
|
this.viewCache.set(key, el);
|
|
7842
7885
|
return el.cloneNode(true);
|
|
7843
7886
|
}
|
|
7887
|
+
/** Whether a block view draws `block`: its blocks show their editable form only while the selection is in them. */
|
|
7888
|
+
isHosted(block3) {
|
|
7889
|
+
return this.blockViews.some(({ view }) => view.match(block3));
|
|
7890
|
+
}
|
|
7891
|
+
/**
|
|
7892
|
+
* The element a block view drew `block` into: the one from the last render
|
|
7893
|
+
* while the block object is the same, else a fresh one from the first
|
|
7894
|
+
* matching view that does not decline. Null when none draws it.
|
|
7895
|
+
*/
|
|
7896
|
+
renderBlockView(block3) {
|
|
7897
|
+
const prev = this.hosted.get(block3.id);
|
|
7898
|
+
if (prev && prev.block === block3) return prev.el;
|
|
7899
|
+
if (prev) this.disposeHosted(block3.id);
|
|
7900
|
+
const ctx = this.viewContext;
|
|
7901
|
+
if (!ctx) return null;
|
|
7902
|
+
let matched = null;
|
|
7903
|
+
for (const { name, view } of this.blockViews) {
|
|
7904
|
+
if (!view.match(block3)) continue;
|
|
7905
|
+
matched = name;
|
|
7906
|
+
const el = this.root.ownerDocument.createElement("div");
|
|
7907
|
+
let result;
|
|
7908
|
+
try {
|
|
7909
|
+
result = view.render(block3, el, ctx);
|
|
7910
|
+
} catch {
|
|
7911
|
+
result = false;
|
|
7912
|
+
}
|
|
7913
|
+
if (result === false) continue;
|
|
7914
|
+
this.hosted.set(block3.id, { block: block3, name, el, dispose: typeof result === "function" ? result : void 0 });
|
|
7915
|
+
return el;
|
|
7916
|
+
}
|
|
7917
|
+
if (matched !== null) this.hosted.set(block3.id, { block: block3, name: matched, el: null });
|
|
7918
|
+
return null;
|
|
7919
|
+
}
|
|
7920
|
+
disposeHosted(id) {
|
|
7921
|
+
const entry = this.hosted.get(id);
|
|
7922
|
+
if (!entry) return;
|
|
7923
|
+
this.hosted.delete(id);
|
|
7924
|
+
try {
|
|
7925
|
+
entry.dispose?.();
|
|
7926
|
+
} catch {
|
|
7927
|
+
}
|
|
7928
|
+
}
|
|
7929
|
+
/** Lets every block view's element go: on destroy. */
|
|
7930
|
+
destroy() {
|
|
7931
|
+
for (const id of [...this.hosted.keys()]) this.disposeHosted(id);
|
|
7932
|
+
}
|
|
7933
|
+
noteViewBlock(key) {
|
|
7934
|
+
let blocks = this.viewBlocks.get(key);
|
|
7935
|
+
if (!blocks) {
|
|
7936
|
+
if (this.viewBlocks.size >= 1e3) this.viewBlocks.delete(this.viewBlocks.keys().next().value);
|
|
7937
|
+
this.viewBlocks.set(key, blocks = /* @__PURE__ */ new Set());
|
|
7938
|
+
}
|
|
7939
|
+
blocks.add(this.renderingBlock);
|
|
7940
|
+
}
|
|
7844
7941
|
/**
|
|
7845
7942
|
* A view that renders later (a diagram, a lazily loaded renderer) shows what
|
|
7846
7943
|
* it filled in at once, and its blocks render again when it settles.
|
|
7847
7944
|
*/
|
|
7848
7945
|
awaitView(key, result) {
|
|
7849
|
-
|
|
7946
|
+
const blocks = /* @__PURE__ */ new Set();
|
|
7947
|
+
this.pendingViews.set(key, blocks);
|
|
7850
7948
|
const settle = (ok) => {
|
|
7851
|
-
|
|
7949
|
+
if (this.pendingViews.get(key) !== blocks) return;
|
|
7852
7950
|
this.pendingViews.delete(key);
|
|
7853
7951
|
if (!ok) {
|
|
7854
7952
|
this.failedViews.add(key);
|
|
7855
7953
|
this.viewCache.delete(key);
|
|
7856
7954
|
}
|
|
7857
|
-
if (blocks
|
|
7955
|
+
if (blocks.size > 0) this.onViewSettled?.([...blocks]);
|
|
7858
7956
|
};
|
|
7859
7957
|
result.then(
|
|
7860
7958
|
(value) => settle(value !== false),
|
|
7861
7959
|
() => settle(false)
|
|
7862
7960
|
);
|
|
7863
7961
|
}
|
|
7962
|
+
/**
|
|
7963
|
+
* Forgets rendered and failed views, of one syntax or all, so the next
|
|
7964
|
+
* render runs the view again: for when what it shows depends on something
|
|
7965
|
+
* besides its source that changed, such as the editor's font. Returns the
|
|
7966
|
+
* rendered blocks that showed them, for the caller to render again.
|
|
7967
|
+
*/
|
|
7968
|
+
dropViews(syntax) {
|
|
7969
|
+
const matches2 = (key) => syntax === void 0 || key.split("\0", 3)[1] === syntax;
|
|
7970
|
+
for (const key of [...this.viewCache.keys()]) if (matches2(key)) this.viewCache.delete(key);
|
|
7971
|
+
for (const key of [...this.failedViews]) if (matches2(key)) this.failedViews.delete(key);
|
|
7972
|
+
for (const key of [...this.pendingViews.keys()]) if (matches2(key)) this.pendingViews.delete(key);
|
|
7973
|
+
const ids = /* @__PURE__ */ new Set();
|
|
7974
|
+
for (const [key, blocks] of [...this.viewBlocks]) {
|
|
7975
|
+
if (!matches2(key)) continue;
|
|
7976
|
+
this.viewBlocks.delete(key);
|
|
7977
|
+
for (const id of blocks) if (this.rendered.has(id)) ids.add(id);
|
|
7978
|
+
}
|
|
7979
|
+
for (const [id, entry] of [...this.hosted]) {
|
|
7980
|
+
if (syntax !== void 0 && entry.name !== syntax) continue;
|
|
7981
|
+
this.disposeHosted(id);
|
|
7982
|
+
if (this.rendered.has(id)) ids.add(id);
|
|
7983
|
+
}
|
|
7984
|
+
return [...ids];
|
|
7985
|
+
}
|
|
7864
7986
|
/**
|
|
7865
7987
|
* `force` re-renders every block. `lazy` (with a full render) lets blocks
|
|
7866
7988
|
* skip layout while off screen; use it only when loading a document.
|
|
@@ -7897,6 +8019,7 @@ var BlockRenderer = class {
|
|
|
7897
8019
|
next.set(block3.id, { block: block3, el, reveal });
|
|
7898
8020
|
}
|
|
7899
8021
|
this.loadedImages.clear();
|
|
8022
|
+
for (const [id, entry] of [...this.hosted]) if (next.get(id)?.block !== entry.block) this.disposeHosted(id);
|
|
7900
8023
|
const nodes = [];
|
|
7901
8024
|
for (const [id, { el }] of next) {
|
|
7902
8025
|
nodes.push(el);
|
|
@@ -8423,7 +8546,7 @@ function setIcon(button, editor, name) {
|
|
|
8423
8546
|
}
|
|
8424
8547
|
|
|
8425
8548
|
// src/view/reveal.ts
|
|
8426
|
-
function revealedViews(state, views, active) {
|
|
8549
|
+
function revealedViews(state, views, active, hosted) {
|
|
8427
8550
|
const out = /* @__PURE__ */ new Map();
|
|
8428
8551
|
if (!active) return out;
|
|
8429
8552
|
const { anchor, focus } = state.selection;
|
|
@@ -8434,6 +8557,7 @@ function revealedViews(state, views, active) {
|
|
|
8434
8557
|
[anchor, anchorBlock]
|
|
8435
8558
|
]) {
|
|
8436
8559
|
const keys = out.get(block3.id) ?? /* @__PURE__ */ new Set();
|
|
8560
|
+
if (hosted?.(block3)) keys.add(REVEAL_BLOCK);
|
|
8437
8561
|
if (block3.type === "raw") {
|
|
8438
8562
|
if (views.has(block3.syntax)) keys.add(REVEAL_BLOCK);
|
|
8439
8563
|
} else if (block3.type === "code") {
|
|
@@ -8455,6 +8579,11 @@ function sameRevealed(a, b) {
|
|
|
8455
8579
|
return a.size === b.size && [...a].every(([id, keys]) => sameReveal(keys, b.get(id)));
|
|
8456
8580
|
}
|
|
8457
8581
|
function viewEntry(block3, forward) {
|
|
8582
|
+
if (block3.type === "table") {
|
|
8583
|
+
const row = forward ? 0 : block3.rows.length - 1;
|
|
8584
|
+
return { blockId: block3.id, offset: forward ? 0 : inlineLength(block3.rows[row][0]), cell: { row, col: 0 } };
|
|
8585
|
+
}
|
|
8586
|
+
if (block3.type !== "raw" && block3.type !== "code") return forward ? startOf(block3) : endOf(block3);
|
|
8458
8587
|
const text = inlineText(block3.content);
|
|
8459
8588
|
if (block3.type === "code" || block3.syntax === "markdown") return { blockId: block3.id, offset: forward ? 0 : text.length };
|
|
8460
8589
|
if (forward) {
|
|
@@ -8543,6 +8672,7 @@ var SIZE_VARS = {
|
|
|
8543
8672
|
radius: "--mbe-radius",
|
|
8544
8673
|
blockGap: "--mbe-block-gap"
|
|
8545
8674
|
};
|
|
8675
|
+
var FONT_PROPERTIES = [SIZE_VARS.font, SIZE_VARS.mono, SIZE_VARS.fontSize, SIZE_VARS.lineHeight];
|
|
8546
8676
|
function tokenProperties(tokens) {
|
|
8547
8677
|
const props = /* @__PURE__ */ new Map();
|
|
8548
8678
|
if (!tokens) return props;
|
|
@@ -8904,6 +9034,8 @@ var Editor = class {
|
|
|
8904
9034
|
return els.length ? els : void 0;
|
|
8905
9035
|
};
|
|
8906
9036
|
this.renderer.views = new Map([["markdown", MARKDOWN_VIEW], ...this.extensions.flatMap((e) => Object.entries(e.views ?? {}))]);
|
|
9037
|
+
this.renderer.viewContext = { editor: this };
|
|
9038
|
+
this.renderer.blockViews = this.extensions.flatMap((e) => e.blockView ? [{ name: e.name, view: e.blockView }] : []);
|
|
8907
9039
|
this.renderer.markdown = this.markdown;
|
|
8908
9040
|
this.renderer.onViewSettled = (ids) => this.viewsSettled(ids);
|
|
8909
9041
|
const codeDecorators = this.extensions.flatMap((e) => e.codeDecorations ? [e.codeDecorations] : []);
|
|
@@ -9379,8 +9511,9 @@ var Editor = class {
|
|
|
9379
9511
|
if ("onLinkClick" in options) this.runtime = { ...this.runtime, onLinkClick: options.onLinkClick };
|
|
9380
9512
|
if ("theme" in options && this.applyTheme(options.theme)) this.paint(true);
|
|
9381
9513
|
}
|
|
9382
|
-
/** Sets the theme. Returns true when
|
|
9514
|
+
/** Sets the theme. Returns true when every block must re-render, for new classes. */
|
|
9383
9515
|
applyTheme(theme) {
|
|
9516
|
+
const before = tokenProperties(this.runtime.theme?.tokens);
|
|
9384
9517
|
this.runtime = { ...this.runtime, theme };
|
|
9385
9518
|
const style = this.root.style;
|
|
9386
9519
|
const props = tokenProperties(theme?.tokens);
|
|
@@ -9389,6 +9522,7 @@ var Editor = class {
|
|
|
9389
9522
|
this.tokenNames = [...props.keys()];
|
|
9390
9523
|
const scheme = theme?.colorScheme ?? "auto";
|
|
9391
9524
|
style.colorScheme = scheme === "auto" ? "" : scheme;
|
|
9525
|
+
if (FONT_PROPERTIES.some((name) => before.get(name) !== props.get(name))) this.renderBlocksAgain(this.renderer.dropViews());
|
|
9392
9526
|
const classes2 = mergeClasses([...this.extensions.map((e) => e.theme?.classes).reverse(), theme?.classes]);
|
|
9393
9527
|
const key = JSON.stringify(classes2);
|
|
9394
9528
|
if (key === this.classesKey) return false;
|
|
@@ -9444,6 +9578,7 @@ var Editor = class {
|
|
|
9444
9578
|
const selection = clampSelection(s.doc, { anchor: range.from, focus: range.to });
|
|
9445
9579
|
return insertText({ ...s, selection, storedMarks: null }, text);
|
|
9446
9580
|
}),
|
|
9581
|
+
insertToken: (text) => !this.composing && run((s) => insertToken(s, text)),
|
|
9447
9582
|
insertMarkdown: (markdown, options = {}) => {
|
|
9448
9583
|
const fragment = parseMarkdown(markdown, this.markdown).blocks;
|
|
9449
9584
|
const at = options.at;
|
|
@@ -9638,6 +9773,7 @@ var Editor = class {
|
|
|
9638
9773
|
this.blockHandle.destroy();
|
|
9639
9774
|
this.linkCard?.destroy();
|
|
9640
9775
|
this.resizeHandles.destroy();
|
|
9776
|
+
this.renderer.destroy();
|
|
9641
9777
|
setPopupContainer(this.root, void 0);
|
|
9642
9778
|
for (const name of Object.keys(this.runtime.attributes ?? {})) this.root.removeAttribute(name);
|
|
9643
9779
|
for (const name of this.tokenNames) this.root.style.removeProperty(name);
|
|
@@ -9882,7 +10018,7 @@ var Editor = class {
|
|
|
9882
10018
|
*/
|
|
9883
10019
|
paint(force = false, lazy = false, hadFocus = this.hasFocus()) {
|
|
9884
10020
|
const views = this.renderer.views;
|
|
9885
|
-
if (views && !this.pointerDown && !this.composing) this.renderer.revealed = revealedViews(this.state, views, !this.readOnly && hadFocus);
|
|
10021
|
+
if (views && !this.pointerDown && !this.composing) this.renderer.revealed = revealedViews(this.state, views, !this.readOnly && hadFocus, this.hosted);
|
|
9886
10022
|
this.renderer.render(this.state.doc, force, lazy);
|
|
9887
10023
|
this.applyHeadingIds();
|
|
9888
10024
|
this.applyReadOnly();
|
|
@@ -9925,6 +10061,30 @@ var Editor = class {
|
|
|
9925
10061
|
this.paint(true);
|
|
9926
10062
|
this.afterRender();
|
|
9927
10063
|
}
|
|
10064
|
+
/**
|
|
10065
|
+
* Renders every view (`Extension.views`) again, or those of one syntax
|
|
10066
|
+
* (`'math'`, `'code:mermaid'`), for when what a view shows depends on
|
|
10067
|
+
* something besides its source that changed: a renderer that finished
|
|
10068
|
+
* loading, or the font a diagram measured its text with. A view that
|
|
10069
|
+
* failed is tried again. Only the blocks that show those views render
|
|
10070
|
+
* again. The editor calls it itself when the theme's font tokens change.
|
|
10071
|
+
* Waits for an input method to finish composing.
|
|
10072
|
+
*/
|
|
10073
|
+
refreshViews(syntax) {
|
|
10074
|
+
if (this.destroyed) return;
|
|
10075
|
+
this.renderBlocksAgain(this.renderer.dropViews(syntax));
|
|
10076
|
+
}
|
|
10077
|
+
/** Renders the given blocks again now, or when the composition in progress settles. */
|
|
10078
|
+
renderBlocksAgain(ids) {
|
|
10079
|
+
if (ids.length === 0) return;
|
|
10080
|
+
if (this.composing || this.composition) {
|
|
10081
|
+
for (const id of ids) this.unsettled.add(id);
|
|
10082
|
+
return;
|
|
10083
|
+
}
|
|
10084
|
+
for (const id of ids) this.renderer.invalidate(id);
|
|
10085
|
+
this.paint();
|
|
10086
|
+
this.afterRender();
|
|
10087
|
+
}
|
|
9928
10088
|
/**
|
|
9929
10089
|
* Views that returned a promise settled: their blocks render again on the
|
|
9930
10090
|
* next frame, or when a composition settles, since rendering would end it.
|
|
@@ -9943,18 +10103,20 @@ var Editor = class {
|
|
|
9943
10103
|
this.afterRender();
|
|
9944
10104
|
});
|
|
9945
10105
|
}
|
|
9946
|
-
/** A block rendered by a view (a formula block, a diagram), or null. */
|
|
10106
|
+
/** A block rendered by a view (a formula block, a diagram, a block an extension's block view draws), or null. */
|
|
9947
10107
|
viewBlock(block3) {
|
|
9948
|
-
if (!block3
|
|
10108
|
+
if (!block3) return null;
|
|
9949
10109
|
return this.renderer.elementFor(block3.id)?.classList.contains("mbe-view-block") ? block3 : null;
|
|
9950
10110
|
}
|
|
9951
10111
|
/** Re-renders if the views that show their source changed: after the selection, focus or editability changes. */
|
|
9952
10112
|
refreshRevealed() {
|
|
9953
10113
|
const views = this.renderer.views;
|
|
9954
10114
|
if (!views || this.pointerDown) return;
|
|
9955
|
-
const next = revealedViews(this.state, views, !this.readOnly && this.hasFocus());
|
|
10115
|
+
const next = revealedViews(this.state, views, !this.readOnly && this.hasFocus(), this.hosted);
|
|
9956
10116
|
if (!sameRevealed(next, this.renderer.revealed)) this.paint();
|
|
9957
10117
|
}
|
|
10118
|
+
/** Whether an extension's block view draws `block` (see `BlockView`). */
|
|
10119
|
+
hosted = (block3) => this.renderer.isHosted(block3);
|
|
9958
10120
|
onMouseUp() {
|
|
9959
10121
|
if (!this.pointerDown) return;
|
|
9960
10122
|
this.pointerDown = false;
|
|
@@ -9985,11 +10147,7 @@ var Editor = class {
|
|
|
9985
10147
|
const content = contentElement(this.renderer.elementFor(current.id));
|
|
9986
10148
|
if (content && !this.onEdgeLine(content, forward)) return null;
|
|
9987
10149
|
}
|
|
9988
|
-
|
|
9989
|
-
const row = forward ? 0 : next.rows.length - 1;
|
|
9990
|
-
return { blockId: next.id, offset: forward ? 0 : inlineLength(next.rows[row][0]), cell: { row, col: 0 } };
|
|
9991
|
-
}
|
|
9992
|
-
return viewEntry(view, forward);
|
|
10150
|
+
return viewEntry(next, forward);
|
|
9993
10151
|
}
|
|
9994
10152
|
/**
|
|
9995
10153
|
* An arrow key next to an image block: from the edge of the block before
|
|
@@ -10084,7 +10242,7 @@ var Editor = class {
|
|
|
10084
10242
|
/** A click on a rendered view puts the caret in its source: at the end of a token, or of a block's formula. */
|
|
10085
10243
|
revealView(view) {
|
|
10086
10244
|
const block3 = view.hasAttribute(BLOCK_ATTR) ? getBlock(this.state.doc, view.getAttribute(BLOCK_ATTR)) : null;
|
|
10087
|
-
const pos = block3 ?
|
|
10245
|
+
const pos = block3 ? viewEntry(block3, false) : domToPosition(this.root, view.parentNode, childIndex(view) + 1);
|
|
10088
10246
|
if (!pos) return;
|
|
10089
10247
|
this.root.focus({ preventScroll: true });
|
|
10090
10248
|
this.setSelection({ anchor: pos, focus: pos });
|
|
@@ -11709,7 +11867,7 @@ function footnotes(options = {}) {
|
|
|
11709
11867
|
return {
|
|
11710
11868
|
name: "footnotes",
|
|
11711
11869
|
markdown: { inline: [inline2], block: [block2] },
|
|
11712
|
-
views: { footnote: (source, element, kind) => (kind === "inline" ? renderReference : renderDefinition)(source, element, kind) },
|
|
11870
|
+
views: { footnote: (source, element, kind, ctx) => (kind === "inline" ? renderReference : renderDefinition)(source, element, kind, ctx) },
|
|
11713
11871
|
onCreate: ({ editor }) => {
|
|
11714
11872
|
const onClick = (e) => {
|
|
11715
11873
|
const ref = e.target.closest?.(".mbe-footnote-ref");
|
|
@@ -11856,7 +12014,7 @@ function* scanMentions(text, from = 0, to = text.length) {
|
|
|
11856
12014
|
if (end > i + 1) i = end - 1;
|
|
11857
12015
|
}
|
|
11858
12016
|
}
|
|
11859
|
-
function decorate2(scan, cls, attributes) {
|
|
12017
|
+
function decorate2(scan, cls, attributes, mark) {
|
|
11860
12018
|
return ({ text, content }) => {
|
|
11861
12019
|
const out = [];
|
|
11862
12020
|
let pos = 0;
|
|
@@ -11869,7 +12027,8 @@ function decorate2(scan, cls, attributes) {
|
|
|
11869
12027
|
if (to <= from) continue;
|
|
11870
12028
|
const match = text.slice(from, to);
|
|
11871
12029
|
const className = typeof cls === "function" ? cls(match) : cls;
|
|
11872
|
-
const
|
|
12030
|
+
const given = typeof attributes === "function" ? attributes(match) : attributes;
|
|
12031
|
+
const attrs = mark ? { ...given, [mark]: match } : given;
|
|
11873
12032
|
out.push(attrs ? { from, to, class: className, attributes: attrs } : { from, to, class: className });
|
|
11874
12033
|
}
|
|
11875
12034
|
}
|
|
@@ -11881,18 +12040,36 @@ function decorate2(scan, cls, attributes) {
|
|
|
11881
12040
|
function escapeSyntax(ch) {
|
|
11882
12041
|
return { name: "escape", triggers: "\\", match: (text, pos) => text[pos + 1] === ch ? pos + 2 : -1 };
|
|
11883
12042
|
}
|
|
11884
|
-
function build(name, char, scan, defaultClass, options) {
|
|
11885
|
-
|
|
12043
|
+
function build(name, char, scan, defaultClass, mark, options) {
|
|
12044
|
+
const onClick = options.onClick;
|
|
12045
|
+
const extension = {
|
|
11886
12046
|
name,
|
|
11887
12047
|
markdown: { inline: [escapeSyntax(char)] },
|
|
11888
|
-
decorations: decorate2(options.scan ?? scan, options.class ?? defaultClass, options.attributes)
|
|
12048
|
+
decorations: decorate2(options.scan ?? scan, options.class ?? defaultClass, options.attributes, onClick ? mark : void 0)
|
|
12049
|
+
};
|
|
12050
|
+
if (!onClick) return extension;
|
|
12051
|
+
return {
|
|
12052
|
+
...extension,
|
|
12053
|
+
// A click on a match follows it as a click on a link does (see Editor.onClick): read-only, or Mod+click while editing.
|
|
12054
|
+
onCreate: ({ editor }) => {
|
|
12055
|
+
const handle = (e) => {
|
|
12056
|
+
const el = e.target.closest?.(`[${mark}]`);
|
|
12057
|
+
if (!el || !editor.root.contains(el) || editor.isEditable && !(isMac ? e.metaKey : e.ctrlKey)) return;
|
|
12058
|
+
if (onClick({ editor, match: el.getAttribute(mark), event: e })) {
|
|
12059
|
+
e.preventDefault();
|
|
12060
|
+
e.stopPropagation();
|
|
12061
|
+
}
|
|
12062
|
+
};
|
|
12063
|
+
editor.root.addEventListener("click", handle);
|
|
12064
|
+
return () => editor.root.removeEventListener("click", handle);
|
|
12065
|
+
}
|
|
11889
12066
|
};
|
|
11890
12067
|
}
|
|
11891
12068
|
function tags(options = {}) {
|
|
11892
|
-
return build("tags", "#", scanTags, "mbe-tag", options);
|
|
12069
|
+
return build("tags", "#", scanTags, "mbe-tag", "data-mbe-tag", options);
|
|
11893
12070
|
}
|
|
11894
12071
|
function mentions(options = {}) {
|
|
11895
|
-
return build("mentions", "@", scanMentions, "mbe-mention", options);
|
|
12072
|
+
return build("mentions", "@", scanMentions, "mbe-mention", "data-mbe-mention", options);
|
|
11896
12073
|
}
|
|
11897
12074
|
|
|
11898
12075
|
// src/extensions/suggestion.ts
|