UNPKG

zent

Version:

一套前端设计语言和基于React的实现

68 lines (57 loc) 1.62 kB
import deselectCurrent from './ToggleSelection'; function copy(text) { let reselectPrevious, range, selection, mark, success = false; try { reselectPrevious = deselectCurrent(); range = document.createRange(); selection = document.getSelection(); mark = document.createElement('span'); mark.textContent = text; // reset user styles for span element mark.style.all = 'unset'; // prevents scrolling to the end of the page mark.style.position = 'fixed'; mark.style.top = 0; mark.style.clip = 'rect(0, 0, 0, 0)'; // used to preserve spaces and line breaks mark.style.whiteSpace = 'pre'; // do not inherit user-select (it may be `none`) mark.style.webkitUserSelect = 'text'; mark.style.MozUserSelect = 'text'; mark.style.msUserSelect = 'text'; mark.style.userSelect = 'text'; document.body.appendChild(mark); range.selectNode(mark); selection.addRange(range); let successful = document.execCommand('copy'); if (!successful) { throw new Error('copy command was unsuccessful'); } success = true; } catch (err) { try { window.clipboardData.setData('text', text); success = true; } catch (e) { console.log(e); // eslint-disable-line } } finally { if (selection) { if (typeof selection.removeRange === 'function') { selection.removeRange(range); } else { selection.removeAllRanges(); } } if (mark) { document.body.removeChild(mark); } reselectPrevious(); } return success; } module.exports = copy;