@contentful/field-editor-rich-text 2.0.0-next.11 → 2.0.0-next.12

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.
@@ -14,11 +14,11 @@ var plateCore = require('@udecode/plate-core');
14
14
  var emotion = require('emotion');
15
15
  var deepEquals = _interopDefault(require('fast-deep-equal'));
16
16
  var noop = _interopDefault(require('lodash/noop'));
17
- var slate = require('slate');
18
17
  var constate = _interopDefault(require('constate'));
19
18
  var plateSerializerDocx = require('@udecode/plate-serializer-docx');
20
19
  var plateBreak = require('@udecode/plate-break');
21
20
  var isHotkey = _interopDefault(require('is-hotkey'));
21
+ var slate = require('slate');
22
22
  var Slate = require('slate-react');
23
23
  var f36Components = require('@contentful/f36-components');
24
24
  var mimetype = _interopDefault(require('@contentful/mimetype'));
@@ -364,41 +364,6 @@ var _constate = /*#__PURE__*/constate(useContentfulEditorHook),
364
364
  ContentfulEditorProvider = _constate[0],
365
365
  useContentfulEditor = _constate[1];
366
366
 
367
- var isTextElement = function isTextElement(node) {
368
- return 'text' in node;
369
- };
370
- /**
371
- * Ensures incoming void nodes have a child leaf text element.
372
- */
373
-
374
-
375
- function sanitizeIncomingSlateDoc(nodes) {
376
- if (nodes === void 0) {
377
- nodes = [];
378
- }
379
-
380
- return nodes.map(function (node) {
381
- var _node$children;
382
-
383
- if (isTextElement(node)) {
384
- return node;
385
- }
386
-
387
- if (node.isVoid && ((_node$children = node.children) == null ? void 0 : _node$children.length) === 0) {
388
- return _extends({}, node, {
389
- children: [{
390
- text: '',
391
- data: {}
392
- }]
393
- });
394
- }
395
-
396
- return _extends({}, node, {
397
- children: sanitizeIncomingSlateDoc(node.children)
398
- });
399
- });
400
- }
401
-
402
367
  var createSoftBreakPlugin = function createSoftBreakPlugin() {
403
368
  return plateBreak.createSoftBreakPlugin({
404
369
  then: function then(editor) {
@@ -2691,7 +2656,7 @@ function extractNodes(editor, path, match) {
2691
2656
  return Array.from(plateCore.getNodes(editor, {
2692
2657
  match: match,
2693
2658
  at: path,
2694
- mode: 'all'
2659
+ mode: 'lowest'
2695
2660
  })).map(function (_ref) {
2696
2661
  var node = _ref[0];
2697
2662
  return node;
@@ -5975,61 +5940,79 @@ var StickyToolbarWrapper = function StickyToolbarWrapper(_ref) {
5975
5940
  }, children);
5976
5941
  };
5977
5942
 
5943
+ /**
5944
+ * For legacy reasons, a document may not have any content at all
5945
+ * e.g:
5946
+ *
5947
+ * {nodeType: document, data: {}, content: []}
5948
+ *
5949
+ * Rendering such document will break the Slate editor
5950
+ */
5951
+
5952
+ var hasContent = function hasContent(doc) {
5953
+ if (!doc) {
5954
+ return false;
5955
+ }
5956
+
5957
+ return doc.content.length > 0;
5958
+ };
5959
+
5960
+ var useNormalizedSlateValue = function useNormalizedSlateValue(_ref) {
5961
+ var id = _ref.id,
5962
+ incomingDoc = _ref.incomingDoc,
5963
+ plugins = _ref.plugins;
5964
+ return React.useMemo(function () {
5965
+ var editor = plateCore.createPlateEditor({
5966
+ id: id,
5967
+ plugins: plugins,
5968
+ disableCorePlugins: disableCorePlugins
5969
+ });
5970
+ var doc = contentfulSlatejsAdapter.toSlatejsDocument({
5971
+ document: hasContent(incomingDoc) ? incomingDoc : Contentful.EMPTY_DOCUMENT,
5972
+ schema: schema
5973
+ }); // Sets editor value & kicks normalization
5974
+
5975
+ slate.Transforms.insertNodes(editor, doc); // TODO: return the editor itself to avoid recompiling & initializing all
5976
+ // of the plugins again. It's currently not possible due to a bug in Plate
5977
+ // with initialValues
5978
+ // See: https://slate-js.slack.com/archives/C013QHXSCG1/p1645112799942819
5979
+
5980
+ return editor.children;
5981
+ }, [id, plugins, incomingDoc]);
5982
+ };
5983
+
5978
5984
  var _excluded = ["sdk", "isInitiallyDisabled", "onAction"];
5979
5985
  var ConnectedRichTextEditor = function ConnectedRichTextEditor(props) {
5986
+ var id = getContentfulEditorId(props.sdk);
5980
5987
  var tracking = useTrackingContext();
5981
- var editor = useContentfulEditor();
5982
-
5983
- var _useState = React.useState([]),
5984
- value = _useState[0],
5985
- setValue = _useState[1];
5986
-
5987
- var classNames = emotion.cx(styles$j.editor, props.minHeight !== undefined ? emotion.css({
5988
- minHeight: props.minHeight
5989
- }) : undefined, props.isDisabled ? styles$j.disabled : styles$j.enabled, props.isToolbarHidden && styles$j.hiddenToolbar);
5990
5988
  var plugins = React__default.useMemo(function () {
5991
5989
  return getPlugins(props.sdk, tracking);
5992
5990
  }, [props.sdk, tracking]);
5993
- React__default.useEffect(function () {
5994
- if (!editor) {
5995
- return;
5996
- }
5997
-
5998
- var docFromAdapter = contentfulSlatejsAdapter.toSlatejsDocument({
5999
- document: props.value || Contentful.EMPTY_DOCUMENT,
6000
- schema: schema
6001
- });
6002
- var doc = sanitizeIncomingSlateDoc(docFromAdapter); // Slate throws an error if the value on the initial render is invalid
6003
- // so we directly set the value on the editor in order
6004
- // to be able to trigger normalization on the initial value before rendering
6005
- // TODO: use https://plate.udecode.io/docs/Plate#normalizeinitialvalue when working
6006
-
6007
- editor.children = doc;
6008
- slate.Editor.normalize(editor, {
6009
- force: true
6010
- }); // We set the value so that the rendering can take over from here
6011
-
6012
- setValue(editor.children);
6013
- }, [props.value, editor]);
5991
+ var initialValue = useNormalizedSlateValue({
5992
+ id: id,
5993
+ incomingDoc: props.value,
5994
+ plugins: plugins
5995
+ });
5996
+ var classNames = emotion.cx(styles$j.editor, props.minHeight !== undefined ? emotion.css({
5997
+ minHeight: props.minHeight
5998
+ }) : undefined, props.isDisabled ? styles$j.disabled : styles$j.enabled, props.isToolbarHidden && styles$j.hiddenToolbar);
6014
5999
  return /*#__PURE__*/React__default.createElement("div", {
6015
6000
  className: styles$j.root,
6016
6001
  "data-test-id": "rich-text-editor"
6017
6002
  }, /*#__PURE__*/React__default.createElement(plateCore.Plate, {
6018
- id: getContentfulEditorId(props.sdk),
6019
- value: value,
6003
+ id: id,
6004
+ initialValue: initialValue,
6020
6005
  plugins: plugins,
6021
6006
  disableCorePlugins: disableCorePlugins,
6022
6007
  editableProps: {
6023
6008
  className: classNames,
6024
6009
  readOnly: props.isDisabled
6025
6010
  },
6026
- onChange: function onChange(slateDoc) {
6027
- setValue(slateDoc);
6028
- var contentfulDoc = contentfulSlatejsAdapter.toContentfulDocument({
6029
- document: slateDoc,
6011
+ onChange: function onChange(document) {
6012
+ props.onChange == null ? void 0 : props.onChange(contentfulSlatejsAdapter.toContentfulDocument({
6013
+ document: document,
6030
6014
  schema: schema
6031
- });
6032
- props.onChange == null ? void 0 : props.onChange(contentfulDoc);
6015
+ }));
6033
6016
  }
6034
6017
  }, !props.isToolbarHidden && /*#__PURE__*/React__default.createElement(StickyToolbarWrapper, {
6035
6018
  isDisabled: props.isDisabled