@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
87 lines (84 loc) • 3.42 kB
JavaScript
import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
import { TableMap } from '@atlaskit/editor-tables/table-map';
import { findTableClosestToPos, getSelectionRect } from '@atlaskit/editor-tables/utils';
export var getSelectedColumnIndexes = function getSelectedColumnIndexes(selectionRect) {
var columnIndexes = [];
for (var i = selectionRect.left; i < selectionRect.right; i++) {
columnIndexes.push(i);
}
return columnIndexes;
};
export var getSelectedRowIndexes = function getSelectedRowIndexes(selectionRect) {
var rowIndexes = [];
for (var i = selectionRect.top; i < selectionRect.bottom; i++) {
rowIndexes.push(i);
}
return rowIndexes;
};
/**
* Treats a column as fully selected when its first-row area is covered by a horizontal merge and
* the explicit `CellSelection` starts below that merged cell. `CellSelection.isColSelection()`
* returns `false` in this case because the selection does not include the merged first-row cell.
*/
export var isColumnSelectionWithMergedFirstRow = function isColumnSelectionWithMergedFirstRow(selection) {
var rect = getSelectionRect(selection);
if (!rect) {
return false;
}
var table = findTableClosestToPos(selection.$anchorCell);
if (!table) {
return false;
}
var map = TableMap.get(table.node);
if (rect.bottom !== map.height || rect.top === 0) {
return false;
}
for (var col = rect.left; col < rect.right; col++) {
var topCellRect = map.findCell(map.map[col]);
var isHorizontallyMerged = topCellRect.right - topCellRect.left > 1;
var bridgesGapAboveSelection = topCellRect.bottom >= rect.top;
if (!isHorizontallyMerged || !bridgesGapAboveSelection) {
return false;
}
}
return true;
};
/**
* Treats a row as fully selected when its first-column area is covered by a vertical merge and the
* explicit `CellSelection` starts to the right of that merged cell. `CellSelection.isRowSelection()`
* returns `false` in this case because the selection does not include the merged first-column cell.
*/
export var isRowSelectionWithMergedFirstColumn = function isRowSelectionWithMergedFirstColumn(selection) {
var rect = getSelectionRect(selection);
if (!rect) {
return false;
}
var table = findTableClosestToPos(selection.$anchorCell);
if (!table) {
return false;
}
var map = TableMap.get(table.node);
if (rect.right !== map.width || rect.left === 0) {
return false;
}
for (var row = rect.top; row < rect.bottom; row++) {
var leftCellRect = map.findCell(map.map[row * map.width]);
var isVerticallyMerged = leftCellRect.bottom - leftCellRect.top > 1;
var bridgesGapLeftOfSelection = leftCellRect.right >= rect.left;
if (!isVerticallyMerged || !bridgesGapLeftOfSelection) {
return false;
}
}
return true;
};
/**
* Returns `true` when the selection covers one or more complete rows or columns (the kind of
* selection produced by clicking a row/column drag handle), including the merged-cell variants
* where `CellSelection.isRowSelection()` / `isColSelection()` return `false`.
*/
export var isFullRowOrColumnSelected = function isFullRowOrColumnSelected(selection) {
if (!(selection instanceof CellSelection)) {
return false;
}
return selection.isRowSelection() || selection.isColSelection() || isRowSelectionWithMergedFirstColumn(selection) || isColumnSelectionWithMergedFirstRow(selection);
};