Prevent hovering over rows when dragging and keep scroll bar in the selected state

This commit is contained in:
Andrew Kingston 2023-06-16 10:17:10 +01:00
parent 70eda7ff81
commit ffd4148655
5 changed files with 30 additions and 10 deletions

View File

@ -12,6 +12,7 @@
config, config,
hoveredRowId, hoveredRowId,
dispatch, dispatch,
isDragging,
} = getContext("grid") } = getContext("grid")
let body let body
@ -47,8 +48,8 @@
class="blank" class="blank"
class:highlighted={$hoveredRowId === BlankRowID} class:highlighted={$hoveredRowId === BlankRowID}
style="width:{renderColumnsWidth}px" style="width:{renderColumnsWidth}px"
on:mouseenter={() => ($hoveredRowId = BlankRowID)} on:mouseenter={$isDragging ? null : () => ($hoveredRowId = BlankRowID)}
on:mouseleave={() => ($hoveredRowId = null)} on:mouseleave={$isDragging ? null : () => ($hoveredRowId = null)}
on:click={() => dispatch("add-row-inline")} on:click={() => dispatch("add-row-inline")}
/> />
{/if} {/if}

View File

@ -16,6 +16,7 @@
focusedRow, focusedRow,
columnHorizontalInversionIndex, columnHorizontalInversionIndex,
contentLines, contentLines,
isDragging,
} = getContext("grid") } = getContext("grid")
$: rowSelected = !!$selectedRows[row._id] $: rowSelected = !!$selectedRows[row._id]
@ -27,8 +28,8 @@
<div <div
class="row" class="row"
on:focus on:focus
on:mouseenter={() => ($hoveredRowId = row._id)} on:mouseenter={$isDragging ? null : () => ($hoveredRowId = row._id)}
on:mouseleave={() => ($hoveredRowId = null)} on:mouseleave={$isDragging ? null : () => ($hoveredRowId = null)}
> >
{#each $renderedColumns as column, columnIdx (column.name)} {#each $renderedColumns as column, columnIdx (column.name)}
{@const cellId = `${row._id}-${column.name}`} {@const cellId = `${row._id}-${column.name}`}

View File

@ -23,6 +23,7 @@
scrollLeft, scrollLeft,
dispatch, dispatch,
contentLines, contentLines,
isDragging,
} = getContext("grid") } = getContext("grid")
$: rowCount = $rows.length $: rowCount = $rows.length
@ -71,8 +72,8 @@
{@const cellId = `${row._id}-${$stickyColumn?.name}`} {@const cellId = `${row._id}-${$stickyColumn?.name}`}
<div <div
class="row" class="row"
on:mouseenter={() => ($hoveredRowId = row._id)} on:mouseenter={$isDragging ? null : () => ($hoveredRowId = row._id)}
on:mouseleave={() => ($hoveredRowId = null)} on:mouseleave={$isDragging ? null : () => ($hoveredRowId = null)}
> >
<GutterCell {row} {rowFocused} {rowHovered} {rowSelected} /> <GutterCell {row} {rowFocused} {rowHovered} {rowSelected} />
{#if $stickyColumn} {#if $stickyColumn}
@ -96,8 +97,10 @@
{#if $config.allowAddRows && ($renderedColumns.length || $stickyColumn)} {#if $config.allowAddRows && ($renderedColumns.length || $stickyColumn)}
<div <div
class="row new" class="row new"
on:mouseenter={() => ($hoveredRowId = BlankRowID)} on:mouseenter={$isDragging
on:mouseleave={() => ($hoveredRowId = null)} ? null
: () => ($hoveredRowId = BlankRowID)}
on:mouseleave={$isDragging ? null : () => ($hoveredRowId = null)}
on:click={() => dispatch("add-row-inline")} on:click={() => dispatch("add-row-inline")}
> >
<GutterCell disableExpand rowHovered={$hoveredRowId === BlankRowID}> <GutterCell disableExpand rowHovered={$hoveredRowId === BlankRowID}>

View File

@ -15,11 +15,17 @@
scrollLeft, scrollLeft,
scrollTop, scrollTop,
height, height,
isDragging,
} = getContext("grid") } = getContext("grid")
// State for dragging bars // State for dragging bars
let initialMouse let initialMouse
let initialScroll let initialScroll
let isDraggingV = false
let isDraggingH = false
// Update state to reflect if we are dragging
$: isDragging.set(isDraggingV || isDraggingH)
// Calculate V scrollbar size and offset // Calculate V scrollbar size and offset
// Terminology is the same for both axes: // Terminology is the same for both axes:
@ -46,6 +52,7 @@
initialScroll = $scrollTop initialScroll = $scrollTop
document.addEventListener("mousemove", moveVDragging) document.addEventListener("mousemove", moveVDragging)
document.addEventListener("mouseup", stopVDragging) document.addEventListener("mouseup", stopVDragging)
isDraggingV = true
} }
const moveVDragging = domDebounce(e => { const moveVDragging = domDebounce(e => {
const delta = e.clientY - initialMouse const delta = e.clientY - initialMouse
@ -59,6 +66,7 @@
const stopVDragging = () => { const stopVDragging = () => {
document.removeEventListener("mousemove", moveVDragging) document.removeEventListener("mousemove", moveVDragging)
document.removeEventListener("mouseup", stopVDragging) document.removeEventListener("mouseup", stopVDragging)
isDraggingV = false
} }
// H scrollbar drag handlers // H scrollbar drag handlers
@ -68,6 +76,7 @@
initialScroll = $scrollLeft initialScroll = $scrollLeft
document.addEventListener("mousemove", moveHDragging) document.addEventListener("mousemove", moveHDragging)
document.addEventListener("mouseup", stopHDragging) document.addEventListener("mouseup", stopHDragging)
isDraggingH = true
} }
const moveHDragging = domDebounce(e => { const moveHDragging = domDebounce(e => {
const delta = e.clientX - initialMouse const delta = e.clientX - initialMouse
@ -81,6 +90,7 @@
const stopHDragging = () => { const stopHDragging = () => {
document.removeEventListener("mousemove", moveHDragging) document.removeEventListener("mousemove", moveHDragging)
document.removeEventListener("mouseup", stopHDragging) document.removeEventListener("mouseup", stopHDragging)
isDraggingH = false
} }
</script> </script>
@ -89,6 +99,7 @@
class="v-scrollbar" class="v-scrollbar"
style="--size:{ScrollBarSize}px; top:{barTop}px; height:{barHeight}px;" style="--size:{ScrollBarSize}px; top:{barTop}px; height:{barHeight}px;"
on:mousedown={startVDragging} on:mousedown={startVDragging}
class:dragging={isDraggingV}
/> />
{/if} {/if}
{#if $showHScrollbar} {#if $showHScrollbar}
@ -96,6 +107,7 @@
class="h-scrollbar" class="h-scrollbar"
style="--size:{ScrollBarSize}px; left:{barLeft}px; width:{barWidth}px;" style="--size:{ScrollBarSize}px; left:{barLeft}px; width:{barWidth}px;"
on:mousedown={startHDragging} on:mousedown={startHDragging}
class:dragging={isDraggingH}
/> />
{/if} {/if}
@ -103,11 +115,12 @@
div { div {
position: absolute; position: absolute;
background: var(--spectrum-global-color-gray-500); background: var(--spectrum-global-color-gray-500);
opacity: 0.7; opacity: 0.5;
border-radius: 4px; border-radius: 4px;
transition: opacity 130ms ease-out; transition: opacity 130ms ease-out;
} }
div:hover { div:hover,
div.dragging {
opacity: 1; opacity: 1;
} }
.v-scrollbar { .v-scrollbar {

View File

@ -16,6 +16,7 @@ export const createStores = () => {
const rowHeight = writable(DefaultRowHeight) const rowHeight = writable(DefaultRowHeight)
const previousFocusedRowId = writable(null) const previousFocusedRowId = writable(null)
const gridFocused = writable(false) const gridFocused = writable(false)
const isDragging = writable(false)
// Derive the current focused row ID // Derive the current focused row ID
const focusedRowId = derived( const focusedRowId = derived(
@ -48,6 +49,7 @@ export const createStores = () => {
hoveredRowId, hoveredRowId,
rowHeight, rowHeight,
gridFocused, gridFocused,
isDragging,
selectedRows: { selectedRows: {
...selectedRows, ...selectedRows,
actions: { actions: {