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

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.
package/README.md CHANGED
@@ -10,6 +10,10 @@ This package contains a React `RichTextEditor` component that is used as the def
10
10
  import { RichTextEditor } from '@contentful/field-editor-rich-text';
11
11
  ```
12
12
 
13
+ ## Clearing a field
14
+
15
+ When an edit leaves only empty paragraphs or list items, the editor removes the locale's field value. Blank paragraphs within documents containing text or other content are preserved, as are whitespace, embedded entities, horizontal rules, and tables.
16
+
13
17
  ## ⚠️ Important: Package Configuration
14
18
 
15
19
  Plate 53.3.14 pins `slate-react` to 0.126.4. To use the latest Slate React version tested by this package (0.127.1), update existing overrides in your application as shown below. Package-manager overrides in this repository are not inherited by applications that install the published package.
@@ -19,7 +19,6 @@ _export(exports, {
19
19
  const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
20
20
  const _fieldeditorreference = require("@contentful/field-editor-reference");
21
21
  const _fieldeditorshared = require("@contentful/field-editor-shared");
22
- const _richtexttypes = /*#__PURE__*/ _interop_require_wildcard(require("@contentful/rich-text-types"));
23
22
  const _css = require("@emotion/css");
24
23
  const _fastdeepequal = /*#__PURE__*/ _interop_require_default(require("fast-deep-equal"));
25
24
  const _noop = /*#__PURE__*/ _interop_require_default(require("lodash/noop"));
@@ -28,6 +27,7 @@ const _CharConstraints = require("./CharConstraints");
28
27
  const _ContentfulEditorProvider = require("./ContentfulEditorProvider");
29
28
  const _editoroverrides = require("./editor-overrides");
30
29
  const _callbacks = require("./helpers/callbacks");
30
+ const _isEmptyRichTextDocument = require("./helpers/isEmptyRichTextDocument");
31
31
  const _toSlateDoc = require("./helpers/toSlateDoc");
32
32
  const _plate = require("./internal/plate");
33
33
  const _pluginAdapter = require("./internal/pluginAdapter");
@@ -173,7 +173,7 @@ const ConnectedRichTextEditor = _react.memo(function ConnectedRichTextEditor(pro
173
173
  });
174
174
  const RichTextEditor = (props)=>{
175
175
  const { sdk, isInitiallyDisabled, onAction, restrictedMarks, onChange, isDisabled, queryClient, ...otherProps } = props;
176
- const isEmptyValue = _react.useCallback((value)=>!value || (0, _fastdeepequal.default)(value, _richtexttypes.EMPTY_DOCUMENT), []);
176
+ const isEmptyValue = _react.useCallback((value)=>!value || (0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(value), []);
177
177
  _react.useEffect(()=>{
178
178
  if (!onChange) {
179
179
  return;
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
6
+ const _fieldeditorshared = require("@contentful/field-editor-shared");
7
+ const _fieldeditortestutils = require("@contentful/field-editor-test-utils");
8
+ const _richtexttypes = require("@contentful/rich-text-types");
9
+ const _react1 = require("@testing-library/react");
10
+ const _vitest = require("vitest");
11
+ const _isEmptyRichTextDocument = require("./isEmptyRichTextDocument");
12
+ function _getRequireWildcardCache(nodeInterop) {
13
+ if (typeof WeakMap !== "function") return null;
14
+ var cacheBabelInterop = new WeakMap();
15
+ var cacheNodeInterop = new WeakMap();
16
+ return (_getRequireWildcardCache = function(nodeInterop) {
17
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
18
+ })(nodeInterop);
19
+ }
20
+ function _interop_require_wildcard(obj, nodeInterop) {
21
+ if (!nodeInterop && obj && obj.__esModule) {
22
+ return obj;
23
+ }
24
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
25
+ return {
26
+ default: obj
27
+ };
28
+ }
29
+ var cache = _getRequireWildcardCache(nodeInterop);
30
+ if (cache && cache.has(obj)) {
31
+ return cache.get(obj);
32
+ }
33
+ var newObj = {
34
+ __proto__: null
35
+ };
36
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
37
+ for(var key in obj){
38
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
39
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
40
+ if (desc && (desc.get || desc.set)) {
41
+ Object.defineProperty(newObj, key, desc);
42
+ } else {
43
+ newObj[key] = obj[key];
44
+ }
45
+ }
46
+ }
47
+ newObj.default = obj;
48
+ if (cache) {
49
+ cache.set(obj, newObj);
50
+ }
51
+ return newObj;
52
+ }
53
+ const paragraph = (value, marks = [])=>({
54
+ nodeType: _richtexttypes.BLOCKS.PARAGRAPH,
55
+ data: {},
56
+ content: [
57
+ {
58
+ nodeType: 'text',
59
+ value,
60
+ marks,
61
+ data: {}
62
+ }
63
+ ]
64
+ });
65
+ const document = (...content)=>({
66
+ nodeType: _richtexttypes.BLOCKS.DOCUMENT,
67
+ data: {},
68
+ content
69
+ });
70
+ const initialValue = document(paragraph('content'));
71
+ const emptyList = (type)=>document({
72
+ nodeType: type,
73
+ data: {},
74
+ content: [
75
+ {
76
+ nodeType: _richtexttypes.BLOCKS.LIST_ITEM,
77
+ data: {},
78
+ content: [
79
+ paragraph('', [
80
+ {
81
+ type: _richtexttypes.MARKS.BOLD
82
+ }
83
+ ])
84
+ ]
85
+ }
86
+ ]
87
+ }, paragraph(''));
88
+ _vitest.it.each([
89
+ [
90
+ 'multiple empty paragraphs',
91
+ document(paragraph(''), paragraph(''))
92
+ ],
93
+ [
94
+ 'an empty unordered list',
95
+ emptyList(_richtexttypes.BLOCKS.UL_LIST)
96
+ ],
97
+ [
98
+ 'an empty ordered list',
99
+ emptyList(_richtexttypes.BLOCKS.OL_LIST)
100
+ ]
101
+ ])('removes the SDK field value after editing to %s', async (_description, editedValue)=>{
102
+ const [field] = (0, _fieldeditortestutils.createFakeFieldAPI)(undefined, initialValue);
103
+ (0, _react1.render)(_react.createElement(_fieldeditorshared.FieldConnector, {
104
+ field: field,
105
+ isInitiallyDisabled: false,
106
+ isEmptyValue: _isEmptyRichTextDocument.isEmptyRichTextDocument,
107
+ debounce: 0
108
+ }, ({ setValue })=>_react.createElement("button", {
109
+ onClick: ()=>void setValue(editedValue)
110
+ }, "Save edit")));
111
+ (0, _vitest.expect)(field.getValue()).toEqual(initialValue);
112
+ _react1.fireEvent.click(_react1.screen.getByRole('button', {
113
+ name: 'Save edit'
114
+ }));
115
+ await (0, _react1.waitFor)(()=>(0, _vitest.expect)(field.getValue()).toBeUndefined());
116
+ });
117
+ (0, _vitest.it)('saves a horizontal rule even though it contains no text', async ()=>{
118
+ const [field] = (0, _fieldeditortestutils.createFakeFieldAPI)(undefined, initialValue);
119
+ const editedValue = document({
120
+ nodeType: _richtexttypes.BLOCKS.HR,
121
+ data: {},
122
+ content: []
123
+ }, paragraph(''));
124
+ (0, _react1.render)(_react.createElement(_fieldeditorshared.FieldConnector, {
125
+ field: field,
126
+ isInitiallyDisabled: false,
127
+ isEmptyValue: _isEmptyRichTextDocument.isEmptyRichTextDocument,
128
+ debounce: 0
129
+ }, ({ setValue })=>_react.createElement("button", {
130
+ onClick: ()=>void setValue(editedValue)
131
+ }, "Save edit")));
132
+ _react1.fireEvent.click(_react1.screen.getByRole('button', {
133
+ name: 'Save edit'
134
+ }));
135
+ await (0, _react1.waitFor)(()=>(0, _vitest.expect)(field.getValue()).toEqual(editedValue));
136
+ });
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "isEmptyRichTextDocument", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return isEmptyRichTextDocument;
9
+ }
10
+ });
11
+ const _richtexttypes = require("@contentful/rich-text-types");
12
+ const knownMarkTypes = new Set(Object.values(_richtexttypes.MARKS));
13
+ const hasOnlyKeys = (value, keys)=>Object.keys(value).every((key)=>keys.includes(key));
14
+ const isJsonObject = (value)=>value !== null && typeof value === 'object' && !Array.isArray(value);
15
+ const hasEmptyData = (value)=>isJsonObject(value) && Object.keys(value).length === 0;
16
+ const isKnownMark = (value)=>isJsonObject(value) && hasOnlyKeys(value, [
17
+ 'type'
18
+ ]) && typeof value.type === 'string' && knownMarkTypes.has(value.type);
19
+ function isEmptyText(node) {
20
+ return isJsonObject(node) && hasOnlyKeys(node, [
21
+ 'nodeType',
22
+ 'value',
23
+ 'marks',
24
+ 'data'
25
+ ]) && node.nodeType === 'text' && node.value === '' && Array.isArray(node.marks) && node.marks.every(isKnownMark) && hasEmptyData(node.data);
26
+ }
27
+ function isEmptyParagraph(node) {
28
+ return isJsonObject(node) && hasOnlyKeys(node, [
29
+ 'nodeType',
30
+ 'data',
31
+ 'content'
32
+ ]) && node.nodeType === _richtexttypes.BLOCKS.PARAGRAPH && hasEmptyData(node.data) && Array.isArray(node.content) && node.content.length > 0 && node.content.every(isEmptyText);
33
+ }
34
+ function isEmptyList(node) {
35
+ return isJsonObject(node) && hasOnlyKeys(node, [
36
+ 'nodeType',
37
+ 'data',
38
+ 'content'
39
+ ]) && (node.nodeType === _richtexttypes.BLOCKS.UL_LIST || node.nodeType === _richtexttypes.BLOCKS.OL_LIST) && hasEmptyData(node.data) && Array.isArray(node.content) && node.content.every(isEmptyListItem);
40
+ }
41
+ function isEmptyListItem(node) {
42
+ return isJsonObject(node) && hasOnlyKeys(node, [
43
+ 'nodeType',
44
+ 'data',
45
+ 'content'
46
+ ]) && node.nodeType === _richtexttypes.BLOCKS.LIST_ITEM && hasEmptyData(node.data) && Array.isArray(node.content) && node.content.length > 0 && node.content.every((child)=>isEmptyParagraph(child) || isEmptyList(child));
47
+ }
48
+ function isEmptyRichTextDocument(value) {
49
+ return isJsonObject(value) && hasOnlyKeys(value, [
50
+ 'nodeType',
51
+ 'data',
52
+ 'content'
53
+ ]) && value.nodeType === _richtexttypes.BLOCKS.DOCUMENT && hasEmptyData(value.data) && Array.isArray(value.content) && value.content.every((node)=>isEmptyParagraph(node) || isEmptyList(node));
54
+ }
@@ -0,0 +1,277 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _richtexttypes = require("@contentful/rich-text-types");
6
+ const _vitest = require("vitest");
7
+ const _isEmptyRichTextDocument = require("./isEmptyRichTextDocument");
8
+ const emptyText = {
9
+ nodeType: 'text',
10
+ value: '',
11
+ marks: [],
12
+ data: {}
13
+ };
14
+ const emptyParagraph = {
15
+ nodeType: 'paragraph',
16
+ data: {},
17
+ content: [
18
+ emptyText
19
+ ]
20
+ };
21
+ const document = (...content)=>({
22
+ nodeType: 'document',
23
+ data: {},
24
+ content
25
+ });
26
+ (0, _vitest.describe)('isEmptyRichTextDocument', ()=>{
27
+ (0, _vitest.it)('recognizes the canonical empty document', ()=>{
28
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(document(emptyParagraph))).toBe(true);
29
+ });
30
+ (0, _vitest.it)('recognizes documents with multiple empty paragraphs', ()=>{
31
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(document(emptyParagraph, emptyParagraph))).toBe(true);
32
+ });
33
+ _vitest.it.each(Object.values(_richtexttypes.MARKS))('recognizes empty text with the %s mark', (type)=>{
34
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(document({
35
+ ...emptyParagraph,
36
+ content: [
37
+ {
38
+ ...emptyText,
39
+ marks: [
40
+ {
41
+ type
42
+ }
43
+ ]
44
+ }
45
+ ]
46
+ }))).toBe(true);
47
+ });
48
+ (0, _vitest.it)('recognizes an empty list with multiple formatting marks', ()=>{
49
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(document({
50
+ nodeType: 'unordered-list',
51
+ data: {},
52
+ content: [
53
+ {
54
+ nodeType: 'list-item',
55
+ data: {},
56
+ content: [
57
+ {
58
+ ...emptyParagraph,
59
+ content: [
60
+ {
61
+ ...emptyText,
62
+ marks: [
63
+ {
64
+ type: _richtexttypes.MARKS.BOLD
65
+ },
66
+ {
67
+ type: _richtexttypes.MARKS.ITALIC
68
+ }
69
+ ]
70
+ }
71
+ ]
72
+ }
73
+ ]
74
+ }
75
+ ]
76
+ }))).toBe(true);
77
+ });
78
+ (0, _vitest.it)('recognizes an empty list followed by the editor trailing paragraph', ()=>{
79
+ const emptyList = {
80
+ nodeType: 'unordered-list',
81
+ data: {},
82
+ content: [
83
+ {
84
+ nodeType: 'list-item',
85
+ data: {},
86
+ content: [
87
+ emptyParagraph
88
+ ]
89
+ }
90
+ ]
91
+ };
92
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(document(emptyList, emptyParagraph))).toBe(true);
93
+ });
94
+ (0, _vitest.it)('recognizes empty nested ordered and unordered lists', ()=>{
95
+ const nestedList = {
96
+ nodeType: 'ordered-list',
97
+ data: {},
98
+ content: [
99
+ {
100
+ nodeType: 'list-item',
101
+ data: {},
102
+ content: [
103
+ emptyParagraph,
104
+ {
105
+ nodeType: 'unordered-list',
106
+ data: {},
107
+ content: [
108
+ {
109
+ nodeType: 'list-item',
110
+ data: {},
111
+ content: [
112
+ emptyParagraph
113
+ ]
114
+ }
115
+ ]
116
+ }
117
+ ]
118
+ }
119
+ ]
120
+ };
121
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(document(nestedList))).toBe(true);
122
+ });
123
+ (0, _vitest.it)('recognizes a document with no blocks', ()=>{
124
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(document())).toBe(true);
125
+ });
126
+ _vitest.it.each([
127
+ [
128
+ 'whitespace text',
129
+ document({
130
+ ...emptyParagraph,
131
+ content: [
132
+ {
133
+ ...emptyText,
134
+ value: ' '
135
+ }
136
+ ]
137
+ })
138
+ ],
139
+ [
140
+ 'nonempty text',
141
+ document({
142
+ ...emptyParagraph,
143
+ content: [
144
+ {
145
+ ...emptyText,
146
+ value: 'text'
147
+ }
148
+ ]
149
+ })
150
+ ],
151
+ [
152
+ 'an unknown mark',
153
+ document({
154
+ ...emptyParagraph,
155
+ content: [
156
+ {
157
+ ...emptyText,
158
+ marks: [
159
+ {
160
+ type: 'custom-mark'
161
+ }
162
+ ]
163
+ }
164
+ ]
165
+ })
166
+ ],
167
+ [
168
+ 'mark metadata',
169
+ document({
170
+ ...emptyParagraph,
171
+ content: [
172
+ {
173
+ ...emptyText,
174
+ marks: [
175
+ {
176
+ type: _richtexttypes.MARKS.BOLD,
177
+ custom: true
178
+ }
179
+ ]
180
+ }
181
+ ]
182
+ })
183
+ ],
184
+ [
185
+ 'an embedded entry',
186
+ document({
187
+ nodeType: 'embedded-entry-block',
188
+ data: {},
189
+ content: []
190
+ })
191
+ ],
192
+ [
193
+ 'a horizontal rule',
194
+ document({
195
+ nodeType: 'hr',
196
+ data: {},
197
+ content: []
198
+ })
199
+ ],
200
+ [
201
+ 'a table',
202
+ document({
203
+ nodeType: 'table',
204
+ data: {},
205
+ content: []
206
+ })
207
+ ],
208
+ [
209
+ 'an empty heading',
210
+ document({
211
+ ...emptyParagraph,
212
+ nodeType: 'heading-1'
213
+ })
214
+ ],
215
+ [
216
+ 'blank paragraphs around text',
217
+ document(emptyParagraph, {
218
+ ...emptyParagraph,
219
+ content: [
220
+ {
221
+ ...emptyText,
222
+ value: 'text'
223
+ }
224
+ ]
225
+ }, emptyParagraph)
226
+ ],
227
+ [
228
+ 'an unknown node',
229
+ document({
230
+ nodeType: 'custom-block',
231
+ data: {},
232
+ content: []
233
+ })
234
+ ],
235
+ [
236
+ 'custom node data',
237
+ document({
238
+ ...emptyParagraph,
239
+ data: {
240
+ custom: true
241
+ }
242
+ })
243
+ ],
244
+ [
245
+ 'extra document data',
246
+ {
247
+ ...document(emptyParagraph),
248
+ data: {
249
+ custom: true
250
+ }
251
+ }
252
+ ]
253
+ ])('preserves %s', (_description, value)=>{
254
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(value)).toBe(false);
255
+ });
256
+ (0, _vitest.it)('preserves invalid or unknown structures', ()=>{
257
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(null)).toBe(false);
258
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)({
259
+ nodeType: 'document',
260
+ data: {},
261
+ content: [
262
+ null
263
+ ]
264
+ })).toBe(false);
265
+ (0, _vitest.expect)((0, _isEmptyRichTextDocument.isEmptyRichTextDocument)(document({
266
+ nodeType: 'unordered-list',
267
+ data: {},
268
+ content: [
269
+ {
270
+ nodeType: 'list-item',
271
+ data: {},
272
+ content: []
273
+ }
274
+ ]
275
+ }))).toBe(false);
276
+ });
277
+ });
@@ -1,7 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { EntityProvider } from '@contentful/field-editor-reference';
3
3
  import { FieldConnector } from '@contentful/field-editor-shared';
4
- import * as Contentful from '@contentful/rich-text-types';
5
4
  import { css, cx } from '@emotion/css';
6
5
  import deepEquals from 'fast-deep-equal';
7
6
  import noop from 'lodash/noop';
@@ -10,6 +9,7 @@ import { CharConstraints } from './CharConstraints';
10
9
  import { ContentfulEditorIdProvider, getContentfulEditorId } from './ContentfulEditorProvider';
11
10
  import { defaultScrollSelectionIntoView } from './editor-overrides';
12
11
  import { createOnChangeCallback } from './helpers/callbacks';
12
+ import { isEmptyRichTextDocument } from './helpers/isEmptyRichTextDocument';
13
13
  import { toSlateDoc } from './helpers/toSlateDoc';
14
14
  import { PlateContent, Plate } from './internal/plate';
15
15
  import { createPlateEditor } from './internal/pluginAdapter';
@@ -109,7 +109,7 @@ export const ConnectedRichTextEditor = React.memo(function ConnectedRichTextEdit
109
109
  });
110
110
  const RichTextEditor = (props)=>{
111
111
  const { sdk, isInitiallyDisabled, onAction, restrictedMarks, onChange, isDisabled, queryClient, ...otherProps } = props;
112
- const isEmptyValue = React.useCallback((value)=>!value || deepEquals(value, Contentful.EMPTY_DOCUMENT), []);
112
+ const isEmptyValue = React.useCallback((value)=>!value || isEmptyRichTextDocument(value), []);
113
113
  React.useEffect(()=>{
114
114
  if (!onChange) {
115
115
  return;
@@ -0,0 +1,91 @@
1
+ import * as React from 'react';
2
+ import { FieldConnector } from '@contentful/field-editor-shared';
3
+ import { createFakeFieldAPI } from '@contentful/field-editor-test-utils';
4
+ import { BLOCKS, MARKS } from '@contentful/rich-text-types';
5
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
6
+ import { expect, it } from 'vitest';
7
+ import { isEmptyRichTextDocument } from './isEmptyRichTextDocument';
8
+ const paragraph = (value, marks = [])=>({
9
+ nodeType: BLOCKS.PARAGRAPH,
10
+ data: {},
11
+ content: [
12
+ {
13
+ nodeType: 'text',
14
+ value,
15
+ marks,
16
+ data: {}
17
+ }
18
+ ]
19
+ });
20
+ const document = (...content)=>({
21
+ nodeType: BLOCKS.DOCUMENT,
22
+ data: {},
23
+ content
24
+ });
25
+ const initialValue = document(paragraph('content'));
26
+ const emptyList = (type)=>document({
27
+ nodeType: type,
28
+ data: {},
29
+ content: [
30
+ {
31
+ nodeType: BLOCKS.LIST_ITEM,
32
+ data: {},
33
+ content: [
34
+ paragraph('', [
35
+ {
36
+ type: MARKS.BOLD
37
+ }
38
+ ])
39
+ ]
40
+ }
41
+ ]
42
+ }, paragraph(''));
43
+ it.each([
44
+ [
45
+ 'multiple empty paragraphs',
46
+ document(paragraph(''), paragraph(''))
47
+ ],
48
+ [
49
+ 'an empty unordered list',
50
+ emptyList(BLOCKS.UL_LIST)
51
+ ],
52
+ [
53
+ 'an empty ordered list',
54
+ emptyList(BLOCKS.OL_LIST)
55
+ ]
56
+ ])('removes the SDK field value after editing to %s', async (_description, editedValue)=>{
57
+ const [field] = createFakeFieldAPI(undefined, initialValue);
58
+ render(React.createElement(FieldConnector, {
59
+ field: field,
60
+ isInitiallyDisabled: false,
61
+ isEmptyValue: isEmptyRichTextDocument,
62
+ debounce: 0
63
+ }, ({ setValue })=>React.createElement("button", {
64
+ onClick: ()=>void setValue(editedValue)
65
+ }, "Save edit")));
66
+ expect(field.getValue()).toEqual(initialValue);
67
+ fireEvent.click(screen.getByRole('button', {
68
+ name: 'Save edit'
69
+ }));
70
+ await waitFor(()=>expect(field.getValue()).toBeUndefined());
71
+ });
72
+ it('saves a horizontal rule even though it contains no text', async ()=>{
73
+ const [field] = createFakeFieldAPI(undefined, initialValue);
74
+ const editedValue = document({
75
+ nodeType: BLOCKS.HR,
76
+ data: {},
77
+ content: []
78
+ }, paragraph(''));
79
+ render(React.createElement(FieldConnector, {
80
+ field: field,
81
+ isInitiallyDisabled: false,
82
+ isEmptyValue: isEmptyRichTextDocument,
83
+ debounce: 0
84
+ }, ({ setValue })=>React.createElement("button", {
85
+ onClick: ()=>void setValue(editedValue)
86
+ }, "Save edit")));
87
+ fireEvent.click(screen.getByRole('button', {
88
+ name: 'Save edit'
89
+ }));
90
+ await waitFor(()=>expect(field.getValue()).toEqual(editedValue));
91
+ });
@@ -0,0 +1,44 @@
1
+ import { BLOCKS, MARKS } from '@contentful/rich-text-types';
2
+ const knownMarkTypes = new Set(Object.values(MARKS));
3
+ const hasOnlyKeys = (value, keys)=>Object.keys(value).every((key)=>keys.includes(key));
4
+ const isJsonObject = (value)=>value !== null && typeof value === 'object' && !Array.isArray(value);
5
+ const hasEmptyData = (value)=>isJsonObject(value) && Object.keys(value).length === 0;
6
+ const isKnownMark = (value)=>isJsonObject(value) && hasOnlyKeys(value, [
7
+ 'type'
8
+ ]) && typeof value.type === 'string' && knownMarkTypes.has(value.type);
9
+ function isEmptyText(node) {
10
+ return isJsonObject(node) && hasOnlyKeys(node, [
11
+ 'nodeType',
12
+ 'value',
13
+ 'marks',
14
+ 'data'
15
+ ]) && node.nodeType === 'text' && node.value === '' && Array.isArray(node.marks) && node.marks.every(isKnownMark) && hasEmptyData(node.data);
16
+ }
17
+ function isEmptyParagraph(node) {
18
+ return isJsonObject(node) && hasOnlyKeys(node, [
19
+ 'nodeType',
20
+ 'data',
21
+ 'content'
22
+ ]) && node.nodeType === BLOCKS.PARAGRAPH && hasEmptyData(node.data) && Array.isArray(node.content) && node.content.length > 0 && node.content.every(isEmptyText);
23
+ }
24
+ function isEmptyList(node) {
25
+ return isJsonObject(node) && hasOnlyKeys(node, [
26
+ 'nodeType',
27
+ 'data',
28
+ 'content'
29
+ ]) && (node.nodeType === BLOCKS.UL_LIST || node.nodeType === BLOCKS.OL_LIST) && hasEmptyData(node.data) && Array.isArray(node.content) && node.content.every(isEmptyListItem);
30
+ }
31
+ function isEmptyListItem(node) {
32
+ return isJsonObject(node) && hasOnlyKeys(node, [
33
+ 'nodeType',
34
+ 'data',
35
+ 'content'
36
+ ]) && node.nodeType === BLOCKS.LIST_ITEM && hasEmptyData(node.data) && Array.isArray(node.content) && node.content.length > 0 && node.content.every((child)=>isEmptyParagraph(child) || isEmptyList(child));
37
+ }
38
+ export function isEmptyRichTextDocument(value) {
39
+ return isJsonObject(value) && hasOnlyKeys(value, [
40
+ 'nodeType',
41
+ 'data',
42
+ 'content'
43
+ ]) && value.nodeType === BLOCKS.DOCUMENT && hasEmptyData(value.data) && Array.isArray(value.content) && value.content.every((node)=>isEmptyParagraph(node) || isEmptyList(node));
44
+ }
@@ -0,0 +1,273 @@
1
+ import { MARKS } from '@contentful/rich-text-types';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { isEmptyRichTextDocument } from './isEmptyRichTextDocument';
4
+ const emptyText = {
5
+ nodeType: 'text',
6
+ value: '',
7
+ marks: [],
8
+ data: {}
9
+ };
10
+ const emptyParagraph = {
11
+ nodeType: 'paragraph',
12
+ data: {},
13
+ content: [
14
+ emptyText
15
+ ]
16
+ };
17
+ const document = (...content)=>({
18
+ nodeType: 'document',
19
+ data: {},
20
+ content
21
+ });
22
+ describe('isEmptyRichTextDocument', ()=>{
23
+ it('recognizes the canonical empty document', ()=>{
24
+ expect(isEmptyRichTextDocument(document(emptyParagraph))).toBe(true);
25
+ });
26
+ it('recognizes documents with multiple empty paragraphs', ()=>{
27
+ expect(isEmptyRichTextDocument(document(emptyParagraph, emptyParagraph))).toBe(true);
28
+ });
29
+ it.each(Object.values(MARKS))('recognizes empty text with the %s mark', (type)=>{
30
+ expect(isEmptyRichTextDocument(document({
31
+ ...emptyParagraph,
32
+ content: [
33
+ {
34
+ ...emptyText,
35
+ marks: [
36
+ {
37
+ type
38
+ }
39
+ ]
40
+ }
41
+ ]
42
+ }))).toBe(true);
43
+ });
44
+ it('recognizes an empty list with multiple formatting marks', ()=>{
45
+ expect(isEmptyRichTextDocument(document({
46
+ nodeType: 'unordered-list',
47
+ data: {},
48
+ content: [
49
+ {
50
+ nodeType: 'list-item',
51
+ data: {},
52
+ content: [
53
+ {
54
+ ...emptyParagraph,
55
+ content: [
56
+ {
57
+ ...emptyText,
58
+ marks: [
59
+ {
60
+ type: MARKS.BOLD
61
+ },
62
+ {
63
+ type: MARKS.ITALIC
64
+ }
65
+ ]
66
+ }
67
+ ]
68
+ }
69
+ ]
70
+ }
71
+ ]
72
+ }))).toBe(true);
73
+ });
74
+ it('recognizes an empty list followed by the editor trailing paragraph', ()=>{
75
+ const emptyList = {
76
+ nodeType: 'unordered-list',
77
+ data: {},
78
+ content: [
79
+ {
80
+ nodeType: 'list-item',
81
+ data: {},
82
+ content: [
83
+ emptyParagraph
84
+ ]
85
+ }
86
+ ]
87
+ };
88
+ expect(isEmptyRichTextDocument(document(emptyList, emptyParagraph))).toBe(true);
89
+ });
90
+ it('recognizes empty nested ordered and unordered lists', ()=>{
91
+ const nestedList = {
92
+ nodeType: 'ordered-list',
93
+ data: {},
94
+ content: [
95
+ {
96
+ nodeType: 'list-item',
97
+ data: {},
98
+ content: [
99
+ emptyParagraph,
100
+ {
101
+ nodeType: 'unordered-list',
102
+ data: {},
103
+ content: [
104
+ {
105
+ nodeType: 'list-item',
106
+ data: {},
107
+ content: [
108
+ emptyParagraph
109
+ ]
110
+ }
111
+ ]
112
+ }
113
+ ]
114
+ }
115
+ ]
116
+ };
117
+ expect(isEmptyRichTextDocument(document(nestedList))).toBe(true);
118
+ });
119
+ it('recognizes a document with no blocks', ()=>{
120
+ expect(isEmptyRichTextDocument(document())).toBe(true);
121
+ });
122
+ it.each([
123
+ [
124
+ 'whitespace text',
125
+ document({
126
+ ...emptyParagraph,
127
+ content: [
128
+ {
129
+ ...emptyText,
130
+ value: ' '
131
+ }
132
+ ]
133
+ })
134
+ ],
135
+ [
136
+ 'nonempty text',
137
+ document({
138
+ ...emptyParagraph,
139
+ content: [
140
+ {
141
+ ...emptyText,
142
+ value: 'text'
143
+ }
144
+ ]
145
+ })
146
+ ],
147
+ [
148
+ 'an unknown mark',
149
+ document({
150
+ ...emptyParagraph,
151
+ content: [
152
+ {
153
+ ...emptyText,
154
+ marks: [
155
+ {
156
+ type: 'custom-mark'
157
+ }
158
+ ]
159
+ }
160
+ ]
161
+ })
162
+ ],
163
+ [
164
+ 'mark metadata',
165
+ document({
166
+ ...emptyParagraph,
167
+ content: [
168
+ {
169
+ ...emptyText,
170
+ marks: [
171
+ {
172
+ type: MARKS.BOLD,
173
+ custom: true
174
+ }
175
+ ]
176
+ }
177
+ ]
178
+ })
179
+ ],
180
+ [
181
+ 'an embedded entry',
182
+ document({
183
+ nodeType: 'embedded-entry-block',
184
+ data: {},
185
+ content: []
186
+ })
187
+ ],
188
+ [
189
+ 'a horizontal rule',
190
+ document({
191
+ nodeType: 'hr',
192
+ data: {},
193
+ content: []
194
+ })
195
+ ],
196
+ [
197
+ 'a table',
198
+ document({
199
+ nodeType: 'table',
200
+ data: {},
201
+ content: []
202
+ })
203
+ ],
204
+ [
205
+ 'an empty heading',
206
+ document({
207
+ ...emptyParagraph,
208
+ nodeType: 'heading-1'
209
+ })
210
+ ],
211
+ [
212
+ 'blank paragraphs around text',
213
+ document(emptyParagraph, {
214
+ ...emptyParagraph,
215
+ content: [
216
+ {
217
+ ...emptyText,
218
+ value: 'text'
219
+ }
220
+ ]
221
+ }, emptyParagraph)
222
+ ],
223
+ [
224
+ 'an unknown node',
225
+ document({
226
+ nodeType: 'custom-block',
227
+ data: {},
228
+ content: []
229
+ })
230
+ ],
231
+ [
232
+ 'custom node data',
233
+ document({
234
+ ...emptyParagraph,
235
+ data: {
236
+ custom: true
237
+ }
238
+ })
239
+ ],
240
+ [
241
+ 'extra document data',
242
+ {
243
+ ...document(emptyParagraph),
244
+ data: {
245
+ custom: true
246
+ }
247
+ }
248
+ ]
249
+ ])('preserves %s', (_description, value)=>{
250
+ expect(isEmptyRichTextDocument(value)).toBe(false);
251
+ });
252
+ it('preserves invalid or unknown structures', ()=>{
253
+ expect(isEmptyRichTextDocument(null)).toBe(false);
254
+ expect(isEmptyRichTextDocument({
255
+ nodeType: 'document',
256
+ data: {},
257
+ content: [
258
+ null
259
+ ]
260
+ })).toBe(false);
261
+ expect(isEmptyRichTextDocument(document({
262
+ nodeType: 'unordered-list',
263
+ data: {},
264
+ content: [
265
+ {
266
+ nodeType: 'list-item',
267
+ data: {},
268
+ content: []
269
+ }
270
+ ]
271
+ }))).toBe(false);
272
+ });
273
+ });
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns whether a Rich Text document contains only empty paragraphs and lists.
3
+ *
4
+ * Restricting the empty shapes keeps zero-text content such as embeds, tables,
5
+ * and horizontal rules from being removed with the field value.
6
+ */
7
+ export declare function isEmptyRichTextDocument(value: unknown): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.6.14",
3
+ "version": "6.6.15",
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": "9742da65a3db578d2a4051c521ea9c1acd33c256"
99
+ "gitHead": "5b41250cf53d4733a4dbac6970272611c024d6cb"
100
100
  }