Add variable to textarea editor

Asked

Viewed 115 times

1

I’m using the https://summernote.org/ to create a text editor.

At the time of editing, it is necessary to add variables, which at the time of printing will be exchanged for the value corresponding to each variable.

Example: The $eCNPJ variable will be a substitute and the corresponding CNPJ will appear.

PROBLEM:

When clicking on the variable, it should appear in the inside of the editor at the marked position, this is not happening.

It seems that when code is executed in the browser, the textarea id is changed <textarea id="editor1" class="form-control" name="texto"></textarea>.

So far I haven’t been able to fix it.

CODE:

$(document).ready(function() {

  $('#editor1').summernote({
    tabsize: 2,
    height: 200
  });
  
  

  $('.button').click(function(event) {
    event.preventDefault();
    const editor = document.getElementById('editor1');
    const posicaoFinal = editor.selectionStart + this.innerText.length;

    editor.value =
      editor.value.slice(0, editor.selectionStart) +
      this.innerText +
      editor.value.slice(editor.selectionEnd);

    editor.setSelectionRange(posicaoFinal, posicaoFinal);
    editor.focus();
  });
  
});
<!-- include libraries(jQuery, bootstrap) -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script>


<!-- include summernote css/js -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.js"></script>


<textarea id="editor1" class="form-control" name="texto"></textarea>

<br>

<small><strong>ESCOLA...:</strong>
  <a href="#" title="CNPJ" class="button">$eCNPJ</a>
  - <a href="#" title="Rasão Social" class="button">$eRSocial</a>
  - <a href="#" title="Nome Fantasia" class="button"> $eNFantasia</a>
  - <a href="#" title="E-mail" class="button"> $eEmail</a>
  - <a href="#" title="Telefone" class="button"> $eTelefone</a>
</small>

1 answer

2


That’s what you need to happen?

$(document).ready(function() {

  $('#editor1').summernote({
    tabsize: 2,
    height: 200
  });
  
  

  $('.button').click(function(event) {
  
    event.preventDefault();
    
    $('#editor1').summernote('insertText', this.innerText);
   
  });
  
});
<!-- include libraries(jQuery, bootstrap) -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script>


<!-- include summernote css/js -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.js"></script>


<textarea id="editor1" class="form-control" name="texto"></textarea>

<br>

<small><strong>ESCOLA...:</strong>
  <a href="#" title="CNPJ" class="button">$eCNPJ</a>
  - <a href="#" title="Rasão Social" class="button">$eRSocial</a>
  - <a href="#" title="Nome Fantasia" class="button"> $eNFantasia</a>
  - <a href="#" title="E-mail" class="button"> $eEmail</a>
  - <a href="#" title="Telefone" class="button"> $eTelefone</a>
</small>

  • Thank you very much Leon!

  • I used this code to paste text without formatting document.querySelector("#editor1").addEventListener("paste", function(e) {&#xA; e.preventDefault();&#xA; var text = e.clipboardData.getData("text/plain");&#xA;&#xA; document.execCommand("insertText", false, text);&#xA; }); but after I changed the editor it no longer pastes without formatting. How can I modify?

  • Check out this link: https://github.com/summernote/summernote/issues/303

  • Leon, a question, question of aesthetics... When adding the variable, how do I have a space in the freight and behind the variable?

  • 1

    $('#editor1'). summernote('insertText', ${this.innerText});

  • Thank you very much!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.