Skip to content

Commit 54fc509

Browse files
authored
feat(PdfReader): remove async/await getPdfUrl (#845)
* feat: redesign backdrop function * refactor: 移除异步逻辑 * chore: bump version 10.0.22
1 parent 5bccde5 commit 54fc509

4 files changed

Lines changed: 41 additions & 26 deletions

File tree

src/components/BootstrapBlazor.PdfReader/BootstrapBlazor.PdfReader.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.0.21</Version>
4+
<Version>10.0.22</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

src/components/BootstrapBlazor.PdfReader/PdfReader.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@
101101
</div>
102102
</div>
103103
</div>
104+
<div class="bb-view-pdf-backdrop invisible"></div>
104105
<div class="bb-view-pdf-info invisible">
105-
<div class="bb-view-pdf-backdrop"></div>
106106
<div class="bb-view-pdf-dialog">
107107
<div class="bb-view-pdf-dialog-title mb-3">@Localizer["DocumentProperties"]</div>
108108
<div class="bb-view-pdf-dialog-item">

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
527527
rotateView(pdfViewer, 90);
528528
});
529529
EventHandler.on(toolbar, "click", ".bb-view-print", async e => {
530-
await printPdf(el, options);
530+
printPdf(el, options);
531531
await invoke.invokeMethodAsync("Printing");
532532
})
533533
EventHandler.on(toolbar, "click", ".dropdown-item-pages", async e => {
@@ -540,15 +540,15 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
540540
pdfViewer.spreadMode = 0;
541541
}
542542
});
543-
EventHandler.on(toolbar, "click", ".bb-view-download", async e => {
543+
EventHandler.on(toolbar, "click", ".bb-view-download", e => {
544544
let fileName = el.getAttribute('data-bb-download');
545545
if (fileName === null) {
546546
const docTitle = el.querySelector('.bb-view-subject');
547547
if (docTitle) {
548548
fileName = docTitle.textContent;
549549
}
550550
}
551-
await downloadPdf(options, fileName);
551+
downloadPdf(options, fileName);
552552
});
553553

554554
EventHandler.on(toolbar, "click", ".dropdown-item-presentation", async e => {
@@ -562,6 +562,8 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
562562
//}
563563
});
564564
EventHandler.on(toolbar, "click", ".dropdown-item-doc", e => {
565+
showBackdrop(el);
566+
565567
const dialog = el.querySelector(".bb-view-pdf-info");
566568
if (dialog) {
567569
dialog.classList.add("show");
@@ -574,25 +576,23 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
574576
if (dialog) {
575577
dialog.classList.remove("show");
576578
}
579+
580+
hideBackdrop(el);
577581
});
578582
}
579583

580-
const downloadPdf = async (options, fileName) => {
584+
const downloadPdf = (options, fileName) => {
581585
if (fileName === null) {
582586
fileName = "download.pdf";
583587
}
584588

585-
await getPdfUrl(options, url => {
589+
getPdfUrl(options, url => {
586590
const anchorElement = document.createElement('a');
587591
anchorElement.href = url;
588592
anchorElement.download = fileName;
589593
document.body.appendChild(anchorElement);
590594
anchorElement.click();
591595
document.body.removeChild(anchorElement);
592-
593-
return new Promise((resolve, reject) => {
594-
resolve();
595-
});
596596
});
597597
}
598598

@@ -761,7 +761,7 @@ const makeThumb = async page => {
761761
return canvas;
762762
}
763763

764-
const printPdf = async (el, options) => {
764+
const printPdf = (el, options) => {
765765
let iframe = el.querySelector(".bb-view-print-iframe");
766766
if (iframe === null) {
767767
iframe = document.createElement("iframe");
@@ -772,26 +772,37 @@ const printPdf = async (el, options) => {
772772
el.appendChild(iframe);
773773
}
774774

775-
await getPdfUrl(options, url => {
775+
getPdfUrl(options, url => {
776776
iframe.src = url;
777777
iframe.onload = () => {
778778
iframe.contentWindow.focus();
779779
iframe.contentWindow.print();
780780
};
781-
return new Promise((resolve, reject) => {
782-
resolve();
783-
});
784781
});
785782
}
786783

787-
const getPdfUrl = async (options, callback) => {
784+
const showBackdrop = el => {
785+
const backdrop = el.querySelector('.bb-view-pdf-backdrop');
786+
if (backdrop) {
787+
backdrop.classList.add('show');
788+
}
789+
}
790+
791+
const hideBackdrop = el => {
792+
const backdrop = el.querySelector('.bb-view-pdf-backdrop');
793+
if (backdrop) {
794+
backdrop.classList.remove('show');
795+
}
796+
}
797+
798+
const getPdfUrl = (options, callback) => {
788799
if (options.url) {
789800
callback(options.url);
790801
}
791802
else if (options.data) {
792803
const blob = new Blob([options.data], { type: 'application/pdf' });
793804
var url = window.URL.createObjectURL(blob);
794-
await callback(url);
805+
callback(url);
795806
window.URL.revokeObjectURL(url);
796807
}
797808
}

src/components/BootstrapBlazor.PdfReader/wwwroot/css/pdf_reader.css

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@
237237
inset: 0;
238238
}
239239

240+
.bb-view-pdf-backdrop {
241+
position: absolute;
242+
inset: 0;
243+
background-color: #000;
244+
opacity: 0.6;
245+
z-index: 1;
246+
}
247+
248+
.bb-view-pdf-backdrop:not(.show) {
249+
display: none;
250+
}
251+
240252
.bb-view-pdf-info {
241253
position: absolute;
242254
inset: 0;
@@ -250,14 +262,6 @@
250262
display: none;
251263
}
252264

253-
.bb-view-pdf-backdrop {
254-
position: absolute;
255-
inset: 0;
256-
background-color: #000;
257-
opacity: 0.6;
258-
z-index: 1;
259-
}
260-
261265
.bb-view-pdf-dialog {
262266
background-color: #fff;
263267
padding: 1rem;

0 commit comments

Comments
 (0)