Skip to content

Commit

Permalink
Merge pull request #14506 from wildan-m/wildan-14300-MessageGetUnsele…
Browse files Browse the repository at this point in the history
…cted

Wildan 14300 message get unselected
  • Loading branch information
youssef-lr authored Jan 27, 2023
2 parents 3670839 + 70cf617 commit df05485
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/libs/Clipboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down

0 comments on commit df05485

Please sign in to comment.