Added scripts list to edit and added script editor dialog
This commit is contained in:
parent
e99cd3f575
commit
636ede230e
2 changed files with 194 additions and 0 deletions
117
LANCommander/Components/ScriptEditorDialog.razor
Normal file
117
LANCommander/Components/ScriptEditorDialog.razor
Normal file
|
@ -0,0 +1,117 @@
|
|||
@using LANCommander.Data.Enums;
|
||||
@using LANCommander.Models
|
||||
@using LANCommander.Services
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
@inject ScriptService ScriptService
|
||||
|
||||
<MudDialog>
|
||||
<DialogContent>
|
||||
<MudPaper Elevation="0" Class="gap-4 d-flex flex-nowrap justify-content-start mb-2">
|
||||
@foreach (var group in Snippets.Select(s => s.Group).Distinct())
|
||||
{
|
||||
<MudMenu EndIcon="@Icons.Material.Filled.KeyboardArrowDown" Label="@group" Color="Color.Primary" Variant="Variant.Filled">
|
||||
@foreach (var snippet in Snippets.Where(s => s.Group == group))
|
||||
{
|
||||
<MudMenuItem OnClick="() => InsertSnippet(snippet)">@snippet.Name</MudMenuItem>
|
||||
}
|
||||
</MudMenu>
|
||||
}
|
||||
|
||||
<MudTooltip Text="Browse Archive For Path">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Folder" OnClick="BrowseForPath" Class="align-self-end" />
|
||||
</MudTooltip>
|
||||
</MudPaper>
|
||||
|
||||
<StandaloneCodeEditor @ref="Editor" Id="editor" ConstructionOptions="EditorConstructionOptions" />
|
||||
|
||||
<MudSelect @bind-Value="Script.Type" Label="Type">
|
||||
@foreach (ScriptType type in Enum.GetValues(typeof(ScriptType)))
|
||||
{
|
||||
<MudSelectItem Value="@((ScriptType)type)">@type</MudSelectItem>
|
||||
}
|
||||
</MudSelect>
|
||||
|
||||
<MudCheckBox @bind-Checked="Script.RequiresAdmin" Color="Color.Primary" Label="Requires Admin"></MudCheckBox>
|
||||
|
||||
<MudTextField @bind-Value="Script.Description" Lines="4" Label="Description" />
|
||||
</DialogContent>
|
||||
|
||||
<DialogActions>
|
||||
<MudButton Color="Color.Primary" Variant="Variant.Filled" StartIcon="@Icons.Material.Filled.Save" OnClick="Save">Save</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
<style>
|
||||
.monaco-editor-container {
|
||||
height: 600px;
|
||||
}
|
||||
</style>
|
||||
|
||||
@code {
|
||||
[CascadingParameter] MudDialogInstance MudDialog { get; set; }
|
||||
[Parameter] public Script Script { get; set; }
|
||||
|
||||
IEnumerable<Snippet> 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<ArchiveFileSelectorDialog>("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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 @@
|
|||
</MudPaper>
|
||||
</MudCardContent>
|
||||
</MudCard>
|
||||
|
||||
<MudCard Class="mt-4">
|
||||
<MudCardHeader>
|
||||
<CardHeaderContent>
|
||||
<MudText Typo="Typo.h6">Scripts</MudText>
|
||||
</CardHeaderContent>
|
||||
</MudCardHeader>
|
||||
|
||||
<MudCardContent>
|
||||
<MudTable Items="Game.Scripts" Elevation="0">
|
||||
<HeaderContent>
|
||||
<MudTh>Type</MudTh>
|
||||
<MudTh>Created By</MudTh>
|
||||
<MudTh>Created On</MudTh>
|
||||
<MudTh></MudTh>
|
||||
</HeaderContent>
|
||||
|
||||
<RowTemplate>
|
||||
<MudTd>@context.Type</MudTd>
|
||||
<MudTd>@context.CreatedBy?.UserName</MudTd>
|
||||
<MudTd>@context.CreatedOn</MudTd>
|
||||
<MudTd Class="d-flex flex-nowrap justify-end">
|
||||
<MudIconButton OnClick="() => EditScript(context)" Icon="@Icons.Material.Filled.Edit"></MudIconButton>
|
||||
<MudIconButton OnClick="() => DeleteScript(context)" Color="Color.Error" Icon="@Icons.Material.Filled.Close"></MudIconButton>
|
||||
</MudTd>
|
||||
</RowTemplate>
|
||||
</MudTable>
|
||||
|
||||
<MudPaper Class="d-flex justify-end" Elevation="0">
|
||||
<MudButton OnClick="() => EditScript()" Color="Color.Primary" StartIcon="@Icons.Material.Filled.Add" Variant="Variant.Filled">Add</MudButton>
|
||||
</MudPaper>
|
||||
</MudCardContent>
|
||||
</MudCard>
|
||||
|
||||
|
@ -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<ScriptEditorDialog>("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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue