UNPKG

19.3 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}
116export type Editor = ExtendedType<'Editor', BaseEditor>;
117export type BaseSelection = Range | null;
118export type Selection = ExtendedType<'Selection', BaseSelection>;
119export type EditorMarks = Omit<Text, 'text'>;
120export interface EditorAboveOptions<T extends Ancestor> {
121 at?: Location;
122 match?: NodeMatch<T>;
123 mode?: MaximizeMode;
124 voids?: boolean;
125}
126export interface EditorAfterOptions {
127 distance?: number;
128 unit?: TextUnitAdjustment;
129 voids?: boolean;
130}
131export interface EditorBeforeOptions {
132 distance?: number;
133 unit?: TextUnitAdjustment;
134 voids?: boolean;
135}
136export interface EditorDirectedDeletionOptions {
137 unit?: TextUnit;
138}
139export interface EditorElementReadOnlyOptions {
140 at?: Location;
141 mode?: MaximizeMode;
142 voids?: boolean;
143}
144export interface EditorFragmentDeletionOptions {
145 direction?: TextDirection;
146}
147export interface EditorLeafOptions {
148 depth?: number;
149 edge?: LeafEdge;
150}
151export interface EditorLevelsOptions<T extends Node> {
152 at?: Location;
153 match?: NodeMatch<T>;
154 reverse?: boolean;
155 voids?: boolean;
156}
157export interface EditorNextOptions<T extends Descendant> {
158 at?: Location;
159 match?: NodeMatch<T>;
160 mode?: SelectionMode;
161 voids?: boolean;
162}
163export interface EditorNodeOptions {
164 depth?: number;
165 edge?: LeafEdge;
166}
167export interface EditorNodesOptions<T extends Node> {
168 at?: Location | Span;
169 match?: NodeMatch<T>;
170 mode?: SelectionMode;
171 universal?: boolean;
172 reverse?: boolean;
173 voids?: boolean;
174 ignoreNonSelectable?: boolean;
175}
176export interface EditorNormalizeOptions {
177 force?: boolean;
178 operation?: Operation;
179}
180export interface EditorParentOptions {
181 depth?: number;
182 edge?: LeafEdge;
183}
184export interface EditorPathOptions {
185 depth?: number;
186 edge?: LeafEdge;
187}
188export interface EditorPathRefOptions {
189 affinity?: TextDirection | null;
190}
191export interface EditorPointOptions {
192 edge?: LeafEdge;
193}
194export interface EditorPointRefOptions {
195 affinity?: TextDirection | null;
196}
197export interface EditorPositionsOptions {
198 at?: Location;
199 unit?: TextUnitAdjustment;
200 reverse?: boolean;
201 voids?: boolean;
202 ignoreNonSelectable?: boolean;
203}
204export interface EditorPreviousOptions<T extends Node> {
205 at?: Location;
206 match?: NodeMatch<T>;
207 mode?: SelectionMode;
208 voids?: boolean;
209}
210export interface EditorRangeRefOptions {
211 affinity?: RangeDirection | null;
212}
213export interface EditorStringOptions {
214 voids?: boolean;
215}
216export interface EditorUnhangRangeOptions {
217 voids?: boolean;
218}
219export interface EditorVoidOptions {
220 at?: Location;
221 mode?: MaximizeMode;
222 voids?: boolean;
223}
224export interface EditorInterface {
225 /**
226 * Get the ancestor above a location in the document.
227 */
228 above: <T extends Ancestor>(editor: Editor, options?: EditorAboveOptions<T>) => NodeEntry<T> | undefined;
229 /**
230 * Add a custom property to the leaf text nodes in the current selection.
231 *
232 * If the selection is currently collapsed, the marks will be added to the
233 * `editor.marks` property instead, and applied when text is inserted next.
234 */
235 addMark: (editor: Editor, key: string, value: any) => void;
236 /**
237 * Get the point after a location.
238 */
239 after: (editor: Editor, at: Location, options?: EditorAfterOptions) => Point | undefined;
240 /**
241 * Get the point before a location.
242 */
243 before: (editor: Editor, at: Location, options?: EditorBeforeOptions) => Point | undefined;
244 /**
245 * Delete content in the editor backward from the current selection.
246 */
247 deleteBackward: (editor: Editor, options?: EditorDirectedDeletionOptions) => void;
248 /**
249 * Delete content in the editor forward from the current selection.
250 */
251 deleteForward: (editor: Editor, options?: EditorDirectedDeletionOptions) => void;
252 /**
253 * Delete the content in the current selection.
254 */
255 deleteFragment: (editor: Editor, options?: EditorFragmentDeletionOptions) => void;
256 /**
257 * Get the start and end points of a location.
258 */
259 edges: (editor: Editor, at: Location) => [Point, Point];
260 /**
261 * Match a read-only element in the current branch of the editor.
262 */
263 elementReadOnly: (editor: Editor, options?: EditorElementReadOnlyOptions) => NodeEntry<Element> | undefined;
264 /**
265 * Get the end point of a location.
266 */
267 end: (editor: Editor, at: Location) => Point;
268 /**
269 * Get the first node at a location.
270 */
271 first: (editor: Editor, at: Location) => NodeEntry;
272 /**
273 * Get the fragment at a location.
274 */
275 fragment: (editor: Editor, at: Location) => Descendant[];
276 /**
277 * Check if a node has block children.
278 */
279 hasBlocks: (editor: Editor, element: Element) => boolean;
280 /**
281 * Check if a node has inline and text children.
282 */
283 hasInlines: (editor: Editor, element: Element) => boolean;
284 hasPath: (editor: Editor, path: Path) => boolean;
285 /**
286 * Check if a node has text children.
287 */
288 hasTexts: (editor: Editor, element: Element) => boolean;
289 /**
290 * Insert a block break at the current selection.
291 *
292 * If the selection is currently expanded, it will be deleted first.
293 */
294 insertBreak: (editor: Editor) => void;
295 /**
296 * Inserts a fragment
297 * at the specified location or (if not defined) the current selection or (if not defined) the end of the document.
298 */
299 insertFragment: (editor: Editor, fragment: Node[], options?: TextInsertFragmentOptions) => void;
300 /**
301 * Atomically inserts `nodes`
302 * at the specified location or (if not defined) the current selection or (if not defined) the end of the document.
303 */
304 insertNode: <T extends Node>(editor: Editor, node: Node, options?: NodeInsertNodesOptions<T>) => void;
305 /**
306 * Insert a soft break at the current selection.
307 *
308 * If the selection is currently expanded, it will be deleted first.
309 */
310 insertSoftBreak: (editor: Editor) => void;
311 /**
312 * Insert a string of text
313 * at the specified location or (if not defined) the current selection or (if not defined) the end of the document.
314 */
315 insertText: (editor: Editor, text: string, options?: TextInsertTextOptions) => void;
316 /**
317 * Check if a value is a block `Element` object.
318 */
319 isBlock: (editor: Editor, value: Element) => boolean;
320 /**
321 * Check if a point is an edge of a location.
322 */
323 isEdge: (editor: Editor, point: Point, at: Location) => boolean;
324 /**
325 * Check if a value is an `Editor` object.
326 */
327 isEditor: (value: any) => value is Editor;
328 /**
329 * Check if a value is a read-only `Element` object.
330 */
331 isElementReadOnly: (editor: Editor, element: Element) => boolean;
332 /**
333 * Check if an element is empty, accounting for void nodes.
334 */
335 isEmpty: (editor: Editor, element: Element) => boolean;
336 /**
337 * Check if a point is the end point of a location.
338 */
339 isEnd: (editor: Editor, point: Point, at: Location) => boolean;
340 /**
341 * Check if a value is an inline `Element` object.
342 */
343 isInline: (editor: Editor, value: Element) => boolean;
344 /**
345 * Check if the editor is currently normalizing after each operation.
346 */
347 isNormalizing: (editor: Editor) => boolean;
348 /**
349 * Check if a value is a selectable `Element` object.
350 */
351 isSelectable: (editor: Editor, element: Element) => boolean;
352 /**
353 * Check if a point is the start point of a location.
354 */
355 isStart: (editor: Editor, point: Point, at: Location) => boolean;
356 /**
357 * Check if a value is a void `Element` object.
358 */
359 isVoid: (editor: Editor, value: Element) => boolean;
360 /**
361 * Get the last node at a location.
362 */
363 last: (editor: Editor, at: Location) => NodeEntry;
364 /**
365 * Get the leaf text node at a location.
366 */
367 leaf: (editor: Editor, at: Location, options?: EditorLeafOptions) => NodeEntry<Text>;
368 /**
369 * Iterate through all of the levels at a location.
370 */
371 levels: <T extends Node>(editor: Editor, options?: EditorLevelsOptions<T>) => Generator<NodeEntry<T>, void, undefined>;
372 /**
373 * Get the marks that would be added to text at the current selection.
374 */
375 marks: (editor: Editor) => Omit<Text, 'text'> | null;
376 /**
377 * Get the matching node in the branch of the document after a location.
378 */
379 next: <T extends Descendant>(editor: Editor, options?: EditorNextOptions<T>) => NodeEntry<T> | undefined;
380 /**
381 * Get the node at a location.
382 */
383 node: (editor: Editor, at: Location, options?: EditorNodeOptions) => NodeEntry;
384 /**
385 * Iterate through all of the nodes in the Editor.
386 */
387 nodes: <T extends Node>(editor: Editor, options?: EditorNodesOptions<T>) => Generator<NodeEntry<T>, void, undefined>;
388 /**
389 * Normalize any dirty objects in the editor.
390 */
391 normalize: (editor: Editor, options?: EditorNormalizeOptions) => void;
392 /**
393 * Get the parent node of a location.
394 */
395 parent: (editor: Editor, at: Location, options?: EditorParentOptions) => NodeEntry<Ancestor>;
396 /**
397 * Get the path of a location.
398 */
399 path: (editor: Editor, at: Location, options?: EditorPathOptions) => Path;
400 /**
401 * Create a mutable ref for a `Path` object, which will stay in sync as new
402 * operations are applied to the editor.
403 */
404 pathRef: (editor: Editor, path: Path, options?: EditorPathRefOptions) => PathRef;
405 /**
406 * Get the set of currently tracked path refs of the editor.
407 */
408 pathRefs: (editor: Editor) => Set<PathRef>;
409 /**
410 * Get the start or end point of a location.
411 */
412 point: (editor: Editor, at: Location, options?: EditorPointOptions) => Point;
413 /**
414 * Create a mutable ref for a `Point` object, which will stay in sync as new
415 * operations are applied to the editor.
416 */
417 pointRef: (editor: Editor, point: Point, options?: EditorPointRefOptions) => PointRef;
418 /**
419 * Get the set of currently tracked point refs of the editor.
420 */
421 pointRefs: (editor: Editor) => Set<PointRef>;
422 /**
423 * Return all the positions in `at` range where a `Point` can be placed.
424 *
425 * By default, moves forward by individual offsets at a time, but
426 * the `unit` option can be used to to move by character, word, line, or block.
427 *
428 * The `reverse` option can be used to change iteration direction.
429 *
430 * Note: By default void nodes are treated as a single point and iteration
431 * will not happen inside their content unless you pass in true for the
432 * `voids` option, then iteration will occur.
433 */
434 positions: (editor: Editor, options?: EditorPositionsOptions) => Generator<Point, void, undefined>;
435 /**
436 * Get the matching node in the branch of the document before a location.
437 */
438 previous: <T extends Node>(editor: Editor, options?: EditorPreviousOptions<T>) => NodeEntry<T> | undefined;
439 /**
440 * Get a range of a location.
441 */
442 range: (editor: Editor, at: Location, to?: Location) => Range;
443 /**
444 * Create a mutable ref for a `Range` object, which will stay in sync as new
445 * operations are applied to the editor.
446 */
447 rangeRef: (editor: Editor, range: Range, options?: EditorRangeRefOptions) => RangeRef;
448 /**
449 * Get the set of currently tracked range refs of the editor.
450 */
451 rangeRefs: (editor: Editor) => Set<RangeRef>;
452 /**
453 * Remove a custom property from all of the leaf text nodes in the current
454 * selection.
455 *
456 * If the selection is currently collapsed, the removal will be stored on
457 * `editor.marks` and applied to the text inserted next.
458 */
459 removeMark: (editor: Editor, key: string) => void;
460 /**
461 * Manually set if the editor should currently be normalizing.
462 *
463 * Note: Using this incorrectly can leave the editor in an invalid state.
464 *
465 */
466 setNormalizing: (editor: Editor, isNormalizing: boolean) => void;
467 /**
468 * Get the start point of a location.
469 */
470 start: (editor: Editor, at: Location) => Point;
471 /**
472 * Get the text string content of a location.
473 *
474 * Note: by default the text of void nodes is considered to be an empty
475 * string, regardless of content, unless you pass in true for the voids option
476 */
477 string: (editor: Editor, at: Location, options?: EditorStringOptions) => string;
478 /**
479 * Convert a range into a non-hanging one.
480 */
481 unhangRange: (editor: Editor, range: Range, options?: EditorUnhangRangeOptions) => Range;
482 /**
483 * Match a void node in the current branch of the editor.
484 */
485 void: (editor: Editor, options?: EditorVoidOptions) => NodeEntry<Element> | undefined;
486 /**
487 * Call a function, deferring normalization until after it completes.
488 */
489 withoutNormalizing: (editor: Editor, fn: () => void) => void;
490}
491export declare const Editor: EditorInterface;
492/**
493 * A helper type for narrowing matched nodes with a predicate.
494 */
495export type NodeMatch<T extends Node> = ((node: Node, path: Path) => node is T) | ((node: Node, path: Path) => boolean);
496export type PropsCompare = (prop: Partial<Node>, node: Partial<Node>) => boolean;
497export type PropsMerge = (prop: Partial<Node>, node: Partial<Node>) => object;
498//# sourceMappingURL=editor.d.ts.map
\No newline at end of file