UNPKG

6.37 kBTypeScriptView Raw
1import { Editor, Path, Range, Text } from '..';
2import { Element, ElementEntry } from './element';
3/**
4 * The `Node` union type represents all of the different types of nodes that
5 * occur in a Slate document tree.
6 */
7export type BaseNode = Editor | Element | Text;
8export type Node = Editor | Element | Text;
9export interface NodeAncestorsOptions {
10 reverse?: boolean;
11}
12export interface NodeChildrenOptions {
13 reverse?: boolean;
14}
15export interface NodeDescendantsOptions {
16 from?: Path;
17 to?: Path;
18 reverse?: boolean;
19 pass?: (node: NodeEntry) => boolean;
20}
21export interface NodeElementsOptions {
22 from?: Path;
23 to?: Path;
24 reverse?: boolean;
25 pass?: (node: NodeEntry) => boolean;
26}
27export interface NodeLevelsOptions {
28 reverse?: boolean;
29}
30export interface NodeNodesOptions {
31 from?: Path;
32 to?: Path;
33 reverse?: boolean;
34 pass?: (entry: NodeEntry) => boolean;
35}
36export interface NodeTextsOptions {
37 from?: Path;
38 to?: Path;
39 reverse?: boolean;
40 pass?: (node: NodeEntry) => boolean;
41}
42export interface NodeInterface {
43 /**
44 * Get the node at a specific path, asserting that it's an ancestor node.
45 */
46 ancestor: (root: Node, path: Path) => Ancestor;
47 /**
48 * Return a generator of all the ancestor nodes above a specific path.
49 *
50 * By default the order is top-down, from highest to lowest ancestor in
51 * the tree, but you can pass the `reverse: true` option to go bottom-up.
52 */
53 ancestors: (root: Node, path: Path, options?: NodeAncestorsOptions) => Generator<NodeEntry<Ancestor>, void, undefined>;
54 /**
55 * Get the child of a node at a specific index.
56 */
57 child: (root: Node, index: number) => Descendant;
58 /**
59 * Iterate over the children of a node at a specific path.
60 */
61 children: (root: Node, path: Path, options?: NodeChildrenOptions) => Generator<NodeEntry<Descendant>, void, undefined>;
62 /**
63 * Get an entry for the common ancesetor node of two paths.
64 */
65 common: (root: Node, path: Path, another: Path) => NodeEntry;
66 /**
67 * Get the node at a specific path, asserting that it's a descendant node.
68 */
69 descendant: (root: Node, path: Path) => Descendant;
70 /**
71 * Return a generator of all the descendant node entries inside a root node.
72 */
73 descendants: (root: Node, options?: NodeDescendantsOptions) => Generator<NodeEntry<Descendant>, void, undefined>;
74 /**
75 * Return a generator of all the element nodes inside a root node. Each iteration
76 * will return an `ElementEntry` tuple consisting of `[Element, Path]`. If the
77 * root node is an element it will be included in the iteration as well.
78 */
79 elements: (root: Node, options?: NodeElementsOptions) => Generator<ElementEntry, void, undefined>;
80 /**
81 * Extract props from a Node.
82 */
83 extractProps: (node: Node) => NodeProps;
84 /**
85 * Get the first node entry in a root node from a path.
86 */
87 first: (root: Node, path: Path) => NodeEntry;
88 /**
89 * Get the sliced fragment represented by a range inside a root node.
90 */
91 fragment: (root: Node, range: Range) => Descendant[];
92 /**
93 * Get the descendant node referred to by a specific path. If the path is an
94 * empty array, it refers to the root node itself.
95 */
96 get: (root: Node, path: Path) => Node;
97 /**
98 * Check if a descendant node exists at a specific path.
99 */
100 has: (root: Node, path: Path) => boolean;
101 /**
102 * Check if a value implements the `Node` interface.
103 */
104 isNode: (value: any) => value is Node;
105 /**
106 * Check if a value is a list of `Node` objects.
107 */
108 isNodeList: (value: any) => value is Node[];
109 /**
110 * Get the last node entry in a root node from a path.
111 */
112 last: (root: Node, path: Path) => NodeEntry;
113 /**
114 * Get the node at a specific path, ensuring it's a leaf text node.
115 */
116 leaf: (root: Node, path: Path) => Text;
117 /**
118 * Return a generator of the in a branch of the tree, from a specific path.
119 *
120 * By default the order is top-down, from highest to lowest node in the tree,
121 * but you can pass the `reverse: true` option to go bottom-up.
122 */
123 levels: (root: Node, path: Path, options?: NodeLevelsOptions) => Generator<NodeEntry, void, undefined>;
124 /**
125 * Check if a node matches a set of props.
126 */
127 matches: (node: Node, props: Partial<Node>) => boolean;
128 /**
129 * Return a generator of all the node entries of a root node. Each entry is
130 * returned as a `[Node, Path]` tuple, with the path referring to the node's
131 * position inside the root node.
132 */
133 nodes: (root: Node, options?: NodeNodesOptions) => Generator<NodeEntry, void, undefined>;
134 /**
135 * Get the parent of a node at a specific path.
136 */
137 parent: (root: Node, path: Path) => Ancestor;
138 /**
139 * Get the concatenated text string of a node's content.
140 *
141 * Note that this will not include spaces or line breaks between block nodes.
142 * It is not a user-facing string, but a string for performing offset-related
143 * computations for a node.
144 */
145 string: (node: Node) => string;
146 /**
147 * Return a generator of all leaf text nodes in a root node.
148 */
149 texts: (root: Node, options?: NodeTextsOptions) => Generator<NodeEntry<Text>, void, undefined>;
150}
151export declare const Node: NodeInterface;
152/**
153 * The `Descendant` union type represents nodes that are descendants in the
154 * tree. It is returned as a convenience in certain cases to narrow a value
155 * further than the more generic `Node` union.
156 */
157export type Descendant = Element | Text;
158/**
159 * The `Ancestor` union type represents nodes that are ancestors in the tree.
160 * It is returned as a convenience in certain cases to narrow a value further
161 * than the more generic `Node` union.
162 */
163export type Ancestor = Editor | Element;
164/**
165 * `NodeEntry` objects are returned when iterating over the nodes in a Slate
166 * document tree. They consist of the node and its `Path` relative to the root
167 * node in the document.
168 */
169export type NodeEntry<T extends Node = Node> = [T, Path];
170/**
171 * Convenience type for returning the props of a node.
172 */
173export type NodeProps = Omit<Editor, 'children'> | Omit<Element, 'children'> | Omit<Text, 'text'>;
174//# sourceMappingURL=node.d.ts.map
\No newline at end of file