Browse Source

feat(nc-gui): add scrolling logic when new options are added

pull/3563/head
Wing-Kam Wong 2 years ago
parent
commit
27649e904b
  1. 30
      packages/nc-gui/components/smartsheet/Kanban.vue
  2. 8
      packages/nc-gui/composables/useKanbanViewData.ts

30
packages/nc-gui/components/smartsheet/Kanban.vue

@ -65,6 +65,7 @@ const {
countByStack, countByStack,
deleteStack, deleteStack,
removeRowFromUncategorizedStack, removeRowFromUncategorizedStack,
shouldScrollToRight,
} = useKanbanViewData(meta, view) } = useKanbanViewData(meta, view)
const { isUIAllowed } = useUIPermission() const { isUIAllowed } = useUIPermission()
@ -72,13 +73,21 @@ const { isUIAllowed } = useUIPermission()
const { appInfo } = $(useGlobal()) const { appInfo } = $(useGlobal())
provide(IsFormInj, ref(false)) provide(IsFormInj, ref(false))
provide(IsGalleryInj, ref(false)) provide(IsGalleryInj, ref(false))
provide(IsGridInj, ref(false)) provide(IsGridInj, ref(false))
provide(IsKanbanInj, ref(true)) provide(IsKanbanInj, ref(true))
provide(ReadonlyInj, !isUIAllowed('xcDatatableEditable')) provide(ReadonlyInj, !isUIAllowed('xcDatatableEditable'))
const fields = inject(FieldsInj, ref([])) const fields = inject(FieldsInj, ref([]))
const kanbanContainerRef = ref()
let isMounted = false
const isRowEmpty = (record: any, col: any) => { const isRowEmpty = (record: any, col: any) => {
const val = record.row[col.title] const val = record.row[col.title]
if (!val) return true if (!val) return true
@ -221,12 +230,31 @@ onMounted(async () => {
await loadKanbanMeta() await loadKanbanMeta()
// load kanban data // load kanban data
await loadKanbanData() await loadKanbanData()
// update isMounted for below watcher
isMounted = true
}) })
watch(
() => shouldScrollToRight.value,
() => {
// use `isMounted` to avoid scrolling from switching from other views
// i.e. scroll only when a new option is added within kanban view
if (isMounted && shouldScrollToRight.value) {
// horizontally scroll to the end of the kanban container
kanbanContainerRef.value.scrollTo({
left: kanbanContainerRef.value.scrollWidth,
behavior: 'smooth',
})
// reset shouldScrollToRight
shouldScrollToRight.value = false
}
},
)
</script> </script>
<template> <template>
<div class="flex h-full bg-white px-2"> <div class="flex h-full bg-white px-2">
<div class="nc-kanban-container flex my-4 px-3 overflow-x-scroll overflow-y-hidden"> <div ref="kanbanContainerRef" class="nc-kanban-container flex my-4 px-3 overflow-x-scroll overflow-y-hidden">
<Draggable <Draggable
v-model="groupingFieldColOptions" v-model="groupingFieldColOptions"
class="flex gap-4" class="flex gap-4"

8
packages/nc-gui/composables/useKanbanViewData.ts

@ -66,6 +66,8 @@ export function useKanbanViewData(
// stack meta in object format // stack meta in object format
const stackMetaObj = useState<Record<string, GroupingFieldColOptionsType[]>>('KanbanStackMetaObj', () => ({})) const stackMetaObj = useState<Record<string, GroupingFieldColOptionsType[]>>('KanbanStackMetaObj', () => ({}))
const shouldScrollToRight = ref(false)
const formatData = (list: Record<string, any>[]) => const formatData = (list: Record<string, any>[]) =>
list.map((row) => ({ list.map((row) => ({
row: { ...row }, row: { ...row },
@ -138,6 +140,7 @@ export function useKanbanViewData(
if (stackMetaObj.value && grp_column_id && stackMetaObj.value[grp_column_id]) { if (stackMetaObj.value && grp_column_id && stackMetaObj.value[grp_column_id]) {
// keep the existing order (index of the array) but update the values done outside kanban // keep the existing order (index of the array) but update the values done outside kanban
let isChanged = false let isChanged = false
let hasNewOptionsAdded = false
for (const option of (groupingFieldColumn.value.colOptions as SelectOptionsType)?.options ?? []) { for (const option of (groupingFieldColumn.value.colOptions as SelectOptionsType)?.options ?? []) {
const idx = stackMetaObj.value[grp_column_id].findIndex((ele) => ele.id === option.id) const idx = stackMetaObj.value[grp_column_id].findIndex((ele) => ele.id === option.id)
if (idx !== -1) { if (idx !== -1) {
@ -167,6 +170,7 @@ export function useKanbanViewData(
formattedData.value[option.title!] = [] formattedData.value[option.title!] = []
countByStack.value[option.title!] = 0 countByStack.value[option.title!] = 0
isChanged = true isChanged = true
hasNewOptionsAdded = true
} }
} }
@ -198,6 +202,9 @@ export function useKanbanViewData(
if (isChanged) { if (isChanged) {
await updateKanbanStackMeta() await updateKanbanStackMeta()
if (hasNewOptionsAdded) {
shouldScrollToRight.value = true
}
} }
} else { } else {
// build stack meta // build stack meta
@ -420,5 +427,6 @@ export function useKanbanViewData(
deleteStack, deleteStack,
updateKanbanStackMeta, updateKanbanStackMeta,
removeRowFromUncategorizedStack, removeRowFromUncategorizedStack,
shouldScrollToRight,
} }
} }

Loading…
Cancel
Save