|
|
- public MermaidForm()
- {
- InitializeComponent();
- webView21.EnsureCoreWebView2Async(null);
- }
- private void MermaidForm_Load(object sender, EventArgs e)
- {
- }
- private async void button1_Click(object sender, EventArgs e)
- {
- string code = richTextBox1.Text.Trim();
- if (string.IsNullOrEmpty(code))
- {
- MessageBox.Show("请输入Mermaid代码");
- return;
- }
- if (string.IsNullOrWhiteSpace(code))
- {
- MessageBox.Show("请输入Mermaid代码");
- return;
- }
- // 本地JS文件路径(exe同目录)
- string jsPath = Path.Combine(Application.StartupPath, "mermaid.min.js");
- string jsWebPath = "file:///" + jsPath.Replace("\", "/");
- // HTML 离线模板
- string safeCode = code.Replace(""", """).Replace("'", "'");
- string html = $@"
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset=""utf-8"">
- <script src=""{jsWebPath}""></script>
- </head>
- <body style=""background:white;padding:20px"">
- <div class=""mermaid"">{safeCode}</div>
- <script>
- mermaid.initialize({{
- startOnLoad: true,
- theme: 'default'
- }});
- </script>
- </body>
- </html>";
- // 加载渲染
- //await webView21.CoreWebView2.NavigateToString(html);
- webView21.NavigateToString(html);
- }
复制代码
|
|