protected void UpdateParagraph() { untouched.Text = CurrentText.Substring(Math.Min(CurrentText.Length, UserText.Length)); PWrapper.Inlines.Clear(); foreach (Run r in Runs) { PWrapper.Inlines.Add(r); } PWrapper.Inlines.Add(Building); PWrapper.Inlines.Add(untouched); TextPointer docStart = CurrentTextTB.Document.ContentStart; TextPointer docEnd = CurrentTextTB.Document.ContentEnd; int trueLength = docStart.GetOffsetToPosition(docEnd); int userStringEndOffset = trueLength - (CurrentText.Length - UserText.Length) - 2; TextPointer tp = CurrentTextTB.Document.ContentStart; tp = tp.GetPositionAtOffset(userStringEndOffset + 10); if (tp == null) { tp = docEnd; } CurrentTextTB.Selection.Select(tp, tp); }
/// <summary> /// Loads the markdown document into the CurrentText /// </summary> /// <param name="filename"></param> /// <returns></returns> public bool Load(string filename = null, SecureString password = null) { if (string.IsNullOrEmpty(filename)) { filename = Filename; } if (password == null) { password = Password; } else { Password = password; } if (!File.Exists(filename)) { FileCrc = null; return(false); } Filename = filename; UpdateCrc(); GetFileEncoding(); try { CurrentText = File.ReadAllText(filename, Encoding); if (password != null) { if (CurrentText.StartsWith(ENCRYPTION_PREFIX)) { string encrypted = CurrentText.Substring(ENCRYPTION_PREFIX.Length); CurrentText = Encryption.DecryptString(encrypted, password.GetString()); if (string.IsNullOrEmpty(CurrentText)) { return(false); } } } OriginalText = CurrentText; AutoSaveBackups = mmApp.Configuration.AutoSaveBackups; AutoSaveDocuments = mmApp.Configuration.AutoSaveDocuments; ProcessScripts = mmApp.Configuration.MarkdownOptions.AllowRenderScriptTags; IsDirty = false; } catch { return(false); } return(true); }
public void DeleteCharacter() { if (Text.text.Length == 1) { return; } Text.text = CurrentText.Substring(0, CurrentText.Length - 1) + Caret; if (_capsOn) { _capsOn = false; UpdateLetters(); } }
private void textBox1_TextChanged(object sender, TextChangedEventArgs e) { if (ReplacingNow) { return; } if (!WordStart) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { switch (textBoxContent.Text.Substring(change.Offset, 1)) { case " ": case ",": case ".": case ":": case ";": case "(": case ")": case "!": case "?": case "\"": WordStart = true; UsernameStarted = false; HashtagStarted = false; break; default: WordStart = false; break; } if (WordStart) { break; } } } } if (UsernameStarted || HashtagStarted) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { switch (textBoxContent.Text.Substring(change.Offset, 1)) { case " ": case ",": case ".": case ":": case ";": case "(": case ")": case "!": case "?": case "\"": WordStart = true; UsernameStarted = false; HashtagStarted = false; break; default: WordStart = false; break; } if (WordStart) { break; } } } } if (WordStart && (!UsernameStarted && !HashtagStarted)) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { if (textBoxContent.Text.Substring(change.Offset, 1) == " ") { WordStart = true; continue; } else { WordStart = false; } if (textBoxContent.Text.Substring(change.Offset, 1) == "@") { UsernameStarted = true; StartPosition = change.Offset; CurrentText = ""; } else if (textBoxContent.Text.Substring(change.Offset, 1) == "#") { HashtagStarted = true; StartPosition = change.Offset; CurrentText = ""; } } } } if (UsernameStarted) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { CurrentText += textBoxContent.Text.Substring(change.Offset, change.AddedLength); } else if (change.RemovedLength > 0) { if (change.RemovedLength >= CurrentText.Length) { UsernameStarted = false; CurrentText = ""; MatchingTexts.Clear(); popupMatchingUsernames.IsOpen = false; return; } else if (change.RemovedLength == CurrentText.Length) { WordStart = true; CurrentText = ""; popupMatchingUsernames.IsOpen = false; MatchingTexts.Clear(); } else { CurrentText = CurrentText.Substring(0, CurrentText.Length - change.RemovedLength); } } } if (Usernames.Where(name => name.ToLower().StartsWith(CurrentText.ToLower())).Count() > 0) { MatchingTexts.Clear(); foreach (string username in Usernames.Where(name => name.ToLower().StartsWith(CurrentText.ToLower()))) { MatchingTexts.Add(username); if (MatchingTexts.Count >= Properties.Settings.Default.MaxNumberOfEntriesInAutocomplete) { break; } } popupMatchingUsernames.PlacementRectangle = textBoxContent.GetRectFromCharacterIndex(textBoxContent.CaretIndex, true); popupMatchingUsernames.IsOpen = true; } else { MatchingTexts.Clear(); popupMatchingUsernames.IsOpen = false; } } else if (HashtagStarted) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { CurrentText += textBoxContent.Text.Substring(change.Offset, change.AddedLength); } else if (change.RemovedLength > 0) { if (change.RemovedLength >= CurrentText.Length) { HashtagStarted = false; CurrentText = ""; MatchingTexts.Clear(); popupMatchingUsernames.IsOpen = false; return; } else if (change.RemovedLength == CurrentText.Length) { WordStart = true; CurrentText = ""; popupMatchingUsernames.IsOpen = false; MatchingTexts.Clear(); } else { CurrentText = CurrentText.Substring(0, CurrentText.Length - change.RemovedLength); } } } if (Hashtags.Where(tag => tag.ToLower().StartsWith(CurrentText.ToLower())).Count() > 0) { MatchingTexts.Clear(); foreach (string hashtag in Hashtags.Where(tag => tag.ToLower().StartsWith(CurrentText.ToLower()))) { MatchingTexts.Add(hashtag); if (MatchingTexts.Count >= Properties.Settings.Default.MaxNumberOfEntriesInAutocomplete) { break; } } popupMatchingUsernames.PlacementRectangle = textBoxContent.GetRectFromCharacterIndex(textBoxContent.CaretIndex, true); popupMatchingUsernames.IsOpen = true; } else { MatchingTexts.Clear(); popupMatchingUsernames.IsOpen = false; } } }
private void textBox1_TextChanged(object sender, TextChangedEventArgs e) { if (ReplacingNow) { return; } if (!WordStart) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { switch (textBoxContent.Text.Substring(change.Offset, 1)) { case " ": case ",": case ".": case ":": case ";": case "(": case ")": case "!": case "?": case "\"": WordStart = true; UsernameStarted = false; HashtagStarted = false; break; default: WordStart = false; break; } if (WordStart) { break; } } } } if (UsernameStarted || HashtagStarted) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { switch (textBoxContent.Text.Substring(change.Offset, 1)) { case " ": case ",": case ".": case ":": case ";": case "(": case ")": case "!": case "?": case "\"": WordStart = true; UsernameStarted = false; HashtagStarted = false; break; default: WordStart = false; break; } if (WordStart) { break; } } } } if (WordStart && (!UsernameStarted && !HashtagStarted)) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { if (textBoxContent.Text.Substring(change.Offset, 1) == " ") { WordStart = true; continue; } else { WordStart = false; } if (textBoxContent.Text.Substring(change.Offset, 1) == "@") { UsernameStarted = true; StartPosition = change.Offset; CurrentText = ""; } else if (textBoxContent.Text.Substring(change.Offset, 1) == "#") { HashtagStarted = true; StartPosition = change.Offset; CurrentText = ""; } } } } if (UsernameStarted) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { CurrentText += textBoxContent.Text.Substring(change.Offset, change.AddedLength); } else if (change.RemovedLength > 0) { if (change.RemovedLength >= CurrentText.Length) { UsernameStarted = false; CurrentText = ""; MatchingTexts.Clear(); popupMatchingUsernames.IsOpen = false; return; } else if (change.RemovedLength == CurrentText.Length) { WordStart = true; CurrentText = ""; popupMatchingUsernames.IsOpen = false; MatchingTexts.Clear(); } else { CurrentText = CurrentText.Substring(0, CurrentText.Length - change.RemovedLength); } } } if (AppController.Current.AllKnownUsernames.Where(name => name.ToLower().StartsWith(CurrentText.ToLower())).Count() > 0) { MatchingTexts.Clear(); foreach (string username in AppController.Current.AllKnownUsernames.Where(name => name.ToLower().StartsWith(CurrentText.ToLower()))) { MatchingTexts.Add(username); if (MatchingTexts.Count >= 10) { break; } } popupMatchingUsernames.PlacementRectangle = textBoxContent.GetRectFromCharacterIndex(textBoxContent.CaretIndex, true); popupMatchingUsernames.IsOpen = true; } else { MatchingTexts.Clear(); popupMatchingUsernames.IsOpen = false; } } else if (HashtagStarted) { foreach (TextChange change in e.Changes) { if (change.AddedLength > 0) { CurrentText += textBoxContent.Text.Substring(change.Offset, change.AddedLength); } else if (change.RemovedLength > 0) { if (change.RemovedLength >= CurrentText.Length) { HashtagStarted = false; CurrentText = ""; MatchingTexts.Clear(); popupMatchingUsernames.IsOpen = false; return; } else if (change.RemovedLength == CurrentText.Length) { WordStart = true; CurrentText = ""; popupMatchingUsernames.IsOpen = false; MatchingTexts.Clear(); } else { CurrentText = CurrentText.Substring(0, CurrentText.Length - change.RemovedLength); } } } if (AppController.Current.AllKnownHashtags.Where(tag => tag.ToLower().StartsWith(CurrentText.ToLower())).Count() > 0) { MatchingTexts.Clear(); foreach (string hashtag in AppController.Current.AllKnownHashtags.Where(tag => tag.ToLower().StartsWith(CurrentText.ToLower()))) { MatchingTexts.Add(hashtag); if (MatchingTexts.Count >= 10) { break; } } popupMatchingUsernames.PlacementRectangle = textBoxContent.GetRectFromCharacterIndex(textBoxContent.CaretIndex, true); popupMatchingUsernames.IsOpen = true; } else { MatchingTexts.Clear(); popupMatchingUsernames.IsOpen = false; } } #region links as markups string pattern = @"\[(?<markupText>[^\]]+)\]\((?<markupLink>[^)]+)\)"; NumberOfChars = textBoxContent.Text.Length; MarkdownLinksInText.Clear(); foreach (Match match in Regex.Matches(textBoxContent.Text, pattern, RegexOptions.IgnoreCase)) { if (!MarkdownLinksInText.ContainsKey(match.Groups["markupLink"].Value)) { MarkdownLinksInText.Add(match.Groups["markupLink"].Value, match.Groups["markupText"].Value); } NumberOfChars -= 4 + match.Groups["markupLink"].Value.Length; } #endregion }