Is there a way to programmatically highlight a portion of the text in the RTE? Like setting the background color of a portion of the text in code?
window.RichTextEditor = {
setBackground: function () {
var rteInstance = document.getElementById('defaultRTE').blazor__instance;
rteInstance.formatter.editorManager.nodeSelection.setSelectionText(
document, rteInstance.inputElement.childNodes[0].childNodes[0], rteInstance.inputElement.childNodes[0].childNodes[0], 9, 20);
return true;
} } |
<head>
…
…
<script src="jsinterop.js"></script> </head> |
<SfButton OnClick="SetBackround">Apply</SfButton>
<SfRichTextEditor ID="defaultRTE" Placeholder="Enter Some Content">
<RichTextEditorToolbarSettings Items="@Tools" />
</SfRichTextEditor> @code { [Inject]
IJSRuntime JsRuntime { get; set; }
private async Task SetBackround()
{
await JsRuntime.InvokeAsync<bool>("RichTextEditor.setBackground");
await this.RteObj.ExecuteCommandAsync(CommandName.BackgroundColor, "yellow"); } . . . } |
Hi
Thanks for the sample code.
Unfortunately, part of what I'm doing is converting the rich text into a word document, and for that EnableXhtml=true is configured on my SfRichTextEditor (otherwise it causes problems with the word conversion)
When I enable that option the selector stops working.
document, rteInstance.inputElement.childNodes[0].childNodes[0].childNodes[0],
rteInstance.inputElement.childNodes[0].childNodes[0].childNodes[0], 9, 20); |
it gives an error when I do mulitable selection
Hi Fadi,
Currently, we are checking your reported query. We will update you with further details on or before April 29, 2022.
Regards,
Buvana S
Hi Fadi,
Thank you for your patience.
You should be able to achieve your requirements using a Document Editor. We provide support for selecting and highlighting the current word, line, and paragraph. To achieve your requirement, you have to move the selection to the place where you need to highlight it.
We have created a sample based on your requirement, please find the sample from below link.
https://www.syncfusion.com/downloads/support/directtrac/general/ze/balzorselectionsample-521091672
Steps to run the above sample.
Run the sample.
Open a word document using the open button in toolbar.
Click on select and highlight button
Code snippet:
SelectionSettings settings = new SelectionSettings() { Extend = false, X = 100.0, Y = 200.0 }; SelectionSettings settings1 = new SelectionSettings() { Extend = false, X = 100.0, Y = 400.0 };
SfDocumentEditor editor = container.DocumentEditor; //move selection to line await container.DocumentEditor.Selection.SelectAsync(settings); //select current line await container.DocumentEditor.Selection.SelectLineAsync(); //highlight selected line container.DocumentEditor.Selection.CharacterFormat.SetHighlightColorAsync(HighlightColor.Pink);
//same for another line await container.DocumentEditor.Selection.SelectAsync(settings1); await container.DocumentEditor.Selection.SelectLineAsync(); container.DocumentEditor.Selection.CharacterFormat.SetHighlightColorAsync(HighlightColor.Red);
|
You can move the selection to the needed place using the select API.
To know more about select API please refer below link.
Once you have moved to the place you can then select either the word or line or paragraph.
For selecting a line ‘SelectLineAsync’ API should be used.
For selecting a paragraph ‘SelectParagraphAsync’ API should be used.
For selecting a word ‘SelectCurrentWordAsync` API should be used.
Once the text was selected , you can add you desired highlight color for the selected content using the SetHighlightColorAsync API
Please let us know if you need any further assistance
Regards,
Buvana S