Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>10.0.1</Version>
</PropertyGroup>

<PropertyGroup>
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Mermaid</PackageTags>
<Description>Bootstrap UI components extensions of Mermaid</Description>
Expand Down
51 changes: 45 additions & 6 deletions src/components/BootstrapBlazor.Mermaid/Mermaid.razor.cs
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/

Expand Down Expand Up @@ -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();
Expand All @@ -48,7 +54,8 @@ public partial class Mermaid
protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
await MermaidChanged();

await Render();
}

/// <summary>
Expand All @@ -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>
Comment thread
ArgoZhang marked this conversation as resolved.
Comment thread
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);
Comment thread
ArgoZhang marked this conversation as resolved.
if (html != null)
{
stream = await Html2PdfService.PdfStreamFromHtmlAsync(html);
}

return stream;
}

/// <summary>
/// 下载 Pdf 文档
/// </summary>
Comment thread
ArgoZhang marked this conversation as resolved.
/// <returns>base64 string of the diagram</returns>
Comment thread
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
ArgumentNullException.ThrowIfNull(fileName);
var stream = await ExportPdfAsync(token);
if (stream != null)
{
await DownloadService.DownloadFromStreamAsync(fileName, stream);
}
ArgumentNullException.ThrowIfNull(fileName);
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentException("fileName cannot be empty or whitespace.", nameof(fileName));
}
var stream = await ExportPdfAsync(token);
if (stream != null)
{
await DownloadService.DownloadFromStreamAsync(fileName, stream);
}

}

private string BuildDiagramText()
{
StringBuilder sb = new();
Expand Down
22 changes: 19 additions & 3 deletions src/components/BootstrapBlazor.Mermaid/Mermaid.razor.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import mermaid from './mermaid.min.js';
import { addScript } from '../BootstrapBlazor/modules/utility.js'

export async function init(id, content) {
await addScript('./_content/BootstrapBlazor.Mermaid/mermaid.min.js');

await render(id, content);
}
Comment thread
ArgoZhang marked this conversation as resolved.

export async function render(id, content) {
mermaid.initialize({ startOnLoad: false });
const render = await mermaid.render(id + '-svg', content);
const render = await mermaid.render(`${id}-svg`, content);
if (render) {
const el = document.getElementById(id);
Comment thread
ArgoZhang marked this conversation as resolved.
el.innerText = "";
el.innerHTML = render.svg;
}
}

export function getContent(id) {
let svgDataUrl = "";
const el = document.getElementById(id);
Expand All @@ -25,3 +31,13 @@ export function getContent(id) {
}
}
}

export function getSvgHtml(id) {
const el = document.getElementById(id);
const svg = el.querySelector('svg');
if (svg) {
return svg.outerHTML;
}

Comment thread
ArgoZhang marked this conversation as resolved.
return "";
}
Loading
Loading