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