mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
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:
parent
076e3b8a64
commit
34c798851d
2
dist/clipboard.min.js
vendored
2
dist/clipboard.min.js
vendored
File diff suppressed because one or more lines are too long
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user