UNPKG

@livelybone/copy

Version:

About clipboard copy, Object simple&deep copy

10 lines (8 loc) 1.39 kB
/** * Bundle of @livelybone/copy * Generated: 2023-07-25 * Version: 2.6.6 * License: MIT * Author: 2631541504@qq.com */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).copyText=t()}(this,function(){"use strict";return function(c){return new Promise(function(e,t){var n,o;navigator.clipboard&&navigator.clipboard.writeText?navigator.clipboard.writeText(c).then(function(){return e(!0)}).catch(function(e){e=new Error(e.message?"copyText failed, reason: ".concat(e.message):"copyText failed");t(e)}):((n=document.createElement("span")).style.position="fixed",n.style.left="0",n.style.top="0",n.style.zIndex="-999",n.innerText=c,document.body.appendChild(n),o=function(e,t){var n,o,c=(t=Object.assign({},{clearSelect:!1,cut:!1},t)).clearSelect,t=t.cut;if(window.getSelection){if(!(o=window.getSelection()))return console.error("copyDom: window.getSelection() return null"),!1;n=window.document.createRange(),o.removeAllRanges(),n.selectNode(e),o.addRange(n)}else{var r=document.body.createTextRange;r&&((n=r()).moveToElementText(e),n.select())}return r=document.execCommand("copy"),o&&c&&o.removeAllRanges(),t&&(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement?e.value="":e.innerHTML=""),r}(n),document.body.removeChild(n),o?e(o):t(new Error("copyText failed")))})}});