Skip to content

Commit 9855378

Browse files
authored
Merge pull request #483 from serverlessworkflow/fix-monaco-bug
Fixes "editor not found" errors in the Dashboard
2 parents 4d35e9f + 6df40ad commit 9855378

File tree

8 files changed

+111
-69
lines changed

8 files changed

+111
-69
lines changed

Diff for: src/dashboard/Synapse.Dashboard/Components/DocumentDetails/Store.cs

+19-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class DocumentDetailsStore(ILogger<DocumentDetailsStore> logger, ISynapse
3333

3434
TextModel? _textModel;
3535
readonly string _textModelUri = monacoEditorHelper.GetResourceUri();
36+
private bool _hasTextEditorInitialized = false;
3637

3738
/// <summary>
3839
/// Gets the service used to perform logging
@@ -291,7 +292,7 @@ public async Task LoadReferencedDocumentAsync()
291292
/// <returns></returns>
292293
public async Task ToggleTextBasedEditorLanguageAsync(string _)
293294
{
294-
await this.OnTextBasedEditorInitAsync();
295+
await this.InitializeTextBasedEditorAsync();
295296
}
296297

297298
/// <summary>
@@ -300,6 +301,17 @@ public async Task ToggleTextBasedEditorLanguageAsync(string _)
300301
/// <returns></returns>
301302
public async Task OnTextBasedEditorInitAsync()
302303
{
304+
this._hasTextEditorInitialized = true;
305+
await this.InitializeTextBasedEditorAsync();
306+
}
307+
308+
/// <summary>
309+
/// Initializes the text editor
310+
/// </summary>
311+
/// <returns></returns>
312+
public async Task InitializeTextBasedEditorAsync()
313+
{
314+
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
303315
await this.SetTextBasedEditorLanguageAsync();
304316
await this.SetTextEditorValueAsync();
305317
}
@@ -313,10 +325,10 @@ public async Task SetTextBasedEditorLanguageAsync()
313325
try
314326
{
315327
var language = this.MonacoEditorHelper.PreferredLanguage;
316-
if (this.TextEditor != null)
328+
if (this.TextEditor != null && this._hasTextEditorInitialized)
317329
{
318330
this._textModel = await Global.GetModel(this.JSRuntime, this._textModelUri);
319-
this._textModel ??= await Global.CreateModel(this.JSRuntime, "", language, this._textModelUri);
331+
this._textModel ??= await Global.CreateModel(this.JSRuntime, " ", language, this._textModelUri);
320332
await Global.SetModelLanguage(this.JSRuntime, this._textModel, language);
321333
await this.TextEditor!.SetModel(this._textModel);
322334
}
@@ -335,7 +347,7 @@ async Task SetTextEditorValueAsync()
335347
{
336348
var document = this.Get(state => state.DocumentJson);
337349
var language = this.MonacoEditorHelper.PreferredLanguage;
338-
if (this.TextEditor != null && !string.IsNullOrWhiteSpace(document))
350+
if (this.TextEditor != null && !string.IsNullOrWhiteSpace(document) && this._hasTextEditorInitialized)
339351
{
340352
try
341353
{
@@ -359,7 +371,7 @@ async Task SetTextEditorValueAsync()
359371
/// <returns>A awaitable task</returns>
360372
public async Task OnCopyToClipboard()
361373
{
362-
if (this.TextEditor == null) return;
374+
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
363375
var text = await this.TextEditor.GetValue();
364376
if (string.IsNullOrWhiteSpace(text)) return;
365377
try
@@ -408,7 +420,7 @@ public override Task InitializeAsync()
408420
/// <returns></returns>
409421
protected async Task OnPreferredThemeChangedAsync(string newTheme)
410422
{
411-
if (this.TextEditor != null)
423+
if (this.TextEditor != null && this._hasTextEditorInitialized)
412424
{
413425
await this.TextEditor.UpdateOptions(new EditorUpdateOptions() { Theme = newTheme });
414426
}
@@ -430,7 +442,7 @@ protected override void Dispose(bool disposing)
430442
this._textModel.DisposeModel();
431443
this._textModel = null;
432444
}
433-
if (this.TextEditor != null)
445+
if (this.TextEditor != null && this._hasTextEditorInitialized)
434446
{
435447
this.TextEditor.Dispose();
436448
this.TextEditor = null;

Diff for: src/dashboard/Synapse.Dashboard/Components/MonacoEditor/Store.cs

+19-13
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class MonacoEditorStore(ILogger<MonacoEditorStore> logger, ISynapseApiCli
3232

3333
TextModel? _textModel;
3434
string _textModelUri = monacoEditorHelper.GetResourceUri();
35+
private bool _hasTextEditorInitialized = false;
3536

3637
/// <summary>
3738
/// Gets the service used to perform logging
@@ -204,26 +205,20 @@ public void SetTexModelName(string modelName)
204205
/// <returns></returns>
205206
public async Task ToggleTextBasedEditorLanguageAsync(string _)
206207
{
207-
if (this.TextEditor == null)
208-
{
209-
return;
210-
}
208+
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
211209
var language = this.MonacoEditorHelper.PreferredLanguage;
212210
try
213211
{
214212
var document = await this.TextEditor.GetValue();
215-
if (document == null)
216-
{
217-
return;
218-
}
213+
if (document == null) return;
219214
document = language == PreferredLanguage.YAML ?
220215
this.YamlSerializer.ConvertFromJson(document) :
221216
this.YamlSerializer.ConvertToJson(document);
222217
this.Reduce(state => state with
223218
{
224219
DocumentText = document
225220
});
226-
await this.OnTextBasedEditorInitAsync();
221+
await this.InitializeTextBasedEditorAsync();
227222
}
228223
catch (Exception ex)
229224
{
@@ -238,6 +233,17 @@ public async Task ToggleTextBasedEditorLanguageAsync(string _)
238233
/// <returns></returns>
239234
public async Task OnTextBasedEditorInitAsync()
240235
{
236+
this._hasTextEditorInitialized = true;
237+
await this.InitializeTextBasedEditorAsync();
238+
}
239+
240+
/// <summary>
241+
/// Initializes the text editor
242+
/// </summary>
243+
/// <returns></returns>
244+
public async Task InitializeTextBasedEditorAsync()
245+
{
246+
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
241247
await this.SetTextBasedEditorLanguageAsync();
242248
await this.SetTextEditorValueAsync();
243249
}
@@ -251,7 +257,7 @@ public async Task SetTextBasedEditorLanguageAsync()
251257
try
252258
{
253259
var language = this.MonacoEditorHelper.PreferredLanguage;
254-
if (this.TextEditor != null)
260+
if (this.TextEditor != null && this._hasTextEditorInitialized)
255261
{
256262
this._textModel = await Global.GetModel(this.JSRuntime, this._textModelUri);
257263
this._textModel ??= await Global.CreateModel(this.JSRuntime, "", language, this._textModelUri);
@@ -272,7 +278,7 @@ public async Task SetTextBasedEditorLanguageAsync()
272278
async Task SetTextEditorValueAsync()
273279
{
274280
var document = this.Get(state => state.DocumentText);
275-
if (this.TextEditor != null && !string.IsNullOrWhiteSpace(document))
281+
if (this.TextEditor != null && !string.IsNullOrWhiteSpace(document) && this._hasTextEditorInitialized)
276282
{
277283
await this.TextEditor.SetValue(document);
278284
}
@@ -284,7 +290,7 @@ async Task SetTextEditorValueAsync()
284290
/// <returns>A awaitable task</returns>
285291
public async Task OnCopyToClipboard()
286292
{
287-
if (this.TextEditor == null) return;
293+
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
288294
var text = await this.TextEditor.GetValue();
289295
if (string.IsNullOrWhiteSpace(text)) return;
290296
try
@@ -321,7 +327,7 @@ public override Task InitializeAsync()
321327
/// <returns></returns>
322328
protected async Task OnPreferredThemeChangedAsync(string newTheme)
323329
{
324-
if (this.TextEditor != null)
330+
if (this.TextEditor != null && this._hasTextEditorInitialized)
325331
{
326332
await this.TextEditor.UpdateOptions(new EditorUpdateOptions() { Theme = newTheme });
327333
}

Diff for: src/dashboard/Synapse.Dashboard/Pages/Functions/Create/Store.cs

+22-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using Semver;
1717
using ServerlessWorkflow.Sdk.Models;
1818
using Synapse.Api.Client.Services;
19-
using Synapse.Dashboard.Pages.Workflows.Create;
2019
using Synapse.Resources;
2120

2221
namespace Synapse.Dashboard.Pages.Functions.Create;
@@ -51,6 +50,7 @@ MonacoInterop monacoInterop
5150
private string _textModelUri = string.Empty;
5251
private bool _disposed = false;
5352
private bool _processingVersion = false;
53+
private bool _hasTextEditorInitialized = false;
5454

5555
/// <summary>
5656
/// Gets the service used to perform logging
@@ -292,7 +292,7 @@ public async Task ToggleTextBasedEditorLanguageAsync(string _)
292292
{
293293
FunctionText = document
294294
});
295-
await this.OnTextBasedEditorInitAsync();
295+
await this.InitializeTextBasedEditorAsync();
296296
}
297297
catch (Exception ex)
298298
{
@@ -307,6 +307,17 @@ public async Task ToggleTextBasedEditorLanguageAsync(string _)
307307
/// <returns></returns>
308308
public async Task OnTextBasedEditorInitAsync()
309309
{
310+
this._hasTextEditorInitialized = true;
311+
await this.InitializeTextBasedEditorAsync();
312+
}
313+
314+
/// <summary>
315+
/// Initializes the text editor
316+
/// </summary>
317+
/// <returns></returns>
318+
public async Task InitializeTextBasedEditorAsync()
319+
{
320+
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
310321
await this.SetTextBasedEditorLanguageAsync();
311322
await this.SetTextEditorValueAsync();
312323
}
@@ -317,10 +328,7 @@ public async Task OnTextBasedEditorInitAsync()
317328
/// <returns></returns>
318329
public async Task SetTextBasedEditorLanguageAsync()
319330
{
320-
if (this.TextEditor == null)
321-
{
322-
return;
323-
}
331+
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
324332
try
325333
{
326334
var language = this.MonacoEditorHelper.PreferredLanguage;
@@ -342,10 +350,7 @@ public async Task SetTextBasedEditorLanguageAsync()
342350
async Task SetTextEditorValueAsync()
343351
{
344352
var document = this.Get(state => state.FunctionText);
345-
if (this.TextEditor == null || string.IsNullOrWhiteSpace(document))
346-
{
347-
return;
348-
}
353+
if (this.TextEditor == null || string.IsNullOrWhiteSpace(document) || !this._hasTextEditorInitialized) return;
349354
try
350355
{
351356
await this.TextEditor.SetValue(document);
@@ -365,7 +370,7 @@ async Task SetTextEditorValueAsync()
365370
/// <returns>An awaitable task</returns>
366371
public async Task OnDidChangeModelContent(ModelContentChangedEvent e)
367372
{
368-
if (this.TextEditor == null) return;
373+
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
369374
var document = await this.TextEditor.GetValue();
370375
this.Reduce(state => state with
371376
{
@@ -379,7 +384,7 @@ public async Task OnDidChangeModelContent(ModelContentChangedEvent e)
379384
/// <returns>A new awaitable <see cref="Task"/></returns>
380385
public async Task SaveCustomFunctionAsync()
381386
{
382-
if (this.TextEditor == null)
387+
if (this.TextEditor == null || !this._hasTextEditorInitialized)
383388
{
384389
this.Reduce(state => state with
385390
{
@@ -513,6 +518,10 @@ public async Task SaveCustomFunctionAsync()
513518
/// <inheritdoc/>
514519
public override async Task InitializeAsync()
515520
{
521+
this.Loading.Where(loading => loading == true).Subscribe(loading =>
522+
{
523+
this._hasTextEditorInitialized = false; // reset text editor state when loading
524+
}, token: this.CancellationTokenSource.Token);
516525
this.Function.SubscribeAsync(async definition => {
517526
string document = string.Empty;
518527
if (definition != null)
@@ -543,7 +552,7 @@ await this.GetCustomFunctionAsync(name!),
543552
/// <returns></returns>
544553
protected async Task OnPreferredThemeChangedAsync(string newTheme)
545554
{
546-
if (this.TextEditor != null)
555+
if (this.TextEditor != null && this._hasTextEditorInitialized)
547556
{
548557
await this.TextEditor.UpdateOptions(new EditorUpdateOptions() { Theme = newTheme });
549558
}

Diff for: src/dashboard/Synapse.Dashboard/Pages/Functions/Create/View.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ else
9292
string? version;
9393
string? chosenName;
9494
string? nameInputValue;
95-
bool loading;
95+
bool loading = true;
9696
bool saving;
9797
ProblemDetails? problemDetails = null;
9898

0 commit comments

Comments
 (0)