first commit -push
This commit is contained in:
31
RobotNet.WebApp/Scripts/Components/ActionButton.razor
Normal file
31
RobotNet.WebApp/Scripts/Components/ActionButton.razor
Normal file
@@ -0,0 +1,31 @@
|
||||
<MudTooltip Text="@Tooltip" Delay="500">
|
||||
<button type="button" class="@Class" @onclick="Click">
|
||||
<span class="mdi @Icon"></span>
|
||||
</button>
|
||||
</MudTooltip>
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
public string Icon { get; set; } = "mdi-alpha";
|
||||
|
||||
[Parameter]
|
||||
public string Tooltip { get; set; } = "";
|
||||
|
||||
[Parameter]
|
||||
public bool Large { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public bool Disabled { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public EventCallback OnClick { get; set; }
|
||||
|
||||
public string Class => $"{(Large ? "large" : "")} {(Disabled ? "disabled" : "")}";
|
||||
|
||||
private async Task Click()
|
||||
{
|
||||
if (Disabled) return;
|
||||
|
||||
await OnClick.InvokeAsync();
|
||||
}
|
||||
}
|
||||
31
RobotNet.WebApp/Scripts/Components/ActionButton.razor.css
Normal file
31
RobotNet.WebApp/Scripts/Components/ActionButton.razor.css
Normal file
@@ -0,0 +1,31 @@
|
||||
button {
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
overflow: hidden;
|
||||
font-size: 20px;
|
||||
border-radius: 3px;
|
||||
margin-left: 5px;
|
||||
color: white;
|
||||
}
|
||||
|
||||
button:not(.disabled):hover {
|
||||
background-color: rgba(255,255,255,0.3);
|
||||
}
|
||||
|
||||
button:not(.disabled):active {
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
button.disabled > span.mdi {
|
||||
color: rgba(255,255,255,0.3) !important;
|
||||
}
|
||||
|
||||
button.large {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
font-size: 28px;
|
||||
border-radius: 6px;
|
||||
}
|
||||
34
RobotNet.WebApp/Scripts/Components/ConsoleItem.razor
Normal file
34
RobotNet.WebApp/Scripts/Components/ConsoleItem.razor
Normal file
@@ -0,0 +1,34 @@
|
||||
@implements IDisposable
|
||||
|
||||
@using Microsoft.CodeAnalysis
|
||||
|
||||
<pre class="@Class">
|
||||
@this.Model.Message
|
||||
</pre>
|
||||
|
||||
@code {
|
||||
[Parameter, EditorRequired]
|
||||
public ConsoleItemModel Model { get; set; } = null!;
|
||||
|
||||
string Class => this.Model.Severity switch
|
||||
{
|
||||
DiagnosticSeverity.Info => "console-info",
|
||||
DiagnosticSeverity.Warning => "console-warning",
|
||||
DiagnosticSeverity.Error => "console-error",
|
||||
_ => "console-none"
|
||||
};
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Model.Changed -= StateHasChanged;
|
||||
}
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
base.OnAfterRender(firstRender);
|
||||
if(firstRender)
|
||||
{
|
||||
Model.Changed += StateHasChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
RobotNet.WebApp/Scripts/Components/ConsoleItem.razor.css
Normal file
21
RobotNet.WebApp/Scripts/Components/ConsoleItem.razor.css
Normal file
@@ -0,0 +1,21 @@
|
||||
.console-none {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.console-info {
|
||||
color: grey;
|
||||
margin: 0px;
|
||||
text-wrap: auto;
|
||||
}
|
||||
|
||||
.console-warning {
|
||||
color: yellow;
|
||||
margin: 0px;
|
||||
text-wrap: auto;
|
||||
}
|
||||
|
||||
.console-error {
|
||||
color: red;
|
||||
margin: 0px;
|
||||
text-wrap: auto;
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
@using RobotNet.Shares
|
||||
@using RobotNet.Clients
|
||||
@using RobotNet.WebApp.Scripts.Models
|
||||
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IHttpClientFactory httpFactory
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">
|
||||
@Title
|
||||
</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<div class="d-flex flex-column" style="width: 500px;">
|
||||
@foreach (var param in Model.Parameters)
|
||||
{
|
||||
<div class="d-flex align-items-center mb-2">
|
||||
<div class="d-flex flex-row-reverse me-2" style="width: 200px;">
|
||||
<MudText>@param.Name : </MudText>
|
||||
</div>
|
||||
<div style="flex: 1;">
|
||||
@switch (param.Type)
|
||||
{
|
||||
case "System.Boolean":
|
||||
<MudSwitch T="bool" Color="Color.Primary" @bind-Value="@param.BoolValue" />
|
||||
break;
|
||||
case "System.Byte":
|
||||
<MudNumericField T="byte"
|
||||
@bind-Value="@param.ByteValue"
|
||||
Min="@System.Byte.MinValue"
|
||||
Max="@System.Byte.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.SByte":
|
||||
<MudNumericField T="sbyte"
|
||||
@bind-Value="@param.SByteValue"
|
||||
Min="@System.SByte.MinValue"
|
||||
Max="@System.SByte.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int16":
|
||||
<MudNumericField T="short"
|
||||
@bind-Value="@param.ShortValue"
|
||||
Min="@System.Int16.MinValue"
|
||||
Max="@System.Int16.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt16":
|
||||
<MudNumericField T="ushort"
|
||||
@bind-Value="@param.UShortValue"
|
||||
Min="@System.UInt16.MinValue"
|
||||
Max="@System.UInt16.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int32":
|
||||
<MudNumericField T="int"
|
||||
@bind-Value="@param.IntValue"
|
||||
Min="@System.Int32.MinValue"
|
||||
Max="@System.Int32.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt32":
|
||||
<MudNumericField T="uint"
|
||||
@bind-Value="@param.UIntValue"
|
||||
Min="@System.UInt32.MinValue"
|
||||
Max="@System.UInt32.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int64":
|
||||
<MudNumericField T="long"
|
||||
@bind-Value="@param.LongValue"
|
||||
Min="@System.Int64.MinValue"
|
||||
Max="@System.Int64.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt64":
|
||||
<MudNumericField T="ulong"
|
||||
@bind-Value="@param.ULongValue"
|
||||
Min="@System.UInt64.MinValue"
|
||||
Max="@System.UInt64.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Single":
|
||||
<MudNumericField T="float"
|
||||
@bind-Value="@param.FloatValue"
|
||||
Min="@System.Single.MinValue"
|
||||
Max="@System.Single.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Double":
|
||||
<MudNumericField T="double"
|
||||
@bind-Value="@param.DoubleValue"
|
||||
Min="@System.Double.MinValue"
|
||||
Max="@System.Double.MaxValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Decimal":
|
||||
<MudNumericField T="decimal"
|
||||
@bind-Value="@param.DecimalValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.String":
|
||||
<MudTextField @bind-Value="@param.StringValue"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Char":
|
||||
<MudTextField @bind-Value="@param.CharValue"
|
||||
MaxLength="1"
|
||||
Label="@param.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Threading.CancellationToken":
|
||||
<span>
|
||||
<CancellationToken>
|
||||
</span>
|
||||
break;
|
||||
default:
|
||||
<MudAlert Severity="Severity.Error" Dense>Unsupport parameter with type @param.Type</MudAlert>
|
||||
break;
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="OnCancel">Cancel</MudButton>
|
||||
<MudButton Color="Color.Primary" OnClick="Instantiate" Disabled="@IsInstantiating">Instantiate</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private IMudDialogInstance Dialog { get; set; } = null!;
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public ScriptMissionModel Model { get; set; } = default!;
|
||||
|
||||
public string Title => $"Instantiate Mission {Model.Name}";
|
||||
private HttpClient http = default!;
|
||||
|
||||
private bool IsInstantiating = false;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
http = httpFactory.CreateClient("ScriptManagerAPI");
|
||||
}
|
||||
|
||||
private void OnCancel() => Dialog.Cancel();
|
||||
|
||||
private async Task Instantiate()
|
||||
{
|
||||
if (IsInstantiating) return;
|
||||
IsInstantiating = true;
|
||||
StateHasChanged(); // Force UI update to reflect instantiation state
|
||||
|
||||
var parameters = Model.Parameters
|
||||
.Where(p => p.Type != "RobotNet.Script.IRobot")
|
||||
.ToDictionary(p => p.Name, p => p.ToString());
|
||||
|
||||
var result = await http.PostFromJsonAsync<MessageResult<Guid>>("api/ScriptMissions/Runner", new InstanceMissionCreateModel(Model.Name, parameters));
|
||||
if (result is null)
|
||||
{
|
||||
Snackbar.Add("Failed to instantiate mission: server response null", Severity.Error);
|
||||
}
|
||||
else if (result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Mission {Model.Name} instantiated successfully with ID: {result.Data}", Severity.Success);
|
||||
Dialog.Close(DialogResult.Ok(Model.Name));
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add($"Failed to instantiate mission: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
259
RobotNet.WebApp/Scripts/Components/Dashboards/Missions.razor
Normal file
259
RobotNet.WebApp/Scripts/Components/Dashboards/Missions.razor
Normal file
@@ -0,0 +1,259 @@
|
||||
@implements IDisposable
|
||||
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using RobotNet.WebApp.Scripts.Models
|
||||
|
||||
@inject IHttpClientFactory httpFactory
|
||||
@inject IDialogService DialogService
|
||||
|
||||
<div class="flex-grow-1 h-100 position-relative overflow-hidden">
|
||||
<div @ref="divRef" class="w-100 h-100 position-absolute top-0 start-0 overflow-hidden">
|
||||
<MudTable Style="margin: 10px;" Items="scriptMissions" Height="@TableHeight" FixedHeader Virtualize Hover Elevation="6">
|
||||
<ToolBarContent>
|
||||
<div @ref="toolbarRef" class="w-100 d-flex flex-row">
|
||||
<h3>Danh sách Script Missions</h3>
|
||||
<MudSpacer />
|
||||
</div>
|
||||
</ToolBarContent>
|
||||
<ColGroup>
|
||||
<col style="width:60px;" />
|
||||
<col />
|
||||
<col />
|
||||
<col style="width:60px;" />
|
||||
<col style="width:60px;" />
|
||||
</ColGroup>
|
||||
<HeaderContent>
|
||||
<MudTh>STT</MudTh>
|
||||
<MudTh>Tên</MudTh>
|
||||
<MudTh>Parameters</MudTh>
|
||||
<MudTh>Script</MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd>@context.Index</MudTd>
|
||||
<MudTd DataLabel="Tên">@context.Name</MudTd>
|
||||
<MudTd DataLabel="Parameters">
|
||||
<span>
|
||||
@if (context.Parameters?.Any() == true)
|
||||
{
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Info" Color="Color.Primary" Size="Size.Small"
|
||||
OnClick="@(() => ShowParametersDialog(context))" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<span>Không có</span>
|
||||
}
|
||||
</span>
|
||||
</MudTd>
|
||||
<MudTd DataLabel="Script">
|
||||
<span>
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Visibility" Color="Color.Primary" Size="Size.Small"
|
||||
OnClick="@(() => ShowCodeDialog(context.Code))" />
|
||||
</span>
|
||||
</MudTd>
|
||||
<MudTd>
|
||||
<span>
|
||||
<MudIconButton Icon="@Icons.Material.Filled.PlayArrow" Color="Color.Success" Size="Size.Small"
|
||||
Disabled="@CallDisabled" OnClick="@(() => ShowInstantiateMissionDialog(context))" />
|
||||
</span>
|
||||
</MudTd>
|
||||
</RowTemplate>
|
||||
</MudTable>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<MudDialog @bind-Visible="_parametersDialogOpen">
|
||||
<DialogContent>
|
||||
<h6>Thông tin Parameters</h6>
|
||||
@if (_selectedParameters != null)
|
||||
{
|
||||
<MudTable Items="_selectedParameters" Style="min-width: 400px;">
|
||||
<HeaderContent>
|
||||
<MudTh>Tên</MudTh>
|
||||
<MudTh>Kiểu</MudTh>
|
||||
<MudTh>Default</MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd>@context.Name</MudTd>
|
||||
<MudTd>@context.Type</MudTd>
|
||||
<MudTd>@context.Default</MudTd>
|
||||
</RowTemplate>
|
||||
</MudTable>
|
||||
}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="CloseParametersDialog">Đóng</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
<MudDialog @bind-Visible="_codeDialogOpen" Style="min-width: 900px;">
|
||||
<DialogContent>
|
||||
<h6>Mã Script đầy đủ</h6>
|
||||
<MudPaper Class="pa-2" Style="overflow-x:auto;">
|
||||
<pre style="white-space:pre-wrap;word-break:break-all;">@_selectedCode</pre>
|
||||
</MudPaper>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="CloseCodeDialog">Đóng</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ProcessorHubClient ProcessorHub { get; set; } = null!;
|
||||
|
||||
private string TableHeight = "0px";
|
||||
private ElementReference divRef;
|
||||
private ElementReference toolbarRef;
|
||||
|
||||
private HttpClient http = default!;
|
||||
private List<ScriptMissionModel> scriptMissions = [];
|
||||
|
||||
// Dialog state
|
||||
private bool _parametersDialogOpen = false;
|
||||
private IEnumerable<ScriptValueModel>? _selectedParameters;
|
||||
|
||||
private bool _codeDialogOpen = false;
|
||||
private string? _selectedCode;
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
var rect = await divRef.MudGetBoundingClientRectAsync();
|
||||
var toolbarRect = await toolbarRef.MudGetBoundingClientRectAsync();
|
||||
TableHeight = $"{rect.Height - 25 - Math.Max(toolbarRect.Height, 64)}px";
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
http = httpFactory.CreateClient("ScriptManagerAPI");
|
||||
await ReloadData();
|
||||
|
||||
if (ProcessorHub.IsConnected)
|
||||
{
|
||||
OnProcessorStateChanged(await ProcessorHub.GetState());
|
||||
}
|
||||
ProcessorHub.StateChanged += OnProcessorStateChanged;
|
||||
ProcessorHub.ConnectionStateChanged += OnConnectionStateChanged;
|
||||
}
|
||||
|
||||
|
||||
private void OnConnectionStateChanged(HubConnectionState state)
|
||||
{
|
||||
if (state == HubConnectionState.Connected)
|
||||
{
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
OnProcessorStateChanged(await ProcessorHub.GetState());
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private bool isRebuild = false;
|
||||
private bool CallDisabled = true;
|
||||
private void OnProcessorStateChanged(ProcessorState state)
|
||||
{
|
||||
if (state == ProcessorState.Building)
|
||||
{
|
||||
isRebuild = true;
|
||||
}
|
||||
else if (state == ProcessorState.Ready)
|
||||
{
|
||||
if (isRebuild)
|
||||
{
|
||||
isRebuild = false;
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
await ReloadData();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (CallDisabled)
|
||||
{
|
||||
if (state == ProcessorState.Running)
|
||||
{
|
||||
CallDisabled = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state != ProcessorState.Running)
|
||||
{
|
||||
CallDisabled = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReloadData()
|
||||
{
|
||||
scriptMissions.Clear();
|
||||
var missions = await http.GetFromJsonAsync<List<ScriptMissionDto>>("/api/ScriptMissions") ?? [];
|
||||
foreach (var item in missions.Select((value, index) => (value, index)))
|
||||
{
|
||||
scriptMissions.Add(new ScriptMissionModel(item.index + 1, item.value));
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
|
||||
private void ShowParametersDialog(ScriptMissionModel model)
|
||||
{
|
||||
_selectedParameters = model.Parameters;
|
||||
_parametersDialogOpen = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void CloseParametersDialog()
|
||||
{
|
||||
_parametersDialogOpen = false;
|
||||
_selectedParameters = null;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void ShowCodeDialog(string code)
|
||||
{
|
||||
_selectedCode = code;
|
||||
_codeDialogOpen = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void CloseCodeDialog()
|
||||
{
|
||||
_codeDialogOpen = false;
|
||||
_selectedCode = null;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task ShowInstantiateMissionDialog(ScriptMissionModel model)
|
||||
{
|
||||
if (CallDisabled) return;
|
||||
|
||||
foreach (var parameter in model.Parameters)
|
||||
{
|
||||
parameter.Reset();
|
||||
}
|
||||
;
|
||||
var parameters = new DialogParameters<InstantiateMissionDialog>()
|
||||
{
|
||||
{x => x.Model, model},
|
||||
};
|
||||
|
||||
var dialog = await DialogService.ShowAsync<InstantiateMissionDialog>("Instantiate new mission", parameters);
|
||||
var resultDialog = await dialog.Result;
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ProcessorHub.StateChanged -= OnProcessorStateChanged;
|
||||
ProcessorHub.ConnectionStateChanged -= OnConnectionStateChanged;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using Microsoft.CodeAnalysis
|
||||
@implements IDisposable
|
||||
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
|
||||
<div class="w-100">
|
||||
<MudCard Class="m-2" Style="height: 300px" Elevation="6">
|
||||
<MudCardHeader>
|
||||
<CardHeaderContent>
|
||||
<MudText Typo="Typo.h6">Process Controller - @State</MudText>
|
||||
</CardHeaderContent>
|
||||
<CardHeaderActions>
|
||||
<ActionButton Icon="mdi-receipt-text-remove" Large Tooltip="Clear console" OnClick="ClearConsole" />
|
||||
<ActionButton Icon="mdi-refresh text-primary" Large Tooltip="Reset" Disabled="@ResetDisable" OnClick="Reset" />
|
||||
<ActionButton Icon="mdi-cog-play text-primary" Large Tooltip="Build" Disabled="@BuildDisable" OnClick="Build" />
|
||||
<ActionButton Icon="mdi-play text-success" Large Tooltip="Run" Disabled="@RunDisable" OnClick="Run" />
|
||||
<ActionButton Icon="mdi-stop text-danger" Large Tooltip="Stop" Disabled="@StopDisable" OnClick="Stop" />
|
||||
</CardHeaderActions>
|
||||
</MudCardHeader>
|
||||
<MudCardContent>
|
||||
<ProcessorConsole @ref="processorConsole" />
|
||||
</MudCardContent>
|
||||
</MudCard>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ProcessorHubClient ProcessorHub { get; set; } = null!;
|
||||
|
||||
private ProcessorConsole? processorConsole;
|
||||
public ProcessorState State { get; private set; } = ProcessorState.Idle;
|
||||
private ProcessorRequest Request = ProcessorRequest.None;
|
||||
|
||||
private bool ResetDisable => State != ProcessorState.Idle && State != ProcessorState.Ready && State != ProcessorState.Error && State != ProcessorState.BuildError;
|
||||
private bool BuildDisable => State != ProcessorState.Idle && State != ProcessorState.Ready && State != ProcessorState.Error && State != ProcessorState.BuildError;
|
||||
private bool RunDisable => State != ProcessorState.Ready;
|
||||
private bool StopDisable => State != ProcessorState.Running;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (ProcessorHub.IsConnected)
|
||||
{
|
||||
OnProcessorStateChanged(await ProcessorHub.GetState());
|
||||
}
|
||||
|
||||
ProcessorHub.StateChanged += OnProcessorStateChanged;
|
||||
ProcessorHub.RequestChanged += OnRequestChanged;
|
||||
ProcessorHub.ConnectionStateChanged += OnConnectionStateChanged;
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
private void OnConnectionStateChanged(HubConnectionState state)
|
||||
{
|
||||
if (state == HubConnectionState.Connected)
|
||||
{
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
OnProcessorStateChanged(await ProcessorHub.GetState());
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void OnProcessorStateChanged(ProcessorState state)
|
||||
{
|
||||
if (State == state) return;
|
||||
|
||||
State = state;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void OnRequestChanged(ProcessorRequest request)
|
||||
{
|
||||
Request = request;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task Reset()
|
||||
{
|
||||
var result = await ProcessorHub.Reset();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Failed to reset processor: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Build()
|
||||
{
|
||||
var result = await ProcessorHub.Build();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Failed to build processor: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Run()
|
||||
{
|
||||
var result = await ProcessorHub.Run();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Failed to run processor: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Stop()
|
||||
{
|
||||
var result = await ProcessorHub.Stop();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Failed to stop processor: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearConsole()
|
||||
{
|
||||
processorConsole?.Clear();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ProcessorHub.StateChanged -= OnProcessorStateChanged;
|
||||
ProcessorHub.RequestChanged -= OnRequestChanged;
|
||||
ProcessorHub.ConnectionStateChanged -= OnConnectionStateChanged;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
@using RobotNet.Shares
|
||||
@using RobotNet.Clients
|
||||
@using RobotNet.WebApp.Scripts.Models
|
||||
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IHttpClientFactory httpFactory
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">
|
||||
@Title
|
||||
</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<div class="d-flex flex-column" style="width: 500px;">
|
||||
<div class="d-flex align-items-center mb-2">
|
||||
<div class="d-flex flex-row-reverse me-2" style="width: 200px;">
|
||||
<MudText>@Model.Name : </MudText>
|
||||
</div>
|
||||
<div style="flex: 1;">
|
||||
@switch (Model.TypeName)
|
||||
{
|
||||
case "System.Boolean":
|
||||
<MudSwitch T="bool" Color="Color.Primary" @bind-Value="@ValueModel.BoolValue" />
|
||||
break;
|
||||
case "System.Byte":
|
||||
<MudNumericField T="byte"
|
||||
@bind-Value="@ValueModel.ByteValue"
|
||||
Min="@System.Byte.MinValue"
|
||||
Max="@System.Byte.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.SByte":
|
||||
<MudNumericField T="sbyte"
|
||||
@bind-Value="@ValueModel.SByteValue"
|
||||
Min="@System.SByte.MinValue"
|
||||
Max="@System.SByte.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int16":
|
||||
<MudNumericField T="short"
|
||||
@bind-Value="@ValueModel.ShortValue"
|
||||
Min="@System.Int16.MinValue"
|
||||
Max="@System.Int16.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt16":
|
||||
<MudNumericField T="ushort"
|
||||
@bind-Value="@ValueModel.UShortValue"
|
||||
Min="@System.UInt16.MinValue"
|
||||
Max="@System.UInt16.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int32":
|
||||
<MudNumericField T="int"
|
||||
@bind-Value="@ValueModel.IntValue"
|
||||
Min="@System.Int32.MinValue"
|
||||
Max="@System.Int32.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt32":
|
||||
<MudNumericField T="uint"
|
||||
@bind-Value="@ValueModel.UIntValue"
|
||||
Min="@System.UInt32.MinValue"
|
||||
Max="@System.UInt32.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int64":
|
||||
<MudNumericField T="long"
|
||||
@bind-Value="@ValueModel.LongValue"
|
||||
Min="@System.Int64.MinValue"
|
||||
Max="@System.Int64.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt64":
|
||||
<MudNumericField T="ulong"
|
||||
@bind-Value="@ValueModel.ULongValue"
|
||||
Min="@System.UInt64.MinValue"
|
||||
Max="@System.UInt64.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Single":
|
||||
<MudNumericField T="float"
|
||||
@bind-Value="@ValueModel.FloatValue"
|
||||
Min="@System.Single.MinValue"
|
||||
Max="@System.Single.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Double":
|
||||
<MudNumericField T="double"
|
||||
@bind-Value="@ValueModel.DoubleValue"
|
||||
Min="@System.Double.MinValue"
|
||||
Max="@System.Double.MaxValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Decimal":
|
||||
<MudNumericField T="decimal"
|
||||
@bind-Value="@ValueModel.DecimalValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.String":
|
||||
<MudTextField @bind-Value="@ValueModel.StringValue"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Char":
|
||||
<MudTextField @bind-Value="@ValueModel.CharValue"
|
||||
MaxLength="1"
|
||||
Label="@ValueModel.Type"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Threading.CancellationToken":
|
||||
<span>
|
||||
<CancellationToken>
|
||||
</span>
|
||||
break;
|
||||
default:
|
||||
<MudAlert Severity="Severity.Error" Dense>Unsupport parameter with type @ValueModel.Type</MudAlert>
|
||||
break;
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="OnCancel">Cancel</MudButton>
|
||||
<MudButton Color="Color.Primary" OnClick="RequestSetValue" Disabled="@IsRequesing">Update</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private IMudDialogInstance Dialog { get; set; } = null!;
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public ScriptVariableModel Model { get; set; } = default!;
|
||||
|
||||
public string Title => $"Set Variable \"{Model.Name}\"";
|
||||
private HttpClient http = default!;
|
||||
|
||||
private ScriptValueModel ValueModel = null!;
|
||||
|
||||
private bool IsRequesing = false;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
ValueModel = new ScriptValueModel(Model.Name, Model.TypeName, "");
|
||||
|
||||
switch (Model.TypeName)
|
||||
{
|
||||
case "System.Boolean":
|
||||
ValueModel.BoolValue = default;
|
||||
break;
|
||||
case "System.Byte":
|
||||
ValueModel.ByteValue = default;
|
||||
break;
|
||||
case "System.SByte":
|
||||
ValueModel.SByteValue = default;
|
||||
break;
|
||||
case "System.Int16":
|
||||
ValueModel.ShortValue = default;
|
||||
break;
|
||||
case "System.UInt16":
|
||||
ValueModel.UShortValue = default;
|
||||
break;
|
||||
case "System.Int32":
|
||||
ValueModel.IntValue = default;
|
||||
break;
|
||||
case "System.UInt32":
|
||||
ValueModel.UIntValue = default;
|
||||
break;
|
||||
case "System.Int64":
|
||||
ValueModel.LongValue = default;
|
||||
break;
|
||||
case "System.UInt64":
|
||||
ValueModel.ULongValue = default;
|
||||
break;
|
||||
case "System.Single":
|
||||
ValueModel.FloatValue = default;
|
||||
break;
|
||||
case "System.Double":
|
||||
ValueModel.DoubleValue = default;
|
||||
break;
|
||||
case "System.Decimal":
|
||||
ValueModel.DecimalValue = default;
|
||||
break;
|
||||
}
|
||||
http = httpFactory.CreateClient("ScriptManagerAPI");
|
||||
}
|
||||
|
||||
private async Task RequestSetValue()
|
||||
{
|
||||
if (IsRequesing) return;
|
||||
IsRequesing = true;
|
||||
StateHasChanged(); // Force UI update to reflect instantiation state
|
||||
|
||||
var result = await http.PutFromJsonAsync<MessageResult>($"/api/ScriptVariables/{ValueModel.Name}", new UpdateVariableModel(ValueModel.Name, ValueModel.ToString()));
|
||||
if (result == null)
|
||||
{
|
||||
Snackbar.Add("Failed to instantiate mission: server response null", Severity.Error);
|
||||
}
|
||||
else if (result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Success);
|
||||
Dialog.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add($"Failed to reset variable value: {result.Message}", Severity.Error);
|
||||
Dialog.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCancel() => Dialog.Cancel();
|
||||
}
|
||||
263
RobotNet.WebApp/Scripts/Components/Dashboards/Tasks.razor
Normal file
263
RobotNet.WebApp/Scripts/Components/Dashboards/Tasks.razor
Normal file
@@ -0,0 +1,263 @@
|
||||
@implements IAsyncDisposable
|
||||
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using RobotNet.WebApp.Scripts.Models
|
||||
|
||||
@inject IHttpClientFactory httpFactory
|
||||
@inject ScriptTaskHubClient ScriptTaskHub
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<div class="flex-grow-1 h-100 position-relative overflow-hidden">
|
||||
<div @ref="divRef" class="w-100 h-100 position-absolute top-0 start-0 overflow-hidden">
|
||||
<MudTable Style="margin: 10px;" Items="scriptTasks" Height="@TableHeight" FixedHeader Virtualize Hover Elevation="6">
|
||||
<ToolBarContent>
|
||||
<div @ref="toolbarRef" class="w-100 d-flex flex-row">
|
||||
<h3>Danh sách Script Tasks</h3>
|
||||
<MudSpacer />
|
||||
</div>
|
||||
</ToolBarContent>
|
||||
<ColGroup>
|
||||
<col style="width:60px;" />
|
||||
<col />
|
||||
<col />
|
||||
<col style="width:60px;" />
|
||||
<col style="width:60px;" />
|
||||
</ColGroup>
|
||||
<HeaderContent>
|
||||
<MudTh></MudTh>
|
||||
<MudTh>Tên</MudTh>
|
||||
<MudTh>Khoảng thời gian (ms)</MudTh>
|
||||
<MudTh>Enable</MudTh>
|
||||
<MudTh>Script</MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd>@context.Index</MudTd>
|
||||
<MudTd DataLabel="Tên">@context.Name</MudTd>
|
||||
<MudTd DataLabel="Khoảng thời gian">@context.Interval</MudTd>
|
||||
<MudTd DataLabel="Enable">
|
||||
<MudSwitch T="bool" @bind-Value="context.Enable" @bind-Value:after="@(() => RequestTaskAction(context.Name, context.Enable))" Color="Color.Primary" Disabled="@actionDisable" />
|
||||
</MudTd>
|
||||
<MudTd DataLabel="Script">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Visibility" Color="Color.Primary" Size="Size.Small"
|
||||
OnClick="@(() => ShowCodeDialog(context.Code))" />
|
||||
</MudTd>
|
||||
</RowTemplate>
|
||||
</MudTable>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<MudDialog @bind-Visible="_codeDialogOpen" Style="min-width: 900px;">
|
||||
<DialogContent>
|
||||
<h6>Mã Script đầy đủ</h6>
|
||||
<MudPaper Class="pa-2" Style="overflow-x:auto;">
|
||||
<pre style="white-space:pre-wrap;word-break:break-all;">@_selectedCode</pre>
|
||||
</MudPaper>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="CloseCodeDialog">Đóng</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ProcessorHubClient ProcessorHub { get; set; } = null!;
|
||||
|
||||
private string TableHeight = "0px";
|
||||
private ElementReference divRef;
|
||||
private ElementReference toolbarRef;
|
||||
|
||||
private HttpClient http = default!;
|
||||
private List<ScriptTaskModel> scriptTasks = [];
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
var rect = await divRef.MudGetBoundingClientRectAsync();
|
||||
var toolbarRect = await toolbarRef.MudGetBoundingClientRectAsync();
|
||||
TableHeight = $"{rect.Height - 25 - Math.Max(toolbarRect.Height, 64)}px";
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
// Dialog state
|
||||
private bool _codeDialogOpen = false;
|
||||
private string? _selectedCode;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
http = httpFactory.CreateClient("ScriptManagerAPI");
|
||||
await ReloadData();
|
||||
|
||||
if (ProcessorHub.IsConnected)
|
||||
{
|
||||
OnProcessorStateChanged(await ProcessorHub.GetState());
|
||||
}
|
||||
|
||||
ProcessorHub.StateChanged += OnProcessorStateChanged;
|
||||
ProcessorHub.ConnectionStateChanged += OnConnectionStateChanged;
|
||||
|
||||
ScriptTaskHub.TaskResumed += OnTaskResumed;
|
||||
ScriptTaskHub.TaskPaused += OnTaskPaused;
|
||||
|
||||
await ScriptTaskHub.StartAsync();
|
||||
}
|
||||
|
||||
private void OnConnectionStateChanged(HubConnectionState state)
|
||||
{
|
||||
if (state == HubConnectionState.Connected)
|
||||
{
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
OnProcessorStateChanged(await ProcessorHub.GetState());
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private bool isRebuild = false;
|
||||
private bool actionDisable = true;
|
||||
private void OnProcessorStateChanged(ProcessorState state)
|
||||
{
|
||||
if (state == ProcessorState.Building)
|
||||
{
|
||||
isRebuild = true;
|
||||
}
|
||||
else if (state == ProcessorState.Ready)
|
||||
{
|
||||
if (isRebuild)
|
||||
{
|
||||
isRebuild = false;
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
await ReloadData();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (actionDisable)
|
||||
{
|
||||
if (state == ProcessorState.Running)
|
||||
{
|
||||
actionDisable = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state != ProcessorState.Running)
|
||||
{
|
||||
actionDisable = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ProcessorState.Running)
|
||||
{
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
var states = await ScriptTaskHub.GetTaskStates();
|
||||
foreach (var task in scriptTasks)
|
||||
{
|
||||
task.Enable = states.ContainsKey(task.Name) && states[task.Name];
|
||||
}
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReloadData()
|
||||
{
|
||||
await InvokeAsync(async Task () =>
|
||||
{
|
||||
scriptTasks.Clear();
|
||||
var tasks = await http.GetFromJsonAsync<List<ScriptTaskDto>>("/api/ScriptTasks") ?? [];
|
||||
foreach (var item in tasks.Select((value, index) => (value, index)))
|
||||
{
|
||||
scriptTasks.Add(new ScriptTaskModel(item.index + 1, item.value));
|
||||
}
|
||||
if (!actionDisable)
|
||||
{
|
||||
var states = await ScriptTaskHub.GetTaskStates();
|
||||
foreach (var task in scriptTasks)
|
||||
{
|
||||
task.Enable = states.ContainsKey(task.Name) && states[task.Name];
|
||||
}
|
||||
|
||||
}
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
private void ShowCodeDialog(string code)
|
||||
{
|
||||
_selectedCode = code;
|
||||
_codeDialogOpen = true;
|
||||
}
|
||||
|
||||
private void CloseCodeDialog()
|
||||
{
|
||||
_codeDialogOpen = false;
|
||||
_selectedCode = null;
|
||||
}
|
||||
|
||||
private void OnTaskResumed(string taskName)
|
||||
{
|
||||
foreach (var task in scriptTasks)
|
||||
{
|
||||
if (task.Name == taskName)
|
||||
{
|
||||
task.Enable = true;
|
||||
StateHasChanged();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTaskPaused(string taskName)
|
||||
{
|
||||
foreach (var task in scriptTasks)
|
||||
{
|
||||
if (task.Name == taskName)
|
||||
{
|
||||
task.Enable = false;
|
||||
StateHasChanged();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RequestTaskAction(string name, bool enable)
|
||||
{
|
||||
if (actionDisable) return;
|
||||
if (enable)
|
||||
{
|
||||
var result = await ScriptTaskHub.Resume(name);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
OnTaskPaused(name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = await ScriptTaskHub.Pause(name);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
OnTaskResumed(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
ProcessorHub.StateChanged -= OnProcessorStateChanged;
|
||||
ProcessorHub.ConnectionStateChanged -= OnConnectionStateChanged;
|
||||
ScriptTaskHub.TaskResumed -= OnTaskResumed;
|
||||
ScriptTaskHub.TaskPaused -= OnTaskPaused;
|
||||
await ScriptTaskHub.StartAsync();
|
||||
}
|
||||
}
|
||||
185
RobotNet.WebApp/Scripts/Components/Dashboards/Variables.razor
Normal file
185
RobotNet.WebApp/Scripts/Components/Dashboards/Variables.razor
Normal file
@@ -0,0 +1,185 @@
|
||||
@implements IDisposable
|
||||
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using RobotNet.Clients
|
||||
@using RobotNet.Shares
|
||||
|
||||
@inject IHttpClientFactory httpFactory
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
|
||||
<div class="flex-grow-1 h-100 position-relative overflow-hidden">
|
||||
<div @ref="divRef" class="w-100 h-100 position-absolute top-0 start-0 overflow-hidden">
|
||||
<MudTable Style="margin: 10px;" Items="_items" Height="@TableHeight" FixedHeader Virtualize Hover Elevation="6">
|
||||
<ToolBarContent>
|
||||
<div @ref="toolbarRef" class="w-100 d-flex flex-row">
|
||||
<h3>Global Variables</h3>
|
||||
<MudSpacer />
|
||||
<ActionButton Icon="mdi-refresh text-primary" Large Tooltip="Reset" OnClick="ReloadData" />
|
||||
</div>
|
||||
</ToolBarContent>
|
||||
<ColGroup>
|
||||
<col style="width:60px;" />
|
||||
<col />
|
||||
<col />
|
||||
<col />
|
||||
<col style="width:90px;" />
|
||||
</ColGroup>
|
||||
<HeaderContent>
|
||||
<MudTh></MudTh>
|
||||
<MudTh>Name</MudTh>
|
||||
<MudTh>Type</MudTh>
|
||||
<MudTh>Value</MudTh>
|
||||
<MudTh></MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd>@context.Index</MudTd>
|
||||
<MudTd DataLabel="Name">@context.Name</MudTd>
|
||||
<MudTd DataLabel="Type">@context.TypeName</MudTd>
|
||||
<MudTd DataLabel="Value">@context.Value</MudTd>
|
||||
<MudTd>
|
||||
<span>
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Edit" Color="Color.Primary" Size="Size.Small"
|
||||
Disabled="@CallDisabled" OnClick="@(() => ShowSetValueDialog(context))" />
|
||||
</span>
|
||||
<span>
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Refresh" Color="Color.Secondary" Size="Size.Small"
|
||||
Disabled="@CallDisabled" OnClick="@(() => ResetValue(context))" />
|
||||
</span>
|
||||
</MudTd>
|
||||
</RowTemplate>
|
||||
</MudTable>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ProcessorHubClient ProcessorHub { get; set; } = null!;
|
||||
|
||||
private string TableHeight = "0px";
|
||||
private ElementReference divRef;
|
||||
private ElementReference toolbarRef;
|
||||
|
||||
private HttpClient http = default!;
|
||||
private List<ScriptVariableModel> _items = [];
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
var rect = await divRef.MudGetBoundingClientRectAsync();
|
||||
var toolbarRect = await toolbarRef.MudGetBoundingClientRectAsync();
|
||||
TableHeight = $"{rect.Height - 25 - Math.Max(toolbarRect.Height, 64)}px";
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
http = httpFactory.CreateClient("ScriptManagerAPI");
|
||||
await ReloadData();
|
||||
|
||||
if (ProcessorHub.IsConnected)
|
||||
{
|
||||
OnProcessorStateChanged(await ProcessorHub.GetState());
|
||||
}
|
||||
|
||||
ProcessorHub.StateChanged += OnProcessorStateChanged;
|
||||
ProcessorHub.ConnectionStateChanged += OnConnectionStateChanged;
|
||||
}
|
||||
|
||||
|
||||
private void OnConnectionStateChanged(HubConnectionState state)
|
||||
{
|
||||
if (state == HubConnectionState.Connected)
|
||||
{
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
OnProcessorStateChanged(await ProcessorHub.GetState());
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private bool CallDisabled = true;
|
||||
private void OnProcessorStateChanged(ProcessorState state)
|
||||
{
|
||||
if (state == ProcessorState.Ready)
|
||||
{
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
await ReloadData();
|
||||
});
|
||||
}
|
||||
|
||||
if (CallDisabled)
|
||||
{
|
||||
if (state == ProcessorState.Running)
|
||||
{
|
||||
CallDisabled = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state != ProcessorState.Running)
|
||||
{
|
||||
CallDisabled = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReloadData()
|
||||
{
|
||||
_items.Clear();
|
||||
|
||||
var vars = await http.GetFromJsonAsync<IEnumerable<ScriptVariableDto>>("/api/ScriptVariables") ?? [];
|
||||
foreach (var item in vars.Select((value, index) => (value, index)))
|
||||
{
|
||||
_items.Add(new ScriptVariableModel(item.index + 1, item.value));
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task ResetValue(ScriptVariableModel model)
|
||||
{
|
||||
if (CallDisabled) return;
|
||||
|
||||
var result = await http.PutFromJsonAsync<MessageResult>($"/api/ScriptVariables/{model.Name}/Reset", new UpdateVariableModel(model.Name, ""));
|
||||
if(result == null)
|
||||
{
|
||||
Snackbar.Add("Failed to reset variable value: server response null", Severity.Error);
|
||||
}
|
||||
else if (result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Success);
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add($"Failed to reset variable value: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ShowSetValueDialog(ScriptVariableModel model)
|
||||
{
|
||||
|
||||
if (CallDisabled) return;
|
||||
|
||||
var parameters = new DialogParameters<SetVariableValueDialog>()
|
||||
{
|
||||
{x => x.Model, model},
|
||||
};
|
||||
|
||||
var dialog = await DialogService.ShowAsync<SetVariableValueDialog>("Set Variable Value", parameters);
|
||||
var resultDialog = await dialog.Result;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ProcessorHub.StateChanged -= OnProcessorStateChanged;
|
||||
ProcessorHub.ConnectionStateChanged -= OnConnectionStateChanged;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">
|
||||
@Title
|
||||
</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<div class="d-flex flex-column" style="width: 500px;">
|
||||
<MudTextField @bind-Value="FileName" Label="@Label" Class="mb-3" ShrinkLabel Variant="Variant.Outlined"
|
||||
DebounceInterval="500" OnDebounceIntervalElapsed="ValidateName" OnBlur="ValidateName"
|
||||
Error="@NameError" ErrorText="@NameErrorText" Adornment="@AdornmentVisible" AdornmentText=".cs" />
|
||||
</div>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="OnCancel">Cancel</MudButton>
|
||||
<MudButton Color="Color.Primary" OnClick="OnCreate" Disabled="@CreateButtonDisable">Create</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private IMudDialogInstance Dialog { get; set; } = null!;
|
||||
|
||||
[Parameter]
|
||||
public string FolderName { get; set; } = "";
|
||||
|
||||
[Parameter]
|
||||
public bool IsFolder { get; set; } = false;
|
||||
|
||||
private string Title => $"Tạo mới {(IsFolder ? "folder" : "file code")}";
|
||||
private string Label => $"/{FolderName}";
|
||||
private Adornment AdornmentVisible => IsFolder ? Adornment.None : Adornment.End;
|
||||
|
||||
private string FileName { get; set; } = "";
|
||||
|
||||
private string NameErrorText = "";
|
||||
private bool NameError = false;
|
||||
|
||||
private bool CreateButtonDisable => NameError;
|
||||
|
||||
private void OnCancel() => Dialog.Cancel();
|
||||
|
||||
public void OnCreate()
|
||||
{
|
||||
ValidateName();
|
||||
|
||||
if (NameError)
|
||||
{
|
||||
Snackbar.Add("Tên chưa đúng!", Severity.Error);
|
||||
}
|
||||
else
|
||||
{
|
||||
Dialog.Close(DialogResult.Ok(FileName));
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateName()
|
||||
{
|
||||
NameErrorText = string.IsNullOrEmpty(FileName) ? "Tên không được để trống" : "";
|
||||
NameError = !string.IsNullOrEmpty(NameErrorText);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">
|
||||
Danh sách script sẽ xóa
|
||||
</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<div class="d-flex flex-column ms-3 overflow-y-scroll overflow-x-auto" style="height: 200px; width: 500px;">
|
||||
@foreach (var file in DeleteList)
|
||||
{
|
||||
<span>
|
||||
@file
|
||||
</span>
|
||||
}
|
||||
</div>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="OnCancel">Cancel</MudButton>
|
||||
<MudButton Color="Color.Primary" OnClick="OnDelete">Delete</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private IMudDialogInstance Dialog { get; set; } = null!;
|
||||
|
||||
[Parameter]
|
||||
public ScriptWorkspace Workspace { get; set; } = null!;
|
||||
|
||||
private IEnumerable<string> DeleteList = [];
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
|
||||
if (Workspace.SelectedFile is not null)
|
||||
{
|
||||
DeleteList = [Workspace.SelectedFile.Path];
|
||||
StateHasChanged();
|
||||
}
|
||||
else if (Workspace.SelectedFolder is not null)
|
||||
{
|
||||
DeleteList = GetListFromFolder(Workspace.SelectedFolder);
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private IEnumerable<string> GetListFromFolder(ScriptFolderModel folder)
|
||||
{
|
||||
var list = new List<string>();
|
||||
list.Add(folder.Path);
|
||||
|
||||
foreach (var dir in folder.Folders)
|
||||
{
|
||||
list.AddRange(GetListFromFolder(dir));
|
||||
}
|
||||
|
||||
foreach (var file in folder.Files)
|
||||
{
|
||||
list.Add(file.Path);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private void OnCancel() => Dialog.Cancel();
|
||||
|
||||
private async Task OnDelete()
|
||||
{
|
||||
if (Workspace.SelectedFile is not null)
|
||||
{
|
||||
var result = await Workspace.DeleteFile(Workspace.SelectedFile);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
Dialog.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
else if (Workspace.SelectedFolder is not null)
|
||||
{
|
||||
var result = await Workspace.DeleteFolder(Workspace.SelectedFolder);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
Dialog.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">
|
||||
@Title
|
||||
</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<div class="d-flex flex-column" style="width: 500px;">
|
||||
<MudTextField @bind-Value="FileName" Label="@Label" Class="mb-3" ShrinkLabel Variant="Variant.Outlined"
|
||||
DebounceInterval="500" OnDebounceIntervalElapsed="ValidateName" OnBlur="ValidateName"
|
||||
Error="@NameError" ErrorText="@NameErrorText" Adornment="@AdornmentVisible" AdornmentText=".cs" />
|
||||
</div>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="OnCancel">Cancel</MudButton>
|
||||
<MudButton Color="Color.Primary" OnClick="OnRename" Disabled="@RenameDisabled">Rename</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
|
||||
[CascadingParameter]
|
||||
private IMudDialogInstance Dialog { get; set; } = null!;
|
||||
|
||||
[Parameter]
|
||||
public ScriptWorkspace Workspace { get; set; } = null!;
|
||||
|
||||
private bool IsFolder;
|
||||
private string Label = "";
|
||||
private string Title => IsFolder ? "Rename folder" : "Rename file";
|
||||
private string NameErrorText = "";
|
||||
private bool NameError = false;
|
||||
private string FileName = "";
|
||||
private string OldName = "";
|
||||
private bool RenameDisabled => FileName == OldName;
|
||||
private Adornment AdornmentVisible => IsFolder ? Adornment.None : Adornment.End;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
if (Workspace.SelectedFile is not null)
|
||||
{
|
||||
IsFolder = false;
|
||||
Label = Workspace.SelectedFile.Parrent?.Name ?? "/";
|
||||
FileName = Workspace.SelectedFile.Name;
|
||||
if (FileName.EndsWith(".cs"))
|
||||
{
|
||||
FileName = FileName.Substring(0, FileName.Length - 3);
|
||||
}
|
||||
OldName = FileName;
|
||||
}
|
||||
else if (Workspace.SelectedFolder is not null)
|
||||
{
|
||||
IsFolder = true;
|
||||
Label = Workspace.SelectedFolder.Parrent?.Name ?? "/";
|
||||
FileName = Workspace.SelectedFolder.Name;
|
||||
OldName = FileName;
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateName()
|
||||
{
|
||||
NameErrorText = string.IsNullOrEmpty(FileName) ? "Tên không được để trống" : "";
|
||||
NameError = !string.IsNullOrEmpty(NameErrorText);
|
||||
}
|
||||
|
||||
private void OnCancel() => Dialog.Cancel();
|
||||
|
||||
private async Task OnRename()
|
||||
{
|
||||
ValidateName();
|
||||
|
||||
if (NameError)
|
||||
{
|
||||
Snackbar.Add("Tên chưa đúng!", Severity.Error);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Workspace.SelectedFile is not null)
|
||||
{
|
||||
var result = await Workspace.Rename(Workspace.SelectedFile, FileName.EndsWith(".cs") ? FileName : FileName + ".cs");
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
Dialog.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
else if (Workspace.SelectedFolder is not null)
|
||||
{
|
||||
var result = await Workspace.Rename(Workspace.SelectedFolder, FileName);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
Dialog.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
262
RobotNet.WebApp/Scripts/Components/Editor.razor
Normal file
262
RobotNet.WebApp/Scripts/Components/Editor.razor
Normal file
@@ -0,0 +1,262 @@
|
||||
@implements IAsyncDisposable
|
||||
|
||||
@using Microsoft.AspNetCore.Components
|
||||
@using Microsoft.CodeAnalysis
|
||||
@using Microsoft.CodeAnalysis.Text
|
||||
@using Microsoft.JSInterop
|
||||
@using RobotNet.WebApp.Scripts.Monaco
|
||||
@using RobotNet.WebApp.Scripts.Monaco.Editor
|
||||
@using RobotNet.WebApp.Scripts.Monaco.Languages
|
||||
@using System.Timers
|
||||
|
||||
@inject IJSRuntime JsRuntime
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<div class="w-100 h-100 d-flex flex-column" tabindex="0" @onkeyup:preventDefault @onkeyup:stopPropagation @onkeyup="OnKeyPress">
|
||||
<div class="w-100 d-flex align-items-center justify-content-between">
|
||||
<div></div>
|
||||
<div>@NameFile</div>
|
||||
<div></div>
|
||||
</div>
|
||||
<div @ref=EditorContainer class="flex-grow-1 w-100"></div>
|
||||
</div>
|
||||
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ScriptWorkspace Workspace { get; set; } = null!;
|
||||
|
||||
private object dotNetHelper = default!;
|
||||
private IJSInProcessObjectReference? editor;
|
||||
private IJSInProcessObjectReference? editorModel;
|
||||
private IJSInProcessObjectReference? disposableCompletionItemProvider;
|
||||
private IJSInProcessObjectReference? disposableSignatureHelpProvider;
|
||||
private IJSInProcessObjectReference? disposableHoverProvider;
|
||||
|
||||
private bool IsEditorInitialized;
|
||||
private string Code = "";
|
||||
private string NameFile = "";
|
||||
public ProcessorState State { get; private set; } = ProcessorState.Idle;
|
||||
private bool IsReadOnly = true;
|
||||
|
||||
private ElementReference EditorContainer;
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
dotNetHelper = DotNetObjectReference.Create(this);
|
||||
|
||||
editor = await JsRuntime.InvokeAsync<IJSInProcessObjectReference>("monaco.editor.create", EditorContainer, new
|
||||
{
|
||||
Value = "",
|
||||
Language = "csharp",
|
||||
Theme = "vs-dark",
|
||||
GlyphMargin = true,
|
||||
AutomaticLayout = true,
|
||||
ReadOnly = IsReadOnly,
|
||||
});
|
||||
|
||||
editorModel = await editor.InvokeAsync<IJSInProcessObjectReference>("getModel");
|
||||
|
||||
await JsRuntime.InvokeVoidAsync("MonacoRuntime.OnCodeEditorDidChangeModelContent", editor, dotNetHelper, nameof(CodeEditorDidChangeModelContent));
|
||||
disposableCompletionItemProvider = await JsRuntime.InvokeAsync<IJSInProcessObjectReference>("MonacoRuntime.CSharpLanguageRegisterCompletionItemProvider", dotNetHelper, nameof(GetCompletionItems));
|
||||
disposableSignatureHelpProvider = await JsRuntime.InvokeAsync<IJSInProcessObjectReference>("MonacoRuntime.CSharpLanguageRegisterSignatureHelpProvider", dotNetHelper, nameof(GetSignatureHelp));
|
||||
disposableHoverProvider = await JsRuntime.InvokeAsync<IJSInProcessObjectReference>("MonacoRuntime.CSharpLanguageRegisterHoverProvider", dotNetHelper, nameof(GetQuickInfo));
|
||||
IsEditorInitialized = true;
|
||||
|
||||
Workspace.OnFileChanged += FileChanged;
|
||||
Workspace.OnDiagnoticChanged += DiagnoticChanged;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnProcessStateChanged(ProcessorState state)
|
||||
{
|
||||
State = state;
|
||||
var isReadOnly = State != ProcessorState.Ready && State != ProcessorState.Idle && State != ProcessorState.BuildError;
|
||||
if (isReadOnly != IsReadOnly)
|
||||
{
|
||||
IsReadOnly = isReadOnly;
|
||||
if (editor != null)
|
||||
{
|
||||
_ = InvokeAsync(async Task () =>
|
||||
{
|
||||
await editor.InvokeVoidAsync("updateOptions", new
|
||||
{
|
||||
ReadOnly = IsReadOnly,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task FileChanged(ScriptFileModel? file)
|
||||
{
|
||||
await this.InvokeAsync(async Task () =>
|
||||
{
|
||||
if (file is null)
|
||||
{
|
||||
NameFile = "";
|
||||
Code = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
NameFile = file.Name;
|
||||
Code = file.EditCode;
|
||||
}
|
||||
if (IsEditorInitialized && editor is not null)
|
||||
{
|
||||
await editor.InvokeVoidAsync("setValue", Code);
|
||||
await JsRuntime.InvokeVoidAsync("monaco.editor.setModelMarkers", editorModel, "default", file?.Diagnostics.Select(ToMonacoDiagnostic) ?? []);
|
||||
// await editor.InvokeVoidAsync("updateOptions", new
|
||||
// {
|
||||
// ReadOnly = IsReadOnly,
|
||||
// });
|
||||
}
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
private async Task DiagnoticChanged(IEnumerable<Diagnostic> diagnostics)
|
||||
{
|
||||
await this.InvokeAsync(async Task () =>
|
||||
{
|
||||
if (IsEditorInitialized && editor is not null)
|
||||
{
|
||||
await JsRuntime.InvokeVoidAsync("monaco.editor.setModelMarkers", editorModel, "default", diagnostics.Select(ToMonacoDiagnostic) ?? []);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
[JSInvokable]
|
||||
public async Task<CompletionList> GetCompletionItems(int line, int column, int kind, char? triggerCharacter)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Suggestions = [.. await Workspace.GetCompletionCurrentFileAsync(line, column, kind, triggerCharacter)],
|
||||
};
|
||||
}
|
||||
|
||||
[JSInvokable]
|
||||
public async Task<string?> GetQuickInfo(int line, int column)
|
||||
{
|
||||
return await Workspace.GetQuickInfoCurrentFile(line, column);
|
||||
}
|
||||
|
||||
[JSInvokable]
|
||||
public async Task<SignatureHelpResult?> GetSignatureHelp(int line, int column)
|
||||
{
|
||||
return await Workspace.GetSignatureHelpCurrentFile(line, column);
|
||||
}
|
||||
|
||||
[JSInvokable]
|
||||
public async Task CodeEditorDidChangeModelContent(ModelContentChangedEvent e)
|
||||
{
|
||||
if (e.IsFlush) return;
|
||||
|
||||
if (e.Changes.Length != 0)
|
||||
{
|
||||
if (editor is not null)
|
||||
{
|
||||
var code = await editor.InvokeAsync<string>("getValue");
|
||||
|
||||
if (code != Code)
|
||||
{
|
||||
Code = code;
|
||||
Workspace.WriteDocument(Code);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isSaving = false;
|
||||
private async Task OnKeyPress(KeyboardEventArgs e)
|
||||
{
|
||||
if ((e.Key == "s" || e.Key == "S") && e.CtrlKey)
|
||||
{
|
||||
if(IsReadOnly)
|
||||
{
|
||||
Snackbar.Add($"Hệ thống đang ở trạng thái {State}, Không thể thay đổi script", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
if (isSaving) return;
|
||||
isSaving = true;
|
||||
var result = await Workspace.SaveCurrentFileAsync();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
isSaving = false;
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (disposableCompletionItemProvider != null)
|
||||
{
|
||||
await disposableCompletionItemProvider.InvokeVoidAsync("dispose");
|
||||
await disposableCompletionItemProvider.DisposeAsync();
|
||||
disposableCompletionItemProvider = null;
|
||||
}
|
||||
|
||||
if (disposableSignatureHelpProvider != null)
|
||||
{
|
||||
await disposableSignatureHelpProvider.InvokeVoidAsync("dispose");
|
||||
await disposableSignatureHelpProvider.DisposeAsync();
|
||||
disposableSignatureHelpProvider = null;
|
||||
}
|
||||
|
||||
if (disposableHoverProvider != null)
|
||||
{
|
||||
await disposableHoverProvider.InvokeVoidAsync("dispose");
|
||||
await disposableHoverProvider.DisposeAsync();
|
||||
disposableHoverProvider = null;
|
||||
}
|
||||
|
||||
if (editorModel != null)
|
||||
{
|
||||
await editorModel.InvokeVoidAsync("dispose");
|
||||
await editorModel.DisposeAsync();
|
||||
editorModel = null;
|
||||
}
|
||||
|
||||
if (editor != null)
|
||||
{
|
||||
await editor.InvokeVoidAsync("dispose");
|
||||
await editor.DisposeAsync();
|
||||
editor = null;
|
||||
}
|
||||
|
||||
if (IsEditorInitialized)
|
||||
{
|
||||
Workspace.OnFileChanged -= FileChanged;
|
||||
Workspace.OnDiagnoticChanged -= DiagnoticChanged;
|
||||
}
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
|
||||
private static MarkerData ToMonacoDiagnostic(Diagnostic diagnostic)
|
||||
{
|
||||
var lineSpan = diagnostic.Location.GetLineSpan();
|
||||
return new()
|
||||
{
|
||||
StartLineNumber = lineSpan.StartLinePosition.Line + 1,
|
||||
StartColumn = lineSpan.StartLinePosition.Character + 1,
|
||||
EndLineNumber = lineSpan.EndLinePosition.Line + 1,
|
||||
EndColumn = lineSpan.EndLinePosition.Character + 1,
|
||||
Message = diagnostic.GetMessage(),
|
||||
Severity = diagnostic.Severity switch
|
||||
{
|
||||
DiagnosticSeverity.Info => MarkerSeverity.Info,
|
||||
DiagnosticSeverity.Warning => MarkerSeverity.Warning,
|
||||
DiagnosticSeverity.Error => MarkerSeverity.Error,
|
||||
_ => MarkerSeverity.Hint,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
113
RobotNet.WebApp/Scripts/Components/EditorHierachy.razor
Normal file
113
RobotNet.WebApp/Scripts/Components/EditorHierachy.razor
Normal file
@@ -0,0 +1,113 @@
|
||||
@using RobotNet.WebApp.Scripts.Components.Dialogs
|
||||
@implements IDisposable
|
||||
|
||||
@inject IJSRuntime JsRuntime
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
|
||||
<MudMenu Class="w-100 h-100" Dense ActivationEvent="@MouseEvent.RightClick" Size="@Size.Small" PositionAtCursor>
|
||||
<ActivatorContent>
|
||||
<div class="editor-hierachy" @onclick="ResetSelect">
|
||||
<div>
|
||||
<CascadingValue Value="ItemRadioName" Name="HierachyItemRadioName">
|
||||
@foreach (var model in Workspace.Folders)
|
||||
{
|
||||
<FolderItem Model="model" />
|
||||
}
|
||||
@foreach (var model in Workspace.Files)
|
||||
{
|
||||
<FileItem Model="model" />
|
||||
}
|
||||
</CascadingValue>
|
||||
</div>
|
||||
</div>
|
||||
</ActivatorContent>
|
||||
<ChildContent>
|
||||
<MudMenuItem Label="Save" OnClick="SaveSelect" />
|
||||
<MudMenuItem Label="Rename" OnClick="RenameSelect" />
|
||||
<MudDivider />
|
||||
<MudMenuItem Label="Delete" Class="text-danger" OnClick="DeleteSelect" />
|
||||
</ChildContent>
|
||||
</MudMenu>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ScriptWorkspace Workspace { get; set; } = null!;
|
||||
|
||||
readonly string ItemRadioName = $"script-{Guid.NewGuid()}";
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
Workspace.RootChanged += OnRootChanged;
|
||||
}
|
||||
|
||||
private void OnRootChanged()
|
||||
{
|
||||
this.InvokeAsync(() =>
|
||||
{
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
private async Task ResetSelect()
|
||||
{
|
||||
Workspace.SelectFolder(null);
|
||||
await JsRuntime.InvokeVoidAsync("UncheckRadioAll", ItemRadioName);
|
||||
}
|
||||
|
||||
private async Task SaveSelect()
|
||||
{
|
||||
if (Workspace.IsReadOnly)
|
||||
{
|
||||
Snackbar.Add($"Hệ thống đang ở trạng thái {Workspace.ProcessorState}, Không thể thay đổi script", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
var results = await Workspace.SaveSelectAsync();
|
||||
foreach (var result in results)
|
||||
{
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DeleteSelect()
|
||||
{
|
||||
if (Workspace.IsReadOnly)
|
||||
{
|
||||
Snackbar.Add($"Hệ thống đang ở trạng thái {Workspace.ProcessorState}, Không thể thay đổi script", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
// Truyền Workspace vào DeleteFileOrFolderDialog qua parameters
|
||||
var parameters = new DialogParameters
|
||||
{
|
||||
{ "Workspace", Workspace }
|
||||
};
|
||||
|
||||
await DialogService.ShowAsync<DeleteFileOrFolderDialog>("", parameters: parameters);
|
||||
}
|
||||
|
||||
private async Task RenameSelect()
|
||||
{
|
||||
if (Workspace.IsReadOnly)
|
||||
{
|
||||
Snackbar.Add($"Hệ thống đang ở trạng thái {Workspace.ProcessorState}, Không thể thay đổi script", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
var parameters = new DialogParameters
|
||||
{
|
||||
{ "Workspace", Workspace }
|
||||
};
|
||||
|
||||
await DialogService.ShowAsync<RenameFileOrFolderDialog>("", parameters: parameters);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Workspace.RootChanged -= OnRootChanged;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
20
RobotNet.WebApp/Scripts/Components/EditorHierachy.razor.css
Normal file
20
RobotNet.WebApp/Scripts/Components/EditorHierachy.razor.css
Normal file
@@ -0,0 +1,20 @@
|
||||
.editor-hierachy {
|
||||
width: 100%;
|
||||
flex-grow: 1;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
background-color: rgb(30, 30, 30);
|
||||
}
|
||||
|
||||
.editor-hierachy > div {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
position: absolute;
|
||||
top: 0px;
|
||||
left: 0px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.editor-hierachy > div:hover {
|
||||
overflow: auto;
|
||||
}
|
||||
70
RobotNet.WebApp/Scripts/Components/FileItem.razor
Normal file
70
RobotNet.WebApp/Scripts/Components/FileItem.razor
Normal file
@@ -0,0 +1,70 @@
|
||||
@implements IDisposable
|
||||
|
||||
@inject IJSRuntime JsRuntime
|
||||
|
||||
<div class="file-item" @onclick:stopPropagation @onclick:preventDefault @onclick="Click" @oncontextmenu="Click">
|
||||
<input id="@ItemId" type="radio" name="@RadioName" hidden>
|
||||
@for (int i = 0; i < Model.Level; i++)
|
||||
{
|
||||
<div class="indent-guide"></div>
|
||||
}
|
||||
<div class="file-icon">
|
||||
<span class="mdi @Icon"></span>
|
||||
</div>
|
||||
<HierachyItemName Model="Model" />
|
||||
<HierachyItemDiagnostics Model="Model" />
|
||||
<HierachyItemModified Model="Model" />
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ScriptWorkspace Workspace { get; set; } = null!;
|
||||
|
||||
[CascadingParameter(Name = "HierachyItemRadioName")]
|
||||
protected string RadioName { get; set; } = "script-explorer-item";
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public ScriptFileModel Model { get; set; } = default!;
|
||||
|
||||
Guid ItemId = Guid.NewGuid();
|
||||
|
||||
private string Icon => "mdi-language-csharp";
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
base.OnAfterRender(firstRender);
|
||||
if (!firstRender) return;
|
||||
|
||||
Model.OnModified += OnModified;
|
||||
Model.OnDiagnosticsChanged += OnDiagnosticsChanged;
|
||||
}
|
||||
|
||||
private void OnModified()
|
||||
{
|
||||
this.InvokeAsync(() =>
|
||||
{
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
private void OnDiagnosticsChanged(int warningCount, int errorCount)
|
||||
{
|
||||
this.InvokeAsync(() =>
|
||||
{
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
private async Task Click()
|
||||
{
|
||||
await JsRuntime.InvokeVoidAsync("SetRadioChecked", ItemId);
|
||||
await Workspace.SelectFile(this.Model);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Model.OnModified -= OnModified;
|
||||
Model.OnDiagnosticsChanged -= OnDiagnosticsChanged;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
44
RobotNet.WebApp/Scripts/Components/FileItem.razor.css
Normal file
44
RobotNet.WebApp/Scripts/Components/FileItem.razor.css
Normal file
@@ -0,0 +1,44 @@
|
||||
.file-item {
|
||||
width: 100%;
|
||||
height: 22px;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.file-item:hover {
|
||||
background-color: rgba(0,0,0,0.3);
|
||||
}
|
||||
|
||||
.file-item:active {
|
||||
background-color: transparent !important;
|
||||
}
|
||||
|
||||
.file-item:has(input:checked) {
|
||||
background-color: rgb(55, 55, 61);
|
||||
}
|
||||
|
||||
.file-item:has(input:checked):focus {
|
||||
background-color: rgb(4, 57, 94);
|
||||
outline-color: rgb(0, 120, 212);
|
||||
}
|
||||
|
||||
.indent-guide {
|
||||
border-right: 1px solid rgba(88,88,88,0.4);
|
||||
width: 8px;
|
||||
display: inline;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.file-icon {
|
||||
display: flex;
|
||||
height: 16px;
|
||||
width: 16px;
|
||||
font-size: 16px;
|
||||
overflow: hidden;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-right: 6px;
|
||||
margin-left: 6px;
|
||||
color: #519aba;
|
||||
}
|
||||
96
RobotNet.WebApp/Scripts/Components/FolderItem.razor
Normal file
96
RobotNet.WebApp/Scripts/Components/FolderItem.razor
Normal file
@@ -0,0 +1,96 @@
|
||||
@implements IDisposable
|
||||
|
||||
@inject IJSRuntime JsRuntime
|
||||
|
||||
<div class="folder-item" @onclick:stopPropagation @onclick:preventDefault @onclick="Click" @oncontextmenu="Click">
|
||||
<input id="@ItemId" type="radio" name="@RadioName" hidden>
|
||||
@for (int i = 0; i < Model.Level; i++)
|
||||
{
|
||||
<div class="indent-guide"></div>
|
||||
}
|
||||
<div class="folder-icon">
|
||||
<span class="mdi @Icon"></span>
|
||||
</div>
|
||||
<HierachyItemName Model="Model" />
|
||||
<HierachyItemDiagnostics Model="Model" />
|
||||
<HierachyItemModified Model="Model" />
|
||||
</div>
|
||||
|
||||
@if (IsExpaned)
|
||||
{
|
||||
@foreach (var model in Model.Folders)
|
||||
{
|
||||
<FolderItem Model="model" />
|
||||
}
|
||||
@foreach (var model in Model.Files)
|
||||
{
|
||||
<FileItem Model="model" />
|
||||
}
|
||||
}
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ScriptWorkspace Workspace { get; set; } = null!;
|
||||
|
||||
[CascadingParameter(Name = "HierachyItemRadioName")]
|
||||
protected string RadioName { get; set; } = "script-explorer-item";
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public ScriptFolderModel Model { get; set; } = default!;
|
||||
|
||||
Guid ItemId = Guid.NewGuid();
|
||||
|
||||
public bool IsExpaned { get; private set; }
|
||||
private string Icon => IsExpaned ? "mdi-chevron-down" : "mdi-chevron-right";
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
base.OnAfterRender(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
Model.OnChildrenChanged += OnChildrenChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnChildrenChanged()
|
||||
{
|
||||
this.InvokeAsync(() =>
|
||||
{
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
private async Task Click()
|
||||
{
|
||||
await JsRuntime.InvokeVoidAsync("SetRadioChecked", ItemId);
|
||||
|
||||
if (Workspace.SelectFolder(this.Model)) return;
|
||||
|
||||
if (IsExpaned)
|
||||
{
|
||||
Collapse();
|
||||
}
|
||||
else
|
||||
{
|
||||
Expand();
|
||||
}
|
||||
}
|
||||
|
||||
public void Collapse()
|
||||
{
|
||||
IsExpaned = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
public void Expand()
|
||||
{
|
||||
IsExpaned = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Model.OnChildrenChanged -= OnChildrenChanged;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
47
RobotNet.WebApp/Scripts/Components/FolderItem.razor.css
Normal file
47
RobotNet.WebApp/Scripts/Components/FolderItem.razor.css
Normal file
@@ -0,0 +1,47 @@
|
||||
.folder-item {
|
||||
width: 100%;
|
||||
height: 22px;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.folder-item:hover {
|
||||
background-color: rgba(0,0,0,0.3);
|
||||
}
|
||||
|
||||
.folder-item:active {
|
||||
background-color: transparent !important;
|
||||
}
|
||||
|
||||
.folder-item:has(input:checked) {
|
||||
background-color: rgb(55, 55, 61);
|
||||
}
|
||||
|
||||
.folder-item:has(input:checked):focus {
|
||||
background-color: rgb(4, 57, 94);
|
||||
outline-color: rgb(0, 120, 212);
|
||||
}
|
||||
|
||||
.indent-guide {
|
||||
border-right: 1px solid rgba(88,88,88,0.4);
|
||||
width: 8px;
|
||||
display: inline;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.folder-icon {
|
||||
display: flex;
|
||||
height: 16px;
|
||||
width: 16px;
|
||||
font-size: 16px;
|
||||
overflow: hidden;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-right: 6px;
|
||||
margin-left: 6px;
|
||||
}
|
||||
|
||||
.folder-icon .mdi-language-csharp {
|
||||
color: #519aba;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
@implements IDisposable
|
||||
|
||||
@if (Model.ErrorCount > 0)
|
||||
{
|
||||
<div class="folder-error">
|
||||
@ErrorStr
|
||||
</div>
|
||||
}
|
||||
@if (Model.WarningCount > 0)
|
||||
{
|
||||
<div class="folder-warnning">
|
||||
@WarningStr
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public IHierachyItemModel Model { get; set; } = default!;
|
||||
|
||||
private string ErrorStr => Model.ErrorCount < 10 ? Model.ErrorCount.ToString() : "9+";
|
||||
|
||||
private string WarningStr => Model.WarningCount < 10 ? Model.WarningCount.ToString() : "9+";
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
base.OnAfterRender(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
Model.OnDiagnosticsChanged += OnDiagnosticsChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDiagnosticsChanged(int warningCount, int errorCount)
|
||||
{
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Model.OnDiagnosticsChanged -= OnDiagnosticsChanged;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
.folder-error {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
color: white;
|
||||
font-size: 11px;
|
||||
margin-right: 3px;
|
||||
border-radius: 50%;
|
||||
background-color: firebrick;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.folder-warnning {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
color: white;
|
||||
font-size: 11px;
|
||||
margin-right: 3px;
|
||||
border-radius: 50%;
|
||||
background-color: darkgoldenrod;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
overflow: hidden;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
@if (Model.IsModified)
|
||||
{
|
||||
<div class="folder-modified">
|
||||
M
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public IHierachyItemModel Model { get; set; } = default!;
|
||||
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
base.OnAfterRender(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
Model.OnModified += StateHasChanged;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Model.OnModified -= StateHasChanged;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
.folder-modified {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
color: white;
|
||||
font-size: 11px;
|
||||
margin-right: 3px;
|
||||
border-radius: 3px;
|
||||
background-color: teal;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
overflow: hidden;
|
||||
}
|
||||
24
RobotNet.WebApp/Scripts/Components/HierachyItemName.razor
Normal file
24
RobotNet.WebApp/Scripts/Components/HierachyItemName.razor
Normal file
@@ -0,0 +1,24 @@
|
||||
<div class="folder-label">
|
||||
@Model.Name
|
||||
</div>
|
||||
|
||||
@code {
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public IHierachyItemModel Model { get; set; } = default!;
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
base.OnAfterRender(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
Model.OnNameChanged += StateHasChanged;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Model.OnNameChanged -= StateHasChanged;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
.folder-label {
|
||||
color: rgb(204,204,204);
|
||||
flex-grow: 1;
|
||||
font-size: 13px;
|
||||
user-select: none;
|
||||
}
|
||||
7
RobotNet.WebApp/Scripts/Components/Loading.razor
Normal file
7
RobotNet.WebApp/Scripts/Components/Loading.razor
Normal file
@@ -0,0 +1,7 @@
|
||||
<div class="w-100 h-100 d-flex justify-content-center align-items-center">
|
||||
<MudProgressCircular Color="Color.Info" Indeterminate="true" />
|
||||
</div>
|
||||
|
||||
@code {
|
||||
|
||||
}
|
||||
70
RobotNet.WebApp/Scripts/Components/ProcessorConsole.razor
Normal file
70
RobotNet.WebApp/Scripts/Components/ProcessorConsole.razor
Normal file
@@ -0,0 +1,70 @@
|
||||
@using Microsoft.CodeAnalysis
|
||||
|
||||
@implements IAsyncDisposable
|
||||
|
||||
@inject ConsoleHubClient consoleHub
|
||||
@inject IJSRuntime JS
|
||||
|
||||
<div class="w-100 h-100 position-relative overflow-hidden">
|
||||
<div @ref="ScrollViewDiv" class="w-100 h-100 position-absolute overflow-x-hidden overflow-y-scroll">
|
||||
<div class="ms-2" >
|
||||
@foreach(var console in Consoles)
|
||||
{
|
||||
<ConsoleItem Model="console" />
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
private ElementReference ScrollViewDiv;
|
||||
private List<ConsoleItemModel> Consoles = [];
|
||||
|
||||
public void AddInfo(string message) => AddMessage(DiagnosticSeverity.Info, message);
|
||||
public void AddWarning(string message) => AddMessage(DiagnosticSeverity.Warning, message);
|
||||
public void AddError(string message) => AddMessage(DiagnosticSeverity.Error, message);
|
||||
|
||||
public async void AddMessage(DiagnosticSeverity severity, string message)
|
||||
{
|
||||
if (Consoles.Any() && Consoles[^1].Severity == severity)
|
||||
{
|
||||
Consoles[^1].Append(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
Consoles.Add(new ConsoleItemModel(severity, message));
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
// Đợi UI cập nhật xong rồi scroll
|
||||
await Task.Yield();
|
||||
await JS.InvokeVoidAsync("ScrollToBottom", ScrollViewDiv);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Consoles.Clear();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
|
||||
consoleHub.MessageInfo += AddInfo;
|
||||
consoleHub.MessageWarning += AddWarning;
|
||||
consoleHub.MessageError += AddError;
|
||||
await consoleHub.StartAsync();
|
||||
await consoleHub.RegisterAllConsoles();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
consoleHub.MessageInfo -= AddInfo;
|
||||
consoleHub.MessageWarning -= AddWarning;
|
||||
consoleHub.MessageError -= AddError;
|
||||
await consoleHub.UnregisterAllConsoles();
|
||||
await consoleHub.StopAsync();
|
||||
}
|
||||
}
|
||||
172
RobotNet.WebApp/Scripts/Components/ProcessorControl.razor
Normal file
172
RobotNet.WebApp/Scripts/Components/ProcessorControl.razor
Normal file
@@ -0,0 +1,172 @@
|
||||
@implements IAsyncDisposable
|
||||
|
||||
@using Microsoft.CodeAnalysis
|
||||
|
||||
@inject ProcessorHubClient processorHub
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
|
||||
<div class="w-100 h-100 d-flex flex-column">
|
||||
<div class="d-flex flex-row-reverse">
|
||||
<div class="mx-1"></div>
|
||||
<ActionButton Icon="mdi-stop text-danger" Large Tooltip="Stop" Disabled="@StopDisable" OnClick="Stop" />
|
||||
<ActionButton Icon="mdi-play text-success" Large Tooltip="Run" Disabled="@RunDisable" OnClick="Run" />
|
||||
<ActionButton Icon="mdi-cog-play text-primary" Large Tooltip="Build" Disabled="@BuildDisable" OnClick="Build" />
|
||||
<ActionButton Icon="mdi-refresh text-primary" Large Tooltip="Reset" Disabled="@ResetDisable" OnClick="Reset" />
|
||||
<ActionButton Icon="mdi-receipt-text-remove" Large Tooltip="Clear console" OnClick="ClearConsole" />
|
||||
</div>
|
||||
<div class="w-100 flex-grow-1">
|
||||
<ProcessorConsole @ref="processorConsole" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ScriptWorkspace Workspace { get; set; } = null!;
|
||||
|
||||
private ProcessorConsole? processorConsole;
|
||||
public ProcessorState State { get; private set; } = ProcessorState.Idle;
|
||||
private ProcessorRequest Request = ProcessorRequest.None;
|
||||
|
||||
private bool ResetDisable => State != ProcessorState.Idle && State != ProcessorState.Ready && State != ProcessorState.Error && State != ProcessorState.BuildError;
|
||||
private bool BuildDisable => State != ProcessorState.Idle && State != ProcessorState.Ready && State != ProcessorState.Error && State != ProcessorState.BuildError;
|
||||
private bool RunDisable => State != ProcessorState.Ready;
|
||||
private bool StopDisable => State != ProcessorState.Running;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
processorHub.StateChanged += OnStateChanged;
|
||||
processorHub.RequestChanged += OnRequestChanged;
|
||||
await processorHub.StartAsync();
|
||||
|
||||
OnStateChanged(await processorHub.GetState());
|
||||
}
|
||||
|
||||
private void OnStateChanged(ProcessorState state)
|
||||
{
|
||||
if (State == state) return;
|
||||
|
||||
State = state;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void OnRequestChanged(ProcessorRequest request)
|
||||
{
|
||||
Request = request;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task Reset()
|
||||
{
|
||||
var result = await processorHub.Reset();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Failed to reset processor: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Build()
|
||||
{
|
||||
if (Workspace.AnyChanges())
|
||||
{
|
||||
bool? confirm = await DialogService.ShowMessageBox("Cảnh báo", "Bạn có muốn lưu lại tất cả thay đổi trước khi build không?", yesText: "Save All", cancelText: "Cancel");
|
||||
|
||||
if (confirm != true) return;
|
||||
|
||||
bool isError = false;
|
||||
var saveResults = await Workspace.SaveAllAsync();
|
||||
foreach (var saveResult in saveResults)
|
||||
{
|
||||
if (!saveResult.IsSuccess)
|
||||
{
|
||||
if (processorConsole != null)
|
||||
{
|
||||
processorConsole.AddError(saveResult.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add(saveResult.Message, Severity.Error);
|
||||
}
|
||||
isError = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isError)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
await Task.Delay(500);
|
||||
}
|
||||
}
|
||||
|
||||
var diagnostics = await Workspace.GetAllDiagnostics();
|
||||
if (diagnostics.Any(d => d.Severity == DiagnosticSeverity.Warning || d.Severity == DiagnosticSeverity.Error))
|
||||
{
|
||||
foreach (var diagnostic in diagnostics)
|
||||
{
|
||||
if (diagnostic.Severity == DiagnosticSeverity.Warning)
|
||||
{
|
||||
if (processorConsole != null)
|
||||
{
|
||||
processorConsole.AddWarning($"File /{diagnostic}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add($"File /{diagnostic}", Severity.Warning);
|
||||
}
|
||||
}
|
||||
else if (diagnostic.Severity == DiagnosticSeverity.Error)
|
||||
{
|
||||
if (processorConsole != null)
|
||||
{
|
||||
processorConsole.AddError($"File /{diagnostic}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add($"File /{diagnostic}", Severity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = await processorHub.Build();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Failed to build processor: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Run()
|
||||
{
|
||||
var result = await processorHub.Run();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Failed to run processor: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Stop()
|
||||
{
|
||||
var result = await processorHub.Stop();
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add($"Failed to stop processor: {result.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearConsole()
|
||||
{
|
||||
processorConsole?.Clear();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
processorHub.StateChanged -= OnStateChanged;
|
||||
processorHub.RequestChanged -= OnRequestChanged;
|
||||
await processorHub.StopAsync();
|
||||
}
|
||||
}
|
||||
75
RobotNet.WebApp/Scripts/Components/SidebarActions.razor
Normal file
75
RobotNet.WebApp/Scripts/Components/SidebarActions.razor
Normal file
@@ -0,0 +1,75 @@
|
||||
@using RobotNet.WebApp.Scripts.Components.Dialogs
|
||||
|
||||
@inject IDialogService DialogService
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<div class="sidebar-actions">
|
||||
<span></span>
|
||||
<div class="d-flex flex-row">
|
||||
<ActionButton Icon="mdi-content-save-all text-primary" Tooltip="Save all" OnClick="SaveAllFiles" />
|
||||
<ActionButton Icon="mdi-file-plus-outline" Tooltip="New file" OnClick="OpenDialogCreateNewFile" />
|
||||
<ActionButton Icon="mdi-folder-plus-outline" Tooltip="New folder" OnClick="OpenDialogCreateNewFolder" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
private ScriptWorkspace Workspace { get; set; } = null!;
|
||||
|
||||
private async Task OpenDialogCreateNewFile()
|
||||
{
|
||||
var parentPath = Workspace.SelectedFolder?.Path ?? "";
|
||||
|
||||
var parameters = new DialogParameters<CreateNewFileOrFolderDialog>()
|
||||
{
|
||||
{x => x.FolderName, parentPath},
|
||||
};
|
||||
|
||||
var dialog = await DialogService.ShowAsync<CreateNewFileOrFolderDialog>("Create new file", parameters);
|
||||
var resultDialog = await dialog.Result;
|
||||
|
||||
if (resultDialog is null || resultDialog.Canceled || resultDialog.Data is not string fileName) return;
|
||||
|
||||
fileName = fileName.EndsWith(".cs") ? fileName : fileName + ".cs";
|
||||
var result = await Workspace.CreateNewFile(fileName);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
private async Task OpenDialogCreateNewFolder()
|
||||
{
|
||||
|
||||
var parentPath = Workspace.SelectedFolder?.Path ?? "";
|
||||
var parameters = new DialogParameters<CreateNewFileOrFolderDialog>()
|
||||
{
|
||||
{x => x.FolderName, parentPath},
|
||||
{x => x.IsFolder, true },
|
||||
};
|
||||
|
||||
var dialog = await DialogService.ShowAsync<CreateNewFileOrFolderDialog>("Create new folder", parameters);
|
||||
var resultDialog = await dialog.Result;
|
||||
|
||||
if (resultDialog is null || resultDialog.Canceled || resultDialog.Data is not string folderName) return;
|
||||
|
||||
var result = await Workspace.CreateNewFolder(folderName);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveAllFiles()
|
||||
{
|
||||
var results = await Workspace.SaveAllAsync();
|
||||
foreach(var result in results)
|
||||
{
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Snackbar.Add(result.Message, Severity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
.sidebar-actions {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 5px 5px;
|
||||
border-bottom: 1px solid rgba(128, 128, 128, 0.3);
|
||||
}
|
||||
3
RobotNet.WebApp/Scripts/Components/_Imports.razor
Normal file
3
RobotNet.WebApp/Scripts/Components/_Imports.razor
Normal file
@@ -0,0 +1,3 @@
|
||||
@using RobotNet.Script.Shares
|
||||
@using RobotNet.WebApp.Clients
|
||||
@using RobotNet.WebApp.Scripts.Models
|
||||
Reference in New Issue
Block a user