@contentful/field-editor-rich-text 6.6.1-canary.2 → 6.6.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.
@@ -224,8 +224,9 @@ function wrapLink(editor, { text, url, target, type, path }) {
224
224
  (0, _transforms.wrapNodes)(editor, link, {
225
225
  split: true
226
226
  });
227
- (0, _transforms.deleteText)(editor);
228
- (0, _transforms.insertText)(editor, text);
227
+ (0, _transforms.insertText)(editor, text, {
228
+ at: editor.selection ?? undefined
229
+ });
229
230
  (0, _transforms.collapseSelection)(editor, {
230
231
  edge: 'end'
231
232
  });
@@ -61,3 +61,223 @@ const _createTestEditor = require("../../test-utils/createTestEditor");
61
61
  }
62
62
  ]);
63
63
  });
64
+ (0, _vitest.it)('preserves formatting and can undo and redo converting selected text', ()=>{
65
+ const { editor } = (0, _createTestEditor.createTestEditor)({});
66
+ const original = [
67
+ {
68
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
69
+ children: [
70
+ {
71
+ text: 'Before bold After',
72
+ bold: true
73
+ }
74
+ ]
75
+ }
76
+ ];
77
+ editor.children = original;
78
+ editor.tf.select({
79
+ anchor: {
80
+ path: [
81
+ 0,
82
+ 0
83
+ ],
84
+ offset: 7
85
+ },
86
+ focus: {
87
+ path: [
88
+ 0,
89
+ 0
90
+ ],
91
+ offset: 11
92
+ }
93
+ });
94
+ editor.tf.withoutNormalizing(()=>{
95
+ (0, _editor.wrapLink)(editor, {
96
+ type: _richtexttypes.INLINES.HYPERLINK,
97
+ url: 'https://example.com',
98
+ text: 'bold'
99
+ });
100
+ });
101
+ const linked = [
102
+ {
103
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
104
+ children: [
105
+ {
106
+ text: 'Before ',
107
+ bold: true
108
+ },
109
+ {
110
+ type: _richtexttypes.INLINES.HYPERLINK,
111
+ data: {
112
+ uri: 'https://example.com'
113
+ },
114
+ children: [
115
+ {
116
+ text: 'bold',
117
+ bold: true
118
+ }
119
+ ]
120
+ },
121
+ {
122
+ text: ' After',
123
+ bold: true
124
+ }
125
+ ]
126
+ }
127
+ ];
128
+ (0, _vitest.expect)(editor.children).toEqual(linked);
129
+ editor.tf.undo();
130
+ (0, _vitest.expect)(editor.children).toEqual(original);
131
+ editor.tf.redo();
132
+ (0, _vitest.expect)(editor.children).toEqual(linked);
133
+ });
134
+ const linkOptions = [
135
+ {
136
+ type: _richtexttypes.INLINES.HYPERLINK,
137
+ url: 'https://example.com'
138
+ },
139
+ {
140
+ type: _richtexttypes.INLINES.ENTRY_HYPERLINK,
141
+ target: {
142
+ sys: {
143
+ id: 'entry-id',
144
+ type: 'Link',
145
+ linkType: 'Entry'
146
+ }
147
+ }
148
+ },
149
+ {
150
+ type: _richtexttypes.INLINES.ASSET_HYPERLINK,
151
+ target: {
152
+ sys: {
153
+ id: 'asset-id',
154
+ type: 'Link',
155
+ linkType: 'Asset'
156
+ }
157
+ }
158
+ }
159
+ ];
160
+ _vitest.describe.each(linkOptions)('$type', (options)=>{
161
+ _vitest.it.each([
162
+ {
163
+ name: 'a forward selection',
164
+ backward: false,
165
+ text: 'My cool website'
166
+ },
167
+ {
168
+ name: 'a backward selection',
169
+ backward: true,
170
+ text: 'My cool website'
171
+ },
172
+ {
173
+ name: 'a changed link label',
174
+ backward: false,
175
+ text: 'New label'
176
+ }
177
+ ])('keeps $name linked without changing surrounding content', ({ backward, text })=>{
178
+ const { editor } = (0, _createTestEditor.createTestEditor)({});
179
+ const existingLink = {
180
+ type: _richtexttypes.INLINES.HYPERLINK,
181
+ data: {
182
+ uri: 'https://existing.example'
183
+ },
184
+ children: [
185
+ {
186
+ text: 'Existing link',
187
+ data: {}
188
+ }
189
+ ],
190
+ isVoid: false
191
+ };
192
+ editor.children = [
193
+ {
194
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
195
+ children: [
196
+ {
197
+ text: 'Before My cool website After',
198
+ data: {}
199
+ },
200
+ existingLink,
201
+ {
202
+ text: '',
203
+ data: {}
204
+ }
205
+ ],
206
+ data: {},
207
+ isVoid: false
208
+ }
209
+ ];
210
+ const start = {
211
+ path: [
212
+ 0,
213
+ 0
214
+ ],
215
+ offset: 7
216
+ };
217
+ const end = {
218
+ path: [
219
+ 0,
220
+ 0
221
+ ],
222
+ offset: 22
223
+ };
224
+ editor.tf.select(backward ? {
225
+ anchor: end,
226
+ focus: start
227
+ } : {
228
+ anchor: start,
229
+ focus: end
230
+ });
231
+ editor.tf.withoutNormalizing(()=>(0, _editor.wrapLink)(editor, {
232
+ ...options,
233
+ text
234
+ }));
235
+ (0, _vitest.expect)(editor.children[0].children).toEqual([
236
+ {
237
+ text: 'Before ',
238
+ data: {}
239
+ },
240
+ {
241
+ type: options.type,
242
+ data: options.url ? {
243
+ uri: options.url
244
+ } : {
245
+ target: options.target
246
+ },
247
+ children: [
248
+ {
249
+ text,
250
+ data: {}
251
+ }
252
+ ]
253
+ },
254
+ {
255
+ text: ' After',
256
+ data: {}
257
+ },
258
+ existingLink,
259
+ {
260
+ text: '',
261
+ data: {}
262
+ }
263
+ ]);
264
+ (0, _vitest.expect)(editor.selection).toEqual({
265
+ anchor: {
266
+ path: [
267
+ 0,
268
+ 1,
269
+ 0
270
+ ],
271
+ offset: text.length
272
+ },
273
+ focus: {
274
+ path: [
275
+ 0,
276
+ 1,
277
+ 0
278
+ ],
279
+ offset: text.length
280
+ }
281
+ });
282
+ });
283
+ });
@@ -116,7 +116,7 @@ function FetchingWrappedAssetCard(props) {
116
116
  isLoading: true
117
117
  });
118
118
  }
119
- if (status === 'error' || !asset?.sys?.id) {
119
+ if (status === 'error') {
120
120
  return /*#__PURE__*/ _react.createElement(_fieldeditorreference.MissingEntityCard, {
121
121
  isDisabled: props.isDisabled,
122
122
  onRemove: props.onRemove,
@@ -106,7 +106,7 @@ const InternalFetchingWrappedEntryCard = (props)=>{
106
106
  release: props.sdk.release,
107
107
  isReference: true
108
108
  });
109
- const contentTypeId = entry?.sys?.contentType?.sys?.id;
109
+ const contentTypeId = entry?.sys.contentType.sys.id;
110
110
  const { data: contentType } = (0, _reactquery.useContentType)(props.sdk, contentTypeId || '', {
111
111
  enabled: !!contentTypeId
112
112
  });
@@ -123,7 +123,7 @@ const InternalFetchingWrappedEntryCard = (props)=>{
123
123
  isLoading: true
124
124
  });
125
125
  }
126
- if (status === 'error' || !entry?.sys?.id) {
126
+ if (status === 'error') {
127
127
  return /*#__PURE__*/ _react.createElement(_fieldeditorreference.MissingEntityCard, {
128
128
  isDisabled: props.isDisabled,
129
129
  onRemove: props.onRemove,
@@ -1,7 +1,7 @@
1
1
  import { BLOCKS, INLINES } from '@contentful/rich-text-types';
2
2
  import { focusEditor } from '../internal/misc';
3
3
  import { getText, getNodeEntries, getNodeEntry, getNodeDescendants, isText, isRangeCollapsed as isSelectionCollapsed, isEditor, isElement, getPathLevels, isCommonPath } from '../internal/queries';
4
- import { setNodes, select, insertNodes, toggleNodeType, moveSelection, unwrapNodes, insertText, wrapNodes, deleteText, collapseSelection } from '../internal/transforms';
4
+ import { setNodes, select, insertNodes, toggleNodeType, moveSelection, unwrapNodes, insertText, wrapNodes, collapseSelection } from '../internal/transforms';
5
5
  import { IS_SAFARI } from './environment';
6
6
  export const LINK_TYPES = [
7
7
  INLINES.HYPERLINK,
@@ -146,8 +146,9 @@ export function wrapLink(editor, { text, url, target, type, path }) {
146
146
  wrapNodes(editor, link, {
147
147
  split: true
148
148
  });
149
- deleteText(editor);
150
- insertText(editor, text);
149
+ insertText(editor, text, {
150
+ at: editor.selection ?? undefined
151
+ });
151
152
  collapseSelection(editor, {
152
153
  edge: 'end'
153
154
  });
@@ -1,5 +1,5 @@
1
1
  import { BLOCKS, INLINES } from '@contentful/rich-text-types';
2
- import { expect, it } from 'vitest';
2
+ import { describe, expect, it } from 'vitest';
3
3
  import { wrapLink } from '../../helpers/editor';
4
4
  import { createTestEditor } from '../../test-utils/createTestEditor';
5
5
  it('converts selected text into a hyperlink', ()=>{
@@ -57,3 +57,223 @@ it('converts selected text into a hyperlink', ()=>{
57
57
  }
58
58
  ]);
59
59
  });
60
+ it('preserves formatting and can undo and redo converting selected text', ()=>{
61
+ const { editor } = createTestEditor({});
62
+ const original = [
63
+ {
64
+ type: BLOCKS.PARAGRAPH,
65
+ children: [
66
+ {
67
+ text: 'Before bold After',
68
+ bold: true
69
+ }
70
+ ]
71
+ }
72
+ ];
73
+ editor.children = original;
74
+ editor.tf.select({
75
+ anchor: {
76
+ path: [
77
+ 0,
78
+ 0
79
+ ],
80
+ offset: 7
81
+ },
82
+ focus: {
83
+ path: [
84
+ 0,
85
+ 0
86
+ ],
87
+ offset: 11
88
+ }
89
+ });
90
+ editor.tf.withoutNormalizing(()=>{
91
+ wrapLink(editor, {
92
+ type: INLINES.HYPERLINK,
93
+ url: 'https://example.com',
94
+ text: 'bold'
95
+ });
96
+ });
97
+ const linked = [
98
+ {
99
+ type: BLOCKS.PARAGRAPH,
100
+ children: [
101
+ {
102
+ text: 'Before ',
103
+ bold: true
104
+ },
105
+ {
106
+ type: INLINES.HYPERLINK,
107
+ data: {
108
+ uri: 'https://example.com'
109
+ },
110
+ children: [
111
+ {
112
+ text: 'bold',
113
+ bold: true
114
+ }
115
+ ]
116
+ },
117
+ {
118
+ text: ' After',
119
+ bold: true
120
+ }
121
+ ]
122
+ }
123
+ ];
124
+ expect(editor.children).toEqual(linked);
125
+ editor.tf.undo();
126
+ expect(editor.children).toEqual(original);
127
+ editor.tf.redo();
128
+ expect(editor.children).toEqual(linked);
129
+ });
130
+ const linkOptions = [
131
+ {
132
+ type: INLINES.HYPERLINK,
133
+ url: 'https://example.com'
134
+ },
135
+ {
136
+ type: INLINES.ENTRY_HYPERLINK,
137
+ target: {
138
+ sys: {
139
+ id: 'entry-id',
140
+ type: 'Link',
141
+ linkType: 'Entry'
142
+ }
143
+ }
144
+ },
145
+ {
146
+ type: INLINES.ASSET_HYPERLINK,
147
+ target: {
148
+ sys: {
149
+ id: 'asset-id',
150
+ type: 'Link',
151
+ linkType: 'Asset'
152
+ }
153
+ }
154
+ }
155
+ ];
156
+ describe.each(linkOptions)('$type', (options)=>{
157
+ it.each([
158
+ {
159
+ name: 'a forward selection',
160
+ backward: false,
161
+ text: 'My cool website'
162
+ },
163
+ {
164
+ name: 'a backward selection',
165
+ backward: true,
166
+ text: 'My cool website'
167
+ },
168
+ {
169
+ name: 'a changed link label',
170
+ backward: false,
171
+ text: 'New label'
172
+ }
173
+ ])('keeps $name linked without changing surrounding content', ({ backward, text })=>{
174
+ const { editor } = createTestEditor({});
175
+ const existingLink = {
176
+ type: INLINES.HYPERLINK,
177
+ data: {
178
+ uri: 'https://existing.example'
179
+ },
180
+ children: [
181
+ {
182
+ text: 'Existing link',
183
+ data: {}
184
+ }
185
+ ],
186
+ isVoid: false
187
+ };
188
+ editor.children = [
189
+ {
190
+ type: BLOCKS.PARAGRAPH,
191
+ children: [
192
+ {
193
+ text: 'Before My cool website After',
194
+ data: {}
195
+ },
196
+ existingLink,
197
+ {
198
+ text: '',
199
+ data: {}
200
+ }
201
+ ],
202
+ data: {},
203
+ isVoid: false
204
+ }
205
+ ];
206
+ const start = {
207
+ path: [
208
+ 0,
209
+ 0
210
+ ],
211
+ offset: 7
212
+ };
213
+ const end = {
214
+ path: [
215
+ 0,
216
+ 0
217
+ ],
218
+ offset: 22
219
+ };
220
+ editor.tf.select(backward ? {
221
+ anchor: end,
222
+ focus: start
223
+ } : {
224
+ anchor: start,
225
+ focus: end
226
+ });
227
+ editor.tf.withoutNormalizing(()=>wrapLink(editor, {
228
+ ...options,
229
+ text
230
+ }));
231
+ expect(editor.children[0].children).toEqual([
232
+ {
233
+ text: 'Before ',
234
+ data: {}
235
+ },
236
+ {
237
+ type: options.type,
238
+ data: options.url ? {
239
+ uri: options.url
240
+ } : {
241
+ target: options.target
242
+ },
243
+ children: [
244
+ {
245
+ text,
246
+ data: {}
247
+ }
248
+ ]
249
+ },
250
+ {
251
+ text: ' After',
252
+ data: {}
253
+ },
254
+ existingLink,
255
+ {
256
+ text: '',
257
+ data: {}
258
+ }
259
+ ]);
260
+ expect(editor.selection).toEqual({
261
+ anchor: {
262
+ path: [
263
+ 0,
264
+ 1,
265
+ 0
266
+ ],
267
+ offset: text.length
268
+ },
269
+ focus: {
270
+ path: [
271
+ 0,
272
+ 1,
273
+ 0
274
+ ],
275
+ offset: text.length
276
+ }
277
+ });
278
+ });
279
+ });
@@ -60,7 +60,7 @@ export function FetchingWrappedAssetCard(props) {
60
60
  isLoading: true
61
61
  });
62
62
  }
63
- if (status === 'error' || !asset?.sys?.id) {
63
+ if (status === 'error') {
64
64
  return /*#__PURE__*/ React.createElement(MissingEntityCard, {
65
65
  isDisabled: props.isDisabled,
66
66
  onRemove: props.onRemove,
@@ -50,7 +50,7 @@ const InternalFetchingWrappedEntryCard = (props)=>{
50
50
  release: props.sdk.release,
51
51
  isReference: true
52
52
  });
53
- const contentTypeId = entry?.sys?.contentType?.sys?.id;
53
+ const contentTypeId = entry?.sys.contentType.sys.id;
54
54
  const { data: contentType } = useContentType(props.sdk, contentTypeId || '', {
55
55
  enabled: !!contentTypeId
56
56
  });
@@ -67,7 +67,7 @@ const InternalFetchingWrappedEntryCard = (props)=>{
67
67
  isLoading: true
68
68
  });
69
69
  }
70
- if (status === 'error' || !entry?.sys?.id) {
70
+ if (status === 'error') {
71
71
  return /*#__PURE__*/ React.createElement(MissingEntityCard, {
72
72
  isDisabled: props.isDisabled,
73
73
  onRemove: props.onRemove,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.6.1-canary.2+0e7f6072",
3
+ "version": "6.6.1",
4
4
  "source": "./src/index.tsx",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -46,7 +46,7 @@
46
46
  "@contentful/f36-icons": "^6.7.1",
47
47
  "@contentful/f36-tokens": "^6.1.2",
48
48
  "@contentful/f36-utils": "^6.1.0",
49
- "@contentful/field-editor-reference": "^8.5.1-canary.4+0e7f6072",
49
+ "@contentful/field-editor-reference": "^8.5.0",
50
50
  "@contentful/field-editor-shared": "^4.6.0",
51
51
  "@contentful/rich-text-plain-text-renderer": "^17.0.0",
52
52
  "@contentful/rich-text-types": "^17.2.5",
@@ -96,5 +96,5 @@
96
96
  "prettier --write"
97
97
  ]
98
98
  },
99
- "gitHead": "0e7f6072b7a2e52a2d22f0fef0d2f9ae86347e21"
99
+ "gitHead": "de96ffd473521caf9b0a67c0680e3fdd5d44b808"
100
100
  }