Add content at cursor position with CKEditor 4.5

4

I have a textarea where text will be typed. This text may contain some specific fields (type variables, which will be replaced later). I need to know how to insert these "variables" in the specific location where I need them, for example where the mouse cursor was, since all the examples I have seen so far only include at the end of the text.

An example of how it should work:

" [customer_name] is registered to the system from [dataCadastro] "

The words in bold would be the "variables". You need to get the last mouse position because the user can choose where to insert the fields, which simplifies for him.

Additional information: textarea contains rich text (CKEditor).

As the image below, the user selects the "group" (eg: personal data, company data, vehicle data, etc.) and then the combo "fields" and grouped by group (eg: personal data = > fields: name, age, address, date of birth, etc.). When selecting an item in combo "fields", this item will be added in the textarea, exactly at the last mouse location, which can be either at the end of the field or in the middle of the text.

    
asked by anonymous 25.10.2016 / 19:55

2 answers

5

The method for inserting arbitrary text at the cursor position is this:

CKEDITOR.instances.editor1.insertText( 'texto' );

In addition to insertText , note that there are insertElement and insertHtml methods, choose the most appropriate for the context.

More details in the manual:

  

link

    
25.10.2016 / 21:42
0

I'm not seeing your code, but you can try:

var texto_var = document.getElementById("id_textarea").value = 
    "O" + nomeDoCliente "está cadastrado no sistema desde "+ dataCadastro;
    
25.10.2016 / 20:10