UNPKG

19.6 kBTypeScriptView Raw
1import { Ancestor, Descendant, Element, ExtendedType, Location, Node, NodeEntry, Operation, Path, PathRef, Point, PointRef, Range, RangeRef, Span, Text, Transforms } from '..';
2import { LeafEdge, MaximizeMode, RangeDirection, SelectionMode, TextDirection, TextUnit, TextUnitAdjustment } from '../types/types';
3import { OmitFirstArg } from '../utils/types';
4import { TextInsertFragmentOptions, TextInsertTextOptions } from './transforms/text';
5import { NodeInsertNodesOptions } from './transforms/node';
6/**
7 * The `Editor` interface stores all the state of a Slate editor. It is extended
8 * by plugins that wish to add their own helpers and implement new behaviors.
9 */
10export interface BaseEditor {
11 children: Descendant[];
12 selection: Selection;
13 operations: Operation[];
14 marks: EditorMarks | null;
15 apply: (operation: Operation) => void;
16 getDirtyPaths: (operation: Operation) => Path[];
17 getFragment: () => Descendant[];
18 isElementReadOnly: (element: Element) => boolean;
19 isSelectable: (element: Element) => boolean;
20 markableVoid: (element: Element) => boolean;
21 normalizeNode: (entry: NodeEntry, options?: {
22 operation?: Operation;
23 }) => void;
24 onChange: (options?: {
25 operation?: Operation;
26 }) => void;
27 shouldNormalize: ({ iteration, dirtyPaths, operation, }: {
28 iteration: number;
29 initialDirtyPathsLength: number;
30 dirtyPaths: Path[];
31 operation?: Operation;
32 }) => boolean;
33 addMark: OmitFirstArg<typeof Editor.addMark>;
34 collapse: OmitFirstArg<typeof Transforms.collapse>;
35 delete: OmitFirstArg<typeof Transforms.delete>;
36 deleteBackward: (unit: TextUnit) => void;
37 deleteForward: (unit: TextUnit) => void;
38 deleteFragment: OmitFirstArg<typeof Editor.deleteFragment>;
39 deselect: OmitFirstArg<typeof Transforms.deselect>;
40 insertBreak: OmitFirstArg<typeof Editor.insertBreak>;
41 insertFragment: OmitFirstArg<typeof Transforms.insertFragment>;
42 insertNode: OmitFirstArg<typeof Editor.insertNode>;
43 insertNodes: OmitFirstArg<typeof Transforms.insertNodes>;
44 insertSoftBreak: OmitFirstArg<typeof Editor.insertSoftBreak>;
45 insertText: OmitFirstArg<typeof Transforms.insertText>;
46 liftNodes: OmitFirstArg<typeof Transforms.liftNodes>;
47 mergeNodes: OmitFirstArg<typeof Transforms.mergeNodes>;
48 move: OmitFirstArg<typeof Transforms.move>;
49 moveNodes: OmitFirstArg<typeof Transforms.moveNodes>;
50 normalize: OmitFirstArg<typeof Editor.normalize>;
51 removeMark: OmitFirstArg<typeof Editor.removeMark>;
52 removeNodes: OmitFirstArg<typeof Transforms.removeNodes>;
53 select: OmitFirstArg<typeof Transforms.select>;
54 setNodes: <T extends Node>(props: Partial<T>, options?: {
55 at?: Location;
56 match?: NodeMatch<T>;
57 mode?: MaximizeMode;
58 hanging?: boolean;
59 split?: boolean;
60 voids?: boolean;
61 compare?: PropsCompare;
62 merge?: PropsMerge;
63 }) => void;
64 setNormalizing: OmitFirstArg<typeof Editor.setNormalizing>;
65 setPoint: OmitFirstArg<typeof Transforms.setPoint>;
66 setSelection: OmitFirstArg<typeof Transforms.setSelection>;
67 splitNodes: OmitFirstArg<typeof Transforms.splitNodes>;
68 unsetNodes: OmitFirstArg<typeof Transforms.unsetNodes>;
69 unwrapNodes: OmitFirstArg<typeof Transforms.unwrapNodes>;
70 withoutNormalizing: OmitFirstArg<typeof Editor.withoutNormalizing>;
71 wrapNodes: OmitFirstArg<typeof Transforms.wrapNodes>;
72 above: <T extends Ancestor>(options?: EditorAboveOptions<T>) => NodeEntry<T> | undefined;
73 after: OmitFirstArg<typeof Editor.after>;
74 before: OmitFirstArg<typeof Editor.before>;
75 edges: OmitFirstArg<typeof Editor.edges>;
76 elementReadOnly: OmitFirstArg<typeof Editor.elementReadOnly>;
77 end: OmitFirstArg<typeof Editor.end>;
78 first: OmitFirstArg<typeof Editor.first>;
79 fragment: OmitFirstArg<typeof Editor.fragment>;
80 getMarks: OmitFirstArg<typeof Editor.marks>;
81 hasBlocks: OmitFirstArg<typeof Editor.hasBlocks>;
82 hasInlines: OmitFirstArg<typeof Editor.hasInlines>;
83 hasPath: OmitFirstArg<typeof Editor.hasPath>;
84 hasTexts: OmitFirstArg<typeof Editor.hasTexts>;
85 isBlock: OmitFirstArg<typeof Editor.isBlock>;
86 isEdge: OmitFirstArg<typeof Editor.isEdge>;
87 isEmpty: OmitFirstArg<typeof Editor.isEmpty>;
88 isEnd: OmitFirstArg<typeof Editor.isEnd>;
89 isInline: OmitFirstArg<typeof Editor.isInline>;
90 isNormalizing: OmitFirstArg<typeof Editor.isNormalizing>;
91 isStart: OmitFirstArg<typeof Editor.isStart>;
92 isVoid: OmitFirstArg<typeof Editor.isVoid>;
93 last: OmitFirstArg<typeof Editor.last>;
94 leaf: OmitFirstArg<typeof Editor.leaf>;
95 levels: <T extends Node>(options?: EditorLevelsOptions<T>) => Generator<NodeEntry<T>, void, undefined>;
96 next: <T extends Descendant>(options?: EditorNextOptions<T>) => NodeEntry<T> | undefined;
97 node: OmitFirstArg<typeof Editor.node>;
98 nodes: <T extends Node>(options?: EditorNodesOptions<T>) => Generator<NodeEntry<T>, void, undefined>;
99 parent: OmitFirstArg<typeof Editor.parent>;
100 path: OmitFirstArg<typeof Editor.path>;
101 pathRef: OmitFirstArg<typeof Editor.pathRef>;
102 pathRefs: OmitFirstArg<typeof Editor.pathRefs>;
103 point: OmitFirstArg<typeof Editor.point>;
104 pointRef: OmitFirstArg<typeof Editor.pointRef>;
105 pointRefs: OmitFirstArg<typeof Editor.pointRefs>;
106 positions: OmitFirstArg<typeof Editor.positions>;
107 previous: <T extends Node>(options?: EditorPreviousOptions<T>) => NodeEntry<T> | undefined;
108 range: OmitFirstArg<typeof Editor.range>;
109 rangeRef: OmitFirstArg<typeof Editor.rangeRef>;
110 rangeRefs: OmitFirstArg<typeof Editor.rangeRefs>;
111 start: OmitFirstArg<typeof Editor.start>;
112 string: OmitFirstArg<typeof Editor.string>;
113 unhangRange: OmitFirstArg<typeof Editor.unhangRange>;
114 void: OmitFirstArg<typeof Editor.void>;
115 shouldMergeNodesRemovePrevNode: OmitFirstArg<typeof Editor.shouldMergeNodesRemovePrevNode>;
116}
117export type Editor = ExtendedType<'Editor', BaseEditor>;
118export type BaseSelection = Range | null;
119export type Selection = ExtendedType<'Selection', BaseSelection>;
120export type EditorMarks = Omit<Text, 'text'>;
121export interface EditorAboveOptions<T extends Ancestor> {
122 at?: Location;
123 match?: NodeMatch<T>;
124 mode?: MaximizeMode;
125 voids?: boolean;
126}
127export interface EditorAfterOptions {
128 distance?: number;
129 unit?: TextUnitAdjustment;
130 voids?: boolean;
131}
132export interface EditorBeforeOptions {
133 distance?: number;
134 unit?: TextUnitAdjustment;
135 voids?: boolean;
136}
137export interface EditorDirectedDeletionOptions {
138 unit?: TextUnit;
139}
140export interface EditorElementReadOnlyOptions {
141 at?: Location;
142 mode?: MaximizeMode;
143 voids?: boolean;
144}
145export interface EditorFragmentDeletionOptions {
146 direction?: TextDirection;
147}
148export interface EditorLeafOptions {
149 depth?: number;
150 edge?: LeafEdge;
151}
152export interface EditorLevelsOptions<T extends Node> {
153 at?: Location;
154 match?: NodeMatch<T>;
155 reverse?: boolean;
156 voids?: boolean;
157}
158export interface EditorNextOptions<T extends Descendant> {
159 at?: Location;
160 match?: NodeMatch<T>;
161 mode?: SelectionMode;
162 voids?: boolean;
163}
164export interface EditorNodeOptions {
165 depth?: number;
166 edge?: LeafEdge;
167}
168export interface EditorNodesOptions<T extends Node> {
169 at?: Location | Span;
170 match?: NodeMatch<T>;
171 mode?: SelectionMode;
172 universal?: boolean;
173 reverse?: boolean;
174 voids?: boolean;
175 ignoreNonSelectable?: boolean;
176}
177export interface EditorNormalizeOptions {
178 force?: boolean;
179 operation?: Operation;
180}
181export interface EditorParentOptions {
182 depth?: number;
183 edge?: LeafEdge;
184}
185export interface EditorPathOptions {
186 depth?: number;
187 edge?: LeafEdge;
188}
189export interface EditorPathRefOptions {
190 affinity?: TextDirection | null;
191}
192export interface EditorPointOptions {
193 edge?: LeafEdge;
194}
195export interface EditorPointRefOptions {
196 affinity?: TextDirection | null;
197}
198export interface EditorPositionsOptions {
199 at?: Location;
200 unit?: TextUnitAdjustment;
201 reverse?: boolean;
202 voids?: boolean;
203 ignoreNonSelectable?: boolean;
204}
205export interface EditorPreviousOptions<T extends Node> {
206 at?: Location;
207 match?: NodeMatch<T>;
208 mode?: SelectionMode;
209 voids?: boolean;
210}
211export interface EditorRangeRefOptions {
212 affinity?: RangeDirection | null;
213}
214export interface EditorStringOptions {
215 voids?: boolean;
216}
217export interface EditorUnhangRangeOptions {
218 voids?: boolean;
219}
220export interface EditorVoidOptions {
221 at?: Location;
222 mode?: MaximizeMode;
223 voids?: boolean;
224}
225export interface EditorInterface {
226 /**
227 * Get the ancestor above a location in the document.
228 */
229 above: <T extends Ancestor>(editor: Editor, options?: EditorAboveOptions<T>) => NodeEntry<T> | undefined;
230 /**
231 * Add a custom property to the leaf text nodes in the current selection.
232 *
233 * If the selection is currently collapsed, the marks will be added to the
234 * `editor.marks` property instead, and applied when text is inserted next.
235 */
236 addMark: (editor: Editor, key: string, value: any) => void;
237 /**
238 * Get the point after a location.
239 */
240 after: (editor: Editor, at: Location, options?: EditorAfterOptions) => Point | undefined;
241 /**
242 * Get the point before a location.
243 */
244 before: (editor: Editor, at: Location, options?: EditorBeforeOptions) => Point | undefined;
245 /**
246 * Delete content in the editor backward from the current selection.
247 */
248 deleteBackward: (editor: Editor, options?: EditorDirectedDeletionOptions) => void;
249 /**
250 * Delete content in the editor forward from the current selection.
251 */
252 deleteForward: (editor: Editor, options?: EditorDirectedDeletionOptions) => void;
253 /**
254 * Delete the content in the current selection.
255 */
256 deleteFragment: (editor: Editor, options?: EditorFragmentDeletionOptions) => void;
257 /**
258 * Get the start and end points of a location.
259 */
260 edges: (editor: Editor, at: Location) => [Point, Point];
261 /**
262 * Match a read-only element in the current branch of the editor.
263 */
264 elementReadOnly: (editor: Editor, options?: EditorElementReadOnlyOptions) => NodeEntry<Element> | undefined;
265 /**
266 * Get the end point of a location.
267 */
268 end: (editor: Editor, at: Location) => Point;
269 /**
270 * Get the first node at a location.
271 */
272 first: (editor: Editor, at: Location) => NodeEntry;
273 /**
274 * Get the fragment at a location.
275 */
276 fragment: (editor: Editor, at: Location) => Descendant[];
277 /**
278 * Check if a node has block children.
279 */
280 hasBlocks: (editor: Editor, element: Element) => boolean;
281 /**
282 * Check if a node has inline and text children.
283 */
284 hasInlines: (editor: Editor, element: Element) => boolean;
285 hasPath: (editor: Editor, path: Path) => boolean;
286 /**
287 * Check if a node has text children.
288 */
289 hasTexts: (editor: Editor, element: Element) => boolean;
290 /**
291 * Insert a block break at the current selection.
292 *
293 * If the selection is currently expanded, it will be deleted first.
294 */
295 insertBreak: (editor: Editor) => void;
296 /**
297 * Inserts a fragment
298 * at the specified location or (if not defined) the current selection or (if not defined) the end of the document.
299 */
300 insertFragment: (editor: Editor, fragment: Node[], options?: TextInsertFragmentOptions) => void;
301 /**
302 * Atomically inserts `nodes`
303 * at the specified location or (if not defined) the current selection or (if not defined) the end of the document.
304 */
305 insertNode: <T extends Node>(editor: Editor, node: Node, options?: NodeInsertNodesOptions<T>) => void;
306 /**
307 * Insert a soft break at the current selection.
308 *
309 * If the selection is currently expanded, it will be deleted first.
310 */
311 insertSoftBreak: (editor: Editor) => void;
312 /**
313 * Insert a string of text
314 * at the specified location or (if not defined) the current selection or (if not defined) the end of the document.
315 */
316 insertText: (editor: Editor, text: string, options?: TextInsertTextOptions) => void;
317 /**
318 * Check if a value is a block `Element` object.
319 */
320 isBlock: (editor: Editor, value: Element) => boolean;
321 /**
322 * Check if a point is an edge of a location.
323 */
324 isEdge: (editor: Editor, point: Point, at: Location) => boolean;
325 /**
326 * Check if a value is an `Editor` object.
327 */
328 isEditor: (value: any) => value is Editor;
329 /**
330 * Check if a value is a read-only `Element` object.
331 */
332 isElementReadOnly: (editor: Editor, element: Element) => boolean;
333 /**
334 * Check if an element is empty, accounting for void nodes.
335 */
336 isEmpty: (editor: Editor, element: Element) => boolean;
337 /**
338 * Check if a point is the end point of a location.
339 */
340 isEnd: (editor: Editor, point: Point, at: Location) => boolean;
341 /**
342 * Check if a value is an inline `Element` object.
343 */
344 isInline: (editor: Editor, value: Element) => boolean;
345 /**
346 * Check if the editor is currently normalizing after each operation.
347 */
348 isNormalizing: (editor: Editor) => boolean;
349 /**
350 * Check if a value is a selectable `Element` object.
351 */
352 isSelectable: (editor: Editor, element: Element) => boolean;
353 /**
354 * Check if a point is the start point of a location.
355 */
356 isStart: (editor: Editor, point: Point, at: Location) => boolean;
357 /**
358 * Check if a value is a void `Element` object.
359 */
360 isVoid: (editor: Editor, value: Element) => boolean;
361 /**
362 * Get the last node at a location.
363 */
364 last: (editor: Editor, at: Location) => NodeEntry;
365 /**
366 * Get the leaf text node at a location.
367 */
368 leaf: (editor: Editor, at: Location, options?: EditorLeafOptions) => NodeEntry<Text>;
369 /**
370 * Iterate through all of the levels at a location.
371 */
372 levels: <T extends Node>(editor: Editor, options?: EditorLevelsOptions<T>) => Generator<NodeEntry<T>, void, undefined>;
373 /**
374 * Get the marks that would be added to text at the current selection.
375 */
376 marks: (editor: Editor) => Omit<Text, 'text'> | null;
377 /**
378 * Get the matching node in the branch of the document after a location.
379 */
380 next: <T extends Descendant>(editor: Editor, options?: EditorNextOptions<T>) => NodeEntry<T> | undefined;
381 /**
382 * Get the node at a location.
383 */
384 node: (editor: Editor, at: Location, options?: EditorNodeOptions) => NodeEntry;
385 /**
386 * Iterate through all of the nodes in the Editor.
387 */
388 nodes: <T extends Node>(editor: Editor, options?: EditorNodesOptions<T>) => Generator<NodeEntry<T>, void, undefined>;
389 /**
390 * Normalize any dirty objects in the editor.
391 */
392 normalize: (editor: Editor, options?: EditorNormalizeOptions) => void;
393 /**
394 * Get the parent node of a location.
395 */
396 parent: (editor: Editor, at: Location, options?: EditorParentOptions) => NodeEntry<Ancestor>;
397 /**
398 * Get the path of a location.
399 */
400 path: (editor: Editor, at: Location, options?: EditorPathOptions) => Path;
401 /**
402 * Create a mutable ref for a `Path` object, which will stay in sync as new
403 * operations are applied to the editor.
404 */
405 pathRef: (editor: Editor, path: Path, options?: EditorPathRefOptions) => PathRef;
406 /**
407 * Get the set of currently tracked path refs of the editor.
408 */
409 pathRefs: (editor: Editor) => Set<PathRef>;
410 /**
411 * Get the start or end point of a location.
412 */
413 point: (editor: Editor, at: Location, options?: EditorPointOptions) => Point;
414 /**
415 * Create a mutable ref for a `Point` object, which will stay in sync as new
416 * operations are applied to the editor.
417 */
418 pointRef: (editor: Editor, point: Point, options?: EditorPointRefOptions) => PointRef;
419 /**
420 * Get the set of currently tracked point refs of the editor.
421 */
422 pointRefs: (editor: Editor) => Set<PointRef>;
423 /**
424 * Return all the positions in `at` range where a `Point` can be placed.
425 *
426 * By default, moves forward by individual offsets at a time, but
427 * the `unit` option can be used to to move by character, word, line, or block.
428 *
429 * The `reverse` option can be used to change iteration direction.
430 *
431 * Note: By default void nodes are treated as a single point and iteration
432 * will not happen inside their content unless you pass in true for the
433 * `voids` option, then iteration will occur.
434 */
435 positions: (editor: Editor, options?: EditorPositionsOptions) => Generator<Point, void, undefined>;
436 /**
437 * Get the matching node in the branch of the document before a location.
438 */
439 previous: <T extends Node>(editor: Editor, options?: EditorPreviousOptions<T>) => NodeEntry<T> | undefined;
440 /**
441 * Get a range of a location.
442 */
443 range: (editor: Editor, at: Location, to?: Location) => Range;
444 /**
445 * Create a mutable ref for a `Range` object, which will stay in sync as new
446 * operations are applied to the editor.
447 */
448 rangeRef: (editor: Editor, range: Range, options?: EditorRangeRefOptions) => RangeRef;
449 /**
450 * Get the set of currently tracked range refs of the editor.
451 */
452 rangeRefs: (editor: Editor) => Set<RangeRef>;
453 /**
454 * Remove a custom property from all of the leaf text nodes in the current
455 * selection.
456 *
457 * If the selection is currently collapsed, the removal will be stored on
458 * `editor.marks` and applied to the text inserted next.
459 */
460 removeMark: (editor: Editor, key: string) => void;
461 /**
462 * Manually set if the editor should currently be normalizing.
463 *
464 * Note: Using this incorrectly can leave the editor in an invalid state.
465 *
466 */
467 setNormalizing: (editor: Editor, isNormalizing: boolean) => void;
468 /**
469 * Get the start point of a location.
470 */
471 start: (editor: Editor, at: Location) => Point;
472 /**
473 * Get the text string content of a location.
474 *
475 * Note: by default the text of void nodes is considered to be an empty
476 * string, regardless of content, unless you pass in true for the voids option
477 */
478 string: (editor: Editor, at: Location, options?: EditorStringOptions) => string;
479 /**
480 * Convert a range into a non-hanging one.
481 */
482 unhangRange: (editor: Editor, range: Range, options?: EditorUnhangRangeOptions) => Range;
483 /**
484 * Match a void node in the current branch of the editor.
485 */
486 void: (editor: Editor, options?: EditorVoidOptions) => NodeEntry<Element> | undefined;
487 /**
488 * Call a function, deferring normalization until after it completes.
489 */
490 withoutNormalizing: (editor: Editor, fn: () => void) => void;
491 /**
492 * Call a function, Determine whether or not remove the previous node when merge.
493 */
494 shouldMergeNodesRemovePrevNode: (editor: Editor, prevNodeEntry: NodeEntry, curNodeEntry: NodeEntry) => boolean;
495}
496export declare const Editor: EditorInterface;
497/**
498 * A helper type for narrowing matched nodes with a predicate.
499 */
500export type NodeMatch<T extends Node> = ((node: Node, path: Path) => node is T) | ((node: Node, path: Path) => boolean);
501export type PropsCompare = (prop: Partial<Node>, node: Partial<Node>) => boolean;
502export type PropsMerge = (prop: Partial<Node>, node: Partial<Node>) => object;
503//# sourceMappingURL=editor.d.ts.map
\No newline at end of file