@onesy/utils
Version:
19 lines (15 loc) • 621 B
JavaScript
import clearSelection from './clearSelection';
const insertTextAtSelection = function (value) {
let clear = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (window.getSelection) {
const selection = window.getSelection();
if (!selection) return;
const range = selection.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(value));
} else if (window.document.selection && window.document.selection.createRange) {
window.document.createRange().text = value;
}
if (clear) clearSelection();
};
export default insertTextAtSelection;