Add support for bulk selecting cells via shift key

This commit is contained in:
Andrew Kingston 2024-06-23 10:34:23 +01:00
parent d4d63c6115
commit 878aa35335
No known key found for this signature in database
2 changed files with 26 additions and 2 deletions

View File

@ -12,6 +12,7 @@
config, config,
validation, validation,
selectedCells, selectedCells,
selectedCellCount,
} = getContext("grid") } = getContext("grid")
export let highlighted export let highlighted
@ -83,7 +84,7 @@
} }
const startSelection = e => { const startSelection = e => {
if (e.button !== 0) { if (e.button !== 0 || e.shiftKey) {
return return
} }
selectedCells.actions.startSelecting(cellId) selectedCells.actions.startSelecting(cellId)
@ -99,6 +100,20 @@
const stopSelection = () => { const stopSelection = () => {
selectedCells.actions.stopSelecting() selectedCells.actions.stopSelecting()
} }
const handleClick = e => {
if (e.shiftKey && $focusedCellId) {
// If we have a focused cell, select the range from that cell to here
selectedCells.actions.setRange($focusedCellId, cellId)
focusedCellId.set(null)
} else if (e.shiftKey && $selectedCellCount) {
// If we already have a selected range of cell, update it
selectedCells.actions.updateTarget(cellId)
} else {
// Otherwise just select this cell
focusedCellId.set(cellId)
}
}
</script> </script>
<GridCell <GridCell
@ -115,7 +130,7 @@
on:mousedown={startSelection} on:mousedown={startSelection}
on:mouseenter={updateSelectionCallback} on:mouseenter={updateSelectionCallback}
on:mouseup={stopSelectionCallback} on:mouseup={stopSelectionCallback}
on:click={() => focusedCellId.set(cellId)} on:click={handleClick}
width={column.width} width={column.width}
> >
<svelte:component <svelte:component

View File

@ -261,6 +261,14 @@ export const createActions = context => {
})) }))
} }
const setCellSelectionRange = (source, target) => {
cellSelection.set({
active: false,
sourceCellId: source,
targetCellId: target,
})
}
const clearCellSelection = () => { const clearCellSelection = () => {
cellSelection.set({ cellSelection.set({
active: false, active: false,
@ -288,6 +296,7 @@ export const createActions = context => {
startSelecting: startCellSelection, startSelecting: startCellSelection,
updateTarget: updateCellSelection, updateTarget: updateCellSelection,
stopSelecting: stopCellSelection, stopSelecting: stopCellSelection,
setRange: setCellSelectionRange,
clear: clearCellSelection, clear: clearCellSelection,
}, },
}, },