@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
39 lines • 1.7 kB
JavaScript
import { tableCellMinWidth } from '@atlaskit/editor-common/styles';
import { akEditorFullWidthLayoutWidth, akEditorMaxLayoutWidth } from '@atlaskit/editor-shared-styles';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { updateCellsMarkup } from './table-transform-utils';
const tableWidth = contentWidth => {
const maxEditorWidth = expValEquals('editor_tinymce_full_width_mode', 'isEnabled', true) || expValEquals('confluence_max_width_content_appearance', 'isEnabled', true) ? akEditorMaxLayoutWidth : akEditorFullWidthLayoutWidth;
return Math.min(maxEditorWidth, contentWidth);
};
export const getTableMeasurement = tableRef => {
const colWidths = getRenderedColgroupColumnWidths(tableRef);
const totalContentWidth = colWidths.reduce((acc, current) => acc + current, 0);
return {
colWidths,
tableWidth: tableWidth(totalContentWidth)
};
};
export const applyTableMeasurement = (tr, tableNode, {
colWidths,
tableWidth
}, tablePos) => {
tr = updateCellsMarkup(tr, tableNode, tablePos, (cell, _rowIndex, colIndex) => {
const newColWidths = colWidths.slice(colIndex, colIndex + cell.attrs.colspan);
return cell.type.createChecked({
...cell.attrs,
colwidth: newColWidths.length ? newColWidths : null
}, cell.content, cell.marks);
});
return tr.setNodeMarkup(tablePos, undefined, {
...tableNode.attrs,
width: tableWidth
});
};
function getRenderedColgroupColumnWidths(tableRef) {
const cols = Array.from(tableRef.querySelectorAll(':scope > colgroup > col'));
return cols.map(col => {
const width = col.getBoundingClientRect().width;
return Math.max(Math.round(width), tableCellMinWidth);
});
}