XAML-Map-Control/MapControl/Shared/HyperlinkText.cs

115 lines
3.6 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2021-01-13 21:19:27 +01:00
// © 2021 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
2021-06-14 21:41:37 +02:00
#if WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Documents;
#elif WINDOWS_UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
#else
2020-03-20 21:29:43 +01:00
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
#endif
namespace MapControl
{
public static class HyperlinkText
{
2021-04-12 20:51:08 +02:00
private static readonly Regex regex = new Regex(@"\[([^\]]+)\]\(([^\)]+)\)");
/// <summary>
/// Converts text containing hyperlinks in markdown syntax [text](url)
/// to a collection of Run and Hyperlink inlines.
/// </summary>
public static IEnumerable<Inline> TextToInlines(this string text)
{
var inlines = new List<Inline>();
while (!string.IsNullOrEmpty(text))
{
var match = regex.Match(text);
if (match.Success &&
match.Groups.Count == 3 &&
2020-04-16 19:24:38 +02:00
Uri.TryCreate(match.Groups[2].Value, UriKind.Absolute, out Uri uri))
{
inlines.Add(new Run { Text = text.Substring(0, match.Index) });
text = text.Substring(match.Index + match.Length);
var link = new Hyperlink { NavigateUri = uri };
link.Inlines.Add(new Run { Text = match.Groups[1].Value });
2021-06-14 21:41:37 +02:00
#if !WINUI && !WINDOWS_UWP
link.ToolTip = uri.ToString();
2020-03-20 21:29:43 +01:00
link.RequestNavigate += (s, e) =>
{
try
{
2021-03-12 21:02:39 +01:00
Process.Start(new ProcessStartInfo(e.Uri.ToString()) { UseShellExecute = true });
2020-03-20 21:29:43 +01:00
}
catch (Exception ex)
{
Debug.WriteLine("{0}: {1}", e.Uri, ex);
}
};
#endif
inlines.Add(link);
}
else
{
inlines.Add(new Run { Text = text });
text = null;
}
}
return inlines;
}
public static readonly DependencyProperty InlinesSourceProperty = DependencyProperty.RegisterAttached(
"InlinesSource", typeof(string), typeof(HyperlinkText), new PropertyMetadata(null, InlinesSourcePropertyChanged));
public static string GetInlinesSource(DependencyObject element)
{
return (string)element.GetValue(InlinesSourceProperty);
}
public static void SetInlinesSource(DependencyObject element, string value)
{
element.SetValue(InlinesSourceProperty, value);
}
private static void InlinesSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
InlineCollection inlines = null;
2021-04-12 20:51:08 +02:00
if (obj is TextBlock block)
{
2021-04-12 20:51:08 +02:00
inlines = block.Inlines;
}
2021-04-12 20:51:08 +02:00
else if (obj is Paragraph paragraph)
{
2021-04-12 20:51:08 +02:00
inlines = paragraph.Inlines;
}
2017-08-04 21:38:58 +02:00
if (inlines != null)
{
inlines.Clear();
foreach (var inline in TextToInlines((string)e.NewValue))
{
inlines.Add(inline);
}
}
}
}
}