Refactor and improve grid keyboard handling

This commit is contained in:
Andrew Kingston 2024-06-23 14:47:29 +01:00
parent 4ec6a22e42
commit eabb6c94d0
No known key found for this signature in database
1 changed files with 52 additions and 56 deletions

View File

@ -46,56 +46,56 @@
} }
} }
// Sugar for preventing default
const handle = async fn => {
e.preventDefault()
await fn()
}
// Handle certain key presses regardless of selection state // Handle certain key presses regardless of selection state
if (e.metaKey || e.ctrlKey) { if (e.metaKey || e.ctrlKey) {
switch (e.key) { switch (e.key) {
case "c": case "c":
e.preventDefault() return handle(() => dispatch("copy"))
dispatch("copy")
return
case "v": case "v":
e.preventDefault() return handle(() => dispatch("paste"))
dispatch("paste")
return
case "Enter": case "Enter":
e.preventDefault() return handle(() => {
if ($config.canAddRows) { if ($config.canAddRows) {
dispatch("add-row-inline") dispatch("add-row-inline")
} }
return })
} }
} }
// Handle certain key presses if we have cells selected // Handle certain key presses if we have cells selected
if ($selectedCellCount) { if ($selectedCellCount) {
e.preventDefault()
switch (e.key) { switch (e.key) {
case "ArrowLeft": case "ArrowLeft":
changeFocusedColumn(-1, e.shiftKey) return handle(() => changeFocusedColumn(-1, e.shiftKey))
break
case "ArrowRight": case "ArrowRight":
changeFocusedColumn(1, e.shiftKey) return handle(() => changeFocusedColumn(1, e.shiftKey))
break
case "ArrowUp": case "ArrowUp":
changeFocusedRow(-1, e.shiftKey) return handle(() => changeFocusedRow(-1, e.shiftKey))
break
case "ArrowDown": case "ArrowDown":
changeFocusedRow(1, e.shiftKey) return handle(() => changeFocusedRow(1, e.shiftKey))
break case "Escape":
return handle(selectedCells.actions.clear)
} }
return
} }
// If nothing selected avoid processing further key presses // Handle certain key presses only if no cell focused
if (!$focusedCellId) { if (!$focusedCellId) {
if (e.key === "Tab" || e.key?.startsWith("Arrow")) { if (e.key === "Tab" || e.key?.startsWith("Arrow")) {
e.preventDefault() handle(focusFirstCell)
focusFirstCell()
} else if (e.key === "Delete" || e.key === "Backspace") { } else if (e.key === "Delete" || e.key === "Backspace") {
handle(() => {
if (Object.keys($selectedRows).length && $config.canDeleteRows) { if (Object.keys($selectedRows).length && $config.canDeleteRows) {
dispatch("request-bulk-delete") dispatch("request-bulk-delete")
} }
})
} }
// Avoid processing anything else
return return
} }
@ -105,18 +105,19 @@
// By setting a tiny timeout here we can ensure that other listeners // By setting a tiny timeout here we can ensure that other listeners
// which depend on being able to read cell state on an escape keypress // which depend on being able to read cell state on an escape keypress
// get a chance to observe the true state before we blur // get a chance to observe the true state before we blur
return handle(() => {
if (api?.isActive()) { if (api?.isActive()) {
setTimeout(api?.blur, 10) setTimeout(api?.blur, 10)
} else { } else {
$focusedCellId = null $focusedCellId = null
} }
menu.actions.close() menu.actions.close()
return })
} else if (e.key === "Tab") { } else if (e.key === "Tab") {
e.preventDefault() return handle(() => {
api?.blur?.() api?.blur?.()
changeFocusedColumn(1) changeFocusedColumn(1)
return })
} }
// Pass the key event to the selected cell and let it decide whether to // Pass the key event to the selected cell and let it decide whether to
@ -127,7 +128,6 @@
return return
} }
} }
e.preventDefault()
// Handle the key ourselves // Handle the key ourselves
if (e.metaKey || e.ctrlKey) { if (e.metaKey || e.ctrlKey) {
@ -135,30 +135,26 @@
} else { } else {
switch (e.key) { switch (e.key) {
case "ArrowLeft": case "ArrowLeft":
changeFocusedColumn(-1, e.shiftKey) return handle(() => changeFocusedColumn(-1, e.shiftKey))
break
case "ArrowRight": case "ArrowRight":
changeFocusedColumn(1, e.shiftKey) return handle(() => changeFocusedColumn(1, e.shiftKey))
break
case "ArrowUp": case "ArrowUp":
changeFocusedRow(-1, e.shiftKey) return handle(() => changeFocusedRow(-1, e.shiftKey))
break
case "ArrowDown": case "ArrowDown":
changeFocusedRow(1, e.shiftKey) return handle(() => changeFocusedRow(1, e.shiftKey))
break
case "Delete": case "Delete":
case "Backspace": case "Backspace":
return handle(() => {
if (Object.keys($selectedRows).length && $config.canDeleteRows) { if (Object.keys($selectedRows).length && $config.canDeleteRows) {
dispatch("request-bulk-delete") dispatch("request-bulk-delete")
} else { } else {
deleteSelectedCell() deleteSelectedCell()
} }
break })
case "Enter": case "Enter":
focusCell() return handle(focusCell)
break
default: default:
startEnteringValue(e.key, e.which) return handle(() => startEnteringValue(e.key, e.which))
} }
} }
} }