Exemplo n.º 1
0
        /// <summary>
        /// Occurs when <see cref="TokensSource"/> property changes.
        /// </summary>
        protected virtual async void OnTokensSourceChanged(string Value)
        {
            if (!TokensChangeHandled)
            {
                TextChangeHandled = true;

                await Dispatcher.BeginInvoke(() =>
                {
                    var d = TokenDelimiter.ToString();

                    //Check to see if delimiter occurs more than once in any one place.
                    var Temp = Regex.Replace(Value, d + "+", d);

                    //If so, correct it
                    if (Temp != Value)
                    {
                        TokensChangeHandled = true;
                        TokensSource        = Temp;
                        TokensChangeHandled = false;
                    }

                    Tokens.Clear();
                    Blocks.Clear();

                    if (Value?.ToString().IsEmpty() == false)
                    {
                        var p = new Paragraph();
                        Tokenizer?.Tokenize(Value, TokenDelimiter)?.ForEach(Token => p.Inlines.Add(GenerateInline(Token)));
                        Blocks.Add(p);
                    }
                });

                TextChangeHandled = false;
            }
        }
Exemplo n.º 2
0
        public IEnumerable <string> GetTokens()
        {
            foreach (InlineUIContainer inline in _rtb.CaretPosition.Paragraph.Inlines.Where(inline => inline is InlineUIContainer && ((inline as InlineUIContainer).Child as TokenItem).TokenKey != null))
            {
                if (inline.Child is TokenItem)
                {
                    var str = (inline.Child as TokenItem).TokenKey.TrimEnd(TokenDelimiter.ToCharArray()).Trim();

                    if (!string.IsNullOrWhiteSpace(str))
                    {
                        yield return(str);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void DeleteTokens(List <string> tokens)
        {
            var para = _rtb.CaretPosition.Paragraph;

            var inlinesToRemove = para.Inlines.Where(inline => inline is InlineUIContainer && ((inline as InlineUIContainer).Child as TokenItem).TokenKey != null).ToList();

            foreach (InlineUIContainer inlineToRemove in inlinesToRemove)
            {
                if (inlineToRemove.Child is TokenItem)
                {
                    var str = (inlineToRemove.Child as TokenItem).TokenKey.TrimEnd(TokenDelimiter.ToCharArray()).Trim();

                    if (tokens.Contains(str))
                    {
                        para.Inlines.Remove(inlineToRemove);
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void ReplaceTextWithToken(string inputText, Token token)
        {
            _surpressTextChangedEvent = true;
            var ok   = !string.IsNullOrWhiteSpace(inputText.Trim().Trim(TokenDelimiter.ToCharArray()));
            var para = _rtb.CaretPosition.Paragraph;

            var matchedRun = para.Inlines.FirstOrDefault(inline =>
            {
                var run = inline as Run;
                return(run != null && run.Text.EndsWith(inputText));
            }) as Run;

            if (matchedRun != null) // Found a Run that matched the inputText
            {
                var tokenContainer = CreateTokenContainer(token);
                para.Inlines.InsertBefore(matchedRun, tokenContainer);

                // Remove only if the Text in the Run is the same as inputText, else split up
                if (matchedRun.Text == inputText)
                {
                    para.Inlines.Remove(matchedRun);
                }
                else // Split up
                {
                    var index   = matchedRun.Text.IndexOf(inputText) + inputText.Length;
                    var tailEnd = new Run(matchedRun.Text.Substring(index));
                    para.Inlines.InsertAfter(matchedRun, tailEnd);
                    para.Inlines.Remove(matchedRun);
                }

                //now append the Text with the token key
                if (!ok)
                {
                    para.Inlines.Remove(tokenContainer);
                }
            }
            _surpressTextChangedEvent = false;
        }