-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(PdfReader): add load progress function #819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,14 +47,12 @@ export async function setData(id, data) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { options, objectUrl } = pdf; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (objectUrl) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| URL.revokeObjectURL(objectUrl); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const objectUrl = createObjectURLFromByte(data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pdf.objectUrl = objectUrl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.url = createObjectURLFromByte(data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { options } = pdf; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.url = objectUrl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options.data = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pdf.objectUrl = options.url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await loadPdf(pdf); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -125,8 +123,34 @@ export function resetThumbnails(id) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const loadPdf = async pdf => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { el, invoke, options } = pdf; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const loadingTask = pdfjsLib.getDocument(options); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const progressEl = el.querySelector('.bb-view-progress'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (progressEl) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| progressEl.classList.add('show'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const bar = el.querySelector('.bb-view-progress-bar'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bar) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bar.style.setProperty('--bb-view-progress-val', '0'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let progressHandler = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| loadingTask.onProgress = function (progressData) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { loaded, total } = progressData; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bar) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const val = loaded / total * 100; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+138
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Handle zero or missing When Suggested implementation: let progressHandler = null;
loadingTask.onProgress = function (progressData) {
const { loaded, total } = progressData;
if (bar) {
// When total is zero or missing, avoid computing a percentage and
// fall back to an indeterminate state by clearing the CSS variable.
if (!total) {
bar.style.removeProperty('--bb-view-progress-val');
return;
}
let val = loaded / total * 100; if (val > 100) {
val = 100;
}
bar.style.setProperty('--bb-view-progress-val', `${val}%`); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (val > 100) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val = 100; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+141
to
+143
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const val = loaded / total * 100; | |
| if (val > 100) { | |
| val = 100; | |
| let val = 0; | |
| if (typeof total === 'number' && total > 0) { | |
| val = loaded / total * 100; | |
| if (val > 100) { | |
| val = 100; | |
| } |
Copilot
AI
Dec 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable 'val' is declared as const on line 141 but is being reassigned on line 143. This will cause a runtime error. Change the declaration from 'const' to 'let' to allow reassignment.
Copilot
AI
Dec 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling clearTimeout inside the timeout callback is unnecessary since the timeout has already fired. This line has no effect and should be removed.
| clearTimeout(progressHandler); |
Copilot
AI
Dec 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The progressEl variable is accessed without a null check, but it was checked for null on line 128. If progressEl is null, this will cause a runtime error. Add a null check before accessing progressEl.
| progressEl.classList.remove('show'); | |
| if (progressEl) { | |
| progressEl.classList.remove('show'); | |
| } |
Copilot
AI
Dec 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition 'progressHandler === null' will only be true once, meaning the timeout to hide the progress bar is set only on the first progress update. Subsequent progress updates won't reset or update this timer. This could cause the progress bar to disappear prematurely while loading is still in progress. Consider clearing the existing timeout before setting a new one, or move this logic to after the loading completes.
| const val = loaded / total * 100; | |
| if (val > 100) { | |
| val = 100; | |
| } | |
| bar.style.setProperty('--bb-view-progress-val', `${val}%`); | |
| if (progressHandler === null) { | |
| progressHandler = setTimeout(() => { | |
| clearTimeout(progressHandler); | |
| progressEl.classList.remove('show'); | |
| }, 300); | |
| } | |
| let val = loaded / total * 100; | |
| if (val > 100) { | |
| val = 100; | |
| } | |
| bar.style.setProperty('--bb-view-progress-val', `${val}%`); | |
| // Always clear and reset the timeout on each progress update | |
| if (progressHandler !== null) { | |
| clearTimeout(progressHandler); | |
| } | |
| progressHandler = setTimeout(() => { | |
| progressHandler = null; | |
| if (progressEl) { | |
| progressEl.classList.remove('show'); | |
| } | |
| }, 300); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,25 @@ | |||||
| height: calc(var(--bb-pdf-view-height) + var(--bb-pdf-toolbar-height)); | ||||||
| } | ||||||
|
|
||||||
| .bb-view-progress { | ||||||
| position: absolute; | ||||||
| top: 0; | ||||||
| left: 0; | ||||||
| right: 0; | ||||||
| height: 0.25rem; | ||||||
| } | ||||||
|
|
||||||
| .bb-view-progress:not(.show) { | ||||||
| display: none; | ||||||
| } | ||||||
|
|
||||||
| .bb-view-progress-bar { | ||||||
| height: 100%; | ||||||
| background-color: var(--bs-primary); | ||||||
| width: var(--bb-view-progress-val, 0); | ||||||
| transition: width .3s linear; | ||||||
|
||||||
| transition: width .3s linear; | |
| transition: width 0.3s linear; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous objectUrl is no longer being revoked before creating a new one in the setData function. This can lead to memory leaks if setData is called multiple times, as the old blob URLs will remain in memory. Consider revoking the old objectUrl before assigning the new one.