-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(PdfViewer): add PdfViewer component #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
|
||
| <PropertyGroup> | ||
| <Version>9.0.0-beta01</Version> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Pdf Viewer</PackageTags> | ||
| <Description>Bootstrap UI components extensions of Pdf Viewer</Description> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="BootstrapBlazor.Components" /> | ||
| <Using Include="Microsoft.JSInterop" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| @namespace BootstrapBlazor.Components | ||
| @inherits BootstrapModuleComponentBase | ||
| @attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.PdfViewer/PdfViewer.razor.js", JSObjectReference = true, AutoInvokeDispose = false)] | ||
|
|
||
| <div @attributes="AdditionalAttributes" id="@Id" data-bb-url="@Url" class="@ClassString" style="@StyleString"></div> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright (c) Argo Zhang (argo@163.com). All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
| // Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
|
||
| using Microsoft.AspNetCore.Components; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// PdfViewer component for displaying PDF files in a Blazor application. | ||
| /// </summary> | ||
| public partial class PdfViewer | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the url for the PDF file to be displayed. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string? Url { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the viewer height. Default is null. | ||
| /// </summary> | ||
| [Parameter] | ||
| public string? Height { get; set; } | ||
|
|
||
| private string? ClassString => CssBuilder.Default("bb-pdf-viewer-container") | ||
| .AddClassFromAttributes(AdditionalAttributes) | ||
| .Build(); | ||
|
|
||
| private string? StyleString => CssBuilder.Default() | ||
| .AddClass($"--bb-pdf-viewer-height: {Height};", !string.IsNullOrEmpty(Height)) | ||
| .Build(); | ||
|
|
||
| private string? _url; | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <param name="firstRender"></param> | ||
| /// <returns></returns> | ||
| protected override async Task OnAfterRenderAsync(bool firstRender) | ||
| { | ||
| await base.OnAfterRenderAsync(firstRender); | ||
|
|
||
| if (firstRender) | ||
| { | ||
| _url = Url; | ||
| } | ||
|
|
||
| if (_url != Url) | ||
| { | ||
| _url = Url; | ||
| await InvokeVoidAsync("loadPdf", Id, _url); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Implement disposal to clean up resources Override Suggested implementation: /// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id);
/// <inheritdoc/>
public override async ValueTask DisposeAsync()
{
// Remove the injected CSS link
await InvokeVoidAsync("removePdfViewerCss", Id);
// Clear the container
await InvokeVoidAsync("clearPdfViewerContainer", Id);
// Remove the entry from Data to prevent memory leaks
Data.Remove(Id);
await base.DisposeAsync();
}
}
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { addLink } from "../BootstrapBlazor/modules/utility.js" | ||
| import Data from "../BootstrapBlazor/modules/data.js" | ||
|
|
||
| export async function init(id) { | ||
| await addLink("./_content/BootstrapBlazor.PdfViewer/pdf-viewer.css"); | ||
|
|
||
| const el = document.getElementById(id); | ||
| const pdfViewer = { el }; | ||
| Data.set(id, pdfViewer); | ||
|
|
||
| const url = el.getAttribute('data-bb-url'); | ||
| loadPdf(id, url); | ||
| } | ||
|
|
||
| export function loadPdf(id, url) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Guard against missing pdfViewer in Data Check if |
||
| const pdfViewer = Data.get(id); | ||
| const { el } = pdfViewer; | ||
| if (url) { | ||
| const { frame } = pdfViewer; | ||
| const viewer = frame || createFrame(el); | ||
| viewer.src = url; | ||
|
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Cache the iframe reference in pdfViewer Assign the new iframe to |
||
| } | ||
| else { | ||
| delete pdfViewer.frame; | ||
| el.innerHTML = ''; | ||
| } | ||
| } | ||
|
|
||
| const createFrame = el => { | ||
| const frame = document.createElement('iframe'); | ||
| frame.classList.add('bb-pdf-viewer'); | ||
| el.appendChild(frame); | ||
| return frame; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| @using BootstrapBlazor.Components; | ||
| @using Microsoft.AspNetCore.Components.Web |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| .bb-pdf-viewer-container { | ||
| width: 100%; | ||
| height: var(--bb-pdf-viewer-height, 500px); | ||
| } | ||
|
|
||
| .bb-pdf-viewer { | ||
| width: 100%; | ||
| height: 100%; | ||
|
Comment on lines
+6
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider adding This will prevent default borders from appearing in some browsers, ensuring a consistent, borderless PDF viewer. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Merge user-defined inline styles with component-generated styles
Extract the
styleattribute from AdditionalAttributes and merge it with StyleString to ensure user-defined styles are preserved.