/// <summary> /// Renders a raw link element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderHyperlink(HyperlinkInline element, IRenderContext context) { if (!(context is InlineRenderContext localContext)) { throw new RenderContextIncorrectException(); } var link = new Hyperlink(); LinkRegister.RegisterNewHyperLink(link, element.Url); var brush = localContext.Foreground; if (LinkForeground != null && !localContext.OverrideForeground) { brush = LinkForeground; } Run linkText = new Run { Text = CollapseWhitespace(context, element.Text), Foreground = brush }; link.Inlines.Add(linkText); localContext.InlineCollection.Add(link); }
/// <summary> /// Renders a raw link element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderHyperlink(HyperlinkInline element, IRenderContext context) { var localContext = context as InlineRenderContext; if (localContext == null) { throw new RenderContextIncorrectException(); } var link = new Hyperlink(); // Register the link LinkRegister.RegisterNewHyperLink(link, element.Url); var brush = localContext.Foreground; if (LinkForeground != null && !localContext.OverrideForeground) { brush = LinkForeground; } // Make a text block for the link Run linkText = new Run { Text = CollapseWhitespace(context, element.Text), Foreground = brush }; link.Inlines.Add(linkText); // Add it to the current inlines localContext.InlineCollection.Add(link); }
/// <summary> /// Renders a link element /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderMarkdownLink(MarkdownLinkInline element, IRenderContext context) { if (!(context is InlineRenderContext localContext)) { throw new RenderContextIncorrectException(); } // HACK: Superscript is not allowed within a hyperlink. But if we switch it around, so // that the superscript is outside the hyperlink, then it will render correctly. // This assumes that the entire hyperlink is to be rendered as superscript. if (AllTextIsSuperscript(element) == false) { var link = new Hyperlink(); LinkRegister.RegisterNewHyperLink(link, element.Url); RemoveSuperscriptRuns(element, insertCaret: true); var childContext = new InlineRenderContext(link.Inlines, context) { Parent = link, WithinHyperlink = true }; if (localContext.OverrideForeground) { link.Foreground = localContext.Foreground; } else if (LinkForeground != null) { link.Foreground = LinkForeground; } RenderInlineChildren(element.Inlines, childContext); context.TrimLeadingWhitespace = childContext.TrimLeadingWhitespace; ToolTipService.SetToolTip(link, element.Tooltip ?? element.Url); localContext.InlineCollection.Add(link); } else { // THE HACK IS ON! // Create a fake superscript element. var fakeSuperscript = new SuperscriptTextInline { Inlines = new List <MarkdownInline> { element } }; RemoveSuperscriptRuns(element, insertCaret: false); RenderSuperscriptRun(fakeSuperscript, context); } }
/// <summary> /// Renders a link element /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderMarkdownLink(MarkdownLinkInline element, IRenderContext context) { if (!(context is InlineRenderContext localContext)) { throw new RenderContextIncorrectException(); } // Regular ol' hyperlink. var link = new Hyperlink(); // Register the link LinkRegister.RegisterNewHyperLink(link, element.Url); // Render the children into the link inline. var childContext = new InlineRenderContext(link.Inlines, context) { Parent = link, WithinHyperlink = true }; if (localContext.OverrideForeground) { link.Foreground = localContext.Foreground; } else if (LinkForeground != null) { link.Foreground = LinkForeground; } RenderInlineChildren(element.Inlines, childContext); context.TrimLeadingWhitespace = childContext.TrimLeadingWhitespace; ToolTipService.SetToolTip(link, element.Tooltip ?? element.Url); // Add it to the current inlines localContext.InlineCollection.Add(link); }
/// <summary> /// Renders an image element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override async void RenderImage(ImageInline element, IRenderContext context) { var localContext = context as InlineRenderContext; if (localContext == null) { throw new RenderContextIncorrectException(); } var inlineCollection = localContext.InlineCollection; var placeholder = InternalRenderTextRun(new TextRunInline { Text = element.Text, Type = MarkdownInlineType.TextRun }, context); var resolvedImage = await ImageResolver.ResolveImageAsync(element.RenderUrl, element.Tooltip); // if image can not be resolved we have to return if (resolvedImage == null) { return; } var image = new Image { Source = resolvedImage, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Stretch = ImageStretch }; HyperlinkButton hyperlinkButton = new HyperlinkButton() { Content = image }; var viewbox = new Viewbox { Child = hyperlinkButton, StretchDirection = Windows.UI.Xaml.Controls.StretchDirection.DownOnly }; viewbox.PointerWheelChanged += Preventative_PointerWheelChanged; var scrollViewer = new ScrollViewer { Content = viewbox, VerticalScrollMode = ScrollMode.Disabled, VerticalScrollBarVisibility = ScrollBarVisibility.Disabled }; var imageContainer = new InlineUIContainer() { Child = scrollViewer }; bool ishyperlink = false; if (element.RenderUrl != element.Url) { ishyperlink = true; } LinkRegister.RegisterNewHyperLink(image, element.Url, ishyperlink); if (ImageMaxHeight > 0) { viewbox.MaxHeight = ImageMaxHeight; } if (ImageMaxWidth > 0) { viewbox.MaxWidth = ImageMaxWidth; } if (element.ImageWidth > 0) { image.Width = element.ImageWidth; image.Stretch = Stretch.UniformToFill; } if (element.ImageHeight > 0) { if (element.ImageWidth == 0) { image.Width = element.ImageHeight; } image.Height = element.ImageHeight; image.Stretch = Stretch.UniformToFill; } if (element.ImageHeight > 0 && element.ImageWidth > 0) { image.Stretch = Stretch.Fill; } // If image size is given then scroll to view overflown part if (element.ImageHeight > 0 || element.ImageWidth > 0) { scrollViewer.HorizontalScrollMode = ScrollMode.Auto; scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; } // Else resize the image else { scrollViewer.HorizontalScrollMode = ScrollMode.Disabled; scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled; } ToolTipService.SetToolTip(image, element.Tooltip); // Try to add it to the current inlines // Could fail because some containers like Hyperlink cannot have inlined images try { var placeholderIndex = inlineCollection.IndexOf(placeholder); inlineCollection.Remove(placeholder); inlineCollection.Insert(placeholderIndex, imageContainer); } catch { // Ignore error } }
/// <summary> /// Renders a raw link element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override void RenderHyperlink(HyperlinkInline element, IRenderContext context) { var localContext = context as InlineRenderContext; if (localContext == null) { throw new RenderContextIncorrectException(); } if (element.LinkType == HyperlinkType.DiscordUserMention || element.LinkType == HyperlinkType.DiscordChannelMention || element.LinkType == HyperlinkType.DiscordRoleMention || element.LinkType == HyperlinkType.DiscordNickMention || element.LinkType == HyperlinkType.QuarrelColor) { bool _halfopacity = false; var link = new HyperlinkButton(); var content = element.Text; bool enabled = true; SolidColorBrush foreground = (SolidColorBrush)Application.Current.Resources["Blurple"]; try { if (element.LinkType == HyperlinkType.DiscordUserMention || element.LinkType == HyperlinkType.DiscordNickMention) { string mentionid = element.Text.Remove(0, (element.LinkType == HyperlinkType.DiscordNickMention ? 2 : 1)); if (Document.Users != null) { Document.Users.TryGetValue(mentionid, out var user); if (!string.IsNullOrEmpty(user.Name)) { link.Tag = mentionid; content = _halfopacity ? user.Name : "@" + user.Name; foreground = IntToColor(user.Colour); /*if (GuildsService.CurrentGuild.Model.Name != "DM") * { * CurrentUsersService.Users.TryGetValue(mentionid, out var member); * if (!string.IsNullOrWhiteSpace(member?.DisplayName)) * { * if (_halfopacity) content = member.DisplayName; * else content = "@" + member.DisplayName; * * foreground = IntToColor(member.TopRole.Color); * } * }*/ } } } else if (element.LinkType == HyperlinkType.DiscordChannelMention) { var key = element.Text.Remove(0, 1); if (Document.Channels != null) { Document.Channels.TryGetValue(key, out var value); content = "#" + value; enabled = true; link.Tag = value; } else { content = "#deleted-channel"; enabled = false; } } else if (element.LinkType == HyperlinkType.DiscordRoleMention) { if (Document.Roles != null && Document.Roles.TryGetValue(element.Text.Remove(0, 2), out var role)) { if (_halfopacity) { content = role.Name; } else { content = "@" + role.Name; } foreground = IntToColor(role.Colour); } else { enabled = false; content = "@deleted-role"; } } else if (element.LinkType == HyperlinkType.QuarrelColor) { string intcolor = element.Text.Replace("@$QUARREL-color", ""); try { var color = IntToColor(Int32.Parse(intcolor)); localContext.InlineCollection.Add(new InlineUIContainer { Child = new Ellipse() { Height = FontSize, Width = FontSize, Fill = color, Margin = new Thickness(0, 0, 2, -2) } }); localContext.InlineCollection.Add(new Run { FontWeight = FontWeights.SemiBold, //Foreground = BoldForeground, Text = color.Color.ToString() }); return; } catch { } } } catch (Exception) { content = "<Invalid Mention>"; } link.Content = CollapseWhitespace(context, content); link.Foreground = foreground; link.FontSize = FontSize; if (_halfopacity) { link.Style = (Style)Application.Current.Resources["DiscordMentionHyperlinkBold"]; } else { link.Style = (Style)Application.Current.Resources["DiscordMentionHyperlink"]; } link.IsEnabled = enabled; LinkRegister.RegisterNewHyperLink(link, element.Url); InlineUIContainer linkContainer = new InlineUIContainer { Child = link }; localContext.InlineCollection.Add(linkContainer); } else { var link = new Hyperlink(); // Register the link LinkRegister.RegisterNewHyperLink(link, element.Url); var brush = localContext.Foreground; if (LinkForeground != null && !localContext.OverrideForeground) { brush = LinkForeground; } // Make a text block for the link Run linkText = new Run { Text = CollapseWhitespace(context, element.Text), Foreground = brush }; link.Inlines.Add(linkText); // Add it to the current inlines localContext.InlineCollection.Add(link); } }
/// <summary> /// Renders an image element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override async void RenderImage(ImageInline element, IRenderContext context) { var localContext = context as InlineRenderContext; if (localContext == null) { throw new RenderContextIncorrectException(); } var inlineCollection = localContext.InlineCollection; var placeholder = InternalRenderTextRun(new TextRunInline { Text = element.Text, Type = MarkdownInlineType.TextRun }, context); var resolvedImage = await ImageResolver.ResolveImageAsync(element.Url, element.Tooltip); // if image can not be resolved we have to return if (resolvedImage == null) { return; } var image = new Image(); var imageContainer = new InlineUIContainer() { Child = image }; LinkRegister.RegisterNewHyperLink(image, element.Url); image.Source = resolvedImage; image.HorizontalAlignment = HorizontalAlignment.Left; image.VerticalAlignment = VerticalAlignment.Top; image.Stretch = ImageStretch; if (element.ImageWidth > 0) { image.Width = element.ImageWidth; image.Stretch = Stretch.UniformToFill; } if (element.ImageHeight > 0) { if (element.ImageWidth == 0) { image.Width = element.ImageHeight; } image.Height = element.ImageHeight; image.Stretch = Stretch.UniformToFill; } if (element.ImageHeight > 0 && element.ImageWidth > 0) { image.Stretch = Stretch.Fill; } ToolTipService.SetToolTip(image, element.Tooltip); // Try to add it to the current inlines // Could fail because some containers like Hyperlink cannot have inlined images try { var placeholderIndex = inlineCollection.IndexOf(placeholder); inlineCollection.Remove(placeholder); inlineCollection.Insert(placeholderIndex, imageContainer); } catch { // Ignore error } }
/// <summary> /// Renders an image element. /// </summary> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> protected override async void RenderImage(ImageInline element, IRenderContext context) { if (!(context is InlineRenderContext localContext)) { throw new RenderContextIncorrectException(); } var inlineCollection = localContext.InlineCollection; var placeholder = InternalRenderTextRun(new TextRunInline { Text = element.Text, Type = MarkdownInlineType.TextRun }, context); var resolvedImage = await ImageResolver.ResolveImageAsync(element.RenderUrl, element.Tooltip); if (resolvedImage == null) { return; } var image = new Image { Source = resolvedImage, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Stretch = ImageStretch }; HyperlinkButton hyperlinkButton = new HyperlinkButton() { Content = image }; var viewbox = new Viewbox { Child = hyperlinkButton, StretchDirection = StretchDirection.DownOnly }; var imageContainer = new InlineUIContainer() { Child = viewbox }; bool ishyperlink = false; if (element.RenderUrl != element.Url) { ishyperlink = true; } LinkRegister.RegisterNewHyperLink(hyperlinkButton, element.Url, ishyperlink); if (ImageMaxHeight > 0) { viewbox.MaxHeight = ImageMaxHeight; } if (ImageMaxWidth > 0) { viewbox.MaxWidth = ImageMaxWidth; } if (element.ImageWidth > 0) { image.Width = element.ImageWidth; image.Stretch = Stretch.UniformToFill; } if (element.ImageHeight > 0) { if (element.ImageWidth == 0) { image.Width = element.ImageHeight; } image.Height = element.ImageHeight; image.Stretch = Stretch.UniformToFill; } if (element.ImageHeight > 0 && element.ImageWidth > 0) { image.Stretch = Stretch.Fill; } ToolTipService.SetToolTip(image, element.Tooltip); // Try to add it to the current inlines // Could fail because some containers like Hyperlink cannot have inlined images try { inlineCollection.InsertAfter(placeholder, imageContainer); inlineCollection.Remove(placeholder); } catch { } }