Это полностью работает для textarea
и для contenteditable
div:
var newText = 'hello\nnewline';
var newHtml = '<b>test</b> and<br>a <a href="hjkh">link</a>';
document.addEventListener("keydown", function(e) {
var elt = e.target;
if (elt.tagName.toLowerCase() === 'textarea' || elt.isContentEditable)
{
if (e.keyCode == 9) {
e.preventDefault();
if (elt.isContentEditable) { // for contenteditable
elt.focus();
sel = document.getSelection();
sel.modify("extend", "backward", "word");
range = sel.getRangeAt(0);
console.log(range.toString().trim());
range.deleteContents();
var el = document.createElement("div");
el.innerHTML = newHtml;
var frag = document.createDocumentFragment(), node;
while (node = el.firstChild) {
frag.appendChild(node);
}
range.insertNode(frag);
range.collapse();
} else { // for texterea/input element
var endingIndex = elt.selectionStart;
var startingIndex = endingIndex && endingIndex - 1;
var value = elt.value;
var regex = /[ ]/;
while (startingIndex > -1) {
if (regex.test(value[startingIndex])) {
++startingIndex;
break;
}
--startingIndex;
}
if (startingIndex < 0) {
startingIndex = 0;
}
value = value.substring(0, startingIndex) + newText + value.substring(endingIndex);
var cursorPosition = startingIndex + newText.length;
e.target.value = value;
e.target.setSelectionRange(cursorPosition, cursorPosition);
}
}
}
});
<div contenteditable>Hello, press TAB to replace this WORD<br>Also press TAB after this ONE</div>
<textarea rows="8" cols="50" id="a">Hello, press TAB to replace this WORD
Also press TAB after this ONE</textarea>