tav-ui
Version:
56 lines (51 loc) • 1.72 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var is = require('../../utils/is2.js');
function useCopyToClipboard(initial) {
const clipboardRef = vue.ref(initial || "");
const isSuccessRef = vue.ref(false);
const copiedRef = vue.ref(false);
vue.watch(clipboardRef, (str) => {
if (is.isDef(str)) {
copiedRef.value = true;
isSuccessRef.value = copyTextToClipboard(str);
}
}, { immediate: !!initial, flush: "sync" });
return { clipboardRef, isSuccessRef, copiedRef };
}
function copyTextToClipboard(input, { target = document.body } = {}) {
const element = document.createElement("textarea");
const previouslyFocusedElement = document.activeElement;
element.value = input;
element.setAttribute("readonly", "");
element.style.contain = "strict";
element.style.position = "absolute";
element.style.left = "-9999px";
element.style.fontSize = "12pt";
const selection = document.getSelection();
let originalRange;
if (selection && selection.rangeCount > 0)
originalRange = selection.getRangeAt(0);
target.append(element);
element.select();
element.selectionStart = 0;
element.selectionEnd = input.length;
let isSuccess = false;
try {
isSuccess = document.execCommand("copy");
} catch (e) {
throw new Error(e);
}
element.remove();
if (originalRange && selection) {
selection.removeAllRanges();
selection.addRange(originalRange);
}
if (previouslyFocusedElement)
previouslyFocusedElement.focus();
return isSuccess;
}
exports.copyTextToClipboard = copyTextToClipboard;
exports.useCopyToClipboard = useCopyToClipboard;
//# sourceMappingURL=useCopyToClipboard2.js.map