Skip to content

Commit a1b726b

Browse files
committed
feat(EmbedPDF): add Theme parameter
1 parent 48ce4c6 commit a1b726b

5 files changed

Lines changed: 179 additions & 7 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@namespace BootstrapBlazor.Components
22
@inherits BootstrapModuleComponentBase
33

4-
<div @attributes="@AdditionalAttributes" id="@Id" style="@StyleString"></div>
4+
<div @attributes="@AdditionalAttributes" id="@Id" class="bb-embed-pdf" style="@StyleString"></div>

src/components/BootstrapBlazor.EmbedPDF/EmbedPDF.razor.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,69 @@ public partial class EmbedPDF
2020
[Parameter]
2121
public string ViewHeight { get; set; } = "600px";
2222

23+
/// <summary>
24+
/// 获得/设置 标签显示模式 默认值 <see cref="EmbedPDFTabBarMode.Always"/>
25+
/// </summary>
26+
[Parameter]
27+
public EmbedPDFTabBarMode TabBarMode { get; set; } = EmbedPDFTabBarMode.Always;
28+
2329
/// <summary>
2430
/// 获得/设置 是否显示外边框
2531
/// </summary>
2632
[Parameter]
2733
public bool ShowBorder { get; set; } = true;
2834

35+
/// <summary>
36+
/// 获得/设置 主题样式 默认 <see cref="EmbedPDFTheme.System"/>
37+
/// </summary>
38+
[Parameter]
39+
public EmbedPDFTheme Theme { get; set; }
40+
2941
private string? StyleString => CssBuilder.Default()
3042
.AddClass("border: 1px solid var(--bs-border-color); border-radius: var(--bs-border-radius); overflow: hidden;", ShowBorder)
3143
.AddClass($"height: {ViewHeight};", !string.IsNullOrEmpty(ViewHeight))
3244
.AddStyleFromAttributes(AdditionalAttributes)
3345
.Build();
46+
47+
private string? _url;
48+
private EmbedPDFTheme _theme;
49+
50+
/// <summary>
51+
/// <inheritdoc/>
52+
/// </summary>
53+
/// <param name="firstRender"></param>
54+
/// <returns></returns>
55+
protected override async Task OnAfterRenderAsync(bool firstRender)
56+
{
57+
await base.OnAfterRenderAsync(firstRender);
58+
59+
if (firstRender)
60+
{
61+
_url = Url;
62+
_theme = Theme;
63+
return;
64+
}
65+
66+
if (_url != Url)
67+
{
68+
_url = Url;
69+
await InvokeVoidAsync("setUrl", Id, _url);
70+
}
71+
if (_theme != Theme)
72+
{
73+
_theme = Theme;
74+
await InvokeVoidAsync("setTheme", Id, _theme.ToDescriptionString());
75+
}
76+
}
77+
78+
/// <summary>
79+
/// <inheritdoc/>
80+
/// </summary>
81+
/// <returns></returns>
82+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
83+
{
84+
TabBar = TabBarMode.ToDescriptionString(),
85+
Theme = Theme.ToDescriptionString(),
86+
Src = Url
87+
});
3488
}
Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,79 @@
1-
import EmbedPDF from './embedpdf.js';
1+
import Data from '../BootstrapBlazor/modules/data.js';
2+
import EventHandler from "../BootstrapBlazor/modules/event-handler.js"
3+
import { default as EmbedPDF, UIPlugin } from './embedpdf.js';
4+
import { getTheme, registerBootstrapBlazorModule } from '../BootstrapBlazor/modules/utility.js'
25

3-
export function init(id, invoke) {
6+
export function init(id, invoke, options) {
47
const el = document.getElementById(id);
58
if (el === null) {
69
return null;
710
}
811

912
const target = el.querySelector('.pdf-viewer');
10-
const src = './samples/sample.pdf';
13+
const { src, tabBar, theme } = options;
1114
const wasmUrl = `${location.origin}/_content/BootstrapBlazor.EmbedPDF/pdfium.wasm`;
12-
EmbedPDF.init({
15+
16+
let preference = theme;
17+
if (preference === 'system') {
18+
preference = getTheme();
19+
}
20+
21+
const viewer = EmbedPDF.init({
1322
type: 'container',
1423
target: el,
1524
src,
1625
worker: true,
17-
tabBar: 'always',
26+
tabBar,
1827
theme: {
19-
preference: 'system'
28+
preference: preference
2029
},
2130
wasmUrl
2231
});
32+
33+
registerBootstrapBlazorModule('EmbedPDF', id, () => {
34+
EventHandler.on(document, 'changed.bb.theme', updateTheme);
35+
});
36+
37+
Data.set(id, { el, invoke, options, viewer });
38+
}
39+
40+
export function setUrl(id, url) {
41+
42+
}
43+
44+
export function setTheme(id, theme) {
45+
const pdf = Data.get(id);
46+
const { viewer } = pdf;
47+
if (viewer) {
48+
viewer.setTheme(theme);
49+
}
50+
}
51+
52+
export function dispose(id) {
53+
const pdf = Data.get(id);
54+
Data.remove(id);
55+
56+
const { EmbedPDF } = window.BootstrapBlazor;
57+
if (EmbedPDF) {
58+
EmbedPDF.dispose(id, () => {
59+
EventHandler.off(document, 'changed.bb.theme', updateTheme);
60+
});
61+
}
62+
}
63+
64+
const updateTheme = e => {
65+
const theme = e.theme;
66+
67+
[...document.querySelectorAll('.bb-embed-pdf')].forEach(s => {
68+
const id = s.getAttribute('id');
69+
if (id) {
70+
const pdf = Data.get(id);
71+
if (pdf) {
72+
const { viewer } = pdf;
73+
if (viewer) {
74+
viewer.setTheme(theme);
75+
}
76+
}
77+
}
78+
});
2379
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone
4+
5+
using System.ComponentModel;
6+
7+
namespace BootstrapBlazor.Components;
8+
9+
/// <summary>
10+
/// 标签模式
11+
/// </summary>
12+
public enum EmbedPDFTabBarMode
13+
{
14+
/// <summary>
15+
/// 始终显示
16+
/// </summary>
17+
[Description("always")]
18+
Always,
19+
20+
/// <summary>
21+
/// 多标签模式
22+
/// </summary>
23+
[Description("multiple")]
24+
Multiple,
25+
26+
/// <summary>
27+
/// 不显示
28+
/// </summary>
29+
[Description("never")]
30+
Never
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone
4+
5+
using System.ComponentModel;
6+
7+
namespace BootstrapBlazor.Components;
8+
9+
/// <summary>
10+
/// 主题枚举
11+
/// </summary>
12+
public enum EmbedPDFTheme
13+
{
14+
/// <summary>
15+
/// 跟随系统
16+
/// </summary>
17+
[Description("system")]
18+
System,
19+
20+
/// <summary>
21+
/// 明亮模式
22+
/// </summary>
23+
[Description("light")]
24+
Light,
25+
26+
/// <summary>
27+
/// 暗黑模式
28+
/// </summary>
29+
[Description("dark")]
30+
Dark
31+
}

0 commit comments

Comments
 (0)