@@ -5,6 +5,12 @@ import type {
5
5
SerializedParagraphNode ,
6
6
SerializedTextNode ,
7
7
SerializedLineBreakNode ,
8
+ SerializedHeadingNode ,
9
+ SerializedListItemNode ,
10
+ SerializedListNode ,
11
+ SerializedTableRowNode ,
12
+ SerializedTableNode ,
13
+ SerializedTableCellNode ,
8
14
} from '../../../nodeTypes.js'
9
15
import { convertLexicalToPlaintext } from './sync/index.js'
10
16
@@ -51,7 +57,83 @@ function paragraphNode(children: DefaultNodeTypes[]): SerializedParagraphNode {
51
57
}
52
58
}
53
59
54
- function rootNode ( nodes : DefaultNodeTypes [ ] ) : DefaultTypedEditorState {
60
+ function headingNode ( children : DefaultNodeTypes [ ] ) : SerializedHeadingNode {
61
+ return {
62
+ type : 'heading' ,
63
+ children,
64
+ direction : 'ltr' ,
65
+ format : '' ,
66
+ indent : 0 ,
67
+ textFormat : 0 ,
68
+ tag : 'h1' ,
69
+ version : 1 ,
70
+ }
71
+ }
72
+
73
+ function listItemNode ( children : DefaultNodeTypes [ ] ) : SerializedListItemNode {
74
+ return {
75
+ type : 'listitem' ,
76
+ children,
77
+ checked : false ,
78
+ direction : 'ltr' ,
79
+ format : '' ,
80
+ indent : 0 ,
81
+ value : 0 ,
82
+ version : 1 ,
83
+ }
84
+ }
85
+
86
+ function listNode ( children : DefaultNodeTypes [ ] ) : SerializedListNode {
87
+ return {
88
+ type : 'list' ,
89
+ children,
90
+ direction : 'ltr' ,
91
+ format : '' ,
92
+ indent : 0 ,
93
+ listType : 'bullet' ,
94
+ start : 0 ,
95
+ tag : 'ul' ,
96
+ version : 1 ,
97
+ }
98
+ }
99
+
100
+ function tableNode ( children : ( DefaultNodeTypes | SerializedTableRowNode ) [ ] ) : SerializedTableNode {
101
+ return {
102
+ type : 'table' ,
103
+ children,
104
+ direction : 'ltr' ,
105
+ format : '' ,
106
+ indent : 0 ,
107
+ version : 1 ,
108
+ }
109
+ }
110
+
111
+ function tableRowNode (
112
+ children : ( DefaultNodeTypes | SerializedTableCellNode ) [ ] ,
113
+ ) : SerializedTableRowNode {
114
+ return {
115
+ type : 'tablerow' ,
116
+ children,
117
+ direction : 'ltr' ,
118
+ format : '' ,
119
+ indent : 0 ,
120
+ version : 1 ,
121
+ }
122
+ }
123
+
124
+ function tableCellNode ( children : DefaultNodeTypes [ ] ) : SerializedTableCellNode {
125
+ return {
126
+ type : 'tablecell' ,
127
+ children,
128
+ direction : 'ltr' ,
129
+ format : '' ,
130
+ indent : 0 ,
131
+ headerState : 0 ,
132
+ version : 1 ,
133
+ }
134
+ }
135
+
136
+ function rootNode ( nodes : ( DefaultNodeTypes | SerializedTableNode ) [ ] ) : DefaultTypedEditorState {
55
137
return {
56
138
root : {
57
139
type : 'root' ,
@@ -72,7 +154,6 @@ describe('convertLexicalToPlaintext', () => {
72
154
data,
73
155
} )
74
156
75
- console . log ( 'plaintext' , plaintext )
76
157
expect ( plaintext ) . toBe ( 'Basic Text' )
77
158
} )
78
159
@@ -111,4 +192,67 @@ describe('convertLexicalToPlaintext', () => {
111
192
112
193
expect ( plaintext ) . toBe ( 'Basic Text\tNext Line' )
113
194
} )
195
+
196
+ it ( 'ensure new lines are added between paragraphs' , ( ) => {
197
+ const data : DefaultTypedEditorState = rootNode ( [
198
+ paragraphNode ( [ textNode ( 'Basic text' ) ] ) ,
199
+ paragraphNode ( [ textNode ( 'Next block-node' ) ] ) ,
200
+ ] )
201
+
202
+ const plaintext = convertLexicalToPlaintext ( {
203
+ data,
204
+ } )
205
+
206
+ expect ( plaintext ) . toBe ( 'Basic text\n\nNext block-node' )
207
+ } )
208
+
209
+ it ( 'ensure new lines are added between heading nodes' , ( ) => {
210
+ const data : DefaultTypedEditorState = rootNode ( [
211
+ headingNode ( [ textNode ( 'Basic text' ) ] ) ,
212
+ headingNode ( [ textNode ( 'Next block-node' ) ] ) ,
213
+ ] )
214
+
215
+ const plaintext = convertLexicalToPlaintext ( {
216
+ data,
217
+ } )
218
+
219
+ expect ( plaintext ) . toBe ( 'Basic text\n\nNext block-node' )
220
+ } )
221
+
222
+ it ( 'ensure new lines are added between list items and lists' , ( ) => {
223
+ const data : DefaultTypedEditorState = rootNode ( [
224
+ listNode ( [ listItemNode ( [ textNode ( 'First item' ) ] ) , listItemNode ( [ textNode ( 'Second item' ) ] ) ] ) ,
225
+ listNode ( [ listItemNode ( [ textNode ( 'Next list' ) ] ) ] ) ,
226
+ ] )
227
+
228
+ const plaintext = convertLexicalToPlaintext ( {
229
+ data,
230
+ } )
231
+
232
+ expect ( plaintext ) . toBe ( 'First item\nSecond item\n\nNext list' )
233
+ } )
234
+
235
+ it ( 'ensure new lines are added between tables, table rows, and table cells' , ( ) => {
236
+ const data : DefaultTypedEditorState = rootNode ( [
237
+ tableNode ( [
238
+ tableRowNode ( [
239
+ tableCellNode ( [ textNode ( 'Cell 1, Row 1' ) ] ) ,
240
+ tableCellNode ( [ textNode ( 'Cell 2, Row 1' ) ] ) ,
241
+ ] ) ,
242
+ tableRowNode ( [
243
+ tableCellNode ( [ textNode ( 'Cell 1, Row 2' ) ] ) ,
244
+ tableCellNode ( [ textNode ( 'Cell 2, Row 2' ) ] ) ,
245
+ ] ) ,
246
+ ] ) ,
247
+ tableNode ( [ tableRowNode ( [ tableCellNode ( [ textNode ( 'Cell in Table 2' ) ] ) ] ) ] ) ,
248
+ ] )
249
+
250
+ const plaintext = convertLexicalToPlaintext ( {
251
+ data,
252
+ } )
253
+
254
+ expect ( plaintext ) . toBe (
255
+ 'Cell 1, Row 1 | Cell 2, Row 1\nCell 1, Row 2 | Cell 2, Row 2\n\nCell in Table 2' ,
256
+ )
257
+ } )
114
258
} )
0 commit comments