summaryrefslogtreecommitdiff
path: root/Juick/Controls/HyperLinkRichTextBox.cs
blob: 6ec688376769258878f2c6e82754eb2d3aee7c38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace Juick.Controls
{
    public class HyperLinkRichTextBox : RichTextBox
    {
        static readonly Regex UrlRegex = new Regex(@"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.Compiled);
        // TODO: Add more entities
        static readonly Regex JuickEntityRegex = new Regex(@"#(\d+)(/\d+)?", RegexOptions.Compiled);
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(HyperLinkRichTextBox), new PropertyMetadata(default(string), TextPropertyChanged));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        private static void TextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var richTextBox = (HyperLinkRichTextBox)dependencyObject;
            var stringValue = (string)dependencyPropertyChangedEventArgs.NewValue;
            var paragraph = new Paragraph();

            var index = 0;
            foreach (var match in UrlRegex.Matches(stringValue).OfType<Match>().Union(JuickEntityRegex.Matches(stringValue).OfType<Match>()))
            {
                Uri uri = null;
                if (!Uri.TryCreate(match.Value, UriKind.Absolute, out uri))
                {
                    // Juick entity
                    uri = new Uri(string.Format("/ThreadView.xaml?mid={0}", JuickEntityRegex.Replace(match.Value, "$1")), UriKind.Relative);
                }
                if (match.Index > 0)
                {
                    var length = match.Index - index;
                    if (length > 0)
                    {
                        paragraph.Inlines.Add(new Run { Text = stringValue.Substring(index, length) });
                    }
                }

                var hyperLink = new Hyperlink
                {
                    NavigateUri = uri
                };
                if (uri.IsAbsoluteUri)
                {
                    hyperLink.TargetName = "_blank";
                    hyperLink.Inlines.Add(uri.Host);
                }
                else
                {
                    hyperLink.Inlines.Add(match.Value);
                }
                paragraph.Inlines.Add(hyperLink);

                index = match.Index + match.Length;
            }

            if (index == 0 || index < stringValue.Length - 1)
            {
                var lastRunText = stringValue.Substring(index);
                if (lastRunText.Length > 0)
                {
                    paragraph.Inlines.Add(new Run { Text = lastRunText });
                }
            }
            richTextBox.Blocks.Clear();
            richTextBox.Blocks.Add(paragraph);
        }
    }
}