106 lines
3.2 KiB
Plaintext
106 lines
3.2 KiB
Plaintext
@using Core.Printing
|
|
@inject ClipboardService ClipboardService
|
|
@inject ISnackbar Snackbar
|
|
|
|
<MudDialog>
|
|
<DialogContent>
|
|
<MudTextField @bind-Value="_search"
|
|
Label="Search tokens"
|
|
Variant="Variant.Outlined"
|
|
Immediate="true"
|
|
Adornment="Adornment.Start"
|
|
AdornmentIcon="@Icons.Material.Filled.Search"
|
|
Class="mb-3" />
|
|
|
|
@if (!HasAnyMatches)
|
|
{
|
|
<MudText Class="mud-text-secondary">No tokens match this search.</MudText>
|
|
}
|
|
else
|
|
{
|
|
@foreach (var group in TokenGroups)
|
|
{
|
|
var tokens = Visible(group.Tokens);
|
|
if (tokens.Count == 0)
|
|
continue;
|
|
<MudText Typo="Typo.caption" Class="mud-text-secondary">@group.Label</MudText>
|
|
<div class="mb-2">
|
|
<TokenChips Tokens="tokens" Insert="Insert" Copy="Copy" />
|
|
</div>
|
|
}
|
|
}
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<MudButton OnClick="Close">Close</MudButton>
|
|
</DialogActions>
|
|
</MudDialog>
|
|
|
|
@code {
|
|
[CascadingParameter]
|
|
IMudDialogInstance MudDialog { get; set; } = null!;
|
|
|
|
[Parameter]
|
|
public PrintEntityType EntityType { get; set; }
|
|
|
|
[Parameter]
|
|
public IReadOnlyList<string> ImportedFieldNames { get; set; } = [];
|
|
|
|
[Parameter]
|
|
public EventCallback<string> OnInsert { get; set; }
|
|
|
|
private string _search = string.Empty;
|
|
|
|
private IEnumerable<(string Label, IReadOnlyList<string> Tokens)> TokenGroups
|
|
{
|
|
get
|
|
{
|
|
yield return ("Layout", PrintFieldCatalog.Layout);
|
|
yield return ("Chapter", PrintFieldCatalog.Chapter);
|
|
yield return (EntityType.ToString(), PrintFieldCatalog.EntityTokens(EntityType));
|
|
if (EntityType == PrintEntityType.Student)
|
|
yield return ("Event ranks", PrintFieldCatalog.StudentRanks);
|
|
if (EntityType == PrintEntityType.Student && ImportedFieldNames.Count > 0)
|
|
yield return ("Additional fields", ImportedFieldNames);
|
|
}
|
|
}
|
|
|
|
private bool HasAnyMatches =>
|
|
TokenGroups.Any(group => Visible(group.Tokens).Count > 0);
|
|
|
|
private IReadOnlyList<string> Visible(IReadOnlyList<string> tokens) =>
|
|
string.IsNullOrWhiteSpace(_search)
|
|
? tokens
|
|
: [.. tokens.Where(Matches)];
|
|
|
|
private bool Matches(string token) =>
|
|
string.IsNullOrWhiteSpace(_search)
|
|
|| token.Contains(_search.Trim(), StringComparison.OrdinalIgnoreCase);
|
|
|
|
private async Task Insert(string token)
|
|
{
|
|
if (OnInsert.HasDelegate)
|
|
await OnInsert.InvokeAsync(token);
|
|
}
|
|
|
|
private async Task Copy(string token)
|
|
{
|
|
try
|
|
{
|
|
await ClipboardService.WriteTextAsync("{{" + token + "}}");
|
|
Snackbar.Add("Copied {{" + token + "}}", Severity.Info);
|
|
}
|
|
catch (JSDisconnectedException)
|
|
{
|
|
}
|
|
catch (TaskCanceledException)
|
|
{
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Snackbar.Add($"Could not copy: {ex.Message}", Severity.Error);
|
|
}
|
|
}
|
|
|
|
private void Close() => MudDialog.Close();
|
|
}
|