阅读 171

copy-text-to-clipboard 源码解读

代码中我们有把文本复制到剪切板的需求,这里我们可以自己写,也可以从npm中找,本着不重复造轮子(buhui),我们使用 copy-text-to-clipboard

安装

npm install copy-text-to-clipboard

源码

const copyTextToClipboard = (input, {target = document.body} = {}) => {     // 创建一个界面外的文本输入框 const element = document.createElement('textarea');     // 缓存之前激活的dom const previouslyFocusedElement = document.activeElement;     // 赋值 element.value = input; // Prevent keyboard from showing on mobile element.setAttribute('readonly', '');     // 界面外的css赋值 element.style.contain = 'strict'; element.style.position = 'absolute'; element.style.left = '-9999px'; element.style.fontSize = '12pt'; // Prevent zooming on iOS     // 创建选区 const selection = document.getSelection(); let originalRange = false; if (selection.rangeCount > 0) { originalRange = selection.getRangeAt(0); }     // 插入的dom中 target.append(element);     // 选中 element.select(); // Explicit selection workaround for iOS element.selectionStart = 0; element.selectionEnd = input.length; let isSuccess = false; try { isSuccess = document.execCommand('copy'); } catch (_) {} element.remove(); if (originalRange) {         // 把原本dom中的选区还原到dom中 selection.removeAllRanges(); selection.addRange(originalRange); } // Get the focus back on the previously focused element, if any if (previouslyFocusedElement) {         // focus选中 previouslyFocusedElement.focus(); } return isSuccess; }; 复制代码

使用

const copy = require('copy-text-to-clipboard'); button.addEventListener('click', () => { copy('????????'); });


作者:JunIce
链接:https://juejin.cn/post/7029194941796450341


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐