1
1
import { Editor as TiptapEditor } from "@tiptap/core" ;
2
2
import { Node } from "prosemirror-model" ;
3
3
import { getBlockInfoFromPos } from "../extensions/Blocks/helpers/getBlockInfoFromPos" ;
4
- import { Block , PartialBlock } from "../extensions/Blocks/api/blockTypes" ;
4
+ import {
5
+ Block ,
6
+ BlockIdentifier ,
7
+ PartialBlock ,
8
+ } from "../extensions/Blocks/api/blockTypes" ;
5
9
import { TextCursorPosition } from "../extensions/Blocks/api/cursorPositionTypes" ;
6
10
import { nodeToBlock } from "./nodeConversions/nodeConversions" ;
7
11
import {
@@ -24,7 +28,8 @@ export class Editor {
24
28
) { }
25
29
26
30
/**
27
- * Gets a list of all top-level blocks that are in the editor.
31
+ * Gets a snapshot of all top-level blocks that are in the editor.
32
+ * @returns An array containing a snapshot of all top-level (non-nested) blocks in the editor.
28
33
*/
29
34
public get topLevelBlocks ( ) : Block [ ] {
30
35
const blocks : Block [ ] = [ ] ;
@@ -38,26 +43,73 @@ export class Editor {
38
43
return blocks ;
39
44
}
40
45
46
+ /**
47
+ * Retrieves a snapshot of an existing block from the editor.
48
+ * @param block The identifier of an existing block that should be retrieved.
49
+ * @returns The block that matches the identifier, or undefined if no matching block was found.
50
+ */
51
+ public getBlock ( block : BlockIdentifier ) : Block | undefined {
52
+ const id = typeof block === "string" ? block : block . id ;
53
+ let newBlock : Block | undefined = undefined ;
54
+
55
+ this . tiptapEditor . state . doc . firstChild ! . descendants ( ( node ) => {
56
+ if ( typeof newBlock !== "undefined" ) {
57
+ return false ;
58
+ }
59
+
60
+ if ( node . type . name !== "blockContainer" || node . attrs . id !== id ) {
61
+ return true ;
62
+ }
63
+
64
+ newBlock = nodeToBlock ( node , this . blockCache ) ;
65
+
66
+ return false ;
67
+ } ) ;
68
+
69
+ return newBlock ;
70
+ }
71
+
41
72
/**
42
73
* Traverses all blocks in the editor, including all nested blocks, and executes a callback for each. The traversal is
43
- * depth-first, which is the same order as blocks appear in the editor by y-coordinate.
74
+ * depth-first, which is the same order as blocks appear in the editor by y-coordinate. Stops traversal if the callback
75
+ * returns false;
44
76
* @param callback The callback to execute for each block.
45
77
* @param reverse Whether the blocks should be traversed in reverse order.
46
78
*/
47
- public allBlocks (
48
- callback : ( block : Block ) => void ,
79
+ public forEachBlock (
80
+ callback : ( block : Block ) => boolean ,
49
81
reverse : boolean = false
50
82
) : void {
83
+ let stop = false ;
84
+
51
85
function helper ( blocks : Block [ ] ) {
52
86
if ( reverse ) {
53
87
for ( const block of blocks . reverse ( ) ) {
54
88
helper ( block . children ) ;
55
- callback ( block ) ;
89
+
90
+ if ( stop ) {
91
+ return ;
92
+ }
93
+
94
+ stop = ! callback ( block ) ;
95
+
96
+ if ( stop ) {
97
+ return ;
98
+ }
56
99
}
57
100
} else {
58
101
for ( const block of blocks ) {
59
- callback ( block ) ;
102
+ stop = ! callback ( block ) ;
103
+
104
+ if ( stop ) {
105
+ return ;
106
+ }
107
+
60
108
helper ( block . children ) ;
109
+
110
+ if ( stop ) {
111
+ return ;
112
+ }
61
113
}
62
114
}
63
115
}
@@ -66,7 +118,8 @@ export class Editor {
66
118
}
67
119
68
120
/**
69
- * Gets information regarding the position of the text cursor in the editor.
121
+ * Gets a snapshot of the text cursor position within the editor.
122
+ * @returns A snapshot of the text cursor position.
70
123
*/
71
124
public get textCursorPosition ( ) : TextCursorPosition {
72
125
const { node } = getBlockInfoFromPos (
@@ -124,7 +177,7 @@ export class Editor {
124
177
}
125
178
126
179
/**
127
- * Executes a callback function whenever the editor's content changes .
180
+ * Executes a callback function whenever the editor's contents change .
128
181
* @param callback The callback function to execute.
129
182
*/
130
183
public onContentChange ( callback : ( ) => void ) {
@@ -136,6 +189,7 @@ export class Editor {
136
189
* is simplified in order to better conform to HTML standards. Block structuring elements are removed, children of
137
190
* blocks which aren't list items are lifted out of them, and list items blocks are wrapped in `ul`/`ol` tags.
138
191
* @param blocks The list of blocks to serialize into HTML.
192
+ * @returns An HTML representation of the blocks.
139
193
*/
140
194
public async blocksToHTML ( blocks : Block [ ] ) : Promise < string > {
141
195
return blocksToHTML ( blocks , this . tiptapEditor . schema ) ;
@@ -144,6 +198,7 @@ export class Editor {
144
198
/**
145
199
* Creates a list of blocks from an HTML string.
146
200
* @param htmlString The HTML string to create a list of blocks from.
201
+ * @returns A list of blocks parsed from the HTML string.
147
202
*/
148
203
public async HTMLToBlocks ( htmlString : string ) : Promise < Block [ ] > {
149
204
return HTMLToBlocks ( htmlString , this . tiptapEditor . schema ) ;
@@ -152,8 +207,9 @@ export class Editor {
152
207
/**
153
208
* Serializes a list of blocks into a Markdown string. The output is simplified as Markdown does not support all
154
209
* features of BlockNote. Block structuring elements are removed, children of blocks which aren't list items are
155
- * lifted out of them , and certain styles are removed.
210
+ * un-nested , and certain styles are removed.
156
211
* @param blocks The list of blocks to serialize into Markdown.
212
+ * @returns A Markdown representation of the blocks.
157
213
*/
158
214
public async blocksToMarkdown ( blocks : Block [ ] ) : Promise < string > {
159
215
return blocksToMarkdown ( blocks , this . tiptapEditor . schema ) ;
@@ -162,6 +218,7 @@ export class Editor {
162
218
/**
163
219
* Creates a list of blocks from a Markdown string.
164
220
* @param markdownString The Markdown string to create a list of blocks from.
221
+ * @returns A list of blocks parsed from the Markdown string.
165
222
*/
166
223
public async markdownToBlocks ( markdownString : string ) : Promise < Block [ ] > {
167
224
return markdownToBlocks ( markdownString , this . tiptapEditor . schema ) ;
0 commit comments