mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-06 00:32:26 -08:00
fix(pdf): contain render failures instead of crashing the app
This commit is contained in:
2 files changed
+21
-9
No files matched your search
@@ -62,8 +62,11 @@ export const PdfViewer = as<'div', PdfViewerProps>(
|
||||
);
|
||||
const isLoading =
|
||||
pdfJSState.status === AsyncStatus.Loading || docState.status === AsyncStatus.Loading;
|
||||
const [renderError, setRenderError] = useState(false);
|
||||
const isError =
|
||||
pdfJSState.status === AsyncStatus.Error || docState.status === AsyncStatus.Error;
|
||||
pdfJSState.status === AsyncStatus.Error ||
|
||||
docState.status === AsyncStatus.Error ||
|
||||
renderError;
|
||||
const [pageNo, setPageNo] = useState(1);
|
||||
const [jumpAnchor, setJumpAnchor] = useState<RectCords>();
|
||||
|
||||
@@ -77,17 +80,26 @@ export const PdfViewer = as<'div', PdfViewerProps>(
|
||||
}, [pdfJSState, loadPdfDocument]);
|
||||
|
||||
useEffect(() => {
|
||||
if (docState.status === AsyncStatus.Success) {
|
||||
const doc = docState.data;
|
||||
if (pageNo < 0 || pageNo > doc.numPages) return;
|
||||
createPage(doc, pageNo, { scale: zoom }).then((canvas) => {
|
||||
if (docState.status !== AsyncStatus.Success) return undefined;
|
||||
const doc = docState.data;
|
||||
if (pageNo < 0 || pageNo > doc.numPages) return undefined;
|
||||
let cancelled = false;
|
||||
setRenderError(false);
|
||||
createPage(doc, pageNo, { scale: zoom })
|
||||
.then((canvas) => {
|
||||
if (cancelled) return;
|
||||
const container = containerRef.current;
|
||||
if (!container) return;
|
||||
container.textContent = '';
|
||||
container.append(canvas);
|
||||
canvas.style.touchAction = 'pan-x pan-y';
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setRenderError(true);
|
||||
});
|
||||
}
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [docState, pageNo, zoom]);
|
||||
|
||||
const handleDownload = () => {
|
||||
@@ -186,7 +198,7 @@ export const PdfViewer = as<'div', PdfViewerProps>(
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
{docState.status === AsyncStatus.Success && (
|
||||
{docState.status === AsyncStatus.Success && !renderError && (
|
||||
<Scroll
|
||||
ref={scrollRef}
|
||||
size="300"
|
||||
|
||||
@@ -41,11 +41,11 @@ export const createPage = async (
|
||||
canvas.width = pageViewport.width;
|
||||
canvas.height = pageViewport.height;
|
||||
|
||||
page.render({
|
||||
await page.render({
|
||||
canvas,
|
||||
canvasContext: context,
|
||||
viewport: pageViewport,
|
||||
});
|
||||
}).promise;
|
||||
|
||||
return canvas;
|
||||
};
|
||||
Reference in new issue
Block a user