Skip to content

Commit f0e73cc

Browse files
authored
feat(PdfReader): simplify code improve performace (#821)
* refactor: 修复给常量赋值错误 * refactor: 重置流长度变量 * refactor: 移除 createObjectURL 方法精简代码逻辑 * refactor: 重构 url data 变量逻辑 * chore: bump version 10.0.15
1 parent bbb0d8f commit f0e73cc

3 files changed

Lines changed: 11 additions & 31 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.14</Version>
4+
<Version>10.0.15</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
200200
{
201201
_url = Url;
202202
_lastStreamHash = string.Empty;
203+
_lastStreamLength = 0;
203204
await InvokeVoidAsync("setUrl", Id, _url);
204205
}
205206
if (_currentPage != CurrentPage)

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

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export async function setUrl(id, url) {
3636

3737
const { options } = pdf;
3838
options.url = url;
39+
options.data = null;
3940
await loadPdf(pdf);
4041
}
4142

@@ -47,31 +48,12 @@ export async function setData(id, data) {
4748
return;
4849
}
4950

50-
const objectUrl = createObjectURLFromByte(data);
51-
pdf.objectUrl = objectUrl;
52-
5351
const { options } = pdf;
54-
options.url = objectUrl;
55-
options.data = null;
52+
options.url = null;
53+
options.data = data;;
5654
await loadPdf(pdf);
5755
}
5856

59-
const createObjectURLFromBase64 = base64Data => {
60-
const binaryString = atob(base64Data);
61-
const bytes = new Uint8Array(binaryString.length);
62-
for (let i = 0; i < binaryString.length; i++) {
63-
bytes[i] = binaryString.charCodeAt(i);
64-
}
65-
66-
const blob = new Blob([bytes], { type: 'application/pdf' });
67-
return URL.createObjectURL(blob);
68-
}
69-
70-
const createObjectURLFromByte = bytes => {
71-
const blob = new Blob([bytes], { type: 'application/pdf' });
72-
return URL.createObjectURL(blob);
73-
}
74-
7557
export function setScaleValue(id, value) {
7658
const { pdfViewer } = Data.get(id);
7759
if (pdfViewer) {
@@ -139,10 +121,7 @@ const loadPdf = async pdf => {
139121

140122
if (bar) {
141123
const val = loaded / total * 100;
142-
if (val > 100) {
143-
val = 100;
144-
}
145-
bar.style.setProperty('--bb-view-progress-val', `${val}%`);
124+
bar.style.setProperty('--bb-view-progress-val', `${Math.min(val, 100)}%`);
146125

147126
if (progressHandler === null) {
148127
progressHandler = setTimeout(() => {
@@ -185,10 +164,7 @@ const loadPdf = async pdf => {
185164
}
186165

187166
const disposePdf = pdf => {
188-
const { el, observer, loadingTask, objectUrl } = pdf;
189-
if (objectUrl) {
190-
URL.revokeObjectURL(objectUrl);
191-
}
167+
const { el, observer, loadingTask } = pdf;
192168

193169
if (observer) {
194170
observer.disconnect();
@@ -203,7 +179,10 @@ const disposePdf = pdf => {
203179
const viewContainer = el.querySelector(".bb-view-container");
204180
if (viewContainer) {
205181
[...viewContainer.children].forEach(i => {
206-
if (!i.classList.contains("pdfViewer")) {
182+
if (i.classList.contains("pdfViewer")) {
183+
i.innerHTML = "";
184+
}
185+
else {
207186
i.remove();
208187
}
209188
})

0 commit comments

Comments
 (0)