에디터 미디어 페이지 계산 로직 분리

This commit is contained in:
2026-07-24 18:07:22 +09:00
parent e3f98cb820
commit 1ec71cc985
7 changed files with 147 additions and 47 deletions

View File

@@ -301,3 +301,77 @@ export const findUploadSizeLimitError = ({
return null
}
/**
* 미디어 선택 모달의 페이지 범위 표시 문구를 만든다.
* @param {Object} options - 범위 표시 옵션
* @param {Object} options.pagination - 서버 페이지네이션 응답
* @param {number} options.currentPage - 현재 페이지 fallback
* @param {number} options.pageCount - 전체 페이지 수
* @param {number} options.defaultPageSize - 기본 페이지 크기
* @returns {string} 범위 표시 문구
*/
export const buildMediaPickerRangeLabel = ({
pagination,
currentPage,
pageCount,
defaultPageSize
}) => {
const total = Number(pagination?.total) || 0
if (total <= 0) {
return '0개'
}
const normalizedPageCount = Math.max(1, Number(pageCount) || 1)
const page = Math.min(Math.max(Number(pagination?.page) || currentPage, 1), normalizedPageCount)
const pageSize = Number(pagination?.pageSize) || defaultPageSize
const start = ((page - 1) * pageSize) + 1
const end = Math.min(start + pageSize - 1, total)
return `${total}개 중 ${start}-${end}개`
}
/**
* 미디어 선택 모달 페이지네이션 항목을 만든다.
* @param {Object} options - 페이지 항목 옵션
* @param {number} options.currentPage - 현재 페이지
* @param {number} options.pageCount - 전체 페이지 수
* @returns {Array<number|string>} 페이지 번호와 ellipsis 키 목록
*/
export const buildMediaPickerPageItems = ({
currentPage,
pageCount
}) => {
const total = Math.max(1, Number(pageCount) || 1)
const current = Math.min(Math.max(Number(currentPage) || 1, 1), total)
const pages = new Set([1, total, current])
for (let page = current - 2; page <= current + 2; page += 1) {
pages.add(page)
}
if (current <= 3) {
pages.add(2)
pages.add(3)
pages.add(4)
}
if (current >= total - 2) {
pages.add(total - 3)
pages.add(total - 2)
pages.add(total - 1)
}
const orderedPages = [...pages].filter((page) => page >= 1 && page <= total).sort((left, right) => left - right)
return orderedPages.reduce((items, page, index) => {
const previousPage = orderedPages[index - 1]
if (previousPage && page - previousPage > 1) {
items.push(`ellipsis-${previousPage}-${page}`)
}
items.push(page)
return items
}, [])
}