Skip to content

Commit

Permalink
Merge pull request #483 from serverlessworkflow/fix-monaco-bug
Browse files Browse the repository at this point in the history
Fixes "editor not found" errors in the Dashboard
  • Loading branch information
cdavernas authored Jan 22, 2025
2 parents 4d35e9f + 6df40ad commit 9855378
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class DocumentDetailsStore(ILogger<DocumentDetailsStore> logger, ISynapse

TextModel? _textModel;
readonly string _textModelUri = monacoEditorHelper.GetResourceUri();
private bool _hasTextEditorInitialized = false;

/// <summary>
/// Gets the service used to perform logging
Expand Down Expand Up @@ -291,7 +292,7 @@ public async Task LoadReferencedDocumentAsync()
/// <returns></returns>
public async Task ToggleTextBasedEditorLanguageAsync(string _)
{
await this.OnTextBasedEditorInitAsync();
await this.InitializeTextBasedEditorAsync();
}

/// <summary>
Expand All @@ -300,6 +301,17 @@ public async Task ToggleTextBasedEditorLanguageAsync(string _)
/// <returns></returns>
public async Task OnTextBasedEditorInitAsync()
{
this._hasTextEditorInitialized = true;
await this.InitializeTextBasedEditorAsync();
}

/// <summary>
/// Initializes the text editor
/// </summary>
/// <returns></returns>
public async Task InitializeTextBasedEditorAsync()
{
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
await this.SetTextBasedEditorLanguageAsync();
await this.SetTextEditorValueAsync();
}
Expand All @@ -313,10 +325,10 @@ public async Task SetTextBasedEditorLanguageAsync()
try
{
var language = this.MonacoEditorHelper.PreferredLanguage;
if (this.TextEditor != null)
if (this.TextEditor != null && this._hasTextEditorInitialized)
{
this._textModel = await Global.GetModel(this.JSRuntime, this._textModelUri);
this._textModel ??= await Global.CreateModel(this.JSRuntime, "", language, this._textModelUri);
this._textModel ??= await Global.CreateModel(this.JSRuntime, " ", language, this._textModelUri);
await Global.SetModelLanguage(this.JSRuntime, this._textModel, language);
await this.TextEditor!.SetModel(this._textModel);
}
Expand All @@ -335,7 +347,7 @@ async Task SetTextEditorValueAsync()
{
var document = this.Get(state => state.DocumentJson);
var language = this.MonacoEditorHelper.PreferredLanguage;
if (this.TextEditor != null && !string.IsNullOrWhiteSpace(document))
if (this.TextEditor != null && !string.IsNullOrWhiteSpace(document) && this._hasTextEditorInitialized)
{
try
{
Expand All @@ -359,7 +371,7 @@ async Task SetTextEditorValueAsync()
/// <returns>A awaitable task</returns>
public async Task OnCopyToClipboard()
{
if (this.TextEditor == null) return;
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
var text = await this.TextEditor.GetValue();
if (string.IsNullOrWhiteSpace(text)) return;
try
Expand Down Expand Up @@ -408,7 +420,7 @@ public override Task InitializeAsync()
/// <returns></returns>
protected async Task OnPreferredThemeChangedAsync(string newTheme)
{
if (this.TextEditor != null)
if (this.TextEditor != null && this._hasTextEditorInitialized)
{
await this.TextEditor.UpdateOptions(new EditorUpdateOptions() { Theme = newTheme });
}
Expand All @@ -430,7 +442,7 @@ protected override void Dispose(bool disposing)
this._textModel.DisposeModel();
this._textModel = null;
}
if (this.TextEditor != null)
if (this.TextEditor != null && this._hasTextEditorInitialized)
{
this.TextEditor.Dispose();
this.TextEditor = null;
Expand Down
32 changes: 19 additions & 13 deletions src/dashboard/Synapse.Dashboard/Components/MonacoEditor/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MonacoEditorStore(ILogger<MonacoEditorStore> logger, ISynapseApiCli

TextModel? _textModel;
string _textModelUri = monacoEditorHelper.GetResourceUri();
private bool _hasTextEditorInitialized = false;

/// <summary>
/// Gets the service used to perform logging
Expand Down Expand Up @@ -204,26 +205,20 @@ public void SetTexModelName(string modelName)
/// <returns></returns>
public async Task ToggleTextBasedEditorLanguageAsync(string _)
{
if (this.TextEditor == null)
{
return;
}
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
var language = this.MonacoEditorHelper.PreferredLanguage;
try
{
var document = await this.TextEditor.GetValue();
if (document == null)
{
return;
}
if (document == null) return;
document = language == PreferredLanguage.YAML ?
this.YamlSerializer.ConvertFromJson(document) :
this.YamlSerializer.ConvertToJson(document);
this.Reduce(state => state with
{
DocumentText = document
});
await this.OnTextBasedEditorInitAsync();
await this.InitializeTextBasedEditorAsync();
}
catch (Exception ex)
{
Expand All @@ -238,6 +233,17 @@ public async Task ToggleTextBasedEditorLanguageAsync(string _)
/// <returns></returns>
public async Task OnTextBasedEditorInitAsync()
{
this._hasTextEditorInitialized = true;
await this.InitializeTextBasedEditorAsync();
}

/// <summary>
/// Initializes the text editor
/// </summary>
/// <returns></returns>
public async Task InitializeTextBasedEditorAsync()
{
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
await this.SetTextBasedEditorLanguageAsync();
await this.SetTextEditorValueAsync();
}
Expand All @@ -251,7 +257,7 @@ public async Task SetTextBasedEditorLanguageAsync()
try
{
var language = this.MonacoEditorHelper.PreferredLanguage;
if (this.TextEditor != null)
if (this.TextEditor != null && this._hasTextEditorInitialized)
{
this._textModel = await Global.GetModel(this.JSRuntime, this._textModelUri);
this._textModel ??= await Global.CreateModel(this.JSRuntime, "", language, this._textModelUri);
Expand All @@ -272,7 +278,7 @@ public async Task SetTextBasedEditorLanguageAsync()
async Task SetTextEditorValueAsync()
{
var document = this.Get(state => state.DocumentText);
if (this.TextEditor != null && !string.IsNullOrWhiteSpace(document))
if (this.TextEditor != null && !string.IsNullOrWhiteSpace(document) && this._hasTextEditorInitialized)
{
await this.TextEditor.SetValue(document);
}
Expand All @@ -284,7 +290,7 @@ async Task SetTextEditorValueAsync()
/// <returns>A awaitable task</returns>
public async Task OnCopyToClipboard()
{
if (this.TextEditor == null) return;
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
var text = await this.TextEditor.GetValue();
if (string.IsNullOrWhiteSpace(text)) return;
try
Expand Down Expand Up @@ -321,7 +327,7 @@ public override Task InitializeAsync()
/// <returns></returns>
protected async Task OnPreferredThemeChangedAsync(string newTheme)
{
if (this.TextEditor != null)
if (this.TextEditor != null && this._hasTextEditorInitialized)
{
await this.TextEditor.UpdateOptions(new EditorUpdateOptions() { Theme = newTheme });
}
Expand Down
35 changes: 22 additions & 13 deletions src/dashboard/Synapse.Dashboard/Pages/Functions/Create/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Semver;
using ServerlessWorkflow.Sdk.Models;
using Synapse.Api.Client.Services;
using Synapse.Dashboard.Pages.Workflows.Create;
using Synapse.Resources;

namespace Synapse.Dashboard.Pages.Functions.Create;
Expand Down Expand Up @@ -51,6 +50,7 @@ MonacoInterop monacoInterop
private string _textModelUri = string.Empty;
private bool _disposed = false;
private bool _processingVersion = false;
private bool _hasTextEditorInitialized = false;

/// <summary>
/// Gets the service used to perform logging
Expand Down Expand Up @@ -292,7 +292,7 @@ public async Task ToggleTextBasedEditorLanguageAsync(string _)
{
FunctionText = document
});
await this.OnTextBasedEditorInitAsync();
await this.InitializeTextBasedEditorAsync();
}
catch (Exception ex)
{
Expand All @@ -307,6 +307,17 @@ public async Task ToggleTextBasedEditorLanguageAsync(string _)
/// <returns></returns>
public async Task OnTextBasedEditorInitAsync()
{
this._hasTextEditorInitialized = true;
await this.InitializeTextBasedEditorAsync();
}

/// <summary>
/// Initializes the text editor
/// </summary>
/// <returns></returns>
public async Task InitializeTextBasedEditorAsync()
{
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
await this.SetTextBasedEditorLanguageAsync();
await this.SetTextEditorValueAsync();
}
Expand All @@ -317,10 +328,7 @@ public async Task OnTextBasedEditorInitAsync()
/// <returns></returns>
public async Task SetTextBasedEditorLanguageAsync()
{
if (this.TextEditor == null)
{
return;
}
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
try
{
var language = this.MonacoEditorHelper.PreferredLanguage;
Expand All @@ -342,10 +350,7 @@ public async Task SetTextBasedEditorLanguageAsync()
async Task SetTextEditorValueAsync()
{
var document = this.Get(state => state.FunctionText);
if (this.TextEditor == null || string.IsNullOrWhiteSpace(document))
{
return;
}
if (this.TextEditor == null || string.IsNullOrWhiteSpace(document) || !this._hasTextEditorInitialized) return;
try
{
await this.TextEditor.SetValue(document);
Expand All @@ -365,7 +370,7 @@ async Task SetTextEditorValueAsync()
/// <returns>An awaitable task</returns>
public async Task OnDidChangeModelContent(ModelContentChangedEvent e)
{
if (this.TextEditor == null) return;
if (this.TextEditor == null || !this._hasTextEditorInitialized) return;
var document = await this.TextEditor.GetValue();
this.Reduce(state => state with
{
Expand All @@ -379,7 +384,7 @@ public async Task OnDidChangeModelContent(ModelContentChangedEvent e)
/// <returns>A new awaitable <see cref="Task"/></returns>
public async Task SaveCustomFunctionAsync()
{
if (this.TextEditor == null)
if (this.TextEditor == null || !this._hasTextEditorInitialized)
{
this.Reduce(state => state with
{
Expand Down Expand Up @@ -513,6 +518,10 @@ public async Task SaveCustomFunctionAsync()
/// <inheritdoc/>
public override async Task InitializeAsync()
{
this.Loading.Where(loading => loading == true).Subscribe(loading =>
{
this._hasTextEditorInitialized = false; // reset text editor state when loading
}, token: this.CancellationTokenSource.Token);
this.Function.SubscribeAsync(async definition => {
string document = string.Empty;
if (definition != null)
Expand Down Expand Up @@ -543,7 +552,7 @@ await this.GetCustomFunctionAsync(name!),
/// <returns></returns>
protected async Task OnPreferredThemeChangedAsync(string newTheme)
{
if (this.TextEditor != null)
if (this.TextEditor != null && this._hasTextEditorInitialized)
{
await this.TextEditor.UpdateOptions(new EditorUpdateOptions() { Theme = newTheme });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ else
string? version;
string? chosenName;
string? nameInputValue;
bool loading;
bool loading = true;
bool saving;
ProblemDetails? problemDetails = null;

Expand Down
Loading

0 comments on commit 9855378

Please sign in to comment.