@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
93 lines (89 loc) • 3.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isRowSelectionWithMergedFirstColumn = exports.isFullRowOrColumnSelected = exports.isColumnSelectionWithMergedFirstRow = exports.getSelectedRowIndexes = exports.getSelectedColumnIndexes = void 0;
var _cellSelection = require("@atlaskit/editor-tables/cell-selection");
var _tableMap = require("@atlaskit/editor-tables/table-map");
var _utils = require("@atlaskit/editor-tables/utils");
var getSelectedColumnIndexes = exports.getSelectedColumnIndexes = function getSelectedColumnIndexes(selectionRect) {
var columnIndexes = [];
for (var i = selectionRect.left; i < selectionRect.right; i++) {
columnIndexes.push(i);
}
return columnIndexes;
};
var getSelectedRowIndexes = exports.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.
*/
var isColumnSelectionWithMergedFirstRow = exports.isColumnSelectionWithMergedFirstRow = function isColumnSelectionWithMergedFirstRow(selection) {
var rect = (0, _utils.getSelectionRect)(selection);
if (!rect) {
return false;
}
var table = (0, _utils.findTableClosestToPos)(selection.$anchorCell);
if (!table) {
return false;
}
var map = _tableMap.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.
*/
var isRowSelectionWithMergedFirstColumn = exports.isRowSelectionWithMergedFirstColumn = function isRowSelectionWithMergedFirstColumn(selection) {
var rect = (0, _utils.getSelectionRect)(selection);
if (!rect) {
return false;
}
var table = (0, _utils.findTableClosestToPos)(selection.$anchorCell);
if (!table) {
return false;
}
var map = _tableMap.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`.
*/
var isFullRowOrColumnSelected = exports.isFullRowOrColumnSelected = function isFullRowOrColumnSelected(selection) {
if (!(selection instanceof _cellSelection.CellSelection)) {
return false;
}
return selection.isRowSelection() || selection.isColSelection() || isRowSelectionWithMergedFirstColumn(selection) || isColumnSelectionWithMergedFirstRow(selection);
};