@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.
@@ -40,20 +40,6 @@ const onKeyDownTable = (editor, plugin)=>{
40
40
  return;
41
41
  }
42
42
  }
43
- if (event.key === 'Backspace') {
44
- const entry = (0, _table.getTableEntries)(editor, {});
45
- if (entry) {
46
- const { table, row, cell } = entry;
47
- const cellText = (0, _queries.getText)(editor, cell[1]);
48
- const isFirstCell = (0, _queries.isFirstChild)(row[1]);
49
- const isFirstRow = (0, _queries.isFirstChild)(table[1]);
50
- if (isFirstCell && isFirstRow && !cellText) {
51
- event.preventDefault();
52
- event.stopPropagation();
53
- return;
54
- }
55
- }
56
- }
57
43
  if (event.key === 'Tab') {
58
44
  const entry = (0, _table.getTableEntries)(editor, {});
59
45
  if (!entry) return;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "withDeleteBackwardTable", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return withDeleteBackwardTable;
9
+ }
10
+ });
11
+ const _richtexttypes = require("@contentful/rich-text-types");
12
+ const _queries = require("../../internal/queries");
13
+ const isElementNode = (node)=>typeof node === 'object' && node !== null && 'type' in node && typeof node.type === 'string' && 'children' in node && Array.isArray(node.children);
14
+ const isEmptyParagraph = (node)=>isElementNode(node) && node.type === _richtexttypes.BLOCKS.PARAGRAPH && node.children.length > 0 && node.children.every((child)=>(0, _queries.isText)(child) && child.text.length === 0);
15
+ const isEmptyTable = (node)=>isElementNode(node) && node.type === _richtexttypes.BLOCKS.TABLE && node.children.length > 0 && node.children.every((row)=>isElementNode(row) && row.type === _richtexttypes.BLOCKS.TABLE_ROW && row.children.length > 0 && row.children.every((cell)=>isElementNode(cell) && (cell.type === _richtexttypes.BLOCKS.TABLE_CELL || cell.type === _richtexttypes.BLOCKS.TABLE_HEADER_CELL) && cell.children.length > 0 && cell.children.every(isEmptyParagraph)));
16
+ const getEmptyTableBeforeBackspace = (editor)=>{
17
+ const { selection } = editor;
18
+ if (!selection || !editor.api.isCollapsed()) return;
19
+ const tableEntry = editor.api.above({
20
+ at: selection.anchor,
21
+ match: {
22
+ type: editor.getType(_richtexttypes.BLOCKS.TABLE)
23
+ }
24
+ });
25
+ if (tableEntry) {
26
+ if (!isEmptyTable(tableEntry[0])) return;
27
+ const firstCellPath = [
28
+ ...tableEntry[1],
29
+ 0,
30
+ 0
31
+ ];
32
+ return editor.api.isStart(selection.anchor, firstCellPath) ? tableEntry[1] : undefined;
33
+ }
34
+ const currentBlockIndex = selection.anchor.path[0];
35
+ const currentBlock = editor.children[currentBlockIndex];
36
+ if (!currentBlock || currentBlock.type !== _richtexttypes.BLOCKS.PARAGRAPH || currentBlockIndex === 0 || !editor.api.isStart(selection.anchor, [
37
+ currentBlockIndex
38
+ ])) {
39
+ return;
40
+ }
41
+ const previousBlock = editor.children[currentBlockIndex - 1];
42
+ return previousBlock && isEmptyTable(previousBlock) ? [
43
+ currentBlockIndex - 1
44
+ ] : undefined;
45
+ };
46
+ const withDeleteBackwardTable = (editor)=>{
47
+ const { deleteBackward } = editor;
48
+ editor.deleteBackward = (unit)=>{
49
+ const emptyTablePath = getEmptyTableBeforeBackspace(editor);
50
+ if (emptyTablePath) {
51
+ editor.tf.removeNodes({
52
+ at: emptyTablePath
53
+ });
54
+ return;
55
+ }
56
+ deleteBackward(unit);
57
+ };
58
+ return editor;
59
+ };
@@ -5,6 +5,64 @@ Object.defineProperty(exports, "__esModule", {
5
5
  const _vitest = require("vitest");
6
6
  const _testutils = require("../../../test-utils");
7
7
  (0, _vitest.describe)('delete backward', ()=>{
8
+ (0, _vitest.test)('removes an empty heading after a table while keeping the table', ()=>{
9
+ const { editor } = (0, _testutils.createTestEditor)({});
10
+ editor.children = [
11
+ {
12
+ type: 'table',
13
+ children: [
14
+ {
15
+ type: 'table-row',
16
+ children: [
17
+ {
18
+ type: 'table-cell',
19
+ children: [
20
+ {
21
+ type: 'paragraph',
22
+ children: [
23
+ {
24
+ text: 'cell'
25
+ }
26
+ ]
27
+ }
28
+ ]
29
+ }
30
+ ]
31
+ }
32
+ ]
33
+ },
34
+ {
35
+ type: 'heading-1',
36
+ children: [
37
+ {
38
+ text: ''
39
+ }
40
+ ]
41
+ },
42
+ {
43
+ type: 'paragraph',
44
+ children: [
45
+ {
46
+ text: ''
47
+ }
48
+ ]
49
+ }
50
+ ];
51
+ editor.tf.select({
52
+ path: [
53
+ 1,
54
+ 0
55
+ ],
56
+ offset: 0
57
+ });
58
+ editor.deleteBackward('character');
59
+ (0, _vitest.expect)(editor.children[0].type).toBe('table');
60
+ (0, _vitest.expect)(editor.children.some((node)=>node.type === 'heading-1')).toBe(false);
61
+ (0, _vitest.expect)(editor.selection?.anchor.path).toEqual([
62
+ 1,
63
+ 0
64
+ ]);
65
+ });
8
66
  const tests = [
9
67
  {
10
68
  title: 'deletes a character of the text inside li',
@@ -52,6 +110,11 @@ const _testutils = require("../../../test-utils");
52
110
  title: 'deletes the first paragraph when followed by a blockquote',
53
111
  input: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null)), (0, _testutils.jsx)("hquote", null, (0, _testutils.jsx)("hp", null, "p1"))),
54
112
  expected: (0, _testutils.jsx)("hquote", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null), "p1"))
113
+ },
114
+ {
115
+ title: 'deletes the empty paragraph after a table with Delete',
116
+ input: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("htable", null, (0, _testutils.jsx)("htr", null, (0, _testutils.jsx)("htd", null, (0, _testutils.jsx)("hp", null, "cell")))), (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null))),
117
+ expected: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("htable", null, (0, _testutils.jsx)("htr", null, (0, _testutils.jsx)("htd", null, (0, _testutils.jsx)("hp", null, "cell")))))
55
118
  }
56
119
  ];
57
120
  const render = (children)=>(0, _testutils.jsx)("editor", null, children, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("htext", null)));
@@ -45,13 +45,27 @@ function createTextPlugin(restrictedMarks = []) {
45
45
  };
46
46
  const { deleteForward, deleteBackward } = editor;
47
47
  editor.deleteBackward = (unit)=>{
48
- if (editor.selection && editor.api.isCollapsed() && editor.api.isStart(editor.selection.anchor, [])) {
48
+ const headingEntry = editor.selection ? (0, _queries.getAboveNode)(editor, {
49
+ match: {
50
+ type: _richtexttypes.HEADINGS
51
+ }
52
+ }) : undefined;
53
+ const isEmptyRootHeading = headingEntry?.[1].length === 1 && (0, _queries.isAncestorEmpty)(editor, headingEntry[0]);
54
+ if (editor.selection && editor.api.isCollapsed() && editor.api.isStart(editor.selection.anchor, []) && !(0, _queries.getAboveNode)(editor, {
55
+ match: {
56
+ type: _richtexttypes.BLOCKS.LIST_ITEM
57
+ }
58
+ }) && !(0, _queries.getAboveNode)(editor, {
59
+ match: {
60
+ type: _richtexttypes.BLOCKS.TABLE
61
+ }
62
+ }) && !isEmptyRootHeading) {
49
63
  return;
50
64
  }
51
- deleteEmptyParagraph(unit, editor, deleteBackward);
65
+ deleteEmptyParagraph(unit, editor, deleteBackward, true);
52
66
  };
53
67
  editor.deleteForward = (unit)=>{
54
- deleteEmptyParagraph(unit, editor, deleteForward);
68
+ deleteEmptyParagraph(unit, editor, deleteForward, false);
55
69
  };
56
70
  fixPasteAsPlainText(editor);
57
71
  return editor;
@@ -73,7 +87,7 @@ function createTextPlugin(restrictedMarks = []) {
73
87
  ]
74
88
  };
75
89
  }
76
- function deleteEmptyParagraph(unit, editor, deleteFunction) {
90
+ function deleteEmptyParagraph(unit, editor, deleteFunction, isBackward) {
77
91
  const entry = (0, _queries.getAboveNode)(editor, {
78
92
  match: {
79
93
  type: _richtexttypes.TEXT_CONTAINERS
@@ -84,7 +98,9 @@ function deleteEmptyParagraph(unit, editor, deleteFunction) {
84
98
  const isTextEmpty = (0, _queries.isAncestorEmpty)(editor, paragraphOrHeading);
85
99
  const isRootLevel = path.length === 1;
86
100
  const hasSiblings = editor.children.length > 1;
87
- if (isTextEmpty && isRootLevel && hasSiblings) {
101
+ const previousRootNode = path.length === 1 && path[0] > 0 ? editor.children[path[0] - 1] : null;
102
+ const isAfterTable = isBackward && paragraphOrHeading.type === _richtexttypes.BLOCKS.PARAGRAPH && previousRootNode?.type === _richtexttypes.BLOCKS.TABLE;
103
+ if (isTextEmpty && isRootLevel && hasSiblings && !isAfterTable) {
88
104
  (0, _transforms.removeNodes)(editor, {
89
105
  at: path
90
106
  });
@@ -1,7 +1,89 @@
1
- import { BLOCKS } from '@contentful/rich-text-types';
1
+ import { BLOCKS, HEADINGS } from '@contentful/rich-text-types';
2
2
  import { describe, expect, it, vi } from 'vitest';
3
3
  import { assertOutput, createTestEditor, jsx } from '../../../test-utils';
4
4
  import { createHeadingPlugin } from '../createHeadingPlugin';
5
+ describe('backward deletion', ()=>{
6
+ it.each(HEADINGS)('keeps a non-empty %s when Backspace is pressed at the document start', (type)=>{
7
+ const { editor } = createTestEditor({});
8
+ editor.children = [
9
+ {
10
+ type,
11
+ children: [
12
+ {
13
+ text: 'x'
14
+ }
15
+ ]
16
+ },
17
+ {
18
+ type: BLOCKS.PARAGRAPH,
19
+ children: [
20
+ {
21
+ text: ''
22
+ }
23
+ ]
24
+ }
25
+ ];
26
+ editor.tf.select({
27
+ path: [
28
+ 0,
29
+ 0
30
+ ],
31
+ offset: 0
32
+ });
33
+ editor.deleteBackward('character');
34
+ expect(editor.children[0]).toMatchObject({
35
+ type,
36
+ children: [
37
+ {
38
+ text: 'x'
39
+ }
40
+ ]
41
+ });
42
+ });
43
+ it.each(HEADINGS)('removes an empty %s on an additional Backspace', (type)=>{
44
+ const { editor } = createTestEditor({});
45
+ editor.children = [
46
+ {
47
+ type,
48
+ children: [
49
+ {
50
+ text: 'x'
51
+ }
52
+ ]
53
+ },
54
+ {
55
+ type: BLOCKS.PARAGRAPH,
56
+ children: [
57
+ {
58
+ text: ''
59
+ }
60
+ ]
61
+ }
62
+ ];
63
+ editor.tf.select({
64
+ path: [
65
+ 0,
66
+ 0
67
+ ],
68
+ offset: 1
69
+ });
70
+ editor.deleteBackward('character');
71
+ expect(editor.children[0]).toMatchObject({
72
+ type,
73
+ children: [
74
+ {
75
+ text: ''
76
+ }
77
+ ]
78
+ });
79
+ editor.deleteBackward('character');
80
+ expect(editor.children[0].type).toBe(BLOCKS.PARAGRAPH);
81
+ expect(editor.selection?.anchor.path).toEqual([
82
+ 0,
83
+ 0
84
+ ]);
85
+ });
86
+ });
5
87
  describe('normalization', ()=>{
6
88
  it('can contain inline entries & hyperlinks', ()=>{
7
89
  const input = jsx("editor", null, jsx("hh1", null, "some text before", jsx("hinline", {
@@ -1,5 +1,6 @@
1
- import { describe, it } from 'vitest';
2
- import { assertOutput, jsx } from '../../../test-utils';
1
+ import { BLOCKS, INLINES } from '@contentful/rich-text-types';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { assertOutput, createTestEditor, jsx } from '../../../test-utils';
3
4
  describe('normalization', ()=>{
4
5
  it('wraps orphaned list items in a list', ()=>{
5
6
  const input = jsx("editor", null, jsx("hli", null, jsx("hp", null, "Item")), jsx("hp", null));
@@ -42,3 +43,200 @@ describe('normalization', ()=>{
42
43
  });
43
44
  });
44
45
  });
46
+ describe('backward deletion', ()=>{
47
+ it.each([
48
+ {
49
+ type: 'hul',
50
+ listName: 'unordered list'
51
+ },
52
+ {
53
+ type: 'hol',
54
+ listName: 'ordered list'
55
+ }
56
+ ])('removes an empty $listName at the start of the document', ({ type })=>{
57
+ const input = jsx("editor", null, type === 'hul' ? jsx("hul", null, jsx("hli", null, jsx("hp", null, jsx("cursor", null)))) : jsx("hol", null, jsx("hli", null, jsx("hp", null, jsx("cursor", null)))), jsx("hp", null, jsx("htext", null)));
58
+ const { editor } = createTestEditor({
59
+ input
60
+ });
61
+ editor.deleteBackward('character');
62
+ const expected = jsx("editor", null, jsx("hp", null, jsx("cursor", null)));
63
+ assertOutput({
64
+ editor,
65
+ expected
66
+ });
67
+ });
68
+ it.each([
69
+ {
70
+ title: 'whitespace',
71
+ itemChildren: [
72
+ {
73
+ type: BLOCKS.PARAGRAPH,
74
+ children: [
75
+ {
76
+ text: ' '
77
+ }
78
+ ]
79
+ }
80
+ ],
81
+ expectedContent: ' '
82
+ },
83
+ {
84
+ title: 'a later paragraph in a multi-paragraph item',
85
+ itemChildren: [
86
+ {
87
+ type: BLOCKS.PARAGRAPH,
88
+ children: [
89
+ {
90
+ text: ''
91
+ }
92
+ ]
93
+ },
94
+ {
95
+ type: BLOCKS.PARAGRAPH,
96
+ children: [
97
+ {
98
+ text: 'content'
99
+ }
100
+ ]
101
+ }
102
+ ],
103
+ expectedContent: 'content'
104
+ },
105
+ {
106
+ title: 'a nested list',
107
+ itemChildren: [
108
+ {
109
+ type: BLOCKS.PARAGRAPH,
110
+ children: [
111
+ {
112
+ text: ''
113
+ }
114
+ ]
115
+ },
116
+ {
117
+ type: BLOCKS.UL_LIST,
118
+ children: [
119
+ {
120
+ type: BLOCKS.LIST_ITEM,
121
+ children: [
122
+ {
123
+ type: BLOCKS.PARAGRAPH,
124
+ children: [
125
+ {
126
+ text: 'nested'
127
+ }
128
+ ]
129
+ }
130
+ ]
131
+ }
132
+ ]
133
+ }
134
+ ],
135
+ expectedContent: 'nested'
136
+ },
137
+ {
138
+ title: 'a heading',
139
+ itemChildren: [
140
+ {
141
+ type: BLOCKS.HEADING_1,
142
+ children: [
143
+ {
144
+ text: 'heading'
145
+ }
146
+ ]
147
+ }
148
+ ],
149
+ expectedContent: 'heading'
150
+ },
151
+ {
152
+ title: 'an embedded entry',
153
+ itemChildren: [
154
+ {
155
+ type: BLOCKS.PARAGRAPH,
156
+ children: [
157
+ {
158
+ text: ''
159
+ }
160
+ ]
161
+ },
162
+ {
163
+ type: BLOCKS.EMBEDDED_ENTRY,
164
+ data: {
165
+ target: {}
166
+ },
167
+ children: [
168
+ {
169
+ text: ''
170
+ }
171
+ ]
172
+ }
173
+ ],
174
+ expectedContent: BLOCKS.EMBEDDED_ENTRY
175
+ },
176
+ {
177
+ title: 'an embedded inline',
178
+ itemChildren: [
179
+ {
180
+ type: BLOCKS.PARAGRAPH,
181
+ children: [
182
+ {
183
+ text: ''
184
+ }
185
+ ]
186
+ },
187
+ {
188
+ type: BLOCKS.PARAGRAPH,
189
+ children: [
190
+ {
191
+ type: INLINES.EMBEDDED_ENTRY,
192
+ data: {
193
+ target: {}
194
+ },
195
+ children: [
196
+ {
197
+ text: ''
198
+ }
199
+ ]
200
+ }
201
+ ]
202
+ }
203
+ ],
204
+ expectedContent: INLINES.EMBEDDED_ENTRY
205
+ }
206
+ ])('preserves a list containing $title', ({ itemChildren, expectedContent })=>{
207
+ const { editor } = createTestEditor({});
208
+ const list = {
209
+ type: BLOCKS.UL_LIST,
210
+ children: [
211
+ {
212
+ type: BLOCKS.LIST_ITEM,
213
+ children: itemChildren
214
+ }
215
+ ]
216
+ };
217
+ editor.children = [
218
+ list,
219
+ {
220
+ type: BLOCKS.PARAGRAPH,
221
+ children: [
222
+ {
223
+ text: ''
224
+ }
225
+ ]
226
+ }
227
+ ];
228
+ editor.tf.select({
229
+ path: [
230
+ 0,
231
+ 0,
232
+ 0,
233
+ 0
234
+ ],
235
+ offset: 0
236
+ });
237
+ editor.deleteBackward('character');
238
+ const updatedList = JSON.stringify(editor.children[0]);
239
+ expect(editor.children[0].type).toBe(BLOCKS.UL_LIST);
240
+ expect(updatedList).toContain(expectedContent);
241
+ });
242
+ });
@@ -1,10 +1,34 @@
1
- import { LIST_ITEM_BLOCKS } from '@contentful/rich-text-types';
1
+ import { BLOCKS, LIST_ITEM_BLOCKS } from '@contentful/rich-text-types';
2
2
  import { normalizeList, deleteFragmentList, deleteForwardList } from '../../internal/list';
3
+ import { getAboveNode, isText } from '../../internal/queries';
3
4
  import { insertListBreak } from './insertListBreak';
4
5
  import { insertListFragment } from './insertListFragment';
5
6
  const validLiChildrenTypes = LIST_ITEM_BLOCKS;
7
+ const isEmptyList = (node)=>(node.type === BLOCKS.UL_LIST || node.type === BLOCKS.OL_LIST) && node.children.length > 0 && node.children.every((item)=>!isText(item) && item.type === BLOCKS.LIST_ITEM && item.children.length > 0 && item.children.every((block)=>!isText(block) && block.type === BLOCKS.PARAGRAPH && block.children.length > 0 && block.children.every((child)=>isText(child) && child.text.length === 0)));
6
8
  export const withList = (editor)=>{
7
- const { deleteForward, deleteFragment } = editor;
9
+ const { deleteBackward, deleteForward, deleteFragment } = editor;
10
+ editor.deleteBackward = (unit)=>{
11
+ const listEntry = getAboveNode(editor, {
12
+ match: {
13
+ type: [
14
+ BLOCKS.UL_LIST,
15
+ BLOCKS.OL_LIST
16
+ ]
17
+ }
18
+ });
19
+ if (editor.selection && editor.api.isCollapsed() && listEntry && listEntry[1].length === 1 && isEmptyList(listEntry[0]) && editor.api.isStart(editor.selection.anchor, listEntry[1])) {
20
+ editor.tf.removeNodes({
21
+ at: listEntry[1]
22
+ });
23
+ const nextBlockIndex = Math.min(listEntry[1][0], editor.children.length - 1);
24
+ const nextBlockStart = editor.api.start([
25
+ nextBlockIndex
26
+ ]);
27
+ if (nextBlockStart) editor.tf.select(nextBlockStart);
28
+ return;
29
+ }
30
+ deleteBackward(unit);
31
+ };
8
32
  editor.deleteForward = (unit)=>{
9
33
  if (deleteForwardList(editor, deleteForward, unit)) return;
10
34
  deleteForward(unit);