@contentful/field-editor-rich-text 0.24.0-next → 0.26.1-next

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.
@@ -1,7 +1,7 @@
1
1
  import React__default, { useState, useEffect, createElement, useMemo, Fragment, useCallback } from 'react';
2
2
  import noop from 'lodash-es/noop';
3
- import { toSlatejsDocument, toContentfulDocument } from '@contentful/contentful-slatejs-adapter';
4
- import { TOP_LEVEL_BLOCKS, BLOCKS, CONTAINERS, VOID_BLOCKS, INLINES, MARKS, EMPTY_DOCUMENT } from '@contentful/rich-text-types';
3
+ import { toContentfulDocument, toSlatejsDocument } from '@contentful/contentful-slatejs-adapter';
4
+ import { TOP_LEVEL_BLOCKS, BLOCKS, CONTAINERS, VOID_BLOCKS, INLINES, LIST_ITEM_BLOCKS, MARKS, EMPTY_DOCUMENT } from '@contentful/rich-text-types';
5
5
  import { useEntities, ScheduledIconWithTooltip, MissingEntityCard, AssetThumbnail, EntityProvider } from '@contentful/field-editor-reference';
6
6
  import { css, cx } from 'emotion';
7
7
  import tokens from '@contentful/forma-36-tokens';
@@ -15,11 +15,12 @@ import constate from 'constate';
15
15
  import find from 'lodash-es/find';
16
16
  import flow from 'lodash-es/flow';
17
17
  import get from 'lodash-es/get';
18
- import { ELEMENT_LIC, ELEMENT_LI, ELEMENT_UL, ELEMENT_OL, toggleList, createListPlugin } from '@udecode/plate-list';
18
+ import { ELEMENT_LIC, ELEMENT_LI, ELEMENT_UL, ELEMENT_OL, createListPlugin as createListPlugin$1, toggleList } from '@udecode/plate-list';
19
19
  import { entityHelpers, isValidImage, shortenStorageUnit, ModalDialogLauncher, FieldConnector } from '@contentful/field-editor-shared';
20
20
  import moment from 'moment';
21
21
  import mimetype from '@contentful/mimetype';
22
22
  import { ELEMENT_TABLE, ELEMENT_TH, ELEMENT_TR, ELEMENT_TD, insertTable, getEmptyRowNode, getEmptyCellNode, deleteRow, deleteColumn, deleteTable, createTablePlugin as createTablePlugin$1, getTableOnKeyDown } from '@udecode/plate-table';
23
+ import { documentToPlainTextString } from '@contentful/rich-text-plain-text-renderer';
23
24
  import { createDeserializeHTMLPlugin } from '@udecode/plate-html-serializer';
24
25
  import { ELEMENT_PARAGRAPH } from '@udecode/plate-paragraph';
25
26
  import { createExitBreakPlugin } from '@udecode/plate-break';
@@ -1128,6 +1129,11 @@ var withListOptions = (_withListOptions = {}, _withListOptions[ELEMENT_LIC] = {
1128
1129
  type: BLOCKS.OL_LIST,
1129
1130
  component: OL
1130
1131
  }, _withListOptions);
1132
+ var createListPlugin = function createListPlugin() {
1133
+ return createListPlugin$1({
1134
+ validLiChildrenTypes: LIST_ITEM_BLOCKS
1135
+ });
1136
+ };
1131
1137
 
1132
1138
  var _withBoldOptions;
1133
1139
  function ToolbarBoldButton(props) {
@@ -2413,13 +2419,17 @@ function FetchingWrappedEntryCard(props) {
2413
2419
 
2414
2420
  var entry = entries[props.entryId];
2415
2421
  var contentType = useMemo(function () {
2422
+ if (!entry || entry === 'failed') {
2423
+ return undefined;
2424
+ }
2425
+
2416
2426
  return props.sdk.space.getCachedContentTypes().find(function (contentType) {
2417
- return contentType.sys.id === (entry == null ? void 0 : entry.sys.contentType.sys.id);
2427
+ return contentType.sys.id === entry.sys.contentType.sys.id;
2418
2428
  });
2419
2429
  }, [props.sdk, entry]);
2420
2430
  var defaultLocaleCode = props.sdk.locales["default"];
2421
2431
  useEffect(function () {
2422
- if (!entry) return;
2432
+ if (!entry || entry === 'failed') return;
2423
2433
  entityHelpers.getEntryImage({
2424
2434
  entry: entry,
2425
2435
  contentType: contentType,
@@ -2551,13 +2561,15 @@ function AssetDropdownMenu(_ref) {
2551
2561
  }
2552
2562
 
2553
2563
  function FetchingWrappedAssetCard(props) {
2564
+ var _asset$fields;
2565
+
2554
2566
  var _useEntities = useEntities(),
2555
2567
  getOrLoadAsset = _useEntities.getOrLoadAsset,
2556
2568
  assets = _useEntities.assets;
2557
2569
 
2558
2570
  var asset = assets[props.assetId];
2559
2571
  var defaultLocaleCode = props.sdk.locales["default"];
2560
- var entityFile = asset != null && asset.fields.file ? asset.fields.file[props.locale] || asset.fields.file[defaultLocaleCode] : undefined;
2572
+ var entityFile = asset != null && (_asset$fields = asset.fields) != null && _asset$fields.file ? asset.fields.file[props.locale] || asset.fields.file[defaultLocaleCode] : undefined;
2561
2573
  useEffect(function () {
2562
2574
  getOrLoadAsset(props.assetId);
2563
2575
  }, [props.assetId]); // eslint-disable-line
@@ -3627,6 +3639,15 @@ var TableActions = function TableActions() {
3627
3639
 
3628
3640
  var _templateObject$4, _templateObject2$3, _templateObject3$3, _templateObject4$1, _styles$1, _withTableOptions;
3629
3641
  var styles$g = (_styles$1 = {}, _styles$1[BLOCKS.TABLE] = /*#__PURE__*/css(_templateObject$4 || (_templateObject$4 = /*#__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), _styles$1[BLOCKS.TABLE_ROW] = /*#__PURE__*/css(_templateObject2$3 || (_templateObject2$3 = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n border: 1px solid ", ";\n &:hover td {\n background-color: transparent !important;\n }\n "])), tokens.gray400), _styles$1[BLOCKS.TABLE_HEADER_CELL] = /*#__PURE__*/css(_templateObject3$3 || (_templateObject3$3 = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n background-color: ", ";\n border: 1px solid ", ";\n padding: 10px 12px;\n font-weight: ", ";\n text-align: left;\n min-width: 48px;\n position: relative;\n div:last-child {\n margin-bottom: 0;\n }\n "])), tokens.gray200, tokens.gray400, tokens.fontWeightMedium), _styles$1[BLOCKS.TABLE_CELL] = /*#__PURE__*/css(_templateObject4$1 || (_templateObject4$1 = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n border: 1px solid ", ";\n padding: 10px 12px;\n min-width: 48px;\n position: relative;\n div:last-child {\n margin-bottom: 0;\n }\n "])), tokens.gray400), _styles$1);
3642
+
3643
+ var slateNodeToText = function slateNodeToText(node) {
3644
+ var contentfulNode = toContentfulDocument({
3645
+ document: [node],
3646
+ schema: schema
3647
+ });
3648
+ return documentToPlainTextString(contentfulNode);
3649
+ };
3650
+
3630
3651
  var Table = function Table(props) {
3631
3652
  return /*#__PURE__*/createElement("table", Object.assign({}, props.attributes, {
3632
3653
  className: styles$g[BLOCKS.TABLE]
@@ -3688,6 +3709,14 @@ function addTableTrackingEvents(editor, _ref) {
3688
3709
  };
3689
3710
  }
3690
3711
 
3712
+ var paragraph = function paragraph() {
3713
+ return {
3714
+ type: BLOCKS.PARAGRAPH,
3715
+ data: {},
3716
+ children: []
3717
+ };
3718
+ };
3719
+
3691
3720
  function addTableNormalization(editor) {
3692
3721
  var normalizeNode = editor.normalizeNode;
3693
3722
 
@@ -3706,12 +3735,20 @@ function addTableNormalization(editor) {
3706
3735
  childPath = _step$value[1];
3707
3736
 
3708
3737
  if (Text.isText(child)) {
3709
- var paragraph = {
3710
- type: BLOCKS.PARAGRAPH,
3711
- data: {},
3712
- children: []
3713
- };
3714
- Transforms.wrapNodes(editor, paragraph, {
3738
+ Transforms.wrapNodes(editor, paragraph(), {
3739
+ at: childPath
3740
+ });
3741
+ } else if (!CONTAINERS[node.type].includes(child.type)) {
3742
+ var paragraphWithTextFromNode = _extends({}, paragraph(), {
3743
+ children: [{
3744
+ text: slateNodeToText(child)
3745
+ }]
3746
+ });
3747
+
3748
+ Transforms.removeNodes(editor, {
3749
+ at: childPath
3750
+ });
3751
+ Transforms.insertNodes(editor, paragraphWithTextFromNode, {
3715
3752
  at: childPath
3716
3753
  });
3717
3754
  }
@@ -3757,7 +3794,34 @@ function createWithTableEvents(tracking) {
3757
3794
 
3758
3795
  var createTablePlugin = function createTablePlugin(tracking) {
3759
3796
  return _extends({}, createTablePlugin$1(), {
3760
- onKeyDown: createWithTableEvents(tracking)
3797
+ onKeyDown: createWithTableEvents(tracking),
3798
+ withOverrides: function withOverrides(editor) {
3799
+ var insertFragment = editor.insertFragment;
3800
+
3801
+ editor.insertFragment = function (fragments) {
3802
+ // 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
3803
+ // More info: https://github.com/ianstormtaylor/slate/pull/4489 and https://github.com/ianstormtaylor/slate/issues/4542
3804
+ var fragmentHasTable = fragments.some(function (fragment) {
3805
+ return fragment.type === BLOCKS.TABLE;
3806
+ });
3807
+
3808
+ if (fragmentHasTable) {
3809
+ var emptyParagraph = {
3810
+ type: BLOCKS.PARAGRAPH,
3811
+ children: [{
3812
+ text: ''
3813
+ }],
3814
+ data: {},
3815
+ isVoid: false
3816
+ };
3817
+ Transforms.insertNodes(editor, emptyParagraph);
3818
+ }
3819
+
3820
+ insertFragment(fragments);
3821
+ };
3822
+
3823
+ return editor;
3824
+ }
3761
3825
  });
3762
3826
  };
3763
3827
  function ToolbarTableButton(props) {
@@ -4235,7 +4299,7 @@ function FetchingWrappedInlineEntryCard(props) {
4235
4299
  }, [entries, props.entryId]);
4236
4300
  var allContentTypes = props.sdk.space.getCachedContentTypes();
4237
4301
  var contentType = React__default.useMemo(function () {
4238
- if (!entry || !allContentTypes) return undefined;
4302
+ if (!entry || entry === 'failed' || !allContentTypes) return undefined;
4239
4303
  return allContentTypes.find(function (contentType) {
4240
4304
  return contentType.sys.id === entry.sys.contentType.sys.id;
4241
4305
  });
@@ -4725,7 +4789,7 @@ var removeComments = function removeComments(_ref) {
4725
4789
 
4726
4790
  var TAG_NAME_TABLE = 'TABLE';
4727
4791
  var TAG_NAME_TABLE_CAPTION = 'CAPTION';
4728
- var DISALLOWED_TABLE_CHILD_ELEMENTS = [TAG_NAME_TABLE, TAG_NAME_TABLE_CAPTION];
4792
+ var DISALLOWED_TABLE_CHILD_ELEMENTS = [TAG_NAME_TABLE_CAPTION];
4729
4793
 
4730
4794
  var isHTMLElement = function isHTMLElement(node) {
4731
4795
  return node.nodeType === Node.ELEMENT_NODE;
@@ -4850,9 +4914,8 @@ function createParagraphPlugin() {
4850
4914
  type: BLOCKS.PARAGRAPH,
4851
4915
  deserialize: function deserialize(element) {
4852
4916
  var isParagraphText = element.nodeName === 'P';
4853
- var isDivText = element.nodeName === 'DIV' && !element.getAttribute('data-entity-type');
4854
4917
  var isNotEmpty = element.textContent !== '';
4855
- var isText = (isDivText || isParagraphText) && isNotEmpty;
4918
+ var isText = isParagraphText && isNotEmpty;
4856
4919
  if (!isText) return;
4857
4920
  return {
4858
4921
  type: BLOCKS.PARAGRAPH