2025-01-27 00:21:46 -06:00
|
|
|
using BlazorMonaco.Editor;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2026-05-08 02:02:33 -05:00
|
|
|
using Microsoft.JSInterop;
|
2025-01-27 00:21:46 -06:00
|
|
|
|
|
|
|
|
namespace LANCommander.UI.Components;
|
|
|
|
|
|
|
|
|
|
public class MonacoCodeEditor : StandaloneCodeEditor
|
|
|
|
|
{
|
2026-05-08 02:02:33 -05:00
|
|
|
private static bool _completionsRegistered;
|
2026-05-10 19:32:27 -05:00
|
|
|
private IJSObjectReference? _module;
|
|
|
|
|
private string? _previousScriptType;
|
2026-05-08 02:02:33 -05:00
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private IJSRuntime JSRuntime { get; set; } = default!;
|
|
|
|
|
|
2025-01-27 00:21:46 -06:00
|
|
|
[Parameter]
|
|
|
|
|
public string Value { get; set; }
|
2026-05-08 02:02:33 -05:00
|
|
|
|
2025-01-27 00:21:46 -06:00
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<string> ValueChanged { get; set; }
|
2026-05-08 02:02:33 -05:00
|
|
|
|
2025-01-27 00:21:46 -06:00
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback OnSave { get; set; }
|
|
|
|
|
|
2026-05-10 19:32:27 -05:00
|
|
|
[Parameter]
|
|
|
|
|
public string? ScriptType { get; set; }
|
|
|
|
|
|
2025-01-27 00:21:46 -06:00
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
OnDidChangeModelContent = Microsoft.AspNetCore.Components.EventCallback.Factory.Create<ModelContentChangedEvent>(this, OnChanged);
|
|
|
|
|
OnDidInit = Microsoft.AspNetCore.Components.EventCallback.Factory.Create(this, OnInit);
|
|
|
|
|
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 19:32:27 -05:00
|
|
|
protected override async Task OnParametersSetAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_module != null && ScriptType != _previousScriptType)
|
|
|
|
|
{
|
|
|
|
|
_previousScriptType = ScriptType;
|
|
|
|
|
await _module.InvokeVoidAsync("setScriptType", ScriptType);
|
|
|
|
|
await RunValidationAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await base.OnParametersSetAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 00:21:46 -06:00
|
|
|
private async Task OnInit()
|
|
|
|
|
{
|
|
|
|
|
await AddCommand((int)BlazorMonaco.KeyMod.CtrlCmd | (int)BlazorMonaco.KeyCode.KeyS, async (editor) =>
|
|
|
|
|
{
|
|
|
|
|
if (OnSave.HasDelegate)
|
|
|
|
|
await OnSave.InvokeAsync();
|
|
|
|
|
}, null);
|
2026-05-08 02:02:33 -05:00
|
|
|
|
2026-05-10 19:32:27 -05:00
|
|
|
_module = await JSRuntime.InvokeAsync<IJSObjectReference>(
|
|
|
|
|
"import", "./_content/LANCommander.UI/bundle.js");
|
|
|
|
|
|
2026-05-08 02:02:33 -05:00
|
|
|
if (!_completionsRegistered)
|
|
|
|
|
{
|
|
|
|
|
_completionsRegistered = true;
|
2026-05-10 19:32:27 -05:00
|
|
|
await _module.InvokeVoidAsync("registerPowerShellCompletions");
|
|
|
|
|
}
|
2026-05-08 02:02:33 -05:00
|
|
|
|
2026-05-10 19:32:27 -05:00
|
|
|
if (ScriptType != null)
|
|
|
|
|
{
|
|
|
|
|
_previousScriptType = ScriptType;
|
|
|
|
|
await _module.InvokeVoidAsync("setScriptType", ScriptType);
|
2026-05-08 02:02:33 -05:00
|
|
|
}
|
2025-01-27 00:21:46 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:01:12 -05:00
|
|
|
private async Task OnChanged(ModelContentChangedEvent e)
|
2025-01-27 00:21:46 -06:00
|
|
|
{
|
|
|
|
|
Value = await GetValue();
|
2026-05-08 02:02:33 -05:00
|
|
|
|
2025-01-27 00:21:46 -06:00
|
|
|
if (ValueChanged.HasDelegate)
|
2026-03-20 00:01:12 -05:00
|
|
|
await ValueChanged.InvokeAsync(Value);
|
2026-05-10 19:32:27 -05:00
|
|
|
|
|
|
|
|
await RunValidationAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task InsertSnippetAsync(string snippetText)
|
|
|
|
|
{
|
|
|
|
|
if (_module != null)
|
|
|
|
|
await _module.InvokeVoidAsync("insertSnippet", snippetText);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RunValidationAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_module == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await _module.InvokeVoidAsync("validateScript");
|
2025-01-27 00:21:46 -06:00
|
|
|
}
|
|
|
|
|
}
|