@contentful/field-editor-rich-text 6.6.13-canary.35 → 6.6.13
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/CharConstraints.js +10 -2
- package/dist/cjs/CharConstraints.test.js +276 -0
- package/dist/cjs/__tests__/performance.test.js +14 -2
- package/dist/cjs/internal/hooks.js +3 -0
- package/dist/cjs/plugins/CharCounter/index.js +3 -0
- package/dist/cjs/plugins/CharCounter/withCharCounter.js +3 -0
- package/dist/esm/CharConstraints.js +11 -3
- package/dist/esm/CharConstraints.test.js +226 -0
- package/dist/esm/__tests__/performance.test.js +14 -2
- package/dist/esm/internal/hooks.js +2 -2
- package/dist/esm/plugins/CharCounter/index.js +3 -0
- package/dist/esm/plugins/CharCounter/withCharCounter.js +3 -0
- package/dist/types/CharConstraints.test.d.ts +1 -0
- package/dist/types/internal/hooks.d.ts +1 -2
- package/dist/types/plugins/CharCounter/withCharCounter.d.ts +4 -0
- package/package.json +4 -4
|
@@ -32,11 +32,19 @@ const styles = {
|
|
|
32
32
|
})
|
|
33
33
|
};
|
|
34
34
|
function CharCounter({ checkConstraints }) {
|
|
35
|
-
const editor = (0, _hooks.
|
|
35
|
+
const editor = (0, _hooks.usePlateEditorRef)();
|
|
36
36
|
if (!editor.getCharacterCount) {
|
|
37
37
|
return null;
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
return _react.default.createElement(SubscribedCharCounter, {
|
|
40
|
+
editor: editor,
|
|
41
|
+
checkConstraints: checkConstraints
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function SubscribedCharCounter({ editor, checkConstraints }) {
|
|
45
|
+
const count = (0, _hooks.useEditorPluginOptions)(editor, {
|
|
46
|
+
key: 'char-counter'
|
|
47
|
+
}, (state)=>state.count) ?? editor.getCharacterCount();
|
|
40
48
|
const valid = checkConstraints(count);
|
|
41
49
|
return _react.default.createElement("span", {
|
|
42
50
|
className: (0, _css.cx)({
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _react = /*#__PURE__*/ _interop_require_default(require("react"));
|
|
6
|
+
const _f36tokens = /*#__PURE__*/ _interop_require_default(require("@contentful/f36-tokens"));
|
|
7
|
+
const _richtexttypes = require("@contentful/rich-text-types");
|
|
8
|
+
const _react1 = require("@testing-library/react");
|
|
9
|
+
const _vitest = require("vitest");
|
|
10
|
+
const _CharConstraints = require("./CharConstraints");
|
|
11
|
+
const _plate = require("./internal/plate");
|
|
12
|
+
const _CharCounter = require("./plugins/CharCounter");
|
|
13
|
+
const _utils = /*#__PURE__*/ _interop_require_wildcard(require("./plugins/CharCounter/utils"));
|
|
14
|
+
const _Paragraph = require("./plugins/Paragraph");
|
|
15
|
+
const _SdkProvider = require("./SdkProvider");
|
|
16
|
+
function _interop_require_default(obj) {
|
|
17
|
+
return obj && obj.__esModule ? obj : {
|
|
18
|
+
default: obj
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
22
|
+
if (typeof WeakMap !== "function") return null;
|
|
23
|
+
var cacheBabelInterop = new WeakMap();
|
|
24
|
+
var cacheNodeInterop = new WeakMap();
|
|
25
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
26
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
27
|
+
})(nodeInterop);
|
|
28
|
+
}
|
|
29
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
30
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
31
|
+
return obj;
|
|
32
|
+
}
|
|
33
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
34
|
+
return {
|
|
35
|
+
default: obj
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
39
|
+
if (cache && cache.has(obj)) {
|
|
40
|
+
return cache.get(obj);
|
|
41
|
+
}
|
|
42
|
+
var newObj = {
|
|
43
|
+
__proto__: null
|
|
44
|
+
};
|
|
45
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
46
|
+
for(var key in obj){
|
|
47
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
48
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
49
|
+
if (desc && (desc.get || desc.set)) {
|
|
50
|
+
Object.defineProperty(newObj, key, desc);
|
|
51
|
+
} else {
|
|
52
|
+
newObj[key] = obj[key];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
newObj.default = obj;
|
|
57
|
+
if (cache) {
|
|
58
|
+
cache.set(obj, newObj);
|
|
59
|
+
}
|
|
60
|
+
return newObj;
|
|
61
|
+
}
|
|
62
|
+
const sdk = {
|
|
63
|
+
field: {
|
|
64
|
+
validations: [
|
|
65
|
+
{
|
|
66
|
+
size: {
|
|
67
|
+
max: 250
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
parameters: {
|
|
73
|
+
instance: {}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
function renderCounter(text) {
|
|
77
|
+
const onContentRender = _vitest.vi.fn();
|
|
78
|
+
const editor = (0, _plate.createPlateEditor)({
|
|
79
|
+
value: [
|
|
80
|
+
{
|
|
81
|
+
type: _richtexttypes.BLOCKS.PARAGRAPH,
|
|
82
|
+
children: [
|
|
83
|
+
{
|
|
84
|
+
text
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
}
|
|
88
|
+
],
|
|
89
|
+
plugins: [
|
|
90
|
+
(0, _Paragraph.createParagraphPlugin)(),
|
|
91
|
+
(0, _CharCounter.createCharCounterPlugin)()
|
|
92
|
+
]
|
|
93
|
+
});
|
|
94
|
+
const view = (0, _react1.render)(_react.default.createElement(_plate.Plate, {
|
|
95
|
+
editor: editor
|
|
96
|
+
}, _react.default.createElement(_react.default.Profiler, {
|
|
97
|
+
id: "editor-content",
|
|
98
|
+
onRender: onContentRender
|
|
99
|
+
}, _react.default.createElement(_plate.PlateContent, null)), _react.default.createElement(_SdkProvider.SdkProvider, {
|
|
100
|
+
sdk: sdk
|
|
101
|
+
}, _react.default.createElement(_CharConstraints.CharConstraints, null))));
|
|
102
|
+
return {
|
|
103
|
+
editor,
|
|
104
|
+
onContentRender,
|
|
105
|
+
...view
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
async function edit(operation) {
|
|
109
|
+
await (0, _react1.act)(async ()=>operation());
|
|
110
|
+
await (0, _react1.act)(async ()=>{
|
|
111
|
+
await _vitest.vi.advanceTimersByTimeAsync(350);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
(0, _vitest.describe)('CharConstraints', ()=>{
|
|
115
|
+
(0, _vitest.beforeEach)(()=>_vitest.vi.useFakeTimers());
|
|
116
|
+
(0, _vitest.afterEach)(()=>{
|
|
117
|
+
_vitest.vi.clearAllTimers();
|
|
118
|
+
_vitest.vi.useRealTimers();
|
|
119
|
+
_vitest.vi.restoreAllMocks();
|
|
120
|
+
});
|
|
121
|
+
(0, _vitest.it)('updates the displayed count after deleting whitespace, undoing and redoing', async ()=>{
|
|
122
|
+
const { editor, getByText } = renderCounter(' '.repeat(305));
|
|
123
|
+
(0, _vitest.expect)(getByText('305 characters')).toHaveStyle({
|
|
124
|
+
color: _f36tokens.default.red600
|
|
125
|
+
});
|
|
126
|
+
await edit(()=>{
|
|
127
|
+
editor.apply({
|
|
128
|
+
type: 'remove_text',
|
|
129
|
+
path: [
|
|
130
|
+
0,
|
|
131
|
+
0
|
|
132
|
+
],
|
|
133
|
+
offset: 223,
|
|
134
|
+
text: ' '.repeat(82)
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
(0, _vitest.expect)(getByText('223 characters')).not.toHaveStyle({
|
|
138
|
+
color: _f36tokens.default.red600
|
|
139
|
+
});
|
|
140
|
+
await edit(()=>editor.undo());
|
|
141
|
+
(0, _vitest.expect)(getByText('305 characters')).toHaveStyle({
|
|
142
|
+
color: _f36tokens.default.red600
|
|
143
|
+
});
|
|
144
|
+
await edit(()=>editor.redo());
|
|
145
|
+
(0, _vitest.expect)(getByText('223 characters')).not.toHaveStyle({
|
|
146
|
+
color: _f36tokens.default.red600
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
(0, _vitest.it)('updates the displayed count after inserting text, undoing and redoing', async ()=>{
|
|
150
|
+
const { editor, getByText } = renderCounter('a'.repeat(244));
|
|
151
|
+
(0, _vitest.expect)(getByText('244 characters')).not.toHaveStyle({
|
|
152
|
+
color: _f36tokens.default.red600
|
|
153
|
+
});
|
|
154
|
+
await edit(()=>editor.tf.insertText('b'.repeat(61), {
|
|
155
|
+
at: {
|
|
156
|
+
path: [
|
|
157
|
+
0,
|
|
158
|
+
0
|
|
159
|
+
],
|
|
160
|
+
offset: 244
|
|
161
|
+
}
|
|
162
|
+
}));
|
|
163
|
+
(0, _vitest.expect)(getByText('305 characters')).toHaveStyle({
|
|
164
|
+
color: _f36tokens.default.red600
|
|
165
|
+
});
|
|
166
|
+
await edit(()=>editor.undo());
|
|
167
|
+
(0, _vitest.expect)(getByText('244 characters')).not.toHaveStyle({
|
|
168
|
+
color: _f36tokens.default.red600
|
|
169
|
+
});
|
|
170
|
+
await edit(()=>editor.redo());
|
|
171
|
+
(0, _vitest.expect)(getByText('305 characters')).toHaveStyle({
|
|
172
|
+
color: _f36tokens.default.red600
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
(0, _vitest.it)('recounts once after a burst of edits pauses for 300 ms', async ()=>{
|
|
176
|
+
const { editor, getByText } = renderCounter('a'.repeat(244));
|
|
177
|
+
const recount = _vitest.vi.spyOn(_utils, 'getTextContent');
|
|
178
|
+
for(let i = 0; i < 30; i++){
|
|
179
|
+
await (0, _react1.act)(async ()=>{
|
|
180
|
+
editor.tf.insertText('b', {
|
|
181
|
+
at: {
|
|
182
|
+
path: [
|
|
183
|
+
0,
|
|
184
|
+
0
|
|
185
|
+
],
|
|
186
|
+
offset: 244 + i
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
await _vitest.vi.advanceTimersByTimeAsync(20);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
(0, _vitest.expect)(getByText('244 characters')).not.toHaveStyle({
|
|
193
|
+
color: _f36tokens.default.red600
|
|
194
|
+
});
|
|
195
|
+
(0, _vitest.expect)(recount).not.toHaveBeenCalled();
|
|
196
|
+
await (0, _react1.act)(async ()=>{
|
|
197
|
+
await _vitest.vi.advanceTimersByTimeAsync(279);
|
|
198
|
+
});
|
|
199
|
+
(0, _vitest.expect)(recount).not.toHaveBeenCalled();
|
|
200
|
+
await (0, _react1.act)(async ()=>{
|
|
201
|
+
await _vitest.vi.advanceTimersByTimeAsync(1);
|
|
202
|
+
});
|
|
203
|
+
(0, _vitest.expect)(recount).toHaveBeenCalledTimes(1);
|
|
204
|
+
(0, _vitest.expect)(getByText('274 characters')).toHaveStyle({
|
|
205
|
+
color: _f36tokens.default.red600
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
(0, _vitest.it)('does not recount for selection-only changes', async ()=>{
|
|
209
|
+
const { editor, getByText } = renderCounter('abc');
|
|
210
|
+
const recount = _vitest.vi.spyOn(_utils, 'getTextContent');
|
|
211
|
+
for(let offset = 0; offset <= 3; offset++){
|
|
212
|
+
await (0, _react1.act)(async ()=>editor.tf.select({
|
|
213
|
+
path: [
|
|
214
|
+
0,
|
|
215
|
+
0
|
|
216
|
+
],
|
|
217
|
+
offset
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
await (0, _react1.act)(async ()=>{
|
|
221
|
+
await _vitest.vi.advanceTimersByTimeAsync(350);
|
|
222
|
+
});
|
|
223
|
+
(0, _vitest.expect)(recount).not.toHaveBeenCalled();
|
|
224
|
+
(0, _vitest.expect)(getByText('3 characters')).toBeInTheDocument();
|
|
225
|
+
});
|
|
226
|
+
(0, _vitest.it)('updates the delayed count without rendering or changing editor content', async ()=>{
|
|
227
|
+
const { editor, container, getByText, onContentRender } = renderCounter('abc');
|
|
228
|
+
await (0, _react1.act)(async ()=>{
|
|
229
|
+
editor.tf.select({
|
|
230
|
+
path: [
|
|
231
|
+
0,
|
|
232
|
+
0
|
|
233
|
+
],
|
|
234
|
+
offset: 3
|
|
235
|
+
});
|
|
236
|
+
editor.tf.insertText('d');
|
|
237
|
+
});
|
|
238
|
+
(0, _vitest.expect)(getByText('3 characters')).toBeInTheDocument();
|
|
239
|
+
const content = container.querySelector('[data-slate-editor]');
|
|
240
|
+
const html = content.innerHTML;
|
|
241
|
+
const value = editor.children;
|
|
242
|
+
const selection = editor.selection;
|
|
243
|
+
onContentRender.mockClear();
|
|
244
|
+
await (0, _react1.act)(async ()=>{
|
|
245
|
+
await _vitest.vi.advanceTimersByTimeAsync(300);
|
|
246
|
+
});
|
|
247
|
+
(0, _vitest.expect)(getByText('4 characters')).toBeInTheDocument();
|
|
248
|
+
(0, _vitest.expect)(onContentRender).not.toHaveBeenCalled();
|
|
249
|
+
(0, _vitest.expect)(editor.children).toBe(value);
|
|
250
|
+
(0, _vitest.expect)(editor.selection).toBe(selection);
|
|
251
|
+
(0, _vitest.expect)(content.innerHTML).toBe(html);
|
|
252
|
+
});
|
|
253
|
+
(0, _vitest.it)('renders the constraints without a counter plugin', ()=>{
|
|
254
|
+
const editor = (0, _plate.createPlateEditor)({
|
|
255
|
+
value: [
|
|
256
|
+
{
|
|
257
|
+
type: _richtexttypes.BLOCKS.PARAGRAPH,
|
|
258
|
+
children: [
|
|
259
|
+
{
|
|
260
|
+
text: 'abc'
|
|
261
|
+
}
|
|
262
|
+
]
|
|
263
|
+
}
|
|
264
|
+
],
|
|
265
|
+
plugins: [
|
|
266
|
+
(0, _Paragraph.createParagraphPlugin)()
|
|
267
|
+
]
|
|
268
|
+
});
|
|
269
|
+
const { queryByText } = (0, _react1.render)(_react.default.createElement(_plate.Plate, {
|
|
270
|
+
editor: editor
|
|
271
|
+
}, _react.default.createElement(_SdkProvider.SdkProvider, {
|
|
272
|
+
sdk: sdk
|
|
273
|
+
}, _react.default.createElement(_CharConstraints.CharConstraints, null))));
|
|
274
|
+
(0, _vitest.expect)(queryByText('3 characters')).not.toBeInTheDocument();
|
|
275
|
+
});
|
|
276
|
+
});
|
|
@@ -115,10 +115,21 @@ const makeDoc = (n)=>node(_richtexttypes.BLOCKS.DOCUMENT, {}, Array.from({
|
|
|
115
115
|
]);
|
|
116
116
|
}));
|
|
117
117
|
(0, _vitest.describe)('rich text performance', ()=>{
|
|
118
|
-
|
|
118
|
+
_vitest.it.each([
|
|
119
|
+
false,
|
|
120
|
+
true
|
|
121
|
+
])('typing only re-renders what changed (counter: %s)', async (withCharValidation)=>{
|
|
119
122
|
const [baseSdk] = (0, _FakeSdk.newReferenceEditorFakeSdk)();
|
|
120
123
|
const sdk = {
|
|
121
124
|
...baseSdk,
|
|
125
|
+
cma: {
|
|
126
|
+
...baseSdk.cma,
|
|
127
|
+
scheduledActions: {
|
|
128
|
+
getMany: async ()=>({
|
|
129
|
+
items: []
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
},
|
|
122
133
|
entry: {
|
|
123
134
|
getSys: ()=>({
|
|
124
135
|
id: 'perf-entry'
|
|
@@ -141,7 +152,8 @@ const makeDoc = (n)=>node(_richtexttypes.BLOCKS.DOCUMENT, {}, Array.from({
|
|
|
141
152
|
onRender: ()=>commits++
|
|
142
153
|
}, _react.createElement(ConnectedRichTextEditor, {
|
|
143
154
|
sdk: sdk,
|
|
144
|
-
value: makeDoc(BLOCK_COUNT)
|
|
155
|
+
value: makeDoc(BLOCK_COUNT),
|
|
156
|
+
withCharValidation: withCharValidation
|
|
145
157
|
})));
|
|
146
158
|
await (0, _react1.act)(async ()=>{
|
|
147
159
|
await new Promise((resolve)=>setTimeout(resolve, 50));
|
|
@@ -11,5 +11,8 @@ Object.defineProperty(exports, "createCharCounterPlugin", {
|
|
|
11
11
|
const _withCharCounter = require("./withCharCounter");
|
|
12
12
|
const createCharCounterPlugin = ()=>({
|
|
13
13
|
key: 'char-counter',
|
|
14
|
+
options: {
|
|
15
|
+
count: undefined
|
|
16
|
+
},
|
|
14
17
|
withOverrides: _withCharCounter.withCharCounter
|
|
15
18
|
});
|
|
@@ -27,6 +27,9 @@ const withCharCounter = (editor)=>{
|
|
|
27
27
|
};
|
|
28
28
|
const recount = (0, _pdebounce.default)(async ()=>{
|
|
29
29
|
count = (0, _utils.getTextContent)(editor).length;
|
|
30
|
+
editor.setOption({
|
|
31
|
+
key: 'char-counter'
|
|
32
|
+
}, 'count', count);
|
|
30
33
|
}, CHARACTER_COUNT_DEBOUNCE_TIME);
|
|
31
34
|
editor.apply = (op)=>{
|
|
32
35
|
apply(op);
|
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import tokens from '@contentful/f36-tokens';
|
|
3
3
|
import { CharValidation, ConstraintsUtils } from '@contentful/field-editor-shared';
|
|
4
4
|
import { css, cx } from '@emotion/css';
|
|
5
|
-
import {
|
|
5
|
+
import { useEditorPluginOptions, usePlateEditorRef } from './internal/hooks';
|
|
6
6
|
import { useSdkContext } from './SdkProvider';
|
|
7
7
|
const styles = {
|
|
8
8
|
container: css({
|
|
@@ -17,11 +17,19 @@ const styles = {
|
|
|
17
17
|
})
|
|
18
18
|
};
|
|
19
19
|
function CharCounter({ checkConstraints }) {
|
|
20
|
-
const editor =
|
|
20
|
+
const editor = usePlateEditorRef();
|
|
21
21
|
if (!editor.getCharacterCount) {
|
|
22
22
|
return null;
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
return React.createElement(SubscribedCharCounter, {
|
|
25
|
+
editor: editor,
|
|
26
|
+
checkConstraints: checkConstraints
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function SubscribedCharCounter({ editor, checkConstraints }) {
|
|
30
|
+
const count = useEditorPluginOptions(editor, {
|
|
31
|
+
key: 'char-counter'
|
|
32
|
+
}, (state)=>state.count) ?? editor.getCharacterCount();
|
|
25
33
|
const valid = checkConstraints(count);
|
|
26
34
|
return React.createElement("span", {
|
|
27
35
|
className: cx({
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import tokens from '@contentful/f36-tokens';
|
|
3
|
+
import { BLOCKS } from '@contentful/rich-text-types';
|
|
4
|
+
import { act, render } from '@testing-library/react';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
+
import { CharConstraints } from './CharConstraints';
|
|
7
|
+
import { createPlateEditor, Plate, PlateContent } from './internal/plate';
|
|
8
|
+
import { createCharCounterPlugin } from './plugins/CharCounter';
|
|
9
|
+
import * as charCounterUtils from './plugins/CharCounter/utils';
|
|
10
|
+
import { createParagraphPlugin } from './plugins/Paragraph';
|
|
11
|
+
import { SdkProvider } from './SdkProvider';
|
|
12
|
+
const sdk = {
|
|
13
|
+
field: {
|
|
14
|
+
validations: [
|
|
15
|
+
{
|
|
16
|
+
size: {
|
|
17
|
+
max: 250
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
parameters: {
|
|
23
|
+
instance: {}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
function renderCounter(text) {
|
|
27
|
+
const onContentRender = vi.fn();
|
|
28
|
+
const editor = createPlateEditor({
|
|
29
|
+
value: [
|
|
30
|
+
{
|
|
31
|
+
type: BLOCKS.PARAGRAPH,
|
|
32
|
+
children: [
|
|
33
|
+
{
|
|
34
|
+
text
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
plugins: [
|
|
40
|
+
createParagraphPlugin(),
|
|
41
|
+
createCharCounterPlugin()
|
|
42
|
+
]
|
|
43
|
+
});
|
|
44
|
+
const view = render(React.createElement(Plate, {
|
|
45
|
+
editor: editor
|
|
46
|
+
}, React.createElement(React.Profiler, {
|
|
47
|
+
id: "editor-content",
|
|
48
|
+
onRender: onContentRender
|
|
49
|
+
}, React.createElement(PlateContent, null)), React.createElement(SdkProvider, {
|
|
50
|
+
sdk: sdk
|
|
51
|
+
}, React.createElement(CharConstraints, null))));
|
|
52
|
+
return {
|
|
53
|
+
editor,
|
|
54
|
+
onContentRender,
|
|
55
|
+
...view
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async function edit(operation) {
|
|
59
|
+
await act(async ()=>operation());
|
|
60
|
+
await act(async ()=>{
|
|
61
|
+
await vi.advanceTimersByTimeAsync(350);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
describe('CharConstraints', ()=>{
|
|
65
|
+
beforeEach(()=>vi.useFakeTimers());
|
|
66
|
+
afterEach(()=>{
|
|
67
|
+
vi.clearAllTimers();
|
|
68
|
+
vi.useRealTimers();
|
|
69
|
+
vi.restoreAllMocks();
|
|
70
|
+
});
|
|
71
|
+
it('updates the displayed count after deleting whitespace, undoing and redoing', async ()=>{
|
|
72
|
+
const { editor, getByText } = renderCounter(' '.repeat(305));
|
|
73
|
+
expect(getByText('305 characters')).toHaveStyle({
|
|
74
|
+
color: tokens.red600
|
|
75
|
+
});
|
|
76
|
+
await edit(()=>{
|
|
77
|
+
editor.apply({
|
|
78
|
+
type: 'remove_text',
|
|
79
|
+
path: [
|
|
80
|
+
0,
|
|
81
|
+
0
|
|
82
|
+
],
|
|
83
|
+
offset: 223,
|
|
84
|
+
text: ' '.repeat(82)
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
expect(getByText('223 characters')).not.toHaveStyle({
|
|
88
|
+
color: tokens.red600
|
|
89
|
+
});
|
|
90
|
+
await edit(()=>editor.undo());
|
|
91
|
+
expect(getByText('305 characters')).toHaveStyle({
|
|
92
|
+
color: tokens.red600
|
|
93
|
+
});
|
|
94
|
+
await edit(()=>editor.redo());
|
|
95
|
+
expect(getByText('223 characters')).not.toHaveStyle({
|
|
96
|
+
color: tokens.red600
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
it('updates the displayed count after inserting text, undoing and redoing', async ()=>{
|
|
100
|
+
const { editor, getByText } = renderCounter('a'.repeat(244));
|
|
101
|
+
expect(getByText('244 characters')).not.toHaveStyle({
|
|
102
|
+
color: tokens.red600
|
|
103
|
+
});
|
|
104
|
+
await edit(()=>editor.tf.insertText('b'.repeat(61), {
|
|
105
|
+
at: {
|
|
106
|
+
path: [
|
|
107
|
+
0,
|
|
108
|
+
0
|
|
109
|
+
],
|
|
110
|
+
offset: 244
|
|
111
|
+
}
|
|
112
|
+
}));
|
|
113
|
+
expect(getByText('305 characters')).toHaveStyle({
|
|
114
|
+
color: tokens.red600
|
|
115
|
+
});
|
|
116
|
+
await edit(()=>editor.undo());
|
|
117
|
+
expect(getByText('244 characters')).not.toHaveStyle({
|
|
118
|
+
color: tokens.red600
|
|
119
|
+
});
|
|
120
|
+
await edit(()=>editor.redo());
|
|
121
|
+
expect(getByText('305 characters')).toHaveStyle({
|
|
122
|
+
color: tokens.red600
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
it('recounts once after a burst of edits pauses for 300 ms', async ()=>{
|
|
126
|
+
const { editor, getByText } = renderCounter('a'.repeat(244));
|
|
127
|
+
const recount = vi.spyOn(charCounterUtils, 'getTextContent');
|
|
128
|
+
for(let i = 0; i < 30; i++){
|
|
129
|
+
await act(async ()=>{
|
|
130
|
+
editor.tf.insertText('b', {
|
|
131
|
+
at: {
|
|
132
|
+
path: [
|
|
133
|
+
0,
|
|
134
|
+
0
|
|
135
|
+
],
|
|
136
|
+
offset: 244 + i
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
await vi.advanceTimersByTimeAsync(20);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
expect(getByText('244 characters')).not.toHaveStyle({
|
|
143
|
+
color: tokens.red600
|
|
144
|
+
});
|
|
145
|
+
expect(recount).not.toHaveBeenCalled();
|
|
146
|
+
await act(async ()=>{
|
|
147
|
+
await vi.advanceTimersByTimeAsync(279);
|
|
148
|
+
});
|
|
149
|
+
expect(recount).not.toHaveBeenCalled();
|
|
150
|
+
await act(async ()=>{
|
|
151
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
152
|
+
});
|
|
153
|
+
expect(recount).toHaveBeenCalledTimes(1);
|
|
154
|
+
expect(getByText('274 characters')).toHaveStyle({
|
|
155
|
+
color: tokens.red600
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
it('does not recount for selection-only changes', async ()=>{
|
|
159
|
+
const { editor, getByText } = renderCounter('abc');
|
|
160
|
+
const recount = vi.spyOn(charCounterUtils, 'getTextContent');
|
|
161
|
+
for(let offset = 0; offset <= 3; offset++){
|
|
162
|
+
await act(async ()=>editor.tf.select({
|
|
163
|
+
path: [
|
|
164
|
+
0,
|
|
165
|
+
0
|
|
166
|
+
],
|
|
167
|
+
offset
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
await act(async ()=>{
|
|
171
|
+
await vi.advanceTimersByTimeAsync(350);
|
|
172
|
+
});
|
|
173
|
+
expect(recount).not.toHaveBeenCalled();
|
|
174
|
+
expect(getByText('3 characters')).toBeInTheDocument();
|
|
175
|
+
});
|
|
176
|
+
it('updates the delayed count without rendering or changing editor content', async ()=>{
|
|
177
|
+
const { editor, container, getByText, onContentRender } = renderCounter('abc');
|
|
178
|
+
await act(async ()=>{
|
|
179
|
+
editor.tf.select({
|
|
180
|
+
path: [
|
|
181
|
+
0,
|
|
182
|
+
0
|
|
183
|
+
],
|
|
184
|
+
offset: 3
|
|
185
|
+
});
|
|
186
|
+
editor.tf.insertText('d');
|
|
187
|
+
});
|
|
188
|
+
expect(getByText('3 characters')).toBeInTheDocument();
|
|
189
|
+
const content = container.querySelector('[data-slate-editor]');
|
|
190
|
+
const html = content.innerHTML;
|
|
191
|
+
const value = editor.children;
|
|
192
|
+
const selection = editor.selection;
|
|
193
|
+
onContentRender.mockClear();
|
|
194
|
+
await act(async ()=>{
|
|
195
|
+
await vi.advanceTimersByTimeAsync(300);
|
|
196
|
+
});
|
|
197
|
+
expect(getByText('4 characters')).toBeInTheDocument();
|
|
198
|
+
expect(onContentRender).not.toHaveBeenCalled();
|
|
199
|
+
expect(editor.children).toBe(value);
|
|
200
|
+
expect(editor.selection).toBe(selection);
|
|
201
|
+
expect(content.innerHTML).toBe(html);
|
|
202
|
+
});
|
|
203
|
+
it('renders the constraints without a counter plugin', ()=>{
|
|
204
|
+
const editor = createPlateEditor({
|
|
205
|
+
value: [
|
|
206
|
+
{
|
|
207
|
+
type: BLOCKS.PARAGRAPH,
|
|
208
|
+
children: [
|
|
209
|
+
{
|
|
210
|
+
text: 'abc'
|
|
211
|
+
}
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
],
|
|
215
|
+
plugins: [
|
|
216
|
+
createParagraphPlugin()
|
|
217
|
+
]
|
|
218
|
+
});
|
|
219
|
+
const { queryByText } = render(React.createElement(Plate, {
|
|
220
|
+
editor: editor
|
|
221
|
+
}, React.createElement(SdkProvider, {
|
|
222
|
+
sdk: sdk
|
|
223
|
+
}, React.createElement(CharConstraints, null))));
|
|
224
|
+
expect(queryByText('3 characters')).not.toBeInTheDocument();
|
|
225
|
+
});
|
|
226
|
+
});
|
|
@@ -70,10 +70,21 @@ const makeDoc = (n)=>node(BLOCKS.DOCUMENT, {}, Array.from({
|
|
|
70
70
|
]);
|
|
71
71
|
}));
|
|
72
72
|
describe('rich text performance', ()=>{
|
|
73
|
-
it(
|
|
73
|
+
it.each([
|
|
74
|
+
false,
|
|
75
|
+
true
|
|
76
|
+
])('typing only re-renders what changed (counter: %s)', async (withCharValidation)=>{
|
|
74
77
|
const [baseSdk] = newReferenceEditorFakeSdk();
|
|
75
78
|
const sdk = {
|
|
76
79
|
...baseSdk,
|
|
80
|
+
cma: {
|
|
81
|
+
...baseSdk.cma,
|
|
82
|
+
scheduledActions: {
|
|
83
|
+
getMany: async ()=>({
|
|
84
|
+
items: []
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
},
|
|
77
88
|
entry: {
|
|
78
89
|
getSys: ()=>({
|
|
79
90
|
id: 'perf-entry'
|
|
@@ -96,7 +107,8 @@ describe('rich text performance', ()=>{
|
|
|
96
107
|
onRender: ()=>commits++
|
|
97
108
|
}, React.createElement(ConnectedRichTextEditor, {
|
|
98
109
|
sdk: sdk,
|
|
99
|
-
value: makeDoc(BLOCK_COUNT)
|
|
110
|
+
value: makeDoc(BLOCK_COUNT),
|
|
111
|
+
withCharValidation: withCharValidation
|
|
100
112
|
})));
|
|
101
113
|
await act(async ()=>{
|
|
102
114
|
await new Promise((resolve)=>setTimeout(resolve, 50));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEditorRef, useEditorState
|
|
2
|
-
export { useReadOnly };
|
|
1
|
+
import { useEditorRef, useEditorState } from 'platejs/react';
|
|
2
|
+
export { useReadOnly, useEditorPluginOptions } from 'platejs/react';
|
|
3
3
|
export const usePlateEditorRef = (id)=>useEditorRef(id);
|
|
4
4
|
export const usePlateEditorState = (id)=>useEditorState(id);
|
|
@@ -12,6 +12,9 @@ export const withCharCounter = (editor)=>{
|
|
|
12
12
|
};
|
|
13
13
|
const recount = debounce(async ()=>{
|
|
14
14
|
count = getTextContent(editor).length;
|
|
15
|
+
editor.setOption({
|
|
16
|
+
key: 'char-counter'
|
|
17
|
+
}, 'count', count);
|
|
15
18
|
}, CHARACTER_COUNT_DEBOUNCE_TIME);
|
|
16
19
|
editor.apply = (op)=>{
|
|
17
20
|
apply(op);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { useReadOnly } from 'platejs/react';
|
|
2
1
|
import { PlateEditor } from './types';
|
|
3
|
-
export { useReadOnly };
|
|
2
|
+
export { useReadOnly, useEditorPluginOptions } from 'platejs/react';
|
|
4
3
|
export declare const usePlateEditorRef: (id?: string) => PlateEditor & {
|
|
5
4
|
store: import("jotai-x").ReturnOfUseStoreApi<import("@platejs/core/react").PlateStoreState<import("@platejs/core/react").PlateEditor>, {
|
|
6
5
|
trackedEditor: import("jotai").Atom<{
|
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import { PlateEditor } from '../../internal';
|
|
2
|
+
import type { PluginConfig } from '../../internal/plate';
|
|
3
|
+
export type CharCounterConfig = PluginConfig<'char-counter', {
|
|
4
|
+
count: number | undefined;
|
|
5
|
+
}>;
|
|
2
6
|
export declare const withCharCounter: (editor: PlateEditor) => PlateEditor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-rich-text",
|
|
3
|
-
"version": "6.6.13
|
|
3
|
+
"version": "6.6.13",
|
|
4
4
|
"source": "./src/index.tsx",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"@contentful/f36-icons": "^6.7.1",
|
|
47
47
|
"@contentful/f36-tokens": "^6.1.2",
|
|
48
48
|
"@contentful/f36-utils": "^6.1.0",
|
|
49
|
-
"@contentful/field-editor-reference": "^8.5.5
|
|
50
|
-
"@contentful/field-editor-shared": "^4.6.
|
|
49
|
+
"@contentful/field-editor-reference": "^8.5.5",
|
|
50
|
+
"@contentful/field-editor-shared": "^4.6.3",
|
|
51
51
|
"@contentful/rich-text-plain-text-renderer": "^17.2.1",
|
|
52
52
|
"@contentful/rich-text-types": "^17.2.7",
|
|
53
53
|
"@emotion/css": "^11.13.5",
|
|
@@ -96,5 +96,5 @@
|
|
|
96
96
|
"prettier --write"
|
|
97
97
|
]
|
|
98
98
|
},
|
|
99
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "af63f65d3de15f82d4fa356aaaaedf4d3a3a7f5b"
|
|
100
100
|
}
|