@contentful/field-editor-rich-text 6.6.15 → 6.6.16

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.
@@ -1,4 +1,4 @@
1
- import { BLOCKS } from '@contentful/rich-text-types';
1
+ import { BLOCKS, INLINES } from '@contentful/rich-text-types';
2
2
  import { describe, expect, it } from 'vitest';
3
3
  import { assertOutput, createTestEditor, jsx } from '../../../test-utils';
4
4
  describe('normalization', ()=>{
@@ -206,3 +206,331 @@ describe('paste', ()=>{
206
206
  ]);
207
207
  });
208
208
  });
209
+ describe('backward deletion', ()=>{
210
+ const paragraph = (value = '')=>({
211
+ type: BLOCKS.PARAGRAPH,
212
+ children: [
213
+ {
214
+ text: value
215
+ }
216
+ ]
217
+ });
218
+ const emptyTable = {
219
+ type: BLOCKS.TABLE,
220
+ children: [
221
+ {
222
+ type: BLOCKS.TABLE_ROW,
223
+ children: [
224
+ {
225
+ type: BLOCKS.TABLE_HEADER_CELL,
226
+ children: [
227
+ paragraph()
228
+ ]
229
+ },
230
+ {
231
+ type: BLOCKS.TABLE_HEADER_CELL,
232
+ children: [
233
+ paragraph()
234
+ ]
235
+ }
236
+ ]
237
+ },
238
+ {
239
+ type: BLOCKS.TABLE_ROW,
240
+ children: [
241
+ {
242
+ type: BLOCKS.TABLE_CELL,
243
+ children: [
244
+ paragraph()
245
+ ]
246
+ },
247
+ {
248
+ type: BLOCKS.TABLE_CELL,
249
+ children: [
250
+ paragraph()
251
+ ]
252
+ }
253
+ ]
254
+ }
255
+ ]
256
+ };
257
+ it('removes an empty table when Backspace is pressed at the start of its first cell', ()=>{
258
+ const { editor } = createTestEditor({});
259
+ editor.children = [
260
+ emptyTable,
261
+ paragraph()
262
+ ];
263
+ editor.tf.select({
264
+ path: [
265
+ 0,
266
+ 0,
267
+ 0,
268
+ 0,
269
+ 0
270
+ ],
271
+ offset: 0
272
+ });
273
+ editor.deleteBackward('character');
274
+ expect(editor.children).toMatchObject([
275
+ paragraph()
276
+ ]);
277
+ expect(editor.selection?.anchor.path).toEqual([
278
+ 0,
279
+ 0
280
+ ]);
281
+ });
282
+ it('removes an empty table when Backspace is pressed at the start of the following paragraph', ()=>{
283
+ const { editor } = createTestEditor({});
284
+ editor.children = [
285
+ emptyTable,
286
+ paragraph()
287
+ ];
288
+ editor.tf.select({
289
+ path: [
290
+ 1,
291
+ 0
292
+ ],
293
+ offset: 0
294
+ });
295
+ editor.deleteBackward('character');
296
+ expect(editor.children).toMatchObject([
297
+ paragraph()
298
+ ]);
299
+ expect(editor.selection?.anchor.path).toEqual([
300
+ 0,
301
+ 0
302
+ ]);
303
+ });
304
+ it('keeps a populated table when Backspace is pressed at the start of its first cell', ()=>{
305
+ const { editor } = createTestEditor({});
306
+ const populatedTable = {
307
+ ...emptyTable,
308
+ children: emptyTable.children.map((row, rowIndex)=>({
309
+ ...row,
310
+ children: row.children.map((cell, cellIndex)=>rowIndex === 0 && cellIndex === 0 ? {
311
+ ...cell,
312
+ children: [
313
+ paragraph('content')
314
+ ]
315
+ } : cell)
316
+ }))
317
+ };
318
+ editor.children = [
319
+ populatedTable,
320
+ paragraph()
321
+ ];
322
+ editor.tf.select({
323
+ path: [
324
+ 0,
325
+ 0,
326
+ 0,
327
+ 0,
328
+ 0
329
+ ],
330
+ offset: 0
331
+ });
332
+ editor.deleteBackward('character');
333
+ expect(editor.children).toMatchObject([
334
+ populatedTable,
335
+ paragraph()
336
+ ]);
337
+ });
338
+ it('does not remove the empty table before a populated table', ()=>{
339
+ const { editor } = createTestEditor({});
340
+ const populatedTable = {
341
+ ...emptyTable,
342
+ children: emptyTable.children.map((row, rowIndex)=>({
343
+ ...row,
344
+ children: row.children.map((cell, cellIndex)=>rowIndex === 0 && cellIndex === 0 ? {
345
+ ...cell,
346
+ children: [
347
+ paragraph('content')
348
+ ]
349
+ } : cell)
350
+ }))
351
+ };
352
+ editor.children = [
353
+ emptyTable,
354
+ populatedTable,
355
+ paragraph()
356
+ ];
357
+ editor.tf.select({
358
+ path: [
359
+ 1,
360
+ 0,
361
+ 0,
362
+ 0,
363
+ 0
364
+ ],
365
+ offset: 0
366
+ });
367
+ editor.deleteBackward('character');
368
+ expect(editor.children).toMatchObject([
369
+ emptyTable,
370
+ populatedTable,
371
+ paragraph()
372
+ ]);
373
+ });
374
+ it.each([
375
+ {
376
+ title: 'whitespace in another cell',
377
+ table: {
378
+ ...emptyTable,
379
+ children: emptyTable.children.map((row, rowIndex)=>({
380
+ ...row,
381
+ children: row.children.map((cell, cellIndex)=>rowIndex === 1 && cellIndex === 1 ? {
382
+ ...cell,
383
+ children: [
384
+ paragraph(' ')
385
+ ]
386
+ } : cell)
387
+ }))
388
+ }
389
+ },
390
+ {
391
+ title: 'an embedded inline in another cell',
392
+ table: {
393
+ ...emptyTable,
394
+ children: emptyTable.children.map((row, rowIndex)=>({
395
+ ...row,
396
+ children: row.children.map((cell, cellIndex)=>rowIndex === 1 && cellIndex === 1 ? {
397
+ ...cell,
398
+ children: [
399
+ {
400
+ type: BLOCKS.PARAGRAPH,
401
+ children: [
402
+ {
403
+ type: INLINES.EMBEDDED_ENTRY,
404
+ data: {
405
+ target: {}
406
+ },
407
+ children: [
408
+ {
409
+ text: ''
410
+ }
411
+ ]
412
+ }
413
+ ]
414
+ }
415
+ ]
416
+ } : cell)
417
+ }))
418
+ }
419
+ }
420
+ ])('keeps a table with $title', ({ table })=>{
421
+ const { editor } = createTestEditor({});
422
+ editor.children = [
423
+ table,
424
+ paragraph()
425
+ ];
426
+ editor.tf.select({
427
+ path: [
428
+ 0,
429
+ 0,
430
+ 0,
431
+ 0,
432
+ 0
433
+ ],
434
+ offset: 0
435
+ });
436
+ editor.deleteBackward('character');
437
+ expect(editor.children).toMatchObject([
438
+ table,
439
+ paragraph()
440
+ ]);
441
+ });
442
+ it('keeps a table when Backspace is pressed inside a non-first cell', ()=>{
443
+ const { editor } = createTestEditor({});
444
+ const populatedTable = {
445
+ ...emptyTable,
446
+ children: emptyTable.children.map((row, rowIndex)=>({
447
+ ...row,
448
+ children: row.children.map((cell, cellIndex)=>rowIndex === 0 && cellIndex === 1 ? {
449
+ ...cell,
450
+ children: [
451
+ paragraph('x')
452
+ ]
453
+ } : cell)
454
+ }))
455
+ };
456
+ editor.children = [
457
+ populatedTable,
458
+ paragraph()
459
+ ];
460
+ editor.tf.select({
461
+ path: [
462
+ 0,
463
+ 0,
464
+ 1,
465
+ 0,
466
+ 0
467
+ ],
468
+ offset: 1
469
+ });
470
+ editor.deleteBackward('character');
471
+ expect(editor.children).toMatchObject([
472
+ emptyTable,
473
+ paragraph()
474
+ ]);
475
+ });
476
+ it('keeps an unselected preceding table when a range in the following paragraph is deleted', ()=>{
477
+ const { editor } = createTestEditor({});
478
+ editor.children = [
479
+ emptyTable,
480
+ paragraph('selected')
481
+ ];
482
+ editor.tf.select({
483
+ anchor: {
484
+ path: [
485
+ 1,
486
+ 0
487
+ ],
488
+ offset: 0
489
+ },
490
+ focus: {
491
+ path: [
492
+ 1,
493
+ 0
494
+ ],
495
+ offset: 8
496
+ }
497
+ });
498
+ editor.deleteBackward('character');
499
+ expect(editor.children[0]).toMatchObject(emptyTable);
500
+ });
501
+ it('keeps an empty table before a heading when Backspace is pressed in the heading', ()=>{
502
+ const { editor } = createTestEditor({});
503
+ editor.children = [
504
+ emptyTable,
505
+ {
506
+ type: BLOCKS.HEADING_1,
507
+ children: [
508
+ {
509
+ text: 'content'
510
+ }
511
+ ]
512
+ },
513
+ paragraph()
514
+ ];
515
+ editor.tf.select({
516
+ path: [
517
+ 1,
518
+ 0
519
+ ],
520
+ offset: 0
521
+ });
522
+ editor.deleteBackward('character');
523
+ expect(editor.children).toMatchObject([
524
+ emptyTable,
525
+ {
526
+ type: BLOCKS.HEADING_1,
527
+ children: [
528
+ {
529
+ text: 'content'
530
+ }
531
+ ]
532
+ },
533
+ paragraph()
534
+ ]);
535
+ });
536
+ });
@@ -13,6 +13,7 @@ import { createEmptyTableCells, getNoOfMissingTableCellsInRow, isNotEmpty } from
13
13
  import { insertTableFragment } from './insertTableFragment';
14
14
  import { onKeyDownTable } from './onKeyDownTable';
15
15
  import { addTableTrackingEvents, withInvalidCellChildrenTracking } from './tableTracking';
16
+ import { withDeleteBackwardTable } from './withDeleteBackwardTable';
16
17
  import { withInsertFragmentTableOverride } from './withInsertFragmentTableOverride';
17
18
  export const createTablePlugin = ()=>createDefaultTablePlugin({
18
19
  type: BLOCKS.TABLE,
@@ -22,6 +23,7 @@ export const createTablePlugin = ()=>createDefaultTablePlugin({
22
23
  withOverrides: (editor, plugin)=>{
23
24
  const { normalizeNode } = editor;
24
25
  editor = withDeleteTable(editor);
26
+ editor = withDeleteBackwardTable(editor);
25
27
  editor = withGetFragmentTable(editor, plugin);
26
28
  editor = withInsertFragmentTable(editor, plugin);
27
29
  editor = withInsertFragmentTableOverride(editor);
@@ -1,7 +1,7 @@
1
1
  import { BLOCKS } from '@contentful/rich-text-types';
2
2
  import { insertEmptyParagraph } from '../../helpers/editor';
3
3
  import { blurEditor } from '../../internal/misc';
4
- import { getAboveNode, getText, isFirstChild, isLastChildPath } from '../../internal/queries';
4
+ import { getAboveNode, isLastChildPath } from '../../internal/queries';
5
5
  import { getTableEntries, getNextTableCell, getPreviousTableCell, onKeyDownTable as defaultKeyDownTable } from '../../internal/table';
6
6
  import { addRowBelow } from './actions';
7
7
  export const onKeyDownTable = (editor, plugin)=>{
@@ -30,20 +30,6 @@ export const onKeyDownTable = (editor, plugin)=>{
30
30
  return;
31
31
  }
32
32
  }
33
- if (event.key === 'Backspace') {
34
- const entry = getTableEntries(editor, {});
35
- if (entry) {
36
- const { table, row, cell } = entry;
37
- const cellText = getText(editor, cell[1]);
38
- const isFirstCell = isFirstChild(row[1]);
39
- const isFirstRow = isFirstChild(table[1]);
40
- if (isFirstCell && isFirstRow && !cellText) {
41
- event.preventDefault();
42
- event.stopPropagation();
43
- return;
44
- }
45
- }
46
- }
47
33
  if (event.key === 'Tab') {
48
34
  const entry = getTableEntries(editor, {});
49
35
  if (!entry) return;
@@ -0,0 +1,49 @@
1
+ import { BLOCKS } from '@contentful/rich-text-types';
2
+ import { isText } from '../../internal/queries';
3
+ const isElementNode = (node)=>typeof node === 'object' && node !== null && 'type' in node && typeof node.type === 'string' && 'children' in node && Array.isArray(node.children);
4
+ const isEmptyParagraph = (node)=>isElementNode(node) && node.type === BLOCKS.PARAGRAPH && node.children.length > 0 && node.children.every((child)=>isText(child) && child.text.length === 0);
5
+ const isEmptyTable = (node)=>isElementNode(node) && node.type === BLOCKS.TABLE && node.children.length > 0 && node.children.every((row)=>isElementNode(row) && row.type === BLOCKS.TABLE_ROW && row.children.length > 0 && row.children.every((cell)=>isElementNode(cell) && (cell.type === BLOCKS.TABLE_CELL || cell.type === BLOCKS.TABLE_HEADER_CELL) && cell.children.length > 0 && cell.children.every(isEmptyParagraph)));
6
+ const getEmptyTableBeforeBackspace = (editor)=>{
7
+ const { selection } = editor;
8
+ if (!selection || !editor.api.isCollapsed()) return;
9
+ const tableEntry = editor.api.above({
10
+ at: selection.anchor,
11
+ match: {
12
+ type: editor.getType(BLOCKS.TABLE)
13
+ }
14
+ });
15
+ if (tableEntry) {
16
+ if (!isEmptyTable(tableEntry[0])) return;
17
+ const firstCellPath = [
18
+ ...tableEntry[1],
19
+ 0,
20
+ 0
21
+ ];
22
+ return editor.api.isStart(selection.anchor, firstCellPath) ? tableEntry[1] : undefined;
23
+ }
24
+ const currentBlockIndex = selection.anchor.path[0];
25
+ const currentBlock = editor.children[currentBlockIndex];
26
+ if (!currentBlock || currentBlock.type !== BLOCKS.PARAGRAPH || currentBlockIndex === 0 || !editor.api.isStart(selection.anchor, [
27
+ currentBlockIndex
28
+ ])) {
29
+ return;
30
+ }
31
+ const previousBlock = editor.children[currentBlockIndex - 1];
32
+ return previousBlock && isEmptyTable(previousBlock) ? [
33
+ currentBlockIndex - 1
34
+ ] : undefined;
35
+ };
36
+ export const withDeleteBackwardTable = (editor)=>{
37
+ const { deleteBackward } = editor;
38
+ editor.deleteBackward = (unit)=>{
39
+ const emptyTablePath = getEmptyTableBeforeBackspace(editor);
40
+ if (emptyTablePath) {
41
+ editor.tf.removeNodes({
42
+ at: emptyTablePath
43
+ });
44
+ return;
45
+ }
46
+ deleteBackward(unit);
47
+ };
48
+ return editor;
49
+ };
@@ -1,6 +1,64 @@
1
- import { describe, test } from 'vitest';
1
+ import { describe, expect, test } from 'vitest';
2
2
  import { jsx, assertOutput, createTestEditor } from '../../../test-utils';
3
3
  describe('delete backward', ()=>{
4
+ test('removes an empty heading after a table while keeping the table', ()=>{
5
+ const { editor } = createTestEditor({});
6
+ editor.children = [
7
+ {
8
+ type: 'table',
9
+ children: [
10
+ {
11
+ type: 'table-row',
12
+ children: [
13
+ {
14
+ type: 'table-cell',
15
+ children: [
16
+ {
17
+ type: 'paragraph',
18
+ children: [
19
+ {
20
+ text: 'cell'
21
+ }
22
+ ]
23
+ }
24
+ ]
25
+ }
26
+ ]
27
+ }
28
+ ]
29
+ },
30
+ {
31
+ type: 'heading-1',
32
+ children: [
33
+ {
34
+ text: ''
35
+ }
36
+ ]
37
+ },
38
+ {
39
+ type: 'paragraph',
40
+ children: [
41
+ {
42
+ text: ''
43
+ }
44
+ ]
45
+ }
46
+ ];
47
+ editor.tf.select({
48
+ path: [
49
+ 1,
50
+ 0
51
+ ],
52
+ offset: 0
53
+ });
54
+ editor.deleteBackward('character');
55
+ expect(editor.children[0].type).toBe('table');
56
+ expect(editor.children.some((node)=>node.type === 'heading-1')).toBe(false);
57
+ expect(editor.selection?.anchor.path).toEqual([
58
+ 1,
59
+ 0
60
+ ]);
61
+ });
4
62
  const tests = [
5
63
  {
6
64
  title: 'deletes a character of the text inside li',
@@ -48,6 +106,11 @@ describe('delete forward', ()=>{
48
106
  title: 'deletes the first paragraph when followed by a blockquote',
49
107
  input: jsx("fragment", null, jsx("hp", null, jsx("cursor", null)), jsx("hquote", null, jsx("hp", null, "p1"))),
50
108
  expected: jsx("hquote", null, jsx("hp", null, jsx("cursor", null), "p1"))
109
+ },
110
+ {
111
+ title: 'deletes the empty paragraph after a table with Delete',
112
+ input: jsx("fragment", null, jsx("htable", null, jsx("htr", null, jsx("htd", null, jsx("hp", null, "cell")))), jsx("hp", null, jsx("cursor", null))),
113
+ expected: jsx("fragment", null, jsx("htable", null, jsx("htr", null, jsx("htd", null, jsx("hp", null, "cell")))))
51
114
  }
52
115
  ];
53
116
  const render = (children)=>jsx("editor", null, children, jsx("hp", null, jsx("htext", null)));
@@ -1,4 +1,4 @@
1
- import { BLOCKS, TEXT_CONTAINERS } from '@contentful/rich-text-types';
1
+ import { BLOCKS, HEADINGS, TEXT_CONTAINERS } from '@contentful/rich-text-types';
2
2
  import { getAboveNode, isAncestorEmpty, getNodeEntries, getPointBefore, isInline, isEndPoint, getPointAfter, isRangeCollapsed, queryNode, isText } from '../../internal/queries';
3
3
  import { setSelection, select, removeNodes, splitNodes, unhangRange, unsetNodes } from '../../internal/transforms';
4
4
  export function createTextPlugin(restrictedMarks = []) {
@@ -35,13 +35,27 @@ export function createTextPlugin(restrictedMarks = []) {
35
35
  };
36
36
  const { deleteForward, deleteBackward } = editor;
37
37
  editor.deleteBackward = (unit)=>{
38
- if (editor.selection && editor.api.isCollapsed() && editor.api.isStart(editor.selection.anchor, [])) {
38
+ const headingEntry = editor.selection ? getAboveNode(editor, {
39
+ match: {
40
+ type: HEADINGS
41
+ }
42
+ }) : undefined;
43
+ const isEmptyRootHeading = headingEntry?.[1].length === 1 && isAncestorEmpty(editor, headingEntry[0]);
44
+ if (editor.selection && editor.api.isCollapsed() && editor.api.isStart(editor.selection.anchor, []) && !getAboveNode(editor, {
45
+ match: {
46
+ type: BLOCKS.LIST_ITEM
47
+ }
48
+ }) && !getAboveNode(editor, {
49
+ match: {
50
+ type: BLOCKS.TABLE
51
+ }
52
+ }) && !isEmptyRootHeading) {
39
53
  return;
40
54
  }
41
- deleteEmptyParagraph(unit, editor, deleteBackward);
55
+ deleteEmptyParagraph(unit, editor, deleteBackward, true);
42
56
  };
43
57
  editor.deleteForward = (unit)=>{
44
- deleteEmptyParagraph(unit, editor, deleteForward);
58
+ deleteEmptyParagraph(unit, editor, deleteForward, false);
45
59
  };
46
60
  fixPasteAsPlainText(editor);
47
61
  return editor;
@@ -63,7 +77,7 @@ export function createTextPlugin(restrictedMarks = []) {
63
77
  ]
64
78
  };
65
79
  }
66
- function deleteEmptyParagraph(unit, editor, deleteFunction) {
80
+ function deleteEmptyParagraph(unit, editor, deleteFunction, isBackward) {
67
81
  const entry = getAboveNode(editor, {
68
82
  match: {
69
83
  type: TEXT_CONTAINERS
@@ -74,7 +88,9 @@ function deleteEmptyParagraph(unit, editor, deleteFunction) {
74
88
  const isTextEmpty = isAncestorEmpty(editor, paragraphOrHeading);
75
89
  const isRootLevel = path.length === 1;
76
90
  const hasSiblings = editor.children.length > 1;
77
- if (isTextEmpty && isRootLevel && hasSiblings) {
91
+ const previousRootNode = path.length === 1 && path[0] > 0 ? editor.children[path[0] - 1] : null;
92
+ const isAfterTable = isBackward && paragraphOrHeading.type === BLOCKS.PARAGRAPH && previousRootNode?.type === BLOCKS.TABLE;
93
+ if (isTextEmpty && isRootLevel && hasSiblings && !isAfterTable) {
78
94
  removeNodes(editor, {
79
95
  at: path
80
96
  });
@@ -0,0 +1,2 @@
1
+ import type { PlateEditor } from '../../internal/types/editor';
2
+ export declare const withDeleteBackwardTable: (editor: PlateEditor) => PlateEditor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.6.15",
3
+ "version": "6.6.16",
4
4
  "source": "./src/index.tsx",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -96,5 +96,5 @@
96
96
  "prettier --write"
97
97
  ]
98
98
  },
99
- "gitHead": "5b41250cf53d4733a4dbac6970272611c024d6cb"
99
+ "gitHead": "b2a4dcf2a6d472e0b7a1db47896dedd163711363"
100
100
  }