From b66010bf77efb6cec8ac202f99d965a166cae2a3 Mon Sep 17 00:00:00 2001 From: vitormalencar Date: Thu, 25 Feb 2021 16:16:12 +0100 Subject: [PATCH] feat: decoupling create fakeElemet logic --- src/clipboard-action.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/clipboard-action.js b/src/clipboard-action.js index fda4be3..67412ce 100644 --- a/src/clipboard-action.js +++ b/src/clipboard-action.js @@ -39,19 +39,12 @@ class ClipboardAction { this.selectTarget(); } } - /** * Creates a fake textarea element, sets its value from `text` property, - * and makes a selection on it. */ - selectFake() { + createFakeElement() { const isRTL = document.documentElement.getAttribute('dir') == 'rtl'; - this.fakeHandlerCallback = () => this.removeFake(); - this.fakeHandler = - this.container.addEventListener('click', this.fakeHandlerCallback) || - true; - this.fakeElem = document.createElement('textarea'); // Prevent zooming on iOS this.fakeElem.style.fontSize = '12pt'; @@ -69,9 +62,26 @@ class ClipboardAction { this.fakeElem.setAttribute('readonly', ''); this.fakeElem.value = this.text; - this.container.appendChild(this.fakeElem); + return this.fakeElem; + } + + /** + * Get's the value of fakeElem, + * and makes a selection on it. + */ + selectFake() { + const fakeElem = this.createFakeElement(); + + this.fakeHandlerCallback = () => this.removeFake(); + + this.fakeHandler = + this.container.addEventListener('click', this.fakeHandlerCallback) || + true; + + this.container.appendChild(fakeElem); + + this.selectedText = select(fakeElem); - this.selectedText = select(this.fakeElem); this.copyText(); this.removeFake();