@usememos/editor 26.10.8 → 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.
@@ -47,19 +47,38 @@ export interface ImageNode {
47
47
  /** The image description; its plain text is the alt text. */
48
48
  readonly children: InlineNode[];
49
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.
50
+ * An alignment and a size written after the description, as in Obsidian:
51
+ * `![alt|320](src)`, `![alt|center](src)` or `![alt|center|320x200](src)`.
52
+ * The description still ends with them; see `imageSuffix`. Not part of
53
+ * CommonMark, so the spec HTML ignores them.
53
54
  */
55
+ readonly align?: ImageNodeAlign;
54
56
  readonly width?: number;
55
57
  readonly height?: number;
56
58
  }
59
+ export type ImageNodeAlign = 'left' | 'center' | 'right';
60
+ /** What an image description ends with: an alignment, a size, or both. */
61
+ export interface ImageSuffix {
62
+ readonly align?: ImageNodeAlign;
63
+ readonly width?: number;
64
+ readonly height?: number;
65
+ /** Where the suffix starts in the text. */
66
+ readonly index: number;
67
+ }
57
68
  /**
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 `|320` are text.
69
+ * The alignment and size at the end of an image description, written as
70
+ * text: `|center`, `|320`, `|320x200` or `|center|320`. The alignment comes
71
+ * first, as in Obsidian image plugins. A `|` escaped with a backslash does
72
+ * not start a suffix; neither does `|`, which is not a `|` in the source.
73
+ * With `source` false, `text` is plain text in which escapes are already gone.
61
74
  */
62
- export declare const IMAGE_SIZE: RegExp;
75
+ export declare function imageSuffix(text: string, source?: boolean): ImageSuffix | null;
76
+ /** The text of an image suffix: `|center|320x200`, or '' for none. A height without a width is not written. */
77
+ export declare function imageSuffixText(s: {
78
+ readonly align?: ImageNodeAlign;
79
+ readonly width?: number;
80
+ readonly height?: number;
81
+ }): string;
63
82
  export type BlockNode = ParagraphNode | HeadingNode | {
64
83
  readonly type: 'thematicBreak';
65
84
  } | BlockQuoteNode | ListNode | CodeBlockNode | HtmlBlockNode | RawNode | TableNode;
@@ -4,10 +4,18 @@ export type Mark = 'bold' | 'italic' | 'strike' | 'code' | 'html';
4
4
  export declare const MARK_ORDER: readonly Mark[];
5
5
  /** Character used as the text of an inline atom (an image). */
6
6
  export declare const ATOM_CHAR = "\uFFFC";
7
+ /** Where an image block sits in its column. Without one it sits at the start of the line. */
8
+ export type ImageAlign = 'left' | 'center' | 'right';
7
9
  export interface ImageAtom {
8
10
  readonly src: string;
9
11
  readonly alt: string;
10
12
  readonly title?: string;
13
+ /**
14
+ * Alignment, saved after the alt text and before the size:
15
+ * `![alt|center](src)` or `![alt|center|320](src)`. It shows only on an
16
+ * image block, an image alone in its paragraph (see `isImageBlock`).
17
+ */
18
+ readonly align?: ImageAlign;
11
19
  /**
12
20
  * Display width in CSS pixels, saved after the alt text as in Obsidian:
13
21
  * `![alt|320](src)`. Without it the image shows at its own size.
@@ -150,6 +158,15 @@ export declare function isTextBlock(block: Block): block is TextBlock;
150
158
  /** Blocks whose content is plain text: no marks, links or images, and Enter inserts a newline. */
151
159
  export declare function isPlainTextBlock(block: Block): block is CodeBlock | RawBlock;
152
160
  /** Inline content at a position's container: the block's content, or a table cell. Null for dividers. */
161
+ /**
162
+ * The offset of the image in an image block, or null when `block` is not one.
163
+ * An image block is a paragraph whose only content is one image, with no link
164
+ * or format, and at most spaces around it. Markdown renders such a paragraph
165
+ * as a lone image, so the editor gives it a block menu and alignment.
166
+ */
167
+ export declare function imageBlockOffset(block: Block): number | null;
168
+ /** True when `block` is an image block (see `imageBlockOffset`). */
169
+ export declare function isImageBlock(block: Block): boolean;
153
170
  export declare function containerContent(block: Block, cell?: Cell): Inline | null;
154
171
  /** Returns the block with new content at a container. */
155
172
  export declare function withContainerContent(block: Block, cell: Cell | undefined, content: Inline): Block;
@@ -7,9 +7,9 @@ export declare class BlockHandle {
7
7
  private readonly cleanup;
8
8
  constructor(editor: Editor);
9
9
  private onMove;
10
- /** Shows the handle next to a block. */
10
+ /** Shows the block handle next to a block. */
11
11
  showFor(blockId: string): void;
12
- /** True when `node` is the handle or inside it. */
12
+ /** True when `node` is the block handle or inside it. */
13
13
  contains(node: Node): boolean;
14
14
  hide(): void;
15
15
  private scheduleHide;
@@ -46,6 +46,15 @@ export declare class Popup {
46
46
  bottom: number;
47
47
  right?: number;
48
48
  }, placement?: Placement, gap?: number, align?: 'start' | 'end'): void;
49
+ /**
50
+ * Shows the popup beside `menu` (a menu's rectangle, viewport coordinates),
51
+ * with its point `offsetY` from its top level with `top`: on the right, or
52
+ * on the left when there is no room, as a submenu opens.
53
+ */
54
+ showBeside(menu: {
55
+ left: number;
56
+ right: number;
57
+ }, top: number, offsetY: number, gap?: number): void;
49
58
  hide(): void;
50
59
  destroy(): void;
51
60
  private copyTheme;
@@ -62,6 +71,14 @@ export interface MenuItem {
62
71
  /** Extra words that match when filtering. */
63
72
  aliases?: string[];
64
73
  danger?: boolean;
74
+ /** For a choice in a menu: true marks the current one with `Menu.checkIcon`. */
75
+ checked?: boolean;
76
+ /** A class for the label, such as one that previews a heading style. */
77
+ labelClass?: string;
78
+ /** `aria-keyshortcuts` of the row. */
79
+ keyShortcuts?: string;
80
+ /** Items of a submenu that opens beside the row, on hover, ArrowRight or Enter, instead of `run`. */
81
+ submenu?: MenuSection;
65
82
  run: () => void;
66
83
  }
67
84
  export interface MenuSection {
@@ -70,10 +87,19 @@ export interface MenuSection {
70
87
  }
71
88
  /** A list of options with a highlighted item, driven by the keyboard (`aria-activedescendant`) or the mouse. */
72
89
  export declare class Menu {
90
+ private readonly owner;
91
+ private readonly className;
73
92
  readonly popup: Popup;
74
93
  private items;
75
94
  private index;
76
95
  private readonly id;
96
+ /** The submenu of the item at `childIndex`, while it is open. */
97
+ private child;
98
+ private childIndex;
99
+ /** Shown after the label of a `checked` item. */
100
+ checkIcon: Icon | undefined;
101
+ /** Shown at the end of a row that opens a submenu. */
102
+ submenuIcon: Icon | undefined;
77
103
  /** Called after an item runs. */
78
104
  onPicked: (() => void) | null;
79
105
  /** Called when the highlight moves, by key, by mouse or because the items changed. */
@@ -82,6 +108,14 @@ export declare class Menu {
82
108
  get el(): HTMLElement;
83
109
  get isOpen(): boolean;
84
110
  get activeDescendant(): string | null;
111
+ /** The open submenu, if any. */
112
+ get submenu(): Menu | null;
113
+ /** True if `node` is in this menu or its submenu. */
114
+ contains(node: Node): boolean;
115
+ /** Opens the submenu of the item at `i` (default: the highlighted one) and highlights its checked or first item. Returns false when it has none. */
116
+ openSubmenu(i?: number): boolean;
117
+ /** Closes the submenu, keeping its item highlighted. */
118
+ closeSubmenu(): void;
85
119
  setSections(sections: MenuSection[], emptyLabel?: string): void;
86
120
  /** Replaces the items with a line of text that is not an option, such as "Loading…". */
87
121
  setMessage(text: string, className: string): void;
@@ -0,0 +1,33 @@
1
+ import type { Editor } from '../editor.js';
2
+ export declare class ResizeHandles {
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
+ /** The image under the pointer, which shows handles while it is hovered. */
8
+ private hovered;
9
+ /** The selected image, which shows handles. */
10
+ private selected;
11
+ /** The image the frame is shown for. */
12
+ private framed;
13
+ private drag;
14
+ private hideTimer;
15
+ private readonly cleanup;
16
+ constructor(editor: Editor);
17
+ /** True when `node` is the frame or one of its resize handles. */
18
+ contains(node: Node): boolean;
19
+ /** Shows the handles for the selected image, or for none. */
20
+ update(selected: ImageTarget | null): void;
21
+ /** Moves the resize handles to where their image is now, after scrolling. */
22
+ reposition(): void;
23
+ hide(): void;
24
+ destroy(): void;
25
+ private onOver;
26
+ private scheduleUnhover;
27
+ private cancelUnhover;
28
+ /** Shows the handles over the hovered image, else the selected one. Only images that loaded can be resized. */
29
+ private placeFrame;
30
+ private startDrag;
31
+ private moveDrag;
32
+ private endDrag;
33
+ }
@@ -1,18 +1,59 @@
1
1
  import type { Editor } from '../editor.js';
2
+ import { Menu } from './popup.js';
3
+ /** A control of the formatting toolbar (the `toolbar` option's `items`). */
4
+ export type ToolbarItem = 'textStyle' | 'bold' | 'italic' | 'strike' | 'inlineCode' | 'link' | 'quote' | 'codeBlock' | 'list';
5
+ /** Every control, in order: the default `items`. */
6
+ export declare const TOOLBAR_ITEMS: readonly ToolbarItem[];
2
7
  export declare class FormatToolbar {
3
8
  private readonly editor;
4
9
  private readonly popup;
5
- private readonly markButtons;
6
- private readonly linkButton;
7
- constructor(editor: Editor);
10
+ private readonly tooltip;
11
+ private readonly buttons;
12
+ private readonly separators;
13
+ private textStyle;
14
+ private list;
15
+ private openTrigger;
16
+ private tooltipTimer;
17
+ private tooltipHiddenAt;
18
+ /** Called when a menu opens, closes or moves its highlight, so the editor can update `aria-activedescendant`. */
19
+ onMenuChange: (() => void) | null;
20
+ constructor(editor: Editor, items?: readonly ToolbarItem[]);
8
21
  get isOpen(): boolean;
22
+ /** The open menu of the text style or list button, if any. */
23
+ get menu(): Menu | null;
24
+ /** True if `node` is in the toolbar or one of its menus. */
25
+ contains(node: Node): boolean;
9
26
  show(): void;
10
27
  hide(): void;
28
+ /** Closes the open menu and leaves the toolbar open. */
29
+ closeMenu(): void;
11
30
  destroy(): void;
31
+ /** Applies a command to the selection and updates the buttons' states. */
32
+ private apply;
12
33
  private update;
34
+ private setTriggerIcon;
35
+ private toggleMenu;
36
+ /** Puts the menu next to its button, on the side of the toolbar away from the selection. */
37
+ private placeMenu;
38
+ private scheduleTooltip;
39
+ private hideTooltip;
40
+ }
41
+ /** What an InputPopover edits: a link URL or an image's alt text. */
42
+ export interface InputPopoverOptions {
43
+ readonly value: string;
44
+ /** The input's accessible name. */
45
+ readonly label: string;
46
+ readonly placeholder: string;
47
+ readonly type: 'url' | 'text';
48
+ /** Text of a button that applies an empty value, shown when the value is not empty. Without it there is no button. */
49
+ readonly removeLabel?: string;
13
50
  }
14
- /** A small input for a link URL. Enter applies, Escape cancels, an empty value removes the link. */
15
- export declare class LinkPopover {
51
+ /**
52
+ * A small input over the editor: the link popover (Mod+K) and an image's
53
+ * alt text. Enter applies the trimmed value, Escape cancels, and the remove
54
+ * button applies an empty value.
55
+ */
56
+ export declare class InputPopover {
16
57
  private readonly popup;
17
58
  private readonly input;
18
59
  private readonly removeButton;
@@ -20,7 +61,7 @@ export declare class LinkPopover {
20
61
  constructor(editor: Editor);
21
62
  get isOpen(): boolean;
22
63
  contains(node: Node): boolean;
23
- open(rect: DOMRect, value: string, done: (href: string | null) => void): void;
64
+ open(rect: DOMRect, options: InputPopoverOptions, done: (value: string | null) => void): void;
24
65
  /** Closes without changing anything and without refocusing the editor. */
25
66
  private close;
26
67
  private finish;
@@ -41,6 +41,8 @@ export interface RenderContext {
41
41
  readonly codeCopy?: boolean;
42
42
  /** An image failed to load and its element was replaced by the unavailable state. */
43
43
  readonly onImageError?: () => void;
44
+ /** A loaded image element with this `src` from the elements being replaced, to show again instead of a new one. */
45
+ readonly loadedImage?: (src: string) => HTMLImageElement | undefined;
44
46
  }
45
47
  /**
46
48
  * Which views of one block show their source: `REVEAL_BLOCK` for a whole raw
@@ -109,6 +111,8 @@ export declare class BlockRenderer {
109
111
  private readonly failedViews;
110
112
  /** The block being rendered, so a pending view knows which blocks to render again. */
111
113
  private renderingBlock;
114
+ /** During a render, the loaded images of the block elements it replaces, by `src`. */
115
+ private loadedImages;
112
116
  constructor(root: HTMLElement, labels: Labels);
113
117
  private context;
114
118
  private renderView;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@usememos/editor",
3
- "version": "26.10.8",
4
- "description": "A block editor for standard Markdown. Zero runtime dependencies, with an optional React component.",
3
+ "version": "26.10.10",
4
+ "description": "A block editor for Markdown. Zero runtime dependencies, with an optional React component.",
5
5
  "keywords": [
6
6
  "markdown",
7
7
  "editor",
@@ -1,50 +0,0 @@
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
- }