feat: change page url

This commit is contained in:
monoid 2023-12-01 14:09:31 +09:00
parent 4fc62c55e2
commit aa13213483
1 changed files with 19 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import { Typography, styled } from "@mui/material"; import { Typography, styled } from "@mui/material";
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { Document } from "../../accessor/document"; import { Document } from "../../accessor/document";
type ComicType = "comic" | "artist cg" | "donjinshi" | "western"; type ComicType = "comic" | "artist cg" | "donjinshi" | "western";
@ -30,27 +31,41 @@ const CurrentView = styled("img")(({theme})=>({
export const ComicReader = (props: { doc: Document }) => { export const ComicReader = (props: { doc: Document }) => {
const additional = props.doc.additional; const additional = props.doc.additional;
const [curPage, setCurPage] = useState(0); const [searchParams, setSearchParams] = useSearchParams();
const curPage = (parseInt(searchParams.get("page") ?? "0"));
const setCurPage = (n: number) => {
setSearchParams([
["page", n.toString()]
]);
}
if (isNaN(curPage)){
return <Typography>Error. Page number is not a number.</Typography>
}
if (!("page" in additional)) { if (!("page" in additional)) {
console.error("invalid content : page read fail : " + JSON.stringify(additional)); console.error("invalid content : page read fail : " + JSON.stringify(additional));
return <Typography>Error. DB error. page restriction</Typography>; return <Typography>Error. DB error. page restriction</Typography>;
} }
const maxPage: number = additional["page"] as number;
const PageDown = () => setCurPage(Math.max(curPage - 1, 0)); const PageDown = () => setCurPage(Math.max(curPage - 1, 0));
const PageUp = () => setCurPage(Math.min(curPage + 1, page - 1)); const PageUp = () => setCurPage(Math.min(curPage + 1, maxPage - 1));
const page: number = additional["page"] as number;
const onKeyUp = (e: KeyboardEvent) => { const onKeyUp = (e: KeyboardEvent) => {
console.log(`currently: ${curPage}/${maxPage}`)
if (e.code === "ArrowLeft") { if (e.code === "ArrowLeft") {
PageDown(); PageDown();
} else if (e.code === "ArrowRight") { } else if (e.code === "ArrowRight") {
PageUp(); PageUp();
} }
}; };
useEffect(() => { useEffect(() => {
document.addEventListener("keydown", onKeyUp); document.addEventListener("keydown", onKeyUp);
return () => { return () => {
document.removeEventListener("keydown", onKeyUp); document.removeEventListener("keydown", onKeyUp);
}; };
}); }, [curPage]);
// theme.mixins.toolbar.minHeight; // theme.mixins.toolbar.minHeight;
return ( return (
<ViewMain> <ViewMain>