@usememos/editor 26.10.18 → 26.10.20
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 +26 -7
- package/dist/base.css +11 -3
- package/dist/index.js +228 -31
- package/dist/index.js.map +3 -3
- package/dist/style.css +17 -6
- package/dist/theme.css +2 -1
- 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 +44 -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,9 +249,25 @@ 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
|
-
`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.
|
|
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 whose long lines scroll sideways rather than wrap, and tables as a light grid with a shaded header row and columns sized to their content. 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.
|
|
252
271
|
|
|
253
272
|
Every color, font and size in `theme.css` is a CSS custom property (`--mbe-text`, `--mbe-accent`, `--mbe-font`, …). Set them with the `theme` option's tokens, with a color set for light and one for dark; colors you leave out keep the defaults:
|
|
254
273
|
|
package/dist/base.css
CHANGED
|
@@ -236,7 +236,8 @@
|
|
|
236
236
|
padding: 0;
|
|
237
237
|
width: 12em;
|
|
238
238
|
}
|
|
239
|
-
|
|
239
|
+
/* Code keeps its lines and scrolls sideways, as GitHub shows it; raw source wraps. */
|
|
240
|
+
:is(.mbe-root, .mbe-flow) > .mbe-code pre { white-space: pre; overflow-x: auto; tab-size: 2; }
|
|
240
241
|
:is(.mbe-root, .mbe-flow) > pre.mbe-raw { white-space: pre-wrap; tab-size: 2; }
|
|
241
242
|
/* Source mode: the document as plain text in the editor's font. */
|
|
242
243
|
.mbe-root > .mbe-source { white-space: pre-wrap; tab-size: 4; overflow-wrap: anywhere; }
|
|
@@ -248,6 +249,10 @@
|
|
|
248
249
|
:is(.mbe-root, .mbe-flow) > .mbe-view-block { overflow-x: auto; overflow-y: hidden; }
|
|
249
250
|
/* A renderer's own block margins (KaTeX's display math has 1em) would add to the block gap. */
|
|
250
251
|
:is(.mbe-root, .mbe-flow) > .mbe-view-block > * { margin-block: 0; }
|
|
252
|
+
/* A block an extension's block view draws (see Extension.blockView): the app owns what is inside, so nothing is clipped. */
|
|
253
|
+
.mbe-root > .mbe-block-view { overflow: visible; }
|
|
254
|
+
/* Tags and mentions with an onClick, read-only: clickable, as links are. */
|
|
255
|
+
.mbe-root.mbe-readonly :is([data-mbe-tag], [data-mbe-mention]) { cursor: pointer; }
|
|
251
256
|
/* Rendered HTML: line breaks between its tags are not text. The Markdown
|
|
252
257
|
between the tags of an HTML section (.mbe-flow) is blocks again, one
|
|
253
258
|
block gap from the HTML around it. */
|
|
@@ -272,16 +277,19 @@
|
|
|
272
277
|
|
|
273
278
|
/* Table */
|
|
274
279
|
:is(.mbe-root, .mbe-flow) > .mbe-table { overflow-x: auto; }
|
|
275
|
-
/* Columns fit their content and wrap at the editor's width
|
|
280
|
+
/* Columns fit their content and wrap at the editor's width, as GitHub lays
|
|
281
|
+
tables out; wider tables scroll. No column has a minimum width, so a long
|
|
282
|
+
column is not squeezed into wrapping by short ones. */
|
|
276
283
|
:is(.mbe-root, .mbe-flow) > .mbe-table table { border-collapse: separate; border-spacing: 0; }
|
|
277
284
|
:is(.mbe-root, .mbe-flow) > .mbe-table th,
|
|
278
285
|
:is(.mbe-root, .mbe-flow) > .mbe-table td {
|
|
279
286
|
padding: 4px 8px;
|
|
280
|
-
min-width: 4em;
|
|
281
287
|
vertical-align: top;
|
|
282
288
|
text-align: start;
|
|
283
289
|
outline: none;
|
|
284
290
|
}
|
|
291
|
+
/* An empty cell keeps room to click into while editing. */
|
|
292
|
+
.mbe-root:not(.mbe-readonly) > .mbe-table :is(th, td).mbe-empty-content { min-width: 4em; }
|
|
285
293
|
|
|
286
294
|
/* Blocks inside collapsible sections, after the block rules so it wins over their margins. */
|
|
287
295
|
:is(.mbe-root, .mbe-flow) > [data-in-section] { margin-inline-start: calc(var(--mbe-section-depth, 0) * var(--mbe-indent-size)); }
|
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
|
}
|
|
@@ -7548,10 +7567,29 @@ function fill(doc) {
|
|
|
7548
7567
|
return br;
|
|
7549
7568
|
}
|
|
7550
7569
|
var VOID_TAGS = /* @__PURE__ */ new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr"]);
|
|
7570
|
+
var HTML_PIECE = /<!--[\s\S]*?-->|<[^<>]*>/g;
|
|
7571
|
+
function splitHtmlSpans(inline3) {
|
|
7572
|
+
if (!inline3.some((s) => s.marks.includes("html"))) return inline3;
|
|
7573
|
+
const out = [];
|
|
7574
|
+
for (const span of normalizeInline(inline3)) {
|
|
7575
|
+
if (!span.marks.includes("html")) {
|
|
7576
|
+
out.push(span);
|
|
7577
|
+
continue;
|
|
7578
|
+
}
|
|
7579
|
+
let last = 0;
|
|
7580
|
+
for (const m of span.text.matchAll(HTML_PIECE)) {
|
|
7581
|
+
if (m.index > last) out.push({ ...span, text: span.text.slice(last, m.index) });
|
|
7582
|
+
out.push({ ...span, text: m[0] });
|
|
7583
|
+
last = m.index + m[0].length;
|
|
7584
|
+
}
|
|
7585
|
+
if (last < span.text.length) out.push({ ...span, text: span.text.slice(last) });
|
|
7586
|
+
}
|
|
7587
|
+
return out;
|
|
7588
|
+
}
|
|
7551
7589
|
function htmlRuns(inline3) {
|
|
7552
7590
|
const spans = [];
|
|
7553
7591
|
let pos = 0;
|
|
7554
|
-
for (const span of inline3) {
|
|
7592
|
+
for (const span of splitHtmlSpans(inline3)) {
|
|
7555
7593
|
spans.push({ span, from: pos, to: pos + span.text.length });
|
|
7556
7594
|
pos += span.text.length;
|
|
7557
7595
|
}
|
|
@@ -7589,6 +7627,7 @@ function renderInline(inline3, parent, ctx, decorations, reveal, cell) {
|
|
|
7589
7627
|
const doc = ctx.doc;
|
|
7590
7628
|
const views = ctx.views;
|
|
7591
7629
|
const viewed = views ? (span, from) => !!span.token && views.has(span.token) && !reveal?.has(tokenKey(cell, from)) : void 0;
|
|
7630
|
+
if (views?.has("html")) inline3 = splitHtmlSpans(inline3);
|
|
7592
7631
|
const runs = views?.has("html") && inline3.some((s) => s.marks.includes("html")) ? htmlRuns(inline3).filter((run) => !reveal?.has(tokenKey(cell, run.from))) : [];
|
|
7593
7632
|
const inRun = (from) => runs.some((run) => from >= run.from && from < run.to);
|
|
7594
7633
|
const isHtml = (span) => span.marks.includes("html");
|
|
@@ -7769,6 +7808,16 @@ var BlockRenderer = class {
|
|
|
7769
7808
|
widgetsAfter;
|
|
7770
7809
|
/** Views of extension syntaxes, by syntax name. */
|
|
7771
7810
|
views;
|
|
7811
|
+
/** What a view gets besides its source: the editor. Set with `views`. */
|
|
7812
|
+
viewContext;
|
|
7813
|
+
/** Block views of extensions, with the extension's name, in extension order. */
|
|
7814
|
+
blockViews = [];
|
|
7815
|
+
/**
|
|
7816
|
+
* Per block drawn by a block view, the element the view drew into (null
|
|
7817
|
+
* 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.
|
|
7819
|
+
*/
|
|
7820
|
+
hosted = /* @__PURE__ */ new Map();
|
|
7772
7821
|
/** The document's Markdown options, for the Markdown inside an HTML section. */
|
|
7773
7822
|
markdown;
|
|
7774
7823
|
/** Theme classes for rendered elements. Set before a full render. */
|
|
@@ -7794,6 +7843,8 @@ var BlockRenderer = class {
|
|
|
7794
7843
|
pendingViews = /* @__PURE__ */ new Map();
|
|
7795
7844
|
/** Views whose promise rejected or resolved to false: their source shows. */
|
|
7796
7845
|
failedViews = /* @__PURE__ */ new Set();
|
|
7846
|
+
/** Per cached or failed view, the blocks that showed it, to render again when `dropViews` forgets it. */
|
|
7847
|
+
viewBlocks = /* @__PURE__ */ new Map();
|
|
7797
7848
|
/** The block being rendered, so a pending view knows which blocks to render again. */
|
|
7798
7849
|
renderingBlock = "";
|
|
7799
7850
|
/** During a render, the loaded images of the block elements it replaces, by `src`. */
|
|
@@ -7808,6 +7859,7 @@ var BlockRenderer = class {
|
|
|
7808
7859
|
decorateCode: this.decorateCode,
|
|
7809
7860
|
views: this.views,
|
|
7810
7861
|
view: this.views && ((syntax, source, kind) => this.renderView(syntax, source, kind)),
|
|
7862
|
+
blockView: this.blockViews.length > 0 ? (block3) => this.renderBlockView(block3) : void 0,
|
|
7811
7863
|
markdown: this.markdown,
|
|
7812
7864
|
classes: this.classes,
|
|
7813
7865
|
icon: this.icon,
|
|
@@ -7819,9 +7871,12 @@ var BlockRenderer = class {
|
|
|
7819
7871
|
}
|
|
7820
7872
|
renderView(syntax, source, kind) {
|
|
7821
7873
|
const view = this.views?.get(syntax);
|
|
7822
|
-
if (!view) return null;
|
|
7874
|
+
if (!view || !this.viewContext) return null;
|
|
7823
7875
|
const key = `${kind}\0${syntax}\0${source}`;
|
|
7824
|
-
if (this.failedViews.has(key))
|
|
7876
|
+
if (this.failedViews.has(key)) {
|
|
7877
|
+
this.noteViewBlock(key);
|
|
7878
|
+
return null;
|
|
7879
|
+
}
|
|
7825
7880
|
let el = this.viewCache.get(key);
|
|
7826
7881
|
if (el) {
|
|
7827
7882
|
this.viewCache.delete(key);
|
|
@@ -7829,38 +7884,125 @@ var BlockRenderer = class {
|
|
|
7829
7884
|
el = this.root.ownerDocument.createElement(kind === "inline" ? "span" : "div");
|
|
7830
7885
|
let result;
|
|
7831
7886
|
try {
|
|
7832
|
-
result = view(source, el, kind);
|
|
7887
|
+
result = view(source, el, kind, this.viewContext);
|
|
7833
7888
|
} catch {
|
|
7889
|
+
result = false;
|
|
7890
|
+
}
|
|
7891
|
+
if (result === false) {
|
|
7892
|
+
this.noteViewBlock(key);
|
|
7834
7893
|
return null;
|
|
7835
7894
|
}
|
|
7836
|
-
if (result === false) return null;
|
|
7837
7895
|
if (result && typeof result.then === "function") this.awaitView(key, result);
|
|
7838
|
-
if (this.viewCache.size >= 500)
|
|
7896
|
+
if (this.viewCache.size >= 500) {
|
|
7897
|
+
const oldest = this.viewCache.keys().next().value;
|
|
7898
|
+
this.viewCache.delete(oldest);
|
|
7899
|
+
this.viewBlocks.delete(oldest);
|
|
7900
|
+
}
|
|
7839
7901
|
}
|
|
7840
7902
|
this.pendingViews.get(key)?.add(this.renderingBlock);
|
|
7903
|
+
this.noteViewBlock(key);
|
|
7841
7904
|
this.viewCache.set(key, el);
|
|
7842
7905
|
return el.cloneNode(true);
|
|
7843
7906
|
}
|
|
7907
|
+
/** Whether a block view draws `block`: its blocks show their editable form only while the selection is in them. */
|
|
7908
|
+
isHosted(block3) {
|
|
7909
|
+
return this.blockViews.some(({ view }) => view.match(block3));
|
|
7910
|
+
}
|
|
7911
|
+
/**
|
|
7912
|
+
* The element a block view drew `block` into: the one from the last render
|
|
7913
|
+
* while the block object is the same, else a fresh one from the first
|
|
7914
|
+
* matching view that does not decline. Null when none draws it.
|
|
7915
|
+
*/
|
|
7916
|
+
renderBlockView(block3) {
|
|
7917
|
+
const prev = this.hosted.get(block3.id);
|
|
7918
|
+
if (prev && prev.block === block3) return prev.el;
|
|
7919
|
+
if (prev) this.disposeHosted(block3.id);
|
|
7920
|
+
const ctx = this.viewContext;
|
|
7921
|
+
if (!ctx) return null;
|
|
7922
|
+
let matched = null;
|
|
7923
|
+
for (const { name, view } of this.blockViews) {
|
|
7924
|
+
if (!view.match(block3)) continue;
|
|
7925
|
+
matched = name;
|
|
7926
|
+
const el = this.root.ownerDocument.createElement("div");
|
|
7927
|
+
let result;
|
|
7928
|
+
try {
|
|
7929
|
+
result = view.render(block3, el, ctx);
|
|
7930
|
+
} catch {
|
|
7931
|
+
result = false;
|
|
7932
|
+
}
|
|
7933
|
+
if (result === false) continue;
|
|
7934
|
+
this.hosted.set(block3.id, { block: block3, name, el, dispose: typeof result === "function" ? result : void 0 });
|
|
7935
|
+
return el;
|
|
7936
|
+
}
|
|
7937
|
+
if (matched !== null) this.hosted.set(block3.id, { block: block3, name: matched, el: null });
|
|
7938
|
+
return null;
|
|
7939
|
+
}
|
|
7940
|
+
disposeHosted(id) {
|
|
7941
|
+
const entry = this.hosted.get(id);
|
|
7942
|
+
if (!entry) return;
|
|
7943
|
+
this.hosted.delete(id);
|
|
7944
|
+
try {
|
|
7945
|
+
entry.dispose?.();
|
|
7946
|
+
} catch {
|
|
7947
|
+
}
|
|
7948
|
+
}
|
|
7949
|
+
/** Lets every block view's element go: on destroy. */
|
|
7950
|
+
destroy() {
|
|
7951
|
+
for (const id of [...this.hosted.keys()]) this.disposeHosted(id);
|
|
7952
|
+
}
|
|
7953
|
+
noteViewBlock(key) {
|
|
7954
|
+
let blocks = this.viewBlocks.get(key);
|
|
7955
|
+
if (!blocks) {
|
|
7956
|
+
if (this.viewBlocks.size >= 1e3) this.viewBlocks.delete(this.viewBlocks.keys().next().value);
|
|
7957
|
+
this.viewBlocks.set(key, blocks = /* @__PURE__ */ new Set());
|
|
7958
|
+
}
|
|
7959
|
+
blocks.add(this.renderingBlock);
|
|
7960
|
+
}
|
|
7844
7961
|
/**
|
|
7845
7962
|
* A view that renders later (a diagram, a lazily loaded renderer) shows what
|
|
7846
7963
|
* it filled in at once, and its blocks render again when it settles.
|
|
7847
7964
|
*/
|
|
7848
7965
|
awaitView(key, result) {
|
|
7849
|
-
|
|
7966
|
+
const blocks = /* @__PURE__ */ new Set();
|
|
7967
|
+
this.pendingViews.set(key, blocks);
|
|
7850
7968
|
const settle = (ok) => {
|
|
7851
|
-
|
|
7969
|
+
if (this.pendingViews.get(key) !== blocks) return;
|
|
7852
7970
|
this.pendingViews.delete(key);
|
|
7853
7971
|
if (!ok) {
|
|
7854
7972
|
this.failedViews.add(key);
|
|
7855
7973
|
this.viewCache.delete(key);
|
|
7856
7974
|
}
|
|
7857
|
-
if (blocks
|
|
7975
|
+
if (blocks.size > 0) this.onViewSettled?.([...blocks]);
|
|
7858
7976
|
};
|
|
7859
7977
|
result.then(
|
|
7860
7978
|
(value) => settle(value !== false),
|
|
7861
7979
|
() => settle(false)
|
|
7862
7980
|
);
|
|
7863
7981
|
}
|
|
7982
|
+
/**
|
|
7983
|
+
* Forgets rendered and failed views, of one syntax or all, so the next
|
|
7984
|
+
* render runs the view again: for when what it shows depends on something
|
|
7985
|
+
* besides its source that changed, such as the editor's font. Returns the
|
|
7986
|
+
* rendered blocks that showed them, for the caller to render again.
|
|
7987
|
+
*/
|
|
7988
|
+
dropViews(syntax) {
|
|
7989
|
+
const matches2 = (key) => syntax === void 0 || key.split("\0", 3)[1] === syntax;
|
|
7990
|
+
for (const key of [...this.viewCache.keys()]) if (matches2(key)) this.viewCache.delete(key);
|
|
7991
|
+
for (const key of [...this.failedViews]) if (matches2(key)) this.failedViews.delete(key);
|
|
7992
|
+
for (const key of [...this.pendingViews.keys()]) if (matches2(key)) this.pendingViews.delete(key);
|
|
7993
|
+
const ids = /* @__PURE__ */ new Set();
|
|
7994
|
+
for (const [key, blocks] of [...this.viewBlocks]) {
|
|
7995
|
+
if (!matches2(key)) continue;
|
|
7996
|
+
this.viewBlocks.delete(key);
|
|
7997
|
+
for (const id of blocks) if (this.rendered.has(id)) ids.add(id);
|
|
7998
|
+
}
|
|
7999
|
+
for (const [id, entry] of [...this.hosted]) {
|
|
8000
|
+
if (syntax !== void 0 && entry.name !== syntax) continue;
|
|
8001
|
+
this.disposeHosted(id);
|
|
8002
|
+
if (this.rendered.has(id)) ids.add(id);
|
|
8003
|
+
}
|
|
8004
|
+
return [...ids];
|
|
8005
|
+
}
|
|
7864
8006
|
/**
|
|
7865
8007
|
* `force` re-renders every block. `lazy` (with a full render) lets blocks
|
|
7866
8008
|
* skip layout while off screen; use it only when loading a document.
|
|
@@ -7897,6 +8039,7 @@ var BlockRenderer = class {
|
|
|
7897
8039
|
next.set(block3.id, { block: block3, el, reveal });
|
|
7898
8040
|
}
|
|
7899
8041
|
this.loadedImages.clear();
|
|
8042
|
+
for (const [id, entry] of [...this.hosted]) if (next.get(id)?.block !== entry.block) this.disposeHosted(id);
|
|
7900
8043
|
const nodes = [];
|
|
7901
8044
|
for (const [id, { el }] of next) {
|
|
7902
8045
|
nodes.push(el);
|
|
@@ -8423,7 +8566,7 @@ function setIcon(button, editor, name) {
|
|
|
8423
8566
|
}
|
|
8424
8567
|
|
|
8425
8568
|
// src/view/reveal.ts
|
|
8426
|
-
function revealedViews(state, views, active) {
|
|
8569
|
+
function revealedViews(state, views, active, hosted) {
|
|
8427
8570
|
const out = /* @__PURE__ */ new Map();
|
|
8428
8571
|
if (!active) return out;
|
|
8429
8572
|
const { anchor, focus } = state.selection;
|
|
@@ -8434,6 +8577,7 @@ function revealedViews(state, views, active) {
|
|
|
8434
8577
|
[anchor, anchorBlock]
|
|
8435
8578
|
]) {
|
|
8436
8579
|
const keys = out.get(block3.id) ?? /* @__PURE__ */ new Set();
|
|
8580
|
+
if (hosted?.(block3)) keys.add(REVEAL_BLOCK);
|
|
8437
8581
|
if (block3.type === "raw") {
|
|
8438
8582
|
if (views.has(block3.syntax)) keys.add(REVEAL_BLOCK);
|
|
8439
8583
|
} else if (block3.type === "code") {
|
|
@@ -8455,6 +8599,11 @@ function sameRevealed(a, b) {
|
|
|
8455
8599
|
return a.size === b.size && [...a].every(([id, keys]) => sameReveal(keys, b.get(id)));
|
|
8456
8600
|
}
|
|
8457
8601
|
function viewEntry(block3, forward) {
|
|
8602
|
+
if (block3.type === "table") {
|
|
8603
|
+
const row = forward ? 0 : block3.rows.length - 1;
|
|
8604
|
+
return { blockId: block3.id, offset: forward ? 0 : inlineLength(block3.rows[row][0]), cell: { row, col: 0 } };
|
|
8605
|
+
}
|
|
8606
|
+
if (block3.type !== "raw" && block3.type !== "code") return forward ? startOf(block3) : endOf(block3);
|
|
8458
8607
|
const text = inlineText(block3.content);
|
|
8459
8608
|
if (block3.type === "code" || block3.syntax === "markdown") return { blockId: block3.id, offset: forward ? 0 : text.length };
|
|
8460
8609
|
if (forward) {
|
|
@@ -8543,6 +8692,7 @@ var SIZE_VARS = {
|
|
|
8543
8692
|
radius: "--mbe-radius",
|
|
8544
8693
|
blockGap: "--mbe-block-gap"
|
|
8545
8694
|
};
|
|
8695
|
+
var FONT_PROPERTIES = [SIZE_VARS.font, SIZE_VARS.mono, SIZE_VARS.fontSize, SIZE_VARS.lineHeight];
|
|
8546
8696
|
function tokenProperties(tokens) {
|
|
8547
8697
|
const props = /* @__PURE__ */ new Map();
|
|
8548
8698
|
if (!tokens) return props;
|
|
@@ -8904,6 +9054,8 @@ var Editor = class {
|
|
|
8904
9054
|
return els.length ? els : void 0;
|
|
8905
9055
|
};
|
|
8906
9056
|
this.renderer.views = new Map([["markdown", MARKDOWN_VIEW], ...this.extensions.flatMap((e) => Object.entries(e.views ?? {}))]);
|
|
9057
|
+
this.renderer.viewContext = { editor: this };
|
|
9058
|
+
this.renderer.blockViews = this.extensions.flatMap((e) => e.blockView ? [{ name: e.name, view: e.blockView }] : []);
|
|
8907
9059
|
this.renderer.markdown = this.markdown;
|
|
8908
9060
|
this.renderer.onViewSettled = (ids) => this.viewsSettled(ids);
|
|
8909
9061
|
const codeDecorators = this.extensions.flatMap((e) => e.codeDecorations ? [e.codeDecorations] : []);
|
|
@@ -9379,8 +9531,9 @@ var Editor = class {
|
|
|
9379
9531
|
if ("onLinkClick" in options) this.runtime = { ...this.runtime, onLinkClick: options.onLinkClick };
|
|
9380
9532
|
if ("theme" in options && this.applyTheme(options.theme)) this.paint(true);
|
|
9381
9533
|
}
|
|
9382
|
-
/** Sets the theme. Returns true when
|
|
9534
|
+
/** Sets the theme. Returns true when every block must re-render, for new classes. */
|
|
9383
9535
|
applyTheme(theme) {
|
|
9536
|
+
const before = tokenProperties(this.runtime.theme?.tokens);
|
|
9384
9537
|
this.runtime = { ...this.runtime, theme };
|
|
9385
9538
|
const style = this.root.style;
|
|
9386
9539
|
const props = tokenProperties(theme?.tokens);
|
|
@@ -9389,6 +9542,7 @@ var Editor = class {
|
|
|
9389
9542
|
this.tokenNames = [...props.keys()];
|
|
9390
9543
|
const scheme = theme?.colorScheme ?? "auto";
|
|
9391
9544
|
style.colorScheme = scheme === "auto" ? "" : scheme;
|
|
9545
|
+
if (FONT_PROPERTIES.some((name) => before.get(name) !== props.get(name))) this.renderBlocksAgain(this.renderer.dropViews());
|
|
9392
9546
|
const classes2 = mergeClasses([...this.extensions.map((e) => e.theme?.classes).reverse(), theme?.classes]);
|
|
9393
9547
|
const key = JSON.stringify(classes2);
|
|
9394
9548
|
if (key === this.classesKey) return false;
|
|
@@ -9444,6 +9598,7 @@ var Editor = class {
|
|
|
9444
9598
|
const selection = clampSelection(s.doc, { anchor: range.from, focus: range.to });
|
|
9445
9599
|
return insertText({ ...s, selection, storedMarks: null }, text);
|
|
9446
9600
|
}),
|
|
9601
|
+
insertToken: (text) => !this.composing && run((s) => insertToken(s, text)),
|
|
9447
9602
|
insertMarkdown: (markdown, options = {}) => {
|
|
9448
9603
|
const fragment = parseMarkdown(markdown, this.markdown).blocks;
|
|
9449
9604
|
const at = options.at;
|
|
@@ -9638,6 +9793,7 @@ var Editor = class {
|
|
|
9638
9793
|
this.blockHandle.destroy();
|
|
9639
9794
|
this.linkCard?.destroy();
|
|
9640
9795
|
this.resizeHandles.destroy();
|
|
9796
|
+
this.renderer.destroy();
|
|
9641
9797
|
setPopupContainer(this.root, void 0);
|
|
9642
9798
|
for (const name of Object.keys(this.runtime.attributes ?? {})) this.root.removeAttribute(name);
|
|
9643
9799
|
for (const name of this.tokenNames) this.root.style.removeProperty(name);
|
|
@@ -9882,7 +10038,7 @@ var Editor = class {
|
|
|
9882
10038
|
*/
|
|
9883
10039
|
paint(force = false, lazy = false, hadFocus = this.hasFocus()) {
|
|
9884
10040
|
const views = this.renderer.views;
|
|
9885
|
-
if (views && !this.pointerDown && !this.composing) this.renderer.revealed = revealedViews(this.state, views, !this.readOnly && hadFocus);
|
|
10041
|
+
if (views && !this.pointerDown && !this.composing) this.renderer.revealed = revealedViews(this.state, views, !this.readOnly && hadFocus, this.hosted);
|
|
9886
10042
|
this.renderer.render(this.state.doc, force, lazy);
|
|
9887
10043
|
this.applyHeadingIds();
|
|
9888
10044
|
this.applyReadOnly();
|
|
@@ -9925,6 +10081,30 @@ var Editor = class {
|
|
|
9925
10081
|
this.paint(true);
|
|
9926
10082
|
this.afterRender();
|
|
9927
10083
|
}
|
|
10084
|
+
/**
|
|
10085
|
+
* Renders every view (`Extension.views`) again, or those of one syntax
|
|
10086
|
+
* (`'math'`, `'code:mermaid'`), for when what a view shows depends on
|
|
10087
|
+
* something besides its source that changed: a renderer that finished
|
|
10088
|
+
* loading, or the font a diagram measured its text with. A view that
|
|
10089
|
+
* failed is tried again. Only the blocks that show those views render
|
|
10090
|
+
* again. The editor calls it itself when the theme's font tokens change.
|
|
10091
|
+
* Waits for an input method to finish composing.
|
|
10092
|
+
*/
|
|
10093
|
+
refreshViews(syntax) {
|
|
10094
|
+
if (this.destroyed) return;
|
|
10095
|
+
this.renderBlocksAgain(this.renderer.dropViews(syntax));
|
|
10096
|
+
}
|
|
10097
|
+
/** Renders the given blocks again now, or when the composition in progress settles. */
|
|
10098
|
+
renderBlocksAgain(ids) {
|
|
10099
|
+
if (ids.length === 0) return;
|
|
10100
|
+
if (this.composing || this.composition) {
|
|
10101
|
+
for (const id of ids) this.unsettled.add(id);
|
|
10102
|
+
return;
|
|
10103
|
+
}
|
|
10104
|
+
for (const id of ids) this.renderer.invalidate(id);
|
|
10105
|
+
this.paint();
|
|
10106
|
+
this.afterRender();
|
|
10107
|
+
}
|
|
9928
10108
|
/**
|
|
9929
10109
|
* Views that returned a promise settled: their blocks render again on the
|
|
9930
10110
|
* next frame, or when a composition settles, since rendering would end it.
|
|
@@ -9943,18 +10123,20 @@ var Editor = class {
|
|
|
9943
10123
|
this.afterRender();
|
|
9944
10124
|
});
|
|
9945
10125
|
}
|
|
9946
|
-
/** A block rendered by a view (a formula block, a diagram), or null. */
|
|
10126
|
+
/** A block rendered by a view (a formula block, a diagram, a block an extension's block view draws), or null. */
|
|
9947
10127
|
viewBlock(block3) {
|
|
9948
|
-
if (!block3
|
|
10128
|
+
if (!block3) return null;
|
|
9949
10129
|
return this.renderer.elementFor(block3.id)?.classList.contains("mbe-view-block") ? block3 : null;
|
|
9950
10130
|
}
|
|
9951
10131
|
/** Re-renders if the views that show their source changed: after the selection, focus or editability changes. */
|
|
9952
10132
|
refreshRevealed() {
|
|
9953
10133
|
const views = this.renderer.views;
|
|
9954
10134
|
if (!views || this.pointerDown) return;
|
|
9955
|
-
const next = revealedViews(this.state, views, !this.readOnly && this.hasFocus());
|
|
10135
|
+
const next = revealedViews(this.state, views, !this.readOnly && this.hasFocus(), this.hosted);
|
|
9956
10136
|
if (!sameRevealed(next, this.renderer.revealed)) this.paint();
|
|
9957
10137
|
}
|
|
10138
|
+
/** Whether an extension's block view draws `block` (see `BlockView`). */
|
|
10139
|
+
hosted = (block3) => this.renderer.isHosted(block3);
|
|
9958
10140
|
onMouseUp() {
|
|
9959
10141
|
if (!this.pointerDown) return;
|
|
9960
10142
|
this.pointerDown = false;
|
|
@@ -9985,11 +10167,7 @@ var Editor = class {
|
|
|
9985
10167
|
const content = contentElement(this.renderer.elementFor(current.id));
|
|
9986
10168
|
if (content && !this.onEdgeLine(content, forward)) return null;
|
|
9987
10169
|
}
|
|
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);
|
|
10170
|
+
return viewEntry(next, forward);
|
|
9993
10171
|
}
|
|
9994
10172
|
/**
|
|
9995
10173
|
* An arrow key next to an image block: from the edge of the block before
|
|
@@ -10084,7 +10262,7 @@ var Editor = class {
|
|
|
10084
10262
|
/** 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
10263
|
revealView(view) {
|
|
10086
10264
|
const block3 = view.hasAttribute(BLOCK_ATTR) ? getBlock(this.state.doc, view.getAttribute(BLOCK_ATTR)) : null;
|
|
10087
|
-
const pos = block3 ?
|
|
10265
|
+
const pos = block3 ? viewEntry(block3, false) : domToPosition(this.root, view.parentNode, childIndex(view) + 1);
|
|
10088
10266
|
if (!pos) return;
|
|
10089
10267
|
this.root.focus({ preventScroll: true });
|
|
10090
10268
|
this.setSelection({ anchor: pos, focus: pos });
|
|
@@ -11709,7 +11887,7 @@ function footnotes(options = {}) {
|
|
|
11709
11887
|
return {
|
|
11710
11888
|
name: "footnotes",
|
|
11711
11889
|
markdown: { inline: [inline2], block: [block2] },
|
|
11712
|
-
views: { footnote: (source, element, kind) => (kind === "inline" ? renderReference : renderDefinition)(source, element, kind) },
|
|
11890
|
+
views: { footnote: (source, element, kind, ctx) => (kind === "inline" ? renderReference : renderDefinition)(source, element, kind, ctx) },
|
|
11713
11891
|
onCreate: ({ editor }) => {
|
|
11714
11892
|
const onClick = (e) => {
|
|
11715
11893
|
const ref = e.target.closest?.(".mbe-footnote-ref");
|
|
@@ -11856,7 +12034,7 @@ function* scanMentions(text, from = 0, to = text.length) {
|
|
|
11856
12034
|
if (end > i + 1) i = end - 1;
|
|
11857
12035
|
}
|
|
11858
12036
|
}
|
|
11859
|
-
function decorate2(scan, cls, attributes) {
|
|
12037
|
+
function decorate2(scan, cls, attributes, mark) {
|
|
11860
12038
|
return ({ text, content }) => {
|
|
11861
12039
|
const out = [];
|
|
11862
12040
|
let pos = 0;
|
|
@@ -11869,7 +12047,8 @@ function decorate2(scan, cls, attributes) {
|
|
|
11869
12047
|
if (to <= from) continue;
|
|
11870
12048
|
const match = text.slice(from, to);
|
|
11871
12049
|
const className = typeof cls === "function" ? cls(match) : cls;
|
|
11872
|
-
const
|
|
12050
|
+
const given = typeof attributes === "function" ? attributes(match) : attributes;
|
|
12051
|
+
const attrs = mark ? { ...given, [mark]: match } : given;
|
|
11873
12052
|
out.push(attrs ? { from, to, class: className, attributes: attrs } : { from, to, class: className });
|
|
11874
12053
|
}
|
|
11875
12054
|
}
|
|
@@ -11881,18 +12060,36 @@ function decorate2(scan, cls, attributes) {
|
|
|
11881
12060
|
function escapeSyntax(ch) {
|
|
11882
12061
|
return { name: "escape", triggers: "\\", match: (text, pos) => text[pos + 1] === ch ? pos + 2 : -1 };
|
|
11883
12062
|
}
|
|
11884
|
-
function build(name, char, scan, defaultClass, options) {
|
|
11885
|
-
|
|
12063
|
+
function build(name, char, scan, defaultClass, mark, options) {
|
|
12064
|
+
const onClick = options.onClick;
|
|
12065
|
+
const extension = {
|
|
11886
12066
|
name,
|
|
11887
12067
|
markdown: { inline: [escapeSyntax(char)] },
|
|
11888
|
-
decorations: decorate2(options.scan ?? scan, options.class ?? defaultClass, options.attributes)
|
|
12068
|
+
decorations: decorate2(options.scan ?? scan, options.class ?? defaultClass, options.attributes, onClick ? mark : void 0)
|
|
12069
|
+
};
|
|
12070
|
+
if (!onClick) return extension;
|
|
12071
|
+
return {
|
|
12072
|
+
...extension,
|
|
12073
|
+
// A click on a match follows it as a click on a link does (see Editor.onClick): read-only, or Mod+click while editing.
|
|
12074
|
+
onCreate: ({ editor }) => {
|
|
12075
|
+
const handle = (e) => {
|
|
12076
|
+
const el = e.target.closest?.(`[${mark}]`);
|
|
12077
|
+
if (!el || !editor.root.contains(el) || editor.isEditable && !(isMac ? e.metaKey : e.ctrlKey)) return;
|
|
12078
|
+
if (onClick({ editor, match: el.getAttribute(mark), event: e })) {
|
|
12079
|
+
e.preventDefault();
|
|
12080
|
+
e.stopPropagation();
|
|
12081
|
+
}
|
|
12082
|
+
};
|
|
12083
|
+
editor.root.addEventListener("click", handle);
|
|
12084
|
+
return () => editor.root.removeEventListener("click", handle);
|
|
12085
|
+
}
|
|
11889
12086
|
};
|
|
11890
12087
|
}
|
|
11891
12088
|
function tags(options = {}) {
|
|
11892
|
-
return build("tags", "#", scanTags, "mbe-tag", options);
|
|
12089
|
+
return build("tags", "#", scanTags, "mbe-tag", "data-mbe-tag", options);
|
|
11893
12090
|
}
|
|
11894
12091
|
function mentions(options = {}) {
|
|
11895
|
-
return build("mentions", "@", scanMentions, "mbe-mention", options);
|
|
12092
|
+
return build("mentions", "@", scanMentions, "mbe-mention", "data-mbe-mention", options);
|
|
11896
12093
|
}
|
|
11897
12094
|
|
|
11898
12095
|
// src/extensions/suggestion.ts
|