Repository navigation
Expand file tree
/
Copy pathDocsPageState.cs
More file actions
68 lines (58 loc) · 2.38 KB
/
Copy pathDocsPageState.cs
File metadata and controls
68 lines (58 loc) · 2.38 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
57
58
59
60
61
62
63
64
65
66
67
68
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;
private readonly DocsVersionResolver? _versions;
public DocsPageState(NavigationGraph graph, NavigationManager nav)
: this(graph, nav, null) { }
// With a resolver, prev/next stay in the page's version and the version folder leaves the breadcrumb.
public DocsPageState(NavigationGraph graph, NavigationManager nav, DocsVersionResolver? versions)
{
_graph = graph;
_nav = nav;
_versions = versions;
_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 if (_versions is { HasVersions: true } versions)
{
var current = CurrentNode;
(Prev, Next) = _graph.GetPrevNext(current, n => versions.InSameScope(n.Url, current.Url));
Breadcrumbs = _graph.GetBreadcrumb(current).Where(n => !versions.IsVersionNode(n)).ToList();
}
else
{
(Prev, Next) = _graph.GetPrevNext(CurrentNode);
Breadcrumbs = _graph.GetBreadcrumb(CurrentNode);
}
OnChange?.Invoke();
}
public void Dispose() => _nav.LocationChanged -= OnLocationChanged;
}