mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.8 KiB
85 lines
1.8 KiB
1 year ago
|
import axios from 'axios'
|
||
|
import type { PaginatedType } from 'nocodb-sdk'
|
||
|
|
||
|
const usePaginationShortcuts = ({
|
||
|
changePage,
|
||
|
paginationDataRef,
|
||
4 months ago
|
isViewDataLoading
|
||
1 year ago
|
}: {
|
||
|
changePage: (page: number) => Promise<void> | undefined
|
||
|
paginationDataRef: Ref<PaginatedType | undefined>
|
||
4 months ago
|
isViewDataLoading: Ref<boolean>
|
||
1 year ago
|
}) => {
|
||
|
const getTotalPages = () => {
|
||
|
return Math.ceil(paginationDataRef.value!.totalRows! / paginationDataRef.value!.pageSize!)
|
||
|
}
|
||
|
|
||
|
const changePageWithLoading = async (page: number) => {
|
||
|
isViewDataLoading.value = true
|
||
|
try {
|
||
|
await changePage?.(page)
|
||
|
isViewDataLoading.value = false
|
||
|
} catch (e) {
|
||
|
if (axios.isCancel(e)) return
|
||
|
|
||
|
isViewDataLoading.value = false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const onLeft = async (e: KeyboardEvent) => {
|
||
1 year ago
|
if (isExpandedCellInputExist()) return
|
||
|
|
||
1 year ago
|
if (!e.altKey) return
|
||
|
e.preventDefault()
|
||
|
|
||
|
const page = paginationDataRef.value!.page! - 1
|
||
|
if (page < 1) return
|
||
|
|
||
|
await changePageWithLoading(page)
|
||
|
}
|
||
|
|
||
|
const onRight = async (e: KeyboardEvent) => {
|
||
1 year ago
|
if (isExpandedCellInputExist()) return
|
||
|
|
||
1 year ago
|
if (!e.altKey) return
|
||
|
e.preventDefault()
|
||
|
|
||
|
const page = paginationDataRef.value!.page! + 1
|
||
|
|
||
|
if (page > getTotalPages()) return
|
||
|
|
||
|
await changePageWithLoading(page)
|
||
|
}
|
||
|
|
||
|
const onDown = async (e: KeyboardEvent) => {
|
||
1 year ago
|
if (isExpandedCellInputExist()) return
|
||
|
|
||
1 year ago
|
if (!e.altKey) return
|
||
|
e.preventDefault()
|
||
|
|
||
4 months ago
|
if (paginationDataRef.value!.page! === getTotalPages()) return
|
||
|
|
||
8 months ago
|
await changePageWithLoading(getTotalPages())
|
||
1 year ago
|
}
|
||
|
|
||
|
const onUp = async (e: KeyboardEvent) => {
|
||
1 year ago
|
if (isExpandedCellInputExist()) return
|
||
|
|
||
1 year ago
|
if (!e.altKey) return
|
||
|
e.preventDefault()
|
||
|
|
||
4 months ago
|
if (paginationDataRef.value!.page! === 1) return
|
||
|
|
||
8 months ago
|
await changePageWithLoading(1)
|
||
1 year ago
|
}
|
||
|
|
||
|
return {
|
||
|
onLeft,
|
||
|
onRight,
|
||
|
onUp,
|
||
|
onDown,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default usePaginationShortcuts
|