This is a migrated thread and some comments may be shown as answers.

Modify selection contents

1 Answer 79 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Mihajlo
Top achievements
Rank 1
Iron
Mihajlo asked on 27 Nov 2018, 01:49 PM

     Let's say I want to give my user the ability to select some text and convert it to some other text, for instance to mangle it with ROT13 cipher. And selection could be made of several ranges, including in table cells. How would I go about this? My first idea was something like the code below, but some kind of update is required, because I only see the change if I scroll the text out view and back in, and even then the changed text does not reflow, but it continues after the right margin. Also, ability to undo the changes would be nice.

foreach (var inlineLayoutBox in radRichTextEditor1.Document.Selection.GetSelectedBoxes())
{
    if (inlineLayoutBox.Text.Contains('e'))
        inlineLayoutBox.Text = inlineLayoutBox.Text.Replace("e", "3333333");
}

 

1 Answer, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 30 Nov 2018, 12:49 PM
Hello Mihajlo,

With the current approach, there is a direct change in the model of the document. As a result, the rendering is not notified of the change, thus it is not updated. What you can do to update the document is to invoke the InvalidateMeasureUpToTheRoot() method on each of the layout boxes that are changed and the UpdateEditorLayout() of RadRichTextBox to force the measuring processes and update the visualization.

My concern here is that using this approach might be a bit risky - the layout boxes are responsible for the rendering and a single span can be represented by several layout boxes. This on its turn, might lead to incorrect behavior in the application. For example, a span can be separated in different layout boxes if a part of its letters has a different styling.

What I can suggest as an alternative implementation is to use the DocumentTextSearch class, which allows you to apply a particular range to search inside through the overloads of the Find() method. You can iterate the ranges of the selection, obtain their positions and perform the search operation on them. Something to note here is that would be a better solution to iterate the selection ranges and the matches from the search backward - from the last item to the first one to avoid additional updates.

Hope this information is helpful.

Regards,
Tanya
Progress Telerik
Get quickly and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
RichTextEditor
Asked by
Mihajlo
Top achievements
Rank 1
Iron
Answers by
Tanya
Telerik team
Share this question
or