@usememos/editor 26.10.25 → 26.10.27

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.
@@ -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' | 'mode' | 'smartLists' | 'editable' | 'taskToggle' | 'autofocus' | 'attributes' | 'resolveUrl' | 'referrerPolicy' | 'headingIds' | 'onLinkClick' | 'theme' | 'toolbar' | 'linkCard' | 'slashMenu' | 'slashHint' | 'labels' | 'icons'>;
5
+ type EditorProps = Pick<EditorOptions, 'markdown' | 'extensions' | 'placeholder' | 'mode' | 'smartLists' | 'sourceHighlighting' | '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;
@@ -0,0 +1,77 @@
1
+ import { type Decoration, type Decorator } from './decorations.js';
2
+ import type { Extension } from './extension.js';
3
+ import type { CodeBlock, Inline } from './model.js';
4
+ import { type Markup, type MarkupKind } from './markdown/markup.js';
5
+ import type { MarkdownOptions } from './markdown/options.js';
6
+ /** Up to this many characters, the decorations are computed on every keystroke; above, a keystroke shows the last ones moved through its edit, and the exact ones follow. */
7
+ export declare const SYNC_LIMIT = 20000;
8
+ /** What makes up the decorations of source mode's block. */
9
+ export interface SourceDecorationConfig {
10
+ /** The built-in highlighting (`sourceHighlighting`). */
11
+ readonly highlight: boolean;
12
+ /** The theme's class per kind of markup. */
13
+ readonly classes?: Readonly<Partial<Record<MarkupKind, string>>>;
14
+ /** The extensions' decorators, run over the text split at its syntax. */
15
+ readonly decorators: readonly Decorator[];
16
+ /** The extensions' code decorators, run over the code of each fence. */
17
+ readonly codeDecorators: readonly NonNullable<Extension['codeDecorations']>[];
18
+ }
19
+ /**
20
+ * The decorations of source mode's block: the Markdown's own highlighting,
21
+ * then the decorators over the text split at its syntax, and the code
22
+ * decorators over each code block in it, their offsets moved and clipped to
23
+ * the code.
24
+ */
25
+ export declare function sourceDecorations(block: CodeBlock, text: string, markup: Markup, config: SourceDecorationConfig): Decoration[];
26
+ /**
27
+ * Source mode's decorations for one editor, computed once per text.
28
+ * Computing them parses the whole text and runs every decorator over it,
29
+ * about a quarter of a frame per 100 KB, so in a long text a keystroke gets
30
+ * the previous decorations moved through its edit (right where nothing
31
+ * changed; the edited range follows) and the exact ones are computed once
32
+ * typing pauses, when `onReady` has the block rendered again if they differ.
33
+ */
34
+ export declare class SourceHighlighter {
35
+ private readonly options;
36
+ private readonly config;
37
+ private readonly onReady;
38
+ private last;
39
+ private markupFor;
40
+ private spansFor;
41
+ private pending;
42
+ constructor(options: MarkdownOptions | undefined, config: () => SourceDecorationConfig, onReady: () => void);
43
+ /** The markup of the text, read once per text: the decorations and autocomplete share it. */
44
+ markup(text: string): Markup;
45
+ /**
46
+ * The text as spans split at its syntax (`sourceInline`), for the
47
+ * suggestion controller, once per text. In a long text being typed in, the
48
+ * last markup read is moved through the edit, as the decorations are, so
49
+ * the popup's check costs no parse on a keystroke.
50
+ */
51
+ spans(text: string): Inline;
52
+ decorations(block: CodeBlock): Decoration[];
53
+ /** Forgets the decorations, for when what they depend on changed (`redecorate`). */
54
+ reset(): void;
55
+ destroy(): void;
56
+ private read;
57
+ private schedule;
58
+ }
59
+ /**
60
+ * Ranges over `before` moved through the one edit that made `after`: ranges
61
+ * before the edit stay, ranges after it move by the edit's length change, a
62
+ * range around it grows or shrinks with it, and ranges inside it go. Null
63
+ * when the edit is too large for that to be worth it.
64
+ */
65
+ export declare function shiftRanges<T extends {
66
+ readonly from: number;
67
+ readonly to: number;
68
+ }>(ranges: readonly T[], before: string, after: string): T[] | null;
69
+ /** The highlighting decorations: the built-in class per kind, and the theme's class for it. */
70
+ export declare function highlightDecorations(markup: Markup, classes?: Readonly<Partial<Record<MarkupKind, string>>>): Decoration[];
71
+ /**
72
+ * The source text as spans split at its syntax: markup, code, URLs, raw
73
+ * HTML, tokens and escapes are spans a decorator skips (`isExcluded`), and
74
+ * the prose between them plain spans, so `tags()` and `mentions()` see the
75
+ * same runs as in rich mode.
76
+ */
77
+ export declare function sourceInline(text: string, markup: Markup): Inline;
@@ -1,4 +1,6 @@
1
1
  import { type MarkdownSource } from './markdown/source.js';
2
+ import type { Range } from './markdown/ast.js';
3
+ import { type Segment } from './markdown/sourceMap.js';
2
4
  import { type Doc, type EditorState, type Position, type Selection } from './model.js';
3
5
  /** The ID of the one block that holds the text in source mode. */
4
6
  export declare const SOURCE_ID = "mbe-source";
@@ -42,6 +44,12 @@ export declare function sourceEnter(state: EditorState): EditorState;
42
44
  */
43
45
  export declare function sourceIndentList(state: EditorState, delta: 1 | -1): EditorState | null;
44
46
  /** Where an offset in `before` moves to in `after`, when the change between them is one replaced range. */
47
+ /** The one range an edit replaced: `[from, toBefore)` of `before` became `[from, toAfter)` of `after`. */
48
+ export declare function changedRange(before: string, after: string): {
49
+ from: number;
50
+ toBefore: number;
51
+ toAfter: number;
52
+ };
45
53
  export declare function mapOffset(offset: number, before: string, after: string): number;
46
54
  interface Placed {
47
55
  /** Where the block's text starts in the source, or its source's start. */
@@ -49,18 +57,27 @@ interface Placed {
49
57
  /** Where the block's source ends. */
50
58
  readonly end: number;
51
59
  readonly text: string | null;
60
+ /** The block's source map and the source range its text covers, when loading made one (see `markdown/sourceMap.ts`). */
61
+ readonly segments: readonly Segment[] | null;
62
+ readonly span: Range | null;
52
63
  }
53
64
  /**
54
65
  * The offset in `loaded`'s text for a position in a rich document whose
55
- * blocks match `loaded`'s by index. The caret keeps its column when the
56
- * block's text before it appears verbatim in the source; otherwise it goes
57
- * to the start of the block's text.
66
+ * blocks match `loaded`'s by index. With the block's source map, the caret
67
+ * keeps its place in the text, past any syntax before it (an offset on
68
+ * syntax goes to the text after it). Without one, a table or a section, it
69
+ * keeps its column when the block's text before it appears verbatim in the
70
+ * source; otherwise it goes to the start of the block's text.
58
71
  */
59
72
  export declare function toSourceOffset(doc: Doc, pos: Position, loaded: {
60
73
  doc: Doc;
61
74
  source: MarkdownSource;
62
75
  }, placed?: Placed[]): number;
63
- /** 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. */
76
+ /**
77
+ * The position in `loaded`'s document for an offset in its text: the block
78
+ * whose source holds it, at the same place in its text. An offset on syntax
79
+ * goes to the text after it, and one inside an image or an escape before it.
80
+ */
64
81
  export declare function fromSourceOffset(offset: number, loaded: {
65
82
  doc: Doc;
66
83
  source: MarkdownSource;
@@ -83,4 +100,41 @@ export declare function blockEndOffset(doc: Doc, blockId: string, loaded: {
83
100
  doc: Doc;
84
101
  source: MarkdownSource;
85
102
  }): number;
103
+ export type WrapMarker = '**' | '*' | '`' | '~~';
104
+ /**
105
+ * Wraps the selection in `marker`, or unwraps it when it is already wrapped,
106
+ * whether or not the markers are selected. The wrapped range is trimmed of
107
+ * whitespace, since `** foo**` is not strong. With no selection, the word at
108
+ * the caret; on whitespace, the pair is inserted with the caret between the
109
+ * markers. Italic inside strong adds its own star, so `**bold**` becomes
110
+ * `***bold***`, and a run of three unwraps one. A code span's backticks are
111
+ * one more than any run inside it.
112
+ */
113
+ export declare function sourceToggleWrap(state: EditorState, marker: WrapMarker): EditorState;
114
+ /**
115
+ * Sets the heading level of the selected lines, after any quote markers: the
116
+ * `#`s replace a list marker or another level, and a level every line already
117
+ * has, or level 0, removes them, as `setParagraph` does.
118
+ */
119
+ export declare function sourceSetHeading(state: EditorState, level: number): EditorState | null;
120
+ export type SourceListKind = 'bullet' | 'ordered' | 'task';
121
+ /**
122
+ * Makes the selected lines list items of `kind`, after any quote markers,
123
+ * replacing another kind's marker or a heading's `#`s; when every line is one
124
+ * already, removes the markers. New ordered items are numbered from the
125
+ * list they join, or from 1.
126
+ */
127
+ export declare function sourceToggleList(state: EditorState, kind: SourceListKind): EditorState | null;
128
+ /** Quotes the selected lines with `> `, or, when every line that has text is quoted, removes one quote marker from each. */
129
+ export declare function sourceToggleQuote(state: EditorState): EditorState | null;
130
+ /**
131
+ * Wraps the selected lines in a code fence, keeping their quote markers; a
132
+ * blank line becomes an empty fence with the caret in it. When the selection
133
+ * is in fenced code, removes that fence's lines.
134
+ */
135
+ export declare function sourceToggleFence(state: EditorState): EditorState | null;
136
+ /** Makes the selected text a link to `url`: `[text](url)`, with the caret after it. Nothing without a selection. */
137
+ export declare function sourceLink(state: EditorState, url: string): EditorState | null;
138
+ /** True when the selection starts in fenced code, where a paste should stay the text as written. */
139
+ export declare function inFence(state: EditorState): boolean;
86
140
  export {};
@@ -1,4 +1,5 @@
1
1
  import type { HeadingLevel } from './model.js';
2
+ import type { MarkupKind } from './markdown/markup.js';
2
3
  /**
3
4
  * Classes added to each rendered element of a type, next to the editor's own
4
5
  * `mbe-*` classes, in editing and read-only alike. Keys use the names of
@@ -36,6 +37,8 @@ export interface ThemeClasses {
36
37
  readonly link?: string;
37
38
  /** Inline tokens of an extension syntax, as source or rendered, by syntax name, such as `math`. */
38
39
  readonly token?: Readonly<Record<string, string>>;
40
+ /** Source mode's highlighting, by kind of markup (see `MarkupKind`): `marker`, `heading`, `strong`, `em`, `code`, `url` and the rest. */
41
+ readonly source?: Readonly<Partial<Record<MarkupKind, string>>>;
39
42
  }
40
43
  /** Colors for one color scheme. Any CSS color. */
41
44
  export interface ThemeColors {
@@ -76,6 +79,8 @@ export interface ThemeTokens {
76
79
  readonly dark?: ThemeColors;
77
80
  readonly font?: string;
78
81
  readonly mono?: string;
82
+ /** The font of source mode's text. Defaults to `font`; set it to `mono` for an all-monospace source view. */
83
+ readonly sourceFont?: string;
79
84
  readonly fontSize?: string | number;
80
85
  readonly lineHeight?: string | number;
81
86
  readonly radius?: string | number;
@@ -30,6 +30,8 @@ export interface RenderContext {
30
30
  readonly decorate?: (block: Block, content: Inline, cell?: Cell) => readonly Decoration[];
31
31
  /** Decorations for a code block's code, such as syntax highlighting. */
32
32
  readonly decorateCode?: (block: CodeBlock) => readonly Decoration[];
33
+ /** Decorations for source mode's one block: the Markdown's highlighting, and the extensions' decorations over its text. */
34
+ readonly decorateSource?: (block: CodeBlock) => readonly Decoration[];
33
35
  /** Syntaxes that have a view. */
34
36
  readonly views?: ReadonlyMap<string, SyntaxView>;
35
37
  /** A token or raw block of an extension syntax, rendered; null shows its source. */
@@ -108,6 +110,8 @@ export declare class BlockRenderer {
108
110
  decorate?: RenderContext['decorate'];
109
111
  /** Decorations for a code block's code. */
110
112
  decorateCode?: RenderContext['decorateCode'];
113
+ /** Decorations for source mode's block. */
114
+ decorateSource?: RenderContext['decorateSource'];
111
115
  /**
112
116
  * Called when views that returned a promise settle, with the blocks that
113
117
  * showed them. Those blocks must render again to show the finished view,
@@ -227,6 +231,10 @@ export declare class BlockRenderer {
227
231
  adopt(block: Block): void;
228
232
  /** Forget a block's element so the next render rebuilds it, e.g. after the browser edited it during IME composition. */
229
233
  invalidate(id: string): void;
234
+ /** Brings source mode's rendered block up to date with the block's text and decorations, in place. False when it has no content element. */
235
+ private patchSource;
236
+ /** Renders source mode's block again with the decorations as they are now, without touching the lines whose text and decorations are the same. */
237
+ refreshSource(block: CodeBlock): void;
230
238
  }
231
239
  export declare function contentElement(blockEl: HTMLElement, cell?: Cell): HTMLElement | null;
232
240
  /** Index of a node among its parent's child nodes. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usememos/editor",
3
- "version": "26.10.25",
3
+ "version": "26.10.27",
4
4
  "description": "A block editor for Markdown. Zero runtime dependencies, with an optional React component.",
5
5
  "keywords": [
6
6
  "markdown",