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

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.
Files changed (30) hide show
  1. package/dist/cjs/internal/pluginAdapter.js +0 -2
  2. package/dist/cjs/plugins/EmbeddedEntityBlock/LinkedEntityBlock.js +1 -1
  3. package/dist/cjs/plugins/EmbeddedEntityInline/LinkedEntityInline.js +1 -1
  4. package/dist/cjs/plugins/EmbeddedResourceBlock/LinkedResourceBlock.js +1 -1
  5. package/dist/cjs/plugins/EmbeddedResourceInline/LinkedResourceInline.js +1 -1
  6. package/dist/cjs/plugins/Hyperlink/HyperlinkModal.js +12 -2
  7. package/dist/cjs/plugins/Hyperlink/__tests__/createHyperlinkPlugin.test.js +239 -0
  8. package/dist/cjs/plugins/Hyperlink/components/EntityHyperlink.js +2 -2
  9. package/dist/cjs/plugins/Hyperlink/components/ResourceHyperlink.js +2 -2
  10. package/dist/cjs/plugins/Hyperlink/components/UrlHyperlink.js +2 -2
  11. package/dist/cjs/plugins/Hyperlink/components/linkHandlers.js +2 -4
  12. package/dist/cjs/plugins/Hyperlink/components/useHyperlinkCommon.js +5 -12
  13. package/dist/esm/internal/pluginAdapter.js +0 -2
  14. package/dist/esm/plugins/EmbeddedEntityBlock/LinkedEntityBlock.js +2 -2
  15. package/dist/esm/plugins/EmbeddedEntityInline/LinkedEntityInline.js +2 -2
  16. package/dist/esm/plugins/EmbeddedResourceBlock/LinkedResourceBlock.js +2 -2
  17. package/dist/esm/plugins/EmbeddedResourceInline/LinkedResourceInline.js +2 -2
  18. package/dist/esm/plugins/Hyperlink/HyperlinkModal.js +12 -2
  19. package/dist/esm/plugins/Hyperlink/__tests__/createHyperlinkPlugin.test.js +240 -1
  20. package/dist/esm/plugins/Hyperlink/components/EntityHyperlink.js +2 -2
  21. package/dist/esm/plugins/Hyperlink/components/ResourceHyperlink.js +2 -2
  22. package/dist/esm/plugins/Hyperlink/components/UrlHyperlink.js +2 -2
  23. package/dist/esm/plugins/Hyperlink/components/linkHandlers.js +2 -4
  24. package/dist/esm/plugins/Hyperlink/components/useHyperlinkCommon.js +6 -13
  25. package/dist/types/plugins/Hyperlink/components/linkHandlers.d.ts +2 -2
  26. package/dist/types/plugins/Hyperlink/components/useHyperlinkCommon.d.ts +3 -18
  27. package/package.json +2 -2
  28. package/dist/cjs/__tests__/performance.test.js +0 -209
  29. package/dist/esm/__tests__/performance.test.js +0 -164
  30. package/dist/types/__tests__/performance.test.d.ts +0 -1
@@ -146,8 +146,6 @@ const createPlateEditor = (options = {})=>{
146
146
  priority: 10000,
147
147
  extendEditor: ({ editor })=>{
148
148
  editor.contentfulPlugins = descriptors;
149
- const { getType } = editor;
150
- editor.getType = (key)=>editor.plugins[key] ? getType(key) : key;
151
149
  return editor;
152
150
  }
153
151
  });
@@ -63,7 +63,7 @@ function LinkedEntityBlock(props) {
63
63
  const { attributes, children, element } = props;
64
64
  const { onEntityFetchComplete } = (0, _linkstracking.useLinkTracking)();
65
65
  const isSelected = (0, _react1.useSelected)();
66
- const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)();
66
+ const editor = (0, _ContentfulEditorProvider.useContentfulEditor)();
67
67
  const sdk = (0, _SdkProvider.useSdkContext)();
68
68
  const isDisabled = (0, _react1.useReadOnly)();
69
69
  const { id: entityId, linkType: entityType } = element.data.target.sys;
@@ -63,7 +63,7 @@ function LinkedEntityInline(props) {
63
63
  const { attributes, children, element } = props;
64
64
  const { onEntityFetchComplete } = (0, _linkstracking.useLinkTracking)();
65
65
  const isSelected = (0, _react1.useSelected)();
66
- const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)();
66
+ const editor = (0, _ContentfulEditorProvider.useContentfulEditor)();
67
67
  const sdk = (0, _SdkProvider.useSdkContext)();
68
68
  const isDisabled = (0, _react1.useReadOnly)();
69
69
  const { id: entryId } = element.data.target.sys;
@@ -25,7 +25,7 @@ function LinkedResourceBlock(props) {
25
25
  const { attributes, children, element } = props;
26
26
  const { onEntityFetchComplete } = (0, _linkstracking.useLinkTracking)();
27
27
  const isSelected = (0, _react1.useSelected)();
28
- const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)();
28
+ const editor = (0, _ContentfulEditorProvider.useContentfulEditor)();
29
29
  const sdk = (0, _SdkProvider.useSdkContext)();
30
30
  const isDisabled = (0, _react1.useReadOnly)();
31
31
  const link = element.data.target.sys;
@@ -25,7 +25,7 @@ function LinkedResourceInline(props) {
25
25
  const { attributes, children, element } = props;
26
26
  const { onEntityFetchComplete } = (0, _linkstracking.useLinkTracking)();
27
27
  const isSelected = (0, _react1.useSelected)();
28
- const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)();
28
+ const editor = (0, _ContentfulEditorProvider.useContentfulEditor)();
29
29
  const sdk = (0, _SdkProvider.useSdkContext)();
30
30
  const isDisabled = (0, _react1.useReadOnly)();
31
31
  const link = element.data.target.sys;
@@ -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
+ });
@@ -58,7 +58,7 @@ function _interop_require_wildcard(obj, nodeInterop) {
58
58
  return newObj;
59
59
  }
60
60
  function EntityHyperlink(props) {
61
- const { editor, sdk, isLinkFocused, isEditorFocused } = (0, _useHyperlinkCommon.useHyperlinkCommon)(props.element);
61
+ const { editor, sdk, isLinkFocused, pathToElement, isEditorFocused } = (0, _useHyperlinkCommon.useHyperlinkCommon)(props.element);
62
62
  const { onEntityFetchComplete } = (0, _linkstracking.useLinkTracking)();
63
63
  const { target } = props.element.data;
64
64
  const tooltipContent = (0, _useEntityInfo.useEntityInfo)({
@@ -86,7 +86,7 @@ function EntityHyperlink(props) {
86
86
  }, tooltipContent);
87
87
  return /*#__PURE__*/ _react.createElement(_LinkPopover.LinkPopover, {
88
88
  isLinkFocused: isLinkFocused,
89
- handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, props.element),
89
+ handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, pathToElement),
90
90
  handleRemoveLink: ()=>(0, _linkHandlers.handleRemoveLink)(editor),
91
91
  popoverText: popoverText,
92
92
  isEditorFocused: isEditorFocused
@@ -58,7 +58,7 @@ function _interop_require_wildcard(obj, nodeInterop) {
58
58
  return newObj;
59
59
  }
60
60
  function ResourceHyperlink(props) {
61
- const { editor, sdk, isLinkFocused, isEditorFocused } = (0, _useHyperlinkCommon.useHyperlinkCommon)(props.element);
61
+ const { editor, sdk, isLinkFocused, pathToElement, isEditorFocused } = (0, _useHyperlinkCommon.useHyperlinkCommon)(props.element);
62
62
  const { onEntityFetchComplete } = (0, _linkstracking.useLinkTracking)();
63
63
  const { target } = props.element.data;
64
64
  const tooltipContent = (0, _useResourceEntityInfo.useResourceEntityInfo)({
@@ -75,7 +75,7 @@ function ResourceHyperlink(props) {
75
75
  }, tooltipContent);
76
76
  return /*#__PURE__*/ _react.createElement(_LinkPopover.LinkPopover, {
77
77
  isLinkFocused: isLinkFocused,
78
- handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, props.element),
78
+ handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, pathToElement),
79
79
  handleRemoveLink: ()=>(0, _linkHandlers.handleRemoveLink)(editor),
80
80
  popoverText: popoverText,
81
81
  isEditorFocused: isEditorFocused
@@ -56,7 +56,7 @@ function _interop_require_wildcard(obj, nodeInterop) {
56
56
  return newObj;
57
57
  }
58
58
  function UrlHyperlink(props) {
59
- const { editor, sdk, isLinkFocused, isEditorFocused } = (0, _useHyperlinkCommon.useHyperlinkCommon)(props.element);
59
+ const { editor, sdk, isLinkFocused, pathToElement, isEditorFocused } = (0, _useHyperlinkCommon.useHyperlinkCommon)(props.element);
60
60
  const uri = props.element.data?.uri;
61
61
  const popoverText = /*#__PURE__*/ _react.createElement(_f36components.TextLink, {
62
62
  className: _styles.styles.openLink,
@@ -66,7 +66,7 @@ function UrlHyperlink(props) {
66
66
  }, uri);
67
67
  return /*#__PURE__*/ _react.createElement(_LinkPopover.LinkPopover, {
68
68
  isLinkFocused: isLinkFocused,
69
- handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, props.element),
69
+ handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, pathToElement),
70
70
  handleRemoveLink: ()=>(0, _linkHandlers.handleRemoveLink)(editor),
71
71
  handleCopyLink: ()=>(0, _linkHandlers.handleCopyLink)(uri),
72
72
  popoverText: popoverText,
@@ -21,11 +21,9 @@ _export(exports, {
21
21
  });
22
22
  const _f36components = require("@contentful/f36-components");
23
23
  const _editor = require("../../../helpers/editor");
24
- const _queries = require("../../../internal/queries");
25
24
  const _HyperlinkModal = require("../HyperlinkModal");
26
- const handleEditLink = (editor, sdk, element)=>{
27
- const pathToElement = editor && (0, _queries.findNodePath)(editor, element);
28
- if (!pathToElement) return;
25
+ const handleEditLink = (editor, sdk, pathToElement)=>{
26
+ if (!editor || !pathToElement) return;
29
27
  (0, _HyperlinkModal.addOrEditLink)(editor, sdk, editor.tracking.onViewportAction, pathToElement);
30
28
  };
31
29
  const handleRemoveLink = (editor)=>{
@@ -9,23 +9,15 @@ Object.defineProperty(exports, "useHyperlinkCommon", {
9
9
  }
10
10
  });
11
11
  const _react = require("react");
12
- const _react1 = require("platejs/react");
13
12
  const _ContentfulEditorProvider = require("../../../ContentfulEditorProvider");
14
13
  const _queries = require("../../../internal/queries");
15
14
  const _SdkProvider = require("../../../SdkProvider");
16
15
  function useHyperlinkCommon(element) {
17
- const id = (0, _ContentfulEditorProvider.useContentfulEditorId)();
18
- const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)(id);
16
+ const editor = (0, _ContentfulEditorProvider.useContentfulEditor)();
19
17
  const sdk = (0, _SdkProvider.useSdkContext)();
20
- const isLinkFocused = (0, _react1.useEditorSelector)((editor)=>{
21
- const focus = editor.selection?.focus;
22
- const pathToElement = focus && (0, _queries.findNodePath)(editor, element);
23
- return !!pathToElement && (0, _queries.isChildPath)(focus.path, pathToElement);
24
- }, [
25
- element
26
- ], {
27
- id
28
- });
18
+ const focus = editor.selection?.focus;
19
+ const pathToElement = (0, _queries.findNodePath)(editor, element);
20
+ const isLinkFocused = pathToElement && focus && (0, _queries.isChildPath)(focus.path, pathToElement);
29
21
  const [isEditorFocused, setIsEditorFocused] = (0, _react.useState)(false);
30
22
  (0, _react.useEffect)(()=>{
31
23
  const handleFocus = ()=>setIsEditorFocused(true);
@@ -49,6 +41,7 @@ function useHyperlinkCommon(element) {
49
41
  editor,
50
42
  sdk,
51
43
  isLinkFocused,
44
+ pathToElement,
52
45
  isEditorFocused
53
46
  };
54
47
  }
@@ -122,8 +122,6 @@ export const createPlateEditor = (options = {})=>{
122
122
  priority: 10000,
123
123
  extendEditor: ({ editor })=>{
124
124
  editor.contentfulPlugins = descriptors;
125
- const { getType } = editor;
126
- editor.getType = (key)=>editor.plugins[key] ? getType(key) : key;
127
125
  return editor;
128
126
  }
129
127
  });
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { useReadOnly, useSelected } from 'platejs/react';
3
- import { useContentfulEditorRef } from '../../ContentfulEditorProvider';
3
+ import { useContentfulEditor } from '../../ContentfulEditorProvider';
4
4
  import { findNodePath } from '../../internal/queries';
5
5
  import { removeNodes } from '../../internal/transforms';
6
6
  import { useSdkContext } from '../../SdkProvider';
@@ -12,7 +12,7 @@ export function LinkedEntityBlock(props) {
12
12
  const { attributes, children, element } = props;
13
13
  const { onEntityFetchComplete } = useLinkTracking();
14
14
  const isSelected = useSelected();
15
- const editor = useContentfulEditorRef();
15
+ const editor = useContentfulEditor();
16
16
  const sdk = useSdkContext();
17
17
  const isDisabled = useReadOnly();
18
18
  const { id: entityId, linkType: entityType } = element.data.target.sys;
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { useReadOnly, useSelected } from 'platejs/react';
3
- import { useContentfulEditorRef } from '../../ContentfulEditorProvider';
3
+ import { useContentfulEditor } from '../../ContentfulEditorProvider';
4
4
  import { focus } from '../../helpers/editor';
5
5
  import { findNodePath } from '../../internal/queries';
6
6
  import { removeNodes } from '../../internal/transforms';
@@ -12,7 +12,7 @@ export function LinkedEntityInline(props) {
12
12
  const { attributes, children, element } = props;
13
13
  const { onEntityFetchComplete } = useLinkTracking();
14
14
  const isSelected = useSelected();
15
- const editor = useContentfulEditorRef();
15
+ const editor = useContentfulEditor();
16
16
  const sdk = useSdkContext();
17
17
  const isDisabled = useReadOnly();
18
18
  const { id: entryId } = element.data.target.sys;
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { useSelected, useReadOnly } from 'platejs/react';
3
- import { useContentfulEditorRef } from '../../ContentfulEditorProvider';
3
+ import { useContentfulEditor } from '../../ContentfulEditorProvider';
4
4
  import { findNodePath, removeNodes } from '../../internal';
5
5
  import { useSdkContext } from '../../SdkProvider';
6
6
  import { useLinkTracking } from '../links-tracking';
@@ -10,7 +10,7 @@ export function LinkedResourceBlock(props) {
10
10
  const { attributes, children, element } = props;
11
11
  const { onEntityFetchComplete } = useLinkTracking();
12
12
  const isSelected = useSelected();
13
- const editor = useContentfulEditorRef();
13
+ const editor = useContentfulEditor();
14
14
  const sdk = useSdkContext();
15
15
  const isDisabled = useReadOnly();
16
16
  const link = element.data.target.sys;
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { useSelected, useReadOnly } from 'platejs/react';
3
- import { useContentfulEditorRef } from '../../ContentfulEditorProvider';
3
+ import { useContentfulEditor } from '../../ContentfulEditorProvider';
4
4
  import { findNodePath, removeNodes } from '../../internal';
5
5
  import { useSdkContext } from '../../SdkProvider';
6
6
  import { useLinkTracking } from '../links-tracking';
@@ -10,7 +10,7 @@ export function LinkedResourceInline(props) {
10
10
  const { attributes, children, element } = props;
11
11
  const { onEntityFetchComplete } = useLinkTracking();
12
12
  const isSelected = useSelected();
13
- const editor = useContentfulEditorRef();
13
+ const editor = useContentfulEditor();
14
14
  const sdk = useSdkContext();
15
15
  const isDisabled = useReadOnly();
16
16
  const link = element.data.target.sys;
@@ -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;