-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
325 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Window x:Class="Microsoft.JavaScript.NodeApi.Examples.Window1" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:Microsoft.JavaScript.NodeApi.Examples" | ||
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf" | ||
mc:Ignorable="d" | ||
Title="Window1" Height="450" Width="800"> | ||
<DockPanel> | ||
<wv2:WebView2 Name="webView" | ||
/> | ||
</DockPanel> | ||
</Window> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Windows; | ||
using Microsoft.Web.WebView2.Core; | ||
|
||
namespace Microsoft.JavaScript.NodeApi.Examples; | ||
|
||
public partial class Window1 : Window | ||
{ | ||
private readonly string markdown; | ||
|
||
public static void CreateWebView2Window(string markdown) | ||
{ | ||
StaThreadWrapper(() => { new Window1(markdown).ShowDialog(); }); | ||
} | ||
|
||
private Window1(string markdown) | ||
{ | ||
this.markdown = markdown; | ||
InitializeComponent(); | ||
} | ||
|
||
private static void StaThreadWrapper(Action action) | ||
{ | ||
var t = new Thread(o => | ||
{ | ||
action(); | ||
////System.Windows.Threading.Dispatcher.Run(); | ||
}); | ||
t.SetApartmentState(ApartmentState.STA); | ||
t.Start(); | ||
t.Join(); | ||
} | ||
|
||
protected override async void OnContentRendered(EventArgs e) | ||
{ | ||
base.OnContentRendered(e); | ||
await webView.EnsureCoreWebView2Async(); // This will work just fine | ||
|
||
webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived; | ||
|
||
string html = $@" | ||
<!DOCTYPE html> | ||
<html lang=""en""> | ||
<body onload=""drawDiagram()""> | ||
<script type=""module""> | ||
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs'; | ||
window.mermaid = mermaid; | ||
</script> | ||
<script> | ||
const drawDiagram = async function () {{ | ||
mermaid.initialize({{ securityLevel: ""sandbox"" }}) | ||
const graphDefinition = `{markdown}`; | ||
const {{ svg }} = await mermaid.render('graphDiv', graphDefinition); | ||
window.chrome.webview.postMessage(svg); | ||
document.getElementById('diagram').innerHTML = svg; | ||
}} | ||
</script> | ||
<div id=""diagram""></div> | ||
</body> | ||
</html> | ||
"; | ||
webView.NavigateToString(html); | ||
} | ||
|
||
/// <summary> | ||
/// Triggers when Mermaid svg is generated | ||
/// </summary> | ||
private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e) | ||
{ | ||
string data = e.TryGetWebMessageAsString(); | ||
Console.Write(data); | ||
////Close(); | ||
} | ||
} |
This file contains 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 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 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 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.