Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show previous PDF document while the next one is being created & rendered #91

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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