@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
48 lines (47 loc) • 3.07 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getTableMeasurement = exports.applyTableMeasurement = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _styles = require("@atlaskit/editor-common/styles");
var _editorSharedStyles = require("@atlaskit/editor-shared-styles");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _tableTransformUtils = require("./table-transform-utils");
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; }
var tableWidth = function tableWidth(contentWidth) {
var maxEditorWidth = (0, _expValEquals.expValEquals)('editor_tinymce_full_width_mode', 'isEnabled', true) || (0, _expValEquals.expValEquals)('confluence_max_width_content_appearance', 'isEnabled', true) ? _editorSharedStyles.akEditorMaxLayoutWidth : _editorSharedStyles.akEditorFullWidthLayoutWidth;
return Math.min(maxEditorWidth, contentWidth);
};
var getTableMeasurement = exports.getTableMeasurement = function getTableMeasurement(tableRef) {
var colWidths = getRenderedColgroupColumnWidths(tableRef);
var totalContentWidth = colWidths.reduce(function (acc, current) {
return acc + current;
}, 0);
return {
colWidths: colWidths,
tableWidth: tableWidth(totalContentWidth)
};
};
var applyTableMeasurement = exports.applyTableMeasurement = function applyTableMeasurement(tr, tableNode, _ref, tablePos) {
var colWidths = _ref.colWidths,
tableWidth = _ref.tableWidth;
tr = (0, _tableTransformUtils.updateCellsMarkup)(tr, tableNode, tablePos, function (cell, _rowIndex, colIndex) {
var newColWidths = colWidths.slice(colIndex, colIndex + cell.attrs.colspan);
return cell.type.createChecked(_objectSpread(_objectSpread({}, cell.attrs), {}, {
colwidth: newColWidths.length ? newColWidths : null
}), cell.content, cell.marks);
});
return tr.setNodeMarkup(tablePos, undefined, _objectSpread(_objectSpread({}, tableNode.attrs), {}, {
width: tableWidth
}));
};
function getRenderedColgroupColumnWidths(tableRef) {
var cols = Array.from(tableRef.querySelectorAll(':scope > colgroup > col'));
return cols.map(function (col) {
var width = col.getBoundingClientRect().width;
return Math.max(Math.round(width), _styles.tableCellMinWidth);
});
}