@contentful/field-editor-rich-text 3.6.0 → 3.6.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.
Files changed (35) hide show
  1. package/dist/cjs/RichTextEditor.js +42 -53
  2. package/dist/cjs/SyncEditorValue.js +107 -0
  3. package/dist/cjs/helpers/callbacks.js +35 -0
  4. package/dist/cjs/helpers/toSlateValue.js +51 -0
  5. package/dist/cjs/internal/hooks.js +12 -2
  6. package/dist/cjs/internal/misc.js +0 -6
  7. package/dist/cjs/plugins/Table/onKeyDownTable.js +14 -0
  8. package/dist/cjs/plugins/Text/__tests__/createTextPlugin.test.js +5 -15
  9. package/dist/cjs/plugins/index.js +0 -5
  10. package/dist/esm/RichTextEditor.js +37 -48
  11. package/dist/esm/SyncEditorValue.js +53 -0
  12. package/dist/esm/helpers/callbacks.js +20 -0
  13. package/dist/{cjs/helpers/sanitizeIncomingSlateDoc.js → esm/helpers/toSlateValue.js} +13 -10
  14. package/dist/esm/internal/hooks.js +9 -2
  15. package/dist/esm/internal/misc.js +0 -3
  16. package/dist/esm/plugins/Table/onKeyDownTable.js +15 -1
  17. package/dist/esm/plugins/Text/__tests__/createTextPlugin.test.js +5 -15
  18. package/dist/esm/plugins/index.js +0 -5
  19. package/dist/types/ContentfulEditorProvider.d.ts +2 -3
  20. package/dist/types/RichTextEditor.d.ts +2 -2
  21. package/dist/types/SyncEditorValue.d.ts +13 -0
  22. package/dist/types/helpers/callbacks.d.ts +3 -0
  23. package/dist/types/helpers/toSlateValue.d.ts +7 -0
  24. package/dist/types/internal/hooks.d.ts +4 -2
  25. package/dist/types/internal/misc.d.ts +2 -2
  26. package/dist/types/test-utils/createEditor.d.ts +2 -0
  27. package/package.json +13 -13
  28. package/dist/cjs/prepareDocument.js +0 -86
  29. package/dist/cjs/useOnValueChanged.js +0 -58
  30. package/dist/esm/helpers/sanitizeIncomingSlateDoc.js +0 -23
  31. package/dist/esm/prepareDocument.js +0 -57
  32. package/dist/esm/useOnValueChanged.js +0 -43
  33. package/dist/types/helpers/sanitizeIncomingSlateDoc.d.ts +0 -6
  34. package/dist/types/prepareDocument.d.ts +0 -19
  35. package/dist/types/useOnValueChanged.d.ts +0 -8
@@ -25,14 +25,14 @@ const _emotion = require("emotion");
25
25
  const _fastdeepequal = _interop_require_default(require("fast-deep-equal"));
26
26
  const _noop = _interop_require_default(require("lodash/noop"));
27
27
  const _ContentfulEditorProvider = require("./ContentfulEditorProvider");
28
- const _misc = require("./internal/misc");
28
+ const _callbacks = require("./helpers/callbacks");
29
+ const _toSlateValue = require("./helpers/toSlateValue");
29
30
  const _plugins = require("./plugins");
30
- const _prepareDocument = require("./prepareDocument");
31
31
  const _RichTextEditorstyles = require("./RichTextEditor.styles");
32
32
  const _SdkProvider = require("./SdkProvider");
33
+ const _SyncEditorValue = require("./SyncEditorValue");
33
34
  const _Toolbar = _interop_require_default(require("./Toolbar"));
34
35
  const _StickyToolbarWrapper = _interop_require_default(require("./Toolbar/components/StickyToolbarWrapper"));
35
- const _useOnValueChanged = require("./useOnValueChanged");
36
36
  function _interop_require_default(obj) {
37
37
  return obj && obj.__esModule ? obj : {
38
38
  default: obj
@@ -79,84 +79,73 @@ function _interop_require_wildcard(obj, nodeInterop) {
79
79
  }
80
80
  const ConnectedRichTextEditor = (props)=>{
81
81
  const id = (0, _ContentfulEditorProvider.getContentfulEditorId)(props.sdk);
82
- const plugins = _react.default.useMemo(()=>(0, _plugins.getPlugins)(props.sdk, props.onAction ?? _noop.default, props.restrictedMarks), [
82
+ const plugins = _react.useMemo(()=>(0, _plugins.getPlugins)(props.sdk, props.onAction ?? _noop.default, props.restrictedMarks), [
83
83
  props.sdk,
84
84
  props.onAction,
85
85
  props.restrictedMarks
86
86
  ]);
87
- const [isFirstRender, setIsFirstRender] = (0, _react.useState)(true);
88
- const [pendingExternalUpdate, setPendingExternalUpdate] = (0, _react.useState)(false);
89
- const onValueChanged = (0, _useOnValueChanged.useOnValueChanged)({
90
- editorId: id,
91
- handler: props.onChange,
92
- skip: pendingExternalUpdate || isFirstRender,
93
- onSkip: ()=>setPendingExternalUpdate(false)
94
- });
95
- (0, _react.useEffect)(()=>{
96
- setIsFirstRender(false);
97
- const editor = (0, _misc.getPlateSelectors)(id).editor();
98
- if (!editor) {
99
- return;
100
- }
101
- setPendingExternalUpdate(true);
102
- (0, _prepareDocument.setEditorContent)(editor, (0, _prepareDocument.documentToEditorValue)(props.value));
87
+ const handleChange = props.onChange;
88
+ const isFirstRender = _react.useRef(true);
89
+ const value = (0, _toSlateValue.toSlateValue)(props.value);
90
+ const onChange = _react.useMemo(()=>(0, _callbacks.createOnChangeCallback)((document)=>{
91
+ if (!isFirstRender.current && handleChange) {
92
+ handleChange(document);
93
+ }
94
+ }), [
95
+ handleChange
96
+ ]);
97
+ const firstInteractionHandler = _react.useCallback(()=>{
98
+ isFirstRender.current = false;
103
99
  }, [
104
- props.value,
105
- id
100
+ isFirstRender
106
101
  ]);
107
102
  const classNames = (0, _emotion.cx)(_RichTextEditorstyles.styles.editor, props.minHeight !== undefined ? (0, _emotion.css)({
108
103
  minHeight: props.minHeight
109
104
  }) : undefined, props.isDisabled ? _RichTextEditorstyles.styles.disabled : _RichTextEditorstyles.styles.enabled, props.isToolbarHidden && _RichTextEditorstyles.styles.hiddenToolbar);
110
- (0, _react.useEffect)(()=>{
111
- if (!isFirstRender) {
112
- return;
113
- }
114
- (0, _platecore.getPlateActions)(id).value((0, _prepareDocument.normalizeEditorValue)((0, _prepareDocument.documentToEditorValue)(props.value), {
115
- plugins,
116
- disableCorePlugins: _plugins.disableCorePlugins
117
- }));
118
- }, [
119
- isFirstRender,
120
- plugins,
121
- id,
122
- props.value
123
- ]);
124
- return _react.default.createElement(_SdkProvider.SdkProvider, {
105
+ return _react.createElement(_SdkProvider.SdkProvider, {
125
106
  sdk: props.sdk
126
- }, _react.default.createElement(_ContentfulEditorProvider.ContentfulEditorIdProvider, {
107
+ }, _react.createElement(_ContentfulEditorProvider.ContentfulEditorIdProvider, {
127
108
  value: id
128
- }, _react.default.createElement("div", {
109
+ }, _react.createElement("div", {
129
110
  className: _RichTextEditorstyles.styles.root,
130
111
  "data-test-id": "rich-text-editor"
131
- }, _react.default.createElement(_platecore.Plate, {
112
+ }, _react.createElement(_platecore.PlateProvider, {
132
113
  id: id,
114
+ initialValue: value,
115
+ normalizeInitialValue: true,
133
116
  plugins: plugins,
134
117
  disableCorePlugins: _plugins.disableCorePlugins,
118
+ onChange: onChange
119
+ }, !props.isToolbarHidden && _react.createElement(_StickyToolbarWrapper.default, {
120
+ isDisabled: props.isDisabled
121
+ }, _react.createElement(_Toolbar.default, {
122
+ isDisabled: props.isDisabled
123
+ })), _react.createElement(_SyncEditorValue.SyncEditorValue, {
124
+ incomingValue: value
125
+ }), _react.createElement(_platecore.Plate, {
126
+ id: id,
135
127
  editableProps: {
136
128
  className: classNames,
137
- readOnly: props.isDisabled
138
- },
139
- onChange: onValueChanged,
140
- firstChildren: !props.isToolbarHidden && _react.default.createElement(_StickyToolbarWrapper.default, {
141
- isDisabled: props.isDisabled
142
- }, _react.default.createElement(_Toolbar.default, {
143
- isDisabled: props.isDisabled
144
- }))
145
- }))));
129
+ readOnly: props.isDisabled,
130
+ onKeyDown: firstInteractionHandler,
131
+ onChange: firstInteractionHandler,
132
+ onClick: firstInteractionHandler
133
+ }
134
+ })))));
146
135
  };
147
136
  const RichTextEditor = (props)=>{
148
137
  const { sdk , isInitiallyDisabled , onAction , restrictedMarks , ...otherProps } = props;
149
- const isEmptyValue = (0, _react.useCallback)((value)=>!value || (0, _fastdeepequal.default)(value, _richtexttypes.EMPTY_DOCUMENT), []);
138
+ const isEmptyValue = _react.useCallback((value)=>!value || (0, _fastdeepequal.default)(value, _richtexttypes.EMPTY_DOCUMENT), []);
150
139
  const id = (0, _ContentfulEditorProvider.getContentfulEditorId)(props.sdk);
151
- return _react.default.createElement(_fieldeditorreference.EntityProvider, {
140
+ return _react.createElement(_fieldeditorreference.EntityProvider, {
152
141
  sdk: sdk
153
- }, _react.default.createElement(_fieldeditorshared.FieldConnector, {
142
+ }, _react.createElement(_fieldeditorshared.FieldConnector, {
154
143
  throttle: 0,
155
144
  field: sdk.field,
156
145
  isInitiallyDisabled: isInitiallyDisabled,
157
146
  isEmptyValue: isEmptyValue,
158
147
  isEqualValues: _fastdeepequal.default
159
- }, ({ lastRemoteValue , disabled , setValue })=>_react.default.createElement(ConnectedRichTextEditor, {
148
+ }, ({ lastRemoteValue , disabled , setValue })=>_react.createElement(ConnectedRichTextEditor, {
160
149
  ...otherProps,
161
150
  key: `rich-text-editor-${id}`,
162
151
  value: lastRemoteValue,
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "SyncEditorValue", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return SyncEditorValue;
9
+ }
10
+ });
11
+ const _react = _interop_require_wildcard(require("react"));
12
+ const _fastdeepequal = _interop_require_default(require("fast-deep-equal"));
13
+ const _hooks = require("./internal/hooks");
14
+ const _misc = require("./internal/misc");
15
+ const _queries = require("./internal/queries");
16
+ const _transforms = require("./internal/transforms");
17
+ function _interop_require_default(obj) {
18
+ return obj && obj.__esModule ? obj : {
19
+ default: obj
20
+ };
21
+ }
22
+ function _getRequireWildcardCache(nodeInterop) {
23
+ if (typeof WeakMap !== "function") return null;
24
+ var cacheBabelInterop = new WeakMap();
25
+ var cacheNodeInterop = new WeakMap();
26
+ return (_getRequireWildcardCache = function(nodeInterop) {
27
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
28
+ })(nodeInterop);
29
+ }
30
+ function _interop_require_wildcard(obj, nodeInterop) {
31
+ if (!nodeInterop && obj && obj.__esModule) {
32
+ return obj;
33
+ }
34
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
35
+ return {
36
+ default: obj
37
+ };
38
+ }
39
+ var cache = _getRequireWildcardCache(nodeInterop);
40
+ if (cache && cache.has(obj)) {
41
+ return cache.get(obj);
42
+ }
43
+ var newObj = {};
44
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
45
+ for(var key in obj){
46
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
47
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
48
+ if (desc && (desc.get || desc.set)) {
49
+ Object.defineProperty(newObj, key, desc);
50
+ } else {
51
+ newObj[key] = obj[key];
52
+ }
53
+ }
54
+ }
55
+ newObj.default = obj;
56
+ if (cache) {
57
+ cache.set(obj, newObj);
58
+ }
59
+ return newObj;
60
+ }
61
+ const setEditorContent = (editor, nodes)=>{
62
+ (0, _misc.withoutNormalizing)(editor, ()=>{
63
+ const children = [
64
+ ...editor.children
65
+ ];
66
+ children.forEach((node)=>editor.apply({
67
+ type: 'remove_node',
68
+ path: [
69
+ 0
70
+ ],
71
+ node
72
+ }));
73
+ if (nodes) {
74
+ const nodesArray = (0, _queries.isNode)(nodes) ? [
75
+ nodes
76
+ ] : nodes;
77
+ nodesArray.forEach((node, i)=>{
78
+ editor.apply({
79
+ type: 'insert_node',
80
+ path: [
81
+ i
82
+ ],
83
+ node
84
+ });
85
+ });
86
+ }
87
+ const point = (0, _queries.getEndPoint)(editor, []);
88
+ if (point) {
89
+ (0, _transforms.select)(editor, point);
90
+ }
91
+ });
92
+ };
93
+ const SyncEditorValue = ({ incomingValue })=>{
94
+ const editor = (0, _hooks.usePlateSelectors)().editor();
95
+ const lastIncomingValue = _react.useRef(incomingValue);
96
+ _react.useEffect(()=>{
97
+ if ((0, _fastdeepequal.default)(lastIncomingValue.current, incomingValue)) {
98
+ return;
99
+ }
100
+ lastIncomingValue.current = incomingValue;
101
+ setEditorContent(editor, incomingValue);
102
+ }, [
103
+ editor,
104
+ incomingValue
105
+ ]);
106
+ return null;
107
+ };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "createOnChangeCallback", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return createOnChangeCallback;
9
+ }
10
+ });
11
+ const _contentfulslatejsadapter = require("@contentful/contentful-slatejs-adapter");
12
+ const _fastdeepequal = _interop_require_default(require("fast-deep-equal"));
13
+ const _debounce = _interop_require_default(require("lodash/debounce"));
14
+ const _Schema = _interop_require_default(require("../constants/Schema"));
15
+ const _removeInternalMarks = require("./removeInternalMarks");
16
+ function _interop_require_default(obj) {
17
+ return obj && obj.__esModule ? obj : {
18
+ default: obj
19
+ };
20
+ }
21
+ const createOnChangeCallback = (handler)=>{
22
+ let cache = null;
23
+ return (0, _debounce.default)((document)=>{
24
+ if ((0, _fastdeepequal.default)(document, cache)) {
25
+ return;
26
+ }
27
+ cache = document;
28
+ const doc = (0, _removeInternalMarks.removeInternalMarks)((0, _contentfulslatejsadapter.toContentfulDocument)({
29
+ document: document,
30
+ schema: _Schema.default
31
+ }));
32
+ const cleanedDocument = (0, _removeInternalMarks.removeInternalMarks)(doc);
33
+ handler?.(cleanedDocument);
34
+ }, 500);
35
+ };
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "toSlateValue", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return toSlateValue;
9
+ }
10
+ });
11
+ const _contentfulslatejsadapter = require("@contentful/contentful-slatejs-adapter");
12
+ const _richtexttypes = require("@contentful/rich-text-types");
13
+ const _Schema = _interop_require_default(require("../constants/Schema"));
14
+ function _interop_require_default(obj) {
15
+ return obj && obj.__esModule ? obj : {
16
+ default: obj
17
+ };
18
+ }
19
+ const isTextElement = (node)=>'text' in node;
20
+ function sanitizeIncomingSlateDoc(nodes = []) {
21
+ return nodes.map((node)=>{
22
+ if (isTextElement(node)) {
23
+ return node;
24
+ }
25
+ if (node.children?.length === 0) {
26
+ return {
27
+ ...node,
28
+ children: [
29
+ {
30
+ text: '',
31
+ data: {}
32
+ }
33
+ ]
34
+ };
35
+ }
36
+ return {
37
+ ...node,
38
+ children: sanitizeIncomingSlateDoc(node?.children)
39
+ };
40
+ });
41
+ }
42
+ const toSlateValue = (doc)=>{
43
+ const hasContent = (doc)=>{
44
+ return (doc?.content || []).length > 0;
45
+ };
46
+ const slateDoc = (0, _contentfulslatejsadapter.toSlatejsDocument)({
47
+ document: doc && hasContent(doc) ? doc : _richtexttypes.EMPTY_DOCUMENT,
48
+ schema: _Schema.default
49
+ });
50
+ return sanitizeIncomingSlateDoc(slateDoc);
51
+ };
@@ -17,6 +17,9 @@ _export(exports, {
17
17
  },
18
18
  usePlateEditorState: function() {
19
19
  return usePlateEditorState;
20
+ },
21
+ usePlateSelectors: function() {
22
+ return usePlateSelectors;
20
23
  }
21
24
  });
22
25
  const _platecore = _interop_require_wildcard(require("@udecode/plate-core"));
@@ -61,5 +64,12 @@ function _interop_require_wildcard(obj, nodeInterop) {
61
64
  return newObj;
62
65
  }
63
66
  const useReadOnly = _slatereact.useReadOnly;
64
- const usePlateEditorRef = _platecore.usePlateEditorRef;
65
- const usePlateEditorState = _platecore.usePlateEditorState;
67
+ const usePlateEditorRef = (id)=>{
68
+ return _platecore.usePlateEditorRef(id);
69
+ };
70
+ const usePlateEditorState = (id)=>{
71
+ return _platecore.usePlateEditorState(id);
72
+ };
73
+ const usePlateSelectors = (id)=>{
74
+ return _platecore.usePlateSelectors(id);
75
+ };
@@ -29,9 +29,6 @@ _export(exports, {
29
29
  },
30
30
  mockPlugin: function() {
31
31
  return mockPlugin;
32
- },
33
- getPlateSelectors: function() {
34
- return getPlateSelectors;
35
32
  }
36
33
  });
37
34
  const _platecore = _interop_require_wildcard(require("@udecode/plate-core"));
@@ -98,6 +95,3 @@ const fromDOMPoint = (editor, domPoint, opts = {
98
95
  const mockPlugin = (plugin)=>{
99
96
  return _platecore.mockPlugin(plugin);
100
97
  };
101
- const getPlateSelectors = (id)=>{
102
- return _platecore.getPlateSelectors(id);
103
- };
@@ -40,6 +40,20 @@ const onKeyDownTable = (editor, plugin)=>{
40
40
  return;
41
41
  }
42
42
  }
43
+ if (event.key === 'Backspace') {
44
+ const entry = (0, _platetable.getTableEntries)(editor, {});
45
+ if (entry) {
46
+ const { table , row , cell } = entry;
47
+ const cellText = (0, _queries.getText)(editor, cell[1]);
48
+ const isFirstCell = (0, _queries.isFirstChild)(row[1]);
49
+ const isFirstRow = (0, _queries.isFirstChild)(table[1]);
50
+ if (isFirstCell && isFirstRow && !cellText) {
51
+ event.preventDefault();
52
+ event.stopPropagation();
53
+ return;
54
+ }
55
+ }
56
+ }
43
57
  if (event.key === 'Tab' && !event.shiftKey) {
44
58
  event.preventDefault();
45
59
  const entry = (0, _platetable.getTableEntries)(editor, {});
@@ -11,19 +11,9 @@ describe('delete backward', ()=>{
11
11
  expected: (0, _testutils.jsx)("hul", null, (0, _testutils.jsx)("hli", null, (0, _testutils.jsx)("hp", null, "p", (0, _testutils.jsx)("cursor", null))))
12
12
  },
13
13
  {
14
- title: 'deletes the empty paragraph at the beginning of the RTE followed by another paragraph',
14
+ title: 'does not delete the very first paragraph',
15
15
  input: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null)), (0, _testutils.jsx)("hp", null, "text")),
16
- expected: (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null), "text")
17
- },
18
- {
19
- title: 'deletes the empty paragraph at the beginning of the RTE followed by li',
20
- input: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null)), (0, _testutils.jsx)("hul", null, (0, _testutils.jsx)("hli", null, (0, _testutils.jsx)("hp", null, "p1")))),
21
- expected: (0, _testutils.jsx)("hul", null, (0, _testutils.jsx)("hli", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null), "p1")))
22
- },
23
- {
24
- title: 'deletes the empty paragraph at the beginning of the RTE followed by a blockquote',
25
- input: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null)), (0, _testutils.jsx)("hquote", null, (0, _testutils.jsx)("hp", null, "p1"))),
26
- expected: (0, _testutils.jsx)("hquote", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null), "p1"))
16
+ expected: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null)), (0, _testutils.jsx)("hp", null, "text"))
27
17
  }
28
18
  ];
29
19
  const render = (children)=>(0, _testutils.jsx)("editor", null, children, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("htext", null)));
@@ -48,17 +38,17 @@ describe('delete forward', ()=>{
48
38
  expected: (0, _testutils.jsx)("hul", null, (0, _testutils.jsx)("hli", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null), "1")))
49
39
  },
50
40
  {
51
- title: 'deletes the empty paragraph at the beginning of the RTE followed by another paragraph',
41
+ title: 'deletes the first paragraph when followed by another paragraph',
52
42
  input: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null)), (0, _testutils.jsx)("hp", null, "text")),
53
43
  expected: (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null), "text")
54
44
  },
55
45
  {
56
- title: 'deletes the empty paragraph at the beginning of the RTE followed by li',
46
+ title: 'deletes the first paragraph when followed by li',
57
47
  input: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null)), (0, _testutils.jsx)("hul", null, (0, _testutils.jsx)("hli", null, (0, _testutils.jsx)("hp", null, "p1")))),
58
48
  expected: (0, _testutils.jsx)("hul", null, (0, _testutils.jsx)("hli", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null), "p1")))
59
49
  },
60
50
  {
61
- title: 'deletes the empty paragraph at the beginning of the RTE followed by a blockquote',
51
+ title: 'deletes the first paragraph when followed by a blockquote',
62
52
  input: (0, _testutils.jsx)("fragment", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null)), (0, _testutils.jsx)("hquote", null, (0, _testutils.jsx)("hp", null, "p1"))),
63
53
  expected: (0, _testutils.jsx)("hquote", null, (0, _testutils.jsx)("hp", null, (0, _testutils.jsx)("cursor", null), "p1"))
64
54
  }
@@ -16,7 +16,6 @@ _export(exports, {
16
16
  return disableCorePlugins;
17
17
  }
18
18
  });
19
- const _platecore = require("@udecode/plate-core");
20
19
  const _plateserializerdocx = require("@udecode/plate-serializer-docx");
21
20
  const _Break = require("./Break");
22
21
  const _CommandPalette = require("./CommandPalette");
@@ -41,8 +40,6 @@ const _Tracking = require("./Tracking");
41
40
  const _TrailingParagraph = require("./TrailingParagraph");
42
41
  const _Voids = require("./Voids");
43
42
  const getPlugins = (sdk, onAction, restrictedMarks)=>[
44
- (0, _platecore.createDeserializeHtmlPlugin)(),
45
- (0, _platecore.createDeserializeAstPlugin)(),
46
43
  (0, _plateserializerdocx.createDeserializeDocxPlugin)(),
47
44
  (0, _Tracking.createTrackingPlugin)(onAction),
48
45
  (0, _DragAndDrop.createDragAndDropPlugin)(),
@@ -72,7 +69,5 @@ const getPlugins = (sdk, onAction, restrictedMarks)=>[
72
69
  (0, _Normalizer.createNormalizerPlugin)()
73
70
  ];
74
71
  const disableCorePlugins = {
75
- deserializeAst: true,
76
- deserializeHtml: true,
77
72
  eventEditor: true
78
73
  };
@@ -1,20 +1,20 @@
1
- import React, { useCallback, useState, useEffect } from 'react';
1
+ import * as React from 'react';
2
2
  import { EntityProvider } from '@contentful/field-editor-reference';
3
3
  import { FieldConnector } from '@contentful/field-editor-shared';
4
4
  import * as Contentful from '@contentful/rich-text-types';
5
- import { Plate, getPlateActions } from '@udecode/plate-core';
5
+ import { Plate, PlateProvider } from '@udecode/plate-core';
6
6
  import { css, cx } from 'emotion';
7
7
  import deepEquals from 'fast-deep-equal';
8
8
  import noop from 'lodash/noop';
9
9
  import { ContentfulEditorIdProvider, getContentfulEditorId } from './ContentfulEditorProvider';
10
- import { getPlateSelectors } from './internal/misc';
10
+ import { createOnChangeCallback } from './helpers/callbacks';
11
+ import { toSlateValue } from './helpers/toSlateValue';
11
12
  import { getPlugins, disableCorePlugins } from './plugins';
12
- import { documentToEditorValue, normalizeEditorValue, setEditorContent } from './prepareDocument';
13
13
  import { styles } from './RichTextEditor.styles';
14
14
  import { SdkProvider } from './SdkProvider';
15
+ import { SyncEditorValue } from './SyncEditorValue';
15
16
  import Toolbar from './Toolbar';
16
17
  import StickyToolbarWrapper from './Toolbar/components/StickyToolbarWrapper';
17
- import { useOnValueChanged } from './useOnValueChanged';
18
18
  export const ConnectedRichTextEditor = (props)=>{
19
19
  const id = getContentfulEditorId(props.sdk);
20
20
  const plugins = React.useMemo(()=>getPlugins(props.sdk, props.onAction ?? noop, props.restrictedMarks), [
@@ -22,43 +22,24 @@ export const ConnectedRichTextEditor = (props)=>{
22
22
  props.onAction,
23
23
  props.restrictedMarks
24
24
  ]);
25
- const [isFirstRender, setIsFirstRender] = useState(true);
26
- const [pendingExternalUpdate, setPendingExternalUpdate] = useState(false);
27
- const onValueChanged = useOnValueChanged({
28
- editorId: id,
29
- handler: props.onChange,
30
- skip: pendingExternalUpdate || isFirstRender,
31
- onSkip: ()=>setPendingExternalUpdate(false)
32
- });
33
- useEffect(()=>{
34
- setIsFirstRender(false);
35
- const editor = getPlateSelectors(id).editor();
36
- if (!editor) {
37
- return;
38
- }
39
- setPendingExternalUpdate(true);
40
- setEditorContent(editor, documentToEditorValue(props.value));
25
+ const handleChange = props.onChange;
26
+ const isFirstRender = React.useRef(true);
27
+ const value = toSlateValue(props.value);
28
+ const onChange = React.useMemo(()=>createOnChangeCallback((document)=>{
29
+ if (!isFirstRender.current && handleChange) {
30
+ handleChange(document);
31
+ }
32
+ }), [
33
+ handleChange
34
+ ]);
35
+ const firstInteractionHandler = React.useCallback(()=>{
36
+ isFirstRender.current = false;
41
37
  }, [
42
- props.value,
43
- id
38
+ isFirstRender
44
39
  ]);
45
40
  const classNames = cx(styles.editor, props.minHeight !== undefined ? css({
46
41
  minHeight: props.minHeight
47
42
  }) : undefined, props.isDisabled ? styles.disabled : styles.enabled, props.isToolbarHidden && styles.hiddenToolbar);
48
- useEffect(()=>{
49
- if (!isFirstRender) {
50
- return;
51
- }
52
- getPlateActions(id).value(normalizeEditorValue(documentToEditorValue(props.value), {
53
- plugins,
54
- disableCorePlugins
55
- }));
56
- }, [
57
- isFirstRender,
58
- plugins,
59
- id,
60
- props.value
61
- ]);
62
43
  return React.createElement(SdkProvider, {
63
44
  sdk: props.sdk
64
45
  }, React.createElement(ContentfulEditorIdProvider, {
@@ -66,25 +47,33 @@ export const ConnectedRichTextEditor = (props)=>{
66
47
  }, React.createElement("div", {
67
48
  className: styles.root,
68
49
  "data-test-id": "rich-text-editor"
69
- }, React.createElement(Plate, {
50
+ }, React.createElement(PlateProvider, {
70
51
  id: id,
52
+ initialValue: value,
53
+ normalizeInitialValue: true,
71
54
  plugins: plugins,
72
55
  disableCorePlugins: disableCorePlugins,
56
+ onChange: onChange
57
+ }, !props.isToolbarHidden && React.createElement(StickyToolbarWrapper, {
58
+ isDisabled: props.isDisabled
59
+ }, React.createElement(Toolbar, {
60
+ isDisabled: props.isDisabled
61
+ })), React.createElement(SyncEditorValue, {
62
+ incomingValue: value
63
+ }), React.createElement(Plate, {
64
+ id: id,
73
65
  editableProps: {
74
66
  className: classNames,
75
- readOnly: props.isDisabled
76
- },
77
- onChange: onValueChanged,
78
- firstChildren: !props.isToolbarHidden && React.createElement(StickyToolbarWrapper, {
79
- isDisabled: props.isDisabled
80
- }, React.createElement(Toolbar, {
81
- isDisabled: props.isDisabled
82
- }))
83
- }))));
67
+ readOnly: props.isDisabled,
68
+ onKeyDown: firstInteractionHandler,
69
+ onChange: firstInteractionHandler,
70
+ onClick: firstInteractionHandler
71
+ }
72
+ })))));
84
73
  };
85
74
  const RichTextEditor = (props)=>{
86
75
  const { sdk , isInitiallyDisabled , onAction , restrictedMarks , ...otherProps } = props;
87
- const isEmptyValue = useCallback((value)=>!value || deepEquals(value, Contentful.EMPTY_DOCUMENT), []);
76
+ const isEmptyValue = React.useCallback((value)=>!value || deepEquals(value, Contentful.EMPTY_DOCUMENT), []);
88
77
  const id = getContentfulEditorId(props.sdk);
89
78
  return React.createElement(EntityProvider, {
90
79
  sdk: sdk
@@ -0,0 +1,53 @@
1
+ import * as React from 'react';
2
+ import equal from 'fast-deep-equal';
3
+ import { usePlateSelectors } from './internal/hooks';
4
+ import { withoutNormalizing } from './internal/misc';
5
+ import { isNode, getEndPoint } from './internal/queries';
6
+ import { select } from './internal/transforms';
7
+ const setEditorContent = (editor, nodes)=>{
8
+ withoutNormalizing(editor, ()=>{
9
+ const children = [
10
+ ...editor.children
11
+ ];
12
+ children.forEach((node)=>editor.apply({
13
+ type: 'remove_node',
14
+ path: [
15
+ 0
16
+ ],
17
+ node
18
+ }));
19
+ if (nodes) {
20
+ const nodesArray = isNode(nodes) ? [
21
+ nodes
22
+ ] : nodes;
23
+ nodesArray.forEach((node, i)=>{
24
+ editor.apply({
25
+ type: 'insert_node',
26
+ path: [
27
+ i
28
+ ],
29
+ node
30
+ });
31
+ });
32
+ }
33
+ const point = getEndPoint(editor, []);
34
+ if (point) {
35
+ select(editor, point);
36
+ }
37
+ });
38
+ };
39
+ export const SyncEditorValue = ({ incomingValue })=>{
40
+ const editor = usePlateSelectors().editor();
41
+ const lastIncomingValue = React.useRef(incomingValue);
42
+ React.useEffect(()=>{
43
+ if (equal(lastIncomingValue.current, incomingValue)) {
44
+ return;
45
+ }
46
+ lastIncomingValue.current = incomingValue;
47
+ setEditorContent(editor, incomingValue);
48
+ }, [
49
+ editor,
50
+ incomingValue
51
+ ]);
52
+ return null;
53
+ };