@usememos/editor 26.10.1 → 26.10.3

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.
@@ -1,9 +1,12 @@
1
1
  import { type ChangeKind } from './history.js';
2
2
  import { type Labels } from './labels.js';
3
+ import { type Icons } from './icons.js';
3
4
  import { type MarkdownOptions } from './markdown/options.js';
4
5
  import type { Extension } from './extension.js';
6
+ import { type SuggestionState } from './suggest/controller.js';
5
7
  import { type Widget, type WidgetRender } from './widgets.js';
6
8
  import { type Block, type EditorState, type HeadingLevel, type Position, type Selection } from './model.js';
9
+ import { type ResolveUrl } from './view/dom.js';
7
10
  import { type Theme } from './theme.js';
8
11
  export interface EditorOptions {
9
12
  /** The element the editor renders into. */
@@ -18,18 +21,55 @@ export interface EditorOptions {
18
21
  placeholder?: string;
19
22
  /** Default true. */
20
23
  editable?: boolean;
24
+ /** While `editable` is false, lets the user tick and untick task items. Default false. */
25
+ taskToggle?: boolean;
21
26
  /** Focus on creation: `true` or `'start'` puts the caret at the start, `'end'` at the end. */
22
27
  autofocus?: boolean | 'start' | 'end';
23
28
  /** Attributes for the editable root, such as `autocorrect` or `aria-label`. */
24
29
  attributes?: Readonly<Record<string, string>>;
25
- /** Maps an image `src` to the URL to display. The saved Markdown keeps the original `src`. */
26
- resolveImageSrc?: (src: string) => string;
30
+ /**
31
+ * Maps a link's or image's URL to the URL to use, or null to drop it: the
32
+ * link is not clickable, the image has no `src`. The saved Markdown keeps
33
+ * the original. Default `defaultResolveUrl`: relative URLs, `http`, `https`,
34
+ * `mailto` and `tel`.
35
+ */
36
+ resolveUrl?: ResolveUrl;
37
+ /** `referrerpolicy` for images, such as `no-referrer`. Fixed for the editor's lifetime. */
38
+ referrerPolicy?: ReferrerPolicy;
39
+ /**
40
+ * Element ids for the document's headings, for in-page links and an
41
+ * outline: called with every heading in order, returns an id (or
42
+ * undefined) for each. Called again when the document changes. Fixed for
43
+ * the editor's lifetime.
44
+ */
45
+ headingIds?: (headings: readonly {
46
+ readonly text: string;
47
+ readonly level: HeadingLevel;
48
+ }[]) => readonly (string | undefined)[];
49
+ /**
50
+ * Called when a link is clicked to be opened: a plain click while read-only,
51
+ * Mod+click while editing. Return true to handle it, for example to route an
52
+ * in-app link; otherwise the editor opens `href` in a new tab.
53
+ */
54
+ onLinkClick?: (ctx: {
55
+ readonly editor: Editor;
56
+ readonly href: string;
57
+ readonly event: MouseEvent;
58
+ }) => boolean | void;
27
59
  /** Classes for rendered elements, colors and fonts, and the color scheme. See `Theme`. */
28
60
  theme?: Theme;
29
61
  /** Overrides UI strings. */
30
62
  labels?: Partial<Labels>;
63
+ /** Replaces built-in icons, by the `labels` key of the same control. A string is shown as text; an element is cloned. */
64
+ icons?: Icons;
31
65
  /** Shows the formatting toolbar on selection. Default true. */
32
66
  toolbar?: boolean;
67
+ /**
68
+ * The element menus, the toolbar and other popups are appended to.
69
+ * Default: the nearest open `<dialog>` or `aria-modal` ancestor of
70
+ * `element`, else `document.body`.
71
+ */
72
+ popupContainer?: HTMLElement;
33
73
  /** IDs of built-in slash menu items to show, in order, or false for no slash menu. See SLASH_ITEM_IDS. */
34
74
  slashMenu?: false | readonly SlashItemId[];
35
75
  /** Shortcut for `on('change')`, with the new Markdown. */
@@ -40,7 +80,7 @@ export interface EditorOptions {
40
80
  onBlur?: (editor: Editor) => void;
41
81
  }
42
82
  /** Options that `setOptions` can change while the editor runs. */
43
- export type RuntimeOptions = Pick<EditorOptions, 'placeholder' | 'editable' | 'attributes' | 'resolveImageSrc' | 'theme'>;
83
+ export type RuntimeOptions = Pick<EditorOptions, 'placeholder' | 'editable' | 'taskToggle' | 'attributes' | 'resolveUrl' | 'onLinkClick' | 'theme' | 'extensions'>;
44
84
  export interface EditorEvents {
45
85
  /** After document changes, at most once per animation frame. */
46
86
  change: {
@@ -62,6 +102,27 @@ export interface EditorEvents {
62
102
  submit: {
63
103
  editor: Editor;
64
104
  };
105
+ /** The autocomplete popup opened, closed, or changed its query, items, highlight or loading state. */
106
+ suggestionChange: {
107
+ editor: Editor;
108
+ state: SuggestionState | null;
109
+ };
110
+ }
111
+ export interface SuggestionApi {
112
+ /** The open suggestion: its query, range, items and highlighted item. Null when none is open. */
113
+ readonly state: SuggestionState | null;
114
+ /**
115
+ * Opens the popup for the token at the caret, as Ctrl+Space does, even
116
+ * where typing would not open it. With `name`, only that suggestion.
117
+ * Returns false when there is no token at the caret.
118
+ */
119
+ open(name?: string): boolean;
120
+ /** Closes the popup and keeps it closed for this token, as Escape does. */
121
+ close(): void;
122
+ /** Moves the highlight by `delta` items, wrapping around, as the arrow keys do. */
123
+ move(delta: number): void;
124
+ /** Picks the item at `index`, or the highlighted one, as Enter does. Returns false when there is none. */
125
+ accept(index?: number): boolean;
65
126
  }
66
127
  /** Names `isActive` and `getAttributes` understand. */
67
128
  export type ActiveName = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'paragraph' | 'heading' | 'bulletList' | 'orderedList' | 'taskList' | 'blockquote' | 'codeBlock' | 'table' | 'divider' | 'raw';
@@ -131,13 +192,17 @@ export interface EditorCommands {
131
192
  selectBlocks(): boolean;
132
193
  focus(position?: 'start' | 'end' | Position): boolean;
133
194
  blur(): boolean;
195
+ /** Scrolls the caret (the selection's focus end) into view, in scrolling containers and the window. */
196
+ scrollIntoView(): boolean;
134
197
  }
135
198
  export declare const SLASH_ITEM_IDS: readonly ["text", "heading1", "heading2", "heading3", "bulletList", "numberedList", "todoList", "quote", "code", "table", "divider"];
136
199
  export type SlashItemId = (typeof SLASH_ITEM_IDS)[number];
137
200
  export declare class Editor {
201
+ /** The editable element the editor renders into: `options.element`, or for `useEditor`, the element `<EditorContent>` mounts. */
138
202
  readonly root: HTMLElement;
139
- readonly labels: Labels;
140
203
  readonly commands: EditorCommands;
204
+ /** The autocomplete popup of `suggestion()` extensions and the slash menu. */
205
+ readonly suggestion: SuggestionApi;
141
206
  private current;
142
207
  /** The Markdown the document was loaded from; unchanged blocks are saved as this text. */
143
208
  private source;
@@ -151,11 +216,15 @@ export declare class Editor {
151
216
  * block, the marks for composed text, and whether it has an undo step yet.
152
217
  */
153
218
  private composition;
219
+ /** When the last composition ended (Safari only): the next key may be the Enter that committed it. */
220
+ private compositionEndedAt;
154
221
  /** Blocks the browser edited during a composition that ended, rendered again on the next frame. */
155
222
  private readonly unsettled;
156
223
  private settleFrame;
157
224
  private readOnly;
158
225
  private wasReadOnly;
226
+ /** Task checkboxes stay usable while read-only. */
227
+ private taskToggle;
159
228
  private readonly options;
160
229
  /** The extensions in priority order, ending with the default keymap unless one was given. */
161
230
  private readonly extensions;
@@ -174,10 +243,18 @@ export declare class Editor {
174
243
  private focused;
175
244
  /** A mouse button is down in the editor. Rendered views keep their state until it is released, so a drag is not disturbed. */
176
245
  private pointerDown;
177
- /** Suggestion menus from extensions, then the slash menu. */
246
+ /** The selection the formatting toolbar was closed for with Escape. It stays closed until the selection changes. */
247
+ private toolbarClosedFor;
248
+ /** Handling a key press, typed text, a paste or a drop: changes then scroll the caret into view. */
249
+ private typing;
250
+ /**
251
+ * A caret key the browser is handling: the block the caret was in, the
252
+ * caret's x, and 1 or -1 for a plain ArrowDown or ArrowUp. The next
253
+ * selection change checks where the caret went (see `onSelectionChange`).
254
+ */
255
+ private caretKey;
256
+ /** Suggestion menus from extensions, then the slash menu (see suggest/controller.ts). */
178
257
  private readonly suggestions;
179
- /** The open suggestion: which one, where its trigger is, and the last query shown. */
180
- private suggest;
181
258
  private readonly blockMenu;
182
259
  private readonly toolbar;
183
260
  private readonly linkPopover;
@@ -192,6 +269,12 @@ export declare class Editor {
192
269
  private blockSel;
193
270
  /** Block elements that carry the selected class. */
194
271
  private selectedEls;
272
+ /** The ids `headingIds` gave the headings of `doc`, by block ID. */
273
+ private headingIdsFor;
274
+ /** `redecorate()` was called during a composition; it runs when the composition settles. */
275
+ private redecoratePending;
276
+ /** The frame that renders blocks again after their views settled. */
277
+ private viewFrame;
195
278
  constructor(options: EditorOptions);
196
279
  /** The document as Markdown. Blocks that were not edited are written exactly as they were loaded. */
197
280
  getMarkdown(): string;
@@ -209,6 +292,8 @@ export declare class Editor {
209
292
  /** True while an input method (IME) is composing text. Replacing the document now would break the composition. */
210
293
  get isComposing(): boolean;
211
294
  get isEditable(): boolean;
295
+ /** True after `destroy()`. The editor then ignores every call that would change it; `getMarkdown()` still returns its last Markdown. */
296
+ get isDestroyed(): boolean;
212
297
  /** True if `name` applies at the selection: a mark on the selected text, or the type of the block with the caret. */
213
298
  isActive(name: ActiveName, attrs?: Readonly<Record<string, unknown>>): boolean;
214
299
  /** 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. */
@@ -241,7 +326,12 @@ export declare class Editor {
241
326
  /** Focuses the editor at the current selection. */
242
327
  focus(): void;
243
328
  blur(): void;
244
- /** Changes `placeholder`, `editable`, `attributes`, `resolveImageSrc` or `theme`. */
329
+ /**
330
+ * Changes `placeholder`, `editable`, `taskToggle`, `attributes`,
331
+ * `resolveUrl`, `onLinkClick` or `theme`. `extensions` only updates the callbacks of
332
+ * `suggestion()` extensions (`items`, `command`, `match`, `allow`), matched
333
+ * by name; other changes to extensions need a new editor.
334
+ */
245
335
  setOptions(options: RuntimeOptions): void;
246
336
  /** Subscribes to an event. Returns a function that unsubscribes. */
247
337
  on<K extends keyof EditorEvents>(type: K, handler: (payload: EditorEvents[K]) => void): () => void;
@@ -255,10 +345,8 @@ export declare class Editor {
255
345
  private toggleMark;
256
346
  private undo;
257
347
  private redo;
258
- /** @internal Viewport rectangle of the selection, for positioning UI. */
259
- selectionRect(): DOMRect | null;
260
- /** @internal The rendered element of a block. */
261
- blockElement(id: string): HTMLElement | undefined;
348
+ /** Scrolls the editor and the elements around it so the caret is visible. */
349
+ private scrollCaretIntoView;
262
350
  destroy(): void;
263
351
  private apply;
264
352
  private commit;
@@ -277,12 +365,26 @@ export declare class Editor {
277
365
  * or has just changed.
278
366
  */
279
367
  private applyReadOnly;
368
+ /** True while read-only with task toggling on: only task checkboxes respond. */
369
+ private get ticking();
370
+ /** Toggles a task item from its checkbox, while editing or while read-only with `taskToggle`. */
371
+ private toggleTaskAt;
280
372
  /** Hints for empty blocks, and the UI that follows the selection. */
281
373
  private afterRender;
282
374
  private syncDomSelection;
283
375
  private toDom;
284
376
  private readDomSelection;
285
377
  private onSelectionChange;
378
+ /**
379
+ * After the browser moved the caret from block `from` with ArrowDown
380
+ * (`dir` 1) or ArrowUp to `to`: the position in the first block it passed
381
+ * over that holds a caret, at `x`. Firefox passes over list items (they are
382
+ * flex boxes), WebKit over code blocks and tables. Null when no block was
383
+ * passed over.
384
+ */
385
+ private skippedBlock;
386
+ /** Where the caret goes moving into `block` from above (`forward`) or below: its first or last line, at `x`. */
387
+ private blockEntry;
286
388
  /**
287
389
  * Renders the document, then puts the browser selection back. Only blocks
288
390
  * whose object or revealed views changed are rebuilt, unless `force`.
@@ -290,15 +392,47 @@ export declare class Editor {
290
392
  * a table replaces the focused cell).
291
393
  */
292
394
  private paint;
395
+ /** Heading ids from the `headingIds` option, computed again only when the document changes. */
396
+ private applyHeadingIds;
397
+ /**
398
+ * Computes decorations again for every block and renders the blocks whose
399
+ * decorations depend on something outside the document changed, such as
400
+ * which mentioned people exist or a highlighter's language that finished
401
+ * loading. Waits for an input method to finish composing.
402
+ */
403
+ redecorate(): void;
404
+ /**
405
+ * Views that returned a promise settled: their blocks render again on the
406
+ * next frame, or when a composition settles, since rendering would end it.
407
+ */
408
+ private viewsSettled;
409
+ /** A block rendered by a view (a formula block, a diagram), or null. */
410
+ private viewBlock;
293
411
  /** Re-renders if the views that show their source changed: after the selection, focus or editability changes. */
294
412
  private refreshRevealed;
295
413
  private onMouseUp;
296
414
  /**
297
415
  * When an arrow key at the edge of a block would move past a rendered raw
298
- * block next to it (the browser skips non-editable blocks), the position in
299
- * that block's source to move to instead.
416
+ * block or a table next to it, the position inside that block to move to
417
+ * instead: the browser skips non-editable blocks, and sometimes a table
418
+ * (its cells are separate editing hosts). A table is entered in its first
419
+ * column.
420
+ */
421
+ private blockPast;
422
+ /**
423
+ * ArrowUp or ArrowDown on the first or last line of a table cell: the
424
+ * position in the cell above or below (same column), or past the table in
425
+ * the next block that holds a caret. Null when the caret is not on that
426
+ * line, or when the table ends the document that way (`exitBlock` adds a
427
+ * paragraph there).
300
428
  */
301
- private viewBlockPast;
429
+ private tableCellPast;
430
+ /**
431
+ * Whether the caret is on the last (`forward`) or first line of the text in
432
+ * `content`. Lines are measured from the text, not the element's box, which
433
+ * has padding (a table cell) and may be taller than its text.
434
+ */
435
+ private onEdgeLine;
302
436
  /** A click on a rendered view puts the caret in its source: at the end of a token, or of a block's formula. */
303
437
  private revealView;
304
438
  /** The browser selection is the source of truth when an event starts. */
@@ -321,6 +455,14 @@ export declare class Editor {
321
455
  private onCompositionEnd;
322
456
  private settleComposition;
323
457
  private onInput;
458
+ /** The length of the composed block's or cell's text. */
459
+ private composedLength;
460
+ /**
461
+ * Ends the composition on purpose: reads the composed text into the model
462
+ * and renders the block from it, which ends the browser's composition.
463
+ * Picking a suggestion item while composing does this first.
464
+ */
465
+ private endComposition;
324
466
  /**
325
467
  * Reads the composed text into the model while composing. The browser's
326
468
  * DOM for the block already shows it, so the block is not re-rendered.
@@ -338,6 +480,15 @@ export declare class Editor {
338
480
  */
339
481
  private readDom;
340
482
  private onKeyDown;
483
+ /**
484
+ * WebKit skips code blocks and tables with ArrowDown and ArrowUp. When no
485
+ * editable text follows them (a rendered footnote or formula is not), it
486
+ * does not move the caret at all. If the browser left the caret on the edge
487
+ * line of its block, move it into the next block that holds a caret.
488
+ */
489
+ private unlessCaretMoves;
490
+ /** Selects all the text of the block (or table cell) holding `target`. False if it holds none. */
491
+ private selectBlockText;
341
492
  private moveMenu;
342
493
  private onMouseDown;
343
494
  /**
@@ -359,15 +510,8 @@ export declare class Editor {
359
510
  private onDrop;
360
511
  /** The document position at a viewport point, or null outside the editor's text. */
361
512
  positionAtPoint(x: number, y: number): Position | null;
362
- /** Opens the suggestion menu whose trigger was just typed, if any. */
363
- private maybeOpenSuggestion;
364
- private closeSuggestion;
365
- /** The text typed after the trigger, or null when the suggestion should close. */
366
- private suggestionQuery;
367
- /** Asks for items when the query changed, and shows them when they arrive. */
368
- private refreshSuggestion;
369
- private placeSuggestion;
370
- private pickSuggestion;
513
+ /** After typing: opens the suggestion whose token is at the caret, if any. */
514
+ private typedForSuggestion;
371
515
  /** The slash menu, as a suggestion. */
372
516
  private slashSuggestion;
373
517
  /** The blocks of the block selection, in document order. Empty when the selection is text. */
@@ -392,17 +536,11 @@ export declare class Editor {
392
536
  */
393
537
  private onBlockKey;
394
538
  private blockMenuSections;
395
- /**
396
- * Only large blocks get the handle and block menu: tables and code blocks.
397
- * Other blocks change type through the slash menu or Markdown shortcuts.
398
- */
399
- hasBlockMenu(blockId: string): boolean;
400
- openBlockMenu(blockId: string, anchor?: DOMRect): void;
401
539
  private closeBlockMenu;
402
- openLinkPopover(): void;
403
540
  private updateUi;
404
- private openMenu;
405
541
  private syncActiveDescendant;
406
542
  private hideUi;
543
+ /** Wraps an input event handler so the changes it makes keep the caret in view. */
544
+ private typed;
407
545
  private listen;
408
546
  }
@@ -1,8 +1,8 @@
1
- import type { Decorator } from './decorations.js';
1
+ import type { Decoration, Decorator } from './decorations.js';
2
2
  import type { Editor } from './editor.js';
3
3
  import type { SuggestionConfig } from './extensions/suggestion.js';
4
4
  import type { MarkdownSyntax } from './markdown/options.js';
5
- import type { Position } from './model.js';
5
+ import type { CodeBlock, Position } from './model.js';
6
6
  import type { ThemeClasses } from './theme.js';
7
7
  export interface ExtensionContext {
8
8
  readonly editor: Editor;
@@ -14,11 +14,24 @@ export interface ExtensionContext {
14
14
  export type KeyHandler = (ctx: ExtensionContext) => boolean;
15
15
  /**
16
16
  * Renders the Markdown source of a token (`kind` is `inline`, `element` a
17
- * span) or of a raw block (`block`, a div) into `element`. Returns false, or
18
- * throws, to show the source instead. Results are cached by source, so the
19
- * output must depend on nothing else.
17
+ * span), a raw block, or the code of a code block (`block`, a div) into
18
+ * `element`. Returns false, or throws, to show the source instead. Results
19
+ * are cached by source, so the output must depend on nothing else.
20
+ *
21
+ * A view that finishes later (a diagram, a renderer that loads on demand)
22
+ * fills in what it can at once and returns a promise: the editor shows that
23
+ * meanwhile, and the finished element when the promise resolves. A promise
24
+ * that rejects or resolves to false shows the source.
20
25
  */
21
- export type SyntaxView = (source: string, element: HTMLElement, kind: 'inline' | 'block') => void | false;
26
+ export type SyntaxView = (source: string, element: HTMLElement, kind: 'inline' | 'block') => void | false | PromiseLike<void | false>;
27
+ /** A code block's code, for decorations such as syntax highlighting. */
28
+ export interface CodeDecorationContext {
29
+ readonly block: CodeBlock;
30
+ /** The code. */
31
+ readonly text: string;
32
+ /** The info string's language, as written; empty for none. */
33
+ readonly language: string;
34
+ }
22
35
  export interface Extension {
23
36
  /** Unique name, for debugging and for replacing a built-in extension. */
24
37
  readonly name: string;
@@ -49,12 +62,20 @@ export interface Extension {
49
62
  readonly event: DragEvent;
50
63
  readonly position: Position | null;
51
64
  }) => boolean;
65
+ /**
66
+ * Classes on ranges of a code block's code, for syntax highlighting, in
67
+ * editing and read-only alike. Recomputed only for code blocks that
68
+ * changed; call `editor.redecorate()` when a language finishes loading.
69
+ * Never saved.
70
+ */
71
+ readonly codeDecorations?: (ctx: CodeDecorationContext) => readonly Decoration[];
52
72
  /**
53
73
  * Shows tokens and raw blocks of a syntax rendered instead of as source,
54
- * keyed by syntax name; `math()` uses this to show formulas. While editing,
55
- * a token shows its source when the caret touches it, and a raw block when
56
- * the selection is in it; clicking either does the same. Read-only always
57
- * shows the rendered view.
74
+ * keyed by syntax name; `math()` uses this to show formulas. `code:` and a
75
+ * language (`code:mermaid`) renders code blocks of that language, such as
76
+ * diagrams. While editing, a token shows its source when the caret touches
77
+ * it, and a block when the selection is in it; clicking either does the
78
+ * same. Read-only always shows the rendered view.
58
79
  */
59
80
  readonly views?: Readonly<Record<string, SyntaxView>>;
60
81
  /**
@@ -1,2 +1,6 @@
1
1
  import type { Extension } from '../extension.js';
2
- export declare function footnotes(): Extension;
2
+ export interface FootnotesOptions {
3
+ /** Render references and definitions instead of showing their source. Default true. */
4
+ readonly render?: boolean;
5
+ }
6
+ export declare function footnotes(options?: FootnotesOptions): Extension;
@@ -4,11 +4,12 @@ export interface MathOptions {
4
4
  * Renders TeX into `element`. `displayMode` is true for a `$$` block.
5
5
  * KaTeX's `render` has this shape:
6
6
  * `render: (tex, el, options) => katex.render(tex, el, { ...options, throwOnError: false })`.
7
- * Throwing shows the source instead.
7
+ * Throwing shows the source instead. A renderer that loads on demand can
8
+ * return a promise; the source shows until it resolves (see `views`).
8
9
  */
9
10
  readonly render?: (tex: string, element: HTMLElement, options: {
10
11
  readonly displayMode: boolean;
11
- }) => void;
12
+ }) => void | PromiseLike<void>;
12
13
  }
13
14
  /** The TeX inside a token (`$x$`, `$$x$$`) or a fenced block (`$$` lines around it). */
14
15
  export declare function mathSource(source: string, kind: 'inline' | 'block'): string;
@@ -1,51 +1,143 @@
1
1
  import type { Editor } from '../editor.js';
2
2
  import type { Extension } from '../extension.js';
3
- import type { Position } from '../model.js';
3
+ import type { Icon } from '../icons.js';
4
+ import type { Block, Cell, Inline, Position } from '../model.js';
4
5
  export interface SuggestionItem {
5
6
  /** Stable ID, for tests and keys. Defaults to the label. */
6
7
  readonly id?: string;
7
8
  readonly label: string;
8
- /** Short glyph shown before the label. */
9
- readonly icon?: string;
9
+ /** Secondary text after the label, such as a user name or a path. */
10
+ readonly detail?: string;
11
+ /** Shown before the label: text, or an element (such as an `<svg>` or an avatar `<img>`) that is cloned. */
12
+ readonly icon?: Icon;
13
+ /** A group heading. Items with the same section should be next to each other; a heading starts where it changes. */
14
+ readonly section?: string;
15
+ /** Written after the trigger by the default command. Default `id`, then `label`. */
16
+ readonly value?: string;
10
17
  }
11
18
  export interface SuggestionRange {
12
- /** The trigger character. */
19
+ /** The start of the trigger. */
13
20
  readonly from: Position;
14
- /** The caret: the end of the query. */
21
+ /** The end of the token, which may be after the caret. */
15
22
  readonly to: Position;
16
23
  }
24
+ /** A token at the caret, as offsets into the block's or cell's text. */
25
+ export interface SuggestionMatch {
26
+ /** The start of the trigger. */
27
+ readonly from: number;
28
+ /** The end of the token. At or after the caret. */
29
+ readonly to: number;
30
+ /** What `items` receives. The built-in matchers return the text between the trigger and the caret. */
31
+ readonly query: string;
32
+ }
33
+ export interface SuggestionMatchContext {
34
+ /** The plain text of the block or table cell with the caret. */
35
+ readonly text: string;
36
+ /** The caret's offset in `text`. */
37
+ readonly caret: number;
38
+ /** The formatted content, to check marks and links. */
39
+ readonly content: Inline;
40
+ /** The suggestion's triggers. */
41
+ readonly triggers: readonly string[];
42
+ }
43
+ /** Finds the token at the caret, or returns null. See `matchToken` for building one from a scanner. */
44
+ export type SuggestionMatcher = (ctx: SuggestionMatchContext) => SuggestionMatch | null;
45
+ export interface SuggestionAllowContext {
46
+ readonly editor: Editor;
47
+ /** The block with the caret. For a table cell, the table. */
48
+ readonly block: Block;
49
+ readonly cell?: Cell;
50
+ /** The start of the trigger, as an offset into the block's or cell's text. */
51
+ readonly from: number;
52
+ readonly query: string;
53
+ /** The text before the trigger in the same block or cell. */
54
+ readonly textBefore: string;
55
+ /** True when the trigger is in inline code, a link, an image, raw HTML or an extension token. */
56
+ readonly excluded: boolean;
57
+ /** True when the user asked for the popup (Ctrl+Space), rather than typing. */
58
+ readonly explicit: boolean;
59
+ }
60
+ export interface SuggestionItemsContext {
61
+ /** What the matcher returned; for the built-in matchers, the text between the trigger and the caret, unchanged. */
62
+ readonly query: string;
63
+ readonly editor: Editor;
64
+ /** True when the user asked for the popup (Ctrl+Space), rather than typing. */
65
+ readonly explicit: boolean;
66
+ /**
67
+ * True while an input method is composing the query. The text may be a
68
+ * reading that matches nothing yet; an empty result then keeps the list shown.
69
+ */
70
+ readonly composing: boolean;
71
+ /** Aborted when the query changes or the popup closes. Pass it to `fetch`. */
72
+ readonly signal: AbortSignal;
73
+ }
74
+ export interface SuggestionCommandContext<T> {
75
+ readonly editor: Editor;
76
+ /** The whole token: from the trigger to the end the matcher reported. */
77
+ readonly range: SuggestionRange;
78
+ readonly item: T;
79
+ readonly query: string;
80
+ }
17
81
  export interface SuggestionConfig<T extends SuggestionItem = SuggestionItem> {
18
- /** Characters that open the menu, such as "#" or "/". */
19
- readonly char: string | readonly string[];
20
- /**
21
- * Items for the text typed after the trigger. May be async; a result that
22
- * arrives after the query changed is dropped.
23
- */
24
- readonly items: (ctx: {
25
- readonly query: string;
26
- readonly editor: Editor;
27
- }) => readonly T[] | Promise<readonly T[]>;
28
- /** Applies the picked item. `range` covers the trigger and the query, for replacing them. */
29
- readonly command: (ctx: {
30
- readonly editor: Editor;
31
- readonly range: SuggestionRange;
32
- readonly item: T;
33
- readonly query: string;
34
- }) => void;
35
- /** Whether the query may contain single spaces. Default false: a space closes the menu. */
82
+ /** Unique among the editor's extensions. Also the name of the suggestion in `editor.suggestion.open(name)`. */
83
+ readonly name: string;
84
+ /**
85
+ * Strings that start a token, such as "#", "@" or "[[". The first one is
86
+ * what the default command writes, so `['#', '#']` turns a full-width `#`
87
+ * into `#` on pick.
88
+ */
89
+ readonly trigger: string | readonly string[];
90
+ /**
91
+ * Items for the query, shown in the order given. May be async: while a
92
+ * newer result is pending, the items shown stay usable; a result that
93
+ * arrives after the query changed is dropped; a rejection counts as no
94
+ * items.
95
+ */
96
+ readonly items: (ctx: SuggestionItemsContext) => readonly T[] | Promise<readonly T[]>;
97
+ /**
98
+ * Applies the picked item, usually by replacing `range`. Default: writes
99
+ * the first trigger, the item's `value` and a space, which is left out
100
+ * when the next character is whitespace or closing punctuation.
101
+ */
102
+ readonly command?: (ctx: SuggestionCommandContext<T>) => void;
103
+ /**
104
+ * Finds the token at the caret. Default: a trigger at the start of a line
105
+ * or after whitespace, followed by text up to the caret with no whitespace
106
+ * (see `allowSpaces` and `allowAnywhere`). The token must start with one of
107
+ * the triggers.
108
+ */
109
+ readonly match?: SuggestionMatcher;
110
+ /**
111
+ * Whether the popup may open for a match. Default: not when the trigger is
112
+ * in inline code, a link, an image, raw HTML or a token. Code blocks and
113
+ * raw blocks never open a popup.
114
+ */
115
+ readonly allow?: (ctx: SuggestionAllowContext) => boolean;
116
+ /** Default matcher only: the query may contain single spaces, and the token ends at the caret. Default false. */
36
117
  readonly allowSpaces?: boolean;
37
- /** Whether the trigger opens the menu inside a word. Default false: only at the start of a line or after whitespace. */
118
+ /** Default matcher only: the trigger may follow any character. Default false: only the start of a line or whitespace. */
38
119
  readonly allowAnywhere?: boolean;
39
- /** Longest query that keeps the menu open. Default 30. */
120
+ /** Waits until the query has not changed for this many ms before calling `items`. Default 0. */
121
+ readonly debounce?: number;
122
+ /** Most items shown. Default 50. */
123
+ readonly limit?: number;
124
+ /** Longest query that keeps the popup open. Default 30. */
40
125
  readonly maxLength?: number;
41
126
  /**
42
127
  * Shown when there are no items, for queries up to three characters.
43
- * Without it the menu hides while there are no items.
128
+ * Without it the popup hides while there are no items.
44
129
  */
45
130
  readonly emptyLabel?: string;
46
- /** Extra class names for the menu element. */
131
+ /** Accessible name of the listbox. Default `labels.suggestions`. */
132
+ readonly label?: string;
133
+ /**
134
+ * `false` draws no popup: matching, keys, async items and ARIA on the root
135
+ * still work, and the app draws its own popup from `editor.suggestion.state`
136
+ * and the `suggestionChange` event.
137
+ */
138
+ readonly render?: false;
139
+ /** Extra class names for the popup element. */
47
140
  readonly className?: string;
48
141
  }
49
- export declare function suggestion<T extends SuggestionItem>(config: SuggestionConfig<T> & {
50
- readonly name?: string;
51
- }): Extension;
142
+ /** An autocomplete popup. Add one per trigger syntax, such as tags, mentions or links. */
143
+ export declare function suggestion<T extends SuggestionItem>(config: SuggestionConfig<T>): Extension;