@contentful/field-editor-rich-text 2.0.0-next.24 → 2.0.0-next.27

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.
@@ -461,7 +461,7 @@ function moveToTheNextChar(editor) {
461
461
  unit: 'character'
462
462
  });
463
463
  }
464
- function insertEmptyParagraph(editor) {
464
+ function insertEmptyParagraph(editor, options) {
465
465
  var emptyParagraph = {
466
466
  type: Contentful.BLOCKS.PARAGRAPH,
467
467
  children: [{
@@ -470,7 +470,7 @@ function insertEmptyParagraph(editor) {
470
470
  data: {},
471
471
  isVoid: false
472
472
  };
473
- slate.Transforms.insertNodes(editor, emptyParagraph);
473
+ slate.Transforms.insertNodes(editor, emptyParagraph, options);
474
474
  }
475
475
  function getElementFromCurrentSelection(editor) {
476
476
  if (!editor.selection) return [];
@@ -3868,37 +3868,44 @@ var moveListItems = function moveListItems(editor, _temp) {
3868
3868
  * Credit: Modified version of Plate's list plugin
3869
3869
  * See: https://github.com/udecode/plate/blob/main/packages/nodes/list
3870
3870
  */
3871
- var listTypes = [Contentful.BLOCKS.UL_LIST, Contentful.BLOCKS.OL_LIST];
3871
+
3872
+ function hasUnliftedListItems(editor, at) {
3873
+ return slate.Editor.nodes(editor, {
3874
+ at: at,
3875
+ match: function match(node, path) {
3876
+ return slate.Element.isElement(node) && node.type === Contentful.BLOCKS.LIST_ITEM && path.length >= 2;
3877
+ }
3878
+ }).next().done;
3879
+ }
3880
+
3872
3881
  var unwrapList = function unwrapList(editor, _temp) {
3873
3882
  var _ref = _temp === void 0 ? {} : _temp,
3874
3883
  at = _ref.at;
3875
3884
 
3876
3885
  slate.Editor.withoutNormalizing(editor, function () {
3877
3886
  do {
3878
- plateCore.unwrapNodes(editor, {
3887
+ // lift list items to the root level
3888
+ slate.Transforms.liftNodes(editor, {
3879
3889
  at: at,
3880
- match: {
3881
- type: Contentful.BLOCKS.LIST_ITEM
3882
- },
3883
- split: true
3884
- });
3885
- plateCore.unwrapNodes(editor, {
3886
- at: at,
3887
- match: {
3888
- type: listTypes
3890
+ match: function match(node) {
3891
+ return slate.Element.isElement(node) && node.type === Contentful.BLOCKS.LIST_ITEM;
3889
3892
  },
3890
- split: true
3893
+ mode: 'lowest'
3891
3894
  });
3892
- } while (plateCore.getAbove(editor, {
3895
+ } while (!hasUnliftedListItems(editor, at)); // finally unwrap all lifted items
3896
+
3897
+
3898
+ plateCore.unwrapNodes(editor, {
3899
+ at: at,
3893
3900
  match: {
3894
- type: listTypes,
3895
- at: at
3896
- }
3897
- }));
3901
+ type: Contentful.BLOCKS.LIST_ITEM
3902
+ },
3903
+ split: false
3904
+ });
3898
3905
  });
3899
3906
  };
3900
3907
 
3901
- var listTypes$1 = [Contentful.BLOCKS.UL_LIST, Contentful.BLOCKS.OL_LIST];
3908
+ var listTypes = [Contentful.BLOCKS.UL_LIST, Contentful.BLOCKS.OL_LIST];
3902
3909
  var toggleList = function toggleList(editor, _ref) {
3903
3910
  var type = _ref.type;
3904
3911
  return slate.Editor.withoutNormalizing(editor, function () {
@@ -3919,7 +3926,7 @@ var toggleList = function toggleList(editor, _ref) {
3919
3926
  }, {
3920
3927
  at: editor.selection,
3921
3928
  match: function match(n) {
3922
- return listTypes$1.includes(n.type);
3929
+ return listTypes.includes(n.type);
3923
3930
  },
3924
3931
  mode: 'lowest'
3925
3932
  });
@@ -3960,19 +3967,25 @@ var toggleList = function toggleList(editor, _ref) {
3960
3967
 
3961
3968
  var commonEntry = slate.Node.common(editor, startPoint.path, endPoint.path);
3962
3969
 
3963
- if (listTypes$1.includes(commonEntry[0].type) || commonEntry[0].type === Contentful.BLOCKS.LIST_ITEM) {
3964
- if (commonEntry[0].type !== type) {
3970
+ if (listTypes.includes(commonEntry[0].type) || commonEntry[0].type === Contentful.BLOCKS.LIST_ITEM) {
3971
+ var listType = commonEntry[0].type;
3972
+
3973
+ if (commonEntry[0].type === Contentful.BLOCKS.LIST_ITEM) {
3974
+ listType = slate.Editor.parent(editor, commonEntry[1])[0].type;
3975
+ }
3976
+
3977
+ if (listType !== type) {
3965
3978
  var startList = plateCore.findNode(editor, {
3966
3979
  at: slate.Range.start(editor.selection),
3967
3980
  match: {
3968
- type: listTypes$1
3981
+ type: listTypes
3969
3982
  },
3970
3983
  mode: 'lowest'
3971
3984
  });
3972
3985
  var endList = plateCore.findNode(editor, {
3973
3986
  at: slate.Range.end(editor.selection),
3974
3987
  match: {
3975
- type: listTypes$1
3988
+ type: listTypes
3976
3989
  },
3977
3990
  mode: 'lowest'
3978
3991
  });
@@ -3987,7 +4000,7 @@ var toggleList = function toggleList(editor, _ref) {
3987
4000
  }, {
3988
4001
  at: editor.selection,
3989
4002
  match: function match(n, path) {
3990
- return listTypes$1.includes(n.type) && path.length >= rangeLength;
4003
+ return listTypes.includes(n.type) && path.length >= rangeLength;
3991
4004
  },
3992
4005
  mode: 'all'
3993
4006
  });
@@ -4005,7 +4018,7 @@ var toggleList = function toggleList(editor, _ref) {
4005
4018
  }).reverse();
4006
4019
 
4007
4020
  _nodes.forEach(function (n) {
4008
- if (listTypes$1.includes(n[0].type)) {
4021
+ if (listTypes.includes(n[0].type)) {
4009
4022
  plateCore.setNodes(editor, {
4010
4023
  type: type
4011
4024
  }, {
@@ -5221,9 +5234,9 @@ var sanitizeHTML = function sanitizeHTML(html) {
5221
5234
  // Parse the HTML string and pipe it through our transformers
5222
5235
  var doc = transformers.reduce(function (value, cb) {
5223
5236
  return cb(value);
5224
- }, new DOMParser().parseFromString(html, 'text/html')); // Remove whitespace between tags
5225
-
5226
- return doc.body.innerHTML.replace(/>\s+</g, '><');
5237
+ }, new DOMParser().parseFromString(html, 'text/html'));
5238
+ return doc.body.innerHTML.replace(/>\s+</g, '><') // Remove whitespace between tags
5239
+ .replace(/(.*)<div.*>(<table.*<\/table>)<\/div>(.*)/g, '$1$2$3'); // remove div containers from tables
5227
5240
  };
5228
5241
 
5229
5242
  /**
@@ -5764,6 +5777,9 @@ var isNotEmpty = function isNotEmpty(editor, _ref4) {
5764
5777
  var path = _ref4[1];
5765
5778
  return Array.from(slate.Node.children(editor, path)).length !== 0;
5766
5779
  };
5780
+ var isTable = function isTable(node) {
5781
+ return slate.Element.isElement(node) && node.type === Contentful.BLOCKS.TABLE;
5782
+ };
5767
5783
 
5768
5784
  var styles$i = {
5769
5785
  topRight: /*#__PURE__*/emotion.css({
@@ -5907,9 +5923,11 @@ var Row = function Row(props) {
5907
5923
  var _templateObject$a;
5908
5924
  var style$5 = /*#__PURE__*/emotion.css(_templateObject$a || (_templateObject$a = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n margin-bottom: 1.5em;\n border-collapse: collapse;\n border-radius: 5px;\n border-style: hidden;\n box-shadow: 0 0 0 1px ", ";\n width: 100%;\n table-layout: fixed;\n overflow: hidden;\n"])), tokens.gray400);
5909
5925
  var Table = function Table(props) {
5910
- return /*#__PURE__*/React.createElement("table", Object.assign({}, props.attributes, {
5926
+ return /*#__PURE__*/React.createElement("div", {
5927
+ "data-block-type": Contentful.BLOCKS.TABLE
5928
+ }, /*#__PURE__*/React.createElement("table", Object.assign({
5911
5929
  className: style$5
5912
- }), /*#__PURE__*/React.createElement("tbody", null, props.children));
5930
+ }, props.attributes), /*#__PURE__*/React.createElement("tbody", null, props.children)));
5913
5931
  };
5914
5932
 
5915
5933
  var createTableOnKeyDown = function createTableOnKeyDown(editor, plugin) {
@@ -5922,6 +5940,38 @@ var createTableOnKeyDown = function createTableOnKeyDown(editor, plugin) {
5922
5940
  event.preventDefault();
5923
5941
  event.stopPropagation();
5924
5942
  return;
5943
+ } // This fixes `Cannot resolve a Slate point from DOM point: [object HTMLDivElement]` when typing while the cursor is before table
5944
+
5945
+
5946
+ var windowSelection = window.getSelection();
5947
+
5948
+ if (windowSelection) {
5949
+ var _windowSelection$anch, _windowSelection$anch2;
5950
+
5951
+ // @ts-expect-error
5952
+ var blockType = (_windowSelection$anch = windowSelection.anchorNode.attributes) == null ? void 0 : (_windowSelection$anch2 = _windowSelection$anch['data-block-type']) == null ? void 0 : _windowSelection$anch2.value; // this attribute comes from `plugins/Table/components/Table.tsx`
5953
+
5954
+ var isBeforeTable = blockType === Contentful.BLOCKS.TABLE;
5955
+
5956
+ if (isBeforeTable) {
5957
+ if (event.key === 'Enter') {
5958
+ var above = plateCore.getAbove(editor, {
5959
+ match: {
5960
+ type: Contentful.BLOCKS.TABLE
5961
+ }
5962
+ });
5963
+ if (!above) return;
5964
+ var tablePath = above[1];
5965
+ insertEmptyParagraph(editor, {
5966
+ at: tablePath,
5967
+ select: true
5968
+ });
5969
+ }
5970
+
5971
+ event.preventDefault();
5972
+ event.stopPropagation();
5973
+ return;
5974
+ }
5925
5975
  }
5926
5976
 
5927
5977
  defaultHandler(event);
@@ -5936,18 +5986,26 @@ var createTablePlugin = function createTablePlugin() {
5936
5986
  handlers: {
5937
5987
  onKeyDown: createTableOnKeyDown
5938
5988
  },
5939
- withOverrides: function withOverrides(editor) {
5989
+ withOverrides: function withOverrides(editor, plugin) {
5990
+ // injects important fixes from plate's original table plugin
5991
+ plateTable.withTable(editor, plugin);
5940
5992
  addTableTrackingEvents(editor);
5941
5993
  var insertFragment = editor.insertFragment;
5942
5994
 
5943
5995
  editor.insertFragment = function (fragments) {
5996
+ var _editor$selection;
5997
+
5944
5998
  // We need to make sure we have a new, empty and clean paragraph in order to paste tables as-is due to how Slate behaves
5945
5999
  // More info: https://github.com/ianstormtaylor/slate/pull/4489 and https://github.com/ianstormtaylor/slate/issues/4542
5946
- var fragmentHasTable = fragments.some(function (fragment) {
5947
- return fragment.type === Contentful.BLOCKS.TABLE;
6000
+ var isInsertingTable = fragments.some(function (fragment) {
6001
+ return isTable(fragment);
5948
6002
  });
6003
+ var isTableFirstFragment = fragments.findIndex(function (fragment) {
6004
+ return isTable(fragment);
6005
+ }) === 0;
6006
+ var currentLineHasText = plateCore.getText(editor, (_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.path) !== '';
5949
6007
 
5950
- if (fragmentHasTable) {
6008
+ if (isInsertingTable && isTableFirstFragment && currentLineHasText) {
5951
6009
  insertEmptyParagraph(editor);
5952
6010
  }
5953
6011