Skip to content

Commit c1dcc3d

Browse files
committed
feat: introduce DocsPageState for managing document navigation and state
1 parent d3d77e0 commit c1dcc3d

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.AspNetCore.Components;
2+
using ShellDocs.Core;
3+
using ShellDocs.Markdown;
4+
5+
namespace ShellDocs.Components;
6+
7+
// Populated by MarkdownContent on each page render, read by DocsLayout's chrome
8+
// (TOC, PrevNext, Breadcrumb) so those components don't need per-page wiring.
9+
public sealed class DocsPageState : IDisposable
10+
{
11+
private readonly NavigationGraph _graph;
12+
private readonly NavigationManager _nav;
13+
14+
public DocsPageState(NavigationGraph graph, NavigationManager nav)
15+
{
16+
_graph = graph;
17+
_nav = nav;
18+
_nav.LocationChanged += OnLocationChanged;
19+
}
20+
21+
public RenderedDocument? Document { get; private set; }
22+
public NavigationNode? CurrentNode { get; private set; }
23+
public NavigationNode? Prev { get; private set; }
24+
public NavigationNode? Next { get; private set; }
25+
public IReadOnlyList<NavigationNode> Breadcrumbs { get; private set; } = Array.Empty<NavigationNode>();
26+
27+
public event Action? OnChange;
28+
29+
public void SetDocument(RenderedDocument? document)
30+
{
31+
Document = document;
32+
Recompute();
33+
}
34+
35+
private void OnLocationChanged(object? sender, Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs e)
36+
=> Recompute();
37+
38+
private void Recompute()
39+
{
40+
var path = new Uri(_nav.Uri).AbsolutePath;
41+
CurrentNode = _graph.ResolveByUrl(path);
42+
if (CurrentNode is null)
43+
{
44+
Prev = Next = null;
45+
Breadcrumbs = Array.Empty<NavigationNode>();
46+
}
47+
else
48+
{
49+
(Prev, Next) = _graph.GetPrevNext(CurrentNode);
50+
Breadcrumbs = _graph.GetBreadcrumb(CurrentNode);
51+
}
52+
OnChange?.Invoke();
53+
}
54+
55+
public void Dispose() => _nav.LocationChanged -= OnLocationChanged;
56+
}

src/ShellDocs.Components/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static IServiceCollection AddShellDocs(this IServiceCollection services,
1919
services.AddScoped<SearchState>();
2020
services.AddScoped<SidebarCollapseState>();
2121
services.AddScoped<CodeGroupSyncState>();
22+
services.AddScoped<DocsPageState>();
2223

2324
/* Auto-register the shipped content primitives so `razor:preview` blocks
2425
in markdown can reference <Callout>, <Card>, <Steps>, <FileTree> etc.

0 commit comments

Comments
 (0)