Skip to content

Commit

Permalink
feat: show previous PDF document while the next one is being created …
Browse files Browse the repository at this point in the history
…& rendered (#91)
  • Loading branch information
wojtekmaj committed Oct 10, 2022
1 parent 6fbd193 commit 2d8592b
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/components/Repl/PDFViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ const DocumentWrapper = styled.div`
z-index: 500;
align-items: center;
justify-content: center;
.react-pdf__Document {
&.previous-document {
canvas {
opacity: 0.5;
}
}
&.rendering-document {
position: absolute;
.react-pdf__Page {
box-shadow: none;
}
}
}
`;

const Message = styled.div`
Expand All @@ -47,6 +63,8 @@ const PDFViewer = ({ value, onUrlChange, onRenderError }) => {

const [currentPage, setCurrentPage] = useState(1);

const [previousRenderValue, setPreviousRenderValue] = useState(null);

const render = useAsync(async () => {
if (!value) return null;

Expand All @@ -73,17 +91,45 @@ const PDFViewer = ({ value, onUrlChange, onRenderError }) => {
setCurrentPage((prev) => Math.min(prev, d.numPages));
};

const isFirstRendering = !previousRenderValue;

const isLatestValueRendered = previousRenderValue === render.value;
const isBusy = render.loading || !isLatestValueRendered;

const shouldShowTextLoader = isFirstRendering && isBusy;
const shouldShowPreviousDocument = !isFirstRendering && isBusy;

return (
<Wrapper>
<Message active={render.loading}>Rendering PDF...</Message>
<Message active={shouldShowTextLoader}>Rendering PDF...</Message>

<Message active={!render.loading && !value}>
You are not rendering a valid document
</Message>

<DocumentWrapper>
<Document file={render.value} onLoadSuccess={onDocumentLoad}>
<Page pageNumber={currentPage} />
{shouldShowPreviousDocument && previousRenderValue ? (
<Document
key={previousRenderValue}
className="previous-document"
file={previousRenderValue}
loading={null}
>
<Page key={currentPage} pageNumber={currentPage} />
</Document>
) : null}
<Document
key={render.value}
className={shouldShowPreviousDocument ? 'rendering-document' : null}
file={render.value}
loading={null}
onLoadSuccess={onDocumentLoad}
>
<Page
key={currentPage}
pageNumber={currentPage}
onRenderSuccess={() => setPreviousRenderValue(render.value)}
/>
</Document>
</DocumentWrapper>

Expand Down

1 comment on commit 2d8592b

@vercel
Copy link

@vercel vercel bot commented on 2d8592b Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

site – ./

react-pdf.org
site-diegomura.vercel.app
site-git-master-diegomura.vercel.app
v3.react-pdf.org

Please sign in to comment.