-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat(PdfViewer): add ShowToolbar parameter #913
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 | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||
| // Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||||||||
| // Copyright (c) Argo Zhang ([email protected]). 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/ | ||||||||
|
|
||||||||
|
|
@@ -7,46 +7,60 @@ | |||||||
| namespace BootstrapBlazor.Components; | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// PdfViewer component for displaying PDF files in a Blazor application. | ||||||||
| /// <para lang="zh">PdfViewer 组件</para> | ||||||||
| /// <para lang="en">PdfViewer component for displaying PDF files in a Blazor application</para> | ||||||||
| /// </summary> | ||||||||
| public partial class PdfViewer | ||||||||
| { | ||||||||
| /// <summary> | ||||||||
| /// Gets or sets the url for the PDF file to be displayed. | ||||||||
| /// <para lang="zh">获得/设置 文档 Url 属性</para> | ||||||||
| /// <para lang="en">Gets or sets the url for the PDF file to be displayed</para> | ||||||||
| /// </summary> | ||||||||
| [Parameter] | ||||||||
| public string? Url { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Gets or sets the page index of the PDF file. | ||||||||
| /// <para lang="zh">获得/设置 页码索引</para> | ||||||||
| /// <para lang="en">Gets or sets the page index of the PDF file</para> | ||||||||
| /// </summary> | ||||||||
| [Parameter] | ||||||||
| public int PageIndex { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Gets or sets the viewer height. Default is null. | ||||||||
| /// <para lang="zh">获得/设置 查看器高度</para> | ||||||||
| /// <para lang="en">Gets or sets the viewer height</para> | ||||||||
| /// </summary> | ||||||||
| [Parameter] | ||||||||
| public string? Height { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Gets or sets the document loaded event callback. | ||||||||
| /// <para lang="zh">获得/设置 文档加载完成回调事件</para> | ||||||||
| /// <para lang="en">Gets or sets the document loaded event callback</para> | ||||||||
| /// </summary> | ||||||||
| [Parameter] | ||||||||
| public Func<Task>? OnLoaded { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Gets or sets the document loaded event callback. | ||||||||
| /// <para lang="zh">获得/设置 文档不支持回调事件</para> | ||||||||
| /// <para lang="en">Gets or sets the document not supported event callback</para> | ||||||||
| /// </summary> | ||||||||
| [Parameter] | ||||||||
| public Func<Task>? NotSupportCallback { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Gets or sets whether to use Google Docs for PDF rendering. Default is false. | ||||||||
| /// <para lang="zh">获得/设置 是否使用 Google Docs 渲染 PDF</para> | ||||||||
| /// <para lang="en">Gets or sets whether to use Google Docs for PDF rendering</para> | ||||||||
| /// </summary> | ||||||||
| [Parameter] | ||||||||
| public bool UseGoogleDocs { get; set; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// <para lang="zh">获得/设置 是否显示工具栏与缩略图侧边栏</para> | ||||||||
| /// <para lang="en">Gets or sets whether to display toolbar and thumbnail sidebar</para> | ||||||||
| /// </summary> | ||||||||
| [Parameter] | ||||||||
| public bool ShowToolbar { get; set; } = true; | ||||||||
|
|
||||||||
| [Inject, NotNull] | ||||||||
| private NavigationManager? NavigationManager { get; set; } | ||||||||
|
|
||||||||
|
|
@@ -64,7 +78,6 @@ public partial class PdfViewer | |||||||
| /// <inheritdoc/> | ||||||||
| /// </summary> | ||||||||
| /// <param name="firstRender"></param> | ||||||||
| /// <returns></returns> | ||||||||
| protected override async Task OnAfterRenderAsync(bool firstRender) | ||||||||
| { | ||||||||
| await base.OnAfterRenderAsync(firstRender); | ||||||||
|
|
@@ -78,7 +91,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender) | |||||||
| /// <summary> | ||||||||
| /// <inheritdoc/> | ||||||||
| /// </summary> | ||||||||
| /// <returns></returns> | ||||||||
| protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new | ||||||||
| { | ||||||||
| LoadedCallaback = nameof(TriggerOnLoaded), | ||||||||
|
|
@@ -94,13 +106,28 @@ private string GetAbsoluteUri(string? url) | |||||||
| } | ||||||||
|
|
||||||||
| var uri = NavigationManager.ToAbsoluteUri(url); | ||||||||
| return $"{uri.AbsoluteUri}#page={PageIndex}"; | ||||||||
| var builder = new UriBuilder(uri.AbsoluteUri); | ||||||||
| builder.Fragment = BuildFragment(builder); | ||||||||
| return builder.Uri.ToString(); | ||||||||
| } | ||||||||
|
|
||||||||
| private string BuildFragment(UriBuilder builder) | ||||||||
| { | ||||||||
| var fragments = new List<string>(); | ||||||||
| if (PageIndex > 0) | ||||||||
| { | ||||||||
| fragments.Add($"page={PageIndex}"); | ||||||||
| } | ||||||||
| if (!ShowToolbar) | ||||||||
| { | ||||||||
| fragments.Add("toolbar=0&navpanes=0"); | ||||||||
|
||||||||
| fragments.Add("toolbar=0&navpanes=0"); | |
| fragments.Add("toolbar=0"); | |
| fragments.Add("navpanes=0"); |
Copilot
AI
Jan 28, 2026
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.
The builder parameter is passed to BuildFragment but never used. If the input URL already contains a fragment (e.g., "file.pdf#existingfragment"), it will be silently overwritten. If this is intentional behavior, the parameter should be removed. If existing fragments should be preserved or merged, the builder.Fragment should be checked and incorporated into the result.
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.
issue (complexity): Consider simplifying the new URI construction logic in
GetAbsoluteUrito avoid the extraUriBuilderandBuildFragmentindirection while still supportingShowToolbarand page index behavior.The new
GetAbsoluteUri/BuildFragmentflow adds indirection without clear benefit. You can keep the newShowToolbarbehavior while simplifying the URI construction and removingUriBuilderand the extra method.A more direct implementation that preserves current behavior (including overwriting any existing fragment and using
"toolbar=0&navpanes=0"as a single unit) could be:This:
UriBuilderallocation andBuildFragmentmethod.List<string>/string.Joinoverhead for at most two conditions, making the control flow easier to follow.