@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
78 lines (76 loc) • 3.08 kB
JavaScript
import { tableNewColumnMinWidth } from '@atlaskit/editor-common/styles';
import { findDomRefAtPos } from '@atlaskit/editor-prosemirror/utils';
import { TableMap } from '@atlaskit/editor-tables/table-map';
import { contentWidth } from './content-width';
import { unitToNumber } from './unit-to-number';
// Reads `width` and `minWidth` of each column from DOM and returns `ColumnState` containing those values
export var getColumnStateFromDOM = function getColumnStateFromDOM(cells, index, minWidth) {
var width = calculateColumnWidth(cells, calculateColumnWidthCallback);
var minColumnWidth = width < minWidth ?
// for newly created column (where width < minWidth) we set minWidth = tableNewColumnMinWidth (140px atm)
tableNewColumnMinWidth : calculateColumnWidth(cells, calculateColumnMinWidthCallback(minWidth));
return {
index: index,
width: width < minWidth ? tableNewColumnMinWidth : width,
minWidth: minColumnWidth
};
};
export var getFreeSpace = function getFreeSpace(state) {
return Math.max(state.width - state.minWidth, 0);
};
// Returns DOM refs of all cells in a column by `columnIndex`
export var getCellsRefsInColumn = function getCellsRefsInColumn(columnIndex, table, tableStart, domAtPos) {
var map = TableMap.get(table);
var cellsPositions = map.cellsInRect({
left: columnIndex,
right: columnIndex + 1,
top: 0,
bottom: map.height
});
var cells = [];
cellsPositions.forEach(function (pos) {
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
var col = findDomRefAtPos(pos + tableStart, domAtPos);
if (col) {
cells.push(col);
}
});
return cells;
};
// calculates column widths based on `cells` DOM refs
export var calculateColumnWidth = function calculateColumnWidth(cells, calculateColumnWidthCb) {
var maxColWidth = 0;
var colSpanWidth = 0;
cells.forEach(function (cellRef) {
var css = getComputedStyle(cellRef);
var colspan = Number(cellRef.getAttribute('colspan') || 1);
if (colspan > 1) {
colSpanWidth = calculateColumnWidthCb(css, cellRef, colspan);
return;
}
if (css) {
var colWidth = calculateColumnWidthCb(css, cellRef, colspan);
maxColWidth = Math.max(colWidth, maxColWidth);
}
});
return maxColWidth || colSpanWidth;
};
export var addContainerLeftRightPadding = function addContainerLeftRightPadding(amount, css) {
return amount + unitToNumber(css.paddingLeft) + unitToNumber(css.paddingRight);
};
function calculateColumnWidthCallback(css) {
return unitToNumber(css.width);
}
function calculateColumnMinWidthCallback(minColumnWidth) {
return function (css, cellRef, colSpan) {
if (colSpan && colSpan > 1) {
return unitToNumber(css.width);
}
var _contentWidth = contentWidth(cellRef, cellRef),
minContentWidth = _contentWidth.minWidth;
// Override the min width, if there is content that can't collapse
// Past a certain width.
return Math.max(addContainerLeftRightPadding(minContentWidth, css), minContentWidth, minColumnWidth);
};
}