@contentful/field-editor-rich-text 4.18.0 → 4.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,11 +24,11 @@ const _platecommon = require("@udecode/plate-common");
24
24
  const _emotion = require("emotion");
25
25
  const _fastdeepequal = /*#__PURE__*/ _interop_require_default(require("fast-deep-equal"));
26
26
  const _noop = /*#__PURE__*/ _interop_require_default(require("lodash/noop"));
27
+ const _usedeepcompare = require("use-deep-compare");
27
28
  const _CharConstraints = require("./CharConstraints");
28
29
  const _ContentfulEditorProvider = require("./ContentfulEditorProvider");
29
30
  const _editoroverrides = require("./editor-overrides");
30
- const _toSlateValue = require("./helpers/toSlateValue");
31
- const _misc = require("./internal/misc");
31
+ const _toSlateDoc = require("./helpers/toSlateDoc");
32
32
  const _plugins = require("./plugins");
33
33
  const _RichTextEditorstyles = require("./RichTextEditor.styles");
34
34
  const _SdkProvider = require("./SdkProvider");
@@ -90,14 +90,10 @@ const ConnectedRichTextEditor = (props)=>{
90
90
  restrictedMarks,
91
91
  withCharValidation
92
92
  ]);
93
- const initialValue = _react.useMemo(()=>{
94
- return (0, _misc.normalizeInitialValue)({
95
- plugins,
96
- disableCorePlugins: _plugins.disableCorePlugins
97
- }, (0, _toSlateValue.toSlateValue)(props.value));
93
+ const initialValue = (0, _usedeepcompare.useDeepCompareMemo)(()=>{
94
+ return (0, _toSlateDoc.toSlateDoc)(props.value);
98
95
  }, [
99
- props.value,
100
- plugins
96
+ props.value
101
97
  ]);
102
98
  const direction = sdk.locales.direction[sdk.field.locale] ?? 'ltr';
103
99
  const classNames = (0, _emotion.cx)(_RichTextEditorstyles.styles.editor, props.minHeight !== undefined ? (0, _emotion.css)({
@@ -0,0 +1,611 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _richtexttypes = require("@contentful/rich-text-types");
6
+ const _nodeFactory = require("../nodeFactory");
7
+ const _toSlateDoc = require("../toSlateDoc");
8
+ describe('toSlateDoc', ()=>{
9
+ const cases = [
10
+ {
11
+ title: 'undefined documents',
12
+ input: undefined,
13
+ expected: [
14
+ {
15
+ type: 'paragraph',
16
+ children: [
17
+ {
18
+ text: ''
19
+ }
20
+ ],
21
+ data: {},
22
+ isVoid: false
23
+ }
24
+ ]
25
+ },
26
+ {
27
+ title: 'empty documents',
28
+ input: (0, _nodeFactory.document)(),
29
+ expected: [
30
+ {
31
+ type: 'paragraph',
32
+ children: [
33
+ {
34
+ text: ''
35
+ }
36
+ ],
37
+ data: {},
38
+ isVoid: false
39
+ }
40
+ ]
41
+ },
42
+ {
43
+ title: 'empty hyperlinks',
44
+ input: (0, _nodeFactory.document)((0, _nodeFactory.block)(_richtexttypes.BLOCKS.PARAGRAPH, {}, (0, _nodeFactory.inline)(_richtexttypes.INLINES.HYPERLINK), (0, _nodeFactory.inline)(_richtexttypes.INLINES.HYPERLINK, {}, (0, _nodeFactory.text)('')))),
45
+ expected: [
46
+ {
47
+ type: 'paragraph',
48
+ children: [
49
+ {
50
+ text: ''
51
+ }
52
+ ],
53
+ data: {},
54
+ isVoid: false
55
+ }
56
+ ]
57
+ },
58
+ {
59
+ title: 'adjacent text nodes with identical marks',
60
+ input: (0, _nodeFactory.document)((0, _nodeFactory.block)(_richtexttypes.BLOCKS.PARAGRAPH, {}, (0, _nodeFactory.text)('This '), (0, _nodeFactory.text)(''), (0, _nodeFactory.text)('should be one '), (0, _nodeFactory.text)('text node.'), (0, _nodeFactory.text)(' but this is unique.', [
61
+ {
62
+ type: 'bold'
63
+ }
64
+ ]), (0, _nodeFactory.text)(' and this should be merged ', [
65
+ {
66
+ type: 'italic'
67
+ }
68
+ ]), (0, _nodeFactory.text)('with this.', [
69
+ {
70
+ type: 'italic'
71
+ }
72
+ ]))),
73
+ expected: [
74
+ {
75
+ type: 'paragraph',
76
+ children: [
77
+ {
78
+ text: 'This should be one text node.',
79
+ data: {}
80
+ },
81
+ {
82
+ text: ' but this is unique.',
83
+ bold: true,
84
+ data: {}
85
+ },
86
+ {
87
+ text: ' and this should be merged with this.',
88
+ italic: true,
89
+ data: {}
90
+ }
91
+ ],
92
+ data: {},
93
+ isVoid: false
94
+ }
95
+ ]
96
+ },
97
+ {
98
+ title: 'inlines without surrounding text',
99
+ input: (0, _nodeFactory.document)((0, _nodeFactory.block)(_richtexttypes.BLOCKS.PARAGRAPH, {}, (0, _nodeFactory.inline)(_richtexttypes.INLINES.EMBEDDED_ENTRY), (0, _nodeFactory.inline)(_richtexttypes.INLINES.EMBEDDED_RESOURCE))),
100
+ expected: [
101
+ {
102
+ type: 'paragraph',
103
+ children: [
104
+ {
105
+ text: ''
106
+ },
107
+ {
108
+ type: _richtexttypes.INLINES.EMBEDDED_ENTRY,
109
+ data: {},
110
+ children: [
111
+ {
112
+ text: ''
113
+ }
114
+ ],
115
+ isVoid: true
116
+ },
117
+ {
118
+ text: ''
119
+ },
120
+ {
121
+ type: _richtexttypes.INLINES.EMBEDDED_RESOURCE,
122
+ data: {},
123
+ children: [
124
+ {
125
+ text: ''
126
+ }
127
+ ],
128
+ isVoid: true
129
+ },
130
+ {
131
+ text: ''
132
+ }
133
+ ],
134
+ data: {},
135
+ isVoid: false
136
+ }
137
+ ]
138
+ },
139
+ {
140
+ title: 'uneven tables',
141
+ input: (0, _nodeFactory.document)((0, _nodeFactory.block)('table', {}, (0, _nodeFactory.block)('table-row', {}, (0, _nodeFactory.block)('table-header-cell', {}, (0, _nodeFactory.block)('paragraph', {}, (0, _nodeFactory.text)('cell #1')))), (0, _nodeFactory.block)('table-row', {}, (0, _nodeFactory.block)('table-cell', {}, (0, _nodeFactory.block)('paragraph', {}, (0, _nodeFactory.text)('cell #1'))), (0, _nodeFactory.block)('table-cell', {}, (0, _nodeFactory.block)('paragraph', {}, (0, _nodeFactory.text)('cell #2'))), (0, _nodeFactory.block)('table-cell', {}, (0, _nodeFactory.block)('paragraph', {}, (0, _nodeFactory.text)('cell #3')))), (0, _nodeFactory.block)('table-row', {}, (0, _nodeFactory.block)('table-cell', {}, (0, _nodeFactory.block)('paragraph', {}, (0, _nodeFactory.text)('cell #1'))), (0, _nodeFactory.block)('table-cell', {}, (0, _nodeFactory.block)('paragraph', {}, (0, _nodeFactory.text)('cell #2')))))),
142
+ expected: [
143
+ {
144
+ type: 'table',
145
+ data: {},
146
+ children: [
147
+ {
148
+ type: 'table-row',
149
+ data: {},
150
+ children: [
151
+ {
152
+ type: 'table-header-cell',
153
+ data: {},
154
+ children: [
155
+ {
156
+ type: 'paragraph',
157
+ data: {},
158
+ children: [
159
+ {
160
+ text: 'cell #1',
161
+ data: {}
162
+ }
163
+ ],
164
+ isVoid: false
165
+ }
166
+ ],
167
+ isVoid: false
168
+ },
169
+ {
170
+ type: 'table-header-cell',
171
+ data: {},
172
+ children: [
173
+ {
174
+ type: 'paragraph',
175
+ data: {},
176
+ children: [
177
+ {
178
+ text: ''
179
+ }
180
+ ],
181
+ isVoid: false
182
+ }
183
+ ],
184
+ isVoid: false
185
+ },
186
+ {
187
+ type: 'table-header-cell',
188
+ data: {},
189
+ children: [
190
+ {
191
+ type: 'paragraph',
192
+ data: {},
193
+ children: [
194
+ {
195
+ text: ''
196
+ }
197
+ ],
198
+ isVoid: false
199
+ }
200
+ ],
201
+ isVoid: false
202
+ }
203
+ ],
204
+ isVoid: false
205
+ },
206
+ {
207
+ type: 'table-row',
208
+ data: {},
209
+ children: [
210
+ {
211
+ type: 'table-cell',
212
+ data: {},
213
+ children: [
214
+ {
215
+ type: 'paragraph',
216
+ data: {},
217
+ children: [
218
+ {
219
+ text: 'cell #1',
220
+ data: {}
221
+ }
222
+ ],
223
+ isVoid: false
224
+ }
225
+ ],
226
+ isVoid: false
227
+ },
228
+ {
229
+ type: 'table-cell',
230
+ data: {},
231
+ children: [
232
+ {
233
+ type: 'paragraph',
234
+ data: {},
235
+ children: [
236
+ {
237
+ text: 'cell #2',
238
+ data: {}
239
+ }
240
+ ],
241
+ isVoid: false
242
+ }
243
+ ],
244
+ isVoid: false
245
+ },
246
+ {
247
+ type: 'table-cell',
248
+ data: {},
249
+ children: [
250
+ {
251
+ type: 'paragraph',
252
+ data: {},
253
+ children: [
254
+ {
255
+ text: 'cell #3',
256
+ data: {}
257
+ }
258
+ ],
259
+ isVoid: false
260
+ }
261
+ ],
262
+ isVoid: false
263
+ }
264
+ ],
265
+ isVoid: false
266
+ },
267
+ {
268
+ type: 'table-row',
269
+ data: {},
270
+ children: [
271
+ {
272
+ type: 'table-cell',
273
+ data: {},
274
+ children: [
275
+ {
276
+ type: 'paragraph',
277
+ data: {},
278
+ children: [
279
+ {
280
+ text: 'cell #1',
281
+ data: {}
282
+ }
283
+ ],
284
+ isVoid: false
285
+ }
286
+ ],
287
+ isVoid: false
288
+ },
289
+ {
290
+ type: 'table-cell',
291
+ data: {},
292
+ children: [
293
+ {
294
+ type: 'paragraph',
295
+ data: {},
296
+ children: [
297
+ {
298
+ text: 'cell #2',
299
+ data: {}
300
+ }
301
+ ],
302
+ isVoid: false
303
+ }
304
+ ],
305
+ isVoid: false
306
+ },
307
+ {
308
+ type: 'table-cell',
309
+ data: {},
310
+ children: [
311
+ {
312
+ type: 'paragraph',
313
+ data: {},
314
+ children: [
315
+ {
316
+ text: ''
317
+ }
318
+ ],
319
+ isVoid: false
320
+ }
321
+ ],
322
+ isVoid: false
323
+ }
324
+ ],
325
+ isVoid: false
326
+ }
327
+ ],
328
+ isVoid: false
329
+ },
330
+ {
331
+ type: 'paragraph',
332
+ data: {},
333
+ children: [
334
+ {
335
+ text: ''
336
+ }
337
+ ],
338
+ isVoid: false
339
+ }
340
+ ]
341
+ },
342
+ {
343
+ title: 'empty blockquotes',
344
+ input: (0, _nodeFactory.document)((0, _nodeFactory.block)(_richtexttypes.BLOCKS.QUOTE, {})),
345
+ expected: [
346
+ {
347
+ type: 'blockquote',
348
+ data: {},
349
+ children: [
350
+ {
351
+ type: 'paragraph',
352
+ data: {},
353
+ children: [
354
+ {
355
+ text: ''
356
+ }
357
+ ],
358
+ isVoid: false
359
+ }
360
+ ],
361
+ isVoid: false
362
+ },
363
+ {
364
+ type: 'paragraph',
365
+ data: {},
366
+ children: [
367
+ {
368
+ text: ''
369
+ }
370
+ ],
371
+ isVoid: false
372
+ }
373
+ ]
374
+ },
375
+ {
376
+ title: 'empty list items',
377
+ input: (0, _nodeFactory.document)((0, _nodeFactory.block)(_richtexttypes.BLOCKS.LIST_ITEM, {})),
378
+ expected: [
379
+ {
380
+ type: 'list-item',
381
+ data: {},
382
+ children: [
383
+ {
384
+ type: 'paragraph',
385
+ data: {},
386
+ children: [
387
+ {
388
+ text: ''
389
+ }
390
+ ],
391
+ isVoid: false
392
+ }
393
+ ],
394
+ isVoid: false
395
+ },
396
+ {
397
+ type: 'paragraph',
398
+ data: {},
399
+ children: [
400
+ {
401
+ text: ''
402
+ }
403
+ ],
404
+ isVoid: false
405
+ }
406
+ ]
407
+ },
408
+ {
409
+ title: 'empty lists',
410
+ input: (0, _nodeFactory.document)((0, _nodeFactory.block)(_richtexttypes.BLOCKS.UL_LIST, {}), (0, _nodeFactory.block)(_richtexttypes.BLOCKS.OL_LIST, {})),
411
+ expected: [
412
+ {
413
+ type: 'unordered-list',
414
+ data: {},
415
+ children: [
416
+ {
417
+ type: 'list-item',
418
+ data: {},
419
+ children: [
420
+ {
421
+ type: 'paragraph',
422
+ data: {},
423
+ children: [
424
+ {
425
+ text: ''
426
+ }
427
+ ],
428
+ isVoid: false
429
+ }
430
+ ],
431
+ isVoid: false
432
+ }
433
+ ],
434
+ isVoid: false
435
+ },
436
+ {
437
+ type: 'ordered-list',
438
+ data: {},
439
+ children: [
440
+ {
441
+ type: 'list-item',
442
+ data: {},
443
+ children: [
444
+ {
445
+ type: 'paragraph',
446
+ data: {},
447
+ children: [
448
+ {
449
+ text: ''
450
+ }
451
+ ],
452
+ isVoid: false
453
+ }
454
+ ],
455
+ isVoid: false
456
+ }
457
+ ],
458
+ isVoid: false
459
+ },
460
+ {
461
+ type: 'paragraph',
462
+ data: {},
463
+ children: [
464
+ {
465
+ text: ''
466
+ }
467
+ ],
468
+ isVoid: false
469
+ }
470
+ ]
471
+ },
472
+ {
473
+ title: 'list of embedded entry blocks',
474
+ input: (0, _nodeFactory.document)((0, _nodeFactory.block)(_richtexttypes.BLOCKS.EMBEDDED_ENTRY, {
475
+ target: {
476
+ sys: {
477
+ id: '4jo9APjyJs31WugGbV0E42',
478
+ type: 'Link',
479
+ linkType: 'Entry'
480
+ }
481
+ }
482
+ }), (0, _nodeFactory.block)(_richtexttypes.BLOCKS.EMBEDDED_ENTRY, {
483
+ target: {
484
+ sys: {
485
+ id: '2bwp9MdklBd4WVLGm1Fngy',
486
+ type: 'Link',
487
+ linkType: 'Entry'
488
+ }
489
+ }
490
+ }), (0, _nodeFactory.block)(_richtexttypes.BLOCKS.EMBEDDED_ENTRY, {
491
+ target: {
492
+ sys: {
493
+ id: '73qAHseh6G40k7ndmON5OD',
494
+ type: 'Link',
495
+ linkType: 'Entry'
496
+ }
497
+ }
498
+ }), (0, _nodeFactory.block)(_richtexttypes.BLOCKS.EMBEDDED_ENTRY, {
499
+ target: {
500
+ sys: {
501
+ id: '7FYYiCprd5VwItrHH3nNJj',
502
+ type: 'Link',
503
+ linkType: 'Entry'
504
+ }
505
+ }
506
+ }), (0, _nodeFactory.block)(_richtexttypes.BLOCKS.PARAGRAPH, {}, (0, _nodeFactory.text)('')), (0, _nodeFactory.block)(_richtexttypes.BLOCKS.PARAGRAPH, {}, (0, _nodeFactory.text)(''))),
507
+ expected: [
508
+ {
509
+ type: 'embedded-entry-block',
510
+ children: [
511
+ {
512
+ text: ''
513
+ }
514
+ ],
515
+ data: {
516
+ target: {
517
+ sys: {
518
+ id: '4jo9APjyJs31WugGbV0E42',
519
+ type: 'Link',
520
+ linkType: 'Entry'
521
+ }
522
+ }
523
+ },
524
+ isVoid: true
525
+ },
526
+ {
527
+ type: 'embedded-entry-block',
528
+ children: [
529
+ {
530
+ text: ''
531
+ }
532
+ ],
533
+ data: {
534
+ target: {
535
+ sys: {
536
+ id: '2bwp9MdklBd4WVLGm1Fngy',
537
+ type: 'Link',
538
+ linkType: 'Entry'
539
+ }
540
+ }
541
+ },
542
+ isVoid: true
543
+ },
544
+ {
545
+ type: 'embedded-entry-block',
546
+ children: [
547
+ {
548
+ text: ''
549
+ }
550
+ ],
551
+ data: {
552
+ target: {
553
+ sys: {
554
+ id: '73qAHseh6G40k7ndmON5OD',
555
+ type: 'Link',
556
+ linkType: 'Entry'
557
+ }
558
+ }
559
+ },
560
+ isVoid: true
561
+ },
562
+ {
563
+ type: 'embedded-entry-block',
564
+ children: [
565
+ {
566
+ text: ''
567
+ }
568
+ ],
569
+ data: {
570
+ target: {
571
+ sys: {
572
+ id: '7FYYiCprd5VwItrHH3nNJj',
573
+ type: 'Link',
574
+ linkType: 'Entry'
575
+ }
576
+ }
577
+ },
578
+ isVoid: true
579
+ },
580
+ {
581
+ type: 'paragraph',
582
+ children: [
583
+ {
584
+ text: '',
585
+ data: {}
586
+ }
587
+ ],
588
+ data: {},
589
+ isVoid: false
590
+ },
591
+ {
592
+ type: 'paragraph',
593
+ children: [
594
+ {
595
+ text: '',
596
+ data: {}
597
+ }
598
+ ],
599
+ data: {},
600
+ isVoid: false
601
+ }
602
+ ]
603
+ }
604
+ ];
605
+ cases.forEach(({ title, input, expected })=>{
606
+ it(`should normalize ${title}`, ()=>{
607
+ const out = (0, _toSlateDoc.toSlateDoc)(input);
608
+ expect(out).toEqual(expected);
609
+ });
610
+ });
611
+ });