LANCommander/LANCommander.UI/Components/MonacoCodeEditor/MonacoCodeEditor.cs

97 lines
2.6 KiB
C#
Raw Normal View History

2025-01-27 00:21:46 -06:00
using BlazorMonaco.Editor;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
2025-01-27 00:21:46 -06:00
namespace LANCommander.UI.Components;
public class MonacoCodeEditor : StandaloneCodeEditor
{
private static bool _completionsRegistered;
private IJSObjectReference? _module;
private string? _previousScriptType;
[Inject]
private IJSRuntime JSRuntime { get; set; } = default!;
2025-01-27 00:21:46 -06:00
[Parameter]
public string Value { get; set; }
2025-01-27 00:21:46 -06:00
[Parameter]
public EventCallback<string> ValueChanged { get; set; }
2025-01-27 00:21:46 -06:00
[Parameter]
public EventCallback OnSave { get; set; }
[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();
}
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);
_module = await JSRuntime.InvokeAsync<IJSObjectReference>(
"import", "./_content/LANCommander.UI/bundle.js");
if (!_completionsRegistered)
{
_completionsRegistered = true;
await _module.InvokeVoidAsync("registerPowerShellCompletions");
}
if (ScriptType != null)
{
_previousScriptType = ScriptType;
await _module.InvokeVoidAsync("setScriptType", ScriptType);
}
await _module.InvokeVoidAsync("SetupFileDropHandler");
2025-01-27 00:21:46 -06:00
}
private async Task OnChanged(ModelContentChangedEvent e)
2025-01-27 00:21:46 -06:00
{
Value = await GetValue();
2025-01-27 00:21:46 -06:00
if (ValueChanged.HasDelegate)
await ValueChanged.InvokeAsync(Value);
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
}
}