UNPKG

tdesign-mobile-vue

Version:
106 lines (103 loc) 2.7 kB
/** * tdesign v1.15.0 * (c) 2026 TDesign Group * @license MIT */ var deselectCurrent = function deselectCurrent() { var selection = document.getSelection(); if (!selection.rangeCount) { return function () {}; } var active = document.activeElement; var ranges = []; for (var i = 0; i < selection.rangeCount; i++) { ranges.push(selection.getRangeAt(i)); } var tagName = active.tagName.toUpperCase(); switch (tagName) { case "INPUT": case "TEXTAREA": active.blur(); break; default: active = null; break; } selection.removeAllRanges(); return function () { selection.type === "Caret" && selection.removeAllRanges(); if (!selection.rangeCount) { ranges.forEach(function (range) { selection.addRange(range); }); } active && active.focus(); }; }; var copy = function copy(text, options) { var reselectPrevious; var range; var selection; var mark; var success = false; if (!options) { options = {}; } try { reselectPrevious = deselectCurrent(); range = document.createRange(); selection = document.getSelection(); mark = document.createElement("span"); mark.textContent = text; mark.style.all = "unset"; mark.style.position = "fixed"; mark.style.top = "0"; mark.style.clip = "rect(0, 0, 0, 0)"; mark.style.whiteSpace = "pre"; mark.style.webkitUserSelect = "text"; mark.style.userSelect = "text"; mark.addEventListener("copy", function (e) { e.stopPropagation(); if (options.format) { e.preventDefault(); e.clipboardData.clearData(); e.clipboardData.setData(options.format, text); } if (options.onCopy) { e.preventDefault(); options.onCopy(e.clipboardData); } }); document.body.appendChild(mark); range.selectNodeContents(mark); selection.addRange(range); var successful = document.execCommand("copy"); if (!successful) { throw new Error("copy command was unsuccessful"); } success = true; } catch (err) { try { window.clipboardData.setData(options.format || "text", text); options.onCopy && options.onCopy(window.clipboardData); success = true; } catch (err2) { console.warn("Copy to clipboard failed", err2); } } finally { if (selection) { if (typeof selection.removeRange === "function") { selection.removeRange(range); } else { selection.removeAllRanges(); } } if (mark) { document.body.removeChild(mark); } reselectPrevious(); } return success; }; export { copy }; //# sourceMappingURL=copy-to-clipboard.js.map