Skip to content

Commit c93c1a2

Browse files
committed
refactor: 增加 loadPdf 方法
1 parent 2ab5086 commit c93c1a2

1 file changed

Lines changed: 40 additions & 25 deletions

File tree

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

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,8 @@ export async function init(id, invoke, options) {
2020
return;
2121
}
2222

23-
const loadingTask = pdfjsLib.getDocument(options);
24-
loadingTask.onProgress = function (progressData) {
25-
26-
};
27-
28-
loadingTask.onPassword = function (updatePassword, reason) {
29-
if (reason === pdfjsLib.PasswordResponses.NEED_PASSWORD) {
30-
31-
}
32-
else if (reason === pdfjsLib.PasswordResponses.INCORRECT_PASSWORD) {
33-
34-
}
35-
};
36-
37-
const container = el.querySelector(".bb-view-container");
38-
const eventBus = new pdfjsViewer.EventBus();
39-
const pdfViewer = new pdfjsViewer.PDFViewer({
40-
container,
41-
eventBus
42-
});
43-
44-
addEventListener(el, pdfViewer, eventBus, invoke, options);
45-
46-
const pdfDocument = await loadingTask.promise;
47-
pdfViewer.setDocument(pdfDocument);
23+
const pdfViewer = await loadPdf(el, invoke, options);
24+
setObserver(el);
4825

4926
Data.set(id, { el, pdfViewer });
5027
}
@@ -86,6 +63,44 @@ export function resetThumbnails(id) {
8663
}
8764
}
8865

66+
const loadPdf = async (el, invoke, options) => {
67+
const loadingTask = pdfjsLib.getDocument(options);
68+
loadingTask.onProgress = function (progressData) {
69+
70+
};
71+
72+
loadingTask.onPassword = function (updatePassword, reason) {
73+
if (reason === pdfjsLib.PasswordResponses.NEED_PASSWORD) {
74+
75+
}
76+
else if (reason === pdfjsLib.PasswordResponses.INCORRECT_PASSWORD) {
77+
78+
}
79+
};
80+
81+
const container = el.querySelector(".bb-view-container");
82+
const eventBus = new pdfjsViewer.EventBus();
83+
const pdfViewer = new pdfjsViewer.PDFViewer({
84+
container,
85+
eventBus
86+
});
87+
88+
addEventListener(el, pdfViewer, eventBus, invoke, options);
89+
90+
const pdfDocument = await loadingTask.promise;
91+
pdfViewer.setDocument(pdfDocument);
92+
93+
return pdfViewer;
94+
}
95+
96+
const setObserver = el => {
97+
const observer = new ResizeObserver(entries => {
98+
console.log(entries);
99+
});
100+
101+
observer.observe(el);
102+
}
103+
89104
const addEventListener = (el, pdfViewer, eventBus, invoke, options) => {
90105
eventBus.on("pagesinit", async () => {
91106
if (options.fitMode) {

0 commit comments

Comments
 (0)