@integec/grid-tools
Version:
Integ Grid Tools
348 lines (269 loc) • 11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSelectedData = exports.getGlobalColumnRange = exports.selector = exports.selectRange = exports.selectAll = exports.down = exports.up = exports.right = exports.left = exports.toggleSelection = exports.selectRectangle = exports.unselectRectangle = exports.isRowSelected = exports.isCellSelected = void 0;
var _range2 = _interopRequireDefault(require("ramda/src/range"));
var _utils = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { if (i % 2) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } else { Object.defineProperties(target, Object.getOwnPropertyDescriptors(arguments[i])); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var isCellSelected = function isCellSelected(rowIndex, columnIndex, selection) {
var bitMaskMap = selection.bitMaskMap;
var rowBitMask = bitMaskMap[rowIndex];
if (!rowBitMask) return false;
return (rowBitMask & BigInt(1) << BigInt(columnIndex)) !== BigInt(0);
};
exports.isCellSelected = isCellSelected;
var isRowSelected = function isRowSelected(rowIndex, selection) {
var bitMaskMap = selection.bitMaskMap;
var rowBitMask = bitMaskMap[rowIndex];
return !!rowBitMask;
};
exports.isRowSelected = isRowSelected;
var unselectRectangle = function unselectRectangle(bitMaskMap, x1, y1, x2, y2) {
var newMap = _objectSpread({}, bitMaskMap || {});
var xMin = Math.min(x1, x2);
var xMax = Math.max(x1, x2);
var yMin = Math.min(y1, y2);
var yMax = Math.max(y1, y2); // mask of consecutive bits for the range xMin..xMax
var width = xMax - xMin + 1;
var colMask = (BigInt(1) << BigInt(width)) - BigInt(1) << BigInt(xMin);
for (var r = yMin; r <= yMax; r++) {
if (newMap[r] !== undefined) {
var updated = newMap[r] & ~colMask; // clear the bits
if (updated === BigInt(0)) {
delete newMap[r]; // remove row entry if empty
} else {
newMap[r] = updated;
}
}
}
return newMap;
};
exports.unselectRectangle = unselectRectangle;
var selectRectangle = function selectRectangle(bitMaskMap, x1, y1, x2, y2) {
var newMap = _objectSpread({}, bitMaskMap || {});
var xMin = Math.min(x1, x2);
var xMax = Math.max(x1, x2);
var yMin = Math.min(y1, y2);
var yMax = Math.max(y1, y2); // mask of consecutive bits for the range xMin..xMax
var width = xMax - xMin + 1;
var colMask = (BigInt(1) << BigInt(width)) - BigInt(1) << BigInt(xMin);
for (var r = yMin; r <= yMax; r++) {
newMap[r] = (newMap[r] || BigInt(0)) | colMask; // OR bits to select
}
return newMap;
};
exports.selectRectangle = selectRectangle;
var toggleSelection = function toggleSelection(rowIndex, columnIndex, isShift, isCtrl, selection, isRowSelection) {
var bitMaskMap = selection.bitMaskMap,
previousRectangleSelection = selection.previousRectangleSelection;
var newBitMaskMap = _objectSpread({}, bitMaskMap || {});
var _ref = previousRectangleSelection || {},
x1 = _ref.x1,
y1 = _ref.y1,
x2 = _ref.x2,
y2 = _ref.y2;
if (isShift && x1 !== undefined && y1 !== undefined) {
newBitMaskMap = _objectSpread({}, unselectRectangle(newBitMaskMap, x1, y1, x2, y2) || {});
newBitMaskMap = _objectSpread({}, selectRectangle(newBitMaskMap, x1, y1, columnIndex, rowIndex) || {});
return _objectSpread({}, selection, {
bitMaskMap: newBitMaskMap,
previousRectangleSelection: {
x1: x1,
y1: y1,
x2: columnIndex,
y2: rowIndex
}
});
} else if (isCtrl) {
var rowMask = newBitMaskMap[rowIndex] || BigInt(0);
var updated = rowMask ^ BigInt(1) << BigInt(columnIndex); // toggle
if (isRowSelection) {
if (rowMask === BigInt(0)) {
newBitMaskMap[rowIndex] = updated;
} else {
delete newBitMaskMap[rowIndex];
}
} else {
if (updated === BigInt(0)) {
delete newBitMaskMap[rowIndex];
} else {
newBitMaskMap[rowIndex] = updated;
}
}
return _objectSpread({}, selection, {
bitMaskMap: newBitMaskMap,
previousRectangleSelection: {
x1: columnIndex,
y1: rowIndex,
x2: columnIndex,
y2: rowIndex
}
});
} else {
newBitMaskMap = {};
newBitMaskMap[rowIndex] = BigInt(1) << BigInt(columnIndex);
return _objectSpread({}, selection, {
bitMaskMap: newBitMaskMap,
previousRectangleSelection: {
x1: columnIndex,
y1: rowIndex,
x2: columnIndex,
y2: rowIndex
}
});
}
};
exports.toggleSelection = toggleSelection;
var left = function left(selection, isShift, isCtrl) {
var previousRectangleSelection = selection.previousRectangleSelection;
var _ref2 = previousRectangleSelection || {},
x2 = _ref2.x2,
y2 = _ref2.y2;
if (x2 === undefined || y2 === undefined) {
return selection; // no previous selection to base on
}
return toggleSelection(y2, Math.max(x2 - 1, 0), isShift, isCtrl, selection);
};
exports.left = left;
var right = function right(selection, isShift, isCtrl, colCount) {
var previousRectangleSelection = selection.previousRectangleSelection;
var _ref3 = previousRectangleSelection || {},
x2 = _ref3.x2,
y2 = _ref3.y2;
if (x2 === undefined || y2 === undefined) {
return selection; // no previous selection to base on
}
return toggleSelection(y2, Math.min(x2 + 1, colCount - 1), isShift, isCtrl, selection);
};
exports.right = right;
var up = function up(selection, isShift, isCtrl) {
var previousRectangleSelection = selection.previousRectangleSelection;
var _ref4 = previousRectangleSelection || {},
x2 = _ref4.x2,
y2 = _ref4.y2;
if (x2 === undefined || y2 === undefined) {
return selection; // no previous selection to base on
}
return toggleSelection(Math.max(y2 - 1, 0), x2, isShift, isCtrl, selection);
};
exports.up = up;
var down = function down(selection, isShift, isCtrl, rowCount) {
var previousRectangleSelection = selection.previousRectangleSelection;
var _ref5 = previousRectangleSelection || {},
x2 = _ref5.x2,
y2 = _ref5.y2;
if (x2 === undefined || y2 === undefined) {
return selection; // no previous selection to base on
}
return toggleSelection(Math.min(y2 + 1, rowCount - 1), x2, isShift, isCtrl, selection);
};
exports.down = down;
var selectAll = function selectAll(colCount, rowCount) {
var fullMask = (BigInt(1) << BigInt(colCount)) - BigInt(1);
var result = {};
for (var r = 0; r < rowCount; r++) {
result[r] = fullMask;
}
return {
bitMaskMap: result,
previousRectangleSelection: {
x1: 0,
y1: 0,
x2: colCount - 1,
y2: rowCount - 1
}
};
};
exports.selectAll = selectAll;
var selectRange = function selectRange(bitMaskMap, x1, y1, x2, y2) {
var result = _objectSpread({}, bitMaskMap);
var minX = Math.min(x1, x2);
var maxX = Math.max(x1, x2);
var minY = Math.min(y1, y2);
var maxY = Math.max(y1, y2); // Precompute the mask for selected columns in this range
var colMask = BigInt(0);
for (var col = minX; col <= maxX; col++) {
colMask |= BigInt(1) << BigInt(col);
} // Apply to all rows in range
for (var row = minY; row <= maxY; row++) {
var existing = result[row] || BigInt(0);
result[row] = existing | colMask;
}
return result;
};
exports.selectRange = selectRange;
var selector = {
left: left,
right: right,
up: up,
down: down,
selectAll: selectAll
};
exports.selector = selector;
var getGlobalColumnRange = function getGlobalColumnRange(bitMaskMap) {
var globalMin = null;
var globalMax = null;
for (var _i = 0, _Object$values = Object.values(bitMaskMap); _i < _Object$values.length; _i++) {
var mask = _Object$values[_i];
if (mask === BigInt(0)) continue;
var binary = mask.toString(2).split('').reverse().join('');
var rowMin = binary.indexOf('1');
var rowMax = binary.lastIndexOf('1');
if (rowMin !== -1) {
var minCol = rowMin;
var maxCol = rowMax;
if (globalMin === null || minCol < globalMin) {
globalMin = minCol;
}
if (globalMax === null || maxCol > globalMax) {
globalMax = maxCol;
}
}
}
if (globalMin === null || globalMax === null) return {
minCol: undefined,
maxCol: undefined
};
return {
minCol: globalMin,
maxCol: globalMax
};
};
exports.getGlobalColumnRange = getGlobalColumnRange;
var getSelectedData = function getSelectedData(_ref6, selection, selectionType) {
var data = _ref6.data,
headers = _ref6.headers;
var bitMaskMap = selection.bitMaskMap;
if (!bitMaskMap || Object.keys(bitMaskMap).length === 0) return [];
var _getGlobalColumnRange = getGlobalColumnRange(bitMaskMap),
minCol = _getGlobalColumnRange.minCol,
maxCol = _getGlobalColumnRange.maxCol;
if (minCol === undefined || maxCol === undefined) return [];
var rowKeys = Object.keys(bitMaskMap).map(function (r) {
return Number(r);
});
var rows = (0, _range2["default"])(Math.min.apply(Math, _toConsumableArray(rowKeys)), Math.max.apply(Math, _toConsumableArray(rowKeys)) + 1);
var cols = selectionType === 'cell' ? (0, _range2["default"])(minCol, maxCol + 1) : (0, _range2["default"])(0, headers.length);
var getData = function getData(rowIdx) {
return function (colIndex) {
if (selectionType === 'cell' ? !isCellSelected(rowIdx, colIndex, selection) : !isRowSelected(rowIdx, selection)) return '';
return (0, _utils.extractData)({
rowData: data[rowIdx],
header: headers[colIndex]
});
};
};
var returnData = rows.map(function (rowIdx) {
return cols.map(getData(rowIdx));
});
return returnData;
};
exports.getSelectedData = getSelectedData;
//# sourceMappingURL=new-selection-utils.js.map