vtils
Version:
一个面向业务的 JavaScript/TypeScript 实用程序库。
49 lines (44 loc) • 1.35 kB
JavaScript
/**
* 复制文本到剪切板。
*
* @param text 要复制的文本
* @param options 选项
* @returns 返回是否复制成功
*/
export function copyTextToClipboard(text, options) {
// https://github.com/sindresorhus/copy-text-to-clipboard/blob/master/index.js
var element = document.createElement('textarea');
element.value = text;
// Prevent keyboard from showing on mobile
element.setAttribute('readonly', '');
// @ts-ignore
element.style.contain = 'strict';
element.style.position = 'absolute';
element.style.left = '-9999px';
element.style.fontSize = '12pt'; // Prevent zooming on iOS
if (options != null && options.containerClass) {
element.className = options.containerClass;
}
var selection = document.getSelection();
var originalRange;
/* istanbul ignore if */
if (selection.rangeCount > 0) {
originalRange = selection.getRangeAt(0);
}
document.body.appendChild(element);
element.select();
// Explicit selection workaround for iOS
element.selectionStart = 0;
element.selectionEnd = text.length;
var isSuccess = false;
try {
isSuccess = document.execCommand('copy');
} catch (_) {}
document.body.removeChild(element);
/* istanbul ignore if */
if (originalRange) {
selection.removeAllRanges();
selection.addRange(originalRange);
}
return isSuccess;
}