forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeUtil.ts
More file actions
42 lines (34 loc) · 957 Bytes
/
nodeUtil.ts
File metadata and controls
42 lines (34 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { Node } from "prosemirror-model";
/**
* Get a TipTap node by id
*/
export function getNodeById(
id: string,
doc: Node,
): { node: Node; posBeforeNode: number } | undefined {
let targetNode: Node | undefined = undefined;
let posBeforeNode: number | undefined = undefined;
doc.firstChild!.descendants((node, pos) => {
// Skips traversing nodes after node with target ID has been found.
if (targetNode) {
return false;
}
// Keeps traversing nodes if block with target ID has not been found.
if (!isNodeBlock(node) || node.attrs.id !== id) {
return true;
}
targetNode = node;
posBeforeNode = pos + 1;
return false;
});
if (targetNode === undefined || posBeforeNode === undefined) {
return undefined;
}
return {
node: targetNode,
posBeforeNode: posBeforeNode,
};
}
export function isNodeBlock(node: Node): boolean {
return node.type.isInGroup("bnBlock");
}