|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 | // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
5 | 5 |
|
6 | | -using Microsoft.Extensions.Logging; |
7 | | - |
8 | 6 | namespace BootstrapBlazor.Components; |
9 | 7 |
|
10 | 8 | /// <summary> |
11 | | -/// 默认 Html to Image 实现 |
12 | | -/// <param name="runtime"></param> |
13 | | -/// <param name="logger"></param> |
| 9 | +/// 默认 Html2Image 实现 |
14 | 10 | /// </summary> |
15 | | -class DefaultHtml2ImageService(IJSRuntime runtime, ILogger<DefaultHtml2ImageService> logger) : IHtml2Image |
| 11 | +class DefaultHtml2ImageService : IHtml2Image |
16 | 12 | { |
17 | | - private JSModule? _jsModule; |
| 13 | + private const string ErrorMessage = "请增加依赖包 BootstrapBlazor.Html2Image 通过 AddBootstrapBlazorHtml2ImageService 进行服务注入; Please add BootstrapBlazor.Html2Image package and use AddBootstrapBlazorHtml2ImageService inject service"; |
18 | 14 |
|
19 | 15 | /// <summary> |
20 | 16 | /// <inheritdoc/> |
21 | 17 | /// </summary> |
22 | | - public Task<string?> GetDataAsync(string selector, Html2ImageOptions options) => Execute(selector, "toPng", options); |
| 18 | + public Task<string?> GetDataAsync(string selector, IHtml2ImageOptions? options = null) => throw new NotImplementedException(ErrorMessage); |
23 | 19 |
|
24 | 20 | /// <summary> |
25 | 21 | /// <inheritdoc/> |
26 | 22 | /// </summary> |
27 | | - public Task<Stream?> GetStreamAsync(string selector, Html2ImageOptions options) => ToBlob(selector, options); |
28 | | - |
29 | | - private async Task<string?> Execute(string selector, string methodName, Html2ImageOptions options) |
30 | | - { |
31 | | - string? data = null; |
32 | | - try |
33 | | - { |
34 | | - _jsModule ??= await runtime.LoadModuleByName("html2image"); |
35 | | - if (_jsModule != null) |
36 | | - { |
37 | | - data = await _jsModule.InvokeAsync<string?>("execute", selector, methodName, options); |
38 | | - } |
39 | | - } |
40 | | - catch (Exception ex) |
41 | | - { |
42 | | - logger.LogError(ex, "{Execute} throw exception", nameof(Execute)); |
43 | | - } |
44 | | - return data; |
45 | | - } |
46 | | - |
47 | | - private async Task<Stream?> ToBlob(string selector, Html2ImageOptions options) |
48 | | - { |
49 | | - Stream? data = null; |
50 | | - try |
51 | | - { |
52 | | - _jsModule ??= await runtime.LoadModuleByName("html2image"); |
53 | | - if (_jsModule != null) |
54 | | - { |
55 | | - var streamRef = await _jsModule.InvokeAsync<IJSStreamReference>("execute", selector, "toBlob", options); |
56 | | - if (streamRef != null) |
57 | | - { |
58 | | - data = await streamRef.OpenReadStreamAsync(streamRef.Length); |
59 | | - } |
60 | | - } |
61 | | - } |
62 | | - catch (Exception ex) |
63 | | - { |
64 | | - logger.LogError(ex, "{ToBlob} throw exception", nameof(ToBlob)); |
65 | | - } |
66 | | - return data; |
67 | | - } |
| 23 | + public Task<Stream?> GetStreamAsync(string selector, IHtml2ImageOptions? options = null) => throw new NotImplementedException(ErrorMessage); |
68 | 24 | } |
0 commit comments