forked from BenjaminLawson/PDF.js-Viewer-Shortcode
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpdfjs-media-button.js
55 lines (45 loc) · 2.13 KB
/
pdfjs-media-button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
jQuery( function( $ ) {
$('.js-insert-pdfjs').click(PDFjs_openMediaWindow);
if (typeof acf !== 'undefined') {
acf.addAction( 'load', function() {
$( '.js-insert-pdfjs' ).on( 'click', function() {
PDFjs_openMediaWindow();
} );
});
}
function PDFjs_openMediaWindow() {
const frame = wp.media( {
title: 'Insert a PDF',
library: { type: 'application/pdf' },
multiple: false,
button: { text: 'Insert' },
} );
frame.on( 'select', function() {
let selectionID = frame.state().get('selection').first().toJSON().id;
let selectionURL = frame.state().get('selection').first().toJSON().url;
selectionURL = selectionURL.replace(/(<([^>]+)>)/gi, "")
let fullscreenLink = "fullscreen=false";
if (typeof window.pdfjs_options.pdfjs_fullscreen_link !== 'undefined' && window.pdfjs_options.pdfjs_fullscreen_link !== '') {
fullscreenLink = "fullscreen=true";
}
let downloadLink = "download=false";
if (typeof window.pdfjs_options.pdfjs_download_button !== 'undefined' && window.pdfjs_options.pdfjs_download_button !== '') {
downloadLink = "download=true";
}
let printLink = "print=false";
if (typeof window.pdfjs_options.pdfjs_print_button !== 'undefined' && window.pdfjs_options.pdfjs_print_button !== '') {
printLink = "print=true";
}
let viewerWidth = "100%";
if (typeof window.pdfjs_options.pdfjs_embed_width !== 'undefined' && window.pdfjs_options.pdfjs_embed_width !== '0' && window.pdfjs_options.pdfjs_embed_width !== '') {
viewerWidth = window.pdfjs_options.pdfjs_embed_width + "px";
}
let viewerHeight = "800px";
if (typeof window.pdfjs_options.pdfjs_embed_height !== 'undefined' && window.pdfjs_options.pdfjs_embed_height !== '0' && window.pdfjs_options.pdfjs_embed_height !== '') {
viewerHeight = window.pdfjs_options.pdfjs_embed_height + "px";
}
wp.media.editor.insert('[pdfjs-viewer url="' + selectionURL + '" attachment_id="' + selectionID + '" viewer_width=' + viewerWidth + ' viewer_height=' + viewerHeight + ' ' + fullscreenLink + ' ' + downloadLink + ' ' + printLink + ']');
} );
frame.open();
}
} );