@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
122 lines (118 loc) • 4.85 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';
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
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 column as fully selected when its last-row area is covered by a horizontal merge and
* the explicit `CellSelection` ends above that merged cell. Using that merged cell as an endpoint
* would expand the selection to the full width of the cell.
*/
export var isColumnSelectionWithMergedLastRow = function isColumnSelectionWithMergedLastRow(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.top !== 0 || rect.bottom === map.height) {
return false;
}
for (var col = rect.left; col < rect.right; col++) {
var bottomCellRect = map.findCell(map.map[(map.height - 1) * map.width + col]);
var isHorizontallyMerged = bottomCellRect.right - bottomCellRect.left > 1;
var bridgesGapBelowSelection = bottomCellRect.top <= rect.bottom;
if (!isHorizontallyMerged || !bridgesGapBelowSelection) {
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;
}
var isExistingFullRowOrColumnSelection = selection.isRowSelection() || selection.isColSelection() || isRowSelectionWithMergedFirstColumn(selection) || isColumnSelectionWithMergedFirstRow(selection);
if (isExperimentEnabled('platform_editor_table_menu_updates_patch_5')) {
return isExistingFullRowOrColumnSelection || isColumnSelectionWithMergedLastRow(selection);
} else {
return isExistingFullRowOrColumnSelection;
}
};