From 7e5a52d0ff494da729b55c3c35973a1ecc2577a2 Mon Sep 17 00:00:00 2001 From: mertmit Date: Sat, 15 Oct 2022 11:50:25 +0300 Subject: [PATCH] feat: better scroll for borders and sticky header Signed-off-by: mertmit --- .../nc-gui/components/smartsheet/Grid.vue | 61 ++++++++++++++++--- .../composables/useMultiSelect/index.ts | 2 +- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/packages/nc-gui/components/smartsheet/Grid.vue b/packages/nc-gui/components/smartsheet/Grid.vue index 862a916c75..2268927167 100644 --- a/packages/nc-gui/components/smartsheet/Grid.vue +++ b/packages/nc-gui/components/smartsheet/Grid.vue @@ -88,6 +88,8 @@ const expandedFormDlg = ref(false) const expandedFormRow = ref() const expandedFormRowState = ref>() const tbodyEl = ref() +const gridWrapper = ref() +const tableHead = ref() const { isLoading, @@ -114,16 +116,57 @@ const { selectCell, selectBlock, selectedRange, clearRangeRows, startSelectRange isPkAvail, clearCell, makeEditable, - () => { - if (selected.row !== null && selected.col !== null) { + (row?: number | null, col?: number | null) => { + row = row ?? selected.row + col = col ?? selected.col + if (row !== undefined && col !== undefined && row !== null && col !== null) { // get active cell - const td = tbodyEl.value?.querySelectorAll('tr')[selected.row]?.querySelectorAll('td')[selected.col + 1] - if (!td) return + const rows = tbodyEl.value?.querySelectorAll('tr') + const td = rows?.[row].querySelectorAll('td')[col === 0 ? 0 : col + 1] + + if (!td || !gridWrapper.value) return + + const { height: headerHeight } = tableHead.value!.getBoundingClientRect() + + const childPos = td.getBoundingClientRect() + const parentPos = gridWrapper.value.getBoundingClientRect() + const relativePos = { + top: childPos.top - parentPos.top, + right: childPos.right - parentPos.right, + bottom: childPos.bottom - parentPos.bottom, + left: childPos.left - parentPos.left, + } + + if (rows && row === rows.length - 2) { + // if last row make 'Add New Row' visible + gridWrapper.value.scrollTo({ + top: gridWrapper.value.scrollHeight, + left: + relativePos.right > 0 + ? gridWrapper.value.scrollLeft + relativePos.right + 9 // 9 is for border + : relativePos.left < 0 + ? gridWrapper.value.scrollLeft + relativePos.left + : gridWrapper.value.scrollLeft, + behavior: 'smooth', + }) + return + } + // scroll into the active cell - td.scrollIntoView({ + gridWrapper.value.scrollTo({ + top: + relativePos.bottom > 0 + ? gridWrapper.value.scrollTop + relativePos.bottom + 9 // 9 is for border + : relativePos.top - headerHeight < 0 + ? gridWrapper.value.scrollTop + relativePos.top - headerHeight + : gridWrapper.value.scrollTop, + left: + relativePos.right > 0 + ? gridWrapper.value.scrollLeft + relativePos.right + 9 // 9 is for border + : relativePos.left < 0 + ? gridWrapper.value.scrollLeft + relativePos.left + : gridWrapper.value.scrollLeft, behavior: 'smooth', - block: 'nearest', - inline: 'nearest', }) } }, @@ -380,7 +423,7 @@ watch( -
+
- +
diff --git a/packages/nc-gui/composables/useMultiSelect/index.ts b/packages/nc-gui/composables/useMultiSelect/index.ts index 1b99e4dda5..91652a6cd0 100644 --- a/packages/nc-gui/composables/useMultiSelect/index.ts +++ b/packages/nc-gui/composables/useMultiSelect/index.ts @@ -17,7 +17,7 @@ export function useMultiSelect( isPkAvail: MaybeRef, clearCell: Function, makeEditable: Function, - scrollToActiveCell?: () => void, + scrollToActiveCell?: (row?: number | null, col?: number | null) => void, ) { const { t } = useI18n()