Skip to content

Commit 7c4c2af

Browse files
committed
fix some bugs when pasting
1 parent e2fa5f9 commit 7c4c2af

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/RichSuggestBox/RichSuggestBox.Helpers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ private static void ForEachLinkInDocument(ITextDocument document, Action<ITextRa
7575
linkRange.Expand(TextRangeUnit.Link);
7676

7777
// Adjacent links have the same index. Manually check each link with Collapse and Expand.
78-
var previousText = linkRange.Text;
78+
var previousStart = linkRange.StartPosition;
7979
var hasAdjacentToken = true;
8080
while (hasAdjacentToken)
8181
{
8282
action?.Invoke(linkRange);
8383

8484
linkRange.Collapse(false);
8585
linkRange.Expand(TextRangeUnit.Link);
86-
hasAdjacentToken = !string.IsNullOrEmpty(linkRange.Text) && linkRange.Text != previousText;
87-
previousText = linkRange.Text;
86+
hasAdjacentToken = !string.IsNullOrEmpty(linkRange.Link) && linkRange.StartPosition != previousStart;
87+
previousStart = linkRange.StartPosition;
8888
}
8989

9090
nextIndex = range.GetIndex(TextRangeUnit.Link);

Microsoft.Toolkit.Uwp.UI.Controls/RichSuggestBox/RichSuggestBox.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,13 @@ private ITextRange CommitSuggestionIntoDocument(ITextRange range, string display
388388
_ignoreChange = true;
389389
TextDocument.BeginUndoGroup();
390390

391-
range.SetText(TextSetOptions.Unhide, displayText);
391+
// We don't want to set text when the display text doesn't change since it may lead to unexpected caret move.
392+
range.GetText(TextGetOptions.NoHidden, out var existingText);
393+
if (existingText != displayText)
394+
{
395+
range.SetText(TextSetOptions.Unhide, displayText);
396+
}
397+
392398
range.Link = $"\"{id}\"";
393399

394400
range.CharacterFormat.BackgroundColor = format.Background;

0 commit comments

Comments
 (0)