Only removes the fake element after another click event, that way an user can hit Ctrl+C to copy because selection still exists

This commit is contained in:
Zeno Rocha 2015-09-24 21:25:37 -07:00
parent 076e3b8a64
commit 34c798851d
2 changed files with 30 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@ -16,7 +16,7 @@ class ClipboardAction {
this.selectedText = ''; this.selectedText = '';
if (this.text) { if (this.text) {
this.selectValue(); this.selectFake();
} }
else if (this.target) { else if (this.target) {
this.selectTarget(); this.selectTarget();
@ -24,22 +24,41 @@ class ClipboardAction {
} }
/** /**
* Selects the content from value passed on `text` property. * Creates a fake input element, sets its value from `text` property,
* and makes a selection on it.
*/ */
selectValue() { selectFake() {
let fake = document.createElement('input'); this.removeFake();
fake.style.position = 'absolute'; this.fakeHandler = document.body.addEventListener('click', () => this.removeFake());
fake.style.left = '-9999px';
fake.value = this.text; this.fakeElem = document.createElement('input');
this.fakeElem.style.position = 'absolute';
this.fakeElem.style.left = '-9999px';
this.fakeElem.setAttribute('readonly', '');
this.fakeElem.value = this.text;
this.selectedText = this.text; this.selectedText = this.text;
document.body.appendChild(fake); document.body.appendChild(this.fakeElem);
fake.select(); this.fakeElem.select();
this.copyText(); this.copyText();
}
document.body.removeChild(fake); /**
* Only removes the fake element after another click event, that way
* an user can hit `Ctrl+C` to copy because selection still exists.
*/
removeFake() {
if (this.fakeHandler) {
document.body.removeEventListener('click');
this.fakeHandler = null;
}
if (this.fakeElem) {
document.body.removeChild(this.fakeElem);
this.fakeElem = null;
}
} }
/** /**