Skip to content

Commit 7b2fbff

Browse files
committed
refactor: 增加页面适配功能
1 parent 52c4bb8 commit 7b2fbff

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

src/components/BootstrapBlazor.PdfReader/PdfReader.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@using Microsoft.AspNetCore.Components.Web
12
@namespace BootstrapBlazor.Components
23
@inherits BootstrapModuleComponentBase
34

@@ -14,8 +15,8 @@
1415
<input type="text" class="bb-view-scale" value="100%" />
1516
<div class="bb-view-icon"><i class="fa-solid fa-plus"></i></div>
1617
<div class="bb-view-divider"></div>
17-
<div class="bb-view-icon bb-view-fit-page"><i class="fa-solid fa-arrows-left-right-to-line"></i></div>
18-
<div class="bb-view-icon bb-view-fit-width"><i class="fa-solid fa-arrows-left-right"></i></div>
18+
<div class="bb-view-icon bb-view-fit-page" @onclick="FitToPage"><i class="fa-solid fa-arrows-left-right-to-line"></i></div>
19+
<div class="bb-view-icon bb-view-fit-width" @onclick="FitToWidth"><i class="fa-solid fa-arrows-left-right"></i></div>
1920
<div class="bb-view-icon bb-view-fit-rotate"><i class="fa-solid fa-rotate-left"></i></div>
2021
<div class="bb-view-divider"></div>
2122
<div class="bb-view-icon bb-view-draw"><i class="fa-solid fa-pen-to-square"></i></div>

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public partial class PdfReader
2424
[Parameter]
2525
public string? ViewHeight { get; set; }
2626

27+
/// <summary>
28+
/// 获得/设置 是否适配当前页面宽度 默认 false
29+
/// </summary>
30+
[Parameter]
31+
public bool IsFitToPage { get; set; }
32+
2733
private string? ClassString => CssBuilder.Default("bb-pdf-reader")
2834
.AddClassFromAttributes(AdditionalAttributes)
2935
.Build();
@@ -49,7 +55,7 @@ protected override void OnParametersSet()
4955
/// <inheritdoc/>
5056
/// </summary>
5157
/// <returns></returns>
52-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new { Url });
58+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new { Url, IsFitToPage });
5359

5460
/// <summary>
5561
/// 跳转到指定页码方法
@@ -58,6 +64,18 @@ protected override void OnParametersSet()
5864
/// <returns></returns>
5965
public Task NavigateToPageAsync(int pageNumber) => InvokeVoidAsync("navigateToPage", Id, pageNumber);
6066

67+
/// <summary>
68+
///
69+
/// </summary>
70+
/// <returns></returns>
71+
public Task FitToPage() => InvokeVoidAsync("fitToPage", Id);
72+
73+
/// <summary>
74+
/// 跳转到指定页码方法
75+
/// </summary>
76+
/// <returns></returns>
77+
public Task FitToWidth() => InvokeVoidAsync("fitToWidth", Id);
78+
6179
/// <summary>
6280
/// 页面开始初始化时回调方法
6381
/// </summary>

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ export async function init(id, invoke, options) {
1515

1616
await addLink('./_content/BootstrapBlazor.PdfReader/css/pdf.css');
1717

18-
//const elementId = el.querySelector('canvas');
19-
//const pdf = new Pdf(elementId);
20-
//pdf.scale = scale;
21-
//pdf.rotation = rotation;
22-
2318
const loadingTask = pdfjsLib.getDocument(options);
24-
2519
loadingTask.onProgress = function (progressData) {
2620
console.log(progressData.loaded, progressData.total);
2721
};
@@ -47,9 +41,12 @@ export async function init(id, invoke, options) {
4741

4842

4943
eventBus.on("pagesinit", function () {
50-
// We can use pdfViewer now, e.g. let's change default scale.
51-
pdfViewer.currentScaleValue = "page-width";
52-
console.log("pagesInit");
44+
if (options.isFitToPage) {
45+
pdfViewer.currentScaleValue = 1.0;
46+
}
47+
else {
48+
pdfViewer.currentScaleValue = "page-width";
49+
}
5350
});
5451

5552
// handle the promise
@@ -82,8 +79,22 @@ export async function init(id, invoke, options) {
8279
Data.set(id, pdfViewer);
8380
}
8481

82+
export function fitToPage(id) {
83+
const pdfViewer = Data.get(id);
84+
if (pdfViewer) {
85+
pdfViewer.currentScaleValue = 1.0;
86+
}
87+
}
88+
89+
export function fitToWidth(id) {
90+
const pdfViewer = Data.get(id);
91+
if (pdfViewer) {
92+
pdfViewer.currentScaleValue = "page-width";
93+
}
94+
}
95+
8596
export function dispose(id) {
86-
Data.remove(id);
97+
Data.get(id);
8798
}
8899

89100
function getCanvas(item) {

0 commit comments

Comments
 (0)