@contentful/field-editor-rich-text 6.6.5 → 6.6.6-canary.0

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.
@@ -23,6 +23,7 @@ const _fieldeditorreference = require("@contentful/field-editor-reference");
23
23
  const _fieldeditorshared = require("@contentful/field-editor-shared");
24
24
  const _richtexttypes = require("@contentful/rich-text-types");
25
25
  const _css = require("@emotion/css");
26
+ const _fastdeepequal = /*#__PURE__*/ _interop_require_default(require("fast-deep-equal"));
26
27
  const _editor = require("../../helpers/editor");
27
28
  const _getAllowedResourcesForNodeType = /*#__PURE__*/ _interop_require_default(require("../../helpers/getAllowedResourcesForNodeType"));
28
29
  const _getLinkedContentTypeIdsForNodeType = /*#__PURE__*/ _interop_require_default(require("../../helpers/getLinkedContentTypeIdsForNodeType"));
@@ -299,6 +300,14 @@ async function addOrEditLink(editor, sdk, logAction, targetPath) {
299
300
  linkEntity = node.data.target;
300
301
  }
301
302
  const selectionAfterFocus = targetPath ?? selectionBeforeBlur;
303
+ const blockIndexes = targetPath ? [
304
+ targetPath[0]
305
+ ] : [
306
+ selectionBeforeBlur.anchor.path[0],
307
+ selectionBeforeBlur.focus.path[0]
308
+ ];
309
+ const targetBlocks = ()=>editor.children.slice(Math.min(...blockIndexes), Math.max(...blockIndexes) + 1);
310
+ const blocksBeforeDialog = targetBlocks();
302
311
  const currentLinkText = linkText || (editor.selection ? (0, _queries.getText)(editor, editor.selection) : '');
303
312
  const isEditing = Boolean(node && path);
304
313
  logAction(isEditing ? 'openEditHyperlinkDialog' : 'openCreateHyperlinkDialog');
@@ -319,8 +328,9 @@ async function addOrEditLink(editor, sdk, logAction, targetPath) {
319
328
  readonly: isReadOnly
320
329
  });
321
330
  });
322
- (0, _transforms.select)(editor, selectionAfterFocus);
323
- if (!data) {
331
+ const isTargetUnchanged = (0, _fastdeepequal.default)(targetBlocks(), blocksBeforeDialog);
332
+ if (isTargetUnchanged) (0, _transforms.select)(editor, selectionAfterFocus);
333
+ if (!data || !isTargetUnchanged) {
324
334
  (0, _editor.focus)(editor);
325
335
  logAction(isEditing ? 'cancelEditHyperlinkDialog' : 'cancelCreateHyperlinkDialog');
326
336
  return;
@@ -2,8 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
+ const _fieldeditorshared = require("@contentful/field-editor-shared");
6
+ const _richtexttypes = require("@contentful/rich-text-types");
5
7
  const _vitest = require("vitest");
8
+ const _editor = require("../../../helpers/editor");
9
+ const _transforms = require("../../../internal/transforms");
6
10
  const _testutils = require("../../../test-utils");
11
+ const _createTestEditor = require("../../../test-utils/createTestEditor");
12
+ const _HyperlinkModal = require("../HyperlinkModal");
7
13
  (0, _vitest.describe)('normalization', ()=>{
8
14
  (0, _vitest.it)('removes empty links from the document structure', ()=>{
9
15
  const input = /*#__PURE__*/ (0, _testutils.jsx)("editor", null, /*#__PURE__*/ (0, _testutils.jsx)("hp", null, /*#__PURE__*/ (0, _testutils.jsx)("htext", null, "link"), /*#__PURE__*/ (0, _testutils.jsx)("hlink", {
@@ -26,3 +32,236 @@ const _testutils = require("../../../test-utils");
26
32
  });
27
33
  });
28
34
  });
35
+ _vitest.vi.mock('@contentful/field-editor-shared', async (importOriginal)=>{
36
+ const actual = await importOriginal();
37
+ return {
38
+ ...actual,
39
+ ModalDialogLauncher: {
40
+ ...actual.ModalDialogLauncher,
41
+ openDialog: _vitest.vi.fn()
42
+ }
43
+ };
44
+ });
45
+ _vitest.vi.mock('../../../helpers/editor', async (importOriginal)=>{
46
+ const actual = await importOriginal();
47
+ return {
48
+ ...actual,
49
+ focus: _vitest.vi.fn()
50
+ };
51
+ });
52
+ (0, _vitest.afterEach)(()=>_vitest.vi.clearAllMocks());
53
+ const paragraph = (text)=>({
54
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
55
+ children: [
56
+ {
57
+ text
58
+ }
59
+ ]
60
+ });
61
+ const link = {
62
+ type: _richtexttypes.INLINES.HYPERLINK,
63
+ data: {
64
+ uri: 'https://old.example'
65
+ },
66
+ children: [
67
+ {
68
+ text: 'link'
69
+ }
70
+ ]
71
+ };
72
+ const response = {
73
+ linkType: _richtexttypes.INLINES.HYPERLINK,
74
+ linkText: 'link',
75
+ linkTarget: 'https://new.example'
76
+ };
77
+ const sdk = {
78
+ field: {
79
+ validation: []
80
+ }
81
+ };
82
+ function openDialog(editor, targetPath) {
83
+ let close;
84
+ _vitest.vi.mocked(_fieldeditorshared.ModalDialogLauncher.openDialog).mockReturnValue(new Promise((resolve)=>{
85
+ close = resolve;
86
+ }));
87
+ const pending = (0, _HyperlinkModal.addOrEditLink)(editor, sdk, _vitest.vi.fn(), targetPath);
88
+ (0, _vitest.expect)(_fieldeditorshared.ModalDialogLauncher.openDialog).toHaveBeenCalledOnce();
89
+ return {
90
+ pending,
91
+ close
92
+ };
93
+ }
94
+ (0, _vitest.describe)('document changes while a hyperlink dialog is open', ()=>{
95
+ _vitest.it.each([
96
+ 'submit',
97
+ 'cancel'
98
+ ])('keeps the replacement document and a valid caret on %s', async (action)=>{
99
+ const { editor } = (0, _createTestEditor.createTestEditor)({});
100
+ editor.children = [
101
+ paragraph('first'),
102
+ paragraph('second'),
103
+ paragraph('link')
104
+ ];
105
+ editor.tf.select({
106
+ anchor: {
107
+ path: [
108
+ 2,
109
+ 0
110
+ ],
111
+ offset: 0
112
+ },
113
+ focus: {
114
+ path: [
115
+ 2,
116
+ 0
117
+ ],
118
+ offset: 4
119
+ }
120
+ });
121
+ const { pending, close } = openDialog(editor);
122
+ (0, _transforms.setEditorValue)(editor, [
123
+ paragraph('replacement')
124
+ ]);
125
+ const selection = structuredClone(editor.selection);
126
+ close(action === 'submit' ? response : null);
127
+ await (0, _vitest.expect)(pending).resolves.toBeUndefined();
128
+ (0, _vitest.expect)(editor.children).toEqual([
129
+ paragraph('replacement')
130
+ ]);
131
+ (0, _vitest.expect)(editor.selection).toEqual(selection);
132
+ (0, _vitest.expect)(_editor.focus).toHaveBeenCalledOnce();
133
+ });
134
+ (0, _vitest.it)('links the selected text when an external update changes another paragraph', async ()=>{
135
+ const { editor } = (0, _createTestEditor.createTestEditor)({});
136
+ editor.children = [
137
+ paragraph('first'),
138
+ paragraph('link')
139
+ ];
140
+ editor.tf.select({
141
+ anchor: {
142
+ path: [
143
+ 1,
144
+ 0
145
+ ],
146
+ offset: 0
147
+ },
148
+ focus: {
149
+ path: [
150
+ 1,
151
+ 0
152
+ ],
153
+ offset: 4
154
+ }
155
+ });
156
+ const { pending, close } = openDialog(editor);
157
+ (0, _transforms.setEditorValue)(editor, [
158
+ paragraph('first!'),
159
+ paragraph('link')
160
+ ]);
161
+ close(response);
162
+ await pending;
163
+ (0, _vitest.expect)(editor.children[0]).toEqual(paragraph('first!'));
164
+ (0, _vitest.expect)(editor.children[1].children).toContainEqual({
165
+ ...link,
166
+ data: {
167
+ uri: response.linkTarget
168
+ }
169
+ });
170
+ });
171
+ (0, _vitest.it)('edits the target link when an external update changes another paragraph', async ()=>{
172
+ const { editor } = (0, _createTestEditor.createTestEditor)({});
173
+ const linkParagraph = {
174
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
175
+ children: [
176
+ {
177
+ text: ''
178
+ },
179
+ link,
180
+ {
181
+ text: ''
182
+ }
183
+ ]
184
+ };
185
+ editor.children = [
186
+ paragraph('first'),
187
+ linkParagraph
188
+ ];
189
+ editor.tf.select({
190
+ path: [
191
+ 1,
192
+ 1,
193
+ 0
194
+ ],
195
+ offset: 2
196
+ });
197
+ const { pending, close } = openDialog(editor, [
198
+ 1,
199
+ 1
200
+ ]);
201
+ (0, _transforms.setEditorValue)(editor, [
202
+ paragraph('first!'),
203
+ linkParagraph
204
+ ]);
205
+ close(response);
206
+ await pending;
207
+ (0, _vitest.expect)(editor.children[0]).toEqual(paragraph('first!'));
208
+ (0, _vitest.expect)(editor.children[1].children[1]).toEqual({
209
+ ...link,
210
+ data: {
211
+ uri: response.linkTarget
212
+ }
213
+ });
214
+ });
215
+ (0, _vitest.it)('does not edit a different link that replaces the original target', async ()=>{
216
+ const { editor } = (0, _createTestEditor.createTestEditor)({});
217
+ editor.children = [
218
+ {
219
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
220
+ children: [
221
+ {
222
+ text: ''
223
+ },
224
+ link,
225
+ {
226
+ text: ''
227
+ }
228
+ ]
229
+ }
230
+ ];
231
+ editor.tf.select({
232
+ path: [
233
+ 0,
234
+ 1,
235
+ 0
236
+ ],
237
+ offset: 2
238
+ });
239
+ const { pending, close } = openDialog(editor, [
240
+ 0,
241
+ 1
242
+ ]);
243
+ const replacement = [
244
+ {
245
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
246
+ children: [
247
+ {
248
+ text: ''
249
+ },
250
+ {
251
+ ...link,
252
+ data: {
253
+ uri: 'https://replacement.example'
254
+ }
255
+ },
256
+ {
257
+ text: ''
258
+ }
259
+ ]
260
+ }
261
+ ];
262
+ (0, _transforms.setEditorValue)(editor, replacement);
263
+ close(response);
264
+ await pending;
265
+ (0, _vitest.expect)(editor.children).toEqual(replacement);
266
+ });
267
+ });
@@ -5,6 +5,7 @@ import { EntityProvider } from '@contentful/field-editor-reference';
5
5
  import { ModalDialogLauncher } from '@contentful/field-editor-shared';
6
6
  import { INLINES } from '@contentful/rich-text-types';
7
7
  import { css } from '@emotion/css';
8
+ import equal from 'fast-deep-equal';
8
9
  import { focus, getNodeEntryFromSelection, insertLink, LINK_TYPES } from '../../helpers/editor';
9
10
  import getAllowedResourcesForNodeType from '../../helpers/getAllowedResourcesForNodeType';
10
11
  import getLinkedContentTypeIdsForNodeType from '../../helpers/getLinkedContentTypeIdsForNodeType';
@@ -235,6 +236,14 @@ export async function addOrEditLink(editor, sdk, logAction, targetPath) {
235
236
  linkEntity = node.data.target;
236
237
  }
237
238
  const selectionAfterFocus = targetPath ?? selectionBeforeBlur;
239
+ const blockIndexes = targetPath ? [
240
+ targetPath[0]
241
+ ] : [
242
+ selectionBeforeBlur.anchor.path[0],
243
+ selectionBeforeBlur.focus.path[0]
244
+ ];
245
+ const targetBlocks = ()=>editor.children.slice(Math.min(...blockIndexes), Math.max(...blockIndexes) + 1);
246
+ const blocksBeforeDialog = targetBlocks();
238
247
  const currentLinkText = linkText || (editor.selection ? getText(editor, editor.selection) : '');
239
248
  const isEditing = Boolean(node && path);
240
249
  logAction(isEditing ? 'openEditHyperlinkDialog' : 'openCreateHyperlinkDialog');
@@ -255,8 +264,9 @@ export async function addOrEditLink(editor, sdk, logAction, targetPath) {
255
264
  readonly: isReadOnly
256
265
  });
257
266
  });
258
- select(editor, selectionAfterFocus);
259
- if (!data) {
267
+ const isTargetUnchanged = equal(targetBlocks(), blocksBeforeDialog);
268
+ if (isTargetUnchanged) select(editor, selectionAfterFocus);
269
+ if (!data || !isTargetUnchanged) {
260
270
  focus(editor);
261
271
  logAction(isEditing ? 'cancelEditHyperlinkDialog' : 'cancelCreateHyperlinkDialog');
262
272
  return;
@@ -1,5 +1,11 @@
1
- import { describe, it } from 'vitest';
1
+ import { ModalDialogLauncher } from '@contentful/field-editor-shared';
2
+ import { BLOCKS, INLINES } from '@contentful/rich-text-types';
3
+ import { afterEach, describe, expect, it, vi } from 'vitest';
4
+ import { focus } from '../../../helpers/editor';
5
+ import { setEditorValue } from '../../../internal/transforms';
2
6
  import { assertOutput, jsx } from '../../../test-utils';
7
+ import { createTestEditor } from '../../../test-utils/createTestEditor';
8
+ import { addOrEditLink } from '../HyperlinkModal';
3
9
  describe('normalization', ()=>{
4
10
  it('removes empty links from the document structure', ()=>{
5
11
  const input = /*#__PURE__*/ jsx("editor", null, /*#__PURE__*/ jsx("hp", null, /*#__PURE__*/ jsx("htext", null, "link"), /*#__PURE__*/ jsx("hlink", {
@@ -22,3 +28,236 @@ describe('normalization', ()=>{
22
28
  });
23
29
  });
24
30
  });
31
+ vi.mock('@contentful/field-editor-shared', async (importOriginal)=>{
32
+ const actual = await importOriginal();
33
+ return {
34
+ ...actual,
35
+ ModalDialogLauncher: {
36
+ ...actual.ModalDialogLauncher,
37
+ openDialog: vi.fn()
38
+ }
39
+ };
40
+ });
41
+ vi.mock('../../../helpers/editor', async (importOriginal)=>{
42
+ const actual = await importOriginal();
43
+ return {
44
+ ...actual,
45
+ focus: vi.fn()
46
+ };
47
+ });
48
+ afterEach(()=>vi.clearAllMocks());
49
+ const paragraph = (text)=>({
50
+ type: BLOCKS.PARAGRAPH,
51
+ children: [
52
+ {
53
+ text
54
+ }
55
+ ]
56
+ });
57
+ const link = {
58
+ type: INLINES.HYPERLINK,
59
+ data: {
60
+ uri: 'https://old.example'
61
+ },
62
+ children: [
63
+ {
64
+ text: 'link'
65
+ }
66
+ ]
67
+ };
68
+ const response = {
69
+ linkType: INLINES.HYPERLINK,
70
+ linkText: 'link',
71
+ linkTarget: 'https://new.example'
72
+ };
73
+ const sdk = {
74
+ field: {
75
+ validation: []
76
+ }
77
+ };
78
+ function openDialog(editor, targetPath) {
79
+ let close;
80
+ vi.mocked(ModalDialogLauncher.openDialog).mockReturnValue(new Promise((resolve)=>{
81
+ close = resolve;
82
+ }));
83
+ const pending = addOrEditLink(editor, sdk, vi.fn(), targetPath);
84
+ expect(ModalDialogLauncher.openDialog).toHaveBeenCalledOnce();
85
+ return {
86
+ pending,
87
+ close
88
+ };
89
+ }
90
+ describe('document changes while a hyperlink dialog is open', ()=>{
91
+ it.each([
92
+ 'submit',
93
+ 'cancel'
94
+ ])('keeps the replacement document and a valid caret on %s', async (action)=>{
95
+ const { editor } = createTestEditor({});
96
+ editor.children = [
97
+ paragraph('first'),
98
+ paragraph('second'),
99
+ paragraph('link')
100
+ ];
101
+ editor.tf.select({
102
+ anchor: {
103
+ path: [
104
+ 2,
105
+ 0
106
+ ],
107
+ offset: 0
108
+ },
109
+ focus: {
110
+ path: [
111
+ 2,
112
+ 0
113
+ ],
114
+ offset: 4
115
+ }
116
+ });
117
+ const { pending, close } = openDialog(editor);
118
+ setEditorValue(editor, [
119
+ paragraph('replacement')
120
+ ]);
121
+ const selection = structuredClone(editor.selection);
122
+ close(action === 'submit' ? response : null);
123
+ await expect(pending).resolves.toBeUndefined();
124
+ expect(editor.children).toEqual([
125
+ paragraph('replacement')
126
+ ]);
127
+ expect(editor.selection).toEqual(selection);
128
+ expect(focus).toHaveBeenCalledOnce();
129
+ });
130
+ it('links the selected text when an external update changes another paragraph', async ()=>{
131
+ const { editor } = createTestEditor({});
132
+ editor.children = [
133
+ paragraph('first'),
134
+ paragraph('link')
135
+ ];
136
+ editor.tf.select({
137
+ anchor: {
138
+ path: [
139
+ 1,
140
+ 0
141
+ ],
142
+ offset: 0
143
+ },
144
+ focus: {
145
+ path: [
146
+ 1,
147
+ 0
148
+ ],
149
+ offset: 4
150
+ }
151
+ });
152
+ const { pending, close } = openDialog(editor);
153
+ setEditorValue(editor, [
154
+ paragraph('first!'),
155
+ paragraph('link')
156
+ ]);
157
+ close(response);
158
+ await pending;
159
+ expect(editor.children[0]).toEqual(paragraph('first!'));
160
+ expect(editor.children[1].children).toContainEqual({
161
+ ...link,
162
+ data: {
163
+ uri: response.linkTarget
164
+ }
165
+ });
166
+ });
167
+ it('edits the target link when an external update changes another paragraph', async ()=>{
168
+ const { editor } = createTestEditor({});
169
+ const linkParagraph = {
170
+ type: BLOCKS.PARAGRAPH,
171
+ children: [
172
+ {
173
+ text: ''
174
+ },
175
+ link,
176
+ {
177
+ text: ''
178
+ }
179
+ ]
180
+ };
181
+ editor.children = [
182
+ paragraph('first'),
183
+ linkParagraph
184
+ ];
185
+ editor.tf.select({
186
+ path: [
187
+ 1,
188
+ 1,
189
+ 0
190
+ ],
191
+ offset: 2
192
+ });
193
+ const { pending, close } = openDialog(editor, [
194
+ 1,
195
+ 1
196
+ ]);
197
+ setEditorValue(editor, [
198
+ paragraph('first!'),
199
+ linkParagraph
200
+ ]);
201
+ close(response);
202
+ await pending;
203
+ expect(editor.children[0]).toEqual(paragraph('first!'));
204
+ expect(editor.children[1].children[1]).toEqual({
205
+ ...link,
206
+ data: {
207
+ uri: response.linkTarget
208
+ }
209
+ });
210
+ });
211
+ it('does not edit a different link that replaces the original target', async ()=>{
212
+ const { editor } = createTestEditor({});
213
+ editor.children = [
214
+ {
215
+ type: BLOCKS.PARAGRAPH,
216
+ children: [
217
+ {
218
+ text: ''
219
+ },
220
+ link,
221
+ {
222
+ text: ''
223
+ }
224
+ ]
225
+ }
226
+ ];
227
+ editor.tf.select({
228
+ path: [
229
+ 0,
230
+ 1,
231
+ 0
232
+ ],
233
+ offset: 2
234
+ });
235
+ const { pending, close } = openDialog(editor, [
236
+ 0,
237
+ 1
238
+ ]);
239
+ const replacement = [
240
+ {
241
+ type: BLOCKS.PARAGRAPH,
242
+ children: [
243
+ {
244
+ text: ''
245
+ },
246
+ {
247
+ ...link,
248
+ data: {
249
+ uri: 'https://replacement.example'
250
+ }
251
+ },
252
+ {
253
+ text: ''
254
+ }
255
+ ]
256
+ }
257
+ ];
258
+ setEditorValue(editor, replacement);
259
+ close(response);
260
+ await pending;
261
+ expect(editor.children).toEqual(replacement);
262
+ });
263
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.6.5",
3
+ "version": "6.6.6-canary.0+adac18d9",
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": "de438e4f6ab157aab21f50a558d6eeb2babec69c"
99
+ "gitHead": "adac18d9a7a4e0204aa737896f6b8916ecca0361"
100
100
  }