fix(PostIt): disable links without a reader context
Prevent the Markdown viewer's default browser handler from activating when the blog reader has no view model, while keeping reader links bound to PostIt after context changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
dc4412b9e8
commit
b90f15333d
6 changed files with 21 additions and 17 deletions
|
|
@ -24,4 +24,4 @@
|
|||
<ItemGroup>
|
||||
<ProjectReference Include="..\PostIt\PostIt.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -130,7 +130,8 @@ public class BlogReaderTests
|
|||
var page = new BlogsPage();
|
||||
var reader = page.FindControl<MarkdownScrollViewer>("ArticleReader")!;
|
||||
var engine = Assert.IsAssignableFrom<IMarkdownEngine2>(reader.Engine);
|
||||
Assert.Null(engine.HyperlinkCommand);
|
||||
Assert.NotNull(engine.HyperlinkCommand);
|
||||
Assert.False(engine.HyperlinkCommand.CanExecute("https://example.test/"));
|
||||
page.DataContext = vm;
|
||||
var nav = new NavigationPage();
|
||||
await nav.PushAsync(page);
|
||||
|
|
@ -152,7 +153,8 @@ public class BlogReaderTests
|
|||
Dispatcher.UIThread.RunJobs();
|
||||
Assert.Empty(vm.ReaderTitle);
|
||||
Assert.Null(vm.ReaderDateModified);
|
||||
Assert.Null(engine.HyperlinkCommand);
|
||||
Assert.NotNull(engine.HyperlinkCommand);
|
||||
Assert.False(engine.HyperlinkCommand.CanExecute("https://example.test/"));
|
||||
Assert.DoesNotContain(sink.Entries, entry => entry.Area == LogArea.Binding);
|
||||
}
|
||||
finally
|
||||
|
|
|
|||
|
|
@ -4,20 +4,32 @@ using Avalonia.Controls;
|
|||
using Avalonia.Platform.Storage;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Markdown.Avalonia;
|
||||
|
||||
namespace PostIt.Views.Blogs;
|
||||
|
||||
public partial class BlogsPage : ContentPage
|
||||
{
|
||||
private static readonly RelayCommand<string?> DisabledLinkCommand =
|
||||
new(static _ => { }, static _ => false);
|
||||
|
||||
public BlogsPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContextChanged += (_, _) =>
|
||||
DataContextChanged += (_, _) => UpdateHyperlinkCommand();
|
||||
UpdateHyperlinkCommand();
|
||||
}
|
||||
|
||||
private void UpdateHyperlinkCommand()
|
||||
{
|
||||
if (ArticleReader.Engine is IMarkdownEngine2 engine)
|
||||
{
|
||||
if (ArticleReader.Engine is IMarkdownEngine2 engine)
|
||||
engine.HyperlinkCommand = (DataContext as ViewModels.BlogsViewModel)?.OpenResourceCommand;
|
||||
};
|
||||
if (DataContext is ViewModels.BlogsViewModel vm)
|
||||
engine.HyperlinkCommand = vm.OpenResourceCommand;
|
||||
else
|
||||
engine.HyperlinkCommand = DisabledLinkCommand;
|
||||
}
|
||||
}
|
||||
|
||||
protected override async void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
|
|
|
|||
Loading…
Reference in a new issue