Browse Source

refactor(gui): trigger scroll to cell only on key navigation

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4033/head
Pranav C 2 years ago
parent
commit
d11fa385c5
  1. 39
      packages/nc-gui/components/smartsheet/Grid.vue
  2. 23
      packages/nc-gui/composables/useMultiSelect/index.ts

39
packages/nc-gui/components/smartsheet/Grid.vue

@ -107,6 +107,19 @@ const { selectCell, selectBlock, selectedRange, clearRangeRows, startSelectRange
isPkAvail, isPkAvail,
clearCell, clearCell,
makeEditable, makeEditable,
() => {
if (selected.row !== null && selected.col !== null) {
// get active cell
const td = tbodyEl.value?.querySelectorAll('tr')[selected.row]?.querySelectorAll('td')[selected.col + 1]
if (!td) return
// scroll into the active cell
td.scrollIntoView({
behavior: 'smooth',
block: 'end',
inline: 'end',
})
}
},
) )
onMounted(loadGridViewColumns) onMounted(loadGridViewColumns)
@ -353,19 +366,19 @@ watch(
const tbodyEl = ref<HTMLElement>() const tbodyEl = ref<HTMLElement>()
watch([() => selected.row, () => selected.col], ([row, col]) => { // watch([() => selected.row, () => selected.col], ([row, col]) => {
if (row !== null && col !== null) { // if (row !== null && col !== null) {
// get active cell // // get active cell
const td = tbodyEl.value?.querySelectorAll('tr')[row]?.querySelectorAll('td')[col + 1] // const td = tbodyEl.value?.querySelectorAll('tr')[row]?.querySelectorAll('td')[col + 1]
if (!td) return // if (!td) return
// scroll into the active cell // // scroll into the active cell
td.scrollIntoView({ // td.scrollIntoView({
behavior: 'smooth', // behavior: 'smooth',
block: 'end', // block: 'end',
inline: 'end', // inline: 'end',
}) // })
} // }
}) // })
</script> </script>
<template> <template>

23
packages/nc-gui/composables/useMultiSelect/index.ts

@ -17,6 +17,7 @@ export function useMultiSelect(
isPkAvail: MaybeRef<boolean>, isPkAvail: MaybeRef<boolean>,
clearCell: Function, clearCell: Function,
makeEditable: Function, makeEditable: Function,
scrollToActiveCell?: () => void,
) { ) {
const { t } = useI18n() const { t } = useI18n()
@ -39,6 +40,7 @@ export function useMultiSelect(
clearRangeRows() clearRangeRows()
selected.row = row selected.row = row
selected.col = col selected.col = col
scrollToActiveCell?.()
} }
function selectBlock(row: number, col: number) { function selectBlock(row: number, col: number) {
@ -159,6 +161,7 @@ export function useMultiSelect(
selected.col = 0 selected.col = 0
} }
} }
scrollToActiveCell?.()
break break
/** on enter key press make cell editable */ /** on enter key press make cell editable */
case 'Enter': case 'Enter':
@ -178,25 +181,37 @@ export function useMultiSelect(
case 'ArrowRight': case 'ArrowRight':
e.preventDefault() e.preventDefault()
clearRangeRows() clearRangeRows()
if (selected.col < unref(columnLength) - 1) selected.col++ if (selected.col < unref(columnLength) - 1) {
selected.col++
scrollToActiveCell?.()
}
break break
case 'ArrowLeft': case 'ArrowLeft':
clearRangeRows() clearRangeRows()
e.preventDefault() e.preventDefault()
clearRangeRows() clearRangeRows()
if (selected.col > 0) selected.col-- if (selected.col > 0) {
selected.col--
scrollToActiveCell?.()
}
break break
case 'ArrowUp': case 'ArrowUp':
clearRangeRows() clearRangeRows()
e.preventDefault() e.preventDefault()
clearRangeRows() clearRangeRows()
if (selected.row > 0) selected.row-- if (selected.row > 0) {
selected.row--
scrollToActiveCell?.()
}
break break
case 'ArrowDown': case 'ArrowDown':
clearRangeRows() clearRangeRows()
e.preventDefault() e.preventDefault()
clearRangeRows() clearRangeRows()
if (selected.row < unref(data).length - 1) selected.row++ if (selected.row < unref(data).length - 1) {
selected.row++
scrollToActiveCell?.()
}
break break
default: default:
{ {

Loading…
Cancel
Save