@contentful/field-editor-rich-text 2.0.0-next.34 → 2.0.0-next.37

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.
@@ -2,7 +2,7 @@ import React__default, { createContext, useContext, useMemo, createElement, useE
2
2
  import { useEntities, ScheduledIconWithTooltip, MissingEntityCard, AssetThumbnail, EntityProvider, getScheduleTooltipContent } from '@contentful/field-editor-reference';
3
3
  import { entityHelpers, shortenStorageUnit, isValidImage, ModalDialogLauncher, FieldConnector } from '@contentful/field-editor-shared';
4
4
  import { BLOCKS, INLINES, TEXT_CONTAINERS, HEADINGS, LIST_ITEM_BLOCKS, MARKS, CONTAINERS, TOP_LEVEL_BLOCKS, VOID_BLOCKS, EMPTY_DOCUMENT } from '@contentful/rich-text-types';
5
- import { usePlateEditorRef, usePlateEditorState, getNodes, toggleNodeType, getText, getAbove, setNodes, isAncestorEmpty, match, getLastChildPath, wrapNodes, getPluginType, unwrapNodes, isCollapsed, isRangeAcrossBlocks, ELEMENT_DEFAULT, findNode, getParent, getBlockAbove, isSelectionAtBlockStart, isSelectionAtBlockEnd, isFirstChild, insertNodes, moveChildren, isBlockAboveEmpty, mockPlugin, deleteFragment, isMarkActive, toggleMark, someHtmlElement, KEY_DESERIALIZE_HTML, hasSingleChild, isLastChild, someNode, getChildren as getChildren$1, createDeserializeHtmlPlugin, createDeserializeAstPlugin, createPlateEditor, getPlateSelectors, getPlateActions, Plate } from '@udecode/plate-core';
5
+ import { usePlateEditorRef, usePlateEditorState, getNodes, toggleNodeType, getText, getAbove, setNodes, isAncestorEmpty, match, getLastChildPath, wrapNodes, getPluginType, unwrapNodes, isCollapsed, isRangeAcrossBlocks, ELEMENT_DEFAULT, findNode, getParent, getBlockAbove, isSelectionAtBlockStart, isSelectionAtBlockEnd, isFirstChild, insertNodes, moveChildren, isBlockAboveEmpty, mockPlugin, deleteFragment, isMarkActive, toggleMark, someHtmlElement, KEY_DESERIALIZE_HTML, hasSingleChild, isLastChild, someNode, getChildren as getChildren$1, queryNode, createDeserializeHtmlPlugin, createDeserializeAstPlugin, createPlateEditor, getPlateSelectors, getPlateActions, Plate } 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-es/noop';
@@ -451,7 +451,7 @@ function moveToTheNextLine(editor) {
451
451
  function moveToTheNextChar(editor) {
452
452
  Transforms.move(editor, {
453
453
  distance: 1,
454
- unit: 'character'
454
+ unit: 'offset'
455
455
  });
456
456
  }
457
457
  function insertEmptyParagraph(editor, options) {
@@ -5436,7 +5436,8 @@ var createSelectOnBackspacePlugin = function createSelectOnBackspacePlugin() {
5436
5436
  return createSelectOnBackspacePlugin$1({
5437
5437
  options: {
5438
5438
  query: {
5439
- allow: [BLOCKS.EMBEDDED_ASSET, BLOCKS.EMBEDDED_ENTRY, BLOCKS.HR, INLINES.EMBEDDED_ENTRY]
5439
+ // `createTextPlugin` is taking care of block elements
5440
+ allow: [INLINES.EMBEDDED_ENTRY]
5440
5441
  }
5441
5442
  }
5442
5443
  });
@@ -6599,19 +6600,20 @@ function createTextPlugin() {
6599
6600
  deleteBackward = editor.deleteBackward;
6600
6601
 
6601
6602
  editor.deleteBackward = function (unit) {
6602
- deleteFirstEmptyParagraph(unit, editor, deleteBackward);
6603
+ deleteEmptyParagraph(unit, editor, deleteBackward);
6603
6604
  };
6604
6605
 
6605
6606
  editor.deleteForward = function (unit) {
6606
- deleteFirstEmptyParagraph(unit, editor, deleteForward);
6607
+ deleteEmptyParagraph(unit, editor, deleteForward);
6607
6608
  };
6608
6609
 
6610
+ fixPasteAsPlainText(editor);
6609
6611
  return editor;
6610
6612
  }
6611
6613
  };
6612
6614
  }
6613
6615
 
6614
- function deleteFirstEmptyParagraph(unit, editor, deleteFunction) {
6616
+ function deleteEmptyParagraph(unit, editor, deleteFunction) {
6615
6617
  var entry = getAbove(editor, {
6616
6618
  match: {
6617
6619
  type: TEXT_CONTAINERS
@@ -6626,10 +6628,29 @@ function deleteFirstEmptyParagraph(unit, editor, deleteFunction) {
6626
6628
  var isRootLevel = path.length === 1;
6627
6629
  var hasSiblings = editor.children.length > 1; // prevent editor from losing focus
6628
6630
 
6629
- if (isTextEmpty && isRootLevel && isFirstChild(path) && hasSiblings) {
6631
+ if (isTextEmpty && isRootLevel && hasSiblings) {
6630
6632
  Transforms.removeNodes(editor, {
6631
6633
  at: path
6632
6634
  });
6635
+ var prevNode = Editor.before(editor, editor.selection, {
6636
+ unit: unit
6637
+ });
6638
+
6639
+ if (prevNode) {
6640
+ var _Editor$nodes = Editor.nodes(editor, {
6641
+ match: function match(node) {
6642
+ return queryNode([node, prevNode.path], {
6643
+ allow: [BLOCKS.EMBEDDED_ASSET, BLOCKS.EMBEDDED_ENTRY, BLOCKS.HR]
6644
+ });
6645
+ },
6646
+ at: prevNode
6647
+ }),
6648
+ prevCell = _Editor$nodes[0];
6649
+
6650
+ if (prevCell) {
6651
+ Transforms.select(editor, prevNode);
6652
+ }
6653
+ }
6633
6654
  } else {
6634
6655
  deleteFunction(unit);
6635
6656
  }
@@ -6637,6 +6658,45 @@ function deleteFirstEmptyParagraph(unit, editor, deleteFunction) {
6637
6658
  deleteFunction(unit);
6638
6659
  }
6639
6660
  }
6661
+ /**
6662
+ * To be compatible with the old behavior we need to treat each 2 consecutive
6663
+ * line breaks as a new paragraph when pasting as plain text (also known as
6664
+ * paste and match style in macOS)
6665
+ */
6666
+
6667
+
6668
+ function fixPasteAsPlainText(editor) {
6669
+ editor.insertTextData = function (data) {
6670
+ var text = data.getData('text/plain');
6671
+
6672
+ if (!text) {
6673
+ return false;
6674
+ }
6675
+
6676
+ var lines = text.split(/\n{2}/);
6677
+ var split = false;
6678
+
6679
+ for (var _iterator = _createForOfIteratorHelperLoose(lines), _step; !(_step = _iterator()).done;) {
6680
+ var line = _step.value;
6681
+
6682
+ // empty lines
6683
+ if (/^(\r\n?|\n)$/.test(line)) {
6684
+ continue;
6685
+ }
6686
+
6687
+ if (split) {
6688
+ Transforms.splitNodes(editor, {
6689
+ always: true
6690
+ });
6691
+ }
6692
+
6693
+ editor.insertText(line);
6694
+ split = true;
6695
+ }
6696
+
6697
+ return true;
6698
+ };
6699
+ }
6640
6700
 
6641
6701
  var createTrailingParagraphPlugin = function createTrailingParagraphPlugin() {
6642
6702
  return createTrailingBlockPlugin({