-
-
Notifications
You must be signed in to change notification settings - Fork 577
feat: in-app documentation viewer #1677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mohnjiles
wants to merge
1
commit into
main
Choose a base branch
from
feat/in-app-docs-viewer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
StabilityMatrix.Avalonia/Controls/DocumentationMarkdownViewer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| using System; | ||
| using System.Windows.Input; | ||
| using Avalonia; | ||
| using Markdown.Avalonia; | ||
|
|
||
| namespace StabilityMatrix.Avalonia.Controls; | ||
|
|
||
| /// <summary> | ||
| /// A <see cref="BetterMarkdownScrollViewer"/> that routes hyperlink clicks through a | ||
| /// bindable <see cref="LinkCommand"/> (so relative <c>.md</c> links can navigate in-app | ||
| /// and external links can open in the browser) and resolves relative image paths against | ||
| /// <see cref="ImageBaseUrl"/> via the engine's asset path root. | ||
| /// </summary> | ||
| public class DocumentationMarkdownViewer : BetterMarkdownScrollViewer | ||
| { | ||
| /// <summary> | ||
| /// Command invoked when a hyperlink is clicked. The command parameter is the raw href string. | ||
| /// </summary> | ||
| public static readonly StyledProperty<ICommand?> LinkCommandProperty = AvaloniaProperty.Register< | ||
| DocumentationMarkdownViewer, | ||
| ICommand? | ||
| >(nameof(LinkCommand)); | ||
|
|
||
| /// <summary> | ||
| /// Base URL used to resolve relative image paths in the rendered markdown | ||
| /// (e.g. the raw URL of the current page's folder). | ||
| /// </summary> | ||
| public static readonly StyledProperty<string?> ImageBaseUrlProperty = AvaloniaProperty.Register< | ||
| DocumentationMarkdownViewer, | ||
| string? | ||
| >(nameof(ImageBaseUrl)); | ||
|
|
||
| public ICommand? LinkCommand | ||
| { | ||
| get => GetValue(LinkCommandProperty); | ||
| set => SetValue(LinkCommandProperty, value); | ||
| } | ||
|
|
||
| public string? ImageBaseUrl | ||
| { | ||
| get => GetValue(ImageBaseUrlProperty); | ||
| set => SetValue(ImageBaseUrlProperty, value); | ||
| } | ||
|
|
||
| public DocumentationMarkdownViewer() | ||
| { | ||
| ApplyLinkCommand(); | ||
| ApplyImageBaseUrl(); | ||
| } | ||
|
|
||
| protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) | ||
| { | ||
| base.OnPropertyChanged(change); | ||
|
|
||
| if (change.Property == LinkCommandProperty) | ||
| { | ||
| ApplyLinkCommand(); | ||
| } | ||
| else if (change.Property == ImageBaseUrlProperty) | ||
| { | ||
| ApplyImageBaseUrl(); | ||
| } | ||
| } | ||
|
|
||
| private void ApplyLinkCommand() | ||
| { | ||
| // The engine (IMarkdownEngine) owns the HyperlinkCommand used for all rendered links. | ||
| if (Engine is IMarkdownEngine engine) | ||
| { | ||
| engine.HyperlinkCommand = LinkCommand; | ||
| } | ||
| } | ||
|
|
||
| private void ApplyImageBaseUrl() | ||
| { | ||
| // AssetPathRoot flows through to the engine's bitmap loader so relative image | ||
| // paths resolve against the raw docs URL. | ||
| AssetPathRoot = ImageBaseUrl ?? string.Empty; | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
StabilityMatrix.Avalonia/ViewModels/Documentation/DocumentationPageNavItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using CommunityToolkit.Mvvm.ComponentModel; | ||
|
|
||
| namespace StabilityMatrix.Avalonia.ViewModels.Documentation; | ||
|
|
||
| /// <summary> | ||
| /// A single navigable documentation page entry in the sidebar. | ||
| /// </summary> | ||
| public partial class DocumentationPageNavItem : ObservableObject | ||
| { | ||
| /// <summary>Display title, e.g. "Overview".</summary> | ||
| public required string Title { get; init; } | ||
|
|
||
| /// <summary>Path relative to the docs root, e.g. <c>getting-started/overview.md</c>.</summary> | ||
| public required string Path { get; init; } | ||
|
|
||
| [ObservableProperty] | ||
| private bool isSelected; | ||
| } |
18 changes: 18 additions & 0 deletions
18
StabilityMatrix.Avalonia/ViewModels/Documentation/DocumentationSectionNavItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace StabilityMatrix.Avalonia.ViewModels.Documentation; | ||
|
|
||
| /// <summary> | ||
| /// A section grouping in the documentation sidebar (e.g. "Getting Started"). | ||
| /// </summary> | ||
| public class DocumentationSectionNavItem | ||
| { | ||
| /// <summary>Section title. Empty for the root section (renders without a header).</summary> | ||
| public required string Title { get; init; } | ||
|
|
||
| /// <summary>Whether this section has a visible header (i.e. is not the root section).</summary> | ||
| public bool HasHeader => !string.IsNullOrEmpty(Title); | ||
|
|
||
| /// <summary>Pages within this section.</summary> | ||
| public required IReadOnlyList<DocumentationPageNavItem> Pages { get; init; } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Engineproperty ofMarkdownScrollVieweris often initialized or changed after the control's constructor runs (e.g., during template application or when custom styles are applied). SinceApplyLinkCommandis only called in the constructor and whenLinkCommandPropertychanges, the customLinkCommandwill never be applied to the engine if the engine is set after construction. Listening to changes on theEngineproperty ensures the command is always correctly bound.