-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathRepl.razor.cs
More file actions
280 lines (219 loc) · 8.71 KB
/
Repl.razor.cs
File metadata and controls
280 lines (219 loc) · 8.71 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
namespace TryMudBlazor.Client.Pages
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.CodeAnalysis;
using Microsoft.JSInterop;
using MudBlazor;
using Try.Core;
using TryMudBlazor.Client.Components;
using TryMudBlazor.Client.Models;
using TryMudBlazor.Client.Services;
public partial class Repl : IDisposable
{
[Inject] private LayoutService LayoutService { get; set; }
private const string MainUserPagePath = "/__main";
private DotNetObjectReference<Repl> dotNetInstance;
private string errorMessage;
private CodeFile activeCodeFile;
[Inject]
public ISnackbar Snackbar { get; set; }
[Inject]
public SnippetsService SnippetsService { get; set; }
[Inject]
public CompilationService CompilationService { get; set; }
[Inject]
public IJSInProcessRuntime JsRuntime { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Parameter]
public string SnippetId { get; set; }
public CodeEditor CodeEditorComponent { get; set; }
public IDictionary<string, CodeFile> CodeFiles { get; set; } = new Dictionary<string, CodeFile>();
private IList<string> CodeFileNames { get; set; } = new List<string>();
private string CodeEditorContent => this.activeCodeFile?.Content;
private CodeFileType CodeFileType => this.activeCodeFile?.Type ?? CodeFileType.Razor;
private bool SaveSnippetPopupVisible { get; set; }
private IReadOnlyCollection<CompilationDiagnostic> Diagnostics { get; set; } = Array.Empty<CompilationDiagnostic>();
private int ErrorsCount => this.Diagnostics.Count(d => d.Severity == DiagnosticSeverity.Error);
private int WarningsCount => this.Diagnostics.Count(d => d.Severity == DiagnosticSeverity.Warning);
private bool AreDiagnosticsShown { get; set; }
private string LoaderText { get; set; }
private bool Loading { get; set; }
private bool ShowDiagnostics { get; set; }
private void ToggleDiagnostics()
{
ShowDiagnostics = !ShowDiagnostics;
AreDiagnosticsShown = ShowDiagnostics;
}
private string Version
{
get
{
var v = typeof(MudText).Assembly.GetName().Version;
return $"v{v.Major}.{v.Minor}.{v.Build}";
}
}
[JSInvokable]
public async Task TriggerCompileAsync()
{
await this.CompileAsync();
this.StateHasChanged();
}
public void Dispose()
{
this.dotNetInstance?.Dispose();
this.JsRuntime.InvokeVoid(Try.Dispose);
}
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
this.dotNetInstance = DotNetObjectReference.Create(this);
this.JsRuntime.InvokeVoid(Try.Initialize, this.dotNetInstance);
}
if (!string.IsNullOrWhiteSpace(this.errorMessage))
{
Snackbar.Add(this.errorMessage, Severity.Error);
this.errorMessage = null;
}
base.OnAfterRender(firstRender);
}
protected override async Task OnInitializedAsync()
{
Snackbar.Clear();
if (!string.IsNullOrWhiteSpace(this.SnippetId))
{
try
{
this.CodeFiles = (await this.SnippetsService.GetSnippetContentAsync(this.SnippetId)).ToDictionary(f => f.Path, f => f);
if (!this.CodeFiles.Any())
{
this.errorMessage = "No files in snippet.";
}
else
{
this.activeCodeFile = this.CodeFiles.First().Value;
}
}
catch (ArgumentException)
{
this.errorMessage = "Invalid Snippet ID.";
}
catch (Exception)
{
this.errorMessage = "Unable to get snippet content. Please try again later.";
}
}
if (!this.CodeFiles.Any())
{
this.activeCodeFile = new CodeFile
{
Path = CoreConstants.MainComponentFilePath,
Content = CoreConstants.MainComponentDefaultFileContent,
};
this.CodeFiles.Add(CoreConstants.MainComponentFilePath, this.activeCodeFile);
}
this.CodeFileNames = this.CodeFiles.Keys.ToList();
await base.OnInitializedAsync();
}
private async Task CompileAsync()
{
this.Loading = true;
this.LoaderText = "Processing";
await Task.Yield();
CompileToAssemblyResult compilationResult = null;
try
{
this.UpdateActiveCodeFileContent();
compilationResult = await this.CompilationService.CompileToAssemblyAsync(
this.CodeFiles.Values,
this.UpdateLoaderTextAsync);
this.Diagnostics = compilationResult.Diagnostics.OrderByDescending(x => x.Severity).ThenBy(x => x.Code).ToList();
this.AreDiagnosticsShown = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Snackbar.Add("Error while compiling the code.", Severity.Error);
}
finally
{
this.Loading = false;
}
if (compilationResult?.AssemblyBytes?.Length > 0)
{
this.JsRuntime.InvokeVoid(Try.CodeExecution.UpdateUserComponentsDll, compilationResult.AssemblyBytes);
this.JsRuntime.InvokeVoid(Try.CodeExecution.HotReloadIframe, "user-page-window", MainUserPagePath);
}
}
private void ShowSaveSnippetPopup() => this.SaveSnippetPopupVisible = true;
private void HandleTabActivate(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return;
}
this.UpdateActiveCodeFileContent();
if (this.CodeFiles.TryGetValue(name, out var codeFile))
{
this.activeCodeFile = codeFile;
this.CodeEditorComponent.Focus();
}
}
private void HandleTabClose(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return;
}
this.CodeFiles.Remove(name);
}
private void HandleTabCreate(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return;
}
var nameWithoutExtension = Path.GetFileNameWithoutExtension(name);
var newCodeFile = new CodeFile { Path = name };
newCodeFile.Content = newCodeFile.Type == CodeFileType.CSharp
? string.Format(CoreConstants.DefaultCSharpFileContentFormat, nameWithoutExtension)
: string.Format(CoreConstants.DefaultRazorFileContentFormat, nameWithoutExtension);
this.CodeFiles.TryAdd(name, newCodeFile);
this.JsRuntime.InvokeVoid(Try.Editor.SetLangugage, newCodeFile.Type == CodeFileType.CSharp ? "csharp" : "razor");
}
private void UpdateActiveCodeFileContent()
{
if (this.activeCodeFile == null)
{
Snackbar.Add("No active file to update.", Severity.Error);
return;
}
this.activeCodeFile.Content = this.CodeEditorComponent.GetCode();
}
private async Task UpdateLoaderTextAsync(string loaderText)
{
this.LoaderText = loaderText;
this.StateHasChanged();
await Task.Yield();
}
private async Task UpdateTheme()
{
await LayoutService.ToggleDarkMode();
string theme = LayoutService.IsDarkMode ? "vs-dark" : "default";
await JsRuntime.InvokeVoidAsync(Try.Editor.SetTheme, theme);
// LayoutService calls StateHasChanged, we need the updated <style> tags for updateIframeTheme to work
await Task.Yield();
await JsRuntime.InvokeVoidAsync("updateIframeTheme");
}
private void ClearCache()
{
NavigationManager.NavigateTo(NavigationManager.BaseUri, forceLoad: true);
}
}
}