private bool SelectionIsWithinWord() { DocumentSpan currentCharacterSpan = new DocumentSpan(selection.Start, 1); string currentText = document.GetTextInSpan(currentCharacterSpan); DocumentSpan previousCharacterSpan = new DocumentSpan(Math.Max(0, selection.Start - 1), 1); string previousText = document.GetTextInSpan(previousCharacterSpan); return(previousText != " " && currentText != " "); }
public void OnNavigatedTo(NavigationContext navigationContext) { this.document = (RichTextDocument)navigationContext.Parameters[HyperlinkDialogParameters.DocumentKey]; this.selection = (Selection)navigationContext.Parameters[HyperlinkDialogParameters.SelectionKey]; HyperlinkNode[] hyperlinksInSpan = document.GetHyperlinks(this.selection.DocumentSpan).ToArray(); if (hyperlinksInSpan.Length > 0) { this.hyperlinkBeingEdited = hyperlinksInSpan[0]; this.TextToDisplay = this.hyperlinkBeingEdited.GetText(); this.Address = this.hyperlinkBeingEdited.Uri; this.ToolTipText = this.hyperlinkBeingEdited.Tooltip; this.dialogMode = HyperlinkDialogMode.EditingHyperlink; } else { if (selection.Length > 0) { this.dialogMode = HyperlinkDialogMode.MakingHyperlink; this.TextToDisplay = this.selection.Text; } else { if (this.SelectionIsWithinWord()) { selection.SelectCurrentWord(); } string textInSelection = document.GetTextInSpan(selection.DocumentSpan); if (textInSelection.Trim().Length < 1) { this.dialogMode = HyperlinkDialogMode.InsertingHyperlink; selection.Collapse(SelectionCollapseDirection.Start); } else { this.dialogMode = HyperlinkDialogMode.MakingHyperlink; this.TextToDisplay = selection.Text; } } } }