aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/nuldoc/nuldoc-src/components/Pagination.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-02-01 00:49:15 +0900
committernsfisis <nsfisis@gmail.com>2026-02-01 00:49:19 +0900
commit6dedddc545e2f1930bdc2256784eb1551bd4231d (patch)
tree75fcb5a6043dc0f2c31b098bf3cfd17a2b938599 /services/nuldoc/nuldoc-src/components/Pagination.ts
parentd08e3edb65b215152aa26e3518fb2f2cd7071c4b (diff)
downloadnsfisis.dev-6dedddc545e2f1930bdc2256784eb1551bd4231d.tar.gz
nsfisis.dev-6dedddc545e2f1930bdc2256784eb1551bd4231d.tar.zst
nsfisis.dev-6dedddc545e2f1930bdc2256784eb1551bd4231d.zip
feat(nuldoc): rewrite nuldoc in Ruby
Diffstat (limited to 'services/nuldoc/nuldoc-src/components/Pagination.ts')
-rw-r--r--services/nuldoc/nuldoc-src/components/Pagination.ts93
1 files changed, 0 insertions, 93 deletions
diff --git a/services/nuldoc/nuldoc-src/components/Pagination.ts b/services/nuldoc/nuldoc-src/components/Pagination.ts
deleted file mode 100644
index d9203165..00000000
--- a/services/nuldoc/nuldoc-src/components/Pagination.ts
+++ /dev/null
@@ -1,93 +0,0 @@
-import { a, div, Element, nav, span } from "../dom.ts";
-
-type Props = {
- currentPage: number;
- totalPages: number;
- basePath: string;
-};
-
-export default function Pagination(
- { currentPage, totalPages, basePath }: Props,
-): Element {
- if (totalPages <= 1) {
- return div({});
- }
-
- const pages = generatePageNumbers(currentPage, totalPages);
-
- return nav(
- { class: "pagination" },
- div(
- { class: "pagination-prev" },
- currentPage > 1
- ? a({ href: pageUrlAt(basePath, currentPage - 1) }, "前へ")
- : null,
- ),
- ...pages.map((page) => {
- if (page === "...") {
- return div({ class: "pagination-elipsis" }, "…");
- } else if (page === currentPage) {
- return div(
- { class: "pagination-page pagination-page-current" },
- span({}, String(page)),
- );
- } else {
- return div(
- { class: "pagination-page" },
- a({ href: pageUrlAt(basePath, page) }, String(page)),
- );
- }
- }),
- div(
- { class: "pagination-next" },
- currentPage < totalPages
- ? a({ href: pageUrlAt(basePath, currentPage + 1) }, "次へ")
- : null,
- ),
- );
-}
-
-type PageItem = number | "...";
-
-/**
- * Generates page numbers for pagination display.
- *
- * - Always show the first page
- * - Always show the last page
- * - Always show the current page
- * - Always show the page before and after the current page
- * - If there's only one page gap between displayed pages, fill it
- * - If there are two or more pages gap between displayed pages, show ellipsis
- */
-function generatePageNumbers(
- currentPage: number,
- totalPages: number,
-): PageItem[] {
- const pages = new Set<number>();
- pages.add(1);
- pages.add(Math.max(1, currentPage - 1));
- pages.add(currentPage);
- pages.add(Math.min(totalPages, currentPage + 1));
- pages.add(totalPages);
-
- const sorted = Array.from(pages).sort((a, b) => a - b);
-
- const result: PageItem[] = [];
- for (let i = 0; i < sorted.length; i++) {
- if (i > 0) {
- const gap = sorted[i] - sorted[i - 1];
- if (gap === 2) {
- result.push(sorted[i - 1] + 1);
- } else if (gap > 2) {
- result.push("...");
- }
- }
- result.push(sorted[i]);
- }
-
- return result;
-}
-
-function pageUrlAt(basePath: string, page: number): string {
- return page === 1 ? basePath : `${basePath}${page}/`;
-}