@usememos/editor 26.10.7 → 26.10.8

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/dist/style.css CHANGED
@@ -111,6 +111,31 @@
111
111
  margin: 8px 0;
112
112
  vertical-align: bottom;
113
113
  }
114
+ .mbe-root .mbe-image-unavailable {
115
+ display: inline-flex;
116
+ align-items: center;
117
+ gap: 6px;
118
+ box-sizing: border-box;
119
+ padding: 4px 8px;
120
+ border: 1px dashed GrayText;
121
+ }
122
+ .mbe-root .mbe-image-alt {
123
+ min-width: 0;
124
+ overflow: hidden;
125
+ text-overflow: ellipsis;
126
+ white-space: nowrap;
127
+ }
128
+ .mbe-root .mbe-image-selected {
129
+ outline: 2px solid Highlight;
130
+ outline-offset: 2px;
131
+ }
132
+ .mbe-root.mbe-image-selection,
133
+ .mbe-root.mbe-image-selection * {
134
+ caret-color: transparent;
135
+ }
136
+ .mbe-root.mbe-image-selection ::selection {
137
+ background: transparent;
138
+ }
114
139
  .mbe-root .mbe-code-bar {
115
140
  display: flex;
116
141
  align-items: center;
@@ -332,6 +357,47 @@
332
357
  gap: 4px;
333
358
  padding: 4px;
334
359
  }
360
+ .mbe-image-frame {
361
+ position: fixed;
362
+ pointer-events: none;
363
+ }
364
+ .mbe-image-handle {
365
+ position: absolute;
366
+ top: 50%;
367
+ width: 6px;
368
+ height: 40%;
369
+ min-height: 16px;
370
+ max-height: 48px;
371
+ box-sizing: border-box;
372
+ transform: translate(-50%, -50%);
373
+ pointer-events: auto;
374
+ touch-action: none;
375
+ cursor: ew-resize;
376
+ background: Canvas;
377
+ border: 1px solid CanvasText;
378
+ border-radius: 3px;
379
+ }
380
+ .mbe-image-handle::before {
381
+ content: "";
382
+ position: absolute;
383
+ inset: -6px -8px;
384
+ }
385
+ .mbe-image-handle-left {
386
+ left: 8px;
387
+ }
388
+ .mbe-image-handle-right {
389
+ left: calc(100% - 8px);
390
+ }
391
+ .mbe-image-frame-small .mbe-image-handle-left {
392
+ left: 0;
393
+ }
394
+ .mbe-image-frame-small .mbe-image-handle-right {
395
+ left: 100%;
396
+ }
397
+ .mbe-image-resizing,
398
+ .mbe-image-resizing .mbe-image-handle {
399
+ cursor: ew-resize;
400
+ }
335
401
  .mbe-link-input {
336
402
  font: inherit;
337
403
  color: inherit;
@@ -469,6 +535,16 @@
469
535
  .mbe-root .mbe-image {
470
536
  border-radius: 8px;
471
537
  }
538
+ .mbe-root .mbe-image-selected {
539
+ outline-color: var(--mbe-accent);
540
+ }
541
+ .mbe-root .mbe-image-unavailable {
542
+ border-color: var(--mbe-border);
543
+ background: var(--mbe-block-code-bg);
544
+ color: var(--mbe-muted);
545
+ font-size: 0.875em;
546
+ line-height: 20px;
547
+ }
472
548
  .mbe-root .mbe-footnote-ref {
473
549
  font-size: 0.75em;
474
550
  line-height: 0;
@@ -674,6 +750,15 @@
674
750
  .mbe-link-input:focus {
675
751
  border-color: var(--mbe-accent);
676
752
  }
753
+ .mbe-image-handle {
754
+ background: rgb(0 0 0 / 0.6);
755
+ border-color: rgb(255 255 255 / 0.85);
756
+ box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
757
+ }
758
+ .mbe-image-handle:hover,
759
+ .mbe-image-resizing .mbe-image-handle {
760
+ background: var(--mbe-accent);
761
+ }
677
762
  .mbe-handle {
678
763
  border-radius: 4px;
679
764
  color: var(--mbe-muted);
package/dist/theme.css CHANGED
@@ -93,6 +93,14 @@
93
93
  .mbe-root :is(.mbe-raw-html, .mbe-token) { color: var(--mbe-muted); }
94
94
  .mbe-root code { background: var(--mbe-code-bg); border-radius: 4px; padding: 2px 4px; }
95
95
  .mbe-root .mbe-image { border-radius: 8px; }
96
+ .mbe-root .mbe-image-selected { outline-color: var(--mbe-accent); }
97
+ .mbe-root .mbe-image-unavailable {
98
+ border-color: var(--mbe-border);
99
+ background: var(--mbe-block-code-bg);
100
+ color: var(--mbe-muted);
101
+ font-size: 0.875em;
102
+ line-height: 20px;
103
+ }
96
104
  /* Footnotes, rendered: a reference is a small raised label, a definition a small muted note. */
97
105
  .mbe-root .mbe-footnote-ref { font-size: 0.75em; line-height: 0; color: var(--mbe-link); }
98
106
  .mbe-root.mbe-readonly .mbe-footnote-ref { cursor: pointer; }
@@ -216,6 +224,14 @@
216
224
  }
217
225
  .mbe-link-input:focus { border-color: var(--mbe-accent); }
218
226
 
227
+ .mbe-image-handle {
228
+ background: rgb(0 0 0 / 0.6);
229
+ border-color: rgb(255 255 255 / 0.85);
230
+ box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
231
+ }
232
+ .mbe-image-handle:hover,
233
+ .mbe-image-resizing .mbe-image-handle { background: var(--mbe-accent); }
234
+
219
235
  .mbe-handle {
220
236
  border-radius: 4px;
221
237
  color: var(--mbe-muted);
@@ -1,4 +1,4 @@
1
- import { type Block, type EditorState, type HeadingLevel, type ListType, type Mark, type Position, type Selection, type TableBlock, type Align } from './model.js';
1
+ import { type Block, type EditorState, type HeadingLevel, type ImageAtom, type ListType, type Mark, type Position, type Selection, type TableBlock, type Align } from './model.js';
2
2
  import type { InlineSyntax } from './markdown/options.js';
3
3
  export type Command = (state: EditorState) => EditorState;
4
4
  /** A copy of a block with a new ID. */
@@ -42,6 +42,21 @@ export declare function activeLink(state: EditorState): {
42
42
  * collapsed selection inside a link, changes the whole link.
43
43
  */
44
44
  export declare function setLink(state: EditorState, href: string | null): EditorState;
45
+ /** The image the selection covers exactly, with the position before it. Null when the selection is anything else. */
46
+ export declare function selectedImage(state: EditorState): {
47
+ pos: Position;
48
+ image: ImageAtom;
49
+ } | null;
50
+ /** Selects the image after `pos` as one unit. */
51
+ export declare function selectImage(state: EditorState, pos: Position): EditorState;
52
+ /** What `updateImage` changes. `null` removes a title or a size; a new width drops the height, so the image keeps its aspect ratio. */
53
+ export interface ImageChange {
54
+ readonly alt?: string;
55
+ readonly title?: string | null;
56
+ readonly width?: number | null;
57
+ }
58
+ /** Changes the image after `pos` and selects it. */
59
+ export declare function updateImage(state: EditorState, pos: Position, change: ImageChange): EditorState;
45
60
  export type BlockTypeSpec = {
46
61
  type: 'paragraph';
47
62
  } | {
@@ -1,3 +1,4 @@
1
+ import * as cmd from './commands.js';
1
2
  import { type ChangeKind } from './history.js';
2
3
  import { type Labels } from './labels.js';
3
4
  import { type Icons } from './icons.js';
@@ -127,7 +128,9 @@ export interface SuggestionApi {
127
128
  accept(index?: number): boolean;
128
129
  }
129
130
  /** Names `isActive` and `getAttributes` understand. */
130
- export type ActiveName = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'paragraph' | 'heading' | 'bulletList' | 'orderedList' | 'taskList' | 'blockquote' | 'codeBlock' | 'table' | 'divider' | 'raw';
131
+ export type ActiveName = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'paragraph' | 'heading' | 'bulletList' | 'orderedList' | 'taskList' | 'blockquote' | 'codeBlock' | 'table' | 'divider' | 'raw'
132
+ /** The selection is exactly one image. */
133
+ | 'image';
131
134
  /** A position that survives reloading the same Markdown: a block index and an offset. */
132
135
  export interface PositionJSON {
133
136
  readonly block: number;
@@ -172,6 +175,11 @@ export interface EditorCommands {
172
175
  href: string;
173
176
  }): boolean;
174
177
  unsetLink(): boolean;
178
+ /**
179
+ * Changes the selected image (see `isActive('image')`): its alt text, its
180
+ * title, or its width in CSS pixels. `null` removes the title or the width.
181
+ */
182
+ updateImage(attrs: cmd.ImageChange): boolean;
175
183
  /** Inserts plain text at the selection, or in place of `range` (for example a suggestion's range). */
176
184
  insertText(text: string, options?: {
177
185
  range?: {
@@ -261,6 +269,9 @@ export declare class Editor {
261
269
  private readonly toolbar;
262
270
  private readonly linkPopover;
263
271
  private readonly handle;
272
+ private readonly imageControls;
273
+ /** The selected image's element, which carries the selected class. */
274
+ private selectedImageEl;
264
275
  private hintEl;
265
276
  /**
266
277
  * The block selection, by block ID, or null for a text selection. While it
@@ -298,7 +309,7 @@ export declare class Editor {
298
309
  get isDestroyed(): boolean;
299
310
  /** True if `name` applies at the selection: a mark on the selected text, or the type of the block with the caret. */
300
311
  isActive(name: ActiveName, attrs?: Readonly<Record<string, unknown>>): boolean;
301
- /** Attributes of `name` at the selection: `href` for a link, `level` for a heading, `language` for a code block, `checked` for a task, `syntax` for a raw block. */
312
+ /** Attributes of `name` at the selection: `src`, `alt`, `title`, `width` and `height` for a selected image, `href` for a link, `level` for a heading, `language` for a code block, `checked` for a task, `syntax` for a raw block. */
302
313
  getAttributes(name: ActiveName): Record<string, unknown>;
303
314
  getSelection(): Selection;
304
315
  /**
@@ -544,6 +555,10 @@ export declare class Editor {
544
555
  /** A press anywhere but the block menu or the block handle closes the menu, in the editor's text too. */
545
556
  private onDocumentPointerDown;
546
557
  private updateUi;
558
+ /** Marks the selected image's element, and hides the browser's selection highlight over it. */
559
+ private paintSelectedImage;
560
+ /** The selected image of `state` and its element, or null. */
561
+ private imageTargetAt;
547
562
  private syncActiveDescendant;
548
563
  private hideUi;
549
564
  /** Wraps an input event handler so the changes it makes keep the caret in view. */
@@ -117,6 +117,13 @@ declare const ICONS: {
117
117
  inlineCode: string[];
118
118
  /** `link` */
119
119
  link: string[];
120
+ /** `image-off` */
121
+ imageUnavailable: (string | [string, {
122
+ x1: string;
123
+ x2: string;
124
+ y1: string;
125
+ y2: string;
126
+ }])[];
120
127
  /** `grip-vertical` */
121
128
  blockHandle: [string, {
122
129
  cx: string;
@@ -49,5 +49,12 @@ export declare const DEFAULT_LABELS: {
49
49
  alignCenter: string;
50
50
  alignRight: string;
51
51
  tableSection: string;
52
+ /** Shown in place of an image that did not load and has no alt text. */
53
+ imageUnavailable: string;
54
+ /** The button over a selected image that edits its alt text, and the field's name. */
55
+ altText: string;
56
+ altTextPlaceholder: string;
57
+ /** Accessible name of the handles that resize an image. */
58
+ resizeImage: string;
52
59
  };
53
60
  export type Labels = typeof DEFAULT_LABELS;
@@ -46,7 +46,20 @@ export interface ImageNode {
46
46
  readonly title: string;
47
47
  /** The image description; its plain text is the alt text. */
48
48
  readonly children: InlineNode[];
49
+ /**
50
+ * A size written after the description, as in Obsidian: `![alt|320](src)`
51
+ * or `![alt|320x200](src)`. The description still ends with it; see
52
+ * `IMAGE_SIZE`. Not part of CommonMark, so the spec HTML ignores it.
53
+ */
54
+ readonly width?: number;
55
+ readonly height?: number;
49
56
  }
57
+ /**
58
+ * The size at the end of an image description, as its plain text ends: `|320`
59
+ * or `|320x200`, whole CSS pixels from 1 to 99999. In the source the `|` must
60
+ * not be escaped; `\|320` and `&#124;320` are text.
61
+ */
62
+ export declare const IMAGE_SIZE: RegExp;
50
63
  export type BlockNode = ParagraphNode | HeadingNode | {
51
64
  readonly type: 'thematicBreak';
52
65
  } | BlockQuoteNode | ListNode | CodeBlockNode | HtmlBlockNode | RawNode | TableNode;
@@ -1,5 +1,5 @@
1
1
  import type { Inline } from '../model.js';
2
- import type { InlineNode } from './ast.js';
2
+ import { type InlineNode } from './ast.js';
3
3
  import { type InlineSyntax, type MarkdownOptions } from './options.js';
4
4
  /** Link reference definitions, keyed by normalized label. */
5
5
  export type RefMap = Map<string, {
@@ -8,6 +8,13 @@ export interface ImageAtom {
8
8
  readonly src: string;
9
9
  readonly alt: string;
10
10
  readonly title?: string;
11
+ /**
12
+ * Display width in CSS pixels, saved after the alt text as in Obsidian:
13
+ * `![alt|320](src)`. Without it the image shows at its own size.
14
+ */
15
+ readonly width?: number;
16
+ /** Display height in CSS pixels, saved as `![alt|320x200](src)`. Only read when a width is given. */
17
+ readonly height?: number;
11
18
  }
12
19
  /** A run of text that shares one format. A hard line break is a "\n" in the text. */
13
20
  export interface Span {
@@ -0,0 +1,50 @@
1
+ import type { Editor } from '../editor.js';
2
+ export declare class ImageControls {
3
+ private readonly editor;
4
+ /** A frame over the image, holding the two handles. It takes no pointer events itself. */
5
+ private readonly frame;
6
+ private readonly handles;
7
+ private readonly toolbar;
8
+ private readonly altPopover;
9
+ private readonly altInput;
10
+ /** The image under the pointer, which shows handles while it is hovered. */
11
+ private hovered;
12
+ /** The selected image, which shows handles and the toolbar. */
13
+ private selected;
14
+ /** The image the frame is shown for. */
15
+ private framed;
16
+ private drag;
17
+ private altDone;
18
+ private hideTimer;
19
+ private readonly cleanup;
20
+ constructor(editor: Editor);
21
+ /** True while the alt text field is open. */
22
+ get isEditingAlt(): boolean;
23
+ /** True while an image is being resized. */
24
+ get isResizing(): boolean;
25
+ /** True when `node` is part of these controls. */
26
+ contains(node: Node): boolean;
27
+ /**
28
+ * Shows the controls for the selected image, or hides them for none. The
29
+ * toolbar shows only when `toolbar` is true (the editor is focused and not
30
+ * busy with something else).
31
+ */
32
+ update(selected: ImageTarget | null, toolbar: boolean): void;
33
+ /** Moves the controls to where their image is now, after scrolling. */
34
+ reposition(): void;
35
+ hide(): void;
36
+ destroy(): void;
37
+ /** The toolbar goes inside the image's top corner, as in Notion, or above an image too small to hold it. */
38
+ private showToolbar;
39
+ private onOver;
40
+ private scheduleUnhover;
41
+ private cancelUnhover;
42
+ /** Shows the handles over the hovered image, else the selected one. Only images that loaded can be resized. */
43
+ private placeFrame;
44
+ private startDrag;
45
+ private moveDrag;
46
+ private endDrag;
47
+ private openAlt;
48
+ private closeAlt;
49
+ private finishAlt;
50
+ }
@@ -39,6 +39,8 @@ export interface RenderContext {
39
39
  readonly icon?: (name: IconName) => Node | null;
40
40
  /** Code blocks get a copy button. */
41
41
  readonly codeCopy?: boolean;
42
+ /** An image failed to load and its element was replaced by the unavailable state. */
43
+ readonly onImageError?: () => void;
42
44
  }
43
45
  /**
44
46
  * Which views of one block show their source: `REVEAL_BLOCK` for a whole raw
@@ -92,6 +94,8 @@ export declare class BlockRenderer {
92
94
  icon?: (name: IconName) => Node | null;
93
95
  /** Code blocks get a copy button. */
94
96
  codeCopy: boolean;
97
+ /** An image failed to load and now shows as unavailable. */
98
+ onImageError?: () => void;
95
99
  /** Per block, the views that show their source. A block re-renders when its entry changes. */
96
100
  revealed: ReadonlyMap<string, BlockReveal>;
97
101
  /**
@@ -134,6 +138,8 @@ export declare function childIndex(node: Node): number;
134
138
  /** Plain text of a content element as the browser currently has it, with atoms as one character. */
135
139
  export declare function domText(contentEl: HTMLElement): string;
136
140
  export declare function domToPosition(root: HTMLElement, node: Node, offset: number): Position | null;
141
+ /** The atom (an image or a rendered token) that starts at `pos`, or null. */
142
+ export declare function atomAt(blockEl: HTMLElement, pos: Position): HTMLElement | null;
137
143
  export declare function positionToDom(blockEl: HTMLElement, pos: Position): {
138
144
  node: Node;
139
145
  offset: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usememos/editor",
3
- "version": "26.10.7",
3
+ "version": "26.10.8",
4
4
  "description": "A block editor for standard Markdown. Zero runtime dependencies, with an optional React component.",
5
5
  "keywords": [
6
6
  "markdown",