diff --git a/src/libs/Clipboard/index.js b/src/libs/Clipboard/index.js index efffd9626e03..b770b2f2c787 100644 --- a/src/libs/Clipboard/index.js +++ b/src/libs/Clipboard/index.js @@ -29,6 +29,24 @@ function setHTMLSync(html, text) { document.body.appendChild(node); const selection = window.getSelection(); + const firstAnchorChild = selection.anchorNode && selection.anchorNode.firstChild; + const isComposer = firstAnchorChild instanceof HTMLTextAreaElement; + let originalSelection = null; + if (isComposer) { + originalSelection = { + start: firstAnchorChild.selectionStart, + end: firstAnchorChild.selectionEnd, + direction: firstAnchorChild.selectionDirection, + }; + } else { + originalSelection = { + anchorNode: selection.anchorNode, + anchorOffset: selection.anchorOffset, + focusNode: selection.focusNode, + focusOffset: selection.focusOffset, + }; + } + selection.removeAllRanges(); const range = document.createRange(); range.selectNodeContents(node); @@ -42,6 +60,13 @@ function setHTMLSync(html, text) { } selection.removeAllRanges(); + + if (isComposer) { + firstAnchorChild.setSelectionRange(originalSelection.start, originalSelection.end, originalSelection.direction); + } else { + selection.setBaseAndExtent(originalSelection.anchorNode, originalSelection.anchorOffset, originalSelection.focusNode, originalSelection.focusOffset); + } + document.body.removeChild(node); }