diff --git a/_config.yml b/_config.yml index 2056e22..111e4f7 100644 --- a/_config.yml +++ b/_config.yml @@ -923,6 +923,9 @@ vendors: algolia_search: instant_search: + # PDF + pdfobject: + # Mermaid mermaid: diff --git a/_vendors.yml b/_vendors.yml index 63b89a8..65d9536 100644 --- a/_vendors.yml +++ b/_vendors.yml @@ -118,6 +118,10 @@ instant_search: name: instantsearch.js version: 4.8.4 file: dist/instantsearch.production.min.js +pdfobject: + name: pdfobject + version: 2.2.4 + file: pdfobject.min.js mermaid: name: mermaid version: 8.8.2 diff --git a/layout/_third-party/tags/pdf.njk b/layout/_third-party/tags/pdf.njk index 30cafcd..7290396 100644 --- a/layout/_third-party/tags/pdf.njk +++ b/layout/_third-party/tags/pdf.njk @@ -1,23 +1,23 @@ {%- if theme.pdf.enable %} +{%- set pdfobject_uri = theme.vendors.pdfobject %} {%- set pdf_uri = url_for('lib/pdf/web/viewer.html') %} -document.querySelectorAll('.pdfobject-container').forEach(element => { - const url = element.dataset.target; - const pdfOpenParams = { - navpanes : 0, - toolbar : 0, - statusbar: 0, - pagemode : 'thumbs', - view : 'FitH' - }; - const pdfOpenFragment = '#' + Object.entries(pdfOpenParams).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&'); - const fullURL = `{{ pdf_uri }}?file=${encodeURIComponent(url)}${pdfOpenFragment}`; - - if (NexT.utils.supportsPDFs()) { - element.innerHTML = ``; - } else { - element.innerHTML = ``; - } -}); +if (document.querySelectorAll('.pdf-container').length) { + NexT.utils.getScript('{{ pdfobject_uri }}', () => { + document.querySelectorAll('.pdf-container').forEach(element => { + PDFObject.embed(element.dataset.target, element, { + pdfOpenParams: { + navpanes : 0, + toolbar : 0, + statusbar: 0, + pagemode : 'thumbs', + view : 'FitH' + }, + PDFJS_URL: '{{ pdf_uri }}', + height : element.dataset.height + }); + }); + }, window.PDFObject); +} {%- endif %} diff --git a/scripts/tags/pdf.js b/scripts/tags/pdf.js index 8145365..6c29f6c 100644 --- a/scripts/tags/pdf.js +++ b/scripts/tags/pdf.js @@ -6,5 +6,5 @@ module.exports = ctx => function(args) { const theme = ctx.theme.config; - return `
`; + return `
`; }; diff --git a/source/js/utils.js b/source/js/utils.js index 6451ca9..1fea7e2 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -281,13 +281,6 @@ NexT.utils = { }); }, - supportsPDFs: function() { - const ua = navigator.userAgent; - const supportsPdfMimeType = typeof navigator.mimeTypes['application/pdf'] !== 'undefined'; - const isIOS = /iphone|ipad|ipod/i.test(ua.toLowerCase()); - return ua.includes('irefox') || (supportsPdfMimeType && !isIOS); - }, - getComputedStyle: function(element) { const clone = element.cloneNode(true); clone.style.position = 'absolute'; diff --git a/test/tags/pdf.js b/test/tags/pdf.js index 6186677..d360000 100644 --- a/test/tags/pdf.js +++ b/test/tags/pdf.js @@ -14,10 +14,10 @@ describe('pdf', () => { }); it('default', () => { - pdf(['https://example.com/sample.pdf']).should.eql('
'); + pdf(['https://example.com/sample.pdf']).should.eql('
'); }); it('custom height', () => { - pdf(['https://example.com/sample.pdf', '1000px']).should.eql('
'); + pdf(['https://example.com/sample.pdf', '1000px']).should.eql('
'); }); });