feat(adminui): inline script-source editor in the virtual-tag modal
This commit is contained in:
@@ -47,7 +47,8 @@
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label class="form-label" for="vtag-script">Script</label>
|
||||
<InputSelect id="vtag-script" @bind-Value="_form.ScriptId" class="form-select form-select-sm">
|
||||
<InputSelect id="vtag-script" @bind-Value="_form.ScriptId"
|
||||
@bind-Value:after="OnScriptChangedAsync" class="form-select form-select-sm">
|
||||
<option value="">— pick script —</option>
|
||||
@foreach (var (id, display) in Scripts)
|
||||
{
|
||||
@@ -57,6 +58,48 @@
|
||||
<ValidationMessage For="@(() => _form.ScriptId)" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@* Inline script-source editor. Shown only when a script is bound. This panel saves
|
||||
the SHARED Script row on its OWN concurrency-guarded button — it is deliberately
|
||||
separate from the virtual-tag Create/Save below and never touches _form or closes
|
||||
the modal. *@
|
||||
@if (!string.IsNullOrEmpty(_form.ScriptId) && _scriptLoaded)
|
||||
{
|
||||
<div class="card mb-3">
|
||||
<div class="card-header py-2 d-flex justify-content-between align-items-center">
|
||||
<span class="fw-semibold">Script source</span>
|
||||
<button type="button" class="btn btn-link btn-sm p-0 text-decoration-none"
|
||||
@onclick="ToggleScriptPanel">
|
||||
@(_scriptExpanded ? "Hide" : "Edit source")
|
||||
</button>
|
||||
</div>
|
||||
@if (_scriptExpanded)
|
||||
{
|
||||
<div class="card-body">
|
||||
<div class="alert alert-warning py-2 small mb-2">
|
||||
Editing shared script "<span class="mono">@_scriptName</span>" — used by
|
||||
@_scriptUsageCount virtual tag(s). Changes affect all of them.
|
||||
</div>
|
||||
<MonacoEditor @bind-Value="_scriptSource" Height="300px" />
|
||||
@if (!string.IsNullOrWhiteSpace(_scriptError))
|
||||
{
|
||||
<div class="text-danger small mt-2">@_scriptError</div>
|
||||
}
|
||||
else if (_scriptSaved)
|
||||
{
|
||||
<div class="text-success small mt-2">Script saved.</div>
|
||||
}
|
||||
<div class="mt-2">
|
||||
<button type="button" class="btn btn-outline-primary btn-sm"
|
||||
@onclick="SaveScriptAsync" disabled="@_scriptSaving">
|
||||
@if (_scriptSaving) { <span class="spinner-border spinner-border-sm me-1"></span> }
|
||||
Save script
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
<div class="row">
|
||||
<div class="col-md-4 mb-3">
|
||||
<label class="form-label">Change-triggered</label>
|
||||
@@ -92,8 +135,8 @@
|
||||
}
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="CancelAsync" disabled="@_busy">Cancel</button>
|
||||
<button type="submit" class="btn btn-primary" disabled="@_busy">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="CancelAsync" disabled="@(_busy || _scriptSaving)">Cancel</button>
|
||||
<button type="submit" class="btn btn-primary" disabled="@(_busy || _scriptSaving)">
|
||||
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
|
||||
@(IsNew ? "Create" : "Save changes")
|
||||
</button>
|
||||
@@ -135,8 +178,37 @@
|
||||
private bool _busy;
|
||||
private string? _error;
|
||||
|
||||
protected override void OnParametersSet()
|
||||
// Tracks which open this modal last loaded for, so unrelated Blazor Server re-renders don't
|
||||
// rebuild _form / reload the script source and clobber in-progress edits. Null while closed.
|
||||
private string? _loadedKey;
|
||||
|
||||
// Inline script-source editor state. Independent of _form and the virtual-tag save.
|
||||
private string _scriptSource = "";
|
||||
private bool _scriptLoaded;
|
||||
private byte[] _scriptRowVersion = Array.Empty<byte>();
|
||||
private string? _scriptName;
|
||||
private int _scriptUsageCount;
|
||||
private bool _scriptExpanded;
|
||||
private bool _scriptSaving;
|
||||
private bool _scriptSaved;
|
||||
private string? _scriptError;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
if (!Visible)
|
||||
{
|
||||
_loadedKey = null; // closed → next open reloads fresh
|
||||
return;
|
||||
}
|
||||
|
||||
// Guard against unrelated re-renders. In Blazor Server any live-status push re-invokes
|
||||
// OnParametersSetAsync; without this the rebuild + script reload below would silently
|
||||
// discard whatever the operator has typed. Only rebuild when the modal OPENS or the
|
||||
// target entity CHANGES.
|
||||
var key = IsNew ? "<new>" : Existing?.VirtualTagId;
|
||||
if (key == _loadedKey) return; // same open, re-render → preserve in-progress form + script edits
|
||||
_loadedKey = key;
|
||||
|
||||
// Rebuild the working form whenever the host (re)opens the modal for a fresh target.
|
||||
if (IsNew)
|
||||
{
|
||||
@@ -157,6 +229,105 @@
|
||||
};
|
||||
}
|
||||
_error = null;
|
||||
_scriptExpanded = false;
|
||||
|
||||
// Load the bound script's source for the inline panel when editing an existing tag that
|
||||
// already has a script (the open-load path; selection changes go through OnScriptChangedAsync).
|
||||
await LoadScriptSourceAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reloads the inline-editor state from the currently selected script. Clears it when no script is
|
||||
/// bound. This drives both the open-load and the post-selection refresh, and never touches _form.
|
||||
/// </summary>
|
||||
private async Task LoadScriptSourceAsync()
|
||||
{
|
||||
_scriptSaved = false;
|
||||
_scriptError = null;
|
||||
|
||||
if (string.IsNullOrEmpty(_form.ScriptId))
|
||||
{
|
||||
_scriptSource = "";
|
||||
_scriptLoaded = false;
|
||||
_scriptRowVersion = Array.Empty<byte>();
|
||||
_scriptName = null;
|
||||
_scriptUsageCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
var loaded = await Svc.GetScriptSourceAsync(_form.ScriptId);
|
||||
if (loaded is null)
|
||||
{
|
||||
_scriptSource = "";
|
||||
_scriptLoaded = false;
|
||||
_scriptRowVersion = Array.Empty<byte>();
|
||||
_scriptName = null;
|
||||
_scriptUsageCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
_scriptSource = loaded.Value.SourceCode;
|
||||
_scriptLoaded = true;
|
||||
_scriptRowVersion = loaded.Value.RowVersion;
|
||||
_scriptName = loaded.Value.Name;
|
||||
_scriptUsageCount = await Svc.CountVirtualTagsUsingScriptAsync(_form.ScriptId);
|
||||
}
|
||||
|
||||
/// <summary>Refreshes the inline script panel when the operator picks a different script.</summary>
|
||||
private Task OnScriptChangedAsync() => LoadScriptSourceAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Toggles the inline script-source panel. Clears the stale "Script saved." banner when
|
||||
/// (re)expanding so a collapse/expand cycle doesn't re-show a confirmation from an earlier save.
|
||||
/// </summary>
|
||||
private void ToggleScriptPanel()
|
||||
{
|
||||
_scriptExpanded = !_scriptExpanded;
|
||||
if (_scriptExpanded)
|
||||
{
|
||||
_scriptSaved = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the edited script body via its own RowVersion-guarded service call. This is wholly
|
||||
/// separate from the virtual-tag save: it does not touch _form, does not raise OnSaved, and does
|
||||
/// not close the modal. On success it re-fetches the source so the panel holds a fresh token.
|
||||
/// </summary>
|
||||
private async Task SaveScriptAsync()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_form.ScriptId) || !_scriptLoaded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_scriptSaving = true;
|
||||
_scriptSaved = false;
|
||||
_scriptError = null;
|
||||
try
|
||||
{
|
||||
var result = await Svc.UpdateScriptSourceAsync(_form.ScriptId, _scriptSource, _scriptRowVersion);
|
||||
if (result.Ok)
|
||||
{
|
||||
// Re-fetch to refresh the concurrency token for any subsequent save.
|
||||
var reloaded = await Svc.GetScriptSourceAsync(_form.ScriptId);
|
||||
if (reloaded is not null)
|
||||
{
|
||||
_scriptSource = reloaded.Value.SourceCode;
|
||||
_scriptRowVersion = reloaded.Value.RowVersion;
|
||||
_scriptName = reloaded.Value.Name;
|
||||
}
|
||||
_scriptSaved = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_scriptError = result.Error;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_scriptSaving = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
|
||||
@@ -401,4 +401,36 @@ public interface IUnsTreeService
|
||||
/// <param name="ct">A token to cancel the operation.</param>
|
||||
/// <returns>Success, a concurrency failure, or a delete-failed failure.</returns>
|
||||
Task<UnsMutationResult> DeleteVirtualTagAsync(string virtualTagId, byte[] rowVersion, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Loads a script's editable source for the inline script-source panel in the virtual-tag modal,
|
||||
/// along with the concurrency token the panel must echo back on save and the script's display name.
|
||||
/// Reads untracked. Returns <c>null</c> when the script no longer exists.
|
||||
/// </summary>
|
||||
/// <param name="scriptId">The script whose source to load.</param>
|
||||
/// <param name="ct">A token to cancel the load.</param>
|
||||
/// <returns>The <c>(SourceCode, RowVersion, Name)</c> triple, or <c>null</c> when missing.</returns>
|
||||
Task<(string SourceCode, byte[] RowVersion, string Name)?> GetScriptSourceAsync(string scriptId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Counts how many virtual tags bind the given script, so the inline editor can warn the operator
|
||||
/// that an edit to a shared script affects every virtual tag using it.
|
||||
/// </summary>
|
||||
/// <param name="scriptId">The script to count usages of.</param>
|
||||
/// <param name="ct">A token to cancel the query.</param>
|
||||
/// <returns>The number of virtual tags whose <c>ScriptId</c> matches.</returns>
|
||||
Task<int> CountVirtualTagsUsingScriptAsync(string scriptId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Saves an edited script body from the inline panel: updates <c>SourceCode</c> and recomputes the
|
||||
/// SHA-256 <c>SourceHash</c> (lower-case hex, matching the Script-edit page). This save is separate
|
||||
/// from the virtual-tag save and is guarded by its own last-write-wins optimistic concurrency on
|
||||
/// <see cref="Configuration.Entities.Script.RowVersion"/>.
|
||||
/// </summary>
|
||||
/// <param name="scriptId">The script to update.</param>
|
||||
/// <param name="sourceCode">The new source body.</param>
|
||||
/// <param name="rowVersion">The concurrency token the panel last read.</param>
|
||||
/// <param name="ct">A token to cancel the operation.</param>
|
||||
/// <returns>Success, a missing-row failure, or a concurrency failure.</returns>
|
||||
Task<(bool Ok, string? Error)> UpdateScriptSourceAsync(string scriptId, string sourceCode, byte[] rowVersion, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
@@ -1014,6 +1016,67 @@ public sealed class UnsTreeService(IDbContextFactory<OtOpcUaConfigDbContext> dbF
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<(string SourceCode, byte[] RowVersion, string Name)?> GetScriptSourceAsync(
|
||||
string scriptId,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
|
||||
var row = await db.Scripts
|
||||
.AsNoTracking()
|
||||
.Where(s => s.ScriptId == scriptId)
|
||||
.Select(s => new { s.SourceCode, s.RowVersion, s.Name })
|
||||
.FirstOrDefaultAsync(ct);
|
||||
|
||||
return row is null ? null : (row.SourceCode, row.RowVersion, row.Name);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<int> CountVirtualTagsUsingScriptAsync(string scriptId, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
return await db.VirtualTags.CountAsync(v => v.ScriptId == scriptId, ct);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<(bool Ok, string? Error)> UpdateScriptSourceAsync(
|
||||
string scriptId,
|
||||
string sourceCode,
|
||||
byte[] rowVersion,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
|
||||
var entity = await db.Scripts.FirstOrDefaultAsync(s => s.ScriptId == scriptId, ct);
|
||||
if (entity is null)
|
||||
{
|
||||
return (false, "Script not found.");
|
||||
}
|
||||
|
||||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = rowVersion;
|
||||
|
||||
entity.SourceCode = sourceCode;
|
||||
entity.SourceHash = HashSource(sourceCode);
|
||||
|
||||
try
|
||||
{
|
||||
await db.SaveChangesAsync(ct);
|
||||
return (true, null);
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
return (false, "This script was changed by someone else. Reload and try again.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the SHA-256 of a script body as lower-case hex — the same algorithm the Script-edit
|
||||
/// page uses, so a body saved from either surface yields an identical compile-cache key.
|
||||
/// </summary>
|
||||
private static string HashSource(string source) =>
|
||||
Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(source)));
|
||||
|
||||
/// <summary>
|
||||
/// Validates a virtual tag's script binding and trigger configuration (mirrors the DbContext CHECK
|
||||
/// constraints): a script must be chosen, at least one of change-trigger / timer must be set, and a
|
||||
|
||||
Reference in New Issue
Block a user