-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathJSRuntimeExtensions.cs
40 lines (33 loc) · 1.55 KB
/
JSRuntimeExtensions.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
37
38
39
40
using System.Reflection;
using Microsoft.JSInterop;
namespace Havit.Blazor.Components.Web;
public static class JSRuntimeExtensions
{
public static ValueTask<IJSObjectReference> ImportModuleAsync(this IJSRuntime jsRuntime, string modulePath, Assembly assemblyForVersionInfo = null)
{
Contract.Requires<ArgumentException>(!String.IsNullOrWhiteSpace(modulePath));
#if !NET9_0_OR_GREATER
// pre-NET9 does not support StaticAssets with ImportMap
if (assemblyForVersionInfo is not null)
{
modulePath = modulePath + "?v=" + GetAssemblyVersionIdentifierForUri(assemblyForVersionInfo);
}
#endif
return jsRuntime.InvokeAsync<IJSObjectReference>("import", modulePath);
}
internal static ValueTask<IJSObjectReference> ImportHavitBlazorWebModuleAsync(this IJSRuntime jsRuntime, string moduleNameWithoutExtension)
{
s_versionIdentifierHavitBlazorWeb ??= GetAssemblyVersionIdentifierForUri(typeof(HxDynamicElement).Assembly);
var path = "./_content/Havit.Blazor.Components.Web/" + moduleNameWithoutExtension + ".js";
#if !NET9_0_OR_GREATER
// pre-NET9 does not support StaticAssets with ImportMap
path = path + "?v=" + s_versionIdentifierHavitBlazorWeb;
#endif
return jsRuntime.InvokeAsync<IJSObjectReference>("import", path);
}
private static string s_versionIdentifierHavitBlazorWeb;
internal static string GetAssemblyVersionIdentifierForUri(Assembly assembly)
{
return Uri.EscapeDataString(((AssemblyInformationalVersionAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyInformationalVersionAttribute), false)).InformationalVersion);
}
}