1
0
Fork 0
mirror of synced 2024-09-17 17:57:47 +12:00

Fix auto scrolling of focused cell

This commit is contained in:
Andrew Kingston 2024-06-24 14:41:29 +01:00
parent 7f391e4fdf
commit dedf264185
No known key found for this signature in database
3 changed files with 22 additions and 17 deletions

View file

@ -10,7 +10,7 @@
rowHeight, rowHeight,
renderedRows, renderedRows,
scrollLeft, scrollLeft,
bodyLeft, stickyWidth,
} = getContext("grid") } = getContext("grid")
$: targetColumn = $columnLookupMap[$reorder.targetColumn] $: targetColumn = $columnLookupMap[$reorder.targetColumn]
@ -18,7 +18,7 @@
$: left = getLeft(targetColumn, insertAfter, $scrollLeft) $: left = getLeft(targetColumn, insertAfter, $scrollLeft)
$: height = $rowHeight * $renderedRows.length + DefaultRowHeight $: height = $rowHeight * $renderedRows.length + DefaultRowHeight
$: style = `left:${left}px; height:${height}px;` $: style = `left:${left}px; height:${height}px;`
$: visible = $isReordering && left >= $bodyLeft $: visible = $isReordering && left >= $stickyWidth
const getLeft = (targetColumn, insertAfter, scrollLeft) => { const getLeft = (targetColumn, insertAfter, scrollLeft) => {
if (!targetColumn) { if (!targetColumn) {

View file

@ -34,7 +34,7 @@ export const createActions = context => {
bounds, bounds,
visibleColumns, visibleColumns,
datasource, datasource,
bodyLeft, stickyWidth,
width, width,
scrollLeft, scrollLeft,
maxScrollLeft, maxScrollLeft,
@ -47,11 +47,11 @@ export const createActions = context => {
const startReordering = (column, e) => { const startReordering = (column, e) => {
const $scrollableColumns = get(scrollableColumns) const $scrollableColumns = get(scrollableColumns)
const $bounds = get(bounds) const $bounds = get(bounds)
const $bodyLeft = get(bodyLeft) const $stickyWidth = get(stickyWidth)
// Generate new breakpoints for the current columns // Generate new breakpoints for the current columns
const breakpoints = $scrollableColumns.map(col => ({ const breakpoints = $scrollableColumns.map(col => ({
x: col.__left - $bodyLeft, x: col.__left - $stickyWidth,
column: col.name, column: col.name,
insertAfter: false, insertAfter: false,
})) }))
@ -60,7 +60,7 @@ export const createActions = context => {
const lastCol = $scrollableColumns[$scrollableColumns.length - 1] const lastCol = $scrollableColumns[$scrollableColumns.length - 1]
if (lastCol) { if (lastCol) {
breakpoints.push({ breakpoints.push({
x: lastCol.__left + lastCol.width - $bodyLeft, x: lastCol.__left + lastCol.width - $stickyWidth,
column: lastCol.name, column: lastCol.name,
insertAfter: true, insertAfter: true,
}) })

View file

@ -32,7 +32,7 @@ export const deriveStores = context => {
} = context } = context
// Memoize store primitives // Memoize store primitives
const bodyLeft = derived(displayColumn, $displayColumn => { const stickyWidth = derived(displayColumn, $displayColumn => {
return ($displayColumn?.width || 0) + GutterWidth return ($displayColumn?.width || 0) + GutterWidth
}) })
@ -58,9 +58,12 @@ export const deriveStores = context => {
return width return width
} }
) )
const screenWidth = derived([width, bodyLeft], ([$width, $bodyLeft]) => { const screenWidth = derived(
return $width + $bodyLeft [width, stickyWidth],
}) ([$width, $stickyWidth]) => {
return $width + $stickyWidth
}
)
const maxScrollLeft = derived( const maxScrollLeft = derived(
[contentWidth, screenWidth], [contentWidth, screenWidth],
([$contentWidth, $screenWidth]) => { ([$contentWidth, $screenWidth]) => {
@ -83,7 +86,7 @@ export const deriveStores = context => {
) )
return { return {
bodyLeft, stickyWidth,
contentHeight, contentHeight,
contentWidth, contentWidth,
screenWidth, screenWidth,
@ -101,12 +104,13 @@ export const initialise = context => {
scroll, scroll,
bounds, bounds,
rowHeight, rowHeight,
visibleColumns, stickyWidth,
scrollTop, scrollTop,
maxScrollTop, maxScrollTop,
scrollLeft, scrollLeft,
maxScrollLeft, maxScrollLeft,
buttonColumnWidth, buttonColumnWidth,
columnLookupMap,
} = context } = context
// Ensure scroll state never goes invalid, which can happen when changing // Ensure scroll state never goes invalid, which can happen when changing
@ -174,15 +178,16 @@ export const initialise = context => {
// Ensure horizontal position is viewable // Ensure horizontal position is viewable
// Check horizontal position of columns next // Check horizontal position of columns next
const $visibleColumns = get(visibleColumns) const { field } = parseCellID($focusedCellId)
const { field: columnName } = parseCellID($focusedCellId) const column = get(columnLookupMap)[field]
const column = $visibleColumns.find(col => col.name === columnName)
if (!column) { if (!column) {
return return
} }
// Ensure column is not cutoff on left edge // Ensure column is not cutoff on left edge
let delta = $scroll.left - column.left + FocusedCellMinOffset const $stickyWidth = get(stickyWidth)
let delta =
$scroll.left - column.__left + FocusedCellMinOffset + $stickyWidth
if (delta > 0) { if (delta > 0) {
scroll.update(state => ({ scroll.update(state => ({
...state, ...state,
@ -196,7 +201,7 @@ export const initialise = context => {
const rightEdge = column.__left + column.width const rightEdge = column.__left + column.width
const rightBound = const rightBound =
$bounds.width + $scroll.left - FocusedCellMinOffset - $buttonColumnWidth $bounds.width + $scroll.left - FocusedCellMinOffset - $buttonColumnWidth
delta = rightEdge - rightBound delta = rightEdge - rightBound - $stickyWidth
if (delta > 0) { if (delta > 0) {
scroll.update(state => ({ scroll.update(state => ({
...state, ...state,