UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

81 lines (76 loc) 4.03 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.splitCellsInColumns = splitCellsInColumns; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _tableMap = require("@atlaskit/editor-tables/table-map"); function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /** * Helper to split all the cells in a range of columns * @param tr * @param tablePos * @param columnStart - Start of the rect included (rect.left) * @param columnEnd - End of the rect not included (rect.right) */ function splitCellsInColumns(tr, tablePos, columnStart, columnEnd) { var mapStart = tr.mapping.maps.length; var table = tr.doc.nodeAt(tablePos); if (!table) { return tr; } var tableStart = tr.doc.resolve(tablePos).start(1); var map = _tableMap.TableMap.get(table); for (var column = columnStart; column < columnEnd; column++) { for (var rowIndex = 0; rowIndex < map.height; rowIndex++) { var cellIndex = rowIndex * map.width + column; var cellPos = map.map[cellIndex]; // Check if the cell is contained by another by another row/column var hasMergedCellsBefore = column > 0 && map.map[cellIndex - 1] === cellPos || rowIndex > 0 && map.map[(rowIndex - 1) * map.width + column] === cellPos; // Check if the cell contains another row/column var hasMergedCellsAfter = column < map.width - 1 && map.map[cellIndex + 1] === cellPos || rowIndex < map.height - 1 && map.map[(rowIndex + 1) * map.width + column] === cellPos; if (!hasMergedCellsBefore && hasMergedCellsAfter) { // Is a merged cell that start in this row/column var cellNode = table.nodeAt(cellPos); if (!cellNode) { continue; } var _ref = cellNode.attrs, colwidth = _ref.colwidth, _ref$colspan = _ref.colspan, colspan = _ref$colspan === void 0 ? 1 : _ref$colspan, _ref$rowspan = _ref.rowspan, rowspan = _ref$rowspan === void 0 ? 1 : _ref$rowspan; var mapping = tr.mapping.slice(mapStart); // Update current node with the simple colspan var baseAttrs = _objectSpread(_objectSpread({}, cellNode.attrs), {}, { colspan: 1, rowspan: 1 }); // Add the new cells for (var cellRowIndex = rowIndex; cellRowIndex < rowIndex + rowspan; cellRowIndex++) { for (var i = 0; i < colspan; i++) { var _mapping = tr.mapping.slice(mapStart); var _cellPos = map.positionAt(cellRowIndex, column + i, table); tr.insert(_mapping.map(_cellPos + tableStart), // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion cellNode.type.createAndFill(_objectSpread(_objectSpread({}, baseAttrs), {}, { colwidth: colwidth ? [colwidth[i]] : undefined }))); } } // Delete the original cell mapping = tr.mapping.slice(mapStart); tr.delete(mapping.map(cellPos + tableStart), mapping.map(cellPos + tableStart + cellNode.nodeSize)); // Skip rows based on rowspan if (rowspan && rowspan > 1) { rowIndex += rowspan - 1; } } } } return tr; }