Script editor now supports drag and drop for .ps1, .reg, and other text files. .ps1 files will be handled by replacing the contents of the text editor. .reg files will be converted to PowerShell commands and inserted into the script editor. Any other text file will be inserted as an escaped string.
168 lines
No EOL
5 KiB
Text
168 lines
No EOL
5 KiB
Text
@using BlazorMonaco.Editor
|
|
@using Microsoft.JSInterop
|
|
@namespace LANCommander.UI.Components
|
|
@inject IJSRuntime JSRuntime
|
|
|
|
<StandaloneCodeEditor
|
|
@ref="_editor"
|
|
OnDidChangeModelContent="OnChangedContent"
|
|
OnDidInit="OnInit"
|
|
ConstructionOptions="_editorConstructionOptions" />
|
|
|
|
@code {
|
|
[Parameter] public EventCallback<string> OnSave { get; set; }
|
|
[Parameter] public EventCallback OnReady { get; set; }
|
|
[Parameter] public Language Language { get; set; }
|
|
[Parameter] public int Height { get; set; }
|
|
[Parameter] public string? Value { get; set; }
|
|
[Parameter] public EventCallback<string?> ValueChanged { get; set; }
|
|
[Parameter] public string? ScriptType { get; set; }
|
|
|
|
static bool _completionsRegistered;
|
|
static bool _yamlCompletionsRegistered;
|
|
bool _isEditorInitialized = false;
|
|
string? _previousValue;
|
|
string? _previousScriptType;
|
|
IJSObjectReference? _module;
|
|
|
|
StandaloneCodeEditor _editor;
|
|
|
|
StandaloneEditorConstructionOptions _editorConstructionOptions(StandaloneCodeEditor editor)
|
|
{
|
|
var options = new StandaloneEditorConstructionOptions
|
|
{
|
|
AutomaticLayout = true,
|
|
Language = Language.Id,
|
|
Theme = "vs-dark",
|
|
Value = Value,
|
|
Minimap = new EditorMinimapOptions { Enabled = false },
|
|
BracketPairColorization = new BracketPairColorizationOptions { Enabled = true },
|
|
WordWrap = "on",
|
|
FixedOverflowWidgets = true,
|
|
};
|
|
|
|
if (Height > 0)
|
|
options.Dimension = new Dimension { Height = Height };
|
|
|
|
return options;
|
|
}
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
if (_isEditorInitialized && ValueChanged.HasDelegate && Value != _previousValue)
|
|
{
|
|
_previousValue = Value;
|
|
await _editor.SetValue(Value);
|
|
}
|
|
|
|
if (_isEditorInitialized && _module != null && ScriptType != _previousScriptType)
|
|
{
|
|
_previousScriptType = ScriptType;
|
|
await _module.InvokeVoidAsync("setScriptType", ScriptType);
|
|
await RunValidationAsync();
|
|
}
|
|
|
|
await base.OnParametersSetAsync();
|
|
}
|
|
|
|
async Task OnInit()
|
|
{
|
|
_isEditorInitialized = true;
|
|
|
|
await _editor.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 (Language.Id == "powershell" && !_completionsRegistered)
|
|
{
|
|
_completionsRegistered = true;
|
|
await _module.InvokeVoidAsync("registerPowerShellCompletions");
|
|
}
|
|
|
|
if (Language.Id == "yaml" && !_yamlCompletionsRegistered)
|
|
{
|
|
_yamlCompletionsRegistered = true;
|
|
await _module.InvokeVoidAsync("registerYamlCompletions");
|
|
}
|
|
|
|
if (ScriptType != null)
|
|
{
|
|
_previousScriptType = ScriptType;
|
|
await _module.InvokeVoidAsync("setScriptType", ScriptType);
|
|
}
|
|
|
|
if (Language.Id == "powershell")
|
|
await _module.InvokeVoidAsync("SetupFileDropHandler");
|
|
|
|
if (OnReady.HasDelegate)
|
|
await OnReady.InvokeAsync();
|
|
}
|
|
|
|
public async Task InsertText(string text)
|
|
{
|
|
var selection = await _editor.GetSelection();
|
|
var range = new BlazorMonaco.Range(
|
|
selection.StartLineNumber, selection.StartColumn,
|
|
selection.EndLineNumber, selection.EndColumn);
|
|
|
|
await _editor.ExecuteEdits("CodeInput", new List<IdentifiedSingleEditOperation>
|
|
{
|
|
new IdentifiedSingleEditOperation
|
|
{
|
|
Range = range,
|
|
Text = text,
|
|
ForceMoveMarkers = true
|
|
}
|
|
}, (List<BlazorMonaco.Selection>)null);
|
|
}
|
|
|
|
public async Task InsertSnippet(string snippetText)
|
|
{
|
|
if (_module == null)
|
|
{
|
|
await InsertText(snippetText);
|
|
return;
|
|
}
|
|
|
|
await _module.InvokeVoidAsync("insertSnippet", snippetText);
|
|
}
|
|
|
|
public async Task<bool> InsertTemplateAsync(string scriptType)
|
|
{
|
|
if (_module == null)
|
|
return false;
|
|
|
|
var template = await _module.InvokeAsync<string?>("getScriptTemplate", scriptType);
|
|
|
|
if (template == null)
|
|
return false;
|
|
|
|
await _editor.SetValue(template);
|
|
return true;
|
|
}
|
|
|
|
async Task OnChangedContent(ModelContentChangedEvent e)
|
|
{
|
|
if (ValueChanged.HasDelegate)
|
|
{
|
|
_previousValue = await _editor.GetValue();
|
|
|
|
await ValueChanged.InvokeAsync(_previousValue);
|
|
}
|
|
|
|
await RunValidationAsync();
|
|
}
|
|
|
|
async Task RunValidationAsync()
|
|
{
|
|
if (_module == null || Language.Id != "powershell")
|
|
return;
|
|
|
|
await _module.InvokeVoidAsync("validateScript");
|
|
}
|
|
} |