diff options
Diffstat (limited to 'Pages/Component')
| -rw-r--r-- | Pages/Component/TagSelectDialog.razor | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/Pages/Component/TagSelectDialog.razor b/Pages/Component/TagSelectDialog.razor index e9f3295..2ba34e9 100644 --- a/Pages/Component/TagSelectDialog.razor +++ b/Pages/Component/TagSelectDialog.razor @@ -6,9 +6,11 @@ <link rel="stylesheet" href="@(nameof(HyperBooru)).styles.css"/> <Dialog Title=@(Title ?? "Select one or more tag(s)") @ref=dialog> - <input type="text" placeholder="Search"/> + <input type="text" placeholder="Search" @ref=queryInput @oninput=QueryInput @onkeypress=QueryKey value=@query autofocus/> <div class="tag-definitions"> @for(int i = 0; i < tagDefinitions.Count(); i++) { + if(!MatchesQuery(tagDefinitions[i].tagDefinition)) + continue; var local = i; var ns = tagDefinitions[i].tagDefinition.Namespace; var alias = tagDefinitions[i].tagDefinition.Alias; @@ -48,6 +50,7 @@ set { if(value) LoadTags(); + query = null; visible = dialog.Visible = value; } } @@ -60,6 +63,9 @@ private bool visible; + private string? query; + private ElementReference queryInput; + public void Show() => Visible = true; public void Hide() => Visible = false; @@ -68,6 +74,11 @@ LoadTags(); } + protected override void OnAfterRender(bool firstRender) { + if(Visible) + queryInput.FocusAsync(); + } + private void LoadTags() { db = dbFactory.CreateDbContext(); @@ -87,6 +98,64 @@ .ToArray(); } + private void QueryInput(ChangeEventArgs e) { + query = (string?) e.Value; + StateHasChanged(); + } + + private void QueryKey(KeyboardEventArgs e) { + if(e.Code != "Enter" && e.Code != "NumpadEnter") + return; + + if(string.IsNullOrEmpty(query)) { + Submit(); + return; + } + + int c = 0; + int? last = null; + for(int i = 0; i < tagDefinitions.Count(); i++) { + if(!MatchesQuery(tagDefinitions[i].tagDefinition)) + continue; + last = i; + c++; + } + + if(c == 1 && last is not null) + tagDefinitions[(int) last].selected = + !tagDefinitions[(int) last].selected; + + query = null; + StateHasChanged(); + } + + private bool MatchesQuery(TagDefinition tagDef) { + TagDefinition? singleTag = null; + + if(string.IsNullOrEmpty(query)) + return true; + + singleTag = tagDefinitions.FirstOrDefault( + e => string.Equals( + e.tagDefinition.Alias, + query, + StringComparison.OrdinalIgnoreCase)).tagDefinition; + + if(singleTag is not null) + return tagDef.Guid == singleTag.Guid; + + singleTag = tagDefinitions.FirstOrDefault( + e => string.Equals( + e.tagDefinition.Name, + query, + StringComparison.OrdinalIgnoreCase)).tagDefinition; + + if(singleTag is not null) + return tagDef.Guid == singleTag.Guid; + + return tagDef.Name.ToLower().Contains(query.ToLower()); + } + private async void Submit() { await OnSubmit.InvokeAsync( tagDefinitions |
