|
| 1 | +// Copyright (c) Argo Zhang (argo@163.com). 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 or https://argozhang.github.io/ |
| 4 | + |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Microsoft.JSInterop; |
| 7 | + |
| 8 | +namespace BootstrapBlazor.Components; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// 默认 Html to Image 实现 |
| 12 | +/// <param name="runtime"></param> |
| 13 | +/// <param name="logger"></param> |
| 14 | +/// </summary> |
| 15 | +class DefaultDom2ImageService(IJSRuntime runtime, ILogger<DefaultDom2ImageService> logger) : IDom2ImageService |
| 16 | +{ |
| 17 | + private JSModule? _jsModule; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// <inheritdoc/> |
| 21 | + /// </summary> |
| 22 | + public async Task<string?> GetUrlAsync(string selector, Dom2ImageOptions? options = null, CancellationToken token = default) |
| 23 | + { |
| 24 | + string? data = null; |
| 25 | + try |
| 26 | + { |
| 27 | + _jsModule ??= await LoadModule(); |
| 28 | + data = await _jsModule.InvokeAsync<string?>("getUrl", token, selector, options); |
| 29 | + } |
| 30 | + catch (OperationCanceledException) { } |
| 31 | + catch (Exception ex) |
| 32 | + { |
| 33 | + logger.LogError(ex, "{GetUrlAsync} throw exception: {ex}", nameof(GetUrlAsync), ex.Format()); |
| 34 | + } |
| 35 | + return data; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// <inheritdoc/> |
| 40 | + /// </summary> |
| 41 | + public async Task<Stream?> GetStreamAsync(string selector, Dom2ImageOptions? options = null, CancellationToken token = default) |
| 42 | + { |
| 43 | + Stream? data = null; |
| 44 | + try |
| 45 | + { |
| 46 | + _jsModule ??= await LoadModule(); |
| 47 | + var streamReference = await _jsModule.InvokeAsync<IJSStreamReference?>("getStream", selector, options); |
| 48 | + if (streamReference != null) |
| 49 | + { |
| 50 | + data = await streamReference.OpenReadStreamAsync(streamReference.Length, token); |
| 51 | + } |
| 52 | + } |
| 53 | + catch (OperationCanceledException) { } |
| 54 | + catch (Exception ex) |
| 55 | + { |
| 56 | + logger.LogError(ex, "{GetStreamAsync} throw exception: {ex}", nameof(GetStreamAsync), ex.Format()); |
| 57 | + } |
| 58 | + return data; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// <inheritdoc/> |
| 63 | + /// </summary> |
| 64 | + /// <param name="selector"></param> |
| 65 | + /// <param name="fileName"></param> |
| 66 | + /// <param name="format"></param> |
| 67 | + /// <param name="backgroundColor"></param> |
| 68 | + /// <param name="options"></param> |
| 69 | + /// <returns></returns> |
| 70 | + public async Task DownloadAsync(string selector, string fileName = "capture", string? format = "png", string? backgroundColor = null, Dom2ImageOptions? options = null) |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + _jsModule ??= await LoadModule(); |
| 75 | + await _jsModule.InvokeAsync<IJSStreamReference?>("downloadAsync", selector, fileName, format, backgroundColor, options); |
| 76 | + } |
| 77 | + catch (OperationCanceledException) { } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + logger.LogError(ex, "{DownloadAsync} throw exception: {ex}", nameof(DownloadAsync), ex.Format()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private Task<JSModule> LoadModule() => runtime.LoadModule("./_content/BootstrapBlazor.Dom2Image/dom2image.js"); |
| 85 | +} |
0 commit comments