|
| 1 | +using Microsoft.AspNetCore.Components; |
| 2 | +using System.Globalization; |
| 3 | + |
| 4 | +namespace BootstrapBlazor.Components; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// <para lang="zh">EmbedPDF 组件</para> |
| 8 | +/// <para lang="en">EmbedPDF component</para> |
| 9 | +/// </summary> |
| 10 | +[JSModuleAutoLoader("./_content/BootstrapBlazor.EmbedPDF/EmbedPDF.razor.js", JSObjectReference = true)] |
| 11 | +public partial class EmbedPDF |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// <para lang="zh">获得/设置 PDF 文档路径</para> |
| 15 | + /// <para lang="en">Gets or sets the PDF document URL</para> |
| 16 | + /// </summary> |
| 17 | + [Parameter] |
| 18 | + public string? Url { get; set; } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// <para lang="zh">获得/设置 PDF 组件高度 默认 600px</para> |
| 22 | + /// <para lang="en">Gets or sets the PDF component height, default is 600px</para> |
| 23 | + /// </summary> |
| 24 | + [Parameter] |
| 25 | + public string ViewHeight { get; set; } = "600px"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// <para lang="zh">获得/设置 是否显示外边框</para> |
| 29 | + /// <para lang="en">Gets or sets a value indicating whether to show border</para> |
| 30 | + /// </summary> |
| 31 | + [Parameter] |
| 32 | + public bool ShowBorder { get; set; } = true; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// <para lang="zh">获得/设置 标签显示模式 默认值 <see cref="EmbedPDFTabBarMode.Always"/></para> |
| 36 | + /// <para lang="en">Gets or sets the tab bar display mode, default is <see cref="EmbedPDFTabBarMode.Always"/></para> |
| 37 | + /// </summary> |
| 38 | + [Parameter] |
| 39 | + public EmbedPDFTabBarMode TabBarMode { get; set; } = EmbedPDFTabBarMode.Always; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// <para lang="zh">获得/设置 滚动策略 默认值 <see cref="EmbedPDFScrollStrategy.Vertical"/></para> |
| 43 | + /// <para lang="en">Gets or sets the scroll strategy, default is <see cref="EmbedPDFScrollStrategy.Vertical"/></para> |
| 44 | + /// </summary> |
| 45 | + [Parameter] |
| 46 | + public EmbedPDFScrollStrategy ScrollStrategy { get; set; } = EmbedPDFScrollStrategy.Vertical; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// <para lang="zh">获得/设置 主题样式 默认 <see cref="EmbedPDFTheme.System"/></para> |
| 50 | + /// <para lang="en">Gets or sets the theme style, default is <see cref="EmbedPDFTheme.System"/></para> |
| 51 | + /// </summary> |
| 52 | + [Parameter] |
| 53 | + public EmbedPDFTheme Theme { get; set; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// <para lang="zh">获得/设置 语言 默认 null 未设置</para> |
| 57 | + /// <para lang="en">Gets or sets the language, default is null</para> |
| 58 | + /// </summary> |
| 59 | + [Parameter] |
| 60 | + public string? Language { get; set; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// <para lang="zh">获得/设置 当前页码</para> |
| 64 | + /// <para lang="en">Gets or sets the current page number</para> |
| 65 | + /// </summary> |
| 66 | + [Parameter] |
| 67 | + public uint CurrentPage { get; set; } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// <para lang="zh">获得/设置 页码之间的间隙 默认 null 未设置 使用默认值 20</para> |
| 71 | + /// <para lang="en">Gets or sets the gap between pages, default is null and uses default value 20</para> |
| 72 | + /// </summary> |
| 73 | + [Parameter] |
| 74 | + public uint PageGap { get; set; } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// <para lang="zh">获得/设置 重新设置 Url 时是否保持当前打开文档 默认 false</para> |
| 78 | + /// <para lang="en">Gets or sets a value indicating whether to keep the current opened document when resetting the URL, default is false</para> |
| 79 | + /// </summary> |
| 80 | + [Parameter] |
| 81 | + public uint IsKeepCurrentDocument { get; set; } |
| 82 | + |
| 83 | + private string? StyleString => CssBuilder.Default() |
| 84 | + .AddClass("border: 1px solid var(--bs-border-color); border-radius: var(--bs-border-radius); overflow: hidden;", ShowBorder) |
| 85 | + .AddClass($"height: {ViewHeight};", !string.IsNullOrEmpty(ViewHeight)) |
| 86 | + .AddStyleFromAttributes(AdditionalAttributes) |
| 87 | + .Build(); |
| 88 | + |
| 89 | + private string? _url; |
| 90 | + private EmbedPDFTheme _theme; |
| 91 | + private string? _language; |
| 92 | + private EmbedPDFScrollStrategy _strategy = EmbedPDFScrollStrategy.Vertical; |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// <inheritdoc/> |
| 96 | + /// </summary> |
| 97 | + protected override void OnParametersSet() |
| 98 | + { |
| 99 | + base.OnParametersSet(); |
| 100 | + |
| 101 | + if (string.IsNullOrEmpty(Language)) |
| 102 | + { |
| 103 | + Language = CultureInfo.CurrentUICulture.Name; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// <inheritdoc/> |
| 109 | + /// </summary> |
| 110 | + /// <param name="firstRender"></param> |
| 111 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 112 | + { |
| 113 | + await base.OnAfterRenderAsync(firstRender); |
| 114 | + |
| 115 | + if (firstRender) |
| 116 | + { |
| 117 | + _url = Url; |
| 118 | + _language = Language; |
| 119 | + _theme = Theme; |
| 120 | + _strategy = ScrollStrategy; |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if (_url != Url) |
| 125 | + { |
| 126 | + _url = Url; |
| 127 | + await InvokeVoidAsync("setUrl", Id, _url, IsKeepCurrentDocument); |
| 128 | + } |
| 129 | + if (_theme != Theme) |
| 130 | + { |
| 131 | + _theme = Theme; |
| 132 | + await InvokeVoidAsync("setTheme", Id, _theme.ToDescriptionString()); |
| 133 | + } |
| 134 | + if (_language != Language) |
| 135 | + { |
| 136 | + _language = Language; |
| 137 | + await InvokeVoidAsync("setLocale", Id, _language); |
| 138 | + } |
| 139 | + if (_strategy != ScrollStrategy) |
| 140 | + { |
| 141 | + _strategy = ScrollStrategy; |
| 142 | + await InvokeVoidAsync("setScrollStrategy", Id, _strategy.ToDescriptionString()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// <inheritdoc/> |
| 148 | + /// </summary> |
| 149 | + protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new |
| 150 | + { |
| 151 | + TabBar = TabBarMode.ToDescriptionString(), |
| 152 | + Theme = Theme.ToDescriptionString(), |
| 153 | + Src = Url, |
| 154 | + Lang = Language, |
| 155 | + CurrentPage, |
| 156 | + ScrollStrategy = ScrollStrategy.ToDescriptionString(), |
| 157 | + PageGap |
| 158 | + }); |
| 159 | +} |
0 commit comments