-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathScripts.cs
More file actions
111 lines (96 loc) · 4.47 KB
/
Scripts.cs
File metadata and controls
111 lines (96 loc) · 4.47 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Microsoft.AspNetCore.StaticAssets;
using Microsoft.JSInterop;
using System.Net.Http.Headers;
using System.Reflection;
namespace SeeSharp.Blazor;
public static class Scripts {
static string ReadResourceText(string filename) {
var assembly = typeof(Scripts).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("SeeSharp.Blazor." + filename)
?? throw new FileNotFoundException("resource file not found", filename);
return new StreamReader(stream).ReadToEnd();
}
/// <summary>
/// Required in the < head > so that the SimpleImageIO.FlipBook can be rendered
/// </summary>
public static readonly string FlipBookScript =
$$"""
<script>
{{SimpleImageIO.FlipBook.HeaderScript}}
function makeFlipBook(jsonArgs, onClickObj, onClickMethodName, onWheelObj, onWheelMethodName, onMouseOverObj, onMouseOverMethodName, onKeyObj, onKeyMethodName) {
let onClick = null;
if (onClickObj && onClickMethodName) {
onClick = (mouseButton, mouseX, mouseY, ID, selectedIdx, keysPressed) =>
onClickObj.invokeMethodAsync(onClickMethodName, mouseButton, mouseX, mouseY, ID, selectedIdx, keysPressed)
}
let onWheel = null
if (onWheelObj && onWheelMethodName) {
onWheel = (mouseX, mouseY, deltaY, ID, selectedIdx, keysPressed) =>
onWheelObj.invokeMethodAsync(onWheelMethodName, mouseX, mouseY, deltaY, ID, selectedIdx, keysPressed)
}
let onMouseOver = null
if (onMouseOverObj && onMouseOverMethodName) {
onMouseOver = (mouseX, mouseY, ID, selectedIdx, keysPressed) =>
onMouseOverObj.invokeMethodAsync(onMouseOverMethodName, mouseX, mouseY, ID, selectedIdx, keysPressed)
}
let onKey = null
if (onKeyObj && onKeyMethodName) {
onKey = (mouseX, mouseY, ID, selectedIdx, keysPressed) =>
onKeyObj.invokeMethodAsync(onKeyMethodName, mouseX, mouseY, ID, selectedIdx, keysPressed)
}
window['flipbook']['MakeFlipBook'](jsonArgs, onClick, onWheel, onMouseOver, onKey);
}
</script>
""";
/// <summary>
/// Utility script to add to the < head > to download arbitrary streams from the server
/// </summary>
public static readonly string DownloadScript =
$$"""
<script>
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}
</script>
""";
public static readonly string WidgetScripts =
$$"""
<script>
{{ReadResourceText("Scripts.rotationInput.js")}}
</script>
""";
public static readonly string UpdateImageScript =
$$"""
<script>
function updateImage(jsonArgs) {
window['flipbook']['UpdateImage'](jsonArgs);
return;
}
</script>
""";
public static readonly string AllScripts = FlipBookScript + DownloadScript + WidgetScripts + UpdateImageScript;
/// <summary>
/// Downloads a stream to the client with the given file name. Requires that <see cref="DownloadScript" />
/// was added to the < head >
/// </summary>
public static async Task DownloadAsFile(IJSRuntime js, string filename, Stream stream) {
using var r = new DotNetStreamReference(stream: stream);
await js.InvokeVoidAsync("downloadFileFromStream", filename, r);
}
public static async Task DownloadAsFile(IJSRuntime js, string filename, string content) {
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(content));
await DownloadAsFile(js, filename, stream);
}
public static async Task DownloadAsFile(IJSRuntime js, string filename, SeeSharp.Common.HtmlReport report)
=> await DownloadAsFile(js, filename, report.ToString());
public static async Task DownloadAsFile(this SeeSharp.Common.HtmlReport report, IJSRuntime js, string filename)
=> await DownloadAsFile(js, filename, report.ToString());
}