Skip to content

Commit 1ba3228

Browse files
committed
add Clear() and AddTokens() methods
these 2 methods support loading data into the control programmatically
1 parent 2ffd1ff commit 1ba3228

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ public void ClearUndoRedoSuggestionHistory()
9797
}
9898
}
9999

100+
/// <summary>
101+
/// Clear the document and token list. This will also clear the undo/redo history.
102+
/// </summary>
103+
public void Clear()
104+
{
105+
lock (_tokensLock)
106+
{
107+
_tokens.Clear();
108+
_visibleTokens.Clear();
109+
TextDocument.Selection.Expand(TextRangeUnit.Story);
110+
TextDocument.Selection.Delete(TextRangeUnit.Story, 0);
111+
TextDocument.ClearUndoRedoHistory();
112+
}
113+
}
114+
115+
/// <summary>
116+
/// Add tokens to be tracked against the document. Duplicate tokens will not be updated.
117+
/// </summary>
118+
/// <param name="tokens">The collection of tokens to be tracked.</param>
119+
public void AddTokens(IEnumerable<RichSuggestToken> tokens)
120+
{
121+
lock (_tokensLock)
122+
{
123+
foreach (var token in tokens)
124+
{
125+
_tokens.TryAdd($"\"{token.Id}\"", token);
126+
}
127+
}
128+
}
129+
100130
/// <summary>
101131
/// Try getting the token associated with a text range.
102132
/// </summary>
@@ -673,7 +703,7 @@ private void ValidateTokenFromRange(ITextRange range)
673703

674704
if (token.ToString() != range.Text)
675705
{
676-
range.Delete(TextRangeUnit.Story, -1);
706+
range.Delete(TextRangeUnit.Story, 0);
677707
token.Active = false;
678708
return;
679709
}
@@ -923,7 +953,7 @@ private void UpdateVisibleTokenList()
923953
{
924954
lock (_tokensLock)
925955
{
926-
var toBeRemoved = _visibleTokens.Where(x => !x.Active).ToArray();
956+
var toBeRemoved = _visibleTokens.Where(x => !x.Active || !_tokens.ContainsKey($"\"{x.Id}\"")).ToArray();
927957

928958
foreach (var elem in toBeRemoved)
929959
{

0 commit comments

Comments
 (0)