diff --git a/README.md b/README.md index fd6c1fc..55bf638 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,17 @@ new Vue({ ``` +## I want to copy texts without a specific button! + +Yes, you can do it by using our new method: `this.$copyText`. see [sample2](sample2.html). + +Modern browser have some limiation like that you can't use `window.open` without a user iteraction. +So there's a same restriction on copy things! Test it before you use it. Make sure you are not +using this method inside any async method. + +Use it before reading [this issue](https://github.com/zenorocha/clipboard.js/issues/218) and +[this page](https://github.com/zenorocha/clipboard.js/wiki/Known-Limitations). + ### Contribution PRs welcome, and issues aswell! If you want any feature that we don't have currently, please create a issue for feature request. diff --git a/sample2.html b/sample2.html new file mode 100644 index 0000000..545bfc2 --- /dev/null +++ b/sample2.html @@ -0,0 +1,42 @@ + + + + + vue-clipboard2 sample app + + + + +
+ + + + + + diff --git a/vue-clipboard.js b/vue-clipboard.js index 62d98ed..e738acc 100644 --- a/vue-clipboard.js +++ b/vue-clipboard.js @@ -2,6 +2,25 @@ var Clipboard = require('clipboard') var VueClipboard = { install: function (Vue) { + Vue.prototype.$copyText = function (text) { + return new Promise(function (resolve, reject) { + var fake_el = document.createElement('button'); + var clipboard = new Clipboard(fake_el, { + text: function () { return text }, + action: function () { return 'copy' } + }); + clipboard.on('success', function (e) { + clipboard.destroy(); + resolve(e); + }); + clipboard.on('error', function (e) { + clipboard.destroy(); + reject(e); + }); + fake_el.click(); + }); + }; + Vue.directive('clipboard', { bind: function (el, binding, vnode) { if(binding.arg === 'success') {