Share critical cell parsing across app and importer
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using RolemasterDb.App.Features;
|
||||
|
||||
namespace RolemasterDb.App.Components.Shared;
|
||||
@@ -35,11 +36,32 @@ public sealed class CriticalBranchEditorModel
|
||||
BranchKind,
|
||||
ConditionKey,
|
||||
ConditionText,
|
||||
ConditionJson,
|
||||
RawText,
|
||||
"{}",
|
||||
BuildRawText(),
|
||||
DescriptionText,
|
||||
RawAffixText,
|
||||
ParsedJson,
|
||||
SerializeParsedEffects(Effects),
|
||||
SortOrder,
|
||||
Effects.Select(effect => effect.ToItem()).ToList());
|
||||
|
||||
private string BuildRawText()
|
||||
{
|
||||
var condition = ConditionText.Trim();
|
||||
var description = DescriptionText.Trim();
|
||||
var firstLine = string.IsNullOrWhiteSpace(description)
|
||||
? $"{condition}:"
|
||||
: $"{condition}: {description}";
|
||||
|
||||
if (string.IsNullOrWhiteSpace(RawAffixText))
|
||||
{
|
||||
return firstLine;
|
||||
}
|
||||
|
||||
return $"{firstLine}{Environment.NewLine}{RawAffixText.Trim()}";
|
||||
}
|
||||
|
||||
private static string SerializeParsedEffects(IReadOnlyList<CriticalEffectEditorModel> effects) =>
|
||||
effects.Count == 0
|
||||
? "{}"
|
||||
: JsonSerializer.Serialize(new { effects = effects.Select(effect => effect.ToItem()).ToList() });
|
||||
}
|
||||
|
||||
@@ -45,6 +45,11 @@
|
||||
{
|
||||
<EditForm Model="Model" OnSubmit="HandleSubmitAsync" class="critical-editor-form">
|
||||
<div class="critical-editor-body">
|
||||
@if (!string.IsNullOrWhiteSpace(ReparseErrorMessage))
|
||||
{
|
||||
<p class="error-text critical-editor-error">@ReparseErrorMessage</p>
|
||||
}
|
||||
|
||||
@if (!string.IsNullOrWhiteSpace(SaveErrorMessage))
|
||||
{
|
||||
<p class="error-text critical-editor-error">@SaveErrorMessage</p>
|
||||
@@ -79,11 +84,23 @@
|
||||
<h4>Raw Text</h4>
|
||||
<p class="muted">Update the source text, then adjust the visible card fields below.</p>
|
||||
</div>
|
||||
<button type="button" class="btn-ritual" @onclick="OnReparse" disabled="@IsSaving || IsReparsing">
|
||||
@(IsReparsing ? "Re-Parsing..." : "Re-Parse Raw Text")
|
||||
</button>
|
||||
</div>
|
||||
<div class="field-shell">
|
||||
<label>Raw Cell Text</label>
|
||||
<InputTextArea class="input-shell critical-editor-textarea tall" @bind-Value="Model.RawCellText" />
|
||||
</div>
|
||||
@if (Model.ValidationMessages.Count > 0)
|
||||
{
|
||||
<div class="critical-editor-validation-list">
|
||||
@foreach (var message in Model.ValidationMessages)
|
||||
{
|
||||
<p class="critical-editor-validation-item">@message</p>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
<div class="field-shell">
|
||||
<label>Result Text Override</label>
|
||||
<InputTextArea class="input-shell critical-editor-textarea compact" @bind-Value="Model.DescriptionText" />
|
||||
@@ -231,18 +248,27 @@
|
||||
[Parameter]
|
||||
public bool IsLoading { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public bool IsReparsing { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public bool IsSaving { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? LoadErrorMessage { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? ReparseErrorMessage { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string? SaveErrorMessage { get; set; }
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public EventCallback OnClose { get; set; }
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public EventCallback OnReparse { get; set; }
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public EventCallback OnSave { get; set; }
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using RolemasterDb.App.Features;
|
||||
|
||||
namespace RolemasterDb.App.Components.Shared;
|
||||
@@ -19,6 +20,7 @@ public sealed class CriticalCellEditorModel
|
||||
public string? RawAffixText { get; set; }
|
||||
public string ParseStatus { get; set; } = string.Empty;
|
||||
public string ParsedJson { get; set; } = "{}";
|
||||
public List<string> ValidationMessages { get; set; } = [];
|
||||
public List<CriticalEffectEditorModel> Effects { get; set; } = [];
|
||||
public List<CriticalBranchEditorModel> Branches { get; set; } = [];
|
||||
|
||||
@@ -40,6 +42,7 @@ public sealed class CriticalCellEditorModel
|
||||
RawAffixText = response.RawAffixText,
|
||||
ParseStatus = response.ParseStatus,
|
||||
ParsedJson = response.ParsedJson,
|
||||
ValidationMessages = response.ValidationMessages.ToList(),
|
||||
Effects = response.Effects.Select(CriticalEffectEditorModel.FromItem).ToList(),
|
||||
Branches = response.Branches.Select(CriticalBranchEditorModel.FromItem).ToList()
|
||||
};
|
||||
@@ -49,8 +52,8 @@ public sealed class CriticalCellEditorModel
|
||||
RawCellText,
|
||||
DescriptionText,
|
||||
RawAffixText,
|
||||
ParseStatus,
|
||||
ParsedJson,
|
||||
ResolveParseStatus(Effects, Branches),
|
||||
SerializeParsedEffects(Effects),
|
||||
Effects.Select(effect => effect.ToItem()).ToList(),
|
||||
Branches
|
||||
.OrderBy(branch => branch.SortOrder)
|
||||
@@ -60,4 +63,16 @@ public sealed class CriticalCellEditorModel
|
||||
return branch.ToItem();
|
||||
})
|
||||
.ToList());
|
||||
|
||||
private static string ResolveParseStatus(
|
||||
IReadOnlyList<CriticalEffectEditorModel> effects,
|
||||
IReadOnlyList<CriticalBranchEditorModel> branches) =>
|
||||
effects.Count > 0 || branches.Any(branch => branch.Effects.Count > 0)
|
||||
? "partial"
|
||||
: "raw";
|
||||
|
||||
private static string SerializeParsedEffects(IReadOnlyList<CriticalEffectEditorModel> effects) =>
|
||||
effects.Count == 0
|
||||
? "{}"
|
||||
: JsonSerializer.Serialize(new { effects = effects.Select(effect => effect.ToItem()).ToList() });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user