Skip to content

Build your personal knowledge base with Trilium Notes

ui/pager: rewrite the pagination button distribution algorithm to better control the length

Adorian Doran82576c9703ad
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index e97aba782b..c1dcbf48f2 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2190,7 +2190,6 @@
"percentage": "%"
},
"pagination": {
- "page_title": "Page of {{startIndex}} - {{endIndex}}",
"total_notes": "{{count}} notes",
"prev_page": "Previous page",
"next_page": "Next page"
diff --git a/apps/client/src/widgets/collections/Pagination.tsx b/apps/client/src/widgets/collections/Pagination.tsx
index 282a6b36a5..5704715441 100644
--- a/apps/client/src/widgets/collections/Pagination.tsx
+++ b/apps/client/src/widgets/collections/Pagination.tsx
@@ -19,28 +19,7 @@ interface PaginationContext {
export function Pager({ page, pageSize, setPage, pageCount, totalNotes }: Omit<PaginationContext, "pageNotes">) {
if (pageCount < 2) return;
- let lastPrinted = false;
- let children: ComponentChildren[] = [];
- for (let i = 1; i <= pageCount; i++) {
- if (pageCount < 20 || i <= 5 || pageCount - i <= 5 || Math.abs(page - i) <= 2) {
- lastPrinted = true;
-
- const startIndex = (i - 1) * pageSize + 1;
- const endIndex = Math.min(totalNotes, i * pageSize);
-
- children.push((
- <Button
- text={i.toString()}
- title={t("pagination.page_title", { startIndex, endIndex })}
- disabled={(i === page)}
- onClick={() => setPage(i)}
- />
- ))
- } else if (lastPrinted) {
- children.push(<>{"... "}&nbsp;{" "}</>);
- lastPrinted = false;
- }
- }
+ const children = createPageButtons(page, setPage, pageCount);
return (
<div class="note-list-pager">
@@ -59,12 +38,59 @@ export function Pager({ page, pageSize, setPage, pageCount, totalNotes }: Omit<P
text={t("pagination.next_page")}
onClick={() => {setPage(++page)}}
/>
-
<span className="note-list-pager-total-count">({t("pagination.total_notes", { count: totalNotes })})</span>
</div>
)
}
+function createPageButtons(page: number, setPage: Dispatch<StateUpdater<number>>, pageCount: number): ComponentChildren[] {
+ const maxButtonCount = 9;
+ const maxLeftRightSegmentLength = 2;
+
+ // The left-side segment
+ const leftLength = Math.min(pageCount, maxLeftRightSegmentLength);
+ const leftStart = 1;
+
+ // The middle segment
+ const middleMaxLength = maxButtonCount - maxLeftRightSegmentLength * 2;
+ const middleLength = Math.min(pageCount - leftLength, middleMaxLength);
+ let middleStart = page - Math.floor(middleLength / 2);
+ middleStart = Math.max(middleStart, leftLength + 1);
+
+ // The right-side segment
+ const rightLength = Math.min(pageCount - (middleLength + leftLength), maxLeftRightSegmentLength);
+ const rightStart = pageCount - rightLength + 1;
+ middleStart = Math.min(middleStart, rightStart - middleLength);
+
+ return [
+ ...createSegment(leftStart, leftLength, page, setPage, false),
+ ...createSegment(middleStart, middleLength, page, setPage, (middleStart - leftLength > 1)),
+ ...createSegment(rightStart, rightLength, page, setPage, (rightStart - (middleStart + middleLength) > 1)),
+ ];
+}
+
+function createSegment(start: number, length: number, currentPage: number, setPage: Dispatch<StateUpdater<number>>, prependEllipsis: boolean): ComponentChildren[] {
+ const children: ComponentChildren[] = [];
+
+ if (prependEllipsis) {
+ children.push("...");
+ }
+
+ for (let i = 0; i < length; i++) {
+ const pageNum = start + i;
+
+ children.push((
+ <Button
+ text={pageNum.toString()}
+ disabled={(pageNum === currentPage)}
+ onClick={() => setPage(pageNum)}
+ />
+ ));
+ }
+
+ return children;
+}
+
export function usePagination(note: FNote, noteIds: string[]): PaginationContext {
const [ page, setPage ] = useState(1);
const [ pageNotes, setPageNotes ] = useState<FNote[]>();