-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsEmbedder.cs
36 lines (31 loc) · 1.04 KB
/
JsEmbedder.cs
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
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Text.Json;
using System.Threading.Tasks;
namespace BlazorJsEmbedder
{
public class JsEmbedder : ComponentBase
{
#region Constants
const string JS_FUNC_PREFIX = "window.embedderFunctions.";
const string JS_EVALUATE_FUNC = "evaluate";
#endregion
#region Parameters and Injects
[Inject] public IJSRuntime JSRuntime { get; set; }
[Parameter] public string JavaScript { get; set; }
#endregion
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(firstRender)
{
await JSRuntime.InvokeVoidAsync(FullFuncName(JS_EVALUATE_FUNC), JavaScript);
}
}
public async Task<T> RunJsFunction<T>(string code)
{
return await JSRuntime.InvokeAsync<T>(FullFuncName(JS_EVALUATE_FUNC), code);
}
private static string FullFuncName(string funcName) =>JS_FUNC_PREFIX + funcName;
}
}