示例#1
0
        public TextPointer Replace(TextRange range, EmojiElement emoji)
        {
            var run = range.Start.Parent as Run;

            if (run == null)
            {
                return(range.End);
            }

            var before  = new TextRange(run.ContentStart, range.Start).Text;
            var after   = new TextRange(range.End, run.ContentEnd).Text;
            var inlines = run.SiblingInlines;

            /* Insert new inlines in reverse order after the run */
            if (!string.IsNullOrEmpty(after))
            {
                inlines.InsertAfter(run, new Run(after));
            }

            inlines.InsertAfter(run, emoji);

            if (!string.IsNullOrEmpty(before))
            {
                inlines.InsertAfter(run, new Run(before));
            }

            TextPointer ret = emoji.ContentEnd; // FIXME

            inlines.Remove(run);
            return(ret);
        }
示例#2
0
        private void FixEmojis()
        {
            if (m_pending_change)
            {
                return;
            }

            /* This will prevent our operation from polluting the undo buffer, but it
             * will create an infinite undo stack... need to fix this. */
            BeginChange();

            m_pending_change = true;

            TextPointer cur = Document.ContentStart;

            while (cur.CompareTo(Document.ContentEnd) < 0)
            {
                TextPointer next = cur.GetNextInsertionPosition(LogicalDirection.Forward);
                if (next == null)
                {
                    break;
                }

                TextRange word  = new TextRange(cur, next);
                var       emoji = EmojiElement.MakeFromString(word.Text);
                if (emoji != null)
                {
                    // Test this so as to preserve caret position
                    bool caret_was_next = (0 == next.CompareTo(CaretPosition));

                    next = Replace(word, emoji);
                    if (caret_was_next)
                    {
                        CaretPosition = next;
                    }
                }

                cur = next;
            }

            EndChange();

            m_pending_change = false;
        }