Merge pull request #12947 from gagahpangeran/markdown-link

Handle link action in `OsuMarkdownLinkText`
This commit is contained in:
Dean Herbert 2021-05-26 16:56:24 +09:00 committed by GitHub
commit 1e77a87fab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 18 deletions

View File

@ -86,6 +86,15 @@ _**italic with underscore, bold with asterisk**_";
}); });
} }
[Test]
public void TestLinkWithTitle()
{
AddStep("Add Link with title", () =>
{
markdownContainer.Text = "[wikipedia](https://www.wikipedia.org \"The Free Encyclopedia\")";
});
}
[Test] [Test]
public void TestInlineCode() public void TestInlineCode()
{ {

View File

@ -6,11 +6,13 @@ using Markdig.Extensions.AutoIdentifiers;
using Markdig.Extensions.Tables; using Markdig.Extensions.Tables;
using Markdig.Extensions.Yaml; using Markdig.Extensions.Yaml;
using Markdig.Syntax; using Markdig.Syntax;
using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Containers.Markdown; using osu.Framework.Graphics.Containers.Markdown;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Online.API;
namespace osu.Game.Graphics.Containers.Markdown namespace osu.Game.Graphics.Containers.Markdown
{ {
@ -21,6 +23,16 @@ namespace osu.Game.Graphics.Containers.Markdown
LineSpacing = 21; LineSpacing = 21;
} }
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var api = parent.Get<IAPIProvider>();
// needs to be set before the base BDL call executes to avoid invalidating any already populated markdown content.
DocumentUrl = api.WebsiteRootUrl;
return base.CreateChildDependencies(parent);
}
protected override void AddMarkdownComponent(IMarkdownObject markdownObject, FillFlowContainer container, int level) protected override void AddMarkdownComponent(IMarkdownObject markdownObject, FillFlowContainer container, int level)
{ {
switch (markdownObject) switch (markdownObject)

View File

@ -1,48 +1,63 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using Markdig.Syntax.Inlines; using Markdig.Syntax.Inlines;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers.Markdown; using osu.Framework.Graphics.Containers.Markdown;
using osu.Framework.Graphics.Sprites; using osu.Game.Online.Chat;
using osu.Framework.Input.Events;
using osu.Game.Overlays; using osu.Game.Overlays;
namespace osu.Game.Graphics.Containers.Markdown namespace osu.Game.Graphics.Containers.Markdown
{ {
public class OsuMarkdownLinkText : MarkdownLinkText public class OsuMarkdownLinkText : MarkdownLinkText
{ {
[Resolved] [Resolved(canBeNull: true)]
private OverlayColourProvider colourProvider { get; set; } private OsuGame game { get; set; }
private SpriteText spriteText; private readonly string text;
private readonly string title;
public OsuMarkdownLinkText(string text, LinkInline linkInline) public OsuMarkdownLinkText(string text, LinkInline linkInline)
: base(text, linkInline) : base(text, linkInline)
{ {
this.text = text;
title = linkInline.Title;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
spriteText.Colour = colourProvider.Light2; var textDrawable = CreateSpriteText().With(t => t.Text = text);
InternalChildren = new Drawable[]
{
textDrawable,
new OsuMarkdownLinkCompiler(new[] { textDrawable })
{
RelativeSizeAxes = Axes.Both,
Action = OnLinkPressed,
TooltipText = title ?? Url,
}
};
} }
public override SpriteText CreateSpriteText() protected override void OnLinkPressed() => game?.HandleLink(Url);
{
return spriteText = base.CreateSpriteText();
}
protected override bool OnHover(HoverEvent e) private class OsuMarkdownLinkCompiler : DrawableLinkCompiler
{ {
spriteText.Colour = colourProvider.Light1; public OsuMarkdownLinkCompiler(IEnumerable<Drawable> parts)
return base.OnHover(e); : base(parts)
} {
}
protected override void OnHoverLost(HoverLostEvent e) [BackgroundDependencyLoader]
{ private void load(OverlayColourProvider colourProvider)
spriteText.Colour = colourProvider.Light2; {
base.OnHoverLost(e); IdleColour = colourProvider.Light2;
HoverColour = colourProvider.Light1;
}
} }
} }
} }