Improve rendering performance

This commit is contained in:
Andrew Kingston 2023-03-02 12:10:12 +00:00
parent ff0f91bca3
commit 9b5f2d92f0
5 changed files with 21 additions and 24 deletions

View File

@ -94,14 +94,14 @@
{#if showVScrollbar} {#if showVScrollbar}
<div <div
class="v-scrollbar" class="v-scrollbar"
style="--top:{barTop}px; height:{barHeight}px;" style="top:{barTop}px; height:{barHeight}px;"
on:mousedown={startVDragging} on:mousedown={startVDragging}
/> />
{/if} {/if}
{#if showHScrollbar} {#if showHScrollbar}
<div <div
class="h-scrollbar" class="h-scrollbar"
style="--left:{barLeft}px; width:{barWidth}px;" style="left:{barLeft}px; width:{barWidth}px;"
on:mousedown={startHDragging} on:mousedown={startHDragging}
/> />
{/if} {/if}
@ -119,15 +119,11 @@
opacity: 0.9; opacity: 0.9;
} }
.v-scrollbar { .v-scrollbar {
top: var(--top);
height: var(--height);
right: 4px; right: 4px;
width: 8px; width: 8px;
} }
.h-scrollbar { .h-scrollbar {
bottom: 4px; bottom: 4px;
height: 8px; height: 8px;
width: var(--width);
left: var(--left);
} }
</style> </style>

View File

@ -7,7 +7,7 @@
export let selected = false export let selected = false
export let reorderSource = false export let reorderSource = false
export let reorderTarget = false export let reorderTarget = false
export let width export let width = ""
</script> </script>
<div <div
@ -23,7 +23,7 @@
on:focus on:focus
on:click on:click
on:mousedown on:mousedown
style="--width:{width}px;" style="flex: 0 0 {width}px;"
> >
<slot /> <slot />
</div> </div>
@ -42,7 +42,6 @@
font-size: var(--cell-font-size); font-size: var(--cell-font-size);
gap: var(--cell-spacing); gap: var(--cell-spacing);
background: var(--cell-background); background: var(--cell-background);
flex: 0 0 var(--width);
position: relative; position: relative;
width: 0; width: 0;
} }

View File

@ -33,7 +33,6 @@
reorderTarget={$reorder.targetColumn === column.name} reorderTarget={$reorder.targetColumn === column.name}
on:click={() => ($selectedCellId = cellIdx)} on:click={() => ($selectedCellId = cellIdx)}
width={column.width} width={column.width}
left={column.left}
> >
<svelte:component <svelte:component
this={getCellRenderer(column)} this={getCellRenderer(column)}

View File

@ -1,5 +1,6 @@
<script> <script>
import { getContext } from "svelte" import { getContext } from "svelte"
import { domDebounce } from "../../utils/utils"
const { const {
cellHeight, cellHeight,
@ -20,8 +21,13 @@
$: hiddenWidths = calculateHiddenWidths($visibleColumns) $: hiddenWidths = calculateHiddenWidths($visibleColumns)
$: scrollLeft = $scroll.left $: scrollLeft = $scroll.left
$: scrollTop = $scroll.top $: scrollTop = $scroll.top
$: offsetX = scrollHorizontally ? -1 * scrollLeft + hiddenWidths : 0 $: style = generateStyle($scroll, hiddenWidths)
$: offsetY = scrollVertically ? -1 * (scrollTop % cellHeight) : 0
const generateStyle = (scroll, hiddenWidths) => {
const offsetX = scrollHorizontally ? -1 * scroll.left + hiddenWidths : 0
const offsetY = scrollVertically ? -1 * (scroll.top % cellHeight) : 0
return `transform: translate3d(${offsetX}px, ${offsetY}px, 0);`
}
// Calculates with total width of all columns currently not rendered // Calculates with total width of all columns currently not rendered
const calculateHiddenWidths = visibleColumns => { const calculateHiddenWidths = visibleColumns => {
@ -38,13 +44,15 @@
// Handles a wheel even and updates the scroll offsets // Handles a wheel even and updates the scroll offsets
const handleWheel = e => { const handleWheel = e => {
e.preventDefault() e.preventDefault()
debouncedHandleWheel(e.deltaX, e.deltaY, e.clientY)
}
const debouncedHandleWheel = domDebounce((deltaX, deltaY, clientY) => {
// Calculate new scroll top // Calculate new scroll top
let newScrollTop = scrollTop + e.deltaY let newScrollTop = scrollTop + deltaY
newScrollTop = Math.max(0, Math.min(newScrollTop, $maxScrollTop)) newScrollTop = Math.max(0, Math.min(newScrollTop, $maxScrollTop))
// Calculate new scroll left // Calculate new scroll left
let newScrollLeft = scrollLeft + e.deltaX let newScrollLeft = scrollLeft + deltaX
newScrollLeft = Math.max(0, Math.min(newScrollLeft, $maxScrollLeft)) newScrollLeft = Math.max(0, Math.min(newScrollLeft, $maxScrollLeft))
// Update state // Update state
@ -54,14 +62,14 @@
}) })
// Hover row under cursor // Hover row under cursor
const y = e.clientY - $bounds.top + (newScrollTop % cellHeight) const y = clientY - $bounds.top + (newScrollTop % cellHeight)
const hoveredRow = $visibleRows[Math.floor(y / cellHeight)] const hoveredRow = $visibleRows[Math.floor(y / cellHeight)]
$hoveredRowId = hoveredRow?._id $hoveredRowId = hoveredRow?._id
} })
</script> </script>
<div class="outer" on:wheel={wheelInteractive ? handleWheel : null}> <div class="outer" on:wheel={wheelInteractive ? handleWheel : null}>
<div class="inner" style="--offset-x:{offsetX}px;--offset-y:{offsetY}px;"> <div {style}>
<slot /> <slot />
</div> </div>
</div> </div>
@ -71,7 +79,4 @@
min-width: 100%; min-width: 100%;
min-height: 100%; min-height: 100%;
} }
.inner {
transform: translate3d(var(--offset-x), var(--offset-y), 0);
}
</style> </style>

View File

@ -57,7 +57,7 @@
> >
<div class="header row"> <div class="header row">
<!-- Field headers --> <!-- Field headers -->
<SheetCell header label on:click={selectAll} width="40" left="0"> <SheetCell header label on:click={selectAll} width="40">
<Checkbox value={rowCount && selectedRowCount === rowCount} /> <Checkbox value={rowCount && selectedRowCount === rowCount} />
</SheetCell> </SheetCell>
@ -66,7 +66,6 @@
header header
sticky sticky
width={$stickyColumn.width} width={$stickyColumn.width}
left={$stickyColumn.left}
reorderTarget={$reorder.targetColumn === $stickyColumn.name} reorderTarget={$reorder.targetColumn === $stickyColumn.name}
> >
<Icon <Icon
@ -111,7 +110,6 @@
selected={$selectedCellId === cellIdx} selected={$selectedCellId === cellIdx}
on:click={() => ($selectedCellId = cellIdx)} on:click={() => ($selectedCellId = cellIdx)}
width={$stickyColumn.width} width={$stickyColumn.width}
left="40"
reorderTarget={$reorder.targetColumn === $stickyColumn.name} reorderTarget={$reorder.targetColumn === $stickyColumn.name}
> >
<svelte:component <svelte:component