diff --git a/README.md b/README.md index 53f0d88..7294642 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,11 @@ Another way of doing it is to copy the content from an `` or ` ## Browser Support +This project relies on both [Select API](https://developer.mozilla.org/en-US/docs/Web/API/Selection) and [execCommand API](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand). When combined, they're supported in the following browsers. + | | | | | | |:---:|:---:|:---:|:---:|:---:| -| 42+ ✔ | 41+ ✔ | 9+ ✔ | 29+ ✔ | ✘ | +| 42+ ✔ | 41+ ✔ | 9+ ✔ | 29+ ✔ | Nope ✘ | ## License diff --git a/dist/clipboard.min.js b/dist/clipboard.min.js index 54e6f7a..1f7f35f 100644 --- a/dist/clipboard.min.js +++ b/dist/clipboard.min.js @@ -1 +1 @@ -"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function a(a,b){for(var c=0;c this.bind(trigger)); } bind(trigger) { - trigger.addEventListener('click', e => { - var value = e.currentTarget.getAttribute('value'); - var targetSelector = e.currentTarget.getAttribute('for'); - var target = document.getElementById(targetSelector); + trigger.addEventListener('click', (e) => this.select(e)); + } - if (value) { - var fake = document.createElement('input'); + select(e) { + var valueAttr = e.currentTarget.getAttribute('value'); + var targetAttr = e.currentTarget.getAttribute('for'); - fake.value = value; - fake.style.opacity = 0; - fake.style.zIndex = -1; + if (valueAttr) { + this.selectValue(valueAttr); + } + else if (targetAttr) { + this.selectTarget(targetAttr); + } + else { + console.error('Missing "for" or "value" attribute'); + } - document.body.appendChild(fake); + e.preventDefault(); + } - fake.select(); - } + selectValue(valueAttr) { + var fake = document.createElement('input'); - if (target) { - if (target.nodeName === 'INPUT' || target.nodeName === 'TEXTAREA') { - target.select(); - } - else { - var range = document.createRange(); - range.selectNode(target); - window.getSelection().addRange(range); - } - } + fake.value = valueAttr; + fake.style.opacity = 0; + fake.style.zIndex = -1; - try { - document.execCommand('copy'); - window.getSelection().removeAllRanges(); + document.body.appendChild(fake); - if (value) { - document.body.removeChild(fake); - } - } - catch (err) { - console.error(err); - } + fake.select(); + this.copy(); - e.preventDefault(); - }); + document.body.removeChild(fake); + } + + selectTarget(targetAttr) { + var target = document.getElementById(targetAttr); + + if (target.nodeName === 'INPUT' || target.nodeName === 'TEXTAREA') { + target.select(); + } + else { + var range = document.createRange(); + range.selectNode(target); + window.getSelection().addRange(range); + } + + this.copy(); + } + + copy() { + try { + document.execCommand('copy'); + window.getSelection().removeAllRanges(); + } + catch (err) { + console.error(err); + } } }