Skip to content

Commit

Permalink
change istextelement to iscomposer to simplify check
Browse files Browse the repository at this point in the history
  • Loading branch information
wildan-m committed Jan 24, 2023
1 parent 40c8ea3 commit 493c114
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions src/libs/Clipboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,6 @@ import * as Browser from '../Browser';

const canSetHtml = () => lodashGet(navigator, 'clipboard.write');

/**
* Check if an HTMLElement is text input or text area
* @param {HTMLElement} el
* @returns {Boolean}
*/
const isTextElement = (el) => {
if (el instanceof HTMLInputElement) {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#input_types
if (/|text|email|number|password|search|tel|url/.test(el.type || '')) {
return true;
}
}
if (el instanceof HTMLTextAreaElement) { return true; }

return false;
};

/**
* Deprecated method to write the content as HTML to clipboard.
* @param {String} html HTML representation
Expand All @@ -48,8 +31,9 @@ function setHTMLSync(html, text) {
const selection = window.getSelection();
let originalSelection = null;
const firstAnchorChild = selection.anchorNode && selection.anchorNode.firstChild;
const isComposer = firstAnchorChild instanceof HTMLTextAreaElement;

if (firstAnchorChild && isTextElement(firstAnchorChild)) {
if (isComposer) {
originalSelection = {
start: firstAnchorChild.selectionStart,
end: firstAnchorChild.selectionEnd,
Expand Down Expand Up @@ -78,7 +62,7 @@ function setHTMLSync(html, text) {

selection.removeAllRanges();

if (firstAnchorChild && isTextElement(firstAnchorChild)) {
if (isComposer) {
firstAnchorChild.setSelectionRange(originalSelection.start, originalSelection.end, originalSelection.direction);
} else {
selection.setBaseAndExtent(originalSelection.anchorNode, originalSelection.anchorOffset, originalSelection.focusNode, originalSelection.focusOffset);
Expand Down

0 comments on commit 493c114

Please sign in to comment.