This commit is contained in:
Nik Butenko 2015-10-15 01:41:33 +00:00
commit 0c75db6b49
3 changed files with 75 additions and 1 deletions

68
example/index.html Normal file
View File

@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
<style>
.copy {
display: inline-block;
padding: 2px 5px;
font: inherit;
border-radius: 5px;
background: rgba(0, 0, 0, 0.1);
cursor: default;
}
button.copy {
border: 1px solid rgba(0, 0, 0, 0.3);
}
#span, #input {
display: inline-block;
width: 100px;
padding: 2px 5px;
margin-right: 10px;
font: inherit;
border: 1px solid rgba(0, 0, 0, 0.1);
border-radius: 5px;
}
</style>
</head>
<body>
<input type="text" id="input" value="input">
<span class="copy" data-clipboard-action="copy" data-clipboard-target="#input">
&lt;span&gt;
</span>
<button class="copy" data-clipboard-action="copy" data-clipboard-target="#input">
&lt;button&gt;
</button>
<br>
<br>
<span id="span">span</span>
<span class="copy" data-clipboard-action="copy" data-clipboard-target="#span">
&lt;span&gt;
</span>
<button class="copy" data-clipboard-action="copy" data-clipboard-target="#span">
&lt;button&gt;
</button>
<br><br>
<textarea cols="30" rows="10" placeholder="Paste here"></textarea>
<script src="../dist/clipboard.js"></script>
<script>
new Clipboard('.copy');
</script>
</body>
</html>

View File

@ -25,7 +25,8 @@
"babelify": "^6.3.0",
"browserify": "^11.2.0",
"delegate-events": "^1.1.1",
"tiny-emitter": "^1.0.0"
"tiny-emitter": "^1.0.0",
"toggle-selection": "^1.0.4"
},
"devDependencies": {
"karma": "^0.13.10",

View File

@ -1,3 +1,5 @@
import deselectCurrent from 'toggle-selection';
/**
* Inner class which performs selection from either `text` or `target`
* properties and then executes copy or cut operations.
@ -92,6 +94,7 @@ class ClipboardAction {
this.selectedText = this.target.value;
}
else {
const reselectPrevious = deselectCurrent();
let range = document.createRange();
let selection = window.getSelection();
@ -99,6 +102,8 @@ class ClipboardAction {
range.selectNodeContents(this.target);
selection.addRange(range);
this.selectedText = selection.toString();
reselectPrevious();
}
this.copyText();