@usememos/editor 0.0.0-stage → 26.10.0
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/LICENSE +21 -0
- package/README.md +246 -2
- package/dist/base.css +218 -0
- package/dist/entities.dom.js +14 -0
- package/dist/entities.dom.js.map +7 -0
- package/dist/entities.js +17 -0
- package/dist/entities.js.map +7 -0
- package/dist/index.js +7304 -0
- package/dist/index.js.map +7 -0
- package/dist/react.js +143 -0
- package/dist/react.js.map +7 -0
- package/dist/style.css +592 -0
- package/dist/theme.css +214 -0
- package/dist/types/clipboard/fromHtml.d.ts +2 -0
- package/dist/types/clipboard/toHtml.d.ts +4 -0
- package/dist/types/commands.d.ts +168 -0
- package/dist/types/decorations.d.ts +40 -0
- package/dist/types/editor.d.ts +408 -0
- package/dist/types/extension.d.ts +70 -0
- package/dist/types/extensions/fileHandler.d.ts +28 -0
- package/dist/types/extensions/footnotes.d.ts +2 -0
- package/dist/types/extensions/keymap.d.ts +2 -0
- package/dist/types/extensions/math.d.ts +15 -0
- package/dist/types/extensions/suggestion.d.ts +51 -0
- package/dist/types/extensions/tags.d.ts +34 -0
- package/dist/types/extensions/textDecorator.d.ts +17 -0
- package/dist/types/history.d.ts +17 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/keymap.d.ts +12 -0
- package/dist/types/labels.d.ts +41 -0
- package/dist/types/markdown/ast.d.ts +129 -0
- package/dist/types/markdown/autolink.d.ts +21 -0
- package/dist/types/markdown/blocks.d.ts +4 -0
- package/dist/types/markdown/entities.d.ts +2 -0
- package/dist/types/markdown/entities.dom.d.ts +2 -0
- package/dist/types/markdown/html.d.ts +2 -0
- package/dist/types/markdown/inline.d.ts +35 -0
- package/dist/types/markdown/lower.d.ts +9 -0
- package/dist/types/markdown/options.d.ts +75 -0
- package/dist/types/markdown/parse.d.ts +4 -0
- package/dist/types/markdown/serialize.d.ts +24 -0
- package/dist/types/markdown/source.d.ts +30 -0
- package/dist/types/model.d.ts +205 -0
- package/dist/types/react/index.d.ts +47 -0
- package/dist/types/theme.d.ts +87 -0
- package/dist/types/ui/handle.d.ts +16 -0
- package/dist/types/ui/popup.d.ts +58 -0
- package/dist/types/ui/toolbar.d.ts +28 -0
- package/dist/types/view/dom.d.ts +85 -0
- package/dist/types/view/reveal.d.ts +12 -0
- package/dist/types/widgets.d.ts +26 -0
- package/package.json +58 -4
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { type ChangeKind } from './history.js';
|
|
2
|
+
import { type Labels } from './labels.js';
|
|
3
|
+
import { type MarkdownOptions } from './markdown/options.js';
|
|
4
|
+
import type { Extension } from './extension.js';
|
|
5
|
+
import { type Widget, type WidgetRender } from './widgets.js';
|
|
6
|
+
import { type Block, type EditorState, type HeadingLevel, type Position, type Selection } from './model.js';
|
|
7
|
+
import { type Theme } from './theme.js';
|
|
8
|
+
export interface EditorOptions {
|
|
9
|
+
/** The element the editor renders into. */
|
|
10
|
+
element: HTMLElement;
|
|
11
|
+
/** Initial Markdown. */
|
|
12
|
+
content?: string;
|
|
13
|
+
/** Markdown dialect: `breaks` and `setext`. Fixed for the editor's lifetime. */
|
|
14
|
+
markdown?: MarkdownOptions;
|
|
15
|
+
/** Extensions, such as `math()` and `footnotes()`. Earlier extensions take priority. Fixed for the editor's lifetime. */
|
|
16
|
+
extensions?: readonly Extension[];
|
|
17
|
+
/** Shown when the document is empty. */
|
|
18
|
+
placeholder?: string;
|
|
19
|
+
/** Default true. */
|
|
20
|
+
editable?: boolean;
|
|
21
|
+
/** Focus on creation: `true` or `'start'` puts the caret at the start, `'end'` at the end. */
|
|
22
|
+
autofocus?: boolean | 'start' | 'end';
|
|
23
|
+
/** Attributes for the editable root, such as `autocorrect` or `aria-label`. */
|
|
24
|
+
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;
|
|
27
|
+
/** Classes for rendered elements, colors and fonts, and the color scheme. See `Theme`. */
|
|
28
|
+
theme?: Theme;
|
|
29
|
+
/** Overrides UI strings. */
|
|
30
|
+
labels?: Partial<Labels>;
|
|
31
|
+
/** Shows the formatting toolbar on selection. Default true. */
|
|
32
|
+
toolbar?: boolean;
|
|
33
|
+
/** IDs of built-in slash menu items to show, in order, or false for no slash menu. See SLASH_ITEM_IDS. */
|
|
34
|
+
slashMenu?: false | readonly SlashItemId[];
|
|
35
|
+
/** Shortcut for `on('change')`, with the new Markdown. */
|
|
36
|
+
onChange?: (markdown: string, editor: Editor) => void;
|
|
37
|
+
/** Shortcut for `on('submit')`, with the current Markdown. */
|
|
38
|
+
onSubmit?: (markdown: string, editor: Editor) => void;
|
|
39
|
+
onFocus?: (editor: Editor) => void;
|
|
40
|
+
onBlur?: (editor: Editor) => void;
|
|
41
|
+
}
|
|
42
|
+
/** Options that `setOptions` can change while the editor runs. */
|
|
43
|
+
export type RuntimeOptions = Pick<EditorOptions, 'placeholder' | 'editable' | 'attributes' | 'resolveImageSrc' | 'theme'>;
|
|
44
|
+
export interface EditorEvents {
|
|
45
|
+
/** After document changes, at most once per animation frame. */
|
|
46
|
+
change: {
|
|
47
|
+
editor: Editor;
|
|
48
|
+
};
|
|
49
|
+
/** The selection moved or the document changed. */
|
|
50
|
+
selectionChange: {
|
|
51
|
+
editor: Editor;
|
|
52
|
+
};
|
|
53
|
+
focus: {
|
|
54
|
+
editor: Editor;
|
|
55
|
+
event: FocusEvent;
|
|
56
|
+
};
|
|
57
|
+
blur: {
|
|
58
|
+
editor: Editor;
|
|
59
|
+
event: FocusEvent;
|
|
60
|
+
};
|
|
61
|
+
/** Mod-Enter or Ctrl-Enter. */
|
|
62
|
+
submit: {
|
|
63
|
+
editor: Editor;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** Names `isActive` and `getAttributes` understand. */
|
|
67
|
+
export type ActiveName = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'paragraph' | 'heading' | 'bulletList' | 'orderedList' | 'taskList' | 'blockquote' | 'codeBlock' | 'table' | 'divider' | 'raw';
|
|
68
|
+
/** A position that survives reloading the same Markdown: a block index and an offset. */
|
|
69
|
+
export interface PositionJSON {
|
|
70
|
+
readonly block: number;
|
|
71
|
+
readonly offset: number;
|
|
72
|
+
readonly cell?: {
|
|
73
|
+
readonly row: number;
|
|
74
|
+
readonly col: number;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export interface SelectionJSON {
|
|
78
|
+
readonly anchor: PositionJSON;
|
|
79
|
+
readonly focus: PositionJSON;
|
|
80
|
+
}
|
|
81
|
+
/** Where `insertMarkdown` inserts. A widget means after the widget's block. */
|
|
82
|
+
export type InsertTarget = 'selection' | 'end' | Position | Widget;
|
|
83
|
+
export interface InsertMarkdownOptions {
|
|
84
|
+
/** Default `'selection'`, which replaces the selection like a paste. A position, a widget or `'end'` replaces nothing. */
|
|
85
|
+
at?: InsertTarget;
|
|
86
|
+
/** Insert as separate blocks even when the Markdown is a single paragraph. Default false. */
|
|
87
|
+
block?: boolean;
|
|
88
|
+
/** `'end'` (default) puts the caret after the inserted content; `'keep'` keeps the selection. */
|
|
89
|
+
select?: 'end' | 'keep';
|
|
90
|
+
}
|
|
91
|
+
/** Commands on `editor.commands`. Each applies one change, records one undo step, and returns true if it did something. */
|
|
92
|
+
export interface EditorCommands {
|
|
93
|
+
toggleBold(): boolean;
|
|
94
|
+
toggleItalic(): boolean;
|
|
95
|
+
toggleStrike(): boolean;
|
|
96
|
+
toggleCode(): boolean;
|
|
97
|
+
setParagraph(): boolean;
|
|
98
|
+
toggleHeading(attrs: {
|
|
99
|
+
level: HeadingLevel;
|
|
100
|
+
}): boolean;
|
|
101
|
+
toggleBulletList(): boolean;
|
|
102
|
+
toggleOrderedList(): boolean;
|
|
103
|
+
toggleTaskList(): boolean;
|
|
104
|
+
toggleBlockquote(): boolean;
|
|
105
|
+
toggleCodeBlock(attrs?: {
|
|
106
|
+
language?: string;
|
|
107
|
+
}): boolean;
|
|
108
|
+
setLink(attrs: {
|
|
109
|
+
href: string;
|
|
110
|
+
}): boolean;
|
|
111
|
+
unsetLink(): boolean;
|
|
112
|
+
/** Inserts plain text at the selection, or in place of `range` (for example a suggestion's range). */
|
|
113
|
+
insertText(text: string, options?: {
|
|
114
|
+
range?: {
|
|
115
|
+
from: Position;
|
|
116
|
+
to: Position;
|
|
117
|
+
};
|
|
118
|
+
}): boolean;
|
|
119
|
+
insertMarkdown(markdown: string, options?: InsertMarkdownOptions): boolean;
|
|
120
|
+
insertTable(attrs?: {
|
|
121
|
+
rows?: number;
|
|
122
|
+
cols?: number;
|
|
123
|
+
}): boolean;
|
|
124
|
+
insertDivider(): boolean;
|
|
125
|
+
indent(): boolean;
|
|
126
|
+
outdent(): boolean;
|
|
127
|
+
undo(): boolean;
|
|
128
|
+
redo(): boolean;
|
|
129
|
+
selectAll(): boolean;
|
|
130
|
+
/** Selects the blocks the selection touches as whole blocks (block selection). Escape does this. */
|
|
131
|
+
selectBlocks(): boolean;
|
|
132
|
+
focus(position?: 'start' | 'end' | Position): boolean;
|
|
133
|
+
blur(): boolean;
|
|
134
|
+
}
|
|
135
|
+
export declare const SLASH_ITEM_IDS: readonly ["text", "heading1", "heading2", "heading3", "bulletList", "numberedList", "todoList", "quote", "code", "table", "divider"];
|
|
136
|
+
export type SlashItemId = (typeof SLASH_ITEM_IDS)[number];
|
|
137
|
+
export declare class Editor {
|
|
138
|
+
readonly root: HTMLElement;
|
|
139
|
+
readonly labels: Labels;
|
|
140
|
+
readonly commands: EditorCommands;
|
|
141
|
+
private current;
|
|
142
|
+
/** The Markdown the document was loaded from; unchanged blocks are saved as this text. */
|
|
143
|
+
private source;
|
|
144
|
+
/** The Markdown dialect: options plus the syntax of extensions. */
|
|
145
|
+
private readonly markdown;
|
|
146
|
+
private readonly history;
|
|
147
|
+
private readonly renderer;
|
|
148
|
+
private composing;
|
|
149
|
+
/**
|
|
150
|
+
* The current composition, kept until its block is rendered again: its
|
|
151
|
+
* block, the marks for composed text, and whether it has an undo step yet.
|
|
152
|
+
*/
|
|
153
|
+
private composition;
|
|
154
|
+
/** Blocks the browser edited during a composition that ended, rendered again on the next frame. */
|
|
155
|
+
private readonly unsettled;
|
|
156
|
+
private settleFrame;
|
|
157
|
+
private readOnly;
|
|
158
|
+
private wasReadOnly;
|
|
159
|
+
private readonly options;
|
|
160
|
+
/** The extensions in priority order, ending with the default keymap unless one was given. */
|
|
161
|
+
private readonly extensions;
|
|
162
|
+
private readonly bindings;
|
|
163
|
+
private runtime;
|
|
164
|
+
/** The merged theme classes, serialized, to skip re-rendering when they did not change. */
|
|
165
|
+
private classesKey;
|
|
166
|
+
/** Custom properties the theme's tokens set on the root. */
|
|
167
|
+
private tokenNames;
|
|
168
|
+
private readonly listeners;
|
|
169
|
+
private readonly cleanup;
|
|
170
|
+
private widgets;
|
|
171
|
+
private changeFrame;
|
|
172
|
+
private plainPasteNext;
|
|
173
|
+
private destroyed;
|
|
174
|
+
private focused;
|
|
175
|
+
/** A mouse button is down in the editor. Rendered views keep their state until it is released, so a drag is not disturbed. */
|
|
176
|
+
private pointerDown;
|
|
177
|
+
/** Suggestion menus from extensions, then the slash menu. */
|
|
178
|
+
private readonly suggestions;
|
|
179
|
+
/** The open suggestion: which one, where its trigger is, and the last query shown. */
|
|
180
|
+
private suggest;
|
|
181
|
+
private readonly blockMenu;
|
|
182
|
+
private readonly toolbar;
|
|
183
|
+
private readonly linkPopover;
|
|
184
|
+
private readonly handle;
|
|
185
|
+
private hintEl;
|
|
186
|
+
/**
|
|
187
|
+
* The block selection, by block ID, or null for a text selection. While it
|
|
188
|
+
* is set, the model selection spans the selected blocks, so block type,
|
|
189
|
+
* indent and mark commands apply to all of them, and the browser selection
|
|
190
|
+
* is a hidden caret at the start of the head block.
|
|
191
|
+
*/
|
|
192
|
+
private blockSel;
|
|
193
|
+
/** Block elements that carry the selected class. */
|
|
194
|
+
private selectedEls;
|
|
195
|
+
constructor(options: EditorOptions);
|
|
196
|
+
/** The document as Markdown. Blocks that were not edited are written exactly as they were loaded. */
|
|
197
|
+
getMarkdown(): string;
|
|
198
|
+
/**
|
|
199
|
+
* Replaces the whole document. Does not emit `change`. By default clears
|
|
200
|
+
* undo history; with `history: 'keep'` the replacement is one undo step.
|
|
201
|
+
*/
|
|
202
|
+
setMarkdown(markdown: string, options?: {
|
|
203
|
+
history?: 'clear' | 'keep';
|
|
204
|
+
}): void;
|
|
205
|
+
/** True when the document holds only empty paragraphs. */
|
|
206
|
+
get isEmpty(): boolean;
|
|
207
|
+
/** The document and selection. Immutable; every change replaces it. */
|
|
208
|
+
get state(): EditorState;
|
|
209
|
+
/** True while an input method (IME) is composing text. Replacing the document now would break the composition. */
|
|
210
|
+
get isComposing(): boolean;
|
|
211
|
+
get isEditable(): boolean;
|
|
212
|
+
/** True if `name` applies at the selection: a mark on the selected text, or the type of the block with the caret. */
|
|
213
|
+
isActive(name: ActiveName, attrs?: Readonly<Record<string, unknown>>): boolean;
|
|
214
|
+
/** 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. */
|
|
215
|
+
getAttributes(name: ActiveName): Record<string, unknown>;
|
|
216
|
+
getSelection(): Selection;
|
|
217
|
+
/**
|
|
218
|
+
* Moves the selection. Accepts a Selection or a SelectionJSON. A value
|
|
219
|
+
* that does not fit the document is clamped to the nearest valid position;
|
|
220
|
+
* a value that is not a selection at all is ignored and returns false.
|
|
221
|
+
*/
|
|
222
|
+
setSelection(selection: Selection | SelectionJSON): boolean;
|
|
223
|
+
/** The selection as JSON-safe data that survives reloading the same Markdown. */
|
|
224
|
+
selectionToJSON(): SelectionJSON;
|
|
225
|
+
hasFocus(): boolean;
|
|
226
|
+
/**
|
|
227
|
+
* Shows app UI after the block that contains `at`. The widget is never
|
|
228
|
+
* saved and is not part of undo history. It moves with its block; if the
|
|
229
|
+
* block is deleted, it moves to the nearest remaining block.
|
|
230
|
+
*/
|
|
231
|
+
addWidget(options: {
|
|
232
|
+
at: Position | 'end';
|
|
233
|
+
render: WidgetRender;
|
|
234
|
+
}): Widget;
|
|
235
|
+
/**
|
|
236
|
+
* Moves widgets whose block is gone. `byIndex` (after setMarkdown, when
|
|
237
|
+
* every block is new) keeps the same block index; otherwise a widget moves
|
|
238
|
+
* to the nearest earlier block that remains, or else the nearest later one.
|
|
239
|
+
*/
|
|
240
|
+
private relocateWidgets;
|
|
241
|
+
/** Focuses the editor at the current selection. */
|
|
242
|
+
focus(): void;
|
|
243
|
+
blur(): void;
|
|
244
|
+
/** Changes `placeholder`, `editable`, `attributes`, `resolveImageSrc` or `theme`. */
|
|
245
|
+
setOptions(options: RuntimeOptions): void;
|
|
246
|
+
/** Subscribes to an event. Returns a function that unsubscribes. */
|
|
247
|
+
on<K extends keyof EditorEvents>(type: K, handler: (payload: EditorEvents[K]) => void): () => void;
|
|
248
|
+
private emit;
|
|
249
|
+
private applyRuntime;
|
|
250
|
+
/** Sets the theme. Returns true when blocks must re-render for new classes. */
|
|
251
|
+
private applyTheme;
|
|
252
|
+
/** Applies a command (a pure function of the state), records history, re-renders, and emits events. */
|
|
253
|
+
run(command: (state: EditorState) => EditorState | null, kind?: ChangeKind): boolean;
|
|
254
|
+
private createCommands;
|
|
255
|
+
private toggleMark;
|
|
256
|
+
private undo;
|
|
257
|
+
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;
|
|
262
|
+
destroy(): void;
|
|
263
|
+
private apply;
|
|
264
|
+
private commit;
|
|
265
|
+
/** Emits a pending change now, so a submit or blur handler sees the latest Markdown. */
|
|
266
|
+
private flushChange;
|
|
267
|
+
private scheduleChange;
|
|
268
|
+
private emitChange;
|
|
269
|
+
/**
|
|
270
|
+
* Emits a pending change, then `submit` (Mod-Enter and Ctrl-Enter call
|
|
271
|
+
* this). Returns false, doing nothing, when no one listens for submit.
|
|
272
|
+
*/
|
|
273
|
+
submit(): boolean;
|
|
274
|
+
/**
|
|
275
|
+
* Applies read-only state to the root and to rendered cells and inputs.
|
|
276
|
+
* New elements render editable, so this only does work when read-only is on
|
|
277
|
+
* or has just changed.
|
|
278
|
+
*/
|
|
279
|
+
private applyReadOnly;
|
|
280
|
+
/** Hints for empty blocks, and the UI that follows the selection. */
|
|
281
|
+
private afterRender;
|
|
282
|
+
private syncDomSelection;
|
|
283
|
+
private toDom;
|
|
284
|
+
private readDomSelection;
|
|
285
|
+
private onSelectionChange;
|
|
286
|
+
/**
|
|
287
|
+
* Renders the document, then puts the browser selection back. Only blocks
|
|
288
|
+
* whose object or revealed views changed are rebuilt, unless `force`.
|
|
289
|
+
* `hadFocus`: whether the editor had focus before the change (re-rendering
|
|
290
|
+
* a table replaces the focused cell).
|
|
291
|
+
*/
|
|
292
|
+
private paint;
|
|
293
|
+
/** Re-renders if the views that show their source changed: after the selection, focus or editability changes. */
|
|
294
|
+
private refreshRevealed;
|
|
295
|
+
private onMouseUp;
|
|
296
|
+
/**
|
|
297
|
+
* 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.
|
|
300
|
+
*/
|
|
301
|
+
private viewBlockPast;
|
|
302
|
+
/** 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
|
+
private revealView;
|
|
304
|
+
/** The browser selection is the source of truth when an event starts. */
|
|
305
|
+
private refreshSelection;
|
|
306
|
+
/** The range the browser says an input event affects. */
|
|
307
|
+
private targetRange;
|
|
308
|
+
/** Events from the code-language input and other form controls inside the root are not editor input. */
|
|
309
|
+
private fromControl;
|
|
310
|
+
private onBeforeInput;
|
|
311
|
+
private typeText;
|
|
312
|
+
/**
|
|
313
|
+
* True for input the `input` handler can read back from the DOM: text typed
|
|
314
|
+
* or replaced, and deletions inside one text container. Other changes (a new
|
|
315
|
+
* block, a deletion that joins blocks) are applied to the model instead.
|
|
316
|
+
*/
|
|
317
|
+
private browserApplies;
|
|
318
|
+
/** Markdown shortcuts and suggestion menus that typing `text` triggers. */
|
|
319
|
+
private afterTyping;
|
|
320
|
+
private onCompositionStart;
|
|
321
|
+
private onCompositionEnd;
|
|
322
|
+
private settleComposition;
|
|
323
|
+
private onInput;
|
|
324
|
+
/**
|
|
325
|
+
* Reads the composed text into the model while composing. The browser's
|
|
326
|
+
* DOM for the block already shows it, so the block is not re-rendered.
|
|
327
|
+
*/
|
|
328
|
+
private syncComposition;
|
|
329
|
+
/**
|
|
330
|
+
* Reads text back from the DOM and applies the difference to the model as
|
|
331
|
+
* one delete and one insert per changed text container.
|
|
332
|
+
*/
|
|
333
|
+
private reconcileFromDom;
|
|
334
|
+
/**
|
|
335
|
+
* The model with the text the DOM shows: one delete and one insert per
|
|
336
|
+
* changed text container, inserted text getting `marks` when set. Only
|
|
337
|
+
* `blockId` when given. Null when the text is the same.
|
|
338
|
+
*/
|
|
339
|
+
private readDom;
|
|
340
|
+
private onKeyDown;
|
|
341
|
+
private moveMenu;
|
|
342
|
+
private onMouseDown;
|
|
343
|
+
/**
|
|
344
|
+
* A click in the empty space below a final code block, table or divider
|
|
345
|
+
* adds a paragraph there. The browser would put the caret inside the
|
|
346
|
+
* block, or in the block above a table.
|
|
347
|
+
*/
|
|
348
|
+
private clickBelowLastBlock;
|
|
349
|
+
private onClick;
|
|
350
|
+
/** The code block language input. */
|
|
351
|
+
private onControlChange;
|
|
352
|
+
private onFocusIn;
|
|
353
|
+
private onFocusOut;
|
|
354
|
+
private onScroll;
|
|
355
|
+
private onCopy;
|
|
356
|
+
private onPaste;
|
|
357
|
+
/** Clipboard content as blocks: the editor's own Markdown, other HTML, or text as Markdown. */
|
|
358
|
+
private pastedBlocks;
|
|
359
|
+
private onDrop;
|
|
360
|
+
/** The document position at a viewport point, or null outside the editor's text. */
|
|
361
|
+
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;
|
|
371
|
+
/** The slash menu, as a suggestion. */
|
|
372
|
+
private slashSuggestion;
|
|
373
|
+
/** The blocks of the block selection, in document order. Empty when the selection is text. */
|
|
374
|
+
get selectedBlocks(): readonly Block[];
|
|
375
|
+
private blockRange;
|
|
376
|
+
/** Selects whole blocks, from the block `anchor` to the block `head`. */
|
|
377
|
+
private selectBlockRange;
|
|
378
|
+
/** Leaves block selection, keeping the hidden caret as a collapsed selection. */
|
|
379
|
+
private dropBlockSelection;
|
|
380
|
+
/** Leaves block selection with the caret at `pos`. */
|
|
381
|
+
private exitBlockSelection;
|
|
382
|
+
/** Where the hidden browser caret sits during a block selection: the start of the head block. */
|
|
383
|
+
private parkedCaret;
|
|
384
|
+
private paintBlockSelection;
|
|
385
|
+
private deleteSelectedBlocks;
|
|
386
|
+
/**
|
|
387
|
+
* Keys while whole blocks are selected. Arrows move the selection by
|
|
388
|
+
* block (Shift extends it), Enter and Left/Right go back to editing text,
|
|
389
|
+
* Backspace and Delete remove the blocks, Mod+A selects every block,
|
|
390
|
+
* Mod+D duplicates, and Escape leaves the editor. Other keys go to the
|
|
391
|
+
* key bindings, which act on all selected blocks.
|
|
392
|
+
*/
|
|
393
|
+
private onBlockKey;
|
|
394
|
+
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
|
+
private closeBlockMenu;
|
|
402
|
+
openLinkPopover(): void;
|
|
403
|
+
private updateUi;
|
|
404
|
+
private openMenu;
|
|
405
|
+
private syncActiveDescendant;
|
|
406
|
+
private hideUi;
|
|
407
|
+
private listen;
|
|
408
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Decorator } from './decorations.js';
|
|
2
|
+
import type { Editor } from './editor.js';
|
|
3
|
+
import type { SuggestionConfig } from './extensions/suggestion.js';
|
|
4
|
+
import type { MarkdownSyntax } from './markdown/options.js';
|
|
5
|
+
import type { Position } from './model.js';
|
|
6
|
+
import type { ThemeClasses } from './theme.js';
|
|
7
|
+
export interface ExtensionContext {
|
|
8
|
+
readonly editor: Editor;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Handles a key. Return true to stop: no later binding runs and the browser
|
|
12
|
+
* default is prevented.
|
|
13
|
+
*/
|
|
14
|
+
export type KeyHandler = (ctx: ExtensionContext) => boolean;
|
|
15
|
+
/**
|
|
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.
|
|
20
|
+
*/
|
|
21
|
+
export type SyntaxView = (source: string, element: HTMLElement, kind: 'inline' | 'block') => void | false;
|
|
22
|
+
export interface Extension {
|
|
23
|
+
/** Unique name, for debugging and for replacing a built-in extension. */
|
|
24
|
+
readonly name: string;
|
|
25
|
+
/** Markdown syntax this extension adds. */
|
|
26
|
+
readonly markdown?: MarkdownSyntax;
|
|
27
|
+
/**
|
|
28
|
+
* Key bindings, with ProseMirror key names: modifiers `Mod-` (Cmd on
|
|
29
|
+
* macOS, Ctrl elsewhere), `Ctrl-`, `Alt-`, `Shift-`, `Meta-`, then a key
|
|
30
|
+
* such as `b`, `7`, `Enter`, `Tab` or `Escape`.
|
|
31
|
+
*/
|
|
32
|
+
readonly keymap?: Readonly<Record<string, KeyHandler>>;
|
|
33
|
+
/**
|
|
34
|
+
* Classes on ranges of text, for each paragraph, heading, list item,
|
|
35
|
+
* quote and table cell. Recomputed only for blocks that changed. Never saved.
|
|
36
|
+
*/
|
|
37
|
+
readonly decorations?: Decorator;
|
|
38
|
+
/** A popup menu opened by a trigger character. See `suggestion()`. */
|
|
39
|
+
readonly suggestion?: SuggestionConfig;
|
|
40
|
+
/** Called on paste before the editor's own handling. Return true to stop it. */
|
|
41
|
+
readonly handlePaste?: (ctx: ExtensionContext & {
|
|
42
|
+
readonly event: ClipboardEvent;
|
|
43
|
+
}) => boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Called on drop before the editor's own handling, with the document
|
|
46
|
+
* position under the pointer (null outside text). Return true to stop it.
|
|
47
|
+
*/
|
|
48
|
+
readonly handleDrop?: (ctx: ExtensionContext & {
|
|
49
|
+
readonly event: DragEvent;
|
|
50
|
+
readonly position: Position | null;
|
|
51
|
+
}) => boolean;
|
|
52
|
+
/**
|
|
53
|
+
* 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.
|
|
58
|
+
*/
|
|
59
|
+
readonly views?: Readonly<Record<string, SyntaxView>>;
|
|
60
|
+
/**
|
|
61
|
+
* Theme classes for the elements this extension cares about, such as
|
|
62
|
+
* `token.math`. The editor's `theme` wins for the same key, and an earlier
|
|
63
|
+
* extension wins over a later one.
|
|
64
|
+
*/
|
|
65
|
+
readonly theme?: {
|
|
66
|
+
readonly classes?: ThemeClasses;
|
|
67
|
+
};
|
|
68
|
+
/** Called once when the editor is created. May return a cleanup function, called on destroy. */
|
|
69
|
+
readonly onCreate?: (ctx: ExtensionContext) => void | (() => void);
|
|
70
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Editor } from '../editor.js';
|
|
2
|
+
import type { Extension } from '../extension.js';
|
|
3
|
+
import type { Position } from '../model.js';
|
|
4
|
+
export interface FileHandlerOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Which files to take: MIME types or patterns such as `image/*`, or a test
|
|
7
|
+
* function. Default: every file.
|
|
8
|
+
*/
|
|
9
|
+
accept?: readonly string[] | ((file: File) => boolean);
|
|
10
|
+
/**
|
|
11
|
+
* Pasted files. Called only when the clipboard has no plain text, so
|
|
12
|
+
* copying text from a page that also puts an image on the clipboard still
|
|
13
|
+
* pastes the text.
|
|
14
|
+
*/
|
|
15
|
+
onPaste?: (ctx: {
|
|
16
|
+
editor: Editor;
|
|
17
|
+
files: File[];
|
|
18
|
+
event: ClipboardEvent;
|
|
19
|
+
}) => void;
|
|
20
|
+
/** Dropped files, with the document position they were dropped at (null outside text). */
|
|
21
|
+
onDrop?: (ctx: {
|
|
22
|
+
editor: Editor;
|
|
23
|
+
files: File[];
|
|
24
|
+
event: DragEvent;
|
|
25
|
+
position: Position | null;
|
|
26
|
+
}) => void;
|
|
27
|
+
}
|
|
28
|
+
export declare function fileHandler(options: FileHandlerOptions): Extension;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Extension } from '../extension.js';
|
|
2
|
+
export interface MathOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Renders TeX into `element`. `displayMode` is true for a `$$` block.
|
|
5
|
+
* KaTeX's `render` has this shape:
|
|
6
|
+
* `render: (tex, el, options) => katex.render(tex, el, { ...options, throwOnError: false })`.
|
|
7
|
+
* Throwing shows the source instead.
|
|
8
|
+
*/
|
|
9
|
+
readonly render?: (tex: string, element: HTMLElement, options: {
|
|
10
|
+
readonly displayMode: boolean;
|
|
11
|
+
}) => void;
|
|
12
|
+
}
|
|
13
|
+
/** The TeX inside a token (`$x$`, `$$x$$`) or a fenced block (`$$` lines around it). */
|
|
14
|
+
export declare function mathSource(source: string, kind: 'inline' | 'block'): string;
|
|
15
|
+
export declare function math(options?: MathOptions): Extension;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Editor } from '../editor.js';
|
|
2
|
+
import type { Extension } from '../extension.js';
|
|
3
|
+
import type { Position } from '../model.js';
|
|
4
|
+
export interface SuggestionItem {
|
|
5
|
+
/** Stable ID, for tests and keys. Defaults to the label. */
|
|
6
|
+
readonly id?: string;
|
|
7
|
+
readonly label: string;
|
|
8
|
+
/** Short glyph shown before the label. */
|
|
9
|
+
readonly icon?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface SuggestionRange {
|
|
12
|
+
/** The trigger character. */
|
|
13
|
+
readonly from: Position;
|
|
14
|
+
/** The caret: the end of the query. */
|
|
15
|
+
readonly to: Position;
|
|
16
|
+
}
|
|
17
|
+
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. */
|
|
36
|
+
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. */
|
|
38
|
+
readonly allowAnywhere?: boolean;
|
|
39
|
+
/** Longest query that keeps the menu open. Default 30. */
|
|
40
|
+
readonly maxLength?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Shown when there are no items, for queries up to three characters.
|
|
43
|
+
* Without it the menu hides while there are no items.
|
|
44
|
+
*/
|
|
45
|
+
readonly emptyLabel?: string;
|
|
46
|
+
/** Extra class names for the menu element. */
|
|
47
|
+
readonly className?: string;
|
|
48
|
+
}
|
|
49
|
+
export declare function suggestion<T extends SuggestionItem>(config: SuggestionConfig<T> & {
|
|
50
|
+
readonly name?: string;
|
|
51
|
+
}): Extension;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type TextRange } from '../decorations.js';
|
|
2
|
+
import type { Extension } from '../extension.js';
|
|
3
|
+
import type { SuggestionConfig, SuggestionItem } from './suggestion.js';
|
|
4
|
+
/** Finds tags or mentions in `text[from, to)`. `text` is the whole container, so a scanner can look before `from`. */
|
|
5
|
+
export type TagScanner = (text: string, from: number, to: number) => Iterable<TextRange>;
|
|
6
|
+
export interface TagsOptions<T extends SuggestionItem = SuggestionItem> {
|
|
7
|
+
/** Class on each match. Default `mbe-tag` for tags and `mbe-mention` for mentions. */
|
|
8
|
+
readonly class?: string;
|
|
9
|
+
readonly attributes?: Readonly<Record<string, string>>;
|
|
10
|
+
/** Finds matches in a run of text. Default `scanTags` or `scanMentions`. */
|
|
11
|
+
readonly scan?: TagScanner;
|
|
12
|
+
/** A suggestion menu opened by the trigger character. See `suggestion()`. */
|
|
13
|
+
readonly suggestion?: Omit<SuggestionConfig<T>, 'char'>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The default tag grammar. A tag is `#` and one or more segments separated
|
|
17
|
+
* by `/`. A segment starts with a letter, number, `_` or emoji and goes on
|
|
18
|
+
* with letters, combining marks, numbers, `_`, `-` and emoji. The `#` is not
|
|
19
|
+
* preceded by a letter, mark, number, `_` or `#` (`C#`, `a#b` and `##x` are
|
|
20
|
+
* not tags). A trailing `/` is not part of a tag, and a tag of only digits
|
|
21
|
+
* (`#123`) is not a tag.
|
|
22
|
+
*/
|
|
23
|
+
export declare function scanTags(text: string, from?: number, to?: number): Generator<TextRange>;
|
|
24
|
+
/**
|
|
25
|
+
* The default mention grammar: `@` and a name of letters, combining marks,
|
|
26
|
+
* numbers, `_`, `.` and `-` that starts with a letter, number or `_` and does
|
|
27
|
+
* not end with `.` or `-`. The `@` is not preceded by a letter, mark, number,
|
|
28
|
+
* `_`, `.`, `+`, `-` or `@`, so the middle of an e-mail address is no mention.
|
|
29
|
+
*/
|
|
30
|
+
export declare function scanMentions(text: string, from?: number, to?: number): Generator<TextRange>;
|
|
31
|
+
/** `#tag` highlighting, `\#` escapes and an optional `#` suggestion menu. */
|
|
32
|
+
export declare function tags<T extends SuggestionItem>(options?: TagsOptions<T>): Extension;
|
|
33
|
+
/** `@name` highlighting, `\@` escapes and an optional `@` suggestion menu. */
|
|
34
|
+
export declare function mentions<T extends SuggestionItem>(options?: TagsOptions<T>): Extension;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { TextRange } from '../decorations.js';
|
|
2
|
+
import type { Extension } from '../extension.js';
|
|
3
|
+
export interface TextDecoratorOptions {
|
|
4
|
+
/** Extension name. Default "textDecorator". */
|
|
5
|
+
name?: string;
|
|
6
|
+
/** Every match gets `class`. The `g` flag is added if missing. */
|
|
7
|
+
pattern?: RegExp;
|
|
8
|
+
/** Returns ranges to decorate. A range's own `class` replaces the default one. */
|
|
9
|
+
scan?: (text: string) => Iterable<TextRange & {
|
|
10
|
+
class?: string;
|
|
11
|
+
attributes?: Readonly<Record<string, string>>;
|
|
12
|
+
}>;
|
|
13
|
+
/** Class for each range. */
|
|
14
|
+
class: string;
|
|
15
|
+
attributes?: Readonly<Record<string, string>>;
|
|
16
|
+
}
|
|
17
|
+
export declare function textDecorator(options: TextDecoratorOptions): Extension;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { EditorState } from './model.js';
|
|
2
|
+
export type ChangeKind = 'typing' | 'other';
|
|
3
|
+
export declare class History {
|
|
4
|
+
private undoStack;
|
|
5
|
+
private redoStack;
|
|
6
|
+
private lastKind;
|
|
7
|
+
private lastTime;
|
|
8
|
+
/** Call before applying a change, with the state the change starts from. */
|
|
9
|
+
record(before: EditorState, kind: ChangeKind, now?: number): void;
|
|
10
|
+
clear(): void;
|
|
11
|
+
/** Ends the current typing group, e.g. when the caret moves. */
|
|
12
|
+
breakGroup(): void;
|
|
13
|
+
undo(current: EditorState): EditorState | null;
|
|
14
|
+
redo(current: EditorState): EditorState | null;
|
|
15
|
+
/** Moves the newest entry of `from` back into the document, and the current state onto `to`. */
|
|
16
|
+
private step;
|
|
17
|
+
}
|