UNPKG

@iicoding/utils

Version:

Browser 端 - 类型判断 - 类似 koa 的异步compose - sleep - 扩展对象属性 - 扩展 storage 对象功能

121 lines (119 loc) 4.27 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/helper/copy.ts var copy_exports = {}; __export(copy_exports, { copy: () => copy, default: () => copy_default }); module.exports = __toCommonJS(copy_exports); var import_toggle_selection = require("./toggle-selection"); var clipboardToIE11Formatting = { "text/plain": "Text", "text/html": "Url", default: "Text" }; var defaultMessage = "Copy to clipboard: #{key}, Enter"; function format(message) { const copyKey = (/mac os x/i.test(navigator.userAgent) ? "⌘" : "Ctrl") + "+C"; return message.replace(/#{\s*key\s*}/g, copyKey); } function copy(text, options) { let debug, message, reselectPrevious, range, selection, mark, success = false; if (!options) { options = {}; } debug = options.debug || false; try { reselectPrevious = (0, import_toggle_selection.toggleSelection)(); range = document.createRange(); selection = document.getSelection(); mark = document.createElement("span"); mark.textContent = text; mark.ariaHidden = "true"; 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.MozUserSelect = "text"; mark.style.msUserSelect = "text"; mark.style.userSelect = "text"; mark.addEventListener("copy", function(e) { var _a, _b; e.stopPropagation(); if (options == null ? void 0 : options.format) { e.preventDefault(); if (typeof e.clipboardData === "undefined") { debug && console.warn("unable to use e.clipboardData"); debug && console.warn("trying IE specific stuff"); window.clipboardData.clearData(); const format2 = clipboardToIE11Formatting[options.format] || clipboardToIE11Formatting["default"]; window.clipboardData.setData(format2, text); } else { (_a = e.clipboardData) == null ? void 0 : _a.clearData(); (_b = e.clipboardData) == null ? void 0 : _b.setData(options.format, text); } } if (options == null ? void 0 : options.onCopy) { e.preventDefault(); options.onCopy(e.clipboardData); } }); document.body.appendChild(mark); range.selectNodeContents(mark); selection.addRange(range); const successful = document.execCommand("copy"); if (!successful) { throw new Error("copy command was unsuccessful"); } success = true; } catch (err) { debug && console.error("unable to copy using execCommand: ", err); debug && console.warn("trying IE specific stuff"); try { window.clipboardData.setData(options.format || "text", text); options.onCopy && options.onCopy(window.clipboardData); success = true; } catch (err2) { debug && console.error("unable to copy using clipboardData: ", err2); debug && console.error("falling back to prompt"); 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; } var copy_default = copy; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { copy });