Use PDFObject

This commit is contained in:
Mimi 2020-11-24 18:34:15 +08:00
parent 09b74ed47e
commit 29b86361b7
6 changed files with 28 additions and 28 deletions

View File

@ -923,6 +923,9 @@ vendors:
algolia_search: algolia_search:
instant_search: instant_search:
# PDF
pdfobject:
# Mermaid # Mermaid
mermaid: mermaid:

View File

@ -118,6 +118,10 @@ instant_search:
name: instantsearch.js name: instantsearch.js
version: 4.8.4 version: 4.8.4
file: dist/instantsearch.production.min.js file: dist/instantsearch.production.min.js
pdfobject:
name: pdfobject
version: 2.2.4
file: pdfobject.min.js
mermaid: mermaid:
name: mermaid name: mermaid
version: 8.8.2 version: 8.8.2

View File

@ -1,23 +1,23 @@
{%- if theme.pdf.enable %} {%- if theme.pdf.enable %}
{%- set pdfobject_uri = theme.vendors.pdfobject %}
{%- set pdf_uri = url_for('lib/pdf/web/viewer.html') %} {%- set pdf_uri = url_for('lib/pdf/web/viewer.html') %}
<script{{ pjax }}> <script{{ pjax }}>
document.querySelectorAll('.pdfobject-container').forEach(element => { if (document.querySelectorAll('.pdf-container').length) {
const url = element.dataset.target; NexT.utils.getScript('{{ pdfobject_uri }}', () => {
const pdfOpenParams = { document.querySelectorAll('.pdf-container').forEach(element => {
PDFObject.embed(element.dataset.target, element, {
pdfOpenParams: {
navpanes : 0, navpanes : 0,
toolbar : 0, toolbar : 0,
statusbar: 0, statusbar: 0,
pagemode : 'thumbs', pagemode : 'thumbs',
view : 'FitH' view : 'FitH'
}; },
const pdfOpenFragment = '#' + Object.entries(pdfOpenParams).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&'); PDFJS_URL: '{{ pdf_uri }}',
const fullURL = `{{ pdf_uri }}?file=${encodeURIComponent(url)}${pdfOpenFragment}`; height : element.dataset.height
if (NexT.utils.supportsPDFs()) {
element.innerHTML = `<embed class="pdfobject" src="${url + pdfOpenFragment}" type="application/pdf" style="height: ${element.dataset.height};">`;
} else {
element.innerHTML = `<iframe src="${fullURL}" style="height: ${element.dataset.height};" frameborder="0"></iframe>`;
}
}); });
});
}, window.PDFObject);
}
</script> </script>
{%- endif %} {%- endif %}

View File

@ -6,5 +6,5 @@
module.exports = ctx => function(args) { module.exports = ctx => function(args) {
const theme = ctx.theme.config; const theme = ctx.theme.config;
return `<div class="pdfobject-container" data-target="${args[0]}" data-height="${args[1] || theme.pdf.height}"></div>`; return `<div class="pdf-container" data-target="${args[0]}" data-height="${args[1] || theme.pdf.height}"></div>`;
}; };

View File

@ -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) { getComputedStyle: function(element) {
const clone = element.cloneNode(true); const clone = element.cloneNode(true);
clone.style.position = 'absolute'; clone.style.position = 'absolute';

View File

@ -14,10 +14,10 @@ describe('pdf', () => {
}); });
it('default', () => { it('default', () => {
pdf(['https://example.com/sample.pdf']).should.eql('<div class="pdfobject-container" data-target="https://example.com/sample.pdf" data-height="500px"></div>'); pdf(['https://example.com/sample.pdf']).should.eql('<div class="pdf-container" data-target="https://example.com/sample.pdf" data-height="500px"></div>');
}); });
it('custom height', () => { it('custom height', () => {
pdf(['https://example.com/sample.pdf', '1000px']).should.eql('<div class="pdfobject-container" data-target="https://example.com/sample.pdf" data-height="1000px"></div>'); pdf(['https://example.com/sample.pdf', '1000px']).should.eql('<div class="pdf-container" data-target="https://example.com/sample.pdf" data-height="1000px"></div>');
}); });
}); });