@contentful/field-editor-rich-text 6.6.4 → 6.6.5-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.
- package/dist/cjs/plugins/Hyperlink/HyperlinkModal.js +46 -40
- package/dist/cjs/plugins/Hyperlink/__tests__/HyperlinkModal.test.js +286 -0
- package/dist/esm/plugins/Hyperlink/HyperlinkModal.js +46 -40
- package/dist/esm/plugins/Hyperlink/__tests__/HyperlinkModal.test.js +282 -0
- package/dist/types/plugins/Hyperlink/__tests__/HyperlinkModal.test.d.ts +1 -0
- package/package.json +2 -2
|
@@ -283,10 +283,7 @@ function HyperlinkModal(props) {
|
|
|
283
283
|
}
|
|
284
284
|
async function addOrEditLink(editor, sdk, logAction, targetPath) {
|
|
285
285
|
const isReadOnly = (0, _queries.isEditorReadOnly)(editor);
|
|
286
|
-
|
|
287
|
-
...editor.selection
|
|
288
|
-
} : undefined;
|
|
289
|
-
if (!targetPath && !selectionBeforeBlur) return;
|
|
286
|
+
if (!targetPath && !editor.selection) return;
|
|
290
287
|
let linkType;
|
|
291
288
|
let linkText;
|
|
292
289
|
let linkTarget;
|
|
@@ -298,46 +295,55 @@ async function addOrEditLink(editor, sdk, logAction, targetPath) {
|
|
|
298
295
|
linkTarget = node.data.uri || '';
|
|
299
296
|
linkEntity = node.data.target;
|
|
300
297
|
}
|
|
301
|
-
const selectionAfterFocus = targetPath ?? selectionBeforeBlur;
|
|
302
298
|
const currentLinkText = linkText || (editor.selection ? (0, _queries.getText)(editor, editor.selection) : '');
|
|
303
299
|
const isEditing = Boolean(node && path);
|
|
304
300
|
logAction(isEditing ? 'openEditHyperlinkDialog' : 'openCreateHyperlinkDialog');
|
|
305
|
-
const
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
301
|
+
const selectionRef = editor.selection ? editor.api.rangeRef(editor.selection) : undefined;
|
|
302
|
+
const linkRef = path ? editor.api.pathRef(path) : undefined;
|
|
303
|
+
try {
|
|
304
|
+
const data = await _fieldeditorshared.ModalDialogLauncher.openDialog({
|
|
305
|
+
title: isEditing ? 'Edit hyperlink' : 'Insert hyperlink',
|
|
306
|
+
width: 'large',
|
|
307
|
+
shouldCloseOnEscapePress: true,
|
|
308
|
+
shouldCloseOnOverlayClick: true,
|
|
309
|
+
allowHeightOverflow: true
|
|
310
|
+
}, ({ onClose })=>{
|
|
311
|
+
return /*#__PURE__*/ _react.createElement(HyperlinkModal, {
|
|
312
|
+
linkTarget: linkTarget,
|
|
313
|
+
linkText: currentLinkText,
|
|
314
|
+
linkType: linkType,
|
|
315
|
+
linkEntity: linkEntity,
|
|
316
|
+
onClose: onClose,
|
|
317
|
+
sdk: sdk,
|
|
318
|
+
readonly: isReadOnly
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
const currentPath = linkRef?.current;
|
|
322
|
+
const selectionAfterFocus = targetPath ? currentPath && editor.api.range(currentPath) : selectionRef?.current;
|
|
323
|
+
if (!selectionAfterFocus || isEditing && !currentPath) return;
|
|
324
|
+
(0, _transforms.select)(editor, selectionAfterFocus);
|
|
325
|
+
if (!data) {
|
|
326
|
+
(0, _editor.focus)(editor);
|
|
327
|
+
logAction(isEditing ? 'cancelEditHyperlinkDialog' : 'cancelCreateHyperlinkDialog');
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const { linkText: text, linkTarget: url, linkType: type, linkEntity: target } = data;
|
|
331
|
+
(0, _internal.withoutNormalizing)(editor, ()=>{
|
|
332
|
+
(0, _editor.insertLink)(editor, {
|
|
333
|
+
text,
|
|
334
|
+
url,
|
|
335
|
+
type,
|
|
336
|
+
target,
|
|
337
|
+
path: currentPath ?? undefined
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
logAction(isEditing ? 'edit' : 'insert', {
|
|
341
|
+
nodeType: type,
|
|
342
|
+
linkType: target?.sys.linkType ?? 'uri'
|
|
320
343
|
});
|
|
321
|
-
});
|
|
322
|
-
(0, _transforms.select)(editor, selectionAfterFocus);
|
|
323
|
-
if (!data) {
|
|
324
344
|
(0, _editor.focus)(editor);
|
|
325
|
-
|
|
326
|
-
|
|
345
|
+
} finally{
|
|
346
|
+
selectionRef?.unref();
|
|
347
|
+
linkRef?.unref();
|
|
327
348
|
}
|
|
328
|
-
const { linkText: text, linkTarget: url, linkType: type, linkEntity: target } = data;
|
|
329
|
-
(0, _internal.withoutNormalizing)(editor, ()=>{
|
|
330
|
-
(0, _editor.insertLink)(editor, {
|
|
331
|
-
text,
|
|
332
|
-
url,
|
|
333
|
-
type,
|
|
334
|
-
target,
|
|
335
|
-
path
|
|
336
|
-
});
|
|
337
|
-
});
|
|
338
|
-
logAction(isEditing ? 'edit' : 'insert', {
|
|
339
|
-
nodeType: type,
|
|
340
|
-
linkType: target?.sys.linkType ?? 'uri'
|
|
341
|
-
});
|
|
342
|
-
(0, _editor.focus)(editor);
|
|
343
349
|
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _fieldeditorshared = require("@contentful/field-editor-shared");
|
|
6
|
+
const _richtexttypes = require("@contentful/rich-text-types");
|
|
7
|
+
const _vitest = require("vitest");
|
|
8
|
+
const _transforms = require("../../../internal/transforms");
|
|
9
|
+
const _createTestEditor = require("../../../test-utils/createTestEditor");
|
|
10
|
+
const _HyperlinkModal = require("../HyperlinkModal");
|
|
11
|
+
_vitest.vi.mock('@contentful/field-editor-shared', async (importOriginal)=>{
|
|
12
|
+
const actual = await importOriginal();
|
|
13
|
+
return {
|
|
14
|
+
...actual,
|
|
15
|
+
ModalDialogLauncher: {
|
|
16
|
+
...actual.ModalDialogLauncher,
|
|
17
|
+
openDialog: _vitest.vi.fn()
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
_vitest.vi.mock('../../../helpers/editor', async (importOriginal)=>{
|
|
22
|
+
const actual = await importOriginal();
|
|
23
|
+
return {
|
|
24
|
+
...actual,
|
|
25
|
+
focus: _vitest.vi.fn()
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
(0, _vitest.afterEach)(()=>_vitest.vi.clearAllMocks());
|
|
29
|
+
const paragraph = (text)=>({
|
|
30
|
+
type: _richtexttypes.BLOCKS.PARAGRAPH,
|
|
31
|
+
children: [
|
|
32
|
+
{
|
|
33
|
+
text
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
});
|
|
37
|
+
const link = {
|
|
38
|
+
type: _richtexttypes.INLINES.HYPERLINK,
|
|
39
|
+
data: {
|
|
40
|
+
uri: 'https://old.example'
|
|
41
|
+
},
|
|
42
|
+
children: [
|
|
43
|
+
{
|
|
44
|
+
text: 'link'
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
};
|
|
48
|
+
const response = {
|
|
49
|
+
linkType: _richtexttypes.INLINES.HYPERLINK,
|
|
50
|
+
linkText: 'link',
|
|
51
|
+
linkTarget: 'https://new.example'
|
|
52
|
+
};
|
|
53
|
+
const sdk = {
|
|
54
|
+
field: {
|
|
55
|
+
validation: []
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
function openDialog(editor, targetPath) {
|
|
59
|
+
let close;
|
|
60
|
+
_vitest.vi.mocked(_fieldeditorshared.ModalDialogLauncher.openDialog).mockReturnValue(new Promise((resolve)=>{
|
|
61
|
+
close = resolve;
|
|
62
|
+
}));
|
|
63
|
+
const pending = (0, _HyperlinkModal.addOrEditLink)(editor, sdk, _vitest.vi.fn(), targetPath);
|
|
64
|
+
(0, _vitest.expect)(_fieldeditorshared.ModalDialogLauncher.openDialog).toHaveBeenCalledOnce();
|
|
65
|
+
return {
|
|
66
|
+
pending,
|
|
67
|
+
close
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
(0, _vitest.describe)('document changes while a hyperlink dialog is open', ()=>{
|
|
71
|
+
_vitest.it.each([
|
|
72
|
+
'submit',
|
|
73
|
+
'cancel'
|
|
74
|
+
])('keeps the replacement document and a valid caret on %s', async (action)=>{
|
|
75
|
+
const { editor } = (0, _createTestEditor.createTestEditor)({});
|
|
76
|
+
editor.children = [
|
|
77
|
+
paragraph('first'),
|
|
78
|
+
paragraph('second'),
|
|
79
|
+
paragraph('link')
|
|
80
|
+
];
|
|
81
|
+
editor.tf.select({
|
|
82
|
+
anchor: {
|
|
83
|
+
path: [
|
|
84
|
+
2,
|
|
85
|
+
0
|
|
86
|
+
],
|
|
87
|
+
offset: 0
|
|
88
|
+
},
|
|
89
|
+
focus: {
|
|
90
|
+
path: [
|
|
91
|
+
2,
|
|
92
|
+
0
|
|
93
|
+
],
|
|
94
|
+
offset: 4
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
const { pending, close } = openDialog(editor);
|
|
98
|
+
(0, _transforms.setEditorValue)(editor, [
|
|
99
|
+
paragraph('replacement')
|
|
100
|
+
]);
|
|
101
|
+
const selection = structuredClone(editor.selection);
|
|
102
|
+
close(action === 'submit' ? response : null);
|
|
103
|
+
await (0, _vitest.expect)(pending).resolves.toBeUndefined();
|
|
104
|
+
(0, _vitest.expect)(editor.children).toEqual([
|
|
105
|
+
paragraph('replacement')
|
|
106
|
+
]);
|
|
107
|
+
(0, _vitest.expect)(editor.selection).toEqual(selection);
|
|
108
|
+
(0, _vitest.expect)(editor.api.rangeRefs().size).toBe(0);
|
|
109
|
+
(0, _vitest.expect)(editor.api.pathRefs().size).toBe(0);
|
|
110
|
+
});
|
|
111
|
+
(0, _vitest.it)('links the original selected text after a paragraph is inserted before it', async ()=>{
|
|
112
|
+
const { editor } = (0, _createTestEditor.createTestEditor)({});
|
|
113
|
+
editor.children = [
|
|
114
|
+
paragraph('first'),
|
|
115
|
+
paragraph('link')
|
|
116
|
+
];
|
|
117
|
+
editor.tf.select({
|
|
118
|
+
anchor: {
|
|
119
|
+
path: [
|
|
120
|
+
1,
|
|
121
|
+
0
|
|
122
|
+
],
|
|
123
|
+
offset: 0
|
|
124
|
+
},
|
|
125
|
+
focus: {
|
|
126
|
+
path: [
|
|
127
|
+
1,
|
|
128
|
+
0
|
|
129
|
+
],
|
|
130
|
+
offset: 4
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
const { pending, close } = openDialog(editor);
|
|
134
|
+
editor.tf.insertNodes(paragraph('new paragraph'), {
|
|
135
|
+
at: [
|
|
136
|
+
0
|
|
137
|
+
]
|
|
138
|
+
});
|
|
139
|
+
close(response);
|
|
140
|
+
await pending;
|
|
141
|
+
(0, _vitest.expect)(editor.children[1]).toEqual(paragraph('first'));
|
|
142
|
+
(0, _vitest.expect)(editor.children[2].children).toContainEqual({
|
|
143
|
+
...link,
|
|
144
|
+
data: {
|
|
145
|
+
uri: response.linkTarget
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
(0, _vitest.expect)(editor.api.rangeRefs().size).toBe(0);
|
|
149
|
+
(0, _vitest.expect)(editor.api.pathRefs().size).toBe(0);
|
|
150
|
+
});
|
|
151
|
+
_vitest.it.each([
|
|
152
|
+
false,
|
|
153
|
+
true
|
|
154
|
+
])('edits the same moved link with explicit target path: %s', async (targeted)=>{
|
|
155
|
+
const { editor } = (0, _createTestEditor.createTestEditor)({});
|
|
156
|
+
editor.children = [
|
|
157
|
+
paragraph('first'),
|
|
158
|
+
{
|
|
159
|
+
type: _richtexttypes.BLOCKS.PARAGRAPH,
|
|
160
|
+
children: [
|
|
161
|
+
{
|
|
162
|
+
text: ''
|
|
163
|
+
},
|
|
164
|
+
link,
|
|
165
|
+
{
|
|
166
|
+
text: ''
|
|
167
|
+
}
|
|
168
|
+
]
|
|
169
|
+
}
|
|
170
|
+
];
|
|
171
|
+
editor.tf.select({
|
|
172
|
+
path: [
|
|
173
|
+
1,
|
|
174
|
+
1,
|
|
175
|
+
0
|
|
176
|
+
],
|
|
177
|
+
offset: 2
|
|
178
|
+
});
|
|
179
|
+
const { pending, close } = openDialog(editor, targeted ? [
|
|
180
|
+
1,
|
|
181
|
+
1
|
|
182
|
+
] : undefined);
|
|
183
|
+
editor.tf.insertNodes(paragraph('new paragraph'), {
|
|
184
|
+
at: [
|
|
185
|
+
0
|
|
186
|
+
]
|
|
187
|
+
});
|
|
188
|
+
close(response);
|
|
189
|
+
await pending;
|
|
190
|
+
(0, _vitest.expect)(editor.children[1]).toEqual(paragraph('first'));
|
|
191
|
+
(0, _vitest.expect)(editor.children[2].children[1]).toEqual({
|
|
192
|
+
...link,
|
|
193
|
+
data: {
|
|
194
|
+
uri: response.linkTarget
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
(0, _vitest.expect)(editor.api.rangeRefs().size).toBe(0);
|
|
198
|
+
(0, _vitest.expect)(editor.api.pathRefs().size).toBe(0);
|
|
199
|
+
});
|
|
200
|
+
(0, _vitest.it)('releases saved locations when opening the dialog fails', async ()=>{
|
|
201
|
+
const { editor } = (0, _createTestEditor.createTestEditor)({});
|
|
202
|
+
editor.children = [
|
|
203
|
+
{
|
|
204
|
+
type: _richtexttypes.BLOCKS.PARAGRAPH,
|
|
205
|
+
children: [
|
|
206
|
+
{
|
|
207
|
+
text: ''
|
|
208
|
+
},
|
|
209
|
+
link,
|
|
210
|
+
{
|
|
211
|
+
text: ''
|
|
212
|
+
}
|
|
213
|
+
]
|
|
214
|
+
}
|
|
215
|
+
];
|
|
216
|
+
editor.tf.select({
|
|
217
|
+
path: [
|
|
218
|
+
0,
|
|
219
|
+
1,
|
|
220
|
+
0
|
|
221
|
+
],
|
|
222
|
+
offset: 2
|
|
223
|
+
});
|
|
224
|
+
_vitest.vi.mocked(_fieldeditorshared.ModalDialogLauncher.openDialog).mockRejectedValue(new Error('dialog failed'));
|
|
225
|
+
await (0, _vitest.expect)((0, _HyperlinkModal.addOrEditLink)(editor, sdk, _vitest.vi.fn(), [
|
|
226
|
+
0,
|
|
227
|
+
1
|
|
228
|
+
])).rejects.toThrow('dialog failed');
|
|
229
|
+
(0, _vitest.expect)(editor.api.rangeRefs().size).toBe(0);
|
|
230
|
+
(0, _vitest.expect)(editor.api.pathRefs().size).toBe(0);
|
|
231
|
+
});
|
|
232
|
+
(0, _vitest.it)('does not edit a different link that replaces the original target', async ()=>{
|
|
233
|
+
const { editor } = (0, _createTestEditor.createTestEditor)({});
|
|
234
|
+
editor.children = [
|
|
235
|
+
{
|
|
236
|
+
type: _richtexttypes.BLOCKS.PARAGRAPH,
|
|
237
|
+
children: [
|
|
238
|
+
{
|
|
239
|
+
text: ''
|
|
240
|
+
},
|
|
241
|
+
link,
|
|
242
|
+
{
|
|
243
|
+
text: ''
|
|
244
|
+
}
|
|
245
|
+
]
|
|
246
|
+
}
|
|
247
|
+
];
|
|
248
|
+
editor.tf.select({
|
|
249
|
+
path: [
|
|
250
|
+
0,
|
|
251
|
+
1,
|
|
252
|
+
0
|
|
253
|
+
],
|
|
254
|
+
offset: 2
|
|
255
|
+
});
|
|
256
|
+
const { pending, close } = openDialog(editor, [
|
|
257
|
+
0,
|
|
258
|
+
1
|
|
259
|
+
]);
|
|
260
|
+
const replacement = [
|
|
261
|
+
{
|
|
262
|
+
type: _richtexttypes.BLOCKS.PARAGRAPH,
|
|
263
|
+
children: [
|
|
264
|
+
{
|
|
265
|
+
text: ''
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
...link,
|
|
269
|
+
data: {
|
|
270
|
+
uri: 'https://replacement.example'
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
text: ''
|
|
275
|
+
}
|
|
276
|
+
]
|
|
277
|
+
}
|
|
278
|
+
];
|
|
279
|
+
(0, _transforms.setEditorValue)(editor, replacement);
|
|
280
|
+
close(response);
|
|
281
|
+
await pending;
|
|
282
|
+
(0, _vitest.expect)(editor.children).toEqual(replacement);
|
|
283
|
+
(0, _vitest.expect)(editor.api.rangeRefs().size).toBe(0);
|
|
284
|
+
(0, _vitest.expect)(editor.api.pathRefs().size).toBe(0);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
@@ -219,10 +219,7 @@ export function HyperlinkModal(props) {
|
|
|
219
219
|
}
|
|
220
220
|
export async function addOrEditLink(editor, sdk, logAction, targetPath) {
|
|
221
221
|
const isReadOnly = isEditorReadOnly(editor);
|
|
222
|
-
|
|
223
|
-
...editor.selection
|
|
224
|
-
} : undefined;
|
|
225
|
-
if (!targetPath && !selectionBeforeBlur) return;
|
|
222
|
+
if (!targetPath && !editor.selection) return;
|
|
226
223
|
let linkType;
|
|
227
224
|
let linkText;
|
|
228
225
|
let linkTarget;
|
|
@@ -234,46 +231,55 @@ export async function addOrEditLink(editor, sdk, logAction, targetPath) {
|
|
|
234
231
|
linkTarget = node.data.uri || '';
|
|
235
232
|
linkEntity = node.data.target;
|
|
236
233
|
}
|
|
237
|
-
const selectionAfterFocus = targetPath ?? selectionBeforeBlur;
|
|
238
234
|
const currentLinkText = linkText || (editor.selection ? getText(editor, editor.selection) : '');
|
|
239
235
|
const isEditing = Boolean(node && path);
|
|
240
236
|
logAction(isEditing ? 'openEditHyperlinkDialog' : 'openCreateHyperlinkDialog');
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
237
|
+
const selectionRef = editor.selection ? editor.api.rangeRef(editor.selection) : undefined;
|
|
238
|
+
const linkRef = path ? editor.api.pathRef(path) : undefined;
|
|
239
|
+
try {
|
|
240
|
+
const data = await ModalDialogLauncher.openDialog({
|
|
241
|
+
title: isEditing ? 'Edit hyperlink' : 'Insert hyperlink',
|
|
242
|
+
width: 'large',
|
|
243
|
+
shouldCloseOnEscapePress: true,
|
|
244
|
+
shouldCloseOnOverlayClick: true,
|
|
245
|
+
allowHeightOverflow: true
|
|
246
|
+
}, ({ onClose })=>{
|
|
247
|
+
return /*#__PURE__*/ React.createElement(HyperlinkModal, {
|
|
248
|
+
linkTarget: linkTarget,
|
|
249
|
+
linkText: currentLinkText,
|
|
250
|
+
linkType: linkType,
|
|
251
|
+
linkEntity: linkEntity,
|
|
252
|
+
onClose: onClose,
|
|
253
|
+
sdk: sdk,
|
|
254
|
+
readonly: isReadOnly
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
const currentPath = linkRef?.current;
|
|
258
|
+
const selectionAfterFocus = targetPath ? currentPath && editor.api.range(currentPath) : selectionRef?.current;
|
|
259
|
+
if (!selectionAfterFocus || isEditing && !currentPath) return;
|
|
260
|
+
select(editor, selectionAfterFocus);
|
|
261
|
+
if (!data) {
|
|
262
|
+
focus(editor);
|
|
263
|
+
logAction(isEditing ? 'cancelEditHyperlinkDialog' : 'cancelCreateHyperlinkDialog');
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const { linkText: text, linkTarget: url, linkType: type, linkEntity: target } = data;
|
|
267
|
+
withoutNormalizing(editor, ()=>{
|
|
268
|
+
insertLink(editor, {
|
|
269
|
+
text,
|
|
270
|
+
url,
|
|
271
|
+
type,
|
|
272
|
+
target,
|
|
273
|
+
path: currentPath ?? undefined
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
logAction(isEditing ? 'edit' : 'insert', {
|
|
277
|
+
nodeType: type,
|
|
278
|
+
linkType: target?.sys.linkType ?? 'uri'
|
|
256
279
|
});
|
|
257
|
-
});
|
|
258
|
-
select(editor, selectionAfterFocus);
|
|
259
|
-
if (!data) {
|
|
260
280
|
focus(editor);
|
|
261
|
-
|
|
262
|
-
|
|
281
|
+
} finally{
|
|
282
|
+
selectionRef?.unref();
|
|
283
|
+
linkRef?.unref();
|
|
263
284
|
}
|
|
264
|
-
const { linkText: text, linkTarget: url, linkType: type, linkEntity: target } = data;
|
|
265
|
-
withoutNormalizing(editor, ()=>{
|
|
266
|
-
insertLink(editor, {
|
|
267
|
-
text,
|
|
268
|
-
url,
|
|
269
|
-
type,
|
|
270
|
-
target,
|
|
271
|
-
path
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
logAction(isEditing ? 'edit' : 'insert', {
|
|
275
|
-
nodeType: type,
|
|
276
|
-
linkType: target?.sys.linkType ?? 'uri'
|
|
277
|
-
});
|
|
278
|
-
focus(editor);
|
|
279
285
|
}
|
|
@@ -0,0 +1,282 @@
|
|
|
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 { setEditorValue } from '../../../internal/transforms';
|
|
5
|
+
import { createTestEditor } from '../../../test-utils/createTestEditor';
|
|
6
|
+
import { addOrEditLink } from '../HyperlinkModal';
|
|
7
|
+
vi.mock('@contentful/field-editor-shared', async (importOriginal)=>{
|
|
8
|
+
const actual = await importOriginal();
|
|
9
|
+
return {
|
|
10
|
+
...actual,
|
|
11
|
+
ModalDialogLauncher: {
|
|
12
|
+
...actual.ModalDialogLauncher,
|
|
13
|
+
openDialog: vi.fn()
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
vi.mock('../../../helpers/editor', async (importOriginal)=>{
|
|
18
|
+
const actual = await importOriginal();
|
|
19
|
+
return {
|
|
20
|
+
...actual,
|
|
21
|
+
focus: vi.fn()
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
afterEach(()=>vi.clearAllMocks());
|
|
25
|
+
const paragraph = (text)=>({
|
|
26
|
+
type: BLOCKS.PARAGRAPH,
|
|
27
|
+
children: [
|
|
28
|
+
{
|
|
29
|
+
text
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
});
|
|
33
|
+
const link = {
|
|
34
|
+
type: INLINES.HYPERLINK,
|
|
35
|
+
data: {
|
|
36
|
+
uri: 'https://old.example'
|
|
37
|
+
},
|
|
38
|
+
children: [
|
|
39
|
+
{
|
|
40
|
+
text: 'link'
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
};
|
|
44
|
+
const response = {
|
|
45
|
+
linkType: INLINES.HYPERLINK,
|
|
46
|
+
linkText: 'link',
|
|
47
|
+
linkTarget: 'https://new.example'
|
|
48
|
+
};
|
|
49
|
+
const sdk = {
|
|
50
|
+
field: {
|
|
51
|
+
validation: []
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
function openDialog(editor, targetPath) {
|
|
55
|
+
let close;
|
|
56
|
+
vi.mocked(ModalDialogLauncher.openDialog).mockReturnValue(new Promise((resolve)=>{
|
|
57
|
+
close = resolve;
|
|
58
|
+
}));
|
|
59
|
+
const pending = addOrEditLink(editor, sdk, vi.fn(), targetPath);
|
|
60
|
+
expect(ModalDialogLauncher.openDialog).toHaveBeenCalledOnce();
|
|
61
|
+
return {
|
|
62
|
+
pending,
|
|
63
|
+
close
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
describe('document changes while a hyperlink dialog is open', ()=>{
|
|
67
|
+
it.each([
|
|
68
|
+
'submit',
|
|
69
|
+
'cancel'
|
|
70
|
+
])('keeps the replacement document and a valid caret on %s', async (action)=>{
|
|
71
|
+
const { editor } = createTestEditor({});
|
|
72
|
+
editor.children = [
|
|
73
|
+
paragraph('first'),
|
|
74
|
+
paragraph('second'),
|
|
75
|
+
paragraph('link')
|
|
76
|
+
];
|
|
77
|
+
editor.tf.select({
|
|
78
|
+
anchor: {
|
|
79
|
+
path: [
|
|
80
|
+
2,
|
|
81
|
+
0
|
|
82
|
+
],
|
|
83
|
+
offset: 0
|
|
84
|
+
},
|
|
85
|
+
focus: {
|
|
86
|
+
path: [
|
|
87
|
+
2,
|
|
88
|
+
0
|
|
89
|
+
],
|
|
90
|
+
offset: 4
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
const { pending, close } = openDialog(editor);
|
|
94
|
+
setEditorValue(editor, [
|
|
95
|
+
paragraph('replacement')
|
|
96
|
+
]);
|
|
97
|
+
const selection = structuredClone(editor.selection);
|
|
98
|
+
close(action === 'submit' ? response : null);
|
|
99
|
+
await expect(pending).resolves.toBeUndefined();
|
|
100
|
+
expect(editor.children).toEqual([
|
|
101
|
+
paragraph('replacement')
|
|
102
|
+
]);
|
|
103
|
+
expect(editor.selection).toEqual(selection);
|
|
104
|
+
expect(editor.api.rangeRefs().size).toBe(0);
|
|
105
|
+
expect(editor.api.pathRefs().size).toBe(0);
|
|
106
|
+
});
|
|
107
|
+
it('links the original selected text after a paragraph is inserted before it', async ()=>{
|
|
108
|
+
const { editor } = createTestEditor({});
|
|
109
|
+
editor.children = [
|
|
110
|
+
paragraph('first'),
|
|
111
|
+
paragraph('link')
|
|
112
|
+
];
|
|
113
|
+
editor.tf.select({
|
|
114
|
+
anchor: {
|
|
115
|
+
path: [
|
|
116
|
+
1,
|
|
117
|
+
0
|
|
118
|
+
],
|
|
119
|
+
offset: 0
|
|
120
|
+
},
|
|
121
|
+
focus: {
|
|
122
|
+
path: [
|
|
123
|
+
1,
|
|
124
|
+
0
|
|
125
|
+
],
|
|
126
|
+
offset: 4
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
const { pending, close } = openDialog(editor);
|
|
130
|
+
editor.tf.insertNodes(paragraph('new paragraph'), {
|
|
131
|
+
at: [
|
|
132
|
+
0
|
|
133
|
+
]
|
|
134
|
+
});
|
|
135
|
+
close(response);
|
|
136
|
+
await pending;
|
|
137
|
+
expect(editor.children[1]).toEqual(paragraph('first'));
|
|
138
|
+
expect(editor.children[2].children).toContainEqual({
|
|
139
|
+
...link,
|
|
140
|
+
data: {
|
|
141
|
+
uri: response.linkTarget
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
expect(editor.api.rangeRefs().size).toBe(0);
|
|
145
|
+
expect(editor.api.pathRefs().size).toBe(0);
|
|
146
|
+
});
|
|
147
|
+
it.each([
|
|
148
|
+
false,
|
|
149
|
+
true
|
|
150
|
+
])('edits the same moved link with explicit target path: %s', async (targeted)=>{
|
|
151
|
+
const { editor } = createTestEditor({});
|
|
152
|
+
editor.children = [
|
|
153
|
+
paragraph('first'),
|
|
154
|
+
{
|
|
155
|
+
type: BLOCKS.PARAGRAPH,
|
|
156
|
+
children: [
|
|
157
|
+
{
|
|
158
|
+
text: ''
|
|
159
|
+
},
|
|
160
|
+
link,
|
|
161
|
+
{
|
|
162
|
+
text: ''
|
|
163
|
+
}
|
|
164
|
+
]
|
|
165
|
+
}
|
|
166
|
+
];
|
|
167
|
+
editor.tf.select({
|
|
168
|
+
path: [
|
|
169
|
+
1,
|
|
170
|
+
1,
|
|
171
|
+
0
|
|
172
|
+
],
|
|
173
|
+
offset: 2
|
|
174
|
+
});
|
|
175
|
+
const { pending, close } = openDialog(editor, targeted ? [
|
|
176
|
+
1,
|
|
177
|
+
1
|
|
178
|
+
] : undefined);
|
|
179
|
+
editor.tf.insertNodes(paragraph('new paragraph'), {
|
|
180
|
+
at: [
|
|
181
|
+
0
|
|
182
|
+
]
|
|
183
|
+
});
|
|
184
|
+
close(response);
|
|
185
|
+
await pending;
|
|
186
|
+
expect(editor.children[1]).toEqual(paragraph('first'));
|
|
187
|
+
expect(editor.children[2].children[1]).toEqual({
|
|
188
|
+
...link,
|
|
189
|
+
data: {
|
|
190
|
+
uri: response.linkTarget
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
expect(editor.api.rangeRefs().size).toBe(0);
|
|
194
|
+
expect(editor.api.pathRefs().size).toBe(0);
|
|
195
|
+
});
|
|
196
|
+
it('releases saved locations when opening the dialog fails', async ()=>{
|
|
197
|
+
const { editor } = createTestEditor({});
|
|
198
|
+
editor.children = [
|
|
199
|
+
{
|
|
200
|
+
type: BLOCKS.PARAGRAPH,
|
|
201
|
+
children: [
|
|
202
|
+
{
|
|
203
|
+
text: ''
|
|
204
|
+
},
|
|
205
|
+
link,
|
|
206
|
+
{
|
|
207
|
+
text: ''
|
|
208
|
+
}
|
|
209
|
+
]
|
|
210
|
+
}
|
|
211
|
+
];
|
|
212
|
+
editor.tf.select({
|
|
213
|
+
path: [
|
|
214
|
+
0,
|
|
215
|
+
1,
|
|
216
|
+
0
|
|
217
|
+
],
|
|
218
|
+
offset: 2
|
|
219
|
+
});
|
|
220
|
+
vi.mocked(ModalDialogLauncher.openDialog).mockRejectedValue(new Error('dialog failed'));
|
|
221
|
+
await expect(addOrEditLink(editor, sdk, vi.fn(), [
|
|
222
|
+
0,
|
|
223
|
+
1
|
|
224
|
+
])).rejects.toThrow('dialog failed');
|
|
225
|
+
expect(editor.api.rangeRefs().size).toBe(0);
|
|
226
|
+
expect(editor.api.pathRefs().size).toBe(0);
|
|
227
|
+
});
|
|
228
|
+
it('does not edit a different link that replaces the original target', async ()=>{
|
|
229
|
+
const { editor } = createTestEditor({});
|
|
230
|
+
editor.children = [
|
|
231
|
+
{
|
|
232
|
+
type: BLOCKS.PARAGRAPH,
|
|
233
|
+
children: [
|
|
234
|
+
{
|
|
235
|
+
text: ''
|
|
236
|
+
},
|
|
237
|
+
link,
|
|
238
|
+
{
|
|
239
|
+
text: ''
|
|
240
|
+
}
|
|
241
|
+
]
|
|
242
|
+
}
|
|
243
|
+
];
|
|
244
|
+
editor.tf.select({
|
|
245
|
+
path: [
|
|
246
|
+
0,
|
|
247
|
+
1,
|
|
248
|
+
0
|
|
249
|
+
],
|
|
250
|
+
offset: 2
|
|
251
|
+
});
|
|
252
|
+
const { pending, close } = openDialog(editor, [
|
|
253
|
+
0,
|
|
254
|
+
1
|
|
255
|
+
]);
|
|
256
|
+
const replacement = [
|
|
257
|
+
{
|
|
258
|
+
type: BLOCKS.PARAGRAPH,
|
|
259
|
+
children: [
|
|
260
|
+
{
|
|
261
|
+
text: ''
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
...link,
|
|
265
|
+
data: {
|
|
266
|
+
uri: 'https://replacement.example'
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
text: ''
|
|
271
|
+
}
|
|
272
|
+
]
|
|
273
|
+
}
|
|
274
|
+
];
|
|
275
|
+
setEditorValue(editor, replacement);
|
|
276
|
+
close(response);
|
|
277
|
+
await pending;
|
|
278
|
+
expect(editor.children).toEqual(replacement);
|
|
279
|
+
expect(editor.api.rangeRefs().size).toBe(0);
|
|
280
|
+
expect(editor.api.pathRefs().size).toBe(0);
|
|
281
|
+
});
|
|
282
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-rich-text",
|
|
3
|
-
"version": "6.6.
|
|
3
|
+
"version": "6.6.5-canary.0+73ea49cf",
|
|
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": "
|
|
99
|
+
"gitHead": "73ea49cfa354b5a8ed3545203a191f885f91201d"
|
|
100
100
|
}
|