diff --git a/src/components/BootstrapBlazor.PdfViewer/BootstrapBlazor.PdfViewer.csproj b/src/components/BootstrapBlazor.PdfViewer/BootstrapBlazor.PdfViewer.csproj index 6c69df1e..7e568d3d 100644 --- a/src/components/BootstrapBlazor.PdfViewer/BootstrapBlazor.PdfViewer.csproj +++ b/src/components/BootstrapBlazor.PdfViewer/BootstrapBlazor.PdfViewer.csproj @@ -1,7 +1,7 @@ - 9.0.0-beta01 + 9.0.0 diff --git a/src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs b/src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs index d80548eb..febcb9f1 100644 --- a/src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs +++ b/src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs @@ -23,6 +23,18 @@ public partial class PdfViewer [Parameter] public string? Height { get; set; } + /// + /// Gets or sets the document loaded event callback. + /// + [Parameter] + public Func? OnLoaded { get; set; } + + /// + /// Gets or sets the document loaded event callback. + /// + [Parameter] + public Func? NotSupportCallback { get; set; } + private string? ClassString => CssBuilder.Default("bb-pdf-viewer-container") .AddClassFromAttributes(AdditionalAttributes) .Build(); @@ -58,5 +70,35 @@ protected override async Task OnAfterRenderAsync(bool firstRender) /// /// /// - protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id); + protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new + { + LoadedCallaback = nameof(TriggerOnLoaded), + NotSupportCallback = nameof(TriggerNotSupportCallback) + }); + + /// + /// Trigger OnLoaded callback when the PDF document is loaded. + /// + /// + [JSInvokable] + public async Task TriggerOnLoaded() + { + if (OnLoaded != null) + { + await OnLoaded(); + } + } + + /// + /// Trigger NotSupportCallback when the PDF viewer does not support the document. + /// + /// + [JSInvokable] + public async Task TriggerNotSupportCallback() + { + if (NotSupportCallback != null) + { + await NotSupportCallback(); + } + } } diff --git a/src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.js b/src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.js index d043fd1c..f29350f7 100644 --- a/src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.js +++ b/src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.js @@ -1,23 +1,34 @@ import { addLink } from "../BootstrapBlazor/modules/utility.js" import Data from "../BootstrapBlazor/modules/data.js" -export async function init(id) { +export async function init(id, invoke, options) { await addLink("./_content/BootstrapBlazor.PdfViewer/pdf-viewer.css"); const el = document.getElementById(id); - const pdfViewer = { el }; + const pdfViewer = { el, invoke, options }; Data.set(id, pdfViewer); const url = el.getAttribute('data-bb-url'); - loadPdf(id, url); + await loadPdf(id, url); } -export function loadPdf(id, url) { +export async function loadPdf(id, url) { const pdfViewer = Data.get(id); - const { el } = pdfViewer; + const { el, invoke, options } = pdfViewer; + + if (!navigator.pdfViewerEnabled) { + await invoke.invokeMethodAsync(options.notSupportCallback); + return; + } + if (url) { const { frame } = pdfViewer; const viewer = frame || createFrame(el); + if (options.loadedCallaback) { + viewer.onload = () => { + invoke.invokeMethodAsync(options.loadedCallaback); + }; + } viewer.src = url; } else {