@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
100 lines (99 loc) • 3.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getTableTopAndBottomCellEdgeAttrs = exports.getRowCellEdgeAttrs = exports.applyTableCellEdgeAttrs = exports.applyCellEdgeAttrs = void 0;
var _tableMap = require("@atlaskit/editor-tables/table-map");
var getTableTopAndBottomCellEdgeAttrs = exports.getTableTopAndBottomCellEdgeAttrs = function getTableTopAndBottomCellEdgeAttrs(tableMap) {
var edgeAttrsByOffset = new Map();
var addLogicalRow = function addLogicalRow(offsets, edge) {
offsets.forEach(function (offset, columnIndex) {
var _edgeAttrsByOffset$ge;
var edgeAttrs = (_edgeAttrsByOffset$ge = edgeAttrsByOffset.get(offset)) !== null && _edgeAttrsByOffset$ge !== void 0 ? _edgeAttrsByOffset$ge : {
reachesBottom: false,
reachesLeft: false,
reachesRight: false,
reachesTop: false
};
edgeAttrs.reachesBottom || (edgeAttrs.reachesBottom = edge === 'bottom');
edgeAttrs.reachesLeft || (edgeAttrs.reachesLeft = columnIndex === 0);
edgeAttrs.reachesRight || (edgeAttrs.reachesRight = columnIndex === tableMap.width - 1);
edgeAttrs.reachesTop || (edgeAttrs.reachesTop = edge === 'top');
edgeAttrsByOffset.set(offset, edgeAttrs);
});
};
var topRow = tableMap.mapByRow[0];
var bottomRow = tableMap.mapByRow[tableMap.height - 1];
if (topRow) {
addLogicalRow(topRow, 'top');
}
if (bottomRow) {
addLogicalRow(bottomRow, 'bottom');
}
return edgeAttrsByOffset;
};
var getRowCellEdgeAttrs = exports.getRowCellEdgeAttrs = function getRowCellEdgeAttrs(_ref) {
var edgeAttrsByOffset = _ref.edgeAttrsByOffset,
rowNode = _ref.rowNode,
rowStart = _ref.rowStart;
var cellEdgeAttrs = [];
rowNode.content.forEach(function (cellNode, cellOffset) {
if (cellNode.type.name !== 'tableCell' && cellNode.type.name !== 'tableHeader') {
return;
}
cellEdgeAttrs.push(edgeAttrsByOffset.get(rowStart + 1 + cellOffset));
});
return cellEdgeAttrs;
};
var applyCellEdgeAttrs = exports.applyCellEdgeAttrs = function applyCellEdgeAttrs(cell, edgeAttrs) {
if (!edgeAttrs) {
return;
}
if (edgeAttrs.reachesTop) {
cell.setAttribute('data-reaches-top', 'true');
}
if (edgeAttrs.reachesBottom) {
cell.setAttribute('data-reaches-bottom', 'true');
}
if (edgeAttrs.reachesLeft) {
cell.setAttribute('data-reaches-left', 'true');
}
if (edgeAttrs.reachesRight) {
cell.setAttribute('data-reaches-right', 'true');
}
};
var applyTableCellEdgeAttrs = exports.applyTableCellEdgeAttrs = function applyTableCellEdgeAttrs(_ref2) {
var element = _ref2.element,
tableNode = _ref2.tableNode;
try {
var table = element instanceof HTMLTableElement ? element : element.querySelector('table');
if (!(table instanceof HTMLTableElement)) {
return;
}
var tableMap = _tableMap.TableMap.get(tableNode);
var cells = Array.from(table.rows).flatMap(function (row) {
return Array.from(row.cells);
});
var cellsByOffset = new Map();
var cellIndex = 0;
tableNode.content.forEach(function (rowNode, rowStart) {
rowNode.content.forEach(function (cellNode, cellOffset) {
if (cellNode.type.name === 'tableCell' || cellNode.type.name === 'tableHeader') {
var cell = cells[cellIndex];
cellIndex++;
if (cell) {
cellsByOffset.set(rowStart + 1 + cellOffset, cell);
}
}
});
});
getTableTopAndBottomCellEdgeAttrs(tableMap).forEach(function (edgeAttrs, offset) {
var cell = cellsByOffset.get(offset);
if (cell) {
applyCellEdgeAttrs(cell, edgeAttrs);
}
});
} catch (_unused) {
// Table structure can be transient while widget DOM is being assembled.
}
};