From 366fff3343299caeec92eee526261c2aba79312e Mon Sep 17 00:00:00 2001 From: Michelle Blanchette Date: Sun, 14 Jan 2024 18:24:51 -0500 Subject: [PATCH] improve pdf-incompatible browser detection and handling, mostly notable for mobile browsers #210 --- .../attachment/AttachmentThumbnail.jsx | 2 +- src/components/attachment/util.jsx | 29 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/components/attachment/AttachmentThumbnail.jsx b/src/components/attachment/AttachmentThumbnail.jsx index a3a5fa0d..4575fe76 100644 --- a/src/components/attachment/AttachmentThumbnail.jsx +++ b/src/components/attachment/AttachmentThumbnail.jsx @@ -29,7 +29,7 @@ export default function AttachmentThumbnail({ attachment }) { } else if ( isFileType(attachment, ['pdf']) ) { content = ( -

Download {attachment.name} to view

+

Download {attachment.name} to view

); } else if ( '_ptc_oembed_html' in attachment && attachment._ptc_oembed_html ) { diff --git a/src/components/attachment/util.jsx b/src/components/attachment/util.jsx index 29ea73bc..b2bd6b54 100644 --- a/src/components/attachment/util.jsx +++ b/src/components/attachment/util.jsx @@ -51,14 +51,27 @@ export function findAndMonitorLoadingMedia(rootNode) { } } - for ( let objectFrame of rootNode.querySelectorAll('object:not(.load-monitoring-disabled)') ) { - if ( objectFrame ) { - // Object has not yet loaded data. - objectFrame.classList.add('--is-loading'); - // Listen for when object data is loaded. - objectFrame.addEventListener('load', handleMediaLoad); - // Listen for when object fails to load. - objectFrame.addEventListener('error', handleMediaError); + for ( let pdfFrame of rootNode.querySelectorAll('object[type="application/pdf"]:not(.load-monitoring-disabled)') ) { + if ( + !! navigator?.mimeTypes['application/pdf']?.enabledPlugin || + navigator.pdfViewerEnabled + ) { + // Browser can display PDFs inline. + if ( + 'contentDocument' in pdfFrame && + null !== pdfFrame.contentDocument + ) { + // Object has not yet loaded the document. + pdfFrame.classList.add('--is-loading'); + // Listen for when object document is loaded. + pdfFrame.addEventListener('load', handleMediaLoad); + // Listen for when object fails to load. + pdfFrame.addEventListener('error', handleMediaError); + } + } else { + // Browser won't load or trigger error event, + // so mark it as failed to load. + pdfFrame.classList.add('--is-error'); } } }