diff --git a/Samples/LinqToTwitter5/net48/CSharp/AspNetSamples/BlazorDemo/Pages/Index.razor b/Samples/LinqToTwitter5/net48/CSharp/AspNetSamples/BlazorDemo/Pages/Index.razor index 09780d27..2a9a65f6 100644 --- a/Samples/LinqToTwitter5/net48/CSharp/AspNetSamples/BlazorDemo/Pages/Index.razor +++ b/Samples/LinqToTwitter5/net48/CSharp/AspNetSamples/BlazorDemo/Pages/Index.razor @@ -2,6 +2,7 @@ @implements IDisposable @using LinqToTwitter @using BlazorDemo.Models +@using System.Text.RegularExpressions @inject BlazorDemo.Services.BlazorAuthorizer BlazorAuthorizer @@ -11,20 +12,52 @@

Tweets for @BlazorAuthorizer.CredentialStore.ScreenName

-

+ +
+
@foreach (var tweet in tweets) {
-
@@@tweet.ScreenName
-
@tweet.Text
+
@@@tweet.ScreenName
+
@((MarkupString)tweet.Text)
} + + @if (ShowPopup) + { + + } } @code { public List tweets = new List(); + bool ShowPopup = false; + string NewTweet = ""; - // This method is called when the control is initialized protected override void OnInitialized() { // Subscribe to the StateChanged EventHandler @@ -47,6 +80,19 @@ BlazorAuthorizer.StateChanged -= OnBlazorAuthorizerStateChanged; } + void OpenPopup() + { + NewTweet = ""; + // Open the Popup + ShowPopup = true; + } + + void ClosePopup() + { + // Close the Popup + ShowPopup = false; + } + async Task GetTweets() { var ctx = new TwitterContext(BlazorAuthorizer); @@ -58,8 +104,43 @@ { ImageUrl = tweet.User.ProfileImageUrl, ScreenName = tweet.User.ScreenNameResponse, - Text = tweet.FullText - }) - .ToListAsync(); + Text = CreateActiveLinks(tweet.FullText) + }).ToListAsync(); + } + + async Task SendTweet() + { + var ctx = new TwitterContext(BlazorAuthorizer); + + Status responseTweet = await ctx.TweetAsync(NewTweet); + + await GetTweets(); + + ClosePopup(); + } + + // Utility + + /// + /// From: https://www.mikesdotnetting.com/article/140/converting-urls-into-links-with-regex + /// Finds web and email addresses in a string and surrounds then with the appropriate HTML anchor tags + /// + /// + /// String + public string CreateActiveLinks(string s) + { + //Finds URLs with no protocol + var urlregex = new Regex(@"\b\({0,1}(?(www|ftp)\.[^ ,""\s<)]*)\b", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + //Finds URLs with a protocol + var httpurlregex = new Regex(@"\b\({0,1}(?[^>](http://www\.|http://|https://|ftp://)[^,""\s<)]*)\b", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + //Finds email addresses + var emailregex = new Regex(@"\b(?[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)\b", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + s = urlregex.Replace(s, " ${url}"); + s = httpurlregex.Replace(s, " ${url}"); + s = emailregex.Replace(s, "${mail}"); + return s; } } \ No newline at end of file