@contentful/field-editor-rich-text 2.0.0-next.28 → 2.0.0-next.30
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.
- package/dist/field-editor-rich-text.cjs.development.js +427 -144
- package/dist/field-editor-rich-text.cjs.development.js.map +1 -1
- package/dist/field-editor-rich-text.cjs.production.min.js +1 -1
- package/dist/field-editor-rich-text.cjs.production.min.js.map +1 -1
- package/dist/field-editor-rich-text.esm.js +431 -148
- package/dist/field-editor-rich-text.esm.js.map +1 -1
- package/dist/plugins/SlashCommands/SlashCommandsPalette.d.ts +5 -0
- package/dist/plugins/SlashCommands/createSlashCommandsPlugin.d.ts +2 -0
- package/dist/plugins/SlashCommands/helpers.d.ts +6 -0
- package/dist/plugins/SlashCommands/index.d.ts +2 -0
- package/dist/plugins/Table/insertTableFragment.d.ts +3 -0
- package/dist/plugins/Table/onKeyDownTable.d.ts +3 -0
- package/dist/plugins/Tracking/createTrackingPlugin.d.ts +1 -1
- package/package.json +1 -1
|
@@ -5449,6 +5449,295 @@ var createSelectOnBackspacePlugin = function createSelectOnBackspacePlugin() {
|
|
|
5449
5449
|
});
|
|
5450
5450
|
};
|
|
5451
5451
|
|
|
5452
|
+
function getCaretTopPoint() {
|
|
5453
|
+
var sel = document.getSelection();
|
|
5454
|
+
if (!sel) return;
|
|
5455
|
+
var r = sel.getRangeAt(0);
|
|
5456
|
+
var rect;
|
|
5457
|
+
var r2; // supposed to be textNode in most cases
|
|
5458
|
+
// but div[contenteditable] when empty
|
|
5459
|
+
|
|
5460
|
+
var node = r.startContainer;
|
|
5461
|
+
var offset = r.startOffset;
|
|
5462
|
+
|
|
5463
|
+
if (offset > 0) {
|
|
5464
|
+
// new range, don't influence DOM state
|
|
5465
|
+
r2 = document.createRange();
|
|
5466
|
+
r2.setStart(node, offset - 1);
|
|
5467
|
+
r2.setEnd(node, offset); // https://developer.mozilla.org/en-US/docs/Web/API/range.getBoundingClientRect
|
|
5468
|
+
// IE9, Safari?(but look good in Safari 8)
|
|
5469
|
+
|
|
5470
|
+
rect = r2.getBoundingClientRect();
|
|
5471
|
+
return {
|
|
5472
|
+
left: rect.right,
|
|
5473
|
+
top: rect.top
|
|
5474
|
+
}; // @ts-expect-error
|
|
5475
|
+
} else if (offset < node.length) {
|
|
5476
|
+
r2 = document.createRange(); // similar but select next on letter
|
|
5477
|
+
|
|
5478
|
+
r2.setStart(node, offset);
|
|
5479
|
+
r2.setEnd(node, offset + 1);
|
|
5480
|
+
rect = r2.getBoundingClientRect();
|
|
5481
|
+
return {
|
|
5482
|
+
left: rect.left,
|
|
5483
|
+
top: rect.top
|
|
5484
|
+
};
|
|
5485
|
+
} else {
|
|
5486
|
+
// textNode has length
|
|
5487
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Element.getBoundingClientRect
|
|
5488
|
+
// @ts-expect-error
|
|
5489
|
+
rect = node.getBoundingClientRect(); // @ts-expect-error
|
|
5490
|
+
|
|
5491
|
+
var styles = getComputedStyle(node);
|
|
5492
|
+
var lineHeight = parseInt(styles.lineHeight);
|
|
5493
|
+
var fontSize = parseInt(styles.fontSize); // roughly half the whitespace... but not exactly
|
|
5494
|
+
|
|
5495
|
+
var delta = (lineHeight - fontSize) / 2;
|
|
5496
|
+
return {
|
|
5497
|
+
left: rect.left,
|
|
5498
|
+
top: rect.top + delta
|
|
5499
|
+
};
|
|
5500
|
+
}
|
|
5501
|
+
}
|
|
5502
|
+
function closePanel(editorId) {
|
|
5503
|
+
document.dispatchEvent(new CustomEvent('close-rte-palette-commands', {
|
|
5504
|
+
detail: {
|
|
5505
|
+
editorId: editorId
|
|
5506
|
+
}
|
|
5507
|
+
}));
|
|
5508
|
+
}
|
|
5509
|
+
function openPanel(editorId) {
|
|
5510
|
+
document.dispatchEvent(new CustomEvent('open-rte-palette-commands', {
|
|
5511
|
+
detail: {
|
|
5512
|
+
editorId: editorId
|
|
5513
|
+
}
|
|
5514
|
+
}));
|
|
5515
|
+
}
|
|
5516
|
+
|
|
5517
|
+
// import debounce from 'lodash/debounce';
|
|
5518
|
+
var SLASH_COMMANDS_PLUGIN_KEY = 'SlashCommands'; // TODO: Explore a solution using marks and ReactDOM.createPortal to activate the commands panel
|
|
5519
|
+
|
|
5520
|
+
function createSlashCommandsPlugin() {
|
|
5521
|
+
return {
|
|
5522
|
+
key: SLASH_COMMANDS_PLUGIN_KEY,
|
|
5523
|
+
type: SLASH_COMMANDS_PLUGIN_KEY,
|
|
5524
|
+
handlers: {
|
|
5525
|
+
onClick: function onClick(editor) {
|
|
5526
|
+
return function () {
|
|
5527
|
+
closePanel(editor.id);
|
|
5528
|
+
};
|
|
5529
|
+
},
|
|
5530
|
+
onKeyDown: function onKeyDown(editor) {
|
|
5531
|
+
return function (event) {
|
|
5532
|
+
closePanel(editor.id);
|
|
5533
|
+
|
|
5534
|
+
if (event.key === '/') {
|
|
5535
|
+
openPanel(editor.id);
|
|
5536
|
+
}
|
|
5537
|
+
};
|
|
5538
|
+
}
|
|
5539
|
+
}
|
|
5540
|
+
};
|
|
5541
|
+
}
|
|
5542
|
+
|
|
5543
|
+
var style$2 = {
|
|
5544
|
+
container: function container(_ref) {
|
|
5545
|
+
var top = _ref.top,
|
|
5546
|
+
left = _ref.left;
|
|
5547
|
+
return emotion.css({
|
|
5548
|
+
position: 'fixed',
|
|
5549
|
+
top: top - 14,
|
|
5550
|
+
left: left + 10,
|
|
5551
|
+
zIndex: 1,
|
|
5552
|
+
boxShadow: '0 5px 15px rgba(0, 0, 0, 0.15)',
|
|
5553
|
+
borderRadius: '8px',
|
|
5554
|
+
userSelect: 'none'
|
|
5555
|
+
});
|
|
5556
|
+
}
|
|
5557
|
+
};
|
|
5558
|
+
function SlashCommandsPalette(_ref2) {
|
|
5559
|
+
var editorId = _ref2.editorId;
|
|
5560
|
+
|
|
5561
|
+
var _React$useState = React.useState(null),
|
|
5562
|
+
position = _React$useState[0],
|
|
5563
|
+
setPosition = _React$useState[1];
|
|
5564
|
+
|
|
5565
|
+
var _React$useState2 = React.useState(false),
|
|
5566
|
+
isOpen = _React$useState2[0],
|
|
5567
|
+
setIsOpen = _React$useState2[1]; // The user is not annoyed every time they type `/`
|
|
5568
|
+
|
|
5569
|
+
|
|
5570
|
+
var MAX_TRIES = 3;
|
|
5571
|
+
|
|
5572
|
+
var _React$useState3 = React.useState(0),
|
|
5573
|
+
currentTries = _React$useState3[0],
|
|
5574
|
+
setCurrentTries = _React$useState3[1];
|
|
5575
|
+
|
|
5576
|
+
React.useEffect(function () {
|
|
5577
|
+
function handler(event) {
|
|
5578
|
+
if (editorId !== event.detail.editorId) return;
|
|
5579
|
+
var topLeft = getCaretTopPoint();
|
|
5580
|
+
if (!topLeft) return;
|
|
5581
|
+
setPosition(topLeft);
|
|
5582
|
+
setIsOpen(true);
|
|
5583
|
+
setCurrentTries(function (curr) {
|
|
5584
|
+
return curr + 1;
|
|
5585
|
+
});
|
|
5586
|
+
}
|
|
5587
|
+
|
|
5588
|
+
document.addEventListener('open-rte-palette-commands', handler);
|
|
5589
|
+
return function () {
|
|
5590
|
+
document.removeEventListener('open-rte-palette-commands', handler);
|
|
5591
|
+
};
|
|
5592
|
+
}, [editorId]);
|
|
5593
|
+
React.useEffect(function () {
|
|
5594
|
+
function handler(event) {
|
|
5595
|
+
if (editorId !== event.detail.editorId) return;
|
|
5596
|
+
closePanel();
|
|
5597
|
+
}
|
|
5598
|
+
|
|
5599
|
+
document.addEventListener('close-rte-palette-commands', handler);
|
|
5600
|
+
return function () {
|
|
5601
|
+
return document.removeEventListener('close-rte-palette-commands', handler);
|
|
5602
|
+
};
|
|
5603
|
+
}, [editorId]);
|
|
5604
|
+
React.useEffect(function () {
|
|
5605
|
+
if (!isOpen) return;
|
|
5606
|
+
|
|
5607
|
+
function handler() {
|
|
5608
|
+
closePanel();
|
|
5609
|
+
}
|
|
5610
|
+
|
|
5611
|
+
window.addEventListener('resize', handler);
|
|
5612
|
+
window.addEventListener('scroll', handler, true);
|
|
5613
|
+
return function () {
|
|
5614
|
+
window.removeEventListener('resize', handler);
|
|
5615
|
+
window.removeEventListener('scroll', handler, true);
|
|
5616
|
+
};
|
|
5617
|
+
}, [isOpen]);
|
|
5618
|
+
|
|
5619
|
+
function closePanel() {
|
|
5620
|
+
setIsOpen(false);
|
|
5621
|
+
setPosition(null);
|
|
5622
|
+
}
|
|
5623
|
+
|
|
5624
|
+
if (!isOpen || !position || currentTries > MAX_TRIES) return null;
|
|
5625
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
5626
|
+
className: style$2.container(position),
|
|
5627
|
+
"data-test-id": "rte-slash-commands"
|
|
5628
|
+
}, /*#__PURE__*/React.createElement(f36Components.Card, null, /*#__PURE__*/React.createElement(f36Components.Text, null, "Slash commands are temporarily unavailable.")));
|
|
5629
|
+
}
|
|
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
|
+
|
|
5452
5741
|
function hasTables(nodes) {
|
|
5453
5742
|
return nodes.some(function (_ref) {
|
|
5454
5743
|
var type = _ref.type;
|
|
@@ -5461,6 +5750,10 @@ var isTableHeaderCell = function isTableHeaderCell(_ref2) {
|
|
|
5461
5750
|
return type === Contentful.BLOCKS.TABLE_HEADER_CELL;
|
|
5462
5751
|
};
|
|
5463
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
|
+
|
|
5464
5757
|
function hasHeadersOutsideFirstRow(nodes) {
|
|
5465
5758
|
return nodes.filter(function (_ref3) {
|
|
5466
5759
|
var type = _ref3.type;
|
|
@@ -5474,6 +5767,32 @@ function hasHeadersOutsideFirstRow(nodes) {
|
|
|
5474
5767
|
});
|
|
5475
5768
|
}
|
|
5476
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
|
+
|
|
5477
5796
|
function addTableTrackingEvents(editor) {
|
|
5478
5797
|
var insertData = editor.insertData;
|
|
5479
5798
|
|
|
@@ -5498,6 +5817,19 @@ function addTableTrackingEvents(editor) {
|
|
|
5498
5817
|
insertData(data);
|
|
5499
5818
|
}
|
|
5500
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
|
+
};
|
|
5501
5833
|
}
|
|
5502
5834
|
|
|
5503
5835
|
var addRow = function addRow(editor, getNextRowPath) {
|
|
@@ -5619,116 +5951,6 @@ var setHeader = function setHeader(editor, enable) {
|
|
|
5619
5951
|
});
|
|
5620
5952
|
};
|
|
5621
5953
|
|
|
5622
|
-
function insertTableAndFocusFirstCell(editor) {
|
|
5623
|
-
plateTable.insertTable(editor, {
|
|
5624
|
-
header: true
|
|
5625
|
-
});
|
|
5626
|
-
replaceEmptyParagraphWithTable(editor);
|
|
5627
|
-
}
|
|
5628
|
-
function isTableActive(editor) {
|
|
5629
|
-
var tableElements = [plateTable.ELEMENT_TABLE, plateTable.ELEMENT_TH, plateTable.ELEMENT_TR, plateTable.ELEMENT_TD];
|
|
5630
|
-
return tableElements.some(function (el) {
|
|
5631
|
-
return isBlockSelected(editor, el);
|
|
5632
|
-
});
|
|
5633
|
-
}
|
|
5634
|
-
function isTableHeaderEnabled(editor) {
|
|
5635
|
-
var tableItem = plateCore.getAbove(editor, {
|
|
5636
|
-
match: {
|
|
5637
|
-
type: Contentful.BLOCKS.TABLE
|
|
5638
|
-
}
|
|
5639
|
-
});
|
|
5640
|
-
|
|
5641
|
-
if (!tableItem) {
|
|
5642
|
-
return false;
|
|
5643
|
-
}
|
|
5644
|
-
|
|
5645
|
-
var firstRow = plateCore.getChildren(tableItem)[0];
|
|
5646
|
-
|
|
5647
|
-
if (!firstRow) {
|
|
5648
|
-
return false;
|
|
5649
|
-
}
|
|
5650
|
-
|
|
5651
|
-
return plateCore.getChildren(firstRow).every(function (_ref) {
|
|
5652
|
-
var node = _ref[0];
|
|
5653
|
-
return node.type === Contentful.BLOCKS.TABLE_HEADER_CELL;
|
|
5654
|
-
});
|
|
5655
|
-
}
|
|
5656
|
-
function replaceEmptyParagraphWithTable(editor) {
|
|
5657
|
-
var tablePath = getAncestorPathFromSelection(editor);
|
|
5658
|
-
if (!tablePath || plateCore.isFirstChild(tablePath)) return;
|
|
5659
|
-
var previousPath = slate.Path.previous(tablePath);
|
|
5660
|
-
if (!previousPath) return;
|
|
5661
|
-
|
|
5662
|
-
var _Editor$nodes = slate.Editor.nodes(editor, {
|
|
5663
|
-
at: previousPath,
|
|
5664
|
-
match: function match(node) {
|
|
5665
|
-
return node.type === Contentful.BLOCKS.PARAGRAPH;
|
|
5666
|
-
}
|
|
5667
|
-
}),
|
|
5668
|
-
nodes = _Editor$nodes[0];
|
|
5669
|
-
|
|
5670
|
-
if (!nodes) return;
|
|
5671
|
-
var previousNode = nodes[0];
|
|
5672
|
-
var isPreviousNodeTextEmpty = plateCore.isAncestorEmpty(editor, previousNode);
|
|
5673
|
-
|
|
5674
|
-
if (isPreviousNodeTextEmpty) {
|
|
5675
|
-
// Switch table with previous empty paragraph
|
|
5676
|
-
slate.Transforms.moveNodes(editor, {
|
|
5677
|
-
at: tablePath,
|
|
5678
|
-
to: previousPath
|
|
5679
|
-
}); // Remove previous paragraph that now is under the table
|
|
5680
|
-
|
|
5681
|
-
slate.Transforms.removeNodes(editor, {
|
|
5682
|
-
at: tablePath
|
|
5683
|
-
});
|
|
5684
|
-
}
|
|
5685
|
-
}
|
|
5686
|
-
/**
|
|
5687
|
-
* Returns the number of cells in a given row vs the table width
|
|
5688
|
-
*
|
|
5689
|
-
* Note: We should only get different table rows cell counts in between
|
|
5690
|
-
* normalization cycles.
|
|
5691
|
-
*/
|
|
5692
|
-
|
|
5693
|
-
var getNoOfMissingTableCellsInRow = function getNoOfMissingTableCellsInRow(editor, _ref2) {
|
|
5694
|
-
var rowPath = _ref2[1];
|
|
5695
|
-
var parent = plateCore.getParent(editor, rowPath); // This is ensured by normalization. The error is here just in case
|
|
5696
|
-
|
|
5697
|
-
if (!parent) {
|
|
5698
|
-
throw new Error('table rows must be wrapped in a table node');
|
|
5699
|
-
}
|
|
5700
|
-
|
|
5701
|
-
var tablePath = parent[1]; // The longest table row determines its width
|
|
5702
|
-
|
|
5703
|
-
var tableWidth = Math.max.apply(Math, Array.from(slate.Node.children(editor, tablePath)).map(function (_ref3) {
|
|
5704
|
-
var path = _ref3[1];
|
|
5705
|
-
return Array.from(slate.Node.children(editor, path)).length;
|
|
5706
|
-
}));
|
|
5707
|
-
var rowWidth = Array.from(slate.Node.children(editor, rowPath)).length;
|
|
5708
|
-
return tableWidth - rowWidth;
|
|
5709
|
-
};
|
|
5710
|
-
var createEmptyTableCells = function createEmptyTableCells(count) {
|
|
5711
|
-
var emptyTableCell = {
|
|
5712
|
-
type: Contentful.BLOCKS.TABLE_CELL,
|
|
5713
|
-
data: {},
|
|
5714
|
-
children: [{
|
|
5715
|
-
type: Contentful.BLOCKS.PARAGRAPH,
|
|
5716
|
-
data: {},
|
|
5717
|
-
children: [{
|
|
5718
|
-
text: ''
|
|
5719
|
-
}]
|
|
5720
|
-
}]
|
|
5721
|
-
};
|
|
5722
|
-
return new Array(count).fill(emptyTableCell);
|
|
5723
|
-
};
|
|
5724
|
-
var isNotEmpty = function isNotEmpty(editor, _ref4) {
|
|
5725
|
-
var path = _ref4[1];
|
|
5726
|
-
return Array.from(slate.Node.children(editor, path)).length !== 0;
|
|
5727
|
-
};
|
|
5728
|
-
var isTable = function isTable(node) {
|
|
5729
|
-
return slate.Element.isElement(node) && node.type === Contentful.BLOCKS.TABLE;
|
|
5730
|
-
};
|
|
5731
|
-
|
|
5732
5954
|
var styles$i = {
|
|
5733
5955
|
topRight: /*#__PURE__*/emotion.css({
|
|
5734
5956
|
position: 'absolute',
|
|
@@ -5843,42 +6065,101 @@ var TableActions = function TableActions() {
|
|
|
5843
6065
|
};
|
|
5844
6066
|
|
|
5845
6067
|
var _templateObject$7;
|
|
5846
|
-
var style$
|
|
6068
|
+
var style$3 = /*#__PURE__*/emotion.css(_templateObject$7 || (_templateObject$7 = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n border: 1px solid ", ";\n border-collapse: collapse;\n padding: 10px 12px;\n min-width: 48px;\n position: relative;\n vertical-align: top;\n\n div:last-child {\n margin-bottom: 0;\n }\n"])), tokens.gray400);
|
|
5847
6069
|
var Cell = function Cell(props) {
|
|
5848
6070
|
var isSelected = Slate.useSelected();
|
|
5849
6071
|
return /*#__PURE__*/React.createElement("td", Object.assign({}, props.attributes, props.element.data, {
|
|
5850
|
-
className: style$
|
|
6072
|
+
className: style$3
|
|
5851
6073
|
}), isSelected && /*#__PURE__*/React.createElement(TableActions, null), props.children);
|
|
5852
6074
|
};
|
|
5853
6075
|
|
|
5854
6076
|
var _templateObject$8;
|
|
5855
|
-
var style$
|
|
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);
|
|
5856
6078
|
var HeaderCell = function HeaderCell(props) {
|
|
5857
6079
|
var isSelected = Slate.useSelected();
|
|
5858
6080
|
return /*#__PURE__*/React.createElement("th", Object.assign({}, props.attributes, props.element.data, {
|
|
5859
|
-
className: style$
|
|
6081
|
+
className: style$4
|
|
5860
6082
|
}), isSelected && /*#__PURE__*/React.createElement(TableActions, null), props.children);
|
|
5861
6083
|
};
|
|
5862
6084
|
|
|
5863
6085
|
var _templateObject$9;
|
|
5864
|
-
var style$
|
|
6086
|
+
var style$5 = /*#__PURE__*/emotion.css(_templateObject$9 || (_templateObject$9 = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n border: 1px solid ", ";\n border-collapse: collapse;\n\n &:hover td {\n background-color: transparent !important;\n }\n"])), tokens.gray400);
|
|
5865
6087
|
var Row = function Row(props) {
|
|
5866
6088
|
return /*#__PURE__*/React.createElement("tr", Object.assign({}, props.attributes, {
|
|
5867
|
-
className: style$
|
|
6089
|
+
className: style$5
|
|
5868
6090
|
}), props.children);
|
|
5869
6091
|
};
|
|
5870
6092
|
|
|
5871
6093
|
var _templateObject$a;
|
|
5872
|
-
var style$
|
|
6094
|
+
var style$6 = /*#__PURE__*/emotion.css(_templateObject$a || (_templateObject$a = /*#__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);
|
|
5873
6095
|
var Table = function Table(props) {
|
|
5874
6096
|
return /*#__PURE__*/React.createElement("div", {
|
|
5875
6097
|
"data-block-type": Contentful.BLOCKS.TABLE
|
|
5876
6098
|
}, /*#__PURE__*/React.createElement("table", Object.assign({
|
|
5877
|
-
className: style$
|
|
6099
|
+
className: style$6
|
|
5878
6100
|
}, props.attributes), /*#__PURE__*/React.createElement("tbody", null, props.children)));
|
|
5879
6101
|
};
|
|
5880
6102
|
|
|
5881
|
-
|
|
6103
|
+
/**
|
|
6104
|
+
* Removes table wrappers when pasting a single table cell
|
|
6105
|
+
*
|
|
6106
|
+
* In Plate/Slate, copying the content of a table cell wraps
|
|
6107
|
+
* it in a <table><tr><td>{content}</td></tr></table> even
|
|
6108
|
+
* when copying partial cell content.
|
|
6109
|
+
*
|
|
6110
|
+
* That's really annoying as there is no way to remove the table
|
|
6111
|
+
* wrappers in that case.
|
|
6112
|
+
*/
|
|
6113
|
+
|
|
6114
|
+
var trimUnnecessaryTableWrapper = function trimUnnecessaryTableWrapper(node) {
|
|
6115
|
+
if (!slate.Element.isElement(node)) {
|
|
6116
|
+
return [node];
|
|
6117
|
+
} // must be a table with a single row
|
|
6118
|
+
|
|
6119
|
+
|
|
6120
|
+
if (node.type !== Contentful.BLOCKS.TABLE || node.children.length !== 1) {
|
|
6121
|
+
return [node];
|
|
6122
|
+
}
|
|
6123
|
+
|
|
6124
|
+
var row = node.children[0]; // the row must contain a single cell
|
|
6125
|
+
|
|
6126
|
+
if (row.children.length !== 1) {
|
|
6127
|
+
return [node];
|
|
6128
|
+
}
|
|
6129
|
+
|
|
6130
|
+
var cell = row.children[0];
|
|
6131
|
+
return cell.children;
|
|
6132
|
+
};
|
|
6133
|
+
|
|
6134
|
+
var insertTableFragment = function insertTableFragment(editor) {
|
|
6135
|
+
var insertFragment = editor.insertFragment;
|
|
6136
|
+
return function (fragments) {
|
|
6137
|
+
var _editor$selection;
|
|
6138
|
+
|
|
6139
|
+
if (!editor.selection) {
|
|
6140
|
+
return;
|
|
6141
|
+
}
|
|
6142
|
+
|
|
6143
|
+
fragments = fragments.flatMap(trimUnnecessaryTableWrapper); // 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
|
|
6144
|
+
// More info: https://github.com/ianstormtaylor/slate/pull/4489 and https://github.com/ianstormtaylor/slate/issues/4542
|
|
6145
|
+
|
|
6146
|
+
var isInsertingTable = fragments.some(function (fragment) {
|
|
6147
|
+
return isTable(fragment);
|
|
6148
|
+
});
|
|
6149
|
+
var isTableFirstFragment = fragments.findIndex(function (fragment) {
|
|
6150
|
+
return isTable(fragment);
|
|
6151
|
+
}) === 0;
|
|
6152
|
+
var currentLineHasText = plateCore.getText(editor, (_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.path) !== '';
|
|
6153
|
+
|
|
6154
|
+
if (isInsertingTable && isTableFirstFragment && currentLineHasText) {
|
|
6155
|
+
insertEmptyParagraph(editor);
|
|
6156
|
+
}
|
|
6157
|
+
|
|
6158
|
+
return insertFragment(fragments);
|
|
6159
|
+
};
|
|
6160
|
+
};
|
|
6161
|
+
|
|
6162
|
+
var onKeyDownTable = function onKeyDownTable(editor, plugin) {
|
|
5882
6163
|
var defaultHandler = plateTable.onKeyDownTable(editor, plugin);
|
|
5883
6164
|
return function (event) {
|
|
5884
6165
|
// This fixes `Cannot resolve a Slate point from DOM point: [object HTMLDivElement]` when typing while the cursor is before table
|
|
@@ -5911,6 +6192,27 @@ var createTableOnKeyDown = function createTableOnKeyDown(editor, plugin) {
|
|
|
5911
6192
|
event.stopPropagation();
|
|
5912
6193
|
return;
|
|
5913
6194
|
}
|
|
6195
|
+
} // Pressing Tab on the last cell creates a new row
|
|
6196
|
+
// Otherwise, jumping between cells is handled in the defaultKeyDownTable
|
|
6197
|
+
|
|
6198
|
+
|
|
6199
|
+
if (event.key === 'Tab' && !event.shiftKey) {
|
|
6200
|
+
event.preventDefault();
|
|
6201
|
+
var res = plateTable.getTableCellEntry(editor, {});
|
|
6202
|
+
|
|
6203
|
+
if (res) {
|
|
6204
|
+
var tableElement = res.tableElement,
|
|
6205
|
+
tableRow = res.tableRow,
|
|
6206
|
+
tableCell = res.tableCell;
|
|
6207
|
+
var isLastCell = plateCore.isLastChild(tableRow, tableCell[1]);
|
|
6208
|
+
var isLastRow = plateCore.isLastChild(tableElement, tableRow[1]);
|
|
6209
|
+
|
|
6210
|
+
if (isLastRow && isLastCell) {
|
|
6211
|
+
addRowBelow(editor); // skip default handler
|
|
6212
|
+
|
|
6213
|
+
return;
|
|
6214
|
+
}
|
|
6215
|
+
}
|
|
5914
6216
|
}
|
|
5915
6217
|
|
|
5916
6218
|
defaultHandler(event);
|
|
@@ -5923,34 +6225,13 @@ var createTablePlugin = function createTablePlugin() {
|
|
|
5923
6225
|
return plateTable.createTablePlugin({
|
|
5924
6226
|
type: Contentful.BLOCKS.TABLE,
|
|
5925
6227
|
handlers: {
|
|
5926
|
-
onKeyDown:
|
|
6228
|
+
onKeyDown: onKeyDownTable
|
|
5927
6229
|
},
|
|
5928
6230
|
withOverrides: function withOverrides(editor, plugin) {
|
|
5929
6231
|
// injects important fixes from plate's original table plugin
|
|
5930
6232
|
plateTable.withTable(editor, plugin);
|
|
5931
6233
|
addTableTrackingEvents(editor);
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
editor.insertFragment = function (fragments) {
|
|
5935
|
-
var _editor$selection;
|
|
5936
|
-
|
|
5937
|
-
// 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
|
|
5938
|
-
// More info: https://github.com/ianstormtaylor/slate/pull/4489 and https://github.com/ianstormtaylor/slate/issues/4542
|
|
5939
|
-
var isInsertingTable = fragments.some(function (fragment) {
|
|
5940
|
-
return isTable(fragment);
|
|
5941
|
-
});
|
|
5942
|
-
var isTableFirstFragment = fragments.findIndex(function (fragment) {
|
|
5943
|
-
return isTable(fragment);
|
|
5944
|
-
}) === 0;
|
|
5945
|
-
var currentLineHasText = plateCore.getText(editor, (_editor$selection = editor.selection) == null ? void 0 : _editor$selection.focus.path) !== '';
|
|
5946
|
-
|
|
5947
|
-
if (isInsertingTable && isTableFirstFragment && currentLineHasText) {
|
|
5948
|
-
insertEmptyParagraph(editor);
|
|
5949
|
-
}
|
|
5950
|
-
|
|
5951
|
-
insertFragment(fragments);
|
|
5952
|
-
};
|
|
5953
|
-
|
|
6234
|
+
editor.insertFragment = insertTableFragment(editor);
|
|
5954
6235
|
return editor;
|
|
5955
6236
|
},
|
|
5956
6237
|
overrideByKey: (_overrideByKey = {}, _overrideByKey[plateTable.ELEMENT_TABLE] = {
|
|
@@ -6398,7 +6679,7 @@ var getPlugins = function getPlugins(sdk, onAction) {
|
|
|
6398
6679
|
return [// AST must come after the HTML deserializer
|
|
6399
6680
|
plateCore.createDeserializeHtmlPlugin(), plateCore.createDeserializeAstPlugin(), plateSerializerDocx.createDeserializeDocxPlugin(), // Tracking - This should come first so all plugins below will have access to `editor.tracking`
|
|
6400
6681
|
createTrackingPlugin(onAction), // Global / Global shortcuts
|
|
6401
|
-
createDragAndDropPlugin(), // Block Elements
|
|
6682
|
+
createDragAndDropPlugin(), createSlashCommandsPlugin(), // Block Elements
|
|
6402
6683
|
createParagraphPlugin(), createListPlugin(), createHrPlugin(), createHeadingPlugin(), createQuotePlugin(), createTablePlugin(), createEmbeddedEntryBlockPlugin(sdk), createEmbeddedAssetBlockPlugin(sdk), // Inline elements
|
|
6403
6684
|
createHyperlinkPlugin(sdk), createEmbeddedEntityInlinePlugin(sdk), // Marks
|
|
6404
6685
|
createMarksPlugin(), // Other
|
|
@@ -6876,7 +7157,9 @@ var ConnectedRichTextEditor = function ConnectedRichTextEditor(props) {
|
|
|
6876
7157
|
isDisabled: props.isDisabled
|
|
6877
7158
|
}, /*#__PURE__*/React__default.createElement(Toolbar, {
|
|
6878
7159
|
isDisabled: props.isDisabled
|
|
6879
|
-
}))
|
|
7160
|
+
})), /*#__PURE__*/React__default.createElement(SlashCommandsPalette, {
|
|
7161
|
+
editorId: id
|
|
7162
|
+
})))));
|
|
6880
7163
|
};
|
|
6881
7164
|
|
|
6882
7165
|
var RichTextEditor = function RichTextEditor(props) {
|