Skip to content

Commit 280878f

Browse files
committed
feat: 增加滚动方向等参数
1 parent 4edab32 commit 280878f

3 files changed

Lines changed: 113 additions & 18 deletions

File tree

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,23 @@ public partial class EmbedPDF
2121
[Parameter]
2222
public string ViewHeight { get; set; } = "600px";
2323

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

3036
/// <summary>
31-
/// 获得/设置 是否显示外边框
37+
/// 获得/设置 标签显示模式 默认值 <see cref="EmbedPDFTabBarMode.Always"/>
3238
/// </summary>
3339
[Parameter]
34-
public bool ShowBorder { get; set; } = true;
40+
public EmbedPDFScrollDirection ScrollDirection { get; set; } = EmbedPDFScrollDirection.Vertical;
3541

3642
/// <summary>
3743
/// 获得/设置 主题样式 默认 <see cref="EmbedPDFTheme.System"/>
@@ -45,6 +51,18 @@ public partial class EmbedPDF
4551
[Parameter]
4652
public string? Language { get; set; }
4753

54+
/// <summary>
55+
/// 获得/设置 当前页码
56+
/// </summary>
57+
[Parameter]
58+
public uint CurrentPage { get; set; }
59+
60+
/// <summary>
61+
/// 获得/设置 页码之间的间隙 默认 null 未设置 使用默认值 20
62+
/// </summary>
63+
[Parameter]
64+
public uint PageGap { get; set; }
65+
4866
private string? StyleString => CssBuilder.Default()
4967
.AddClass("border: 1px solid var(--bs-border-color); border-radius: var(--bs-border-radius); overflow: hidden;", ShowBorder)
5068
.AddClass($"height: {ViewHeight};", !string.IsNullOrEmpty(ViewHeight))
@@ -55,6 +73,19 @@ public partial class EmbedPDF
5573
private EmbedPDFTheme _theme;
5674
private string? _language;
5775

76+
/// <summary>
77+
/// <inheritdoc/>
78+
/// </summary>
79+
protected override void OnParametersSet()
80+
{
81+
base.OnParametersSet();
82+
83+
if (string.IsNullOrEmpty(Language))
84+
{
85+
Language = CultureInfo.CurrentUICulture.Name;
86+
}
87+
}
88+
5889
/// <summary>
5990
/// <inheritdoc/>
6091
/// </summary>
@@ -82,6 +113,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
82113
_theme = Theme;
83114
await InvokeVoidAsync("setTheme", Id, _theme.ToDescriptionString());
84115
}
116+
if (_language != Language)
117+
{
118+
_language = Language;
119+
await InvokeVoidAsync("setLocale", Id, _language);
120+
}
85121
}
86122

87123
/// <summary>
@@ -93,6 +129,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
93129
TabBar = TabBarMode.ToDescriptionString(),
94130
Theme = Theme.ToDescriptionString(),
95131
Src = Url,
96-
Lang = Language ?? CultureInfo.CurrentUICulture.Name
132+
Lang = Language,
133+
CurrentPage,
134+
ScrollDirection = ScrollDirection.ToDescriptionString(),
135+
PageGap
97136
});
98137
}

src/components/BootstrapBlazor.EmbedPDF/EmbedPDF.razor.js

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@ import EventHandler from "../BootstrapBlazor/modules/event-handler.js"
33
import { default as EmbedPDF, DocumentManagerPlugin } from './embedpdf.js'
44
import { getTheme, registerBootstrapBlazorModule } from '../BootstrapBlazor/modules/utility.js'
55

6-
export function init(id, invoke, options) {
6+
export async function init(id, invoke, options) {
77
const el = document.getElementById(id);
88
if (el === null) {
99
return null;
1010
}
1111

1212
const target = el.querySelector('.pdf-viewer');
13-
const { src, tabBar, theme, lang } = options;
13+
const { src, tabBar, theme, lang, currentPage, scrollDirection, pageGap } = options;
1414
const wasmUrl = `${location.origin}/_content/BootstrapBlazor.EmbedPDF/pdfium.wasm`;
1515

1616
let preference = theme;
1717
if (preference === 'system') {
1818
preference = getTheme();
1919
}
2020

21+
let currentPageGap = pageGap;
22+
if (currentPageGap === 0) {
23+
currentPageGap = 20;
24+
}
25+
2126
const viewer = EmbedPDF.init({
2227
type: 'container',
2328
target: el,
@@ -31,6 +36,10 @@ export function init(id, invoke, options) {
3136
i18n: {
3237
defaultLocale: lang,
3338
fallbackLocale: 'en'
39+
},
40+
scroll: {
41+
defaultStrategy: scrollDirection,
42+
defaultPageGap: currentPageGap
3443
}
3544
});
3645

@@ -39,33 +48,55 @@ export function init(id, invoke, options) {
3948
});
4049

4150
Data.set(id, { el, invoke, options, viewer });
51+
52+
const registry = await viewer.registry;
53+
const scroll = registry.getPlugin('scroll').provides();
54+
55+
scroll.onLayoutReady((event) => {
56+
scroll.scrollToPage({
57+
pageNumber: 5,
58+
behavior: 'instant'
59+
});
60+
});
4261
}
4362

4463
export async function setUrl(id, url) {
45-
if (url) {
46-
const pdf = Data.get(id);
47-
const { viewer } = pdf;
48-
49-
if (viewer) {
50-
const registry = await viewer.registry;
51-
const docManager = registry.getPlugin('document-manager').provides();
52-
docManager.openDocumentUrl({
53-
url,
54-
documentId: getFileName(url),
55-
autoActivate: true
56-
});
57-
}
64+
const pdf = Data.get(id);
65+
const { viewer } = pdf;
66+
67+
if (viewer) {
68+
const registry = await viewer.registry;
69+
const docManager = registry.getPlugin('document-manager').provides();
70+
const name = getFileName(url);
71+
docManager.openDocumentUrl({
72+
url,
73+
name,
74+
documentId: name,
75+
autoActivate: true
76+
});
5877
}
5978
}
6079

6180
export function setTheme(id, theme) {
6281
const pdf = Data.get(id);
6382
const { viewer } = pdf;
83+
6484
if (viewer) {
6585
viewer.setTheme(theme);
6686
}
6787
}
6888

89+
export async function setLocale(id, locale) {
90+
const pdf = Data.get(id);
91+
const { viewer } = pdf;
92+
93+
if (viewer) {
94+
const registry = await viewer.registry;
95+
const i18n = registry.getPlugin('i18n').provides();
96+
i18n.setLocale(locale);
97+
}
98+
}
99+
69100
export function dispose(id) {
70101
const pdf = Data.get(id);
71102
Data.remove(id);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 EmbedPDFScrollDirection
13+
{
14+
/// <summary>
15+
/// 垂直方向
16+
/// </summary>
17+
[Description("vertical")]
18+
Vertical,
19+
20+
/// <summary>
21+
/// 水平方向
22+
/// </summary>
23+
[Description("Horizontal")]
24+
Horizontal
25+
}

0 commit comments

Comments
 (0)