99 lines
3.7 KiB
JavaScript
99 lines
3.7 KiB
JavaScript
import { useSlots as h, ref as c, computed as b, onMounted as k, openBlock as _, createElementBlock as S, normalizeClass as A, unref as B } from "vue";
|
|
async function L(l) {
|
|
try {
|
|
return navigator.clipboard.writeText(l);
|
|
} catch {
|
|
const e = document.createElement("textarea"), a = document.activeElement;
|
|
e.value = l, e.setAttribute("readonly", ""), e.style.contain = "strict", e.style.position = "absolute", e.style.left = "-9999px", e.style.fontSize = "12pt";
|
|
const t = document.getSelection(), n = t ? t.rangeCount > 0 && t.getRangeAt(0) : null;
|
|
document.body.appendChild(e), e.select(), e.selectionStart = 0, e.selectionEnd = l.length, document.execCommand("copy"), document.body.removeChild(e), t && n && (t.removeAllRanges(), t.addRange(n)), a && a.focus();
|
|
}
|
|
}
|
|
const N = (l, e) => {
|
|
const a = l.__vccOpts || l;
|
|
for (const [t, n] of e)
|
|
a[t] = n;
|
|
return a;
|
|
}, $ = ["data-message", "data-label"], M = ["innerHTML"], d = {
|
|
position: "auto",
|
|
target: "auto",
|
|
message: "copied",
|
|
label: null,
|
|
classes: "copy-btn",
|
|
preferSibling: "previous"
|
|
}, O = {
|
|
__name: "CopyButton",
|
|
props: {
|
|
position: {
|
|
// start, end, auto
|
|
type: String,
|
|
default: () => d.position
|
|
},
|
|
target: {
|
|
// previous, next, auto
|
|
type: String,
|
|
default: () => d.target
|
|
},
|
|
message: {
|
|
type: String,
|
|
default: () => d.message
|
|
},
|
|
label: {
|
|
type: String,
|
|
default: () => d.label
|
|
},
|
|
classes: {
|
|
type: String,
|
|
default: () => d.classes
|
|
},
|
|
content: {
|
|
type: String,
|
|
default: null
|
|
}
|
|
},
|
|
setup(l) {
|
|
var v, g;
|
|
const e = l, a = h(), t = c(null), n = c(null), p = c(!0), f = c(null), r = c(null), C = e.label ? e.label : (g = (v = a.default) == null ? void 0 : v.call(a)[0]) == null ? void 0 : g.children, E = b(() => `<!-- Button not rendered: ${f.value} -->`), x = b(() => e.message.replace("$CONTENT", r.value));
|
|
k(() => {
|
|
var m, y;
|
|
const s = ((m = t.value.previousElementSibling) == null ? void 0 : m.tagName) === "CODE" ? t.value.previousElementSibling : null, i = ((y = t.value.nextElementSibling) == null ? void 0 : y.tagName) === "CODE" ? t.value.nextElementSibling : null;
|
|
if (!s && !i && !e.content) {
|
|
p.value = !1, f.value = "No code element found and no content prop given";
|
|
return;
|
|
}
|
|
if (e.content) {
|
|
r.value = e.content;
|
|
return;
|
|
}
|
|
let o = e.position;
|
|
if (e.target === "auto" ? s && i ? (n.value = s, o === "auto" && (o = "end")) : n.value = s || i : e.target === "previous" ? (n.value = s, o === "auto" && (o = "end")) : e.target === "next" && (n.value = i, o === "auto" && (o = "start")), !n.value) {
|
|
f.value = "Failed to select code node";
|
|
return;
|
|
}
|
|
let u = "beforeend";
|
|
o === "auto" ? s ? u = "beforeend" : i && (u = "afterbegin") : o === "start" ? u = "afterbegin" : o === "end" && (u = "beforeend"), r.value = n.value.innerText, n.value.innerText = "", n.value.insertAdjacentHTML("beforeend", `<span>${r.value}</span>`), n.value.insertAdjacentElement(u, t.value), t.value.classList.add(`copy-btn-${u}`);
|
|
});
|
|
async function T() {
|
|
await L(r.value), t.value.classList.add("copied"), setTimeout(() => {
|
|
t.value.classList.remove("copied");
|
|
}, 1e3);
|
|
}
|
|
return (s, i) => p.value ? (_(), S("span", {
|
|
key: 0,
|
|
class: A(l.classes),
|
|
ref_key: "btn",
|
|
ref: t,
|
|
onClick: T,
|
|
"data-message": x.value,
|
|
"data-label": B(C)
|
|
}, null, 10, $)) : (_(), S("span", {
|
|
key: 1,
|
|
innerHTML: E.value
|
|
}, null, 8, M));
|
|
}
|
|
}, R = /* @__PURE__ */ N(O, [["__scopeId", "data-v-32f61984"]]), H = R;
|
|
export {
|
|
R as default,
|
|
H as defaultSettings
|
|
};
|