How to get a text element where the click was?

Hello! I use DocumentEditorComponent.
I want to get a text element that the user has clicked on. I need it in a few cases. One of them: I want to change the context menu depending on the clicked text element.

Thanks!

3 Replies 1 reply marked as answer

KB Kurthis Banu Abdul Majeeth Syncfusion Team June 21, 2021 12:40 PM UTC

Hi Yuliia, 

You can get the selected text using below code snippet. 

Code snippet: 
this.container.documentEditor.selection.text; 

Documentation Link: 

Regarding:  I want to change the context menu depending on the clicked text element. 

Document Editor allows you to add custom option in context menu. It can be achieved by using the addCustomMenu() method and custom action is defined using the customContextMenuSelect.Kindly refer the below sample . 

Sample Link: 

Documentation Link: 

Please let us know if you have any questions. 

Regards, 
Kurthis Banu A. 



YU Yuliia June 21, 2021 04:03 PM UTC

Thanks!

this.container.documentEditor.selection.text  It is about the selected text. But what about the clicked text?


KB Kurthis Banu Abdul Majeeth Syncfusion Team June 22, 2021 10:46 AM UTC

Hi Yuliia,  

Regarding: But what about the clicked text? 

Kindly refer the below code snippet to achieve your requirement. 

Code snippet:  
var startOffset = containerInstance.documentEditor.selection.startOffset
var endOffset   = containerInstance.documentEditor.selection.endOffset; 
 
container.documentEditor.selection.selectCurrentWord(); 
 
var text = this.container.documentEditor.selection.text;    // you can customize based on your requirement. 
 
// You can set cursor position to anywhere in the document via select API of selection module. 
 
container.documentEditor.selection.select(startOffset, endOffset) ; 
 
 
 

Documentation Link: 

Please let us know if you have any questions. 

Regards, 
Kurthis Banu A. 


Marked as answer
Loader.
Up arrow icon