@contentful/field-editor-rich-text 2.0.0-next.23 → 2.0.0-next.26

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.
@@ -427,10 +427,12 @@ function isBlockSelected(editor, type) {
427
427
  function isRootLevel(path) {
428
428
  return path.length === 1;
429
429
  }
430
- function getNodeEntryFromSelection(editor, nodeTypeOrTypes) {
431
- if (!editor.selection) return [];
430
+ function getNodeEntryFromSelection(editor, nodeTypeOrTypes, path) {
431
+ var _path, _editor$selection;
432
+
433
+ path = (_path = path) != null ? _path : (_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.path;
434
+ if (!path) return [];
432
435
  var nodeTypes = Array.isArray(nodeTypeOrTypes) ? nodeTypeOrTypes : [nodeTypeOrTypes];
433
- var path = editor.selection.focus.path;
434
436
 
435
437
  for (var i = 0; i < path.length; i++) {
436
438
  var nodeEntry = slate.Editor.node(editor, path.slice(0, i + 1));
@@ -589,18 +591,18 @@ function getAncestorPathFromSelection(editor) {
589
591
  });
590
592
  }
591
593
  var isAtEndOfTextSelection = function isAtEndOfTextSelection(editor) {
592
- var _editor$selection, _editor$selection2;
594
+ var _editor$selection2, _editor$selection3;
593
595
 
594
- return ((_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.offset) === plateCore.getText(editor, (_editor$selection2 = editor.selection) == null ? void 0 : _editor$selection2.focus.path).length;
596
+ return ((_editor$selection2 = editor.selection) == null ? void 0 : _editor$selection2.focus.offset) === plateCore.getText(editor, (_editor$selection3 = editor.selection) == null ? void 0 : _editor$selection3.focus.path).length;
595
597
  };
596
598
  function currentSelectionStartsTableCell(editor) {
597
- var _editor$selection3;
599
+ var _editor$selection4;
598
600
 
599
601
  var _getNodeEntryFromSele2 = getNodeEntryFromSelection(editor, [Contentful.BLOCKS.TABLE_CELL, Contentful.BLOCKS.TABLE_HEADER_CELL]),
600
602
  tableCellNode = _getNodeEntryFromSele2[0],
601
603
  path = _getNodeEntryFromSele2[1];
602
604
 
603
- return !!tableCellNode && (!plateCore.getText(editor, path) || ((_editor$selection3 = editor.selection) == null ? void 0 : _editor$selection3.focus.offset) === 0);
605
+ return !!tableCellNode && (!plateCore.getText(editor, path) || ((_editor$selection4 = editor.selection) == null ? void 0 : _editor$selection4.focus.offset) === 0);
604
606
  }
605
607
  /**
606
608
  * This traversal strategy is unfortunately necessary because Slate doesn't
@@ -1063,6 +1065,9 @@ function FetchingWrappedEntryCard(props) {
1063
1065
  var styles$3 = {
1064
1066
  root: /*#__PURE__*/emotion.css({
1065
1067
  marginBottom: '1.25rem !important',
1068
+ display: 'block'
1069
+ }),
1070
+ container: /*#__PURE__*/emotion.css({
1066
1071
  // The next 2 properties ensure Entity card won't be aligned above
1067
1072
  // a list item marker (i.e. bullet)
1068
1073
  display: 'inline-block',
@@ -1107,7 +1112,8 @@ function LinkedEntityBlock(props) {
1107
1112
  }), /*#__PURE__*/React__default.createElement("div", {
1108
1113
  // COMPAT: This makes copy & paste work for Chromium/Blink browsers and Safari
1109
1114
  contentEditable: HAS_BEFORE_INPUT_SUPPORT ? false : undefined,
1110
- draggable: HAS_BEFORE_INPUT_SUPPORT ? true : undefined
1115
+ draggable: HAS_BEFORE_INPUT_SUPPORT ? true : undefined,
1116
+ className: styles$3.container
1111
1117
  }, entityType === 'Entry' && /*#__PURE__*/React__default.createElement(FetchingWrappedEntryCard, {
1112
1118
  sdk: sdk,
1113
1119
  entryId: entityId,
@@ -3210,29 +3216,31 @@ function HyperlinkModal(props) {
3210
3216
  testId: "confirm-cta"
3211
3217
  }, props.linkType ? 'Update' : 'Insert'))));
3212
3218
  }
3213
- function addOrEditLink(_x, _x2, _x3) {
3219
+ function addOrEditLink(_x, _x2, _x3, _x4) {
3214
3220
  return _addOrEditLink.apply(this, arguments);
3215
3221
  }
3216
3222
 
3217
3223
  function _addOrEditLink() {
3218
- _addOrEditLink = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(editor, sdk, logAction) {
3224
+ _addOrEditLink = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(editor, sdk, logAction, targetPath) {
3219
3225
  var _target$sys$linkType;
3220
3226
 
3221
- var linkType, linkText, linkTarget, linkEntity, _getNodeEntryFromSele, node, path, selectionBeforeBlur, currentLinkText, isEditing, data, text, url, type, target;
3227
+ var selectionBeforeBlur, linkType, linkText, linkTarget, linkEntity, _getNodeEntryFromSele, node, path, selectionAfterFocus, currentLinkText, isEditing, data, text, url, type, target;
3222
3228
 
3223
3229
  return runtime_1.wrap(function _callee3$(_context3) {
3224
3230
  while (1) {
3225
3231
  switch (_context3.prev = _context3.next) {
3226
3232
  case 0:
3227
- if (editor.selection) {
3228
- _context3.next = 2;
3233
+ selectionBeforeBlur = editor.selection ? _extends({}, editor.selection) : undefined;
3234
+
3235
+ if (!(!targetPath && !selectionBeforeBlur)) {
3236
+ _context3.next = 3;
3229
3237
  break;
3230
3238
  }
3231
3239
 
3232
3240
  return _context3.abrupt("return");
3233
3241
 
3234
- case 2:
3235
- _getNodeEntryFromSele = getNodeEntryFromSelection(editor, LINK_TYPES), node = _getNodeEntryFromSele[0], path = _getNodeEntryFromSele[1];
3242
+ case 3:
3243
+ _getNodeEntryFromSele = getNodeEntryFromSelection(editor, LINK_TYPES, targetPath), node = _getNodeEntryFromSele[0], path = _getNodeEntryFromSele[1];
3236
3244
 
3237
3245
  if (node && path) {
3238
3246
  linkType = node.type;
@@ -3241,11 +3249,11 @@ function _addOrEditLink() {
3241
3249
  linkEntity = node.data.target;
3242
3250
  }
3243
3251
 
3244
- selectionBeforeBlur = _extends({}, editor.selection);
3245
- currentLinkText = linkText || slate.Editor.string(editor, editor.selection);
3252
+ selectionAfterFocus = targetPath != null ? targetPath : selectionBeforeBlur;
3253
+ currentLinkText = linkText || (editor.selection ? slate.Editor.string(editor, editor.selection) : '');
3246
3254
  isEditing = Boolean(node && path);
3247
3255
  logAction(isEditing ? 'openEditHyperlinkDialog' : 'openCreateHyperlinkDialog');
3248
- _context3.next = 10;
3256
+ _context3.next = 11;
3249
3257
  return fieldEditorShared.ModalDialogLauncher.openDialog({
3250
3258
  title: isEditing ? 'Edit hyperlink' : 'Insert hyperlink',
3251
3259
  width: 'large',
@@ -3264,20 +3272,21 @@ function _addOrEditLink() {
3264
3272
  });
3265
3273
  });
3266
3274
 
3267
- case 10:
3275
+ case 11:
3268
3276
  data = _context3.sent;
3277
+ slate.Transforms.select(editor, selectionAfterFocus);
3269
3278
 
3270
3279
  if (data) {
3271
- _context3.next = 14;
3280
+ _context3.next = 17;
3272
3281
  break;
3273
3282
  }
3274
3283
 
3284
+ focus(editor);
3275
3285
  logAction(isEditing ? 'cancelEditHyperlinkDialog' : 'cancelCreateHyperlinkDialog');
3276
3286
  return _context3.abrupt("return");
3277
3287
 
3278
- case 14:
3288
+ case 17:
3279
3289
  text = data.linkText, url = data.linkTarget, type = data.linkType, target = data.linkEntity;
3280
- slate.Transforms.select(editor, selectionBeforeBlur);
3281
3290
  slate.Editor.withoutNormalizing(editor, function () {
3282
3291
  insertLink(editor, {
3283
3292
  text: text,
@@ -3293,7 +3302,7 @@ function _addOrEditLink() {
3293
3302
  });
3294
3303
  focus(editor);
3295
3304
 
3296
- case 19:
3305
+ case 21:
3297
3306
  case "end":
3298
3307
  return _context3.stop();
3299
3308
  }
@@ -3547,7 +3556,11 @@ function EntityHyperlink(props) {
3547
3556
  event.preventDefault();
3548
3557
  event.stopPropagation();
3549
3558
  if (!editor) return;
3550
- addOrEditLink(editor, sdk, editor.tracking.onViewportAction);
3559
+ var p = Slate.ReactEditor.toSlatePoint(editor, [event.target, 0], {
3560
+ exactMatch: false,
3561
+ suppressThrow: false
3562
+ });
3563
+ addOrEditLink(editor, sdk, editor.tracking.onViewportAction, p.path);
3551
3564
  }
3552
3565
 
3553
3566
  return /*#__PURE__*/React.createElement(f36Components.Tooltip, {
@@ -3575,7 +3588,11 @@ function UrlHyperlink(props) {
3575
3588
  event.preventDefault();
3576
3589
  event.stopPropagation();
3577
3590
  if (!editor) return;
3578
- addOrEditLink(editor, sdk, editor.tracking.onViewportAction);
3591
+ var p = Slate.ReactEditor.toSlatePoint(editor, [event.target, 0], {
3592
+ exactMatch: false,
3593
+ suppressThrow: false
3594
+ });
3595
+ addOrEditLink(editor, sdk, editor.tracking.onViewportAction, p.path);
3579
3596
  }
3580
3597
 
3581
3598
  return /*#__PURE__*/React.createElement(f36Components.Tooltip, {
@@ -3851,37 +3868,44 @@ var moveListItems = function moveListItems(editor, _temp) {
3851
3868
  * Credit: Modified version of Plate's list plugin
3852
3869
  * See: https://github.com/udecode/plate/blob/main/packages/nodes/list
3853
3870
  */
3854
- 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
+
3855
3881
  var unwrapList = function unwrapList(editor, _temp) {
3856
3882
  var _ref = _temp === void 0 ? {} : _temp,
3857
3883
  at = _ref.at;
3858
3884
 
3859
3885
  slate.Editor.withoutNormalizing(editor, function () {
3860
3886
  do {
3861
- plateCore.unwrapNodes(editor, {
3887
+ // lift list items to the root level
3888
+ slate.Transforms.liftNodes(editor, {
3862
3889
  at: at,
3863
- match: {
3864
- type: Contentful.BLOCKS.LIST_ITEM
3890
+ match: function match(node) {
3891
+ return slate.Element.isElement(node) && node.type === Contentful.BLOCKS.LIST_ITEM;
3865
3892
  },
3866
- split: true
3893
+ mode: 'lowest'
3867
3894
  });
3868
- plateCore.unwrapNodes(editor, {
3869
- at: at,
3870
- match: {
3871
- type: listTypes
3872
- },
3873
- split: true
3874
- });
3875
- } while (plateCore.getAbove(editor, {
3895
+ } while (!hasUnliftedListItems(editor, at)); // finally unwrap all lifted items
3896
+
3897
+
3898
+ plateCore.unwrapNodes(editor, {
3899
+ at: at,
3876
3900
  match: {
3877
- type: listTypes,
3878
- at: at
3879
- }
3880
- }));
3901
+ type: Contentful.BLOCKS.LIST_ITEM
3902
+ },
3903
+ split: false
3904
+ });
3881
3905
  });
3882
3906
  };
3883
3907
 
3884
- var listTypes$1 = [Contentful.BLOCKS.UL_LIST, Contentful.BLOCKS.OL_LIST];
3908
+ var listTypes = [Contentful.BLOCKS.UL_LIST, Contentful.BLOCKS.OL_LIST];
3885
3909
  var toggleList = function toggleList(editor, _ref) {
3886
3910
  var type = _ref.type;
3887
3911
  return slate.Editor.withoutNormalizing(editor, function () {
@@ -3902,7 +3926,7 @@ var toggleList = function toggleList(editor, _ref) {
3902
3926
  }, {
3903
3927
  at: editor.selection,
3904
3928
  match: function match(n) {
3905
- return listTypes$1.includes(n.type);
3929
+ return listTypes.includes(n.type);
3906
3930
  },
3907
3931
  mode: 'lowest'
3908
3932
  });
@@ -3943,19 +3967,25 @@ var toggleList = function toggleList(editor, _ref) {
3943
3967
 
3944
3968
  var commonEntry = slate.Node.common(editor, startPoint.path, endPoint.path);
3945
3969
 
3946
- if (listTypes$1.includes(commonEntry[0].type) || commonEntry[0].type === Contentful.BLOCKS.LIST_ITEM) {
3947
- 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) {
3948
3978
  var startList = plateCore.findNode(editor, {
3949
3979
  at: slate.Range.start(editor.selection),
3950
3980
  match: {
3951
- type: listTypes$1
3981
+ type: listTypes
3952
3982
  },
3953
3983
  mode: 'lowest'
3954
3984
  });
3955
3985
  var endList = plateCore.findNode(editor, {
3956
3986
  at: slate.Range.end(editor.selection),
3957
3987
  match: {
3958
- type: listTypes$1
3988
+ type: listTypes
3959
3989
  },
3960
3990
  mode: 'lowest'
3961
3991
  });
@@ -3970,7 +4000,7 @@ var toggleList = function toggleList(editor, _ref) {
3970
4000
  }, {
3971
4001
  at: editor.selection,
3972
4002
  match: function match(n, path) {
3973
- return listTypes$1.includes(n.type) && path.length >= rangeLength;
4003
+ return listTypes.includes(n.type) && path.length >= rangeLength;
3974
4004
  },
3975
4005
  mode: 'all'
3976
4006
  });
@@ -3988,7 +4018,7 @@ var toggleList = function toggleList(editor, _ref) {
3988
4018
  }).reverse();
3989
4019
 
3990
4020
  _nodes.forEach(function (n) {
3991
- if (listTypes$1.includes(n[0].type)) {
4021
+ if (listTypes.includes(n[0].type)) {
3992
4022
  plateCore.setNodes(editor, {
3993
4023
  type: type
3994
4024
  }, {
@@ -4139,7 +4169,25 @@ var replaceNodeWithListItems = function replaceNodeWithListItems(editor, entry)
4139
4169
  });
4140
4170
  };
4141
4171
  var isListTypeActive = function isListTypeActive(editor, type) {
4142
- // Lists can be nested. Here, we take the list type at the lowest level
4172
+ var selection = editor.selection;
4173
+
4174
+ if (!selection) {
4175
+ return false;
4176
+ }
4177
+
4178
+ if (slate.Range.isExpanded(selection)) {
4179
+ var _Range$edges = slate.Range.edges(selection),
4180
+ start = _Range$edges[0],
4181
+ end = _Range$edges[1];
4182
+
4183
+ var node = slate.Node.common(editor, start.path, end.path);
4184
+
4185
+ if (node[0].type === type) {
4186
+ return true;
4187
+ }
4188
+ } // Lists can be nested. Here, we take the list type at the lowest level
4189
+
4190
+
4143
4191
  var listNode = plateCore.getBlockAbove(editor, {
4144
4192
  match: {
4145
4193
  type: [Contentful.BLOCKS.OL_LIST, Contentful.BLOCKS.UL_LIST]
@@ -5186,9 +5234,9 @@ var sanitizeHTML = function sanitizeHTML(html) {
5186
5234
  // Parse the HTML string and pipe it through our transformers
5187
5235
  var doc = transformers.reduce(function (value, cb) {
5188
5236
  return cb(value);
5189
- }, new DOMParser().parseFromString(html, 'text/html')); // Remove whitespace between tags
5190
-
5191
- 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
5192
5240
  };
5193
5241
 
5194
5242
  /**
@@ -5729,6 +5777,9 @@ var isNotEmpty = function isNotEmpty(editor, _ref4) {
5729
5777
  var path = _ref4[1];
5730
5778
  return Array.from(slate.Node.children(editor, path)).length !== 0;
5731
5779
  };
5780
+ var isTable = function isTable(node) {
5781
+ return slate.Element.isElement(node) && node.type === Contentful.BLOCKS.TABLE;
5782
+ };
5732
5783
 
5733
5784
  var styles$i = {
5734
5785
  topRight: /*#__PURE__*/emotion.css({
@@ -5906,13 +5957,19 @@ var createTablePlugin = function createTablePlugin() {
5906
5957
  var insertFragment = editor.insertFragment;
5907
5958
 
5908
5959
  editor.insertFragment = function (fragments) {
5960
+ var _editor$selection;
5961
+
5909
5962
  // 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
5910
5963
  // More info: https://github.com/ianstormtaylor/slate/pull/4489 and https://github.com/ianstormtaylor/slate/issues/4542
5911
- var fragmentHasTable = fragments.some(function (fragment) {
5912
- return fragment.type === Contentful.BLOCKS.TABLE;
5964
+ var isInsertingTable = fragments.some(function (fragment) {
5965
+ return isTable(fragment);
5913
5966
  });
5967
+ var isTableFirstFragment = fragments.findIndex(function (fragment) {
5968
+ return isTable(fragment);
5969
+ }) === 0;
5970
+ var currentLineHasText = plateCore.getText(editor, (_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.path) !== '';
5914
5971
 
5915
- if (fragmentHasTable) {
5972
+ if (isInsertingTable && isTableFirstFragment && currentLineHasText) {
5916
5973
  insertEmptyParagraph(editor);
5917
5974
  }
5918
5975