-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(Mermaid): add DownloadPdfAsync method #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/components/BootstrapBlazor.Mermaid/BootstrapBlazor.Mermaid.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||||||||||||||||||||||||||||||
| // Copyright (c) Argo Zhang (argo@163.com). All rights reserved. | ||||||||||||||||||||||||||||||||||||||
| // Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved. | ||||||||||||||||||||||||||||||||||||||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||||||||||||||||||||||||||||||||||||||
| // Website: https://www.blazor.zone or https://argozhang.github.io/ | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
@@ -37,6 +37,12 @@ public partial class Mermaid | |||||||||||||||||||||||||||||||||||||
| [Parameter] | ||||||||||||||||||||||||||||||||||||||
| public string? Title { set; get; } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| [Inject, NotNull] | ||||||||||||||||||||||||||||||||||||||
| private IHtml2Pdf? Html2PdfService { get; set; } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| [Inject, NotNull] | ||||||||||||||||||||||||||||||||||||||
| private DownloadService? DownloadService { get; set; } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private string? ClassString => CssBuilder.Default("bb-mermaid mermaid") | ||||||||||||||||||||||||||||||||||||||
| .AddClassFromAttributes(AdditionalAttributes) | ||||||||||||||||||||||||||||||||||||||
| .Build(); | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -48,7 +54,8 @@ public partial class Mermaid | |||||||||||||||||||||||||||||||||||||
| protected override async Task OnParametersSetAsync() | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| await base.OnParametersSetAsync(); | ||||||||||||||||||||||||||||||||||||||
| await MermaidChanged(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| await Render(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -58,21 +65,53 @@ protected override async Task OnParametersSetAsync() | |||||||||||||||||||||||||||||||||||||
| protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, BuildDiagramText()); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
| /// 内容改变时重新渲染mermaid | ||||||||||||||||||||||||||||||||||||||
| /// 内容改变时重新渲染 mermaid | ||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||
| /// <returns></returns> | ||||||||||||||||||||||||||||||||||||||
| [Obsolete("已弃用,请使用 Render 代替; Deprecated; please use Render instead.")] | ||||||||||||||||||||||||||||||||||||||
| [ExcludeFromCodeCoverage] | ||||||||||||||||||||||||||||||||||||||
| public Task MermaidChanged() => InvokeVoidAsync("init", Id, BuildDiagramText()); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
| /// 内容改变时重新渲染 mermaid | ||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||
| public Task Render() => InvokeVoidAsync("render", Id, BuildDiagramText()); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
| /// 导出图为 base64 字符串 | ||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||
| /// <returns>base64 string of the diagram</returns> | ||||||||||||||||||||||||||||||||||||||
| public Task<string?> ExportBase64MermaidAsync() => InvokeAsync<string>("getContent", Id); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
| /// 构造 Mermaid 代码 | ||||||||||||||||||||||||||||||||||||||
| /// 导出图为 Pdf 文档流 | ||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||
|
ArgoZhang marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||
| /// <returns></returns> | ||||||||||||||||||||||||||||||||||||||
| public async Task<Stream?> ExportPdfAsync(CancellationToken token = default) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| Stream? stream = null; | ||||||||||||||||||||||||||||||||||||||
| var html = await InvokeAsync<string?>("getSvgHtml", token, Id); | ||||||||||||||||||||||||||||||||||||||
|
ArgoZhang marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||
| if (html != null) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| stream = await Html2PdfService.PdfStreamFromHtmlAsync(html); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return stream; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||
| /// 下载 Pdf 文档 | ||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||
|
ArgoZhang marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||
| /// <returns>base64 string of the diagram</returns> | ||||||||||||||||||||||||||||||||||||||
|
ArgoZhang marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||
| public async Task DownloadPdfAsync(string fileName, CancellationToken token = default) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| ArgumentNullException.ThrowIfNull(fileName); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| var stream = await ExportPdfAsync(token); | ||||||||||||||||||||||||||||||||||||||
| if (stream != null) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| await DownloadService.DownloadFromStreamAsync(fileName, stream); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+106
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Consider validating fileName for empty or whitespace values. Also check for empty or whitespace-only fileName values using string.IsNullOrWhiteSpace(fileName).
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private string BuildDiagramText() | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| StringBuilder sb = new(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.