diff --git a/LANCommander/Components/ScriptEditorDialog.razor b/LANCommander/Components/ScriptEditorDialog.razor new file mode 100644 index 0000000..c7ffba4 --- /dev/null +++ b/LANCommander/Components/ScriptEditorDialog.razor @@ -0,0 +1,117 @@ +@using LANCommander.Data.Enums; +@using LANCommander.Models +@using LANCommander.Services +@inject ISnackbar Snackbar +@inject IDialogService DialogService +@inject ScriptService ScriptService + + + + + @foreach (var group in Snippets.Select(s => s.Group).Distinct()) + { + + @foreach (var snippet in Snippets.Where(s => s.Group == group)) + { + @snippet.Name + } + + } + + + + + + + + + + @foreach (ScriptType type in Enum.GetValues(typeof(ScriptType))) + { + @type + } + + + + + + + + + Save + + + + + +@code { + [CascadingParameter] MudDialogInstance MudDialog { get; set; } + [Parameter] public Script Script { get; set; } + + IEnumerable Snippets { get; set; } + StandaloneCodeEditor Editor; + + private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor) + { + return new StandaloneEditorConstructionOptions + { + AutomaticLayout = true, + Language = "powershell", + Value = Script.Contents, + Theme = "vs-dark", + }; + } + + protected override async Task OnInitializedAsync() + { + Snippets = ScriptService.GetSnippets(); + + if (Script == null) + Script = new Script(); + } + + private async Task Save() + { + var value = await Editor.GetValue(); + + ScriptService.Update(Script); + + Snackbar.Add("Script saved!", Severity.Success); + MudDialog.Close(); + } + + private async void InsertSnippet(Snippet snippet) + { + Editor.Trigger("keyboard", "type", new + { + text = snippet.Content + }); + } + + private async void BrowseForPath() + { + var parameters = new DialogParameters + { + ["ArchiveId"] = Script.Game.Archives.OrderByDescending(a => a.CreatedOn).First().Id + }; + + var dialog = await DialogService.ShowAsync("File Selector", parameters); + var result = await dialog.Result; + + if (!result.Canceled) + { + var path = result.Data as string; + + Editor.Trigger("keyboard", "type", new + { + text = $"$InstallDir\\{path.Replace('/', '\\')}" + }); + + StateHasChanged(); + } + } +} diff --git a/LANCommander/Pages/Games/Edit.razor b/LANCommander/Pages/Games/Edit.razor index 53fa12f..5acef6c 100644 --- a/LANCommander/Pages/Games/Edit.razor +++ b/LANCommander/Pages/Games/Edit.razor @@ -1,6 +1,7 @@ @page "/Games/{id:guid}/Edit" @inject GameService GameService @inject ArchiveService ArchiveService +@inject ScriptService ScriptService @inject IDialogService DialogService @inject ISnackbar Snackbar @@ -161,6 +162,37 @@ + + + + + Scripts + + + + + + + Type + Created By + Created On + + + + + @context.Type + @context.CreatedBy?.UserName + @context.CreatedOn + + + + + + + + + Add + @@ -288,4 +320,49 @@ StateHasChanged(); } + + private async void EditScript(Script script = null) + { + if (script == null) + script = new Script() + { + GameId = Game.Id, + Game = Game + }; + + var parameters = new DialogParameters + { + ["Script"] = script + }; + + var dialog = await DialogService.ShowAsync("Edit Script", parameters, new DialogOptions() + { + CloseButton = true, + DisableBackdropClick = true, + FullWidth = true, + MaxWidth = MaxWidth.ExtraLarge, + CloseOnEscapeKey = false, + }); + + var result = await dialog.Result; + + await GameService.Context.Entry(Game).Collection(nameof(Game.Archives)).LoadAsync(); + + StateHasChanged(); + } + + private async void DeleteScript(Script script) + { + bool? result = await DialogService.ShowMessageBox( + "Delete Script", + "Do you really want to delete this script? You will not be able to recover it later.", + "Delete", + "Cancel" + ); + + if (result == true) + await ScriptService.Delete(script); + + StateHasChanged(); + } }