Skip to content

Commit

Permalink
Implement built-in search
Browse files Browse the repository at this point in the history
Remove Sass
  • Loading branch information
WilStead committed Mar 28, 2024
1 parent e8c52b9 commit d20b63e
Show file tree
Hide file tree
Showing 46 changed files with 379 additions and 5,500 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
uses: actions/setup-dotnet@v1
with:
# Semantic version range syntax or exact version of a dotnet version
dotnet-version: '8.0.x'
dotnet-version: '8.x'
include-prerelease: true

- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: publish
env:
VERSION: '0.7.8-preview'
VERSION: '0.8.0-preview'
PRERELEASE: true
on:
push:
Expand All @@ -16,7 +16,7 @@ jobs:
uses: actions/setup-dotnet@v1
with:
# Semantic version range syntax or exact version of a dotnet version
dotnet-version: '8.0.x'
dotnet-version: '8.x'
include-prerelease: true

- name: Install dependencies
Expand Down
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.8.0-preview
### Changed
- Implement built-in search
- Replaced 3rd party emoji popup

## 0.7.8-preview
### Fixed
- Decode wiki route
Expand Down
7 changes: 4 additions & 3 deletions sample/Tavenem.Wiki.Blazor.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion sample/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tavenem Wiki Blazor Sample</title>
<base href="/" />
<link href="_content/Tavenem.Blazor.Framework/framework.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<link href="Tavenem.Wiki.Blazor.Sample.styles.css" rel="stylesheet" />
</head>
Expand Down
17 changes: 17 additions & 0 deletions src/Client/Models/SearchResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Tavenem.DataStorage;
using Tavenem.Wiki.Queries;

namespace Tavenem.Wiki.Blazor.Models;

/// <summary>
/// The result of a search operation.
/// </summary>
/// <param name="Request">The original search request.</param>
/// <param name="SearchHits">
/// A <see cref="PagedList{T}" /> of <see cref="SearchHit" /> instances representing the results.
/// </param>
/// <param name="ExactMatch">A page whose title matched the search query exactly.</param>
public record SearchResult(
SearchRequest Request,
PagedList<SearchHit> SearchHits,
Page? ExactMatch = null);
2 changes: 1 addition & 1 deletion src/Client/Pages/EditView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override Task SetParametersAsync(ParameterView parameters)
else
{
Content = newArticle.MarkdownContent;
HtmlContent = new(newArticle.Html);
HtmlContent = new(newArticle.Html ?? string.Empty);
Title = newArticle.Title.ToString();
IsScript = string.CompareOrdinal(newArticle.Title.Namespace, WikiOptions.ScriptNamespace) == 0;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Client/Pages/HistoryView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public partial class HistoryView : OfflineSupportComponent, IAsyncDisposable
/// <summary>
/// The requested page number.
/// </summary>
[Parameter] public long? PageNumber { get; set; }
[Parameter] public int? PageNumber { get; set; }

/// <summary>
/// The requested page size.
Expand Down Expand Up @@ -276,14 +276,14 @@ private async Task OnNextRequestedAsync()
if (Revisions?.HasNextPage == true)
{
CurrentPageNumber++;
PageNumber = (long)CurrentPageNumber;
PageNumber = (int)CurrentPageNumber;
await RefreshAsync();
}
}

private async Task OnPageNumberChangedAsync()
{
PageNumber = (long)CurrentPageNumber;
PageNumber = (int)CurrentPageNumber;
await RefreshAsync();
}

Expand Down
20 changes: 2 additions & 18 deletions src/Client/Pages/Search.razor
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,12 @@
DisplayHelpTextOnFocus="true"
HelpText="Specify one or more owners. Exclude an owner by clicking the chip."
InitialUsers="@CurrentOwner" />
<Select Label="Sort by" @bind-Value="CurrentSort">
<Option Label="Default" Value="@("default")" />
<Option Label="Last revision date/time" Value="@("timestamp")" />
</Select>
<Switch Label="Descending"
UncheckedLabel="Ascending"
@bind-Value="CurrentDescending" />
<button class="btn primary" @onclick="OnSetSearchAsync">Filter</button>
</Collapse>
}
else if (!string.IsNullOrEmpty(CurrentDomain)
|| !string.IsNullOrEmpty(CurrentNamespace)
|| !string.IsNullOrEmpty(CurrentOwner)
|| CurrentDescending)
|| !string.IsNullOrEmpty(CurrentOwner))
{
<Collapse Title="Advanced search:" class="wiki-search-advanced">
@if (!string.IsNullOrEmpty(CurrentDomain))
Expand All @@ -93,14 +85,6 @@
DisplayHelpTextOnFocus="true"
InitialUsers="@CurrentOwner" />
}
@if (CurrentSort?.Equals("timestamp") == true)
{
<p>Sorted by: Last revision date/time</p>
}
@if (CurrentDescending)
{
<p>Descending</p>
}
</Collapse>
}
</div>
Expand All @@ -119,7 +103,7 @@
<li>
<div class="wiki-search-result-title">
<a href="@WikiState.Link(result.Title)"
class="wiki-link wiki-link-exists">@result.Title.ToString()</a>
class="wiki-link wiki-link-exists">@result.Title.ToString(WikiOptions)</a>
</div>
@if (!string.IsNullOrEmpty(result.Excerpt))
{
Expand Down
115 changes: 17 additions & 98 deletions src/Client/Pages/Search.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Diagnostics.CodeAnalysis;
using Tavenem.Blazor.Framework;
using Tavenem.Wiki.Blazor.Client.Shared;
using Tavenem.Wiki.Blazor.Services.Search;
using Tavenem.Wiki.Queries;

namespace Tavenem.Wiki.Blazor.Client.Pages;

Expand All @@ -19,7 +19,7 @@ public partial class Search : OfflineSupportComponent
/// <summary>
/// The requested page number.
/// </summary>
[Parameter] public long? PageNumber { get; set; }
[Parameter] public int? PageNumber { get; set; }

/// <summary>
/// The requested page size.
Expand Down Expand Up @@ -51,8 +51,6 @@ public partial class Search : OfflineSupportComponent
/// </summary>
[Parameter] public string? Sort { get; set; }

private bool CurrentDescending { get; set; }

private string? CurrentDomain { get; set; }

private string? CurrentNamespace { get; set; }
Expand All @@ -65,8 +63,6 @@ public partial class Search : OfflineSupportComponent

private string? CurrentQuery { get; set; }

private string? CurrentSort { get; set; }

private List<IWikiOwner> DeselectedOwners { get; set; } = [];

[CascadingParameter] private bool IsInteractive { get; set; }
Expand All @@ -75,9 +71,7 @@ public partial class Search : OfflineSupportComponent

[Inject, NotNull] private QueryStateService? QueryStateService { get; set; }

private ISearchResult? Result { get; set; }

[Inject, NotNull] ISearchClient? SearchClient { get; set; }
private SearchResult? Result { get; set; }

private List<IWikiOwner> SelectedOwners { get; set; } = [];

Expand All @@ -101,27 +95,6 @@ protected override void OnInitialized()
CurrentPageSize = Math.Clamp(pageSize, 5, 500);
}

var sorts = QueryStateService.RegisterProperty(
"pg",
"s",
OnSortChangedAsync,
50);
if (sorts?.Count > 0)
{
CurrentSort = sorts[0];
}

var descendings = QueryStateService.RegisterProperty(
"pg",
"d",
OnDescendingChangedAsync,
false);
if (descendings?.Count > 0
&& bool.TryParse(descendings[0], out var descending))
{
CurrentDescending = descending;
}

var filters = QueryStateService.RegisterProperty(
"pg",
"f",
Expand All @@ -136,16 +109,12 @@ protected override void OnInitialized()
/// <inheritdoc/>
protected override async Task RefreshAsync()
{
CurrentDescending = Descending;
CurrentDomain = SearchDomain;
CurrentNamespace = SearchNamespace;
CurrentOwner = SearchOwner;
CurrentQuery = Query?.Trim();
CurrentPageNumber = (ulong)Math.Max(1, PageNumber ?? 1);
CurrentPageSize = Math.Clamp(PageSize ?? 50, 5, 200);
CurrentSort = string.Equals(Sort, "timestamp")
? Sort
: "default";
Result = null;
ExactMatch = null;

Expand All @@ -154,25 +123,19 @@ protected override async Task RefreshAsync()
return;
}

var request = new SearchRequest()
{
Descending = Descending,
Owner = CurrentOwner,
PageNumber = (int)(CurrentPageNumber + 1),
PageSize = CurrentPageSize,
Query = CurrentQuery,
Sort = CurrentSort,
Namespace = CurrentNamespace,
Domain = CurrentDomain,
};
var request = new SearchRequest(
CurrentQuery,
CurrentDomain,
CurrentNamespace,
(int)CurrentPageNumber,
CurrentPageSize,
CurrentOwner);
var results = await PostAsync(
$"{WikiBlazorClientOptions.WikiServerApiRoute}/search",
request,
WikiBlazorJsonSerializerContext.Default.SearchRequest,
WikiJsonSerializerContext.Default.SearchRequest,
WikiBlazorJsonSerializerContext.Default.SearchResult,
async user => SearchClient is null
? null
: await WikiDataManager.SearchAsync(SearchClient, user, request));
async user => await WikiDataManager.SearchAsync(user, request));
ExactMatch = results?.ExactMatch;
Result = results;
}
Expand All @@ -181,29 +144,15 @@ protected override async Task RefreshAsync()
{
if (string.IsNullOrEmpty(input))
{
return Enumerable.Empty<KeyValuePair<string, object>>();
return [];
}

var suggestions = await FetchDataAsync(
$"{WikiBlazorClientOptions.WikiServerApiRoute}/searchsuggest?input={input}",
WikiBlazorJsonSerializerContext.Default.ListString,
async user => await WikiDataManager.GetSearchSuggestionsAsync(
SearchClient,
user,
input));
async user => await WikiDataManager.GetSearchSuggestionsAsync(user, input));
return suggestions?.Select(x => new KeyValuePair<string, object>(x, x))
?? Enumerable.Empty<KeyValuePair<string, object>>();
}

private async Task OnDescendingChangedAsync(QueryChangeEventArgs args)
{
if (bool.TryParse(args.Value, out var descending)
&& descending != CurrentDescending)
{
CurrentDescending = descending;
Descending = descending;
await RefreshAsync();
}
?? [];
}

private async Task OnFilterChangedAsync(QueryChangeEventArgs args)
Expand All @@ -221,14 +170,14 @@ private async Task OnNextRequestedAsync()
if (Result?.SearchHits?.HasNextPage == true)
{
CurrentPageNumber++;
PageNumber = (long)CurrentPageNumber;
PageNumber = (int)CurrentPageNumber;
await RefreshAsync();
}
}

private async Task OnPageNumberChangedAsync()
{
PageNumber = (long)CurrentPageNumber;
PageNumber = (int)CurrentPageNumber;
await RefreshAsync();
}

Expand Down Expand Up @@ -280,18 +229,6 @@ private async Task OnSearchAsync()

private async Task OnSetSearchAsync()
{
Descending = CurrentDescending;
QueryStateService.SetPropertyValue(
"pg",
"d",
CurrentDescending);

Descending = CurrentDescending;
QueryStateService.SetPropertyValue(
"pg",
"d",
CurrentDescending);

SearchDomain = CurrentDomain;
QueryStateService.SetPropertyValue(
"s",
Expand Down Expand Up @@ -329,24 +266,6 @@ private async Task OnSetSearchAsync()
"o",
owners);

Sort = string.Equals(CurrentSort, "timestamp")
? CurrentSort
: null;
QueryStateService.SetPropertyValue(
"pg",
"s",
Sort);

await RefreshAsync();
}

private async Task OnSortChangedAsync(QueryChangeEventArgs args)
{
if (!string.Equals(args.Value, CurrentSort))
{
Sort = args.Value;
CurrentSort = args.Value;
await RefreshAsync();
}
}
}

0 comments on commit d20b63e

Please sign in to comment.