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

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.
@@ -131,7 +131,20 @@ const withDeleteTable = (editor)=>{
131
131
  return applyOverride(editor, _table.withDeleteTable);
132
132
  };
133
133
  const withGetFragmentTable = (editor, _plugin)=>applyOverride(editor, _table.withGetFragmentTable);
134
- const withInsertFragmentTable = (editor, _plugin)=>applyOverride(editor, _table.withInsertFragmentTable);
134
+ const withInsertFragmentTable = (editor, _plugin)=>{
135
+ const { insertFragment } = editor;
136
+ editor.insertFragment = (fragment)=>{
137
+ const context = (0, _platejs.getEditorPlugin)(editor, _table.BaseTablePlugin);
138
+ _table.withInsertFragmentTable({
139
+ ...context,
140
+ tf: {
141
+ ...context.tf,
142
+ insertFragment
143
+ }
144
+ }).transforms.insertFragment(fragment);
145
+ };
146
+ return editor;
147
+ };
135
148
  const withInsertTextTable = (editor, _plugin)=>applyOverride(editor, _table.withInsertTextTable);
136
149
  const withSelectionTable = (editor)=>applyOverride(editor, _table.withTableCellSelection);
137
150
  const withSetFragmentDataTable = (editor)=>applyOverride(editor, _table.withSetFragmentDataTable);
@@ -283,7 +283,10 @@ function HyperlinkModal(props) {
283
283
  }
284
284
  async function addOrEditLink(editor, sdk, logAction, targetPath) {
285
285
  const isReadOnly = (0, _queries.isEditorReadOnly)(editor);
286
- if (!targetPath && !editor.selection) return;
286
+ const selectionBeforeBlur = editor.selection ? {
287
+ ...editor.selection
288
+ } : undefined;
289
+ if (!targetPath && !selectionBeforeBlur) return;
287
290
  let linkType;
288
291
  let linkText;
289
292
  let linkTarget;
@@ -295,55 +298,46 @@ async function addOrEditLink(editor, sdk, logAction, targetPath) {
295
298
  linkTarget = node.data.uri || '';
296
299
  linkEntity = node.data.target;
297
300
  }
301
+ const selectionAfterFocus = targetPath ?? selectionBeforeBlur;
298
302
  const currentLinkText = linkText || (editor.selection ? (0, _queries.getText)(editor, editor.selection) : '');
299
303
  const isEditing = Boolean(node && path);
300
304
  logAction(isEditing ? 'openEditHyperlinkDialog' : 'openCreateHyperlinkDialog');
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'
305
+ const data = await _fieldeditorshared.ModalDialogLauncher.openDialog({
306
+ title: isEditing ? 'Edit hyperlink' : 'Insert hyperlink',
307
+ width: 'large',
308
+ shouldCloseOnEscapePress: true,
309
+ shouldCloseOnOverlayClick: true,
310
+ allowHeightOverflow: true
311
+ }, ({ onClose })=>{
312
+ return /*#__PURE__*/ _react.createElement(HyperlinkModal, {
313
+ linkTarget: linkTarget,
314
+ linkText: currentLinkText,
315
+ linkType: linkType,
316
+ linkEntity: linkEntity,
317
+ onClose: onClose,
318
+ sdk: sdk,
319
+ readonly: isReadOnly
343
320
  });
321
+ });
322
+ (0, _transforms.select)(editor, selectionAfterFocus);
323
+ if (!data) {
344
324
  (0, _editor.focus)(editor);
345
- } finally{
346
- selectionRef?.unref();
347
- linkRef?.unref();
325
+ logAction(isEditing ? 'cancelEditHyperlinkDialog' : 'cancelCreateHyperlinkDialog');
326
+ return;
348
327
  }
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);
349
343
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
+ const _richtexttypes = require("@contentful/rich-text-types");
5
6
  const _vitest = require("vitest");
6
7
  const _testutils = require("../../../test-utils");
7
8
  (0, _vitest.describe)('normalization', ()=>{
@@ -91,3 +92,121 @@ const _testutils = require("../../../test-utils");
91
92
  });
92
93
  });
93
94
  });
95
+ (0, _vitest.describe)('paste', ()=>{
96
+ const paragraph = (value)=>({
97
+ type: _richtexttypes.BLOCKS.PARAGRAPH,
98
+ children: [
99
+ {
100
+ text: value
101
+ }
102
+ ]
103
+ });
104
+ const table = (rows)=>({
105
+ type: _richtexttypes.BLOCKS.TABLE,
106
+ children: rows.map((cells)=>({
107
+ type: _richtexttypes.BLOCKS.TABLE_ROW,
108
+ children: cells.map((value)=>({
109
+ type: _richtexttypes.BLOCKS.TABLE_CELL,
110
+ children: [
111
+ paragraph(value)
112
+ ]
113
+ }))
114
+ }))
115
+ });
116
+ _vitest.it.each([
117
+ {
118
+ name: 'rows',
119
+ cells: [
120
+ [
121
+ 'A1',
122
+ 'B1'
123
+ ],
124
+ [
125
+ 'A2',
126
+ 'B2'
127
+ ]
128
+ ]
129
+ },
130
+ {
131
+ name: 'columns',
132
+ cells: [
133
+ [
134
+ 'A1',
135
+ 'B1',
136
+ 'C1'
137
+ ]
138
+ ]
139
+ }
140
+ ])('expands table $name when pasting text and a table into a cell', ({ cells })=>{
141
+ const { editor } = (0, _testutils.createTestEditor)({});
142
+ editor.children = [
143
+ table([
144
+ [
145
+ 'Old A',
146
+ 'Old B'
147
+ ]
148
+ ]),
149
+ paragraph('Keep following paragraph')
150
+ ];
151
+ editor.tf.select({
152
+ path: [
153
+ 0,
154
+ 0,
155
+ 0,
156
+ 0,
157
+ 0
158
+ ],
159
+ offset: 2
160
+ });
161
+ editor.insertFragment([
162
+ paragraph('Before table'),
163
+ table(cells)
164
+ ]);
165
+ (0, _vitest.expect)(editor.children).toMatchObject([
166
+ table(cells),
167
+ paragraph('Keep following paragraph')
168
+ ]);
169
+ editor.insertText('!');
170
+ const lastRow = cells.length - 1;
171
+ const lastCol = cells[lastRow].length - 1;
172
+ (0, _vitest.expect)(editor.api.string([
173
+ 0,
174
+ lastRow,
175
+ lastCol
176
+ ])).toBe(`${cells[lastRow][lastCol]}!`);
177
+ });
178
+ (0, _vitest.it)('pastes a paragraph into a cell without expanding the table', ()=>{
179
+ const { editor } = (0, _testutils.createTestEditor)({});
180
+ editor.children = [
181
+ table([
182
+ [
183
+ 'Before after',
184
+ 'Keep cell'
185
+ ]
186
+ ]),
187
+ paragraph('Keep following paragraph')
188
+ ];
189
+ editor.tf.select({
190
+ path: [
191
+ 0,
192
+ 0,
193
+ 0,
194
+ 0,
195
+ 0
196
+ ],
197
+ offset: 7
198
+ });
199
+ editor.insertFragment([
200
+ paragraph('pasted ')
201
+ ]);
202
+ (0, _vitest.expect)(editor.children).toMatchObject([
203
+ table([
204
+ [
205
+ 'Before pasted after',
206
+ 'Keep cell'
207
+ ]
208
+ ]),
209
+ paragraph('Keep following paragraph')
210
+ ]);
211
+ });
212
+ });
@@ -1,6 +1,6 @@
1
1
  import * as table from '@platejs/table';
2
2
  import { onKeyDownTable as onNativeKeyDownTable } from '@platejs/table/react';
3
- import { KEYS, assignLegacyApi, assignLegacyTransforms } from 'platejs';
3
+ import { KEYS, assignLegacyApi, assignLegacyTransforms, getEditorPlugin as getSlateEditorPlugin } from 'platejs';
4
4
  import { getEditorPlugin } from 'platejs/react';
5
5
  export * from '@platejs/table';
6
6
  export const ELEMENT_TABLE = KEYS.table;
@@ -33,7 +33,20 @@ export const withDeleteTable = (editor)=>{
33
33
  return applyOverride(editor, table.withDeleteTable);
34
34
  };
35
35
  export const withGetFragmentTable = (editor, _plugin)=>applyOverride(editor, table.withGetFragmentTable);
36
- export const withInsertFragmentTable = (editor, _plugin)=>applyOverride(editor, table.withInsertFragmentTable);
36
+ export const withInsertFragmentTable = (editor, _plugin)=>{
37
+ const { insertFragment } = editor;
38
+ editor.insertFragment = (fragment)=>{
39
+ const context = getSlateEditorPlugin(editor, table.BaseTablePlugin);
40
+ table.withInsertFragmentTable({
41
+ ...context,
42
+ tf: {
43
+ ...context.tf,
44
+ insertFragment
45
+ }
46
+ }).transforms.insertFragment(fragment);
47
+ };
48
+ return editor;
49
+ };
37
50
  export const withInsertTextTable = (editor, _plugin)=>applyOverride(editor, table.withInsertTextTable);
38
51
  export const withSelectionTable = (editor)=>applyOverride(editor, table.withTableCellSelection);
39
52
  export const withSetFragmentDataTable = (editor)=>applyOverride(editor, table.withSetFragmentDataTable);
@@ -219,7 +219,10 @@ export function HyperlinkModal(props) {
219
219
  }
220
220
  export async function addOrEditLink(editor, sdk, logAction, targetPath) {
221
221
  const isReadOnly = isEditorReadOnly(editor);
222
- if (!targetPath && !editor.selection) return;
222
+ const selectionBeforeBlur = editor.selection ? {
223
+ ...editor.selection
224
+ } : undefined;
225
+ if (!targetPath && !selectionBeforeBlur) return;
223
226
  let linkType;
224
227
  let linkText;
225
228
  let linkTarget;
@@ -231,55 +234,46 @@ export async function addOrEditLink(editor, sdk, logAction, targetPath) {
231
234
  linkTarget = node.data.uri || '';
232
235
  linkEntity = node.data.target;
233
236
  }
237
+ const selectionAfterFocus = targetPath ?? selectionBeforeBlur;
234
238
  const currentLinkText = linkText || (editor.selection ? getText(editor, editor.selection) : '');
235
239
  const isEditing = Boolean(node && path);
236
240
  logAction(isEditing ? 'openEditHyperlinkDialog' : 'openCreateHyperlinkDialog');
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'
241
+ const data = await ModalDialogLauncher.openDialog({
242
+ title: isEditing ? 'Edit hyperlink' : 'Insert hyperlink',
243
+ width: 'large',
244
+ shouldCloseOnEscapePress: true,
245
+ shouldCloseOnOverlayClick: true,
246
+ allowHeightOverflow: true
247
+ }, ({ onClose })=>{
248
+ return /*#__PURE__*/ React.createElement(HyperlinkModal, {
249
+ linkTarget: linkTarget,
250
+ linkText: currentLinkText,
251
+ linkType: linkType,
252
+ linkEntity: linkEntity,
253
+ onClose: onClose,
254
+ sdk: sdk,
255
+ readonly: isReadOnly
279
256
  });
257
+ });
258
+ select(editor, selectionAfterFocus);
259
+ if (!data) {
280
260
  focus(editor);
281
- } finally{
282
- selectionRef?.unref();
283
- linkRef?.unref();
261
+ logAction(isEditing ? 'cancelEditHyperlinkDialog' : 'cancelCreateHyperlinkDialog');
262
+ return;
284
263
  }
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);
285
279
  }
@@ -1,5 +1,6 @@
1
- import { describe, it } from 'vitest';
2
- import { assertOutput, jsx } from '../../../test-utils';
1
+ import { BLOCKS } 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
  describe('Table', ()=>{
5
6
  it('removes empty table nodes', ()=>{
@@ -87,3 +88,121 @@ describe('normalization', ()=>{
87
88
  });
88
89
  });
89
90
  });
91
+ describe('paste', ()=>{
92
+ const paragraph = (value)=>({
93
+ type: BLOCKS.PARAGRAPH,
94
+ children: [
95
+ {
96
+ text: value
97
+ }
98
+ ]
99
+ });
100
+ const table = (rows)=>({
101
+ type: BLOCKS.TABLE,
102
+ children: rows.map((cells)=>({
103
+ type: BLOCKS.TABLE_ROW,
104
+ children: cells.map((value)=>({
105
+ type: BLOCKS.TABLE_CELL,
106
+ children: [
107
+ paragraph(value)
108
+ ]
109
+ }))
110
+ }))
111
+ });
112
+ it.each([
113
+ {
114
+ name: 'rows',
115
+ cells: [
116
+ [
117
+ 'A1',
118
+ 'B1'
119
+ ],
120
+ [
121
+ 'A2',
122
+ 'B2'
123
+ ]
124
+ ]
125
+ },
126
+ {
127
+ name: 'columns',
128
+ cells: [
129
+ [
130
+ 'A1',
131
+ 'B1',
132
+ 'C1'
133
+ ]
134
+ ]
135
+ }
136
+ ])('expands table $name when pasting text and a table into a cell', ({ cells })=>{
137
+ const { editor } = createTestEditor({});
138
+ editor.children = [
139
+ table([
140
+ [
141
+ 'Old A',
142
+ 'Old B'
143
+ ]
144
+ ]),
145
+ paragraph('Keep following paragraph')
146
+ ];
147
+ editor.tf.select({
148
+ path: [
149
+ 0,
150
+ 0,
151
+ 0,
152
+ 0,
153
+ 0
154
+ ],
155
+ offset: 2
156
+ });
157
+ editor.insertFragment([
158
+ paragraph('Before table'),
159
+ table(cells)
160
+ ]);
161
+ expect(editor.children).toMatchObject([
162
+ table(cells),
163
+ paragraph('Keep following paragraph')
164
+ ]);
165
+ editor.insertText('!');
166
+ const lastRow = cells.length - 1;
167
+ const lastCol = cells[lastRow].length - 1;
168
+ expect(editor.api.string([
169
+ 0,
170
+ lastRow,
171
+ lastCol
172
+ ])).toBe(`${cells[lastRow][lastCol]}!`);
173
+ });
174
+ it('pastes a paragraph into a cell without expanding the table', ()=>{
175
+ const { editor } = createTestEditor({});
176
+ editor.children = [
177
+ table([
178
+ [
179
+ 'Before after',
180
+ 'Keep cell'
181
+ ]
182
+ ]),
183
+ paragraph('Keep following paragraph')
184
+ ];
185
+ editor.tf.select({
186
+ path: [
187
+ 0,
188
+ 0,
189
+ 0,
190
+ 0,
191
+ 0
192
+ ],
193
+ offset: 7
194
+ });
195
+ editor.insertFragment([
196
+ paragraph('pasted ')
197
+ ]);
198
+ expect(editor.children).toMatchObject([
199
+ table([
200
+ [
201
+ 'Before pasted after',
202
+ 'Keep cell'
203
+ ]
204
+ ]),
205
+ paragraph('Keep following paragraph')
206
+ ]);
207
+ });
208
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "6.6.5-canary.0+73ea49cf",
3
+ "version": "6.6.5",
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": "73ea49cfa354b5a8ed3545203a191f885f91201d"
99
+ "gitHead": "de438e4f6ab157aab21f50a558d6eeb2babec69c"
100
100
  }
@@ -1,286 +0,0 @@
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
- });
@@ -1,282 +0,0 @@
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
- });