refactor: lazyload pdf.js
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Baoshuo Ren 2023-02-02 11:19:35 +08:00
parent daf0c55485
commit fe5e9b17e5
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
7 changed files with 61 additions and 53 deletions

View File

@ -2,7 +2,6 @@
requireLib('bootstrap5'); requireLib('bootstrap5');
requireLib('hljs'); requireLib('hljs');
requireLib('mathjax'); requireLib('mathjax');
requireLib('pdf.js');
requirePHPLib('form'); requirePHPLib('form');
requirePHPLib('judger'); requirePHPLib('judger');

View File

@ -2,7 +2,6 @@
requireLib('bootstrap5'); requireLib('bootstrap5');
requireLib('hljs'); requireLib('hljs');
requireLib('mathjax'); requireLib('mathjax');
requireLib('pdf.js');
requirePHPLib('form'); requirePHPLib('form');
requirePHPLib('judger'); requirePHPLib('judger');

View File

@ -2,7 +2,6 @@
requireLib('bootstrap5'); requireLib('bootstrap5');
requireLib('mathjax'); requireLib('mathjax');
requireLib('hljs'); requireLib('hljs');
requireLib('pdf.js');
requirePHPLib('form'); requirePHPLib('form');
Auth::check() || redirectToLogin(); Auth::check() || redirectToLogin();

View File

@ -185,7 +185,6 @@ class UOJBlogEditor {
if ($this->type == 'blog') { if ($this->type == 'blog') {
$req_lib = [ $req_lib = [
'mathjax' => '', 'mathjax' => '',
'pdf.js' => '',
]; ];
if (isset($REQUIRE_LIB['bootstrap5'])) { if (isset($REQUIRE_LIB['bootstrap5'])) {

View File

@ -308,54 +308,6 @@ if (!isset($ShowPageHeader)) {
<?= HTML::css_link('/css/font-awesome.min.css') ?> <?= HTML::css_link('/css/font-awesome.min.css') ?>
<?php endif ?> <?php endif ?>
<?php if (isset($REQUIRE_LIB['pdf.js'])) : ?>
<!-- pdf.js -->
<?= HTML::js_src('/js/pdf.js') ?>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = '/js/pdf.worker.js';
function randstr(prefix) {
return Math.random().toString(36).replace('0.', prefix || '');
}
$(document).ready(function() {
$('div[data-pdf]').each(function() {
var _this = $(this);
var pdf_src = $(this).data('src');
$(this).css('width', '100%').css('height', '100%');
var task = pdfjsLib.getDocument(pdf_src);
var id = 'pdf_' + task.docId;
$(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>');
pdf.getPage(i).then(function(page) {
var viewport = page.getViewport({
scale: 2,
});
var canvas = document.getElementById(id + '_page_' + page.pageNumber);
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render({
canvasContext: canvas.getContext('2d'),
viewport: viewport,
});
});
}
});
});
});
</script>
<?php endif ?>
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]> <!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>

View File

@ -235,7 +235,8 @@ h6,
/* PDF */ /* PDF */
.pdf-page-canvas { .pdf-page-canvas {
width: 100% !important; /* min-width: 100%; */
max-width: 100%;
} }
/* Form */ /* Form */

View File

@ -1052,6 +1052,65 @@ function showCommentReplies(id, replies) {
); );
} }
// PDF
$(document).ready(function() {
$('div[data-pdf]').each(function() {
$(this).append(
$('<div class="uoj-pdf-loading-spinner border d-flex flex-column justify-content-center align-items-center" style="width: 100%; height: 350px;" />')
.append('<div class="spinner-border text-muted" style="width: 3rem; height: 3rem;" />')
.append('<div class="mt-3">Loading PDF...</div>')
);
});
$LAB.script('/js/pdf.js').wait(function() {
pdfjsLib.GlobalWorkerOptions.workerSrc = '/js/pdf.worker.js';
// Support HiDPI-screens.
var outputScale = window.devicePixelRatio || 1;
$('div[data-pdf]').each(function() {
var _this = $(this);
var pdf_src = $(this).data('src');
$(this).css('width', '100%').css('height', '100%');
var task = pdfjsLib.getDocument(pdf_src);
var id = 'pdf_' + task.docId;
$(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>');
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,
});
});
}
$('.uoj-pdf-loading-spinner', _this).remove();
});
});
});
});
// Tooltip // Tooltip
$(document).ready(function() { $(document).ready(function() {
bootstrap.Tooltip.jQueryInterface.call($('[data-bs-toggle="tooltip"]')); bootstrap.Tooltip.jQueryInterface.call($('[data-bs-toggle="tooltip"]'));