JSFiddle - React, Tailwind, and code Playground
HTML
<script
src="https://code.jquery.com/jquery-2.2.3.min.js"
integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="
crossorigin="anonymous"></script>
<script src="//cdn.ckeditor.com/4.5.8/standard/ckeditor.js"></script>
<div>
<button id="insert">
Вставить токен
</button>
<button id="ckeditor">
Включить ckeditor
</button>
<button id="ckeditordel">
Выключить ckeditor
</button>
</div>
<div>
<textarea id="textarea1" rows="10" cols="50">
первая строка
вторая строка
</textarea>
</div>
JavaScript
jQuery.fn.extend({
insertAtCaret: function(myValue){
return this.each(function(i) {
if (document.selection) {
//For browsers like Internet Explorer
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
else if (this.selectionStart || this.selectionStart == '0') {
//For browsers like Firefox and Webkit based
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
});
}
});
CKEDITOR.env.isCompatible = true;
$(document).ready(function(){
var editor = null;
$('#insert').click(function(){
$('#textarea1').insertAtCaret(' [[INSERT]] ');
});
$('#ckeditor').click(function(){
editor = CKEDITOR.replace('textarea1');
});
$('#ckeditordel').click(function(){
if (editor) {
editor.destroy();
editor = null;
}
});
});