chore: load pdf.js only if have pdf content

This commit is contained in:
Baoshuo Ren 2023-02-04 11:02:51 +08:00
parent cd36b3d209
commit d4d98f6d55
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1291,53 +1291,55 @@ $(document).ready(function() {
);
});
$LAB.script('/js/pdf.js').wait(function() {
pdfjsLib.GlobalWorkerOptions.workerSrc = '/js/pdf.worker.js';
if ($('div[data-pdf]').length > 0) {
$LAB.script('/js/pdf.js').wait(function() {
pdfjsLib.GlobalWorkerOptions.workerSrc = '/js/pdf.worker.js';
// Support HiDPI-screens.
var outputScale = window.devicePixelRatio || 1;
// Support HiDPI-screens.
var outputScale = window.devicePixelRatio || 1;
$('div[data-pdf]').each(function() {
var _this = $(this);
var pdf_src = $(this).data('src');
$('div[data-pdf]').each(function() {
var _this = $(this);
var pdf_src = $(this).data('src');
$(this).css('width', '100%').css('height', '100%');
$(this).css('width', '100%').css('height', '100%');
var task = pdfjsLib.getDocument(pdf_src);
var id = 'pdf_' + task.docId;
var task = pdfjsLib.getDocument(pdf_src);
var id = 'pdf_' + task.docId;
$(this).attr('id', id + '_container');
$(this).attr('id', id + '_container');
task.promise.then(function(pdf) {
for (var i = 1; i <= pdf.numPages; i++) {
$(_this).append('<canvas id="' + id + '_page_' + i + '" class="pdf-page-canvas"></canvas>');
task.promise.then(function(pdf) {
for (var i = 1; i <= pdf.numPages; i++) {
$(_this).append('<canvas id="' + id + '_page_' + i + '" class="pdf-page-canvas"></canvas>');
pdf.getPage(i).then(function(page) {
var viewport = page.getViewport({
scale: 2.5,
pdf.getPage(i).then(function(page) {
var viewport = page.getViewport({
scale: 2.5,
});
var canvas = document.getElementById(id + '_page_' + page.pageNumber);
canvas.height = Math.floor(viewport.height * outputScale);
canvas.width = Math.floor(viewport.width * outputScale);
var transform = outputScale !== 1
? [outputScale, 0, 0, outputScale, 0, 0]
: null;
page.render({
canvasContext: canvas.getContext('2d'),
viewport: viewport,
transform: transform,
});
});
}
var canvas = document.getElementById(id + '_page_' + page.pageNumber);
canvas.height = Math.floor(viewport.height * outputScale);
canvas.width = Math.floor(viewport.width * outputScale);
var transform = outputScale !== 1
? [outputScale, 0, 0, outputScale, 0, 0]
: null;
page.render({
canvasContext: canvas.getContext('2d'),
viewport: viewport,
transform: transform,
});
});
}
$('.uoj-pdf-loading-spinner', _this).remove();
$('.uoj-pdf-loading-spinner', _this).remove();
});
});
});
});
}
});
// Tooltip