Skip to content

Commit c20963a

Browse files
authored
feat(PdfViewer): add OnLoaded parameter (#452)
* feat: 增加 OnLoaded 回调方法 * chore: bump version 9.0.0 * refactor: 增加平台支持逻辑 * refactor: 重构逻辑 * refactor: 增加异步逻辑
1 parent 61b5230 commit c20963a

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

src/components/BootstrapBlazor.PdfViewer/BootstrapBlazor.PdfViewer.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>9.0.0-beta01</Version>
4+
<Version>9.0.0</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ public partial class PdfViewer
2323
[Parameter]
2424
public string? Height { get; set; }
2525

26+
/// <summary>
27+
/// Gets or sets the document loaded event callback.
28+
/// </summary>
29+
[Parameter]
30+
public Func<Task>? OnLoaded { get; set; }
31+
32+
/// <summary>
33+
/// Gets or sets the document loaded event callback.
34+
/// </summary>
35+
[Parameter]
36+
public Func<Task>? NotSupportCallback { get; set; }
37+
2638
private string? ClassString => CssBuilder.Default("bb-pdf-viewer-container")
2739
.AddClassFromAttributes(AdditionalAttributes)
2840
.Build();
@@ -58,5 +70,35 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5870
/// <inheritdoc/>
5971
/// </summary>
6072
/// <returns></returns>
61-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id);
73+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
74+
{
75+
LoadedCallaback = nameof(TriggerOnLoaded),
76+
NotSupportCallback = nameof(TriggerNotSupportCallback)
77+
});
78+
79+
/// <summary>
80+
/// Trigger OnLoaded callback when the PDF document is loaded.
81+
/// </summary>
82+
/// <returns></returns>
83+
[JSInvokable]
84+
public async Task TriggerOnLoaded()
85+
{
86+
if (OnLoaded != null)
87+
{
88+
await OnLoaded();
89+
}
90+
}
91+
92+
/// <summary>
93+
/// Trigger NotSupportCallback when the PDF viewer does not support the document.
94+
/// </summary>
95+
/// <returns></returns>
96+
[JSInvokable]
97+
public async Task TriggerNotSupportCallback()
98+
{
99+
if (NotSupportCallback != null)
100+
{
101+
await NotSupportCallback();
102+
}
103+
}
62104
}

src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
import { addLink } from "../BootstrapBlazor/modules/utility.js"
22
import Data from "../BootstrapBlazor/modules/data.js"
33

4-
export async function init(id) {
4+
export async function init(id, invoke, options) {
55
await addLink("./_content/BootstrapBlazor.PdfViewer/pdf-viewer.css");
66

77
const el = document.getElementById(id);
8-
const pdfViewer = { el };
8+
const pdfViewer = { el, invoke, options };
99
Data.set(id, pdfViewer);
1010

1111
const url = el.getAttribute('data-bb-url');
12-
loadPdf(id, url);
12+
await loadPdf(id, url);
1313
}
1414

15-
export function loadPdf(id, url) {
15+
export async function loadPdf(id, url) {
1616
const pdfViewer = Data.get(id);
17-
const { el } = pdfViewer;
17+
const { el, invoke, options } = pdfViewer;
18+
19+
if (!navigator.pdfViewerEnabled) {
20+
await invoke.invokeMethodAsync(options.notSupportCallback);
21+
return;
22+
}
23+
1824
if (url) {
1925
const { frame } = pdfViewer;
2026
const viewer = frame || createFrame(el);
27+
if (options.loadedCallaback) {
28+
viewer.onload = () => {
29+
invoke.invokeMethodAsync(options.loadedCallaback);
30+
};
31+
}
2132
viewer.src = url;
2233
}
2334
else {

0 commit comments

Comments
 (0)