@usememos/editor 26.10.15 → 26.10.16
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 +41 -3
- package/dist/base.css +6 -2
- package/dist/index.js +792 -187
- package/dist/index.js.map +4 -4
- package/dist/react.js +2 -2
- package/dist/react.js.map +2 -2
- package/dist/style.css +11 -3
- package/dist/theme.css +2 -1
- package/dist/types/clipboard/fromHtml.d.ts +7 -0
- package/dist/types/editor.d.ts +28 -1
- package/dist/types/extensions/tags.d.ts +13 -10
- package/dist/types/history.d.ts +11 -2
- package/dist/types/index.d.ts +1 -0
- package/dist/types/markdown/autolink.d.ts +14 -6
- package/dist/types/markdown/lower.d.ts +4 -3
- package/dist/types/model.d.ts +19 -2
- package/dist/types/react/index.d.ts +2 -2
- package/dist/types/sourceMode.d.ts +71 -0
- package/dist/types/view/dom.d.ts +4 -0
- package/dist/types/widgets.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ A block editor for Markdown, for React or any web app.
|
|
|
6
6
|
- Block editing: slash menu, a formatting toolbar, Markdown shortcuts, and a block handle on tables, code blocks and images that stand alone, which opens the block menu.
|
|
7
7
|
- One package, `@usememos/editor`, with zero runtime dependencies. It works without a framework, and `@usememos/editor/react` is a thin React wrapper.
|
|
8
8
|
|
|
9
|
-
Status: early releases, published to npm as [`@usememos/editor`](https://www.npmjs.com/package/@usememos/editor). Versions follow memos' CalVer (`26.10`, `26.10.1`) and do not signal breaking changes, so pin an exact version. [docs/syntax.md](docs/syntax.md) specifies the Markdown it reads and writes, and [docs/architecture.md](docs/architecture.md) explains how it works.
|
|
9
|
+
Status: early releases, published to npm as [`@usememos/editor`](https://www.npmjs.com/package/@usememos/editor). Versions follow memos' CalVer (`26.10.0`, `26.10.1`) and do not signal breaking changes, so pin an exact version. [docs/syntax.md](docs/syntax.md) specifies the Markdown it reads and writes, and [docs/architecture.md](docs/architecture.md) explains how it works.
|
|
10
10
|
|
|
11
11
|
## Use it
|
|
12
12
|
|
|
@@ -54,6 +54,7 @@ The same names work in `new Editor(options)` and as React props, except that Rea
|
|
|
54
54
|
| `placeholder` | `string` | Shown when the document is empty. |
|
|
55
55
|
| `editable` | `boolean` | Default `true`. With `false`, no editing, and links open on click. |
|
|
56
56
|
| `taskToggle` | `boolean` | Default `false`. While `editable` is `false`, lets the user tick and untick task items, for example for a memo's owner. Each tick emits `change` with Markdown in which only that item's `[ ]` or `[x]` changed. Ignored while `editable` is `true`. |
|
|
57
|
+
| `mode` | `'rich' \| 'source'` | Default `'rich'`. `'source'` shows the whole document as its Markdown text, for people who prefer to edit the text itself. Switching changes no text, emits no `change` and keeps undo history. See [Source mode](#source-mode). |
|
|
57
58
|
| `autofocus` | `boolean \| 'start' \| 'end'` | Focuses the editor on creation, with the caret at the start or the end. |
|
|
58
59
|
| `attributes` | `Record<string, string>` | Attributes for the editable root, such as `autocorrect`, `autocapitalize` or `aria-label`. |
|
|
59
60
|
| `resolveUrl` | `(url, kind) => string \| null` | The URL to use for a link (`kind` `'link'`) or an image (`'image'`), or `null` to drop it: the link is not clickable, the image has no `src`. The saved Markdown keeps the original. Default `defaultResolveUrl`, exported: relative URLs, `http`, `https`, `mailto` and `tel`. Call it from your own policy to extend it, for example to allow `sms:` or to block `http:` images. |
|
|
@@ -76,9 +77,9 @@ The same names work in `new Editor(options)` and as React props, except that Rea
|
|
|
76
77
|
|
|
77
78
|
For note-style Markdown, as in the playground, use `markdown: { breaks: true, setext: false }` and `extensions: [math(), footnotes(), tags(), mentions()]`.
|
|
78
79
|
|
|
79
|
-
`editor.setOptions({ placeholder, editable, taskToggle, 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.
|
|
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.
|
|
80
81
|
|
|
81
|
-
In React, `placeholder`, `editable`, `taskToggle`, `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. 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
83
|
|
|
83
84
|
For apps that manage the editor instance themselves, `useEditor(options, deps)` creates one and `<EditorContent editor={editor} />` shows it:
|
|
84
85
|
|
|
@@ -116,6 +117,7 @@ function Composer() {
|
|
|
116
117
|
| `root` | The editable element: `element`, or for `useEditor`, the element `<EditorContent>` shows. |
|
|
117
118
|
| `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. |
|
|
118
119
|
| `destroy()`, `isDestroyed` | Removes the editor's content, listeners and popups from the page. After it, every call that would change the editor does nothing and commands return `false`, so a late callback (an upload that finishes after the editor closed) is safe. `getMarkdown()` still returns the last Markdown. |
|
|
120
|
+
| `mode` | `'rich'` or `'source'`: the mode the editor shows now. See [Source mode](#source-mode). |
|
|
119
121
|
| `hasFocus()`, `focus()`, `blur()`, `isEditable`, `setOptions(options)` | |
|
|
120
122
|
|
|
121
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.
|
|
@@ -317,6 +319,8 @@ To style the editor entirely with your own classes, import `@usememos/editor/bas
|
|
|
317
319
|
|
|
318
320
|
With blocks selected: ArrowUp and ArrowDown select the block above or below, Shift extends the selection, Mod+A selects every block, Backspace and Delete delete them, Mod+C, Mod+X and paste copy, cut and replace them as whole blocks, Mod+D duplicates them, and the block type keys, Tab and the mark keys apply to all of them. Enter, ArrowLeft and ArrowRight go back to editing text. Typing does nothing. See [Block selection](docs/architecture.md#block-selection).
|
|
319
321
|
|
|
322
|
+
In source mode the formatting keys do nothing and Enter inserts a line break; see [Source mode](#source-mode).
|
|
323
|
+
|
|
320
324
|
Markdown shortcuts at the start of a line: `#`–`######`, `-`, `*`, `+`, `1.`, `[ ]`, `[x]`, `>`, `[!note]` and the other callout types, `>>>` (collapsible section), ` ``` `, `---`. Inline: `**bold**`, `*italic*`, `_italic_`, `` `code` ``, `~~strike~~`.
|
|
321
325
|
|
|
322
326
|
## Images
|
|
@@ -358,6 +362,40 @@ Make one with Collapsible section in the slash menu, Mod+Shift+6, or `>>> ` at t
|
|
|
358
362
|
|
|
359
363
|
Only a `<details>` written the way the editor saves it loads as a section: the opening tag and a `<summary>` of plain text, a blank line, Markdown, and `</details>` at the start of a line. Other `<details>`, such as one on a single line, stay raw HTML.
|
|
360
364
|
|
|
365
|
+
## Source mode
|
|
366
|
+
|
|
367
|
+
With `mode: 'source'`, the editor shows the whole document as Markdown text, as Obsidian's source mode and GitLab's plain text editor do. The editor has no button or key for it: the app switches with `setOptions({ mode })` or the React `mode` prop, and remembers the choice if it wants to.
|
|
368
|
+
|
|
369
|
+
```tsx
|
|
370
|
+
<MarkdownEditor value={markdown} onChange={setMarkdown} mode={showSource ? 'source' : 'rich'} />
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
**Switching** is a change of view, not an edit. The text shown in source mode is exactly `getMarkdown()`. Going back to rich mode loads the text as `setMarkdown` does, so `getMarkdown()` returns it byte for byte, including lines edited in source mode. A switch emits no `change`, adds no undo step and does not move focus. Undo and redo work across it: undoing an edit made in the other mode restores it in the current mode. The caret stays in the same block, and keeps its column when the text before it in that block has no Markdown syntax; otherwise it goes to the start of the block's text. A switch requested while an input method is composing waits for the composition to end.
|
|
374
|
+
|
|
375
|
+
**What works in source mode.** Source mode is plain text editing, like a `<textarea>` or a code block. It has no autocomplete and no formatting keys.
|
|
376
|
+
|
|
377
|
+
| | |
|
|
378
|
+
| --- | --- |
|
|
379
|
+
| Typing, deleting, input methods, spellcheck | As in a code block |
|
|
380
|
+
| Enter | Inserts a line break |
|
|
381
|
+
| Tab, Shift+Tab | As in a code block: Tab inserts two spaces, or indents the selected lines by two spaces. Shift+Tab removes up to two spaces (or a tab) from the start of those lines |
|
|
382
|
+
| Paste | The clipboard's plain text (`text/plain`). Files go to `fileHandler` as in rich mode |
|
|
383
|
+
| Copy and cut | The selected text, as `text/plain` |
|
|
384
|
+
| Mod+Enter, undo, redo, Mod+A | As in rich mode |
|
|
385
|
+
| Escape | Leaves the editor |
|
|
386
|
+
| Formatting keys (Mod+B, Mod+I, Mod+E, Mod+Shift+X, Mod+Alt+0–4, Mod+Alt+C, Mod+Shift+6–9) | Do nothing. Mod+K is left to the app |
|
|
387
|
+
| Markdown shortcuts, autolinks | Do not run: the text is the Markdown |
|
|
388
|
+
| Autocomplete | Off. `suggestion()` popups, the slash menu and Ctrl+Space do not open |
|
|
389
|
+
| `fileHandler`, `handlePaste`, `handleDrop`, extension key bindings | As in rich mode |
|
|
390
|
+
| `commands.*` | `insertText` and `insertMarkdown` insert the text as written; `insertMarkdown` with `block: true` puts it on lines of its own. `indent`, `outdent`, `undo`, `redo`, `selectAll`, `focus`, `blur` and `scrollIntoView` work. The mark, block type, link, image, table, divider, callout, section and `selectBlocks` commands return `false` |
|
|
391
|
+
| Placeholder | Shown when the text is empty |
|
|
392
|
+
| `addWidget` | Shown below the text. A widget remembers its place in the text, which moves with edits before it, and `insertMarkdown(md, { at: widget })` inserts there |
|
|
393
|
+
| `editable: false` | The text is read-only and can be selected and copied. `taskToggle` has no effect |
|
|
394
|
+
|
|
395
|
+
The toolbar, slash menu, autocomplete popup, block handle, block menu, link card, rendered views, decorations, images and syntax highlighting do not show. `isActive()` returns `false` and `getAttributes()` returns `{}`.
|
|
396
|
+
|
|
397
|
+
**Positions.** In source mode the document is one block that holds all the text, so a `Position` names that block and its `offset` counts UTF-16 code units of the Markdown. `getSelection`, `setSelection`, `selectionToJSON` and `insertText({ range })` use that shape. A selection saved in the other mode is clamped.
|
|
398
|
+
|
|
361
399
|
## What saving changes
|
|
362
400
|
|
|
363
401
|
Blocks you did not edit are saved exactly as they were loaded, byte for byte, including the blank lines between them. `getMarkdown()` right after loading returns the input unchanged.
|
package/dist/base.css
CHANGED
|
@@ -234,6 +234,8 @@
|
|
|
234
234
|
}
|
|
235
235
|
:is(.mbe-root, .mbe-flow) > .mbe-code pre,
|
|
236
236
|
:is(.mbe-root, .mbe-flow) > pre.mbe-raw { white-space: pre-wrap; tab-size: 2; }
|
|
237
|
+
/* Source mode: the document as plain text in the editor's font. */
|
|
238
|
+
.mbe-root > .mbe-source { white-space: pre-wrap; tab-size: 4; overflow-wrap: anywhere; }
|
|
237
239
|
:is(.mbe-root, .mbe-flow) > .mbe-code pre { margin: 0; }
|
|
238
240
|
:is(.mbe-root, .mbe-flow) > .mbe-code pre code { display: block; }
|
|
239
241
|
|
|
@@ -291,12 +293,14 @@
|
|
|
291
293
|
.mbe-root > .mbe-paragraph.mbe-empty.mbe-show-hint::before,
|
|
292
294
|
.mbe-root:not(.mbe-readonly) > .mbe-empty[data-hint]:is(h1, h2, h3, h4, h5, h6, blockquote:not(.mbe-list-item))::before,
|
|
293
295
|
.mbe-root:not(.mbe-readonly) > .mbe-list-item.mbe-empty[data-hint] > .mbe-content::before,
|
|
294
|
-
.mbe-root:not(.mbe-readonly) > .mbe-details.mbe-empty[data-hint] > .mbe-content::before
|
|
296
|
+
.mbe-root:not(.mbe-readonly) > .mbe-details.mbe-empty[data-hint] > .mbe-content::before,
|
|
297
|
+
.mbe-root > .mbe-source.mbe-empty:only-child > .mbe-source-text::before {
|
|
295
298
|
opacity: 0.5;
|
|
296
299
|
pointer-events: none;
|
|
297
300
|
position: absolute;
|
|
298
301
|
}
|
|
299
|
-
.mbe-root > .mbe-paragraph.mbe-empty:only-child::before
|
|
302
|
+
.mbe-root > .mbe-paragraph.mbe-empty:only-child::before,
|
|
303
|
+
.mbe-root > .mbe-source.mbe-empty:only-child > .mbe-source-text::before { content: var(--mbe-placeholder, ''); }
|
|
300
304
|
.mbe-root > .mbe-paragraph.mbe-empty.mbe-show-hint::before { content: var(--mbe-slash-hint, ''); }
|
|
301
305
|
.mbe-root:not(.mbe-readonly) > .mbe-empty[data-hint]:is(h1, h2, h3, h4, h5, h6, blockquote:not(.mbe-list-item))::before,
|
|
302
306
|
.mbe-root:not(.mbe-readonly) > .mbe-list-item.mbe-empty[data-hint] > .mbe-content::before,
|