TelegramPWA/src/util/insertHtmlInSelection.ts
Alexander Zinchuk 3afcde3217 Initial commit
2021-04-09 14:11:51 +03:00

19 lines
581 B
TypeScript

export default function insertHtmlInSelection(html: string) {
const selection = window.getSelection();
if (selection && selection.getRangeAt && selection.rangeCount) {
const range = selection.getRangeAt(0);
range.deleteContents();
const fragment = range.createContextualFragment(html);
const lastInsertedNode = fragment.lastChild;
range.insertNode(fragment);
if (lastInsertedNode) {
range.setStartAfter(lastInsertedNode);
range.setEndAfter(lastInsertedNode);
selection.removeAllRanges();
selection.addRange(range);
}
}
}