forked from mendix/ExtensionAPI-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentServer.cs
More file actions
65 lines (62 loc) · 2.27 KB
/
ContentServer.cs
File metadata and controls
65 lines (62 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.ComponentModel.Composition;
using System.Text;
using Mendix.StudioPro.ExtensionsAPI.UI.WebServer;
namespace MyCompany.MyProject.MendixExtension;
[Export(typeof(WebServerExtension))]
public class ContentServer : WebServerExtension
{
private const string Content = """
<html>
<head>
<script>
function sendMessage(message, data) {
if (window.chrome?.webview) {
window.chrome.webview.postMessage({ message, data })
} else if (window.webkit?.messageHandlers.studioPro) {
window.webkit.messageHandlers.studioPro.postMessage(JSON.stringify({ message, data }))
}
}
function registerMessageListener(eventHandler) {
if (window.chrome?.webview) {
window.chrome.webview.addEventListener("message", (event) => eventHandler(event.data))
sendMessage("MessageListenerRegistered")
} else if (window.webkit?.messageHandlers.studioPro) {
window.WKPostMessage = (json) => {
const wkMessage = JSON.parse(json)
eventHandler(wkMessage)
}
sendMessage("MessageListenerRegistered")
}
}
function init() {
registerMessageListener(msgHandler);
}
function msgHandler(event) {
console.log('message sent to JS: '+event.data);
}
function create() {
sendMessage(document.getElementById("entity_name").value, null);
}
</script>
</head>
<body onload="init()">
<div>
<p>Entity Name</p>
<input type="text" style="width: 150px" id="entity_name"/>
<p><button onclick="create();">Create Entity</button></p>
</div>
</body>
</html>
""";
public override void InitializeWebServer(IWebServer webServer)
{
webServer.AddRoute("index", async (_, response, _) =>
{
response.ContentType = "text/html";
response.StatusCode = 200;
var content = Encoding.ASCII.GetBytes(Content);
response.ContentLength64 = content.Length;
await response.OutputStream.WriteAsync(content, CancellationToken.None);
});
}
}