@contentful/field-editor-rich-text 2.0.0-next.22 → 2.0.0-next.25

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.
@@ -420,10 +420,12 @@ function isBlockSelected(editor, type) {
420
420
  function isRootLevel(path) {
421
421
  return path.length === 1;
422
422
  }
423
- function getNodeEntryFromSelection(editor, nodeTypeOrTypes) {
424
- if (!editor.selection) return [];
423
+ function getNodeEntryFromSelection(editor, nodeTypeOrTypes, path) {
424
+ var _path, _editor$selection;
425
+
426
+ path = (_path = path) != null ? _path : (_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.path;
427
+ if (!path) return [];
425
428
  var nodeTypes = Array.isArray(nodeTypeOrTypes) ? nodeTypeOrTypes : [nodeTypeOrTypes];
426
- var path = editor.selection.focus.path;
427
429
 
428
430
  for (var i = 0; i < path.length; i++) {
429
431
  var nodeEntry = Editor.node(editor, path.slice(0, i + 1));
@@ -582,18 +584,18 @@ function getAncestorPathFromSelection(editor) {
582
584
  });
583
585
  }
584
586
  var isAtEndOfTextSelection = function isAtEndOfTextSelection(editor) {
585
- var _editor$selection, _editor$selection2;
587
+ var _editor$selection2, _editor$selection3;
586
588
 
587
- return ((_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.offset) === getText(editor, (_editor$selection2 = editor.selection) == null ? void 0 : _editor$selection2.focus.path).length;
589
+ return ((_editor$selection2 = editor.selection) == null ? void 0 : _editor$selection2.focus.offset) === getText(editor, (_editor$selection3 = editor.selection) == null ? void 0 : _editor$selection3.focus.path).length;
588
590
  };
589
591
  function currentSelectionStartsTableCell(editor) {
590
- var _editor$selection3;
592
+ var _editor$selection4;
591
593
 
592
594
  var _getNodeEntryFromSele2 = getNodeEntryFromSelection(editor, [BLOCKS.TABLE_CELL, BLOCKS.TABLE_HEADER_CELL]),
593
595
  tableCellNode = _getNodeEntryFromSele2[0],
594
596
  path = _getNodeEntryFromSele2[1];
595
597
 
596
- return !!tableCellNode && (!getText(editor, path) || ((_editor$selection3 = editor.selection) == null ? void 0 : _editor$selection3.focus.offset) === 0);
598
+ return !!tableCellNode && (!getText(editor, path) || ((_editor$selection4 = editor.selection) == null ? void 0 : _editor$selection4.focus.offset) === 0);
597
599
  }
598
600
  /**
599
601
  * This traversal strategy is unfortunately necessary because Slate doesn't
@@ -1055,7 +1057,14 @@ function FetchingWrappedEntryCard(props) {
1055
1057
 
1056
1058
  var styles$3 = {
1057
1059
  root: /*#__PURE__*/css({
1058
- marginBottom: '1.25rem !important'
1060
+ marginBottom: '1.25rem !important',
1061
+ display: 'block'
1062
+ }),
1063
+ container: /*#__PURE__*/css({
1064
+ // The next 2 properties ensure Entity card won't be aligned above
1065
+ // a list item marker (i.e. bullet)
1066
+ display: 'inline-block',
1067
+ verticalAlign: 'text-top'
1059
1068
  })
1060
1069
  };
1061
1070
  function LinkedEntityBlock(props) {
@@ -1096,7 +1105,8 @@ function LinkedEntityBlock(props) {
1096
1105
  }), /*#__PURE__*/React__default.createElement("div", {
1097
1106
  // COMPAT: This makes copy & paste work for Chromium/Blink browsers and Safari
1098
1107
  contentEditable: HAS_BEFORE_INPUT_SUPPORT ? false : undefined,
1099
- draggable: HAS_BEFORE_INPUT_SUPPORT ? true : undefined
1108
+ draggable: HAS_BEFORE_INPUT_SUPPORT ? true : undefined,
1109
+ className: styles$3.container
1100
1110
  }, entityType === 'Entry' && /*#__PURE__*/React__default.createElement(FetchingWrappedEntryCard, {
1101
1111
  sdk: sdk,
1102
1112
  entryId: entityId,
@@ -3199,29 +3209,31 @@ function HyperlinkModal(props) {
3199
3209
  testId: "confirm-cta"
3200
3210
  }, props.linkType ? 'Update' : 'Insert'))));
3201
3211
  }
3202
- function addOrEditLink(_x, _x2, _x3) {
3212
+ function addOrEditLink(_x, _x2, _x3, _x4) {
3203
3213
  return _addOrEditLink.apply(this, arguments);
3204
3214
  }
3205
3215
 
3206
3216
  function _addOrEditLink() {
3207
- _addOrEditLink = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(editor, sdk, logAction) {
3217
+ _addOrEditLink = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(editor, sdk, logAction, targetPath) {
3208
3218
  var _target$sys$linkType;
3209
3219
 
3210
- var linkType, linkText, linkTarget, linkEntity, _getNodeEntryFromSele, node, path, selectionBeforeBlur, currentLinkText, isEditing, data, text, url, type, target;
3220
+ var selectionBeforeBlur, linkType, linkText, linkTarget, linkEntity, _getNodeEntryFromSele, node, path, selectionAfterFocus, currentLinkText, isEditing, data, text, url, type, target;
3211
3221
 
3212
3222
  return runtime_1.wrap(function _callee3$(_context3) {
3213
3223
  while (1) {
3214
3224
  switch (_context3.prev = _context3.next) {
3215
3225
  case 0:
3216
- if (editor.selection) {
3217
- _context3.next = 2;
3226
+ selectionBeforeBlur = editor.selection ? _extends({}, editor.selection) : undefined;
3227
+
3228
+ if (!(!targetPath && !selectionBeforeBlur)) {
3229
+ _context3.next = 3;
3218
3230
  break;
3219
3231
  }
3220
3232
 
3221
3233
  return _context3.abrupt("return");
3222
3234
 
3223
- case 2:
3224
- _getNodeEntryFromSele = getNodeEntryFromSelection(editor, LINK_TYPES), node = _getNodeEntryFromSele[0], path = _getNodeEntryFromSele[1];
3235
+ case 3:
3236
+ _getNodeEntryFromSele = getNodeEntryFromSelection(editor, LINK_TYPES, targetPath), node = _getNodeEntryFromSele[0], path = _getNodeEntryFromSele[1];
3225
3237
 
3226
3238
  if (node && path) {
3227
3239
  linkType = node.type;
@@ -3230,11 +3242,11 @@ function _addOrEditLink() {
3230
3242
  linkEntity = node.data.target;
3231
3243
  }
3232
3244
 
3233
- selectionBeforeBlur = _extends({}, editor.selection);
3234
- currentLinkText = linkText || Editor.string(editor, editor.selection);
3245
+ selectionAfterFocus = targetPath != null ? targetPath : selectionBeforeBlur;
3246
+ currentLinkText = linkText || (editor.selection ? Editor.string(editor, editor.selection) : '');
3235
3247
  isEditing = Boolean(node && path);
3236
3248
  logAction(isEditing ? 'openEditHyperlinkDialog' : 'openCreateHyperlinkDialog');
3237
- _context3.next = 10;
3249
+ _context3.next = 11;
3238
3250
  return ModalDialogLauncher.openDialog({
3239
3251
  title: isEditing ? 'Edit hyperlink' : 'Insert hyperlink',
3240
3252
  width: 'large',
@@ -3253,20 +3265,21 @@ function _addOrEditLink() {
3253
3265
  });
3254
3266
  });
3255
3267
 
3256
- case 10:
3268
+ case 11:
3257
3269
  data = _context3.sent;
3270
+ Transforms.select(editor, selectionAfterFocus);
3258
3271
 
3259
3272
  if (data) {
3260
- _context3.next = 14;
3273
+ _context3.next = 17;
3261
3274
  break;
3262
3275
  }
3263
3276
 
3277
+ focus(editor);
3264
3278
  logAction(isEditing ? 'cancelEditHyperlinkDialog' : 'cancelCreateHyperlinkDialog');
3265
3279
  return _context3.abrupt("return");
3266
3280
 
3267
- case 14:
3281
+ case 17:
3268
3282
  text = data.linkText, url = data.linkTarget, type = data.linkType, target = data.linkEntity;
3269
- Transforms.select(editor, selectionBeforeBlur);
3270
3283
  Editor.withoutNormalizing(editor, function () {
3271
3284
  insertLink(editor, {
3272
3285
  text: text,
@@ -3282,7 +3295,7 @@ function _addOrEditLink() {
3282
3295
  });
3283
3296
  focus(editor);
3284
3297
 
3285
- case 19:
3298
+ case 21:
3286
3299
  case "end":
3287
3300
  return _context3.stop();
3288
3301
  }
@@ -3536,7 +3549,11 @@ function EntityHyperlink(props) {
3536
3549
  event.preventDefault();
3537
3550
  event.stopPropagation();
3538
3551
  if (!editor) return;
3539
- addOrEditLink(editor, sdk, editor.tracking.onViewportAction);
3552
+ var p = ReactEditor.toSlatePoint(editor, [event.target, 0], {
3553
+ exactMatch: false,
3554
+ suppressThrow: false
3555
+ });
3556
+ addOrEditLink(editor, sdk, editor.tracking.onViewportAction, p.path);
3540
3557
  }
3541
3558
 
3542
3559
  return /*#__PURE__*/createElement(Tooltip, {
@@ -3564,7 +3581,11 @@ function UrlHyperlink(props) {
3564
3581
  event.preventDefault();
3565
3582
  event.stopPropagation();
3566
3583
  if (!editor) return;
3567
- addOrEditLink(editor, sdk, editor.tracking.onViewportAction);
3584
+ var p = ReactEditor.toSlatePoint(editor, [event.target, 0], {
3585
+ exactMatch: false,
3586
+ suppressThrow: false
3587
+ });
3588
+ addOrEditLink(editor, sdk, editor.tracking.onViewportAction, p.path);
3568
3589
  }
3569
3590
 
3570
3591
  return /*#__PURE__*/createElement(Tooltip, {
@@ -4128,7 +4149,25 @@ var replaceNodeWithListItems = function replaceNodeWithListItems(editor, entry)
4128
4149
  });
4129
4150
  };
4130
4151
  var isListTypeActive = function isListTypeActive(editor, type) {
4131
- // Lists can be nested. Here, we take the list type at the lowest level
4152
+ var selection = editor.selection;
4153
+
4154
+ if (!selection) {
4155
+ return false;
4156
+ }
4157
+
4158
+ if (Range.isExpanded(selection)) {
4159
+ var _Range$edges = Range.edges(selection),
4160
+ start = _Range$edges[0],
4161
+ end = _Range$edges[1];
4162
+
4163
+ var node = Node.common(editor, start.path, end.path);
4164
+
4165
+ if (node[0].type === type) {
4166
+ return true;
4167
+ }
4168
+ } // Lists can be nested. Here, we take the list type at the lowest level
4169
+
4170
+
4132
4171
  var listNode = getBlockAbove(editor, {
4133
4172
  match: {
4134
4173
  type: [BLOCKS.OL_LIST, BLOCKS.UL_LIST]
@@ -5175,9 +5214,9 @@ var sanitizeHTML = function sanitizeHTML(html) {
5175
5214
  // Parse the HTML string and pipe it through our transformers
5176
5215
  var doc = transformers.reduce(function (value, cb) {
5177
5216
  return cb(value);
5178
- }, new DOMParser().parseFromString(html, 'text/html')); // Remove whitespace between tags
5179
-
5180
- return doc.body.innerHTML.replace(/>\s+</g, '><');
5217
+ }, new DOMParser().parseFromString(html, 'text/html'));
5218
+ return doc.body.innerHTML.replace(/>\s+</g, '><') // Remove whitespace between tags
5219
+ .replace(/(.*)<div.*>(<table.*<\/table>)<\/div>(.*)/g, '$1$2$3'); // remove div containers from tables
5181
5220
  };
5182
5221
 
5183
5222
  /**
@@ -5718,6 +5757,9 @@ var isNotEmpty = function isNotEmpty(editor, _ref4) {
5718
5757
  var path = _ref4[1];
5719
5758
  return Array.from(Node.children(editor, path)).length !== 0;
5720
5759
  };
5760
+ var isTable = function isTable(node) {
5761
+ return Element.isElement(node) && node.type === BLOCKS.TABLE;
5762
+ };
5721
5763
 
5722
5764
  var styles$i = {
5723
5765
  topRight: /*#__PURE__*/css({
@@ -5895,13 +5937,19 @@ var createTablePlugin = function createTablePlugin() {
5895
5937
  var insertFragment = editor.insertFragment;
5896
5938
 
5897
5939
  editor.insertFragment = function (fragments) {
5940
+ var _editor$selection;
5941
+
5898
5942
  // 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
5899
5943
  // More info: https://github.com/ianstormtaylor/slate/pull/4489 and https://github.com/ianstormtaylor/slate/issues/4542
5900
- var fragmentHasTable = fragments.some(function (fragment) {
5901
- return fragment.type === BLOCKS.TABLE;
5944
+ var isInsertingTable = fragments.some(function (fragment) {
5945
+ return isTable(fragment);
5902
5946
  });
5947
+ var isTableFirstFragment = fragments.findIndex(function (fragment) {
5948
+ return isTable(fragment);
5949
+ }) === 0;
5950
+ var currentLineHasText = getText(editor, (_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.path) !== '';
5903
5951
 
5904
- if (fragmentHasTable) {
5952
+ if (isInsertingTable && isTableFirstFragment && currentLineHasText) {
5905
5953
  insertEmptyParagraph(editor);
5906
5954
  }
5907
5955