@usememos/editor 26.10.14 → 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.
@@ -30,6 +30,8 @@ export interface Span {
30
30
  /** Sorted by MARK_ORDER, no duplicates. */
31
31
  readonly marks: readonly Mark[];
32
32
  readonly link?: string;
33
+ /** The link's title, shown as a tooltip and saved as `[text](url "title")`. Only with `link`. */
34
+ readonly linkTitle?: string;
33
35
  /** An image. Its `text` is a single ATOM_CHAR. */
34
36
  readonly image?: ImageAtom;
35
37
  /**
@@ -43,6 +45,7 @@ export type Inline = readonly Span[];
43
45
  export interface Format {
44
46
  readonly marks: readonly Mark[];
45
47
  readonly link?: string;
48
+ readonly linkTitle?: string;
46
49
  readonly token?: string;
47
50
  }
48
51
  export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
@@ -70,20 +73,31 @@ export interface ListItemBlock {
70
73
  readonly checked: boolean;
71
74
  /** Ordered items only: the number of the first item of a list. Ignored on later items. */
72
75
  readonly start?: number;
76
+ /** The item is inside a blockquote, with the quote blocks and quoted items around it (see `quoteKind`). */
77
+ readonly quote?: true;
78
+ /** Quoted items only: the item is inside a callout of this type. */
79
+ readonly callout?: CalloutType;
80
+ /** Quoted items only: the item starts a new blockquote (see `QuoteBlock.quoteStart`). */
81
+ readonly quoteStart?: true;
73
82
  readonly content: Inline;
74
83
  }
75
84
  /** The kinds of callout, as in GitHub's alert syntax (`> [!NOTE]`). */
76
85
  export declare const CALLOUT_TYPES: readonly ["note", "tip", "important", "warning", "caution"];
77
86
  export type CalloutType = (typeof CALLOUT_TYPES)[number];
78
87
  /**
79
- * One paragraph of a block quote. Adjacent quotes with the same `callout`
80
- * form one blockquote.
88
+ * One paragraph of a block quote. Adjacent quotes and quoted list items with
89
+ * the same `callout` form one blockquote (see `quoteKind`).
81
90
  */
82
91
  export interface QuoteBlock {
83
92
  readonly id: string;
84
93
  readonly type: 'quote';
85
94
  /** Set on every paragraph of a callout, written as a GitHub alert (`> [!NOTE]`). */
86
95
  readonly callout?: CalloutType;
96
+ /**
97
+ * The block starts a new blockquote, though the block before it is in a
98
+ * blockquote of the same kind: in Markdown, a blank line between them.
99
+ */
100
+ readonly quoteStart?: true;
87
101
  readonly content: Inline;
88
102
  }
89
103
  /** Code block. Content is plain text (no marks); lines are separated by "\n". */
@@ -165,11 +179,13 @@ export interface EditorState {
165
179
  export declare function createId(): string;
166
180
  export declare function paragraph(content?: Inline, id?: string): ParagraphBlock;
167
181
  export declare function heading(level: HeadingLevel, content?: Inline, id?: string): HeadingBlock;
182
+ /** `quote` puts the item inside a blockquote: `'quote'` for a plain quote, or a callout's type. */
168
183
  export declare function listItem(listType: ListType, content?: Inline, opts?: {
169
184
  indent?: number;
170
185
  checked?: boolean;
171
186
  start?: number;
172
187
  id?: string;
188
+ quote?: QuoteKind | null;
173
189
  }): ListItemBlock;
174
190
  export declare function quote(content?: Inline, id?: string, callout?: CalloutType): QuoteBlock;
175
191
  export declare function codeBlock(language?: string, text?: string, id?: string): CodeBlock;
@@ -186,6 +202,22 @@ export declare function details(content?: Inline, opts?: {
186
202
  }): DetailsBlock;
187
203
  export declare function detailsEnd(id?: string): DetailsEndBlock;
188
204
  export declare function plain(text: string): Span[];
205
+ /**
206
+ * Which blockquote a block is part of: `'quote'` for a plain quote, or the
207
+ * type of a callout. Quote blocks and quoted list items are the only blocks
208
+ * in quotes; adjacent ones of the same kind form one blockquote.
209
+ */
210
+ export type QuoteKind = 'quote' | CalloutType;
211
+ /** The blockquote a block is part of, or null for a block outside quotes. */
212
+ export declare function quoteKind(block: Block | undefined): QuoteKind | null;
213
+ /** True if `a` and `b` are parts of one blockquote when they are neighbours (`b` after `a`). */
214
+ export declare function sameQuote(a: Block | undefined, b: Block | undefined): boolean;
215
+ /** `to` with the `quoteStart` of `from`, when both are quoted: a block that changes type keeps its quote boundary. */
216
+ export declare function keepQuoteStart<B extends Block>(from: Block, to: B): B;
217
+ /** A list item moved into a blockquote of `kind`, or out of any with null. Keeps its ID. */
218
+ export declare function withQuote(item: ListItemBlock, kind: QuoteKind | null): ListItemBlock;
219
+ /** A block as it is inside its blockquote: a quote block is a paragraph there, and a quoted list item a plain one. */
220
+ export declare function unquote(block: Block): Block;
189
221
  export declare function isTextBlock(block: Block): block is TextBlock;
190
222
  /** Blocks with no text and no cells: dividers and the ends of collapsible sections. */
191
223
  export declare function isTextless(block: Block): block is DividerBlock | DetailsEndBlock;
@@ -212,6 +244,8 @@ export declare function withContent<B extends TextBlock>(block: B, content: Inli
212
244
  export declare function toPlain(inline: Inline): Span[];
213
245
  export declare function sortMarks(marks: Iterable<Mark>): Mark[];
214
246
  export declare function sameMarks(a: readonly Mark[], b: readonly Mark[]): boolean;
247
+ /** Whether two spans are in the same link, or both in none: the same destination and title. */
248
+ export declare function sameLink(a: Pick<Span, 'link' | 'linkTitle'>, b: Pick<Span, 'link' | 'linkTitle'> | undefined): boolean;
215
249
  export declare function inlineText(inline: Inline): string;
216
250
  export declare function inlineLength(inline: Inline): number;
217
251
  export declare function normalizeInline(inline: Inline): Span[];
@@ -231,7 +265,10 @@ export declare function rangeHasMark(inline: Inline, from: number, to: number, m
231
265
  export declare function setMarkInRange(inline: Inline, from: number, to: number, mark: Mark, on: boolean): Span[];
232
266
  /** Makes [from, to) one token of an inline syntax extension. */
233
267
  export declare function setTokenInRange(inline: Inline, from: number, to: number, token: string): Span[];
234
- /** Sets (or with `href` null, removes) a link on [from, to). */
268
+ /**
269
+ * Sets (or with `href` null, removes) a link on [from, to). A new link keeps
270
+ * the title of the first link in the range, so changing a link's URL keeps it.
271
+ */
235
272
  export declare function setLinkInRange(inline: Inline, from: number, to: number, href: string | null): Span[];
236
273
  /** Extension tokens that touch an offset, edges included, with their start offsets. At most two. */
237
274
  export declare function tokensAt(inline: Inline, offset: number): {
@@ -2,7 +2,7 @@ import { Editor, type EditorOptions, type SuggestionState } from '@usememos/edit
2
2
  import { type CSSProperties } from 'react';
3
3
  export type { Editor, SuggestionState } from '@usememos/editor';
4
4
  /** Constructor options that the React components take as props. */
5
- type EditorProps = Pick<EditorOptions, 'markdown' | 'extensions' | 'placeholder' | 'editable' | 'taskToggle' | 'autofocus' | 'attributes' | 'resolveUrl' | 'referrerPolicy' | 'headingIds' | 'onLinkClick' | 'theme' | 'toolbar' | 'linkCard' | 'slashMenu' | 'slashHint' | 'labels' | 'icons'>;
5
+ type EditorProps = Pick<EditorOptions, 'markdown' | 'extensions' | 'placeholder' | 'mode' | 'editable' | 'taskToggle' | 'autofocus' | 'attributes' | 'resolveUrl' | 'referrerPolicy' | 'headingIds' | 'onLinkClick' | 'theme' | 'toolbar' | 'linkCard' | 'slashMenu' | 'slashHint' | 'labels' | 'icons'>;
6
6
  export interface MarkdownEditorProps extends EditorProps {
7
7
  /** Markdown. The editor updates only when this is not a value it emitted itself. */
8
8
  value?: string;
@@ -20,7 +20,7 @@ export interface MarkdownEditorProps extends EditorProps {
20
20
  /**
21
21
  * The editor as a controlled React component. A `ref` gives the core `Editor`.
22
22
  *
23
- * `placeholder`, `editable`, `taskToggle`, `attributes`, `resolveUrl`, `onLinkClick` and `theme` update live.
23
+ * `placeholder`, `mode`, `editable`, `taskToggle`, `attributes`, `resolveUrl`, `onLinkClick` and `theme` update live.
24
24
  * A change to the names in `extensions` recreates the editor, keeping its
25
25
  * Markdown; with the same names, the callbacks of `suggestion()` extensions
26
26
  * (`items`, `command`, `match`, `allow`) update live. Other props are read
@@ -0,0 +1,71 @@
1
+ import { type MarkdownSource } from './markdown/source.js';
2
+ import { type Doc, type EditorState, type Position, type Selection } from './model.js';
3
+ /** The ID of the one block that holds the text in source mode. */
4
+ export declare const SOURCE_ID = "mbe-source";
5
+ export type EditorMode = 'rich' | 'source';
6
+ export declare function sourceDoc(text: string): Doc;
7
+ export declare function isSourceDoc(doc: Doc): boolean;
8
+ /** The text of a source document. */
9
+ export declare function sourceText(doc: Doc): string;
10
+ export declare function sourcePosition(offset: number): Position;
11
+ export declare function sourceState(text: string, anchor: number, focus?: number): EditorState;
12
+ /** The selected range of a source state, in text order. */
13
+ export declare function sourceRange(state: EditorState): {
14
+ from: number;
15
+ to: number;
16
+ };
17
+ /** Inserts text at the selection, replacing it. */
18
+ export declare function insertSourceText(state: EditorState, insert: string): EditorState;
19
+ /**
20
+ * Inserts text at `offset` without replacing anything. With `block`, the
21
+ * text goes on lines of its own: a line break is added before it unless it
22
+ * starts a line, and after it unless a line break or the end follows.
23
+ */
24
+ export declare function insertSourceAt(state: EditorState, offset: number, insert: string, block: boolean, keepSelection: boolean): EditorState;
25
+ /** Backspace: deletes the selection or the character before the caret; nothing at the start. */
26
+ export declare function sourceDeleteBackward(state: EditorState, previous: (text: string, offset: number) => number): EditorState | null;
27
+ /** Delete: deletes the selection or the character after the caret; nothing at the end. */
28
+ export declare function sourceDeleteForward(state: EditorState, next: (text: string, offset: number) => number): EditorState | null;
29
+ /** Where an offset in `before` moves to in `after`, when the change between them is one replaced range. */
30
+ export declare function mapOffset(offset: number, before: string, after: string): number;
31
+ interface Placed {
32
+ /** Where the block's text starts in the source, or its source's start. */
33
+ readonly start: number;
34
+ /** Where the block's source ends. */
35
+ readonly end: number;
36
+ readonly text: string | null;
37
+ }
38
+ /**
39
+ * The offset in `loaded`'s text for a position in a rich document whose
40
+ * blocks match `loaded`'s by index. The caret keeps its column when the
41
+ * block's text before it appears verbatim in the source; otherwise it goes
42
+ * to the start of the block's text.
43
+ */
44
+ export declare function toSourceOffset(doc: Doc, pos: Position, loaded: {
45
+ doc: Doc;
46
+ source: MarkdownSource;
47
+ }, placed?: Placed[]): number;
48
+ /** The position in `loaded`'s document for an offset in its text: the block whose source holds it, at the same column when it can be. */
49
+ export declare function fromSourceOffset(offset: number, loaded: {
50
+ doc: Doc;
51
+ source: MarkdownSource;
52
+ }, placed?: Placed[]): Position;
53
+ /** Maps a selection of a rich document to offsets in `loaded`'s text. */
54
+ export declare function selectionToSource(doc: Doc, sel: Selection, loaded: {
55
+ doc: Doc;
56
+ source: MarkdownSource;
57
+ }): {
58
+ anchor: number;
59
+ focus: number;
60
+ };
61
+ /** Maps offsets in a text to a selection of `loaded`, the document loaded from it. */
62
+ export declare function selectionFromSource(anchor: number, focus: number, loaded: {
63
+ doc: Doc;
64
+ source: MarkdownSource;
65
+ }): Selection;
66
+ /** Where a block's source ends in `loaded`'s text, for a widget that follows the block. */
67
+ export declare function blockEndOffset(doc: Doc, blockId: string, loaded: {
68
+ doc: Doc;
69
+ source: MarkdownSource;
70
+ }): number;
71
+ export {};
@@ -54,7 +54,7 @@ export interface ThemeColors {
54
54
  /** Highlighted menu items and toolbar buttons. */
55
55
  readonly hover?: string;
56
56
  readonly danger?: string;
57
- /** The border and title of each callout type. */
57
+ /** The icon of each callout type, and a light tint of it behind the text. */
58
58
  readonly calloutNote?: string;
59
59
  readonly calloutTip?: string;
60
60
  readonly calloutImportant?: string;
@@ -4,7 +4,7 @@ import type { Labels } from '../labels.js';
4
4
  import type { IconName } from '../icons.js';
5
5
  import type { ThemeClasses } from '../theme.js';
6
6
  import { type Decoration } from '../decorations.js';
7
- import type { MarkdownOptions } from '../markdown/options.js';
7
+ import { type MarkdownOptions } from '../markdown/options.js';
8
8
  export declare const BLOCK_ATTR = "data-block-id";
9
9
  /** The view name for code blocks of a language: `code:mermaid`. */
10
10
  export declare function codeViewName(language: string): string;
@@ -42,6 +42,8 @@ export interface RenderContext {
42
42
  readonly icon?: (name: IconName) => Node | null;
43
43
  /** Code blocks get a copy button. */
44
44
  readonly codeCopy?: boolean;
45
+ /** The document is source mode's one code block: render it as plain text. */
46
+ readonly source?: boolean;
45
47
  /** An image failed to load and its element was replaced by the unavailable state. */
46
48
  readonly onImageError?: () => void;
47
49
  /** A loaded image element with this `src` from the elements being replaced, to show again instead of a new one. */
@@ -57,6 +59,12 @@ export declare function tokenKey(cell: Cell | undefined, from: number): string;
57
59
  export declare function sameReveal(a: BlockReveal | undefined, b: BlockReveal | undefined): boolean;
58
60
  /** The label and icon name of a callout type, such as `calloutNote`. */
59
61
  export declare function calloutName(type: CalloutType): `callout${Capitalize<CalloutType>}`;
62
+ /**
63
+ * Marks the built-in view of raw Markdown blocks in `RenderContext.views`.
64
+ * It is never called: the renderer draws those blocks itself (see
65
+ * `renderMarkdown`), and an extension's own `markdown` view replaces it.
66
+ */
67
+ export declare const MARKDOWN_VIEW: SyntaxView;
60
68
  /**
61
69
  * Runs of inline raw HTML that render as one piece: an opening tag through
62
70
  * its closing tag, with the text between (`<kbd>Ctrl</kbd>`), or a single tag
@@ -103,6 +111,8 @@ export declare class BlockRenderer {
103
111
  icon?: (name: IconName) => Node | null;
104
112
  /** Code blocks get a copy button. */
105
113
  codeCopy: boolean;
114
+ /** Source mode: the code block renders as plain text. Changing it needs a forced render. */
115
+ source: boolean;
106
116
  /** An image failed to load and now shows as unavailable. */
107
117
  onImageError?: () => void;
108
118
  /** Collapsible sections shown collapsed, by the ID of their title block. */
@@ -8,6 +8,6 @@ export declare function sameRevealed(a: ReadonlyMap<string, BlockReveal>, b: Rea
8
8
  * Where the caret goes in a rendered block's source. A raw block: past its
9
9
  * opening line going forward (such as a `$$` fence), at the end of its
10
10
  * next-to-last line going back or on a click. A code block holds only the
11
- * code: its start or its end.
11
+ * code, and raw Markdown has no fence: their start or their end.
12
12
  */
13
13
  export declare function viewEntry(block: RawBlock | CodeBlock, forward: boolean): Position;
@@ -17,6 +17,8 @@ export declare class WidgetImpl implements Widget {
17
17
  private render;
18
18
  private readonly onRemove;
19
19
  blockId: string;
20
+ /** In source mode, the text offset the widget sits after. */
21
+ offset: number;
20
22
  isRemoved: boolean;
21
23
  private cleanup;
22
24
  constructor(element: HTMLElement, blockId: string, render: WidgetRender, onRemove: (widget: WidgetImpl) => void);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usememos/editor",
3
- "version": "26.10.14",
3
+ "version": "26.10.16",
4
4
  "description": "A block editor for Markdown. Zero runtime dependencies, with an optional React component.",
5
5
  "keywords": [
6
6
  "markdown",