Skip to content

Commit ed48b6f

Browse files
Tony-ST0754CopilotArgoZhang
authored
feat(EmbedPDF): update embed-pdf-viewer to v3.0.0 (#1279)
* chore(EmbedPDF): update embed-pdf-viewer to v3.0.0 (#1278) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * chore: 更新脚本 * refactor: 更新升级到 v3 版本 * chore: bump version 11.0.0-rc.3 --------- Co-authored-by: Tony-ST0754 <244411587+Tony-ST0754@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Argo Zhang <argo@live.ca>
1 parent 138521e commit ed48b6f

110 files changed

Lines changed: 131597 additions & 87 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/components/BootstrapBlazor.EmbedPDF/BootstrapBlazor.EmbedPDF.csproj‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

3+
<PropertyGroup>
4+
<Version>11.0.0-rc.3</Version>
5+
</PropertyGroup>
36

47
<PropertyGroup>
58
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Pdf Viewer</PackageTags>

‎src/components/BootstrapBlazor.EmbedPDF/EmbedPDF.razor.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace BootstrapBlazor.Components;
1010
[JSModuleAutoLoader("./_content/BootstrapBlazor.EmbedPDF/EmbedPDF.razor.js", JSObjectReference = true)]
1111
public partial class EmbedPDF
1212
{
13+
private const string AssetsBaseUrl = "./_content/BootstrapBlazor.EmbedPDF/";
14+
1315
/// <summary>
1416
/// <para lang="zh">获得/设置 PDF 文档路径</para>
1517
/// <para lang="en">Gets or sets the PDF document URL</para>
@@ -152,6 +154,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
152154
Theme = Theme.ToDescriptionString(),
153155
Src = Url,
154156
Lang = Language,
157+
AssetsUrl = AssetsBaseUrl,
155158
CurrentPage,
156159
ScrollStrategy = ScrollStrategy.ToDescriptionString(),
157160
PageGap

‎src/components/BootstrapBlazor.EmbedPDF/EmbedPDF.razor.js‎

Lines changed: 90 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,115 +9,124 @@ export async function init(id, invoke, options) {
99
return null;
1010
}
1111

12-
const target = el.querySelector('.pdf-viewer');
13-
const { src, tabBar, theme, lang, currentPage, scrollStrategy, pageGap } = options;
14-
const wasmUrl = `${location.origin}/_content/BootstrapBlazor.EmbedPDF/pdfium.wasm`;
15-
16-
let preference = theme;
17-
if (preference === 'system') {
18-
preference = getTheme();
19-
}
20-
21-
let currentPageGap = pageGap;
22-
if (currentPageGap === 0) {
23-
currentPageGap = 20;
24-
}
12+
const { src, tabBar, theme, lang, assetsUrl, currentPage, scrollStrategy, pageGap } = options;
13+
const preference = getViewerTheme(theme);
14+
const currentPageGap = pageGap === 0 ? 20 : pageGap;
15+
const pdf = {
16+
element: null,
17+
viewer: null,
18+
scrollStrategy,
19+
resolveReady: null
20+
};
21+
pdf.ready = new Promise(resolve => {
22+
pdf.resolveReady = resolve;
23+
});
2524

26-
const viewer = EmbedPDF.init({
27-
type: 'container',
25+
const element = EmbedPDF.init({
2826
target: el,
2927
src,
30-
worker: true,
31-
tabBar,
32-
theme: {
33-
preference: preference
28+
locale: lang || 'auto',
29+
theme: preference,
30+
layout: layout => layout.tabs(tabBar),
31+
plugins: {
32+
stage: {
33+
layout: scrollStrategy,
34+
gap: currentPageGap
35+
},
36+
i18n: {
37+
fallbackLocale: 'en'
38+
}
3439
},
35-
wasmUrl,
36-
i18n: {
37-
defaultLocale: lang,
38-
fallbackLocale: 'en'
40+
engine: {
41+
assetsUrl
3942
},
40-
scroll: {
41-
defaultStrategy: scrollStrategy,
42-
defaultPageGap: currentPageGap
43+
onReady: viewer => {
44+
pdf.viewer = viewer;
45+
viewer.documents.onOpened(({ documentId }) => {
46+
const stage = viewer.forDocument(documentId).get('stage');
47+
stage.updateSettings({ layout: pdf.scrollStrategy });
48+
if (currentPage > 0) {
49+
stage.goToPage(currentPage - 1);
50+
}
51+
});
52+
53+
viewer.documents.onActiveChanged(() => {
54+
viewer.tryGet('stage')?.updateSettings({ layout: pdf.scrollStrategy });
55+
});
56+
pdf.resolveReady(viewer);
4357
}
4458
});
4559

60+
pdf.element = element;
61+
Data.set(id, pdf);
62+
4663
registerBootstrapBlazorModule('EmbedPDF', id, () => {
4764
EventHandler.on(document, 'changed.bb.theme', updateTheme);
4865
});
49-
50-
Data.set(id, { el, invoke, options, viewer });
51-
52-
const registry = await viewer.registry;
53-
const scroll = registry.getPlugin('scroll').provides();
54-
55-
if (currentPage !== 0) {
56-
scroll.onLayoutReady((event) => {
57-
scroll.scrollToPage({
58-
pageNumber: currentPage,
59-
behavior: 'instant'
60-
});
61-
});
62-
}
66+
await pdf.ready;
6367
}
6468

6569
export async function setUrl(id, url, keepCurrentDoc) {
6670
const pdf = Data.get(id);
67-
const { viewer } = pdf;
68-
69-
if (viewer) {
70-
const registry = await viewer.registry;
71-
const docManager = registry.getPlugin('document-manager').provides();
72-
if (!keepCurrentDoc) {
73-
const doc = docManager.getActiveDocument();
74-
if (doc) {
75-
docManager.closeDocument(doc.id).toPromise();
76-
}
71+
if (!pdf) {
72+
return;
73+
}
74+
75+
const viewer = await pdf.ready;
76+
if (!viewer || Data.get(id) !== pdf) {
77+
return;
78+
}
79+
80+
if (!keepCurrentDoc) {
81+
const activeDocumentId = viewer.documents.getActiveId();
82+
if (activeDocumentId) {
83+
await viewer.documents.close(activeDocumentId);
7784
}
78-
const name = getFileName(url);
79-
docManager.openDocumentUrl({
80-
url,
81-
name,
82-
autoActivate: true
83-
}).toPromise();
8485
}
86+
87+
if (!url || Data.get(id) !== pdf) {
88+
return;
89+
}
90+
91+
await viewer.documents.open(
92+
{ kind: 'url', url },
93+
{ name: getFileName(url) }
94+
);
8595
}
8696

8797
export function setTheme(id, theme) {
8898
const pdf = Data.get(id);
89-
const { viewer } = pdf;
90-
91-
if (viewer) {
92-
viewer.setTheme(theme);
99+
if (pdf?.element) {
100+
pdf.element.theme = getViewerTheme(theme);
93101
}
94102
}
95103

96-
export async function setLocale(id, locale) {
104+
export function setLocale(id, locale) {
97105
const pdf = Data.get(id);
98-
const { viewer } = pdf;
99-
100-
if (viewer) {
101-
const registry = await viewer.registry;
102-
const i18n = registry.getPlugin('i18n').provides();
103-
i18n.setLocale(locale);
106+
if (pdf?.element) {
107+
pdf.element.locale = locale;
104108
}
105109
}
106110

107-
export async function setScrollStrategy(id, strategy) {
111+
export function setScrollStrategy(id, strategy) {
108112
const pdf = Data.get(id);
109-
const { viewer } = pdf;
110-
111-
if (viewer) {
112-
const registry = await viewer.registry;
113-
const scroll = registry.getPlugin('scroll').provides();
114-
scroll.setScrollStrategy(strategy);
113+
if (pdf) {
114+
pdf.scrollStrategy = strategy;
115+
const stage = pdf.viewer?.tryGet('stage');
116+
if (stage) {
117+
stage.updateSettings({ layout: strategy });
118+
}
115119
}
116120
}
117121

118122
export function dispose(id) {
119123
const pdf = Data.get(id);
120124
Data.remove(id);
125+
pdf?.resolveReady(null);
126+
127+
if (pdf?.element) {
128+
pdf.element.remove();
129+
}
121130

122131
const { EmbedPDF } = window.BootstrapBlazor;
123132
if (EmbedPDF) {
@@ -134,11 +143,8 @@ const updateTheme = e => {
134143
const id = s.getAttribute('id');
135144
if (id) {
136145
const pdf = Data.get(id);
137-
if (pdf) {
138-
const { viewer } = pdf;
139-
if (viewer) {
140-
viewer.setTheme(theme);
141-
}
146+
if (pdf?.element) {
147+
pdf.element.theme = getViewerTheme(theme);
142148
}
143149
}
144150
});
@@ -151,3 +157,8 @@ function getFileName(filePath) {
151157
);
152158
return filePath.substring(lastSeparatorIndex + 1);
153159
}
160+
161+
function getViewerTheme(theme) {
162+
const preference = theme === 'system' ? getTheme() : theme;
163+
return preference === 'auto' ? 'system' : preference;
164+
}

‎src/components/BootstrapBlazor.EmbedPDF/wwwroot/browser-BKLM0ThC-BCLQGPD6.js‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎src/components/BootstrapBlazor.EmbedPDF/wwwroot/chunks/allura-kQfqxOOE.js‎

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)