UNPKG

tdesign-vue-next

Version:
113 lines (110 loc) 3 kB
/** * tdesign v1.13.2 * (c) 2025 tdesign * @license MIT */ var defaultMessage = "Copy to clipboard: #{key}, Enter"; var format = function format(message) { var copyKey = (/mac os x/i.test(navigator.userAgent) ? "\u2318" : "Ctrl") + "+C"; return message.replace(/#{\s*key\s*}/g, copyKey); }; 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 message, reselectPrevious, range, selection, mark, 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) { message = format("message" in options ? options.message : defaultMessage); window.prompt(message, text); } } 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 as default }; //# sourceMappingURL=index.js.map