@usememos/editor 26.10.9 → 26.10.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # @usememos/editor
2
2
 
3
- A block editor for standard Markdown, for React or any web app.
3
+ A block editor for Markdown, for React or any web app.
4
4
 
5
5
  - Markdown in, Markdown out: CommonMark plus GFM tables, task lists, strikethrough and autolinks. Blocks you did not edit are saved byte for byte.
6
- - Block editing: slash menu, a formatting toolbar, Markdown shortcuts, and a block handle on tables and code blocks.
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
  - About 56 KB minified and gzipped, plus 3.5 KB of CSS: a third of what a comparable CodeMirror 6, Tiptap or Lexical setup adds (see [Performance](docs/architecture.md#performance)).
9
9
 
@@ -63,11 +63,11 @@ The same names work in `new Editor(options)` and as React props, except that Rea
63
63
  | `referrerPolicy` | `ReferrerPolicy` | `referrerpolicy` for images, such as `no-referrer`. Images always load lazily. |
64
64
  | `onLinkClick` | `({ editor, href, event }) => boolean` | Called when a link is clicked to be opened: a plain click while read-only, Mod+click while editing. Return `true` to handle it, for example to route an in-app link or scroll to an anchor; otherwise the editor opens `href` in a new tab. |
65
65
  | `theme` | `{ classes?, tokens?, colorScheme? }` | Your own classes on rendered elements, colors and fonts, and light or dark. See [Theming](#theming). |
66
- | `toolbar` | `boolean` | Shows the formatting toolbar on selection. Default `true`. |
66
+ | `toolbar` | `boolean \| { items? }` | Shows the formatting toolbar on selection. Default `true`. `items` lists its controls; they keep the built-in order: `textStyle`, `bold`, `italic`, `strike`, `inlineCode`, `link`, `quote`, `codeBlock`, `list` (exported as `TOOLBAR_ITEMS`). For example, `{ items: ['bold', 'italic', 'strike', 'inlineCode', 'link'] }` keeps it to inline formatting. |
67
67
  | `popupContainer` | `HTMLElement` | Where menus, the toolbar and the autocomplete popup are appended. Default: the nearest open `<dialog>` or `aria-modal` ancestor of `element`, else `document.body`. |
68
68
  | `slashMenu` | `false \| string[]` | Built-in slash menu items to show, in order, or `false` for none: `text`, `heading1`, `heading2`, `heading3`, `bulletList`, `numberedList`, `todoList`, `quote`, `code`, `table`, `divider`. |
69
69
  | `labels` | `Partial<Labels>` | Overrides UI strings, for localization. See `src/labels.ts`. |
70
- | `icons` | `Partial<Record<IconName, string \| Element>>` | Replaces built-in icons, which are [Lucide](https://lucide.dev) icons. A string is shown as text; an element, such as an `<svg>`, is cloned for every place it appears. Names match the `labels` keys of the same controls: the slash menu items, `duplicate`, `delete`, the table actions, `bold`, `italic`, `strike`, `inlineCode`, `link`, `blockHandle` and `imageUnavailable` (shown in place of an image that did not load). |
70
+ | `icons` | `Partial<Record<IconName, string \| Element>>` | Replaces built-in icons, which are [Lucide](https://lucide.dev) icons. A string is shown as text; an element, such as an `<svg>`, is cloned for every place it appears. Names match the `labels` keys of the same controls: the slash menu items, the block actions (`duplicate`, `delete`, the table cell actions, `altText` and `resetImageSize`), `bold`, `italic`, `strike`, `inlineCode`, `link`, `chevronDown` and `selected` (the formatting toolbar's menu buttons and their check mark), `chevronRight` (a menu row that opens a submenu), `imageAlignLeft`, `imageAlignCenter` and `imageAlignRight` (an image block's Align choices), `blockHandle` and `imageUnavailable` (shown in place of an image that did not load). |
71
71
  | `onChange` | `(markdown: string, editor) => void` | Called after document changes, at most once per animation frame. |
72
72
  | `onSubmit` | `(markdown: string, editor) => void` | Called on Mod+Enter or Ctrl+Enter with the current Markdown, for comment boxes and chat inputs. |
73
73
  | `onFocus`, `onBlur` | `(editor) => void` | Focus events. |
@@ -98,10 +98,10 @@ function Composer() {
98
98
  | `setMarkdown(markdown, { history? })` | Replaces the document. `history: 'clear'` (default) clears undo history; `'keep'` makes the replacement one undo step. Does not emit `change`. |
99
99
  | `isEmpty` | True when the document holds only empty paragraphs. |
100
100
  | `state` | The document and selection, immutable. |
101
- | `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`, `toggleCodeBlock({ language? })`, `setLink({ href })`, `unsetLink`, `updateImage({ alt?, title?, 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 or width, and a new width drops a height so the image keeps its aspect ratio. `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. |
101
+ | `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`, `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. `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. |
102
102
  | `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. |
103
103
  | `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`, `codeBlock`, `table`, `divider`, `raw`. `attrs` narrows it, for example `isActive('heading', { level: 2 })`. |
104
- | `getAttributes(name)` | `src`, `alt`, `title`, `width` and `height` 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. |
104
+ | `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. |
105
105
  | `selectedBlocks` | The blocks of a block selection, in document order, or an empty array when the selection is text. |
106
106
  | `getSelection()`, `setSelection(selection)` | Read or move the selection. `setSelection` also takes a `SelectionJSON`, and clamps one that does not fit the document. |
107
107
  | `selectionToJSON()` | The selection as JSON-safe data (block index and offset). It still fits after reloading the same Markdown, so an app can store the caret and restore it. |
@@ -286,15 +286,16 @@ To style the editor entirely with your own classes, import `@usememos/editor/bas
286
286
  | ArrowUp, ArrowDown, Enter, Tab, Escape | In an open autocomplete popup: move, pick, pick, close |
287
287
  | Mod+B, Mod+I, Mod+E | Bold, italic, inline code |
288
288
  | Mod+Shift+X, Mod+Shift+S | Strikethrough |
289
- | Mod+K | Link |
289
+ | Mod+K | Link, for selected text or the link at the caret. Otherwise the key is left to the app, for example for a command menu |
290
290
  | Mod+Alt+0, Mod+Alt+1–3 | Paragraph, heading 1–3 |
291
291
  | Mod+Alt+C | Code block |
292
292
  | Mod+Shift+7, 8, 9 | Numbered, bullet, task list |
293
- | Mod+/ | Block menu, in a table or code block |
293
+ | Mod+/ | Block menu, in a table, code block or image block |
294
294
  | Mod+Z, Mod+Shift+Z (Ctrl+Y) | Undo, redo |
295
295
  | Shift+Enter | Line break inside a block |
296
296
  | Tab, Shift+Tab | Indent and outdent list items; next and previous table cell |
297
297
  | Mod+Shift+V | Paste as plain text |
298
+ | Backspace right after a Markdown shortcut | Restore the typed characters, such as `**bold**` or `## ` |
298
299
  | Mod+Enter, Ctrl+Enter | `submit` |
299
300
  | Click an image | Select it. Backspace or Delete removes it, ArrowLeft and ArrowRight move past it, Enter starts a new paragraph after it, typing replaces it |
300
301
  | Escape | Close a menu or the toolbar; otherwise select the block with the caret (or every block a text selection crosses); in a block selection, leave the editor |
@@ -305,9 +306,11 @@ Markdown shortcuts at the start of a line: `#`–`######`, `-`, `*`, `+`, `1.`,
305
306
 
306
307
  ## Images
307
308
 
308
- While editing, clicking an image selects it, with an outline. Its side handles, shown on hover and on the selected image, resize it: the width follows the pointer up to the width of the line, keeps the aspect ratio, and snaps to the width of other images in the document when it comes within 8 pixels. Each resize is one undo step. An image that does not load, or whose URL `resolveUrl` dropped, shows an icon and its alt text (`labels.imageUnavailable` when it has none), and can still be selected and deleted. Read-only editors show images at their saved size, with none of these controls.
309
+ While editing, clicking an image selects it, with an outline. Its resize handles, shown on hover and on the selected image, resize it: the width follows the pointer up to the width of the line, keeps the aspect ratio, and snaps to the width of other images in the document when it comes within 8 pixels. Each resize is one undo step. An image that does not load, or whose URL `resolveUrl` dropped, shows an icon and its alt text (`labels.imageUnavailable` when it has none), and can still be selected and deleted. Read-only editors show images at their saved size and alignment, with none of these controls.
309
310
 
310
- The size is saved after the alt text, as in Obsidian: `![chart|320](chart.png)`, or `![chart|320x200](chart.png)` with a height. It is a whole number of CSS pixels, and the image never shows wider than the column. Other Markdown renderers show the image at its own size and read the alt text as `chart|320`. See [Images](docs/syntax.md#links-and-images) in the syntax doc.
311
+ An image alone in its paragraph is an image block. It has the block handle and the block menu, like a table. Its block actions are an Align submenu (Left, Center, Right); Alt text, which opens a field for the alt text (Enter saves it, Escape keeps the old text); for a resized image, Reset size, which removes the width; then Duplicate and Delete. The alignment and size stay in the alt text when it is edited. The caret never rests beside an image block: arrow keys and a click select the image, Backspace at the start of the next block selects it and a second Backspace deletes it, and typing does nothing. Enter adds a paragraph after it. A centered image grows by twice the drag, so the resize handle stays under the pointer.
312
+
313
+ The size and the alignment are saved after the alt text, as in Obsidian and its image plugins: `![chart|320](chart.png)`, `![chart|320x200](chart.png)` with a height, `![chart|center](chart.png)` or `![chart|center|320](chart.png)`. The size is a whole number of CSS pixels, and the image never shows wider than the column. The alignment applies only to image blocks. Other Markdown renderers show the image at its own size, on the left, and read the alt text as `chart|center|320`. See [Image size and alignment](docs/syntax.md#image-size-and-alignment) in the syntax doc.
311
314
 
312
315
  ## What saving changes
313
316
 
package/dist/base.css CHANGED
@@ -7,7 +7,7 @@
7
7
  * Block spacing lives here so classes added by a theme cannot change it:
8
8
  * these rules are more specific than a single utility class.
9
9
  * All selectors are scoped under .mbe-root (the editable area) and
10
- * .mbe-ui (menus, toolbar and handle, which live in document.body).
10
+ * .mbe-ui (menus, toolbar and block handle, which live in document.body).
11
11
  */
12
12
  .mbe-root,
13
13
  .mbe-ui {
@@ -100,6 +100,10 @@
100
100
  .mbe-root a.mbe-link { cursor: text; }
101
101
  .mbe-root.mbe-readonly a.mbe-link { cursor: pointer; }
102
102
  .mbe-root .mbe-image { max-width: 100%; height: auto; margin: 8px 0; vertical-align: bottom; }
103
+ /* An image alone in its paragraph moves within the column; it keeps its width. */
104
+ .mbe-root .mbe-image-block[data-align="left"] { text-align: left; }
105
+ .mbe-root .mbe-image-block[data-align="center"] { text-align: center; }
106
+ .mbe-root .mbe-image-block[data-align="right"] { text-align: right; }
103
107
  /* An image that did not load: an icon and its alt text, on one line. */
104
108
  .mbe-root .mbe-image-unavailable {
105
109
  display: inline-flex;
@@ -206,7 +210,7 @@
206
210
  .mbe-ui[hidden] { display: none !important; }
207
211
  .mbe-menu,
208
212
  .mbe-toolbar,
209
- .mbe-link-popover {
213
+ .mbe-input-popover {
210
214
  position: fixed;
211
215
  background: Canvas;
212
216
  color: CanvasText;
@@ -233,28 +237,46 @@
233
237
  .mbe-menu-empty,
234
238
  .mbe-menu-loading { padding: 6px 8px; }
235
239
 
236
- .mbe-toolbar { display: flex; align-items: center; gap: 2px; padding: 3px; }
240
+ /* Wider than the screen, the toolbar scrolls sideways; its max-width is set to fit. */
241
+ .mbe-toolbar { box-sizing: border-box; display: flex; align-items: center; gap: 2px; padding: 4px; overflow-x: auto; scrollbar-width: none; }
242
+ .mbe-toolbar::-webkit-scrollbar { display: none; }
237
243
  .mbe-tool {
244
+ flex: none;
238
245
  font: inherit;
239
246
  color: inherit;
240
247
  background: none;
241
248
  border: none;
242
- padding: 4px 6px;
243
- min-width: 28px;
244
- min-height: 28px;
249
+ padding: 4px 7px;
250
+ min-width: 32px;
251
+ min-height: 32px;
245
252
  display: inline-flex;
246
253
  align-items: center;
247
254
  justify-content: center;
248
255
  cursor: pointer;
249
256
  }
257
+ .mbe-tool[hidden],
258
+ .mbe-tool-sep[hidden] { display: none; }
259
+ .mbe-tool .mbe-icon { width: 18px; height: 18px; }
260
+ .mbe-tool-menu { gap: 1px; padding-inline: 7px 4px; }
261
+ .mbe-tool-icon,
262
+ .mbe-tool-chevron { display: inline-flex; }
263
+ .mbe-tool .mbe-tool-chevron .mbe-icon { width: 13px; height: 13px; }
250
264
  .mbe-tool.mbe-active { background: Highlight; color: HighlightText; }
251
- .mbe-tool-sep { width: 1px; align-self: stretch; margin: 3px 2px; background: GrayText; }
265
+ .mbe-tool-sep { flex: none; width: 1px; height: 18px; margin: 0 3px; background: GrayText; }
266
+ .mbe-menu-check { flex: none; display: inline-flex; margin-inline-start: auto; }
267
+ .mbe-menu-submenu { flex: none; display: inline-flex; margin-inline-start: auto; }
268
+ .mbe-menu-submenu .mbe-icon { width: 14px; height: 14px; }
269
+ .mbe-submenu { min-width: 160px; }
270
+ /* The row whose submenu is open stays marked while the pointer is in the submenu. */
271
+ .mbe-menu-item[aria-expanded="true"] { background: Highlight; color: HighlightText; }
272
+
273
+ .mbe-tooltip { position: fixed; pointer-events: none; white-space: nowrap; padding: 4px 8px; background: CanvasText; color: Canvas; }
252
274
 
253
- .mbe-link-popover { display: flex; gap: 4px; padding: 4px; }
275
+ .mbe-input-popover { display: flex; gap: 4px; padding: 4px; }
254
276
 
255
- /* The handles that resize an image: a frame over it that only its handles respond to. */
277
+ /* The resize handles of an image: a frame over it that only its resize handles respond to. */
256
278
  .mbe-image-frame { position: fixed; pointer-events: none; }
257
- .mbe-image-handle {
279
+ .mbe-resize-handle {
258
280
  position: absolute;
259
281
  top: 50%;
260
282
  width: 6px;
@@ -270,17 +292,17 @@
270
292
  border: 1px solid CanvasText;
271
293
  border-radius: 3px;
272
294
  }
273
- /* A larger area to grab than the handle shows. */
274
- .mbe-image-handle::before { content: ''; position: absolute; inset: -6px -8px; }
275
- .mbe-image-handle-left { left: 8px; }
276
- .mbe-image-handle-right { left: calc(100% - 8px); }
277
- .mbe-image-frame-small .mbe-image-handle-left { left: 0; }
278
- .mbe-image-frame-small .mbe-image-handle-right { left: 100%; }
295
+ /* A larger area to grab than the resize handle shows. */
296
+ .mbe-resize-handle::before { content: ''; position: absolute; inset: -6px -8px; }
297
+ .mbe-resize-handle-left { left: 8px; }
298
+ .mbe-resize-handle-right { left: calc(100% - 8px); }
299
+ .mbe-image-frame-small .mbe-resize-handle-left { left: 0; }
300
+ .mbe-image-frame-small .mbe-resize-handle-right { left: 100%; }
279
301
  .mbe-image-resizing,
280
- .mbe-image-resizing .mbe-image-handle { cursor: ew-resize; }
281
- .mbe-link-input { font: inherit; color: inherit; width: 260px; padding: 5px 8px; }
302
+ .mbe-image-resizing .mbe-resize-handle { cursor: ew-resize; }
303
+ .mbe-popover-input { font: inherit; color: inherit; width: 260px; padding: 5px 8px; }
282
304
 
283
- .mbe-handle {
305
+ .mbe-block-handle {
284
306
  position: fixed;
285
307
  width: 20px;
286
308
  height: 22px;