Skip to content

Commit eee34f4

Browse files
committed
refactor: 移除异步逻辑
1 parent 88afe21 commit eee34f4

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

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

Lines changed: 26 additions & 25 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,15 +562,12 @@ 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");
568570
}
569-
570-
const backdrop = el.querySelector(".bb-view-pdf-backdrop");
571-
if (backdrop) {
572-
backdrop.classList.add("show");
573-
}
574571
});
575572

576573
const closeButton = el.querySelector(".btn-close-doc");
@@ -580,29 +577,22 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
580577
dialog.classList.remove("show");
581578
}
582579

583-
const backdrop = el.querySelector(".bb-view-pdf-backdrop");
584-
if (backdrop) {
585-
backdrop.classList.remove("show");
586-
}
580+
hideBackdrop(el);
587581
});
588582
}
589583

590-
const downloadPdf = async (options, fileName) => {
584+
const downloadPdf = (options, fileName) => {
591585
if (fileName === null) {
592586
fileName = "download.pdf";
593587
}
594588

595-
await getPdfUrl(options, url => {
589+
getPdfUrl(options, url => {
596590
const anchorElement = document.createElement('a');
597591
anchorElement.href = url;
598592
anchorElement.download = fileName;
599593
document.body.appendChild(anchorElement);
600594
anchorElement.click();
601595
document.body.removeChild(anchorElement);
602-
603-
return new Promise((resolve, reject) => {
604-
resolve();
605-
});
606596
});
607597
}
608598

@@ -771,7 +761,7 @@ const makeThumb = async page => {
771761
return canvas;
772762
}
773763

774-
const printPdf = async (el, options) => {
764+
const printPdf = (el, options) => {
775765
let iframe = el.querySelector(".bb-view-print-iframe");
776766
if (iframe === null) {
777767
iframe = document.createElement("iframe");
@@ -782,26 +772,37 @@ const printPdf = async (el, options) => {
782772
el.appendChild(iframe);
783773
}
784774

785-
await getPdfUrl(options, url => {
775+
getPdfUrl(options, url => {
786776
iframe.src = url;
787777
iframe.onload = () => {
788778
iframe.contentWindow.focus();
789779
iframe.contentWindow.print();
790780
};
791-
return new Promise((resolve, reject) => {
792-
resolve();
793-
});
794781
});
795782
}
796783

797-
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) => {
798799
if (options.url) {
799800
callback(options.url);
800801
}
801802
else if (options.data) {
802803
const blob = new Blob([options.data], { type: 'application/pdf' });
803804
var url = window.URL.createObjectURL(blob);
804-
await callback(url);
805+
callback(url);
805806
window.URL.revokeObjectURL(url);
806807
}
807808
}

0 commit comments

Comments
 (0)