mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4ab305297 | ||
|
|
294e9fcb5d | ||
|
|
79c3361ca4 | ||
|
|
c3fefc1fc0 | ||
|
|
42bd266f0b | ||
|
|
53a733fcb5 | ||
|
|
7a5a910bcd | ||
|
|
0163f7cb72 | ||
|
|
941bdbb16e | ||
|
|
00d5cc4e96 | ||
|
|
07a1f8456b | ||
|
|
ff3cd2c722 | ||
|
|
d346f30e5d | ||
|
|
e5fe34c524 | ||
|
|
0a6aace544 | ||
|
|
43d9c11aaf | ||
|
|
90a52149ed | ||
|
|
83b9d6a84d | ||
|
|
3d13baa385 | ||
|
|
7e37c95121 | ||
|
|
bc9bcdd678 | ||
|
|
4c9e29a0dc | ||
|
|
bd6dc9eb9f | ||
|
|
a88bb77be4 | ||
|
|
e86dc2caa2 | ||
|
|
8392a7ba1c | ||
|
|
31e3622e17 | ||
|
|
ee1eb455f4 | ||
|
|
d50f2fb73f | ||
|
|
c8221c742d | ||
|
|
efc22cf651 | ||
|
|
4b27a72dce | ||
|
|
c12c610b22 | ||
|
|
1d772a0cbe | ||
|
|
d9254459b7 | ||
|
|
2aa163b1d0 | ||
|
|
783dc6f3cf |
4
.babelrc
Normal file
4
.babelrc
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"presets": ["es2015-loose"],
|
||||||
|
"plugins": ["transform-es2015-modules-umd"]
|
||||||
|
}
|
||||||
15
.github/issue_template.md
vendored
Normal file
15
.github/issue_template.md
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
### Minimal example
|
||||||
|
|
||||||
|
> Fork this [JSFiddle](https://jsfiddle.net/zenorocha/5kk0eysw/) and reproduce your issue.
|
||||||
|
|
||||||
|
### Expected behaviour
|
||||||
|
|
||||||
|
I thought that by going to the page '...' and pressing the button '...' then '...' would happen.
|
||||||
|
|
||||||
|
### Actual behaviour
|
||||||
|
|
||||||
|
Instead of '...', what I saw was that '...' happened instead.
|
||||||
|
|
||||||
|
### Browsers affected
|
||||||
|
|
||||||
|
I tested on all major browsers and only IE 11 does not work.
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "clipboard",
|
"name": "clipboard",
|
||||||
"version": "1.5.5",
|
"version": "1.5.11",
|
||||||
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "dist/clipboard.js",
|
"main": "dist/clipboard.js",
|
||||||
|
|||||||
811
dist/clipboard.js
vendored
811
dist/clipboard.js
vendored
@@ -1,5 +1,5 @@
|
|||||||
/*!
|
/*!
|
||||||
* clipboard.js v1.5.5
|
* clipboard.js v1.5.10
|
||||||
* https://zenorocha.github.io/clipboard.js
|
* https://zenorocha.github.io/clipboard.js
|
||||||
*
|
*
|
||||||
* Licensed MIT © Zeno Rocha
|
* Licensed MIT © Zeno Rocha
|
||||||
@@ -16,48 +16,7 @@ module.exports = function (element, selector, checkYoSelf) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
},{"matches-selector":2}],2:[function(require,module,exports){
|
},{"matches-selector":5}],2:[function(require,module,exports){
|
||||||
|
|
||||||
/**
|
|
||||||
* Element prototype.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var proto = Element.prototype;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Vendor function.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var vendor = proto.matchesSelector
|
|
||||||
|| proto.webkitMatchesSelector
|
|
||||||
|| proto.mozMatchesSelector
|
|
||||||
|| proto.msMatchesSelector
|
|
||||||
|| proto.oMatchesSelector;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Expose `match()`.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = match;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Match `el` to `selector`.
|
|
||||||
*
|
|
||||||
* @param {Element} el
|
|
||||||
* @param {String} selector
|
|
||||||
* @return {Boolean}
|
|
||||||
* @api public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function match(el, selector) {
|
|
||||||
if (vendor) return vendor.call(el, selector);
|
|
||||||
var nodes = el.parentNode.querySelectorAll(selector);
|
|
||||||
for (var i = 0; i < nodes.length; ++i) {
|
|
||||||
if (nodes[i] == el) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},{}],3:[function(require,module,exports){
|
|
||||||
var closest = require('closest');
|
var closest = require('closest');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,16 +26,17 @@ var closest = require('closest');
|
|||||||
* @param {String} selector
|
* @param {String} selector
|
||||||
* @param {String} type
|
* @param {String} type
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
|
* @param {Boolean} useCapture
|
||||||
* @return {Object}
|
* @return {Object}
|
||||||
*/
|
*/
|
||||||
function delegate(element, selector, type, callback) {
|
function delegate(element, selector, type, callback, useCapture) {
|
||||||
var listenerFn = listener.apply(this, arguments);
|
var listenerFn = listener.apply(this, arguments);
|
||||||
|
|
||||||
element.addEventListener(type, listenerFn);
|
element.addEventListener(type, listenerFn, useCapture);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
destroy: function() {
|
destroy: function() {
|
||||||
element.removeEventListener(type, listenerFn);
|
element.removeEventListener(type, listenerFn, useCapture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,7 +62,7 @@ function listener(element, selector, type, callback) {
|
|||||||
|
|
||||||
module.exports = delegate;
|
module.exports = delegate;
|
||||||
|
|
||||||
},{"closest":1}],4:[function(require,module,exports){
|
},{"closest":1}],3:[function(require,module,exports){
|
||||||
/**
|
/**
|
||||||
* Check if argument is a HTML element.
|
* Check if argument is a HTML element.
|
||||||
*
|
*
|
||||||
@@ -147,13 +107,13 @@ exports.string = function(value) {
|
|||||||
* @param {Object} value
|
* @param {Object} value
|
||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
*/
|
*/
|
||||||
exports.function = function(value) {
|
exports.fn = function(value) {
|
||||||
var type = Object.prototype.toString.call(value);
|
var type = Object.prototype.toString.call(value);
|
||||||
|
|
||||||
return type === '[object Function]';
|
return type === '[object Function]';
|
||||||
};
|
};
|
||||||
|
|
||||||
},{}],5:[function(require,module,exports){
|
},{}],4:[function(require,module,exports){
|
||||||
var is = require('./is');
|
var is = require('./is');
|
||||||
var delegate = require('delegate');
|
var delegate = require('delegate');
|
||||||
|
|
||||||
@@ -175,7 +135,7 @@ function listen(target, type, callback) {
|
|||||||
throw new TypeError('Second argument must be a String');
|
throw new TypeError('Second argument must be a String');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is.function(callback)) {
|
if (!is.fn(callback)) {
|
||||||
throw new TypeError('Third argument must be a Function');
|
throw new TypeError('Third argument must be a Function');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,7 +210,48 @@ function listenSelector(selector, type, callback) {
|
|||||||
|
|
||||||
module.exports = listen;
|
module.exports = listen;
|
||||||
|
|
||||||
},{"./is":4,"delegate":3}],6:[function(require,module,exports){
|
},{"./is":3,"delegate":2}],5:[function(require,module,exports){
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Element prototype.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var proto = Element.prototype;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vendor function.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var vendor = proto.matchesSelector
|
||||||
|
|| proto.webkitMatchesSelector
|
||||||
|
|| proto.mozMatchesSelector
|
||||||
|
|| proto.msMatchesSelector
|
||||||
|
|| proto.oMatchesSelector;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expose `match()`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = match;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Match `el` to `selector`.
|
||||||
|
*
|
||||||
|
* @param {Element} el
|
||||||
|
* @param {String} selector
|
||||||
|
* @return {Boolean}
|
||||||
|
* @api public
|
||||||
|
*/
|
||||||
|
|
||||||
|
function match(el, selector) {
|
||||||
|
if (vendor) return vendor.call(el, selector);
|
||||||
|
var nodes = el.parentNode.querySelectorAll(selector);
|
||||||
|
for (var i = 0; i < nodes.length; ++i) {
|
||||||
|
if (nodes[i] == el) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},{}],6:[function(require,module,exports){
|
||||||
function select(element) {
|
function select(element) {
|
||||||
var selectedText;
|
var selectedText;
|
||||||
|
|
||||||
@@ -349,397 +350,391 @@ E.prototype = {
|
|||||||
module.exports = E;
|
module.exports = E;
|
||||||
|
|
||||||
},{}],8:[function(require,module,exports){
|
},{}],8:[function(require,module,exports){
|
||||||
'use strict';
|
(function (global, factory) {
|
||||||
|
if (typeof define === "function" && define.amd) {
|
||||||
|
define(['module', 'select'], factory);
|
||||||
|
} else if (typeof exports !== "undefined") {
|
||||||
|
factory(module, require('select'));
|
||||||
|
} else {
|
||||||
|
var mod = {
|
||||||
|
exports: {}
|
||||||
|
};
|
||||||
|
factory(mod, global.select);
|
||||||
|
global.clipboardAction = mod.exports;
|
||||||
|
}
|
||||||
|
})(this, function (module, _select) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
exports.__esModule = true;
|
var _select2 = _interopRequireDefault(_select);
|
||||||
|
|
||||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
function _interopRequireDefault(obj) {
|
||||||
|
return obj && obj.__esModule ? obj : {
|
||||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
default: obj
|
||||||
|
};
|
||||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
|
||||||
|
|
||||||
var _select = require('select');
|
|
||||||
|
|
||||||
var _select2 = _interopRequireDefault(_select);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Inner class which performs selection from either `text` or `target`
|
|
||||||
* properties and then executes copy or cut operations.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var ClipboardAction = (function () {
|
|
||||||
/**
|
|
||||||
* @param {Object} options
|
|
||||||
*/
|
|
||||||
|
|
||||||
function ClipboardAction(options) {
|
|
||||||
_classCallCheck(this, ClipboardAction);
|
|
||||||
|
|
||||||
this.resolveOptions(options);
|
|
||||||
this.initSelection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
|
||||||
* Defines base properties passed from constructor.
|
return typeof obj;
|
||||||
* @param {Object} options
|
} : function (obj) {
|
||||||
*/
|
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
||||||
|
|
||||||
ClipboardAction.prototype.resolveOptions = function resolveOptions() {
|
|
||||||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
|
||||||
|
|
||||||
this.action = options.action;
|
|
||||||
this.emitter = options.emitter;
|
|
||||||
this.target = options.target;
|
|
||||||
this.text = options.text;
|
|
||||||
this.trigger = options.trigger;
|
|
||||||
|
|
||||||
this.selectedText = '';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
function _classCallCheck(instance, Constructor) {
|
||||||
* Decides which selection strategy is going to be applied based
|
if (!(instance instanceof Constructor)) {
|
||||||
* on the existence of `text` and `target` properties.
|
throw new TypeError("Cannot call a class as a function");
|
||||||
*/
|
|
||||||
|
|
||||||
ClipboardAction.prototype.initSelection = function initSelection() {
|
|
||||||
if (this.text && this.target) {
|
|
||||||
throw new Error('Multiple attributes declared, use either "target" or "text"');
|
|
||||||
} else if (this.text) {
|
|
||||||
this.selectFake();
|
|
||||||
} else if (this.target) {
|
|
||||||
this.selectTarget();
|
|
||||||
} else {
|
|
||||||
throw new Error('Missing required attributes, use either "target" or "text"');
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
var _createClass = function () {
|
||||||
* Creates a fake textarea element, sets its value from `text` property,
|
function defineProperties(target, props) {
|
||||||
* and makes a selection on it.
|
for (var i = 0; i < props.length; i++) {
|
||||||
*/
|
var descriptor = props[i];
|
||||||
|
descriptor.enumerable = descriptor.enumerable || false;
|
||||||
ClipboardAction.prototype.selectFake = function selectFake() {
|
descriptor.configurable = true;
|
||||||
var _this = this;
|
if ("value" in descriptor) descriptor.writable = true;
|
||||||
|
Object.defineProperty(target, descriptor.key, descriptor);
|
||||||
this.removeFake();
|
|
||||||
|
|
||||||
this.fakeHandler = document.body.addEventListener('click', function () {
|
|
||||||
return _this.removeFake();
|
|
||||||
});
|
|
||||||
|
|
||||||
this.fakeElem = document.createElement('textarea');
|
|
||||||
this.fakeElem.style.position = 'absolute';
|
|
||||||
this.fakeElem.style.left = '-9999px';
|
|
||||||
this.fakeElem.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';
|
|
||||||
this.fakeElem.setAttribute('readonly', '');
|
|
||||||
this.fakeElem.value = this.text;
|
|
||||||
|
|
||||||
document.body.appendChild(this.fakeElem);
|
|
||||||
|
|
||||||
this.selectedText = _select2['default'](this.fakeElem);
|
|
||||||
this.copyText();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Only removes the fake element after another click event, that way
|
|
||||||
* a user can hit `Ctrl+C` to copy because selection still exists.
|
|
||||||
*/
|
|
||||||
|
|
||||||
ClipboardAction.prototype.removeFake = function removeFake() {
|
|
||||||
if (this.fakeHandler) {
|
|
||||||
document.body.removeEventListener('click');
|
|
||||||
this.fakeHandler = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.fakeElem) {
|
|
||||||
document.body.removeChild(this.fakeElem);
|
|
||||||
this.fakeElem = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Selects the content from element passed on `target` property.
|
|
||||||
*/
|
|
||||||
|
|
||||||
ClipboardAction.prototype.selectTarget = function selectTarget() {
|
|
||||||
this.selectedText = _select2['default'](this.target);
|
|
||||||
this.copyText();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes the copy operation based on the current selection.
|
|
||||||
*/
|
|
||||||
|
|
||||||
ClipboardAction.prototype.copyText = function copyText() {
|
|
||||||
var succeeded = undefined;
|
|
||||||
|
|
||||||
try {
|
|
||||||
succeeded = document.execCommand(this.action);
|
|
||||||
} catch (err) {
|
|
||||||
succeeded = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.handleResult(succeeded);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fires an event based on the copy operation result.
|
|
||||||
* @param {Boolean} succeeded
|
|
||||||
*/
|
|
||||||
|
|
||||||
ClipboardAction.prototype.handleResult = function handleResult(succeeded) {
|
|
||||||
if (succeeded) {
|
|
||||||
this.emitter.emit('success', {
|
|
||||||
action: this.action,
|
|
||||||
text: this.selectedText,
|
|
||||||
trigger: this.trigger,
|
|
||||||
clearSelection: this.clearSelection.bind(this)
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.emitter.emit('error', {
|
|
||||||
action: this.action,
|
|
||||||
trigger: this.trigger,
|
|
||||||
clearSelection: this.clearSelection.bind(this)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes current selection and focus from `target` element.
|
|
||||||
*/
|
|
||||||
|
|
||||||
ClipboardAction.prototype.clearSelection = function clearSelection() {
|
|
||||||
if (this.target) {
|
|
||||||
this.target.blur();
|
|
||||||
}
|
|
||||||
|
|
||||||
window.getSelection().removeAllRanges();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the `action` to be performed which can be either 'copy' or 'cut'.
|
|
||||||
* @param {String} action
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroy lifecycle.
|
|
||||||
*/
|
|
||||||
|
|
||||||
ClipboardAction.prototype.destroy = function destroy() {
|
|
||||||
this.removeFake();
|
|
||||||
};
|
|
||||||
|
|
||||||
_createClass(ClipboardAction, [{
|
|
||||||
key: 'action',
|
|
||||||
set: function set() {
|
|
||||||
var action = arguments.length <= 0 || arguments[0] === undefined ? 'copy' : arguments[0];
|
|
||||||
|
|
||||||
this._action = action;
|
|
||||||
|
|
||||||
if (this._action !== 'copy' && this._action !== 'cut') {
|
|
||||||
throw new Error('Invalid "action" value, use either "copy" or "cut"');
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
|
return function (Constructor, protoProps, staticProps) {
|
||||||
|
if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
||||||
|
if (staticProps) defineProperties(Constructor, staticProps);
|
||||||
|
return Constructor;
|
||||||
|
};
|
||||||
|
}();
|
||||||
|
|
||||||
|
var ClipboardAction = function () {
|
||||||
/**
|
/**
|
||||||
* Gets the `action` property.
|
* @param {Object} options
|
||||||
* @return {String}
|
|
||||||
*/
|
*/
|
||||||
get: function get() {
|
|
||||||
return this._action;
|
function ClipboardAction(options) {
|
||||||
|
_classCallCheck(this, ClipboardAction);
|
||||||
|
|
||||||
|
this.resolveOptions(options);
|
||||||
|
this.initSelection();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the `target` property using an element
|
* Defines base properties passed from constructor.
|
||||||
* that will be have its content copied.
|
* @param {Object} options
|
||||||
* @param {Element} target
|
|
||||||
*/
|
*/
|
||||||
}, {
|
|
||||||
key: 'target',
|
|
||||||
set: function set(target) {
|
ClipboardAction.prototype.resolveOptions = function resolveOptions() {
|
||||||
if (target !== undefined) {
|
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
||||||
if (target && typeof target === 'object' && target.nodeType === 1) {
|
|
||||||
this._target = target;
|
this.action = options.action;
|
||||||
} else {
|
this.emitter = options.emitter;
|
||||||
throw new Error('Invalid "target" value, use a valid Element');
|
this.target = options.target;
|
||||||
|
this.text = options.text;
|
||||||
|
this.trigger = options.trigger;
|
||||||
|
|
||||||
|
this.selectedText = '';
|
||||||
|
};
|
||||||
|
|
||||||
|
ClipboardAction.prototype.initSelection = function initSelection() {
|
||||||
|
if (this.text) {
|
||||||
|
this.selectFake();
|
||||||
|
} else if (this.target) {
|
||||||
|
this.selectTarget();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ClipboardAction.prototype.selectFake = function selectFake() {
|
||||||
|
var _this = this;
|
||||||
|
|
||||||
|
var isRTL = document.documentElement.getAttribute('dir') == 'rtl';
|
||||||
|
|
||||||
|
this.removeFake();
|
||||||
|
|
||||||
|
this.fakeHandler = document.body.addEventListener('click', function () {
|
||||||
|
return _this.removeFake();
|
||||||
|
}) || true;
|
||||||
|
|
||||||
|
this.fakeElem = document.createElement('textarea');
|
||||||
|
// Prevent zooming on iOS
|
||||||
|
this.fakeElem.style.fontSize = '12pt';
|
||||||
|
// Reset box model
|
||||||
|
this.fakeElem.style.border = '0';
|
||||||
|
this.fakeElem.style.padding = '0';
|
||||||
|
this.fakeElem.style.margin = '0';
|
||||||
|
// Move element out of screen horizontally
|
||||||
|
this.fakeElem.style.position = 'absolute';
|
||||||
|
this.fakeElem.style[isRTL ? 'right' : 'left'] = '-9999px';
|
||||||
|
// Move element to the same position vertically
|
||||||
|
this.fakeElem.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';
|
||||||
|
this.fakeElem.setAttribute('readonly', '');
|
||||||
|
this.fakeElem.value = this.text;
|
||||||
|
|
||||||
|
document.body.appendChild(this.fakeElem);
|
||||||
|
|
||||||
|
this.selectedText = (0, _select2.default)(this.fakeElem);
|
||||||
|
this.copyText();
|
||||||
|
};
|
||||||
|
|
||||||
|
ClipboardAction.prototype.removeFake = function removeFake() {
|
||||||
|
if (this.fakeHandler) {
|
||||||
|
document.body.removeEventListener('click');
|
||||||
|
this.fakeHandler = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.fakeElem) {
|
||||||
|
document.body.removeChild(this.fakeElem);
|
||||||
|
this.fakeElem = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ClipboardAction.prototype.selectTarget = function selectTarget() {
|
||||||
|
this.selectedText = (0, _select2.default)(this.target);
|
||||||
|
this.copyText();
|
||||||
|
};
|
||||||
|
|
||||||
|
ClipboardAction.prototype.copyText = function copyText() {
|
||||||
|
var succeeded = undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
succeeded = document.execCommand(this.action);
|
||||||
|
} catch (err) {
|
||||||
|
succeeded = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.handleResult(succeeded);
|
||||||
|
};
|
||||||
|
|
||||||
|
ClipboardAction.prototype.handleResult = function handleResult(succeeded) {
|
||||||
|
if (succeeded) {
|
||||||
|
this.emitter.emit('success', {
|
||||||
|
action: this.action,
|
||||||
|
text: this.selectedText,
|
||||||
|
trigger: this.trigger,
|
||||||
|
clearSelection: this.clearSelection.bind(this)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.emitter.emit('error', {
|
||||||
|
action: this.action,
|
||||||
|
trigger: this.trigger,
|
||||||
|
clearSelection: this.clearSelection.bind(this)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ClipboardAction.prototype.clearSelection = function clearSelection() {
|
||||||
|
if (this.target) {
|
||||||
|
this.target.blur();
|
||||||
|
}
|
||||||
|
|
||||||
|
window.getSelection().removeAllRanges();
|
||||||
|
};
|
||||||
|
|
||||||
|
ClipboardAction.prototype.destroy = function destroy() {
|
||||||
|
this.removeFake();
|
||||||
|
};
|
||||||
|
|
||||||
|
_createClass(ClipboardAction, [{
|
||||||
|
key: 'action',
|
||||||
|
set: function set() {
|
||||||
|
var action = arguments.length <= 0 || arguments[0] === undefined ? 'copy' : arguments[0];
|
||||||
|
|
||||||
|
this._action = action;
|
||||||
|
|
||||||
|
if (this._action !== 'copy' && this._action !== 'cut') {
|
||||||
|
throw new Error('Invalid "action" value, use either "copy" or "cut"');
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
get: function get() {
|
||||||
|
return this._action;
|
||||||
}
|
}
|
||||||
},
|
}, {
|
||||||
|
key: 'target',
|
||||||
|
set: function set(target) {
|
||||||
|
if (target !== undefined) {
|
||||||
|
if (target && (typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object' && target.nodeType === 1) {
|
||||||
|
if (this.action === 'copy' && target.hasAttribute('disabled')) {
|
||||||
|
throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
if (this.action === 'cut' && (target.hasAttribute('readonly') || target.hasAttribute('disabled'))) {
|
||||||
* Gets the `target` property.
|
throw new Error('Invalid "target" attribute. You can\'t cut text from elements with "readonly" or "disabled" attributes');
|
||||||
* @return {String|HTMLElement}
|
}
|
||||||
*/
|
|
||||||
get: function get() {
|
|
||||||
return this._target;
|
|
||||||
}
|
|
||||||
}]);
|
|
||||||
|
|
||||||
return ClipboardAction;
|
this._target = target;
|
||||||
})();
|
} else {
|
||||||
|
throw new Error('Invalid "target" value, use a valid Element');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
get: function get() {
|
||||||
|
return this._target;
|
||||||
|
}
|
||||||
|
}]);
|
||||||
|
|
||||||
exports['default'] = ClipboardAction;
|
return ClipboardAction;
|
||||||
module.exports = exports['default'];
|
}();
|
||||||
|
|
||||||
|
module.exports = ClipboardAction;
|
||||||
|
});
|
||||||
|
|
||||||
},{"select":6}],9:[function(require,module,exports){
|
},{"select":6}],9:[function(require,module,exports){
|
||||||
'use strict';
|
(function (global, factory) {
|
||||||
|
if (typeof define === "function" && define.amd) {
|
||||||
exports.__esModule = true;
|
define(['module', './clipboard-action', 'tiny-emitter', 'good-listener'], factory);
|
||||||
|
} else if (typeof exports !== "undefined") {
|
||||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
factory(module, require('./clipboard-action'), require('tiny-emitter'), require('good-listener'));
|
||||||
|
} else {
|
||||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
var mod = {
|
||||||
|
exports: {}
|
||||||
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
};
|
||||||
|
factory(mod, global.clipboardAction, global.tinyEmitter, global.goodListener);
|
||||||
var _clipboardAction = require('./clipboard-action');
|
global.clipboard = mod.exports;
|
||||||
|
|
||||||
var _clipboardAction2 = _interopRequireDefault(_clipboardAction);
|
|
||||||
|
|
||||||
var _tinyEmitter = require('tiny-emitter');
|
|
||||||
|
|
||||||
var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter);
|
|
||||||
|
|
||||||
var _goodListener = require('good-listener');
|
|
||||||
|
|
||||||
var _goodListener2 = _interopRequireDefault(_goodListener);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base class which takes one or more elements, adds event listeners to them,
|
|
||||||
* and instantiates a new `ClipboardAction` on each click.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var Clipboard = (function (_Emitter) {
|
|
||||||
_inherits(Clipboard, _Emitter);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
|
||||||
* @param {Object} options
|
|
||||||
*/
|
|
||||||
|
|
||||||
function Clipboard(trigger, options) {
|
|
||||||
_classCallCheck(this, Clipboard);
|
|
||||||
|
|
||||||
_Emitter.call(this);
|
|
||||||
|
|
||||||
this.resolveOptions(options);
|
|
||||||
this.listenClick(trigger);
|
|
||||||
}
|
}
|
||||||
|
})(this, function (module, _clipboardAction, _tinyEmitter, _goodListener) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var _clipboardAction2 = _interopRequireDefault(_clipboardAction);
|
||||||
|
|
||||||
|
var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter);
|
||||||
|
|
||||||
|
var _goodListener2 = _interopRequireDefault(_goodListener);
|
||||||
|
|
||||||
|
function _interopRequireDefault(obj) {
|
||||||
|
return obj && obj.__esModule ? obj : {
|
||||||
|
default: obj
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function _classCallCheck(instance, Constructor) {
|
||||||
|
if (!(instance instanceof Constructor)) {
|
||||||
|
throw new TypeError("Cannot call a class as a function");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _possibleConstructorReturn(self, call) {
|
||||||
|
if (!self) {
|
||||||
|
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
||||||
|
}
|
||||||
|
|
||||||
|
return call && (typeof call === "object" || typeof call === "function") ? call : self;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _inherits(subClass, superClass) {
|
||||||
|
if (typeof superClass !== "function" && superClass !== null) {
|
||||||
|
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
||||||
|
constructor: {
|
||||||
|
value: subClass,
|
||||||
|
enumerable: false,
|
||||||
|
writable: true,
|
||||||
|
configurable: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
var Clipboard = function (_Emitter) {
|
||||||
|
_inherits(Clipboard, _Emitter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
||||||
|
* @param {Object} options
|
||||||
|
*/
|
||||||
|
|
||||||
|
function Clipboard(trigger, options) {
|
||||||
|
_classCallCheck(this, Clipboard);
|
||||||
|
|
||||||
|
var _this = _possibleConstructorReturn(this, _Emitter.call(this));
|
||||||
|
|
||||||
|
_this.resolveOptions(options);
|
||||||
|
_this.listenClick(trigger);
|
||||||
|
return _this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines if attributes would be resolved using internal setter functions
|
||||||
|
* or custom functions that were passed in the constructor.
|
||||||
|
* @param {Object} options
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
Clipboard.prototype.resolveOptions = function resolveOptions() {
|
||||||
|
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
||||||
|
|
||||||
|
this.action = typeof options.action === 'function' ? options.action : this.defaultAction;
|
||||||
|
this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;
|
||||||
|
this.text = typeof options.text === 'function' ? options.text : this.defaultText;
|
||||||
|
};
|
||||||
|
|
||||||
|
Clipboard.prototype.listenClick = function listenClick(trigger) {
|
||||||
|
var _this2 = this;
|
||||||
|
|
||||||
|
this.listener = (0, _goodListener2.default)(trigger, 'click', function (e) {
|
||||||
|
return _this2.onClick(e);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Clipboard.prototype.onClick = function onClick(e) {
|
||||||
|
var trigger = e.delegateTarget || e.currentTarget;
|
||||||
|
|
||||||
|
if (this.clipboardAction) {
|
||||||
|
this.clipboardAction = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.clipboardAction = new _clipboardAction2.default({
|
||||||
|
action: this.action(trigger),
|
||||||
|
target: this.target(trigger),
|
||||||
|
text: this.text(trigger),
|
||||||
|
trigger: trigger,
|
||||||
|
emitter: this
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Clipboard.prototype.defaultAction = function defaultAction(trigger) {
|
||||||
|
return getAttributeValue('action', trigger);
|
||||||
|
};
|
||||||
|
|
||||||
|
Clipboard.prototype.defaultTarget = function defaultTarget(trigger) {
|
||||||
|
var selector = getAttributeValue('target', trigger);
|
||||||
|
|
||||||
|
if (selector) {
|
||||||
|
return document.querySelector(selector);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Clipboard.prototype.defaultText = function defaultText(trigger) {
|
||||||
|
return getAttributeValue('text', trigger);
|
||||||
|
};
|
||||||
|
|
||||||
|
Clipboard.prototype.destroy = function destroy() {
|
||||||
|
this.listener.destroy();
|
||||||
|
|
||||||
|
if (this.clipboardAction) {
|
||||||
|
this.clipboardAction.destroy();
|
||||||
|
this.clipboardAction = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return Clipboard;
|
||||||
|
}(_tinyEmitter2.default);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to retrieve attribute value.
|
* Helper function to retrieve attribute value.
|
||||||
* @param {String} suffix
|
* @param {String} suffix
|
||||||
* @param {Element} element
|
* @param {Element} element
|
||||||
*/
|
*/
|
||||||
|
function getAttributeValue(suffix, element) {
|
||||||
|
var attribute = 'data-clipboard-' + suffix;
|
||||||
|
|
||||||
/**
|
if (!element.hasAttribute(attribute)) {
|
||||||
* Defines if attributes would be resolved using internal setter functions
|
return;
|
||||||
* or custom functions that were passed in the constructor.
|
|
||||||
* @param {Object} options
|
|
||||||
*/
|
|
||||||
|
|
||||||
Clipboard.prototype.resolveOptions = function resolveOptions() {
|
|
||||||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
|
|
||||||
|
|
||||||
this.action = typeof options.action === 'function' ? options.action : this.defaultAction;
|
|
||||||
this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;
|
|
||||||
this.text = typeof options.text === 'function' ? options.text : this.defaultText;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a click event listener to the passed trigger.
|
|
||||||
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
|
||||||
*/
|
|
||||||
|
|
||||||
Clipboard.prototype.listenClick = function listenClick(trigger) {
|
|
||||||
var _this = this;
|
|
||||||
|
|
||||||
this.listener = _goodListener2['default'](trigger, 'click', function (e) {
|
|
||||||
return _this.onClick(e);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines a new `ClipboardAction` on each click event.
|
|
||||||
* @param {Event} e
|
|
||||||
*/
|
|
||||||
|
|
||||||
Clipboard.prototype.onClick = function onClick(e) {
|
|
||||||
var trigger = e.delegateTarget || e.currentTarget;
|
|
||||||
|
|
||||||
if (this.clipboardAction) {
|
|
||||||
this.clipboardAction = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.clipboardAction = new _clipboardAction2['default']({
|
return element.getAttribute(attribute);
|
||||||
action: this.action(trigger),
|
|
||||||
target: this.target(trigger),
|
|
||||||
text: this.text(trigger),
|
|
||||||
trigger: trigger,
|
|
||||||
emitter: this
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default `action` lookup function.
|
|
||||||
* @param {Element} trigger
|
|
||||||
*/
|
|
||||||
|
|
||||||
Clipboard.prototype.defaultAction = function defaultAction(trigger) {
|
|
||||||
return getAttributeValue('action', trigger);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default `target` lookup function.
|
|
||||||
* @param {Element} trigger
|
|
||||||
*/
|
|
||||||
|
|
||||||
Clipboard.prototype.defaultTarget = function defaultTarget(trigger) {
|
|
||||||
var selector = getAttributeValue('target', trigger);
|
|
||||||
|
|
||||||
if (selector) {
|
|
||||||
return document.querySelector(selector);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default `text` lookup function.
|
|
||||||
* @param {Element} trigger
|
|
||||||
*/
|
|
||||||
|
|
||||||
Clipboard.prototype.defaultText = function defaultText(trigger) {
|
|
||||||
return getAttributeValue('text', trigger);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroy lifecycle.
|
|
||||||
*/
|
|
||||||
|
|
||||||
Clipboard.prototype.destroy = function destroy() {
|
|
||||||
this.listener.destroy();
|
|
||||||
|
|
||||||
if (this.clipboardAction) {
|
|
||||||
this.clipboardAction.destroy();
|
|
||||||
this.clipboardAction = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return Clipboard;
|
|
||||||
})(_tinyEmitter2['default']);
|
|
||||||
|
|
||||||
function getAttributeValue(suffix, element) {
|
|
||||||
var attribute = 'data-clipboard-' + suffix;
|
|
||||||
|
|
||||||
if (!element.hasAttribute(attribute)) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return element.getAttribute(attribute);
|
module.exports = Clipboard;
|
||||||
}
|
});
|
||||||
|
|
||||||
exports['default'] = Clipboard;
|
},{"./clipboard-action":8,"good-listener":4,"tiny-emitter":7}]},{},[9])(9)
|
||||||
module.exports = exports['default'];
|
|
||||||
|
|
||||||
},{"./clipboard-action":8,"good-listener":5,"tiny-emitter":7}]},{},[9])(9)
|
|
||||||
});
|
});
|
||||||
4
dist/clipboard.min.js
vendored
4
dist/clipboard.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -3,10 +3,10 @@
|
|||||||
Package.describe({
|
Package.describe({
|
||||||
name: "zenorocha:clipboard",
|
name: "zenorocha:clipboard",
|
||||||
summary: "Modern copy to clipboard. No Flash. Just 2kb.",
|
summary: "Modern copy to clipboard. No Flash. Just 2kb.",
|
||||||
version: "1.5.5",
|
version: "1.5.11",
|
||||||
git: "https://github.com/zenorocha/clipboard.js"
|
git: "https://github.com/zenorocha/clipboard.js"
|
||||||
});
|
});
|
||||||
|
|
||||||
Package.onUse(function(api) {
|
Package.onUse(function(api) {
|
||||||
api.addFiles("dist/clipboard.min.js", "client");
|
api.addFiles("dist/clipboard.js", "client");
|
||||||
});
|
});
|
||||||
|
|||||||
31
package.json
31
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "clipboard",
|
"name": "clipboard",
|
||||||
"version": "1.5.5",
|
"version": "1.5.11",
|
||||||
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
||||||
"repository": "zenorocha/clipboard.js",
|
"repository": "zenorocha/clipboard.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -11,31 +11,38 @@
|
|||||||
"cut"
|
"cut"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"good-listener": "^1.1.4",
|
"good-listener": "^1.1.6",
|
||||||
"select": "^1.0.6",
|
"select": "^1.0.6",
|
||||||
"tiny-emitter": "^1.0.0"
|
"tiny-emitter": "^1.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel": "^5.8.29",
|
"babel-cli": "^6.5.1",
|
||||||
"babelify": "^6.3.0",
|
"babel-core": "^6.5.2",
|
||||||
"browserify": "^11.2.0",
|
"babel-plugin-transform-es2015-modules-umd": "^6.5.0",
|
||||||
|
"babel-preset-es2015": "^6.5.0",
|
||||||
|
"babel-preset-es2015-loose": "^7.0.0",
|
||||||
|
"babelify": "^7.2.0",
|
||||||
|
"bannerify": "Vekat/bannerify#feature-option",
|
||||||
|
"browserify": "^13.0.0",
|
||||||
|
"chai": "^3.4.1",
|
||||||
|
"install": "^0.4.4",
|
||||||
"karma": "^0.13.10",
|
"karma": "^0.13.10",
|
||||||
"karma-browserify": "^4.4.0",
|
"karma-browserify": "^5.0.1",
|
||||||
"karma-chai": "^0.1.0",
|
"karma-chai": "^0.1.0",
|
||||||
"karma-mocha": "^0.2.0",
|
"karma-mocha": "^0.2.0",
|
||||||
"karma-phantomjs-launcher": "^0.2.1",
|
"karma-phantomjs-launcher": "^1.0.0",
|
||||||
"karma-sinon": "^1.0.4",
|
"karma-sinon": "^1.0.4",
|
||||||
"mocha": "^2.3.3",
|
"mocha": "^2.3.3",
|
||||||
"phantomjs-polyfill": "0.0.1",
|
"phantomjs-prebuilt": "^2.1.4",
|
||||||
|
"sinon": "^1.17.2",
|
||||||
"uglify-js": "^2.4.24",
|
"uglify-js": "^2.4.24",
|
||||||
"watchify": "^3.4.0",
|
"watchify": "^3.4.0"
|
||||||
"bannerify": "Vekat/bannerify#feature-option"
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run build-debug && npm run build-min",
|
"build": "npm run build-debug && npm run build-min",
|
||||||
"build-debug": "browserify src/clipboard.js -s Clipboard -t [babelify --loose all] -p [bannerify --file .banner ] -o dist/clipboard.js",
|
"build-debug": "browserify src/clipboard.js -s Clipboard -t [babelify] -p [bannerify --file .banner ] -o dist/clipboard.js",
|
||||||
"build-min": "uglifyjs dist/clipboard.js --comments '/!/' -m screw_ie8=true -c screw_ie8=true,unused=false -o dist/clipboard.min.js",
|
"build-min": "uglifyjs dist/clipboard.js --comments '/!/' -m screw_ie8=true -c screw_ie8=true,unused=false -o dist/clipboard.min.js",
|
||||||
"build-watch": "watchify src/clipboard.js -s Clipboard -t [babelify --loose all] -o dist/clipboard.js -v",
|
"build-watch": "watchify src/clipboard.js -s Clipboard -t [babelify] -o dist/clipboard.js -v",
|
||||||
"test": "karma start --single-run",
|
"test": "karma start --single-run",
|
||||||
"prepublish": "babel src --out-dir lib --loose all"
|
"prepublish": "babel src --out-dir lib --loose all"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
[](https://travis-ci.org/zenorocha/clipboard.js)
|
[](https://travis-ci.org/zenorocha/clipboard.js)
|
||||||

|

|
||||||
|
|
||||||
> Modern copy to clipboard. No Flash. Just 2kb
|
> Modern copy to clipboard. No Flash. Just 3kb gzipped.
|
||||||
|
|
||||||
<a href="http://clipboardjs.com/"><img width="728" src="https://cloud.githubusercontent.com/assets/398893/9983535/5ab0a950-5fb4-11e5-9602-e73c0b661883.jpg" alt="Demo"></a>
|
<a href="http://clipboardjs.com/"><img width="728" src="https://cloud.githubusercontent.com/assets/398893/9983535/5ab0a950-5fb4-11e5-9602-e73c0b661883.jpg" alt="Demo"></a>
|
||||||
|
|
||||||
@@ -151,7 +151,7 @@ new Clipboard('.btn', {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
Also, with are working with single page apps, you may want to manage the lifecycle of the DOM more precisely. Here's how you clean up the events and objects that we create.
|
Also, if you are working with single page apps, you may want to manage the lifecycle of the DOM more precisely. Here's how you clean up the events and objects that we create.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var clipboard = new Clipboard('.btn');
|
var clipboard = new Clipboard('.btn');
|
||||||
@@ -168,7 +168,7 @@ This library relies on both [Selection](https://developer.mozilla.org/en-US/docs
|
|||||||
|
|
||||||
Although copy/cut operations with [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) aren't supported on Safari yet (including mobile), it gracefully degrades because [Selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection) is supported.
|
Although copy/cut operations with [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) aren't supported on Safari yet (including mobile), it gracefully degrades because [Selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection) is supported.
|
||||||
|
|
||||||
That means you can show a tooltip saying `Copied!` when `success` event is called and `Press Ctrl+C to copy` when `error` event is called because the text is already selected.
|
That means you can show a tooltip saying `Copied!` when `success` event is called and `Press Command+C to copy` when `error` event is called because the text is already selected.
|
||||||
|
|
||||||
For a live demonstration, open this [site](http://clipboardjs.com) on Safari.
|
For a live demonstration, open this [site](http://clipboardjs.com) on Safari.
|
||||||
|
|
||||||
|
|||||||
@@ -32,18 +32,12 @@ class ClipboardAction {
|
|||||||
* on the existence of `text` and `target` properties.
|
* on the existence of `text` and `target` properties.
|
||||||
*/
|
*/
|
||||||
initSelection() {
|
initSelection() {
|
||||||
if (this.text && this.target) {
|
if (this.text) {
|
||||||
throw new Error('Multiple attributes declared, use either "target" or "text"');
|
|
||||||
}
|
|
||||||
else if (this.text) {
|
|
||||||
this.selectFake();
|
this.selectFake();
|
||||||
}
|
}
|
||||||
else if (this.target) {
|
else if (this.target) {
|
||||||
this.selectTarget();
|
this.selectTarget();
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
throw new Error('Missing required attributes, use either "target" or "text"');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,13 +45,23 @@ class ClipboardAction {
|
|||||||
* and makes a selection on it.
|
* and makes a selection on it.
|
||||||
*/
|
*/
|
||||||
selectFake() {
|
selectFake() {
|
||||||
|
const isRTL = document.documentElement.getAttribute('dir') == 'rtl';
|
||||||
|
|
||||||
this.removeFake();
|
this.removeFake();
|
||||||
|
|
||||||
this.fakeHandler = document.body.addEventListener('click', () => this.removeFake());
|
this.fakeHandler = document.body.addEventListener('click', () => this.removeFake()) || true;
|
||||||
|
|
||||||
this.fakeElem = document.createElement('textarea');
|
this.fakeElem = document.createElement('textarea');
|
||||||
|
// Prevent zooming on iOS
|
||||||
|
this.fakeElem.style.fontSize = '12pt';
|
||||||
|
// Reset box model
|
||||||
|
this.fakeElem.style.border = '0';
|
||||||
|
this.fakeElem.style.padding = '0';
|
||||||
|
this.fakeElem.style.margin = '0';
|
||||||
|
// Move element out of screen horizontally
|
||||||
this.fakeElem.style.position = 'absolute';
|
this.fakeElem.style.position = 'absolute';
|
||||||
this.fakeElem.style.left = '-9999px';
|
this.fakeElem.style[ isRTL ? 'right' : 'left' ] = '-9999px';
|
||||||
|
// Move element to the same position vertically
|
||||||
this.fakeElem.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';
|
this.fakeElem.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';
|
||||||
this.fakeElem.setAttribute('readonly', '');
|
this.fakeElem.setAttribute('readonly', '');
|
||||||
this.fakeElem.value = this.text;
|
this.fakeElem.value = this.text;
|
||||||
@@ -169,6 +173,14 @@ class ClipboardAction {
|
|||||||
set target(target) {
|
set target(target) {
|
||||||
if (target !== undefined) {
|
if (target !== undefined) {
|
||||||
if (target && typeof target === 'object' && target.nodeType === 1) {
|
if (target && typeof target === 'object' && target.nodeType === 1) {
|
||||||
|
if (this.action === 'copy' && target.hasAttribute('disabled')) {
|
||||||
|
throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.action === 'cut' && (target.hasAttribute('readonly') || target.hasAttribute('disabled'))) {
|
||||||
|
throw new Error('Invalid "target" attribute. You can\'t cut text from elements with "readonly" or "disabled" attributes');
|
||||||
|
}
|
||||||
|
|
||||||
this._target = target;
|
this._target = target;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -193,4 +205,4 @@ class ClipboardAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ClipboardAction;
|
module.exports = ClipboardAction;
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class Clipboard extends Emitter {
|
|||||||
* @param {Event} e
|
* @param {Event} e
|
||||||
*/
|
*/
|
||||||
onClick(e) {
|
onClick(e) {
|
||||||
var trigger = e.delegateTarget || e.currentTarget;
|
const trigger = e.delegateTarget || e.currentTarget;
|
||||||
|
|
||||||
if (this.clipboardAction) {
|
if (this.clipboardAction) {
|
||||||
this.clipboardAction = null;
|
this.clipboardAction = null;
|
||||||
@@ -70,7 +70,7 @@ class Clipboard extends Emitter {
|
|||||||
* @param {Element} trigger
|
* @param {Element} trigger
|
||||||
*/
|
*/
|
||||||
defaultTarget(trigger) {
|
defaultTarget(trigger) {
|
||||||
let selector = getAttributeValue('target', trigger);
|
const selector = getAttributeValue('target', trigger);
|
||||||
|
|
||||||
if (selector) {
|
if (selector) {
|
||||||
return document.querySelector(selector);
|
return document.querySelector(selector);
|
||||||
@@ -105,7 +105,7 @@ class Clipboard extends Emitter {
|
|||||||
* @param {Element} element
|
* @param {Element} element
|
||||||
*/
|
*/
|
||||||
function getAttributeValue(suffix, element) {
|
function getAttributeValue(suffix, element) {
|
||||||
let attribute = `data-clipboard-${suffix}`;
|
const attribute = `data-clipboard-${suffix}`;
|
||||||
|
|
||||||
if (!element.hasAttribute(attribute)) {
|
if (!element.hasAttribute(attribute)) {
|
||||||
return;
|
return;
|
||||||
@@ -114,4 +114,4 @@ function getAttributeValue(suffix, element) {
|
|||||||
return element.getAttribute(attribute);
|
return element.getAttribute(attribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Clipboard;
|
module.exports = Clipboard;
|
||||||
|
|||||||
@@ -35,27 +35,17 @@ describe('ClipboardAction', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#initSelection', () => {
|
describe('#initSelection', () => {
|
||||||
it('should throw an error since both "text" and "target" were passed', done => {
|
it('should set the position right style property', done => {
|
||||||
try {
|
// Set document direction
|
||||||
new ClipboardAction({
|
document.documentElement.setAttribute('dir', 'rtl');
|
||||||
text: 'foo',
|
|
||||||
target: document.querySelector('#input')
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch(e) {
|
|
||||||
assert.equal(e.message, 'Multiple attributes declared, use either "target" or "text"');
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should throw an error since neither "text" nor "target" were passed', done => {
|
let clip = new ClipboardAction({
|
||||||
try {
|
emitter: new Emitter(),
|
||||||
new ClipboardAction();
|
text: 'foo'
|
||||||
}
|
});
|
||||||
catch(e) {
|
|
||||||
assert.equal(e.message, 'Missing required attributes, use either "target" or "text"');
|
assert.equal(clip.fakeElem.style.right, '-9999px');
|
||||||
done();
|
done();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ describe('Clipboard', () => {
|
|||||||
assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
|
assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throws exception target', done => {
|
it('should throw an exception when target is invalid', done => {
|
||||||
try {
|
try {
|
||||||
var clipboard = new Clipboard('.btn', {
|
var clipboard = new Clipboard('.btn', {
|
||||||
target: function() {
|
target: function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user