tracked change from 'insert' to 'delete'?

1 Answer 102 Views
RichTextBox
Suzanne
Top achievements
Rank 1
Suzanne asked on 25 Oct 2022, 10:47 AM

Hello,

I asked this question for the radrichtextbox:

https://www.telerik.com/forums/radrichtextbox-get-selected-revisions#5508333

And the answer was very helpful! Unfortunately I am finding that I need to do this operation in a bit more of a granular fashion. For example, if the tracked change is an insert (for example 'This is brand new text'), is there a way that I can highlight a subset of the inserted text (for example 'brand') and mark it as deleted?

Or, if this is impossible, is there a way to find all text that has been marked as 'strikethrough' and remove it?

1 Answer, 1 is accepted

Sort by
0
Vladislav
Telerik team
answered on 28 Oct 2022, 09:19 AM

Hello Suzanne,

I was able to achieve the desired result by tinkering with the code snippet provided in the other thread. I have marked the changed code in bold. The ModifySelectedRevisions() method takes one new parameter now, which contains the text that we want to mark as deleted.

private void ModifySelectedRevisions(string wordToRemove)
{
    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, wordToRemove);
    }
}

private void ProcessRevision(Revision revision, string wordToRemove)
{
    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.RemoveAllOccurences(startPosition, endPosition, wordToRemove);
    }
}

private void RemoveAllOccurences(DocumentPosition start, DocumentPosition end, string wordToRemove)
{
    DocumentTextSearch documentTextSearch = new DocumentTextSearch(this.radRichTextBox.Document);
    DocumentPosition currentStart = new DocumentPosition(start);

    bool shouldFindNext;
    do
    {
        TextRange find = documentTextSearch.Find(wordToRemove, currentStart, end);
        shouldFindNext = find != null;
        if (shouldFindNext)
        {
            currentStart.MoveToPosition(find.EndPosition);
            this.RemoveTextRange(find);
        }
    }
    while (shouldFindNext);
}

private void RemoveTextRange(TextRange range)
{
    this.radRichTextBox.Document.Selection.SetSelectionStart(range.StartPosition);
    this.radRichTextBox.Document.Selection.AddSelectionEnd(range.EndPosition);
    radRichTextBox.Delete(true);
}

I hope this helps.

Regards,
Vladislav
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.

Tags
RichTextBox
Asked by
Suzanne
Top achievements
Rank 1
Answers by
Vladislav
Telerik team
Share this question
or