Skip to content

Commit e5e2a31

Browse files
committed
feat: 增加 OnLoaded 回调方法
1 parent 61b5230 commit e5e2a31

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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

7+
if (!navigator.pdfViewerEnabled) {
8+
await invoke.invokeMethodAsync(options.notSupportCallback);
9+
}
10+
711
const el = document.getElementById(id);
8-
const pdfViewer = { el };
12+
const pdfViewer = { el, invoke, options };
913
Data.set(id, pdfViewer);
1014

1115
const url = el.getAttribute('data-bb-url');
@@ -16,8 +20,13 @@ export function loadPdf(id, url) {
1620
const pdfViewer = Data.get(id);
1721
const { el } = pdfViewer;
1822
if (url) {
19-
const { frame } = pdfViewer;
23+
const { frame, invoke, options } = pdfViewer;
2024
const viewer = frame || createFrame(el);
25+
if (options.loadedCallaback) {
26+
viewer.onload = () => {
27+
invoke.invokeMethodAsync(options.loadedCallaback);
28+
};
29+
}
2130
viewer.src = url;
2231
}
2332
else {

0 commit comments

Comments
 (0)