@contentful/field-editor-rich-text 2.0.0-next.31 → 2.0.0-next.32

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.
@@ -5628,210 +5628,6 @@ function SlashCommandsPalette(_ref2) {
5628
5628
  }, /*#__PURE__*/React.createElement(f36Components.Card, null, /*#__PURE__*/React.createElement(f36Components.Text, null, "Slash commands are temporarily unavailable.")));
5629
5629
  }
5630
5630
 
5631
- function insertTableAndFocusFirstCell(editor) {
5632
- plateTable.insertTable(editor, {
5633
- header: true
5634
- });
5635
- replaceEmptyParagraphWithTable(editor);
5636
- }
5637
- function isTableActive(editor) {
5638
- var tableElements = [plateTable.ELEMENT_TABLE, plateTable.ELEMENT_TH, plateTable.ELEMENT_TR, plateTable.ELEMENT_TD];
5639
- return tableElements.some(function (el) {
5640
- return isBlockSelected(editor, el);
5641
- });
5642
- }
5643
- function isTableHeaderEnabled(editor) {
5644
- var tableItem = plateCore.getAbove(editor, {
5645
- match: {
5646
- type: Contentful.BLOCKS.TABLE
5647
- }
5648
- });
5649
-
5650
- if (!tableItem) {
5651
- return false;
5652
- }
5653
-
5654
- var firstRow = plateCore.getChildren(tableItem)[0];
5655
-
5656
- if (!firstRow) {
5657
- return false;
5658
- }
5659
-
5660
- return plateCore.getChildren(firstRow).every(function (_ref) {
5661
- var node = _ref[0];
5662
- return node.type === Contentful.BLOCKS.TABLE_HEADER_CELL;
5663
- });
5664
- }
5665
- function replaceEmptyParagraphWithTable(editor) {
5666
- var tablePath = getAncestorPathFromSelection(editor);
5667
- if (!tablePath || plateCore.isFirstChild(tablePath)) return;
5668
- var previousPath = slate.Path.previous(tablePath);
5669
- if (!previousPath) return;
5670
-
5671
- var _Editor$nodes = slate.Editor.nodes(editor, {
5672
- at: previousPath,
5673
- match: function match(node) {
5674
- return node.type === Contentful.BLOCKS.PARAGRAPH;
5675
- }
5676
- }),
5677
- nodes = _Editor$nodes[0];
5678
-
5679
- if (!nodes) return;
5680
- var previousNode = nodes[0];
5681
- var isPreviousNodeTextEmpty = plateCore.isAncestorEmpty(editor, previousNode);
5682
-
5683
- if (isPreviousNodeTextEmpty) {
5684
- // Switch table with previous empty paragraph
5685
- slate.Transforms.moveNodes(editor, {
5686
- at: tablePath,
5687
- to: previousPath
5688
- }); // Remove previous paragraph that now is under the table
5689
-
5690
- slate.Transforms.removeNodes(editor, {
5691
- at: tablePath
5692
- });
5693
- }
5694
- }
5695
- /**
5696
- * Returns the number of cells in a given row vs the table width
5697
- *
5698
- * Note: We should only get different table rows cell counts in between
5699
- * normalization cycles.
5700
- */
5701
-
5702
- var getNoOfMissingTableCellsInRow = function getNoOfMissingTableCellsInRow(editor, _ref2) {
5703
- var rowPath = _ref2[1];
5704
- var parent = plateCore.getParent(editor, rowPath); // This is ensured by normalization. The error is here just in case
5705
-
5706
- if (!parent) {
5707
- throw new Error('table rows must be wrapped in a table node');
5708
- }
5709
-
5710
- var tablePath = parent[1]; // The longest table row determines its width
5711
-
5712
- var tableWidth = Math.max.apply(Math, Array.from(slate.Node.children(editor, tablePath)).map(function (_ref3) {
5713
- var path = _ref3[1];
5714
- return Array.from(slate.Node.children(editor, path)).length;
5715
- }));
5716
- var rowWidth = Array.from(slate.Node.children(editor, rowPath)).length;
5717
- return tableWidth - rowWidth;
5718
- };
5719
- var createEmptyTableCells = function createEmptyTableCells(count) {
5720
- var emptyTableCell = {
5721
- type: Contentful.BLOCKS.TABLE_CELL,
5722
- data: {},
5723
- children: [{
5724
- type: Contentful.BLOCKS.PARAGRAPH,
5725
- data: {},
5726
- children: [{
5727
- text: ''
5728
- }]
5729
- }]
5730
- };
5731
- return new Array(count).fill(emptyTableCell);
5732
- };
5733
- var isNotEmpty = function isNotEmpty(editor, _ref4) {
5734
- var path = _ref4[1];
5735
- return Array.from(slate.Node.children(editor, path)).length !== 0;
5736
- };
5737
- var isTable = function isTable(node) {
5738
- return slate.Element.isElement(node) && node.type === Contentful.BLOCKS.TABLE;
5739
- };
5740
-
5741
- function hasTables(nodes) {
5742
- return nodes.some(function (_ref) {
5743
- var type = _ref.type;
5744
- return type === Contentful.BLOCKS.TABLE;
5745
- });
5746
- }
5747
-
5748
- var isTableHeaderCell = function isTableHeaderCell(_ref2) {
5749
- var type = _ref2.type;
5750
- return type === Contentful.BLOCKS.TABLE_HEADER_CELL;
5751
- };
5752
-
5753
- var isTableCellOrHeader = function isTableCellOrHeader(node) {
5754
- return slate.Element.isElement(node) && [Contentful.BLOCKS.TABLE_HEADER_CELL, Contentful.BLOCKS.TABLE_CELL].includes(node.type);
5755
- };
5756
-
5757
- function hasHeadersOutsideFirstRow(nodes) {
5758
- return nodes.filter(function (_ref3) {
5759
- var type = _ref3.type;
5760
- return type === Contentful.BLOCKS.TABLE;
5761
- }).flatMap(function (_ref4) {
5762
- var children = _ref4.children;
5763
- return children.slice(1);
5764
- }).some(function (_ref5) {
5765
- var children = _ref5.children;
5766
- return children.some(isTableHeaderCell);
5767
- });
5768
- }
5769
-
5770
- function trackInvalidTableCellChildren(editor, table) {
5771
- var cells = table.children.flatMap(function (row) {
5772
- var _row$children;
5773
-
5774
- return (_row$children = row.children) != null ? _row$children : [];
5775
- }).filter(isTableCellOrHeader); // get invalid direct children
5776
-
5777
- var invalidNodeTypes = cells.flatMap(function (cell) {
5778
- // Only paragraphs are allowed inside tables at the moment
5779
- return cell.children.filter(function (node) {
5780
- return slate.Element.isElement(node) && node.type !== Contentful.BLOCKS.PARAGRAPH;
5781
- }).map(function (node) {
5782
- return node.type;
5783
- });
5784
- });
5785
-
5786
- for (var _iterator = _createForOfIteratorHelperLoose(new Set(invalidNodeTypes)), _step; !(_step = _iterator()).done;) {
5787
- var _editor$tracking;
5788
-
5789
- var nodeType = _step.value;
5790
- (_editor$tracking = editor.tracking) == null ? void 0 : _editor$tracking.onViewportAction('invalidTablePaste', {
5791
- nodeType: nodeType
5792
- });
5793
- }
5794
- }
5795
-
5796
- function addTableTrackingEvents(editor) {
5797
- var insertData = editor.insertData;
5798
-
5799
- editor.insertData = function (data) {
5800
- var html = data.getData('text/html');
5801
-
5802
- if (html) {
5803
- var markupBefore = editor.children;
5804
- insertData(data);
5805
- var markupAfter = editor.children;
5806
- setTimeout(function () {
5807
- if (hasTables(markupBefore)) return;
5808
-
5809
- if (hasTables(markupAfter)) {
5810
- editor.tracking.onViewportAction('paste', {
5811
- tablePasted: true,
5812
- hasHeadersOutsideFirstRow: hasHeadersOutsideFirstRow(markupAfter)
5813
- });
5814
- }
5815
- }, 1);
5816
- } else {
5817
- insertData(data);
5818
- }
5819
- };
5820
-
5821
- var insertFragment = editor.insertFragment;
5822
-
5823
- editor.insertFragment = function (fragment) {
5824
- var tables = fragment.filter(isTable);
5825
-
5826
- for (var _iterator2 = _createForOfIteratorHelperLoose(tables), _step2; !(_step2 = _iterator2()).done;) {
5827
- var table = _step2.value;
5828
- trackInvalidTableCellChildren(editor, table);
5829
- }
5830
-
5831
- return insertFragment(fragment);
5832
- };
5833
- }
5834
-
5835
5631
  var addRow = function addRow(editor, getNextRowPath) {
5836
5632
  if (plateCore.someNode(editor, {
5837
5633
  match: {
@@ -5951,6 +5747,116 @@ var setHeader = function setHeader(editor, enable) {
5951
5747
  });
5952
5748
  };
5953
5749
 
5750
+ function insertTableAndFocusFirstCell(editor) {
5751
+ plateTable.insertTable(editor, {
5752
+ header: true
5753
+ });
5754
+ replaceEmptyParagraphWithTable(editor);
5755
+ }
5756
+ function isTableActive(editor) {
5757
+ var tableElements = [plateTable.ELEMENT_TABLE, plateTable.ELEMENT_TH, plateTable.ELEMENT_TR, plateTable.ELEMENT_TD];
5758
+ return tableElements.some(function (el) {
5759
+ return isBlockSelected(editor, el);
5760
+ });
5761
+ }
5762
+ function isTableHeaderEnabled(editor) {
5763
+ var tableItem = plateCore.getAbove(editor, {
5764
+ match: {
5765
+ type: Contentful.BLOCKS.TABLE
5766
+ }
5767
+ });
5768
+
5769
+ if (!tableItem) {
5770
+ return false;
5771
+ }
5772
+
5773
+ var firstRow = plateCore.getChildren(tableItem)[0];
5774
+
5775
+ if (!firstRow) {
5776
+ return false;
5777
+ }
5778
+
5779
+ return plateCore.getChildren(firstRow).every(function (_ref) {
5780
+ var node = _ref[0];
5781
+ return node.type === Contentful.BLOCKS.TABLE_HEADER_CELL;
5782
+ });
5783
+ }
5784
+ function replaceEmptyParagraphWithTable(editor) {
5785
+ var tablePath = getAncestorPathFromSelection(editor);
5786
+ if (!tablePath || plateCore.isFirstChild(tablePath)) return;
5787
+ var previousPath = slate.Path.previous(tablePath);
5788
+ if (!previousPath) return;
5789
+
5790
+ var _Editor$nodes = slate.Editor.nodes(editor, {
5791
+ at: previousPath,
5792
+ match: function match(node) {
5793
+ return node.type === Contentful.BLOCKS.PARAGRAPH;
5794
+ }
5795
+ }),
5796
+ nodes = _Editor$nodes[0];
5797
+
5798
+ if (!nodes) return;
5799
+ var previousNode = nodes[0];
5800
+ var isPreviousNodeTextEmpty = plateCore.isAncestorEmpty(editor, previousNode);
5801
+
5802
+ if (isPreviousNodeTextEmpty) {
5803
+ // Switch table with previous empty paragraph
5804
+ slate.Transforms.moveNodes(editor, {
5805
+ at: tablePath,
5806
+ to: previousPath
5807
+ }); // Remove previous paragraph that now is under the table
5808
+
5809
+ slate.Transforms.removeNodes(editor, {
5810
+ at: tablePath
5811
+ });
5812
+ }
5813
+ }
5814
+ /**
5815
+ * Returns the number of cells in a given row vs the table width
5816
+ *
5817
+ * Note: We should only get different table rows cell counts in between
5818
+ * normalization cycles.
5819
+ */
5820
+
5821
+ var getNoOfMissingTableCellsInRow = function getNoOfMissingTableCellsInRow(editor, _ref2) {
5822
+ var rowPath = _ref2[1];
5823
+ var parent = plateCore.getParent(editor, rowPath); // This is ensured by normalization. The error is here just in case
5824
+
5825
+ if (!parent) {
5826
+ throw new Error('table rows must be wrapped in a table node');
5827
+ }
5828
+
5829
+ var tablePath = parent[1]; // The longest table row determines its width
5830
+
5831
+ var tableWidth = Math.max.apply(Math, Array.from(slate.Node.children(editor, tablePath)).map(function (_ref3) {
5832
+ var path = _ref3[1];
5833
+ return Array.from(slate.Node.children(editor, path)).length;
5834
+ }));
5835
+ var rowWidth = Array.from(slate.Node.children(editor, rowPath)).length;
5836
+ return tableWidth - rowWidth;
5837
+ };
5838
+ var createEmptyTableCells = function createEmptyTableCells(count) {
5839
+ var emptyTableCell = {
5840
+ type: Contentful.BLOCKS.TABLE_CELL,
5841
+ data: {},
5842
+ children: [{
5843
+ type: Contentful.BLOCKS.PARAGRAPH,
5844
+ data: {},
5845
+ children: [{
5846
+ text: ''
5847
+ }]
5848
+ }]
5849
+ };
5850
+ return new Array(count).fill(emptyTableCell);
5851
+ };
5852
+ var isNotEmpty = function isNotEmpty(editor, _ref4) {
5853
+ var path = _ref4[1];
5854
+ return Array.from(slate.Node.children(editor, path)).length !== 0;
5855
+ };
5856
+ var isTable = function isTable(node) {
5857
+ return slate.Element.isElement(node) && node.type === Contentful.BLOCKS.TABLE;
5858
+ };
5859
+
5954
5860
  var styles$i = {
5955
5861
  topRight: /*#__PURE__*/emotion.css({
5956
5862
  position: 'absolute',
@@ -6074,7 +5980,7 @@ var Cell = function Cell(props) {
6074
5980
  };
6075
5981
 
6076
5982
  var _templateObject$8;
6077
- var style$4 = /*#__PURE__*/emotion.css(_templateObject$8 || (_templateObject$8 = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n background-color: ", ";\n border: 1px solid ", ";\n border-collapse: collapse;\n padding: 10px 12px;\n font-weight: ", ";\n text-align: left;\n min-width: 48px;\n position: relative;\n\n div:last-child {\n margin-bottom: 0;\n }\n"])), tokens.gray200, tokens.gray400, tokens.fontWeightNormal);
5983
+ var style$4 = /*#__PURE__*/emotion.css(_templateObject$8 || (_templateObject$8 = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n background-clip: padding-box;\n background-color: ", ";\n border: 1px solid ", ";\n border-collapse: collapse;\n padding: 10px 12px;\n font-weight: ", ";\n text-align: left;\n min-width: 48px;\n position: relative;\n\n div:last-child {\n margin-bottom: 0;\n }\n"])), tokens.gray200, tokens.gray400, tokens.fontWeightNormal);
6078
5984
  var HeaderCell = function HeaderCell(props) {
6079
5985
  var isSelected = Slate.useSelected();
6080
5986
  return /*#__PURE__*/React.createElement("th", Object.assign({}, props.attributes, props.element.data, {
@@ -6221,6 +6127,72 @@ var onKeyDownTable = function onKeyDownTable(editor, plugin) {
6221
6127
  };
6222
6128
  };
6223
6129
 
6130
+ function hasTables(nodes) {
6131
+ return nodes.some(function (_ref) {
6132
+ var type = _ref.type;
6133
+ return type === Contentful.BLOCKS.TABLE;
6134
+ });
6135
+ }
6136
+
6137
+ var isTableHeaderCell = function isTableHeaderCell(_ref2) {
6138
+ var type = _ref2.type;
6139
+ return type === Contentful.BLOCKS.TABLE_HEADER_CELL;
6140
+ };
6141
+
6142
+ function hasHeadersOutsideFirstRow(nodes) {
6143
+ return nodes.filter(function (_ref3) {
6144
+ var type = _ref3.type;
6145
+ return type === Contentful.BLOCKS.TABLE;
6146
+ }).flatMap(function (_ref4) {
6147
+ var children = _ref4.children;
6148
+ return children.slice(1);
6149
+ }).some(function (_ref5) {
6150
+ var children = _ref5.children;
6151
+ return children.some(isTableHeaderCell);
6152
+ });
6153
+ }
6154
+
6155
+ function addTableTrackingEvents(editor) {
6156
+ var insertData = editor.insertData;
6157
+
6158
+ editor.insertData = function (data) {
6159
+ var html = data.getData('text/html');
6160
+
6161
+ if (html) {
6162
+ var markupBefore = editor.children;
6163
+ insertData(data);
6164
+ var markupAfter = editor.children;
6165
+ setTimeout(function () {
6166
+ if (hasTables(markupBefore)) return;
6167
+
6168
+ if (hasTables(markupAfter)) {
6169
+ editor.tracking.onViewportAction('paste', {
6170
+ tablePasted: true,
6171
+ hasHeadersOutsideFirstRow: hasHeadersOutsideFirstRow(markupAfter)
6172
+ });
6173
+ }
6174
+ }, 1);
6175
+ } else {
6176
+ insertData(data);
6177
+ }
6178
+ };
6179
+ }
6180
+ var withInvalidCellChildrenTracking = function withInvalidCellChildrenTracking(transformer) {
6181
+ return function (editor, childEntry) {
6182
+ var node = childEntry[0];
6183
+
6184
+ if (slate.Element.isElement(node)) {
6185
+ var _editor$tracking;
6186
+
6187
+ (_editor$tracking = editor.tracking) == null ? void 0 : _editor$tracking.onViewportAction('invalidTablePaste', {
6188
+ nodeType: node.type
6189
+ });
6190
+ }
6191
+
6192
+ return transformer(editor, childEntry);
6193
+ };
6194
+ };
6195
+
6224
6196
  var createTablePlugin = function createTablePlugin() {
6225
6197
  var _overrideByKey;
6226
6198
 
@@ -6294,14 +6266,14 @@ var createTablePlugin = function createTablePlugin() {
6294
6266
  component: HeaderCell,
6295
6267
  normalizer: [{
6296
6268
  validChildren: Contentful.CONTAINERS[Contentful.BLOCKS.TABLE_HEADER_CELL],
6297
- transform: transformParagraphs
6269
+ transform: withInvalidCellChildrenTracking(transformParagraphs)
6298
6270
  }]
6299
6271
  }, _overrideByKey[plateTable.ELEMENT_TD] = {
6300
6272
  type: Contentful.BLOCKS.TABLE_CELL,
6301
6273
  component: Cell,
6302
6274
  normalizer: [{
6303
6275
  validChildren: Contentful.CONTAINERS[Contentful.BLOCKS.TABLE_CELL],
6304
- transform: transformParagraphs
6276
+ transform: withInvalidCellChildrenTracking(transformParagraphs)
6305
6277
  }]
6306
6278
  }, _overrideByKey)
6307
6279
  });
@@ -6565,6 +6537,43 @@ var actionOrigin = {
6565
6537
  VIEWPORT: 'viewport-interaction',
6566
6538
  COMMAND_PALETTE: 'command-palette'
6567
6539
  };
6540
+
6541
+ function getPastingSource(data) {
6542
+ var textHtml = data.getData('text/html');
6543
+ var doc = new DOMParser().parseFromString(textHtml, 'text/html');
6544
+
6545
+ if (doc.querySelector('[id*="docs-internal-guid"]')) {
6546
+ return 'Google Docs';
6547
+ }
6548
+
6549
+ if (doc.querySelector('google-sheets-html-origin') || doc.querySelector('[data-sheets-value]')) {
6550
+ return 'Google Spreadsheets';
6551
+ }
6552
+
6553
+ if (doc.querySelector('meta[content*="Microsoft Excel"]')) {
6554
+ return 'Microsoft Excel';
6555
+ }
6556
+
6557
+ if (doc.querySelector('meta[content*="Microsoft Word"]')) {
6558
+ return 'Microsoft Word';
6559
+ } // TODO: MS Word Online doesn't give us specific tags, we might need to have a closer look at its tracking result since we are using generic values to identify it
6560
+
6561
+
6562
+ if (doc.querySelector('[style*="Arial_MSFontService"]') && (doc.querySelector('.TextRun') || doc.querySelector('.OutlineElement'))) {
6563
+ return 'Microsoft Word Online';
6564
+ }
6565
+
6566
+ if (doc.querySelector('meta[content="Cocoa HTML Writer"]')) {
6567
+ return 'Apple Notes';
6568
+ }
6569
+
6570
+ if (doc.querySelector('[style*="Slack-Lato, Slack-Fractions"]')) {
6571
+ return 'Slack';
6572
+ }
6573
+
6574
+ return '';
6575
+ }
6576
+
6568
6577
  var createTrackingPlugin = function createTrackingPlugin(onAction) {
6569
6578
  var trackingActions = {
6570
6579
  onViewportAction: function onViewportAction(actionName, data) {
@@ -6620,11 +6629,18 @@ var createTrackingPlugin = function createTrackingPlugin(onAction) {
6620
6629
  var characterCountBefore = getCharacterCount(editor);
6621
6630
  setTimeout(function () {
6622
6631
  var characterCountAfter = getCharacterCount(editor);
6623
- trackingActions.onShortcutAction('paste', {
6632
+ var payload = {
6624
6633
  characterCountAfter: characterCountAfter,
6625
6634
  characterCountBefore: characterCountBefore,
6626
6635
  characterCountSelection: characterCountSelection
6627
- });
6636
+ };
6637
+ var source = getPastingSource(data);
6638
+
6639
+ if (source) {
6640
+ payload.source = source;
6641
+ }
6642
+
6643
+ trackingActions.onShortcutAction('paste', payload);
6628
6644
  });
6629
6645
  }
6630
6646