UNPKG

@1771technologies/lytenyte-pro

Version:

Blazingly fast headless React data grid with 100s of features.

75 lines (74 loc) 3.06 kB
import { dataRectFromCellPosition } from "./data-rect-from-cell-position.js"; import { isOverlappingRect } from "./is-overlapping-rect.js"; import { isFullyWithinRect } from "./is-fully-within-rect.js"; export function expandSelectionUp(api, selections, setSelections, meta, position, rowCount) { const pos = dataRectFromCellPosition(position); const rect = selections.at(-1); if (!rect || !isOverlappingRect(rect, pos) || isFullyWithinRect(pos, rect)) return; if (meta) { const next = { ...rect, rowStart: 0, rowEnd: pos.rowStart + 1 }; const nextSelections = [...selections]; nextSelections[nextSelections.length - 1] = next; setSelections(nextSelections); if (pos.rowStart !== rowCount - 1) api.scrollIntoView({ row: 0 }); return; } const isAtEdge = pos.rowStart == rect.rowStart || pos.rowEnd === rect.rowEnd; let pivotStart = pos.rowStart; let pivotEnd = pos.rowEnd; // Our cell some how is spanned over. so for the current rowIndex, find the maximum span along the columns if (!isAtEdge) { for (let i = rect.columnStart; i < rect.columnEnd; i++) { const cell = dataRectFromCellPosition(api.cellRoot(pos.rowStart, i)); pivotStart = Math.min(pivotStart, cell.rowStart); pivotEnd = Math.max(pivotEnd, cell.rowEnd); } } let next; if (rect.rowEnd > pivotEnd) { let lowestRowStart = Infinity; let c = rect; for (let i = rect.columnStart; i < rect.columnEnd; i++) { const cell = dataRectFromCellPosition(api.cellRoot(rect.rowEnd - 1, i)); lowestRowStart = Math.min(cell.rowStart, lowestRowStart); if (cell.rowStart < lowestRowStart) { lowestRowStart = cell.rowStart; c = cell; } } api.scrollIntoView({ row: lowestRowStart - 1 }); next = { ...rect, rowEnd: lowestRowStart, columnStart: Math.min(c.columnStart, rect.columnStart), columnEnd: Math.max(c.columnEnd, rect.columnEnd), }; } else { if (rect.rowStart === 0) return; let lowestRowStart = Infinity; let c = rect; for (let i = rect.columnStart; i < rect.columnEnd; i++) { const cell = dataRectFromCellPosition(api.cellRoot(rect.rowStart - 1, i)); lowestRowStart = Math.min(cell.rowStart, lowestRowStart); if (cell.rowStart < lowestRowStart) { lowestRowStart = cell.rowStart; c = cell; } } api.scrollIntoView({ row: lowestRowStart }); next = { ...rect, rowStart: lowestRowStart, columnStart: Math.min(c.columnStart, rect.columnStart), columnEnd: Math.max(c.columnEnd, rect.columnEnd), }; } const nextSelections = [...selections]; nextSelections[nextSelections.length - 1] = next; setSelections(nextSelections); return; }