@contentful/field-editor-rich-text 6.6.5 → 6.6.6-canary.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/__tests__/performance.test.js +209 -0
- package/dist/cjs/internal/pluginAdapter.js +2 -0
- package/dist/cjs/plugins/EmbeddedEntityBlock/LinkedEntityBlock.js +1 -1
- package/dist/cjs/plugins/EmbeddedEntityInline/LinkedEntityInline.js +1 -1
- package/dist/cjs/plugins/EmbeddedResourceBlock/LinkedResourceBlock.js +1 -1
- package/dist/cjs/plugins/EmbeddedResourceInline/LinkedResourceInline.js +1 -1
- package/dist/cjs/plugins/Hyperlink/components/EntityHyperlink.js +2 -2
- package/dist/cjs/plugins/Hyperlink/components/ResourceHyperlink.js +2 -2
- package/dist/cjs/plugins/Hyperlink/components/UrlHyperlink.js +2 -2
- package/dist/cjs/plugins/Hyperlink/components/linkHandlers.js +4 -2
- package/dist/cjs/plugins/Hyperlink/components/useHyperlinkCommon.js +12 -5
- package/dist/esm/__tests__/performance.test.js +164 -0
- package/dist/esm/internal/pluginAdapter.js +2 -0
- package/dist/esm/plugins/EmbeddedEntityBlock/LinkedEntityBlock.js +2 -2
- package/dist/esm/plugins/EmbeddedEntityInline/LinkedEntityInline.js +2 -2
- package/dist/esm/plugins/EmbeddedResourceBlock/LinkedResourceBlock.js +2 -2
- package/dist/esm/plugins/EmbeddedResourceInline/LinkedResourceInline.js +2 -2
- package/dist/esm/plugins/Hyperlink/components/EntityHyperlink.js +2 -2
- package/dist/esm/plugins/Hyperlink/components/ResourceHyperlink.js +2 -2
- package/dist/esm/plugins/Hyperlink/components/UrlHyperlink.js +2 -2
- package/dist/esm/plugins/Hyperlink/components/linkHandlers.js +4 -2
- package/dist/esm/plugins/Hyperlink/components/useHyperlinkCommon.js +13 -6
- package/dist/types/__tests__/performance.test.d.ts +1 -0
- package/dist/types/plugins/Hyperlink/components/linkHandlers.d.ts +2 -2
- package/dist/types/plugins/Hyperlink/components/useHyperlinkCommon.d.ts +18 -3
- package/package.json +2 -2
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
|
|
6
|
+
const _richtexttypes = require("@contentful/rich-text-types");
|
|
7
|
+
const _react1 = require("@testing-library/react");
|
|
8
|
+
const _vitest = require("vitest");
|
|
9
|
+
const _FakeSdk = require("../__fixtures__/FakeSdk");
|
|
10
|
+
const _fixtures = require("../__fixtures__/fixtures");
|
|
11
|
+
const _toSlateDoc = require("../helpers/toSlateDoc");
|
|
12
|
+
const _testutils = require("../test-utils");
|
|
13
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
14
|
+
if (typeof WeakMap !== "function") return null;
|
|
15
|
+
var cacheBabelInterop = new WeakMap();
|
|
16
|
+
var cacheNodeInterop = new WeakMap();
|
|
17
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
18
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
19
|
+
})(nodeInterop);
|
|
20
|
+
}
|
|
21
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
22
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
23
|
+
return obj;
|
|
24
|
+
}
|
|
25
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
26
|
+
return {
|
|
27
|
+
default: obj
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
31
|
+
if (cache && cache.has(obj)) {
|
|
32
|
+
return cache.get(obj);
|
|
33
|
+
}
|
|
34
|
+
var newObj = {
|
|
35
|
+
__proto__: null
|
|
36
|
+
};
|
|
37
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
38
|
+
for(var key in obj){
|
|
39
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
40
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
41
|
+
if (desc && (desc.get || desc.set)) {
|
|
42
|
+
Object.defineProperty(newObj, key, desc);
|
|
43
|
+
} else {
|
|
44
|
+
newObj[key] = obj[key];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
newObj.default = obj;
|
|
49
|
+
if (cache) {
|
|
50
|
+
cache.set(obj, newObj);
|
|
51
|
+
}
|
|
52
|
+
return newObj;
|
|
53
|
+
}
|
|
54
|
+
const editors = [];
|
|
55
|
+
_vitest.vi.mock('../internal/pluginAdapter', async (importOriginal)=>{
|
|
56
|
+
const mod = await importOriginal();
|
|
57
|
+
return {
|
|
58
|
+
...mod,
|
|
59
|
+
createPlateEditor: (options)=>{
|
|
60
|
+
const editor = mod.createPlateEditor(options);
|
|
61
|
+
editors.push(editor);
|
|
62
|
+
return editor;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
const hyperlinkRenders = [];
|
|
67
|
+
_vitest.vi.mock('../plugins/Hyperlink/components/useHyperlinkCommon', async (importOriginal)=>{
|
|
68
|
+
const mod = await importOriginal();
|
|
69
|
+
return {
|
|
70
|
+
useHyperlinkCommon: (element)=>{
|
|
71
|
+
hyperlinkRenders.push(element);
|
|
72
|
+
return mod.useHyperlinkCommon(element);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
const embedRenders = [];
|
|
77
|
+
_vitest.vi.mock('../plugins/EmbeddedEntityBlock/LinkedEntityBlock', async (importOriginal)=>{
|
|
78
|
+
const mod = await importOriginal();
|
|
79
|
+
return {
|
|
80
|
+
LinkedEntityBlock: (props)=>{
|
|
81
|
+
embedRenders.push(props.element);
|
|
82
|
+
return mod.LinkedEntityBlock(props);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
const { ConnectedRichTextEditor } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("../RichTextEditor")));
|
|
87
|
+
const PERF = !!process.env.PERF;
|
|
88
|
+
const BLOCK_COUNT = PERF ? 300 : 50;
|
|
89
|
+
const KEYSTROKES = PERF ? 100 : 10;
|
|
90
|
+
const text = (value)=>({
|
|
91
|
+
nodeType: 'text',
|
|
92
|
+
value,
|
|
93
|
+
marks: [],
|
|
94
|
+
data: {}
|
|
95
|
+
});
|
|
96
|
+
const makeDoc = (n)=>({
|
|
97
|
+
nodeType: _richtexttypes.BLOCKS.DOCUMENT,
|
|
98
|
+
data: {},
|
|
99
|
+
content: Array.from({
|
|
100
|
+
length: n
|
|
101
|
+
}, (_, i)=>i % 10 === 5 ? {
|
|
102
|
+
nodeType: _richtexttypes.BLOCKS.EMBEDDED_ENTRY,
|
|
103
|
+
data: {
|
|
104
|
+
target: {
|
|
105
|
+
sys: {
|
|
106
|
+
id: _fixtures.entries.published.sys.id,
|
|
107
|
+
type: 'Link',
|
|
108
|
+
linkType: 'Entry'
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
content: []
|
|
113
|
+
} : {
|
|
114
|
+
nodeType: _richtexttypes.BLOCKS.PARAGRAPH,
|
|
115
|
+
data: {},
|
|
116
|
+
content: [
|
|
117
|
+
text(`Paragraph ${i} `),
|
|
118
|
+
{
|
|
119
|
+
nodeType: _richtexttypes.INLINES.HYPERLINK,
|
|
120
|
+
data: {
|
|
121
|
+
uri: 'https://example.com'
|
|
122
|
+
},
|
|
123
|
+
content: [
|
|
124
|
+
text('a link')
|
|
125
|
+
]
|
|
126
|
+
},
|
|
127
|
+
text(' trailing text.')
|
|
128
|
+
]
|
|
129
|
+
})
|
|
130
|
+
});
|
|
131
|
+
const report = (msg)=>{
|
|
132
|
+
if (PERF) console.log(`[perf] ${msg}`);
|
|
133
|
+
};
|
|
134
|
+
(0, _vitest.describe)('rich text performance', ()=>{
|
|
135
|
+
(0, _vitest.it)('typing does not re-render unrelated links and embeds', async ()=>{
|
|
136
|
+
const [baseSdk] = (0, _FakeSdk.newReferenceEditorFakeSdk)();
|
|
137
|
+
const sdk = {
|
|
138
|
+
...baseSdk,
|
|
139
|
+
entry: {
|
|
140
|
+
getSys: ()=>({
|
|
141
|
+
id: 'perf-entry'
|
|
142
|
+
})
|
|
143
|
+
},
|
|
144
|
+
locales: {
|
|
145
|
+
...baseSdk.locales,
|
|
146
|
+
direction: {}
|
|
147
|
+
},
|
|
148
|
+
access: {
|
|
149
|
+
can: async ()=>true
|
|
150
|
+
},
|
|
151
|
+
parameters: {
|
|
152
|
+
instance: {}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
let commits = 0;
|
|
156
|
+
const { container } = (0, _react1.render)(/*#__PURE__*/ _react.createElement(_react.Profiler, {
|
|
157
|
+
id: "rte",
|
|
158
|
+
onRender: ()=>commits++
|
|
159
|
+
}, /*#__PURE__*/ _react.createElement(ConnectedRichTextEditor, {
|
|
160
|
+
sdk: sdk,
|
|
161
|
+
value: makeDoc(BLOCK_COUNT)
|
|
162
|
+
})));
|
|
163
|
+
await (0, _react1.act)(async ()=>{
|
|
164
|
+
await new Promise((resolve)=>setTimeout(resolve, 50));
|
|
165
|
+
});
|
|
166
|
+
const editor = editors.at(-1);
|
|
167
|
+
editor.tf.select({
|
|
168
|
+
path: [
|
|
169
|
+
0,
|
|
170
|
+
0
|
|
171
|
+
],
|
|
172
|
+
offset: 3
|
|
173
|
+
});
|
|
174
|
+
await (0, _react1.act)(async ()=>{});
|
|
175
|
+
commits = 0;
|
|
176
|
+
hyperlinkRenders.length = 0;
|
|
177
|
+
embedRenders.length = 0;
|
|
178
|
+
const start = performance.now();
|
|
179
|
+
for(let i = 0; i < KEYSTROKES; i++){
|
|
180
|
+
await (0, _react1.act)(async ()=>{
|
|
181
|
+
editor.tf.insertText('x');
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
const elapsed = performance.now() - start;
|
|
185
|
+
(0, _vitest.expect)(container.textContent).toContain(`Par${'x'.repeat(KEYSTROKES)}agraph 0`);
|
|
186
|
+
report(`${BLOCK_COUNT} blocks: ${(elapsed / KEYSTROKES).toFixed(2)}ms/keystroke, ` + `${commits / KEYSTROKES} commits/keystroke, ${hyperlinkRenders.length} link renders, ${embedRenders.length} embed renders`);
|
|
187
|
+
(0, _vitest.expect)(commits / KEYSTROKES).toBeLessThan(25);
|
|
188
|
+
(0, _vitest.expect)(hyperlinkRenders).toHaveLength(0);
|
|
189
|
+
(0, _vitest.expect)(embedRenders).toHaveLength(0);
|
|
190
|
+
}, 120000);
|
|
191
|
+
(0, _vitest.it)('normalizing does not resolve unregistered plugins', ()=>{
|
|
192
|
+
const { editor } = (0, _testutils.createTestEditor)({});
|
|
193
|
+
editor.children = (0, _toSlateDoc.toSlateDoc)(makeDoc(BLOCK_COUNT));
|
|
194
|
+
const { getPlugin } = editor;
|
|
195
|
+
const unregistered = new Set();
|
|
196
|
+
editor.getPlugin = (plugin)=>{
|
|
197
|
+
if (!editor.plugins[plugin.key]) unregistered.add(plugin.key);
|
|
198
|
+
return getPlugin(plugin);
|
|
199
|
+
};
|
|
200
|
+
const start = performance.now();
|
|
201
|
+
editor.tf.normalize({
|
|
202
|
+
force: true
|
|
203
|
+
});
|
|
204
|
+
report(`normalize ${BLOCK_COUNT} blocks: ${(performance.now() - start).toFixed(0)}ms`);
|
|
205
|
+
(0, _vitest.expect)([
|
|
206
|
+
...unregistered
|
|
207
|
+
]).toEqual([]);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
@@ -146,6 +146,8 @@ 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;
|
|
149
151
|
return editor;
|
|
150
152
|
}
|
|
151
153
|
});
|
|
@@ -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.
|
|
66
|
+
const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)();
|
|
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.
|
|
66
|
+
const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)();
|
|
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.
|
|
28
|
+
const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)();
|
|
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.
|
|
28
|
+
const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)();
|
|
29
29
|
const sdk = (0, _SdkProvider.useSdkContext)();
|
|
30
30
|
const isDisabled = (0, _react1.useReadOnly)();
|
|
31
31
|
const link = element.data.target.sys;
|
|
@@ -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,
|
|
61
|
+
const { editor, sdk, isLinkFocused, 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,
|
|
89
|
+
handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, props.element),
|
|
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,
|
|
61
|
+
const { editor, sdk, isLinkFocused, 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,
|
|
78
|
+
handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, props.element),
|
|
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,
|
|
59
|
+
const { editor, sdk, isLinkFocused, 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,
|
|
69
|
+
handleEditLink: ()=>(0, _linkHandlers.handleEditLink)(editor, sdk, props.element),
|
|
70
70
|
handleRemoveLink: ()=>(0, _linkHandlers.handleRemoveLink)(editor),
|
|
71
71
|
handleCopyLink: ()=>(0, _linkHandlers.handleCopyLink)(uri),
|
|
72
72
|
popoverText: popoverText,
|
|
@@ -21,9 +21,11 @@ _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");
|
|
24
25
|
const _HyperlinkModal = require("../HyperlinkModal");
|
|
25
|
-
const handleEditLink = (editor, sdk,
|
|
26
|
-
|
|
26
|
+
const handleEditLink = (editor, sdk, element)=>{
|
|
27
|
+
const pathToElement = editor && (0, _queries.findNodePath)(editor, element);
|
|
28
|
+
if (!pathToElement) return;
|
|
27
29
|
(0, _HyperlinkModal.addOrEditLink)(editor, sdk, editor.tracking.onViewportAction, pathToElement);
|
|
28
30
|
};
|
|
29
31
|
const handleRemoveLink = (editor)=>{
|
|
@@ -9,15 +9,23 @@ Object.defineProperty(exports, "useHyperlinkCommon", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
const _react = require("react");
|
|
12
|
+
const _react1 = require("platejs/react");
|
|
12
13
|
const _ContentfulEditorProvider = require("../../../ContentfulEditorProvider");
|
|
13
14
|
const _queries = require("../../../internal/queries");
|
|
14
15
|
const _SdkProvider = require("../../../SdkProvider");
|
|
15
16
|
function useHyperlinkCommon(element) {
|
|
16
|
-
const
|
|
17
|
+
const id = (0, _ContentfulEditorProvider.useContentfulEditorId)();
|
|
18
|
+
const editor = (0, _ContentfulEditorProvider.useContentfulEditorRef)(id);
|
|
17
19
|
const sdk = (0, _SdkProvider.useSdkContext)();
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
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
|
+
});
|
|
21
29
|
const [isEditorFocused, setIsEditorFocused] = (0, _react.useState)(false);
|
|
22
30
|
(0, _react.useEffect)(()=>{
|
|
23
31
|
const handleFocus = ()=>setIsEditorFocused(true);
|
|
@@ -41,7 +49,6 @@ function useHyperlinkCommon(element) {
|
|
|
41
49
|
editor,
|
|
42
50
|
sdk,
|
|
43
51
|
isLinkFocused,
|
|
44
|
-
pathToElement,
|
|
45
52
|
isEditorFocused
|
|
46
53
|
};
|
|
47
54
|
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { BLOCKS, INLINES } from '@contentful/rich-text-types';
|
|
3
|
+
import { act, render } from '@testing-library/react';
|
|
4
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { newReferenceEditorFakeSdk } from '../__fixtures__/FakeSdk';
|
|
6
|
+
import { entries } from '../__fixtures__/fixtures';
|
|
7
|
+
import { toSlateDoc } from '../helpers/toSlateDoc';
|
|
8
|
+
import { createTestEditor } from '../test-utils';
|
|
9
|
+
const editors = [];
|
|
10
|
+
vi.mock('../internal/pluginAdapter', async (importOriginal)=>{
|
|
11
|
+
const mod = await importOriginal();
|
|
12
|
+
return {
|
|
13
|
+
...mod,
|
|
14
|
+
createPlateEditor: (options)=>{
|
|
15
|
+
const editor = mod.createPlateEditor(options);
|
|
16
|
+
editors.push(editor);
|
|
17
|
+
return editor;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
const hyperlinkRenders = [];
|
|
22
|
+
vi.mock('../plugins/Hyperlink/components/useHyperlinkCommon', async (importOriginal)=>{
|
|
23
|
+
const mod = await importOriginal();
|
|
24
|
+
return {
|
|
25
|
+
useHyperlinkCommon: (element)=>{
|
|
26
|
+
hyperlinkRenders.push(element);
|
|
27
|
+
return mod.useHyperlinkCommon(element);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
const embedRenders = [];
|
|
32
|
+
vi.mock('../plugins/EmbeddedEntityBlock/LinkedEntityBlock', async (importOriginal)=>{
|
|
33
|
+
const mod = await importOriginal();
|
|
34
|
+
return {
|
|
35
|
+
LinkedEntityBlock: (props)=>{
|
|
36
|
+
embedRenders.push(props.element);
|
|
37
|
+
return mod.LinkedEntityBlock(props);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
const { ConnectedRichTextEditor } = await import('../RichTextEditor');
|
|
42
|
+
const PERF = !!process.env.PERF;
|
|
43
|
+
const BLOCK_COUNT = PERF ? 300 : 50;
|
|
44
|
+
const KEYSTROKES = PERF ? 100 : 10;
|
|
45
|
+
const text = (value)=>({
|
|
46
|
+
nodeType: 'text',
|
|
47
|
+
value,
|
|
48
|
+
marks: [],
|
|
49
|
+
data: {}
|
|
50
|
+
});
|
|
51
|
+
const makeDoc = (n)=>({
|
|
52
|
+
nodeType: BLOCKS.DOCUMENT,
|
|
53
|
+
data: {},
|
|
54
|
+
content: Array.from({
|
|
55
|
+
length: n
|
|
56
|
+
}, (_, i)=>i % 10 === 5 ? {
|
|
57
|
+
nodeType: BLOCKS.EMBEDDED_ENTRY,
|
|
58
|
+
data: {
|
|
59
|
+
target: {
|
|
60
|
+
sys: {
|
|
61
|
+
id: entries.published.sys.id,
|
|
62
|
+
type: 'Link',
|
|
63
|
+
linkType: 'Entry'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
content: []
|
|
68
|
+
} : {
|
|
69
|
+
nodeType: BLOCKS.PARAGRAPH,
|
|
70
|
+
data: {},
|
|
71
|
+
content: [
|
|
72
|
+
text(`Paragraph ${i} `),
|
|
73
|
+
{
|
|
74
|
+
nodeType: INLINES.HYPERLINK,
|
|
75
|
+
data: {
|
|
76
|
+
uri: 'https://example.com'
|
|
77
|
+
},
|
|
78
|
+
content: [
|
|
79
|
+
text('a link')
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
text(' trailing text.')
|
|
83
|
+
]
|
|
84
|
+
})
|
|
85
|
+
});
|
|
86
|
+
const report = (msg)=>{
|
|
87
|
+
if (PERF) console.log(`[perf] ${msg}`);
|
|
88
|
+
};
|
|
89
|
+
describe('rich text performance', ()=>{
|
|
90
|
+
it('typing does not re-render unrelated links and embeds', async ()=>{
|
|
91
|
+
const [baseSdk] = newReferenceEditorFakeSdk();
|
|
92
|
+
const sdk = {
|
|
93
|
+
...baseSdk,
|
|
94
|
+
entry: {
|
|
95
|
+
getSys: ()=>({
|
|
96
|
+
id: 'perf-entry'
|
|
97
|
+
})
|
|
98
|
+
},
|
|
99
|
+
locales: {
|
|
100
|
+
...baseSdk.locales,
|
|
101
|
+
direction: {}
|
|
102
|
+
},
|
|
103
|
+
access: {
|
|
104
|
+
can: async ()=>true
|
|
105
|
+
},
|
|
106
|
+
parameters: {
|
|
107
|
+
instance: {}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
let commits = 0;
|
|
111
|
+
const { container } = render(/*#__PURE__*/ React.createElement(React.Profiler, {
|
|
112
|
+
id: "rte",
|
|
113
|
+
onRender: ()=>commits++
|
|
114
|
+
}, /*#__PURE__*/ React.createElement(ConnectedRichTextEditor, {
|
|
115
|
+
sdk: sdk,
|
|
116
|
+
value: makeDoc(BLOCK_COUNT)
|
|
117
|
+
})));
|
|
118
|
+
await act(async ()=>{
|
|
119
|
+
await new Promise((resolve)=>setTimeout(resolve, 50));
|
|
120
|
+
});
|
|
121
|
+
const editor = editors.at(-1);
|
|
122
|
+
editor.tf.select({
|
|
123
|
+
path: [
|
|
124
|
+
0,
|
|
125
|
+
0
|
|
126
|
+
],
|
|
127
|
+
offset: 3
|
|
128
|
+
});
|
|
129
|
+
await act(async ()=>{});
|
|
130
|
+
commits = 0;
|
|
131
|
+
hyperlinkRenders.length = 0;
|
|
132
|
+
embedRenders.length = 0;
|
|
133
|
+
const start = performance.now();
|
|
134
|
+
for(let i = 0; i < KEYSTROKES; i++){
|
|
135
|
+
await act(async ()=>{
|
|
136
|
+
editor.tf.insertText('x');
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const elapsed = performance.now() - start;
|
|
140
|
+
expect(container.textContent).toContain(`Par${'x'.repeat(KEYSTROKES)}agraph 0`);
|
|
141
|
+
report(`${BLOCK_COUNT} blocks: ${(elapsed / KEYSTROKES).toFixed(2)}ms/keystroke, ` + `${commits / KEYSTROKES} commits/keystroke, ${hyperlinkRenders.length} link renders, ${embedRenders.length} embed renders`);
|
|
142
|
+
expect(commits / KEYSTROKES).toBeLessThan(25);
|
|
143
|
+
expect(hyperlinkRenders).toHaveLength(0);
|
|
144
|
+
expect(embedRenders).toHaveLength(0);
|
|
145
|
+
}, 120000);
|
|
146
|
+
it('normalizing does not resolve unregistered plugins', ()=>{
|
|
147
|
+
const { editor } = createTestEditor({});
|
|
148
|
+
editor.children = toSlateDoc(makeDoc(BLOCK_COUNT));
|
|
149
|
+
const { getPlugin } = editor;
|
|
150
|
+
const unregistered = new Set();
|
|
151
|
+
editor.getPlugin = (plugin)=>{
|
|
152
|
+
if (!editor.plugins[plugin.key]) unregistered.add(plugin.key);
|
|
153
|
+
return getPlugin(plugin);
|
|
154
|
+
};
|
|
155
|
+
const start = performance.now();
|
|
156
|
+
editor.tf.normalize({
|
|
157
|
+
force: true
|
|
158
|
+
});
|
|
159
|
+
report(`normalize ${BLOCK_COUNT} blocks: ${(performance.now() - start).toFixed(0)}ms`);
|
|
160
|
+
expect([
|
|
161
|
+
...unregistered
|
|
162
|
+
]).toEqual([]);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
@@ -122,6 +122,8 @@ 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;
|
|
125
127
|
return editor;
|
|
126
128
|
}
|
|
127
129
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useReadOnly, useSelected } from 'platejs/react';
|
|
3
|
-
import {
|
|
3
|
+
import { useContentfulEditorRef } 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 =
|
|
15
|
+
const editor = useContentfulEditorRef();
|
|
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 {
|
|
3
|
+
import { useContentfulEditorRef } 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 =
|
|
15
|
+
const editor = useContentfulEditorRef();
|
|
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 {
|
|
3
|
+
import { useContentfulEditorRef } 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 =
|
|
13
|
+
const editor = useContentfulEditorRef();
|
|
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 {
|
|
3
|
+
import { useContentfulEditorRef } 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 =
|
|
13
|
+
const editor = useContentfulEditorRef();
|
|
14
14
|
const sdk = useSdkContext();
|
|
15
15
|
const isDisabled = useReadOnly();
|
|
16
16
|
const link = element.data.target.sys;
|
|
@@ -7,7 +7,7 @@ import { LinkPopover } from './LinkPopover';
|
|
|
7
7
|
import { styles } from './styles';
|
|
8
8
|
import { useHyperlinkCommon } from './useHyperlinkCommon';
|
|
9
9
|
export function EntityHyperlink(props) {
|
|
10
|
-
const { editor, sdk, isLinkFocused,
|
|
10
|
+
const { editor, sdk, isLinkFocused, isEditorFocused } = useHyperlinkCommon(props.element);
|
|
11
11
|
const { onEntityFetchComplete } = useLinkTracking();
|
|
12
12
|
const { target } = props.element.data;
|
|
13
13
|
const tooltipContent = useEntityInfo({
|
|
@@ -35,7 +35,7 @@ export function EntityHyperlink(props) {
|
|
|
35
35
|
}, tooltipContent);
|
|
36
36
|
return /*#__PURE__*/ React.createElement(LinkPopover, {
|
|
37
37
|
isLinkFocused: isLinkFocused,
|
|
38
|
-
handleEditLink: ()=>handleEditLink(editor, sdk,
|
|
38
|
+
handleEditLink: ()=>handleEditLink(editor, sdk, props.element),
|
|
39
39
|
handleRemoveLink: ()=>handleRemoveLink(editor),
|
|
40
40
|
popoverText: popoverText,
|
|
41
41
|
isEditorFocused: isEditorFocused
|
|
@@ -7,7 +7,7 @@ import { LinkPopover } from './LinkPopover';
|
|
|
7
7
|
import { styles } from './styles';
|
|
8
8
|
import { useHyperlinkCommon } from './useHyperlinkCommon';
|
|
9
9
|
export function ResourceHyperlink(props) {
|
|
10
|
-
const { editor, sdk, isLinkFocused,
|
|
10
|
+
const { editor, sdk, isLinkFocused, isEditorFocused } = useHyperlinkCommon(props.element);
|
|
11
11
|
const { onEntityFetchComplete } = useLinkTracking();
|
|
12
12
|
const { target } = props.element.data;
|
|
13
13
|
const tooltipContent = useResourceEntityInfo({
|
|
@@ -24,7 +24,7 @@ export function ResourceHyperlink(props) {
|
|
|
24
24
|
}, tooltipContent);
|
|
25
25
|
return /*#__PURE__*/ React.createElement(LinkPopover, {
|
|
26
26
|
isLinkFocused: isLinkFocused,
|
|
27
|
-
handleEditLink: ()=>handleEditLink(editor, sdk,
|
|
27
|
+
handleEditLink: ()=>handleEditLink(editor, sdk, props.element),
|
|
28
28
|
handleRemoveLink: ()=>handleRemoveLink(editor),
|
|
29
29
|
popoverText: popoverText,
|
|
30
30
|
isEditorFocused: isEditorFocused
|
|
@@ -5,7 +5,7 @@ import { LinkPopover } from './LinkPopover';
|
|
|
5
5
|
import { styles } from './styles';
|
|
6
6
|
import { useHyperlinkCommon } from './useHyperlinkCommon';
|
|
7
7
|
export function UrlHyperlink(props) {
|
|
8
|
-
const { editor, sdk, isLinkFocused,
|
|
8
|
+
const { editor, sdk, isLinkFocused, isEditorFocused } = useHyperlinkCommon(props.element);
|
|
9
9
|
const uri = props.element.data?.uri;
|
|
10
10
|
const popoverText = /*#__PURE__*/ React.createElement(TextLink, {
|
|
11
11
|
className: styles.openLink,
|
|
@@ -15,7 +15,7 @@ export function UrlHyperlink(props) {
|
|
|
15
15
|
}, uri);
|
|
16
16
|
return /*#__PURE__*/ React.createElement(LinkPopover, {
|
|
17
17
|
isLinkFocused: isLinkFocused,
|
|
18
|
-
handleEditLink: ()=>handleEditLink(editor, sdk,
|
|
18
|
+
handleEditLink: ()=>handleEditLink(editor, sdk, props.element),
|
|
19
19
|
handleRemoveLink: ()=>handleRemoveLink(editor),
|
|
20
20
|
handleCopyLink: ()=>handleCopyLink(uri),
|
|
21
21
|
popoverText: popoverText,
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Notification } from '@contentful/f36-components';
|
|
2
2
|
import { unwrapLink } from '../../../helpers/editor';
|
|
3
|
+
import { findNodePath } from '../../../internal/queries';
|
|
3
4
|
import { addOrEditLink } from '../HyperlinkModal';
|
|
4
|
-
export const handleEditLink = (editor, sdk,
|
|
5
|
-
|
|
5
|
+
export const handleEditLink = (editor, sdk, element)=>{
|
|
6
|
+
const pathToElement = editor && findNodePath(editor, element);
|
|
7
|
+
if (!pathToElement) return;
|
|
6
8
|
addOrEditLink(editor, sdk, editor.tracking.onViewportAction, pathToElement);
|
|
7
9
|
};
|
|
8
10
|
export const handleRemoveLink = (editor)=>{
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { useEditorSelector } from 'platejs/react';
|
|
3
|
+
import { useContentfulEditorId, useContentfulEditorRef } from '../../../ContentfulEditorProvider';
|
|
3
4
|
import { findNodePath, isChildPath } from '../../../internal/queries';
|
|
4
5
|
import { useSdkContext } from '../../../SdkProvider';
|
|
5
6
|
export function useHyperlinkCommon(element) {
|
|
6
|
-
const
|
|
7
|
+
const id = useContentfulEditorId();
|
|
8
|
+
const editor = useContentfulEditorRef(id);
|
|
7
9
|
const sdk = useSdkContext();
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
const isLinkFocused = useEditorSelector((editor)=>{
|
|
11
|
+
const focus = editor.selection?.focus;
|
|
12
|
+
const pathToElement = focus && findNodePath(editor, element);
|
|
13
|
+
return !!pathToElement && isChildPath(focus.path, pathToElement);
|
|
14
|
+
}, [
|
|
15
|
+
element
|
|
16
|
+
], {
|
|
17
|
+
id
|
|
18
|
+
});
|
|
11
19
|
const [isEditorFocused, setIsEditorFocused] = useState(false);
|
|
12
20
|
useEffect(()=>{
|
|
13
21
|
const handleFocus = ()=>setIsEditorFocused(true);
|
|
@@ -31,7 +39,6 @@ export function useHyperlinkCommon(element) {
|
|
|
31
39
|
editor,
|
|
32
40
|
sdk,
|
|
33
41
|
isLinkFocused,
|
|
34
|
-
pathToElement,
|
|
35
42
|
isEditorFocused
|
|
36
43
|
};
|
|
37
44
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FieldAppSDK } from '@contentful/app-sdk';
|
|
2
|
-
import {
|
|
3
|
-
export declare const handleEditLink: (editor: PlateEditor, sdk: FieldAppSDK,
|
|
2
|
+
import { Element, PlateEditor } from '../../../internal/types';
|
|
3
|
+
export declare const handleEditLink: (editor: PlateEditor, sdk: FieldAppSDK, element: Element) => void;
|
|
4
4
|
export declare const handleRemoveLink: (editor: PlateEditor) => void;
|
|
5
5
|
export declare const handleCopyLink: (uri: string | undefined) => Promise<void>;
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { FieldAppSDK } from '@contentful/app-sdk';
|
|
2
|
+
import { PlateEditor } from '../../../internal/types';
|
|
2
3
|
export declare function useHyperlinkCommon(element: any): {
|
|
3
|
-
editor:
|
|
4
|
+
editor: PlateEditor & {
|
|
5
|
+
store: import("jotai-x").ReturnOfUseStoreApi<import("@platejs/core/react").PlateStoreState<import("@platejs/core/react").PlateEditor>, {
|
|
6
|
+
trackedEditor: import("jotai").Atom<{
|
|
7
|
+
editor: any;
|
|
8
|
+
version: number | null;
|
|
9
|
+
}>;
|
|
10
|
+
trackedSelection: import("jotai").Atom<{
|
|
11
|
+
selection: any;
|
|
12
|
+
version: number | null;
|
|
13
|
+
}>;
|
|
14
|
+
trackedValue: import("jotai").Atom<{
|
|
15
|
+
value: any;
|
|
16
|
+
version: number | null;
|
|
17
|
+
}>;
|
|
18
|
+
}>;
|
|
19
|
+
};
|
|
4
20
|
sdk: FieldAppSDK;
|
|
5
|
-
isLinkFocused: boolean
|
|
6
|
-
pathToElement: import("@platejs/slate").Path | undefined;
|
|
21
|
+
isLinkFocused: boolean;
|
|
7
22
|
isEditorFocused: boolean;
|
|
8
23
|
};
|
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.6-canary.1+ca62f04b",
|
|
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": "ca62f04b56a4b31cb1b7651efc98cd424f196d48"
|
|
100
100
|
}
|