From 98b00762c525a50ef0e2b9daaf3885ccb71f2f8e Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Mon, 18 Dec 2023 15:02:29 -0500 Subject: [PATCH] PDF back/forward buttons --- CHANGELOG.md | 2 ++ client/MessagePDF.coffee | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d100577..355aa4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ instead of version numbers. * PDFs now support typing a page number manually, which makes it easier to navigate large documents. [[#346](https://github.com/edemaine/coauthor/issues/346)] +* PDFs remember your previous page when you click on a link, + adding back/forward navigation buttons similar to browser back/forward. ## 2023-12-17 diff --git a/client/MessagePDF.coffee b/client/MessagePDF.coffee index 7755ed0..0dc94cc 100644 --- a/client/MessagePDF.coffee +++ b/client/MessagePDF.coffee @@ -44,6 +44,8 @@ WrappedMessagePDF = React.memo ({file}) -> [pageInput, setPageInput] = useState 1 [pageNum, setPageNum] = useState() [numPages, setNumPages] = useState() + [pageBack, setPageBack] = useState [] + [pageForward, setPageForward] = useState [] [fit, setFit] = useState 'page' theme = useMessageTheme file [dims, setDims] = useState {width: 0, height: 0} @@ -84,7 +86,7 @@ WrappedMessagePDF = React.memo ({file}) -> useEffect -> if pdf? and pageNum? pdf.getPage pageNum - .then (pageLoaded) -> setPage pageLoaded + .then (pageLoaded) => setPage pageLoaded else setPage undefined undefined @@ -156,6 +158,7 @@ WrappedMessagePDF = React.memo ({file}) -> -> renderTask.cancel() , [page, elementWidth, fit, inView] + ## Synchronize page input with navigation of page number useEffect => setPageInput pageNum if pageNum? , [pageNum] @@ -172,6 +175,14 @@ WrappedMessagePDF = React.memo ({file}) -> p = Math.round e.currentTarget.valueAsNumber if 1 <= p <= numPages setPageNum p + onPageBack = (e) => + setPageForward [pageNum, ...pageForward] + setPageNum pageBack[0] + setPageBack pageBack[1..] + onPageForward = (e) => + setPageBack [pageNum, ...pageBack] + setPageNum pageForward[0] + setPageForward pageForward[1..] onFit = (newFit) => (e) => e.currentTarget.blur() setFit newFit @@ -188,6 +199,22 @@ WrappedMessagePDF = React.memo ({file}) -> else <>
+ {if pageBack.length or pageForward.length + <> + + + + + + + + } {if fit == 'page'