RadRichTextBox: Get selected revisions?

1 Answer 116 Views
RichTextBox
Suzanne
Top achievements
Rank 1
Suzanne asked on 21 Jun 2022, 10:06 AM

Hello,

I would like to convert select track changes inserts in to track changes delete. I have this code...

Revision rev = radRichTextBox.Document.GetCurrentRevision();
            if (rev.RevisionElements.First().ToString().Contains("Insert"))
            {
                DocumentPosition startPosition = new DocumentPosition(rev.SelectionRange.StartPosition, true);
                DocumentPosition endPosition = new DocumentPosition(rev.SelectionRange.EndPosition, true);
                radRichTextBox.AcceptRevision(rev);
                this.radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
                this.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
                radRichTextBox.Delete(true);
            }

But the problem is that I can only do it for the current revision (Document.GetCurrentRevision). I would like to do it for all selected revisions. Anyone out there have a solution?

1 Answer, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 23 Jun 2022, 02:07 PM

Hello Suzanne,

You can obtain all revisions from the selection through the GetAnnotationMarkersOfType method. An important part to keep the positioning correct is to reverse the collection of revisions so that the modifications go from the last to the first revision. Here is an example:

private void ModifySelectedRevisions()
{
    var insertRangeStarts = this.radRichTextBox.Document.Selection.GetAnnotationMarkersOfType<InsertRangeStart>().ToList();
    insertRangeStarts.Reverse();
    this.radRichTextBox.Document.Selection.Clear();

    foreach (var start in insertRangeStarts)
    {
        var paragraph = start.Parent as Paragraph;
        this.radRichTextBox.Document.CaretPosition.MoveToDocumentElementStart(start);
        var revision = this.radRichTextBox.Document.GetCurrentRevision();
        this.ProcessRevision(revision);
    }
}

private void ProcessRevision(Revision revision)
{
    if (revision.RevisionElements.First().ToString().Contains("Insert"))
    {
        DocumentPosition startPosition = new DocumentPosition(revision.SelectionRange.StartPosition, true);
        DocumentPosition endPosition = new DocumentPosition(revision.SelectionRange.EndPosition, true);
        radRichTextBox.AcceptRevision(revision);
        this.radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
        this.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
        radRichTextBox.Delete(true);
    }
}

Hope this is helpful.

Regards,
Tanya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Suzanne
Top achievements
Rank 1
commented on 24 Jun 2022, 09:23 AM

Thank you for your answer! I finally ended up with this code that seems to do the trick...

List<Revision> revs = radRichTextBox.Document.GetAllRevisions().ToList();
            List<Revision> revsSelected = new List<Revision>();
            for (int i = revs.Count() - 1; i >= 0; i--)
            {
                Revision rev = revs[i];
                if (rev.RevisionElements.First().ToString().Contains("Insert"))
                {
                    if (radRichTextBox.Document.Selection.Ranges.First().StartPosition <= rev.SelectionRange.StartPosition &&
                    radRichTextBox.Document.Selection.Ranges.Last().EndPosition >= rev.SelectionRange.EndPosition)
                    {
                        revsSelected.Add(revs[i]);
                    }
                    else if (radRichTextBox.Document.Selection.Ranges.First().StartPosition <= rev.SelectionRange.StartPosition &&
                    radRichTextBox.Document.Selection.Ranges.Last.EndPosition >= rev.SelectionRange.StartPosition)
                    {
                        revsSelected.Add(revs[i]);
                    }
                    else if (radRichTextBox.Document.Selection.Ranges.First().EndPosition >= rev.SelectionRange.EndPosition &&
                        radRichTextBox.Document.Selection.Ranges.First.StartPosition <= rev.SelectionRange.EndPosition)
                    {
                        revsSelected.Add(revs[i]);
                    }
                }
            }
            for (int i = revsSelected.Count() - 1; i >= 0; i--)
            {
                Revision rev = revsSelected[i];
                if (rev.RevisionElements.First().ToString().Contains("Insert"))
                {
                    DocumentPosition startPosition = new DocumentPosition(rev.SelectionRange.StartPosition, true);
                    DocumentPosition endPosition = new DocumentPosition(rev.SelectionRange.EndPosition, true);
                    radRichTextBox.AcceptRevision(rev);
                    startPosition.MoveToNext();
                    this.radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
                    this.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
                    radRichTextBox.Delete(true);
                }
            }
Tags
RichTextBox
Asked by
Suzanne
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Share this question
or