@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
40 lines (39 loc) • 1.47 kB
JavaScript
import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
import { cellWrapping, splitCellWithType } from '@atlaskit/editor-tables/utils';
import { getPluginState } from '../plugin-factory';
export var canSplitCellSelection = function canSplitCellSelection(selection) {
var cellNode;
if (!(selection instanceof CellSelection)) {
cellNode = cellWrapping(selection.$from);
if (!cellNode) {
return false;
}
} else {
if (selection.$anchorCell.pos !== selection.$headCell.pos) {
return false;
}
cellNode = selection.$anchorCell.nodeAfter;
}
return Boolean(cellNode && (cellNode.attrs.colspan !== 1 || cellNode.attrs.rowspan !== 1));
};
/**
* We need to split cell keeping the right type of cell given current table configuration.
* We are using editor-tables splitCellWithType that allows you to choose what cell type should be.
*/
export var splitCell = function splitCell(state, dispatch) {
var tableState = getPluginState(state);
var _state$schema$nodes = state.schema.nodes,
tableHeader = _state$schema$nodes.tableHeader,
tableCell = _state$schema$nodes.tableCell;
if (dispatch) {
return splitCellWithType(function (_ref) {
var row = _ref.row,
col = _ref.col;
if (row === 0 && tableState.isHeaderRowEnabled || col === 0 && tableState.isHeaderColumnEnabled) {
return tableHeader;
}
return tableCell;
})(state, dispatch);
}
return true;
};