UNPKG

vue-amazing-ui

Version:

An Amazing Vue3 UI Components Library, Using TypeScript.

671 lines (670 loc) 23.9 kB
import { defineComponent as Ml, ref as A, computed as c, watch as Le, watchEffect as Rl, createBlock as J, openBlock as b, unref as z, normalizeStyle as v, withCtx as ae, createElementVNode as n, mergeProps as me, createElementBlock as R, createCommentVNode as H, renderSlot as Ne, createTextVNode as se, toDisplayString as ge, Fragment as be, normalizeClass as Hl, withModifiers as El, renderList as Be } from "vue"; import { toRgbaString as U, toHexString as K, rgba as x, rgb2hsv as ke, hsla as j, hsl2hsv as Pe, hsva as X, hsl2rgb as ye, hsv2rgb as B, rgb2hsl as we, hsv2hsl as ne, toHexaString as P, toHslaString as _, toRgbString as $e, toHslString as Se, toHsvaString as O, toHsvString as Ae } from "seemly"; import zl from "../tooltip/index.js"; import _e from "../input/index.js"; import De from "../button/index.js"; import { useInject as Ul, useSlotsExist as xl } from "../utils/index.js"; const Vl = { class: "color-picker-preview" }, Il = { class: "color-picker-preview-sliders" }, Ll = { key: 0, class: "color-picker-preview-circle" }, Nl = ["value"], Bl = { class: "color-picker-input" }, Pl = { class: "color-picker-input-group" }, _l = { key: 0, class: "color-picker-swatches" }, Dl = ["onClick"], Fl = { key: 0, class: "color-picker-actions" }, Xl = { key: 1, class: "color-picker-footer" }, Ol = { class: "color-picker-wrap" }, Zl = { class: "color-picker-fill" }, k = "12px", Fe = 12, f = "6px", Xe = 6, Jl = /* @__PURE__ */ Ml({ __name: "ColorPicker", props: { width: { default: "100%" }, label: { type: Function, default: void 0 }, tooltipStyle: { default: () => ({}) }, inputProps: { default: () => ({}) }, showAlpha: { type: Boolean, default: !0 }, showPreview: { type: Boolean, default: !1 }, size: { default: "middle" }, disabled: { type: Boolean, default: !1 }, value: { default: void 0 }, modes: { default: () => ["rgb", "hex", "hsl"] }, swatches: { default: () => [] }, actions: { default: () => [] }, footer: { default: void 0 } }, emits: ["update:value", "complete", "confirm", "clear"], setup(Oe, { emit: Ze }) { const s = Oe, ue = A(), Q = A(null), ee = A(null), ie = A(null), E = A(0), ce = A(1), le = A([0, 0]), G = A(), i = A(s.value), m = A(re(i.value) || s.modes[0] || "rgb"), te = A(), V = A([]), { colorPalettes: Ce, shadowColor: Te } = Ul("ColorPicker"), D = Ze, je = xl("footer"), Ge = c(() => je.value || s.footer), We = c(() => ({ width: k, height: k, borderRadius: f, left: `calc(${le.value[0]}% - ${f})`, bottom: `calc(${le.value[1]}% - ${f})` })), Ye = c(() => h.value ? `rgb(${h.value[0]}, ${h.value[1]}, ${h.value[2]})` : ""), qe = c(() => ({ backgroundColor: Ye.value, borderRadius: f, width: k, height: k })), Me = c(() => ({ height: k, borderRadius: f })), Je = c(() => ({ position: "relative", boxShadow: "inset 0 0 2px 0 rgba(0, 0, 0, 0.24)", backgroundImage: "linear-gradient(90deg, red, #ff0 16.66%, #0f0 33.33%, #0ff 50%, #00f 66.66%, #f0f 83.33%, red)", height: k, borderRadius: f })), Ke = c(() => ({ left: `calc((${E.value}%) / 359 * 100 - ${f})`, borderRadius: f, width: k, height: k })), Qe = c(() => ({ backgroundColor: `hsl(${E.value}, 100%, 50%)`, borderRadius: f, width: k, height: k })), el = c(() => h.value ? `linear-gradient(to right, rgba(${h.value[0]}, ${h.value[1]}, ${h.value[2]}, 0) 0%, rgba(${h.value[0]}, ${h.value[1]}, ${h.value[2]}, 1) 100%)` : ""), ll = c(() => ({ left: `calc(${ce.value * 100}% - ${f})`, borderRadius: f, width: k, height: k })), tl = c(() => ({ backgroundColor: h.value ? U(h.value) : void 0, borderRadius: f, width: k, height: k })), Re = c(() => h.value && K(h.value)), He = c(() => typeof s.width == "number" ? `${s.width}px` : s.width), de = c(() => `${{ small: 24, middle: 32, large: 40 }[s.size]}px`), rl = c(() => { const e = { small: 6, middle: 8, large: 10 }; return `calc((${de.value} - ${e[s.size]}px) / 3)`; }), ve = c(() => re(i.value)), W = c(() => m.value.toUpperCase() + (s.showAlpha ? "A" : "")), ol = c(() => W.value.split("")); let y, w, Z, T, I, L, N, $; const C = c(() => { if (!i.value) return null; switch (ve.value) { case "hsv": return X(i.value); case "hsl": return [y, w, Z, $] = j(i.value), [...Pe(y, w, Z), $]; case "rgb": case "hex": return [I, L, N, $] = x(i.value), [...ke(I, L, N), $]; } }), h = c(() => { if (!i.value) return null; switch (ve.value) { case "rgb": case "hex": return x(i.value); case "hsv": return [y, w, T, $] = X(i.value), [...B(y, w, T), $]; case "hsl": return [y, w, Z, $] = j(i.value), [...ye(y, w, Z), $]; } }), F = c(() => { if (!i.value) return null; switch (ve.value) { case "hsl": return j(i.value); case "hsv": return [y, w, T, $] = X(i.value), [...ne(y, w, T), $]; case "rgb": case "hex": return [I, L, N, $] = x(i.value), [...we(I, L, N), $]; } }), M = c(() => { switch (m.value) { case "rgb": case "hex": return h.value; case "hsv": return C.value; case "hsl": return F.value; } }), Ee = c( () => s.swatches.map((e) => { const l = re(e); return { value: e, mode: l, legalValue: yl(e, l) }; }) ); Le( () => s.value, (e) => { i.value = e; } ), Le( () => [m.value, M.value], () => { m.value === "hex" ? te.value = M.value === null ? void 0 : (s.showAlpha ? P : K)(M.value) : M.value === null ? V.value = new Array(s.showAlpha ? 4 : 3).fill(void 0) : V.value = M.value.map((e, l) => s.showAlpha && l === 3 ? `${Math.floor(e * 100)}%` : `${Math.floor(e)}`); }, { immediate: !0, deep: !0 } ), Rl(() => { (G.value === void 0 || G.value !== i.value) && C.value && (E.value = C.value[0], ce.value = C.value[3], le.value = [C.value[1], C.value[2]]), G.value = void 0; }); function g(e, l) { l === "cursor" ? G.value = e : G.value = void 0, i.value = e, D("update:value", e), l === "input" && D("complete", e); } function al(e, l) { const t = C.value ? C.value[3] : 1; switch (le.value = [e, l], m.value) { case "hsv": g((s.showAlpha ? O : Ae)([E.value, e, l, t]), "cursor"); break; case "hsl": g( (s.showAlpha ? _ : Se)([...ne(E.value, e, l), t]), "cursor" ); break; case "rgb": g( (s.showAlpha ? U : $e)([...B(E.value, e, l), t]), "cursor" ); break; case "hex": g( (s.showAlpha ? P : K)([...B(E.value, e, l), t]), "cursor" ); break; } } function sl(e) { Q.value && (document.addEventListener("mousemove", he), document.addEventListener("mouseup", ze), he(e)); } function he(e) { if (!Q.value) return; const { width: l, height: t, left: o, bottom: S } = Q.value.getBoundingClientRect(), r = (S - e.clientY) / t, a = (e.clientX - o) / l, u = 100 * (a > 1 ? 1 : a < 0 ? 0 : a), d = 100 * (r > 1 ? 1 : r < 0 ? 0 : r); al(u, d); } function ze() { document.removeEventListener("mousemove", he), document.removeEventListener("mouseup", ze), D("complete", i.value); } function nl(e) { if (E.value = e, !C.value) return; const [, l, t, o] = C.value; switch (m.value) { case "hsv": g((s.showAlpha ? O : Ae)([e, l, t, o]), "cursor"); break; case "rgb": g((s.showAlpha ? U : $e)([...B(e, l, t), o]), "cursor"); break; case "hex": g((s.showAlpha ? P : K)([...B(e, l, t), o]), "cursor"); break; case "hsl": g((s.showAlpha ? _ : Se)([...ne(e, l, t), o]), "cursor"); break; } } function ul(e) { ee.value && (document.addEventListener("mousemove", pe), document.addEventListener("mouseup", Ue), pe(e)); } function il(e) { return e = Math.round(e), e >= 360 ? 359 : e < 0 ? 0 : e; } function pe(e) { if (!ee.value) return; const { width: l, left: t } = ee.value.getBoundingClientRect(), o = il((e.clientX - t - Xe) / (l - Fe) * 360); nl(o); } function Ue() { document.removeEventListener("mousemove", pe), document.removeEventListener("mouseup", Ue), D("complete", i.value); } function cl(e) { switch (m.value) { case "hsv": [y, w, T] = C.value, g(O([y, w, T, e]), "cursor"); break; case "rgb": [I, L, N] = h.value, g(U([I, L, N, e]), "cursor"); break; case "hex": [I, L, N] = h.value, g(P([I, L, N, e]), "cursor"); break; case "hsl": [y, w, Z] = F.value, g(_([y, w, Z, e]), "cursor"); break; } ce.value = e; } function dl(e) { document.addEventListener("mousemove", fe), document.addEventListener("mouseup", xe), fe(e); } function vl(e) { return e = Math.round(e * 100) / 100, e > 1 ? 1 : e < 0 ? 0 : e; } function fe(e) { if (!ie.value) return; const { width: l, left: t } = ie.value.getBoundingClientRect(), o = vl((e.clientX - t - Xe) / (l - Fe)); cl(o); } function xe() { document.removeEventListener("mousemove", fe), document.removeEventListener("mouseup", xe), D("complete", i.value); } function re(e) { if (e !== void 0) { if (/^ *#/.test(e)) return "hex"; if (e.includes("rgb")) return "rgb"; if (e.includes("hsl")) return "hsl"; if (e.includes("hsv")) return "hsv"; } } function hl() { const e = s.modes.findIndex((l) => l === m.value); e !== -1 ? m.value = s.modes[(e + 1) % s.modes.length] : m.value = "rgb"; } function pl(e) { const l = e.target, t = Ie(l.value.toUpperCase(), m.value, "hex"); g(t, "input"); } function fl(e) { const l = e.trim(); return /^#[0-9a-fA-F]+$/.test(l) ? [4, 5, 7, 9].includes(l.length) : !1; } function ml(e) { return /^\d{1,3}\.?\d*$/.test(e.trim()) ? Math.max(0, Math.min(Number.parseInt(e), 360)) : !1; } function gl(e) { return /^\d{1,3}\.?\d*$/.test(e.trim()) ? Math.max(0, Math.min(Number.parseInt(e), 100)) : !1; } function bl(e) { return /^\d{1,3}\.?\d*%$/.test(e.trim()) ? Math.max(0, Math.min(Number.parseInt(e) / 100, 100)) : !1; } function kl(e) { return /^\d{1,3}\.?\d*$/.test(e.trim()) ? Math.max(0, Math.min(Number.parseInt(e), 255)) : !1; } function Y(e, l) { return e === void 0 ? "" : l === "HEX" || l === "HEXA" ? e : l === "A" ? `${Math.floor(Number(e) * 100)}%` : String(Math.floor(Number(e))); } function oe(e) { g(e, "input"); } function q(e, l) { if (m.value === "hex") { oe((s.showAlpha ? P : K)(l)); return; } let t; switch (M.value === null ? t = [0, 0, 0, 0] : t = [...M.value], m.value) { case "hsv": t[e] = l, oe((s.showAlpha ? O : Ae)(t)); break; case "rgb": t[e] = l, oe((s.showAlpha ? U : $e)(t)); break; case "hsl": t[e] = l, oe((s.showAlpha ? _ : Se)(t)); break; } } function Ve(e, l) { const t = e.target; let o, S, r; const a = t.value; l === void 0 ? r = M.value === null ? void 0 : M.value : r = M.value?.[l] === void 0 ? void 0 : M.value?.[l].toString(); const u = l === void 0 ? W.value : W.value[l]; switch (u) { case "HEX": case "HEXA": S = fl(a), S && q(0, a), te.value = Y(a, u); break; case "H": o = ml(a), o === !1 ? V.value[l] = Y(r, u) : q(l, o); break; case "S": case "L": case "V": o = gl(a), o === !1 ? V.value[l] = Y(r, u) : q(l, o); break; case "A": o = bl(a), o === !1 ? V.value[l] = Y(r, u) : q(l, o); break; case "R": case "G": case "B": o = kl(a), o === !1 ? V.value[l] = Y(r, u) : q(l, o); break; } } function yl(e, l) { if (l === "hsv") { const [t, o, S, r] = X(e); return U([...B(t, o, S), r]); } return e; } function wl(e) { const l = document.createElement("canvas").getContext("2d"); return l ? (l.fillStyle = e, l.fillStyle) : "#000000"; } function Ie(e, l, t) { return t = t || re(e), t ? t === l ? e : { rgb: { hex(r) { return P(x(r)); }, hsl(r) { const [a, u, d, p] = x(r); return _([...we(a, u, d), p]); }, hsv(r) { const [a, u, d, p] = x(r); return O([...ke(a, u, d), p]); } }, hex: { rgb(r) { return U(x(r)); }, hsl(r) { const [a, u, d, p] = x(r); return _([...we(a, u, d), p]); }, hsv(r) { const [a, u, d, p] = x(r); return O([...ke(a, u, d), p]); } }, hsl: { hex(r) { const [a, u, d, p] = j(r); return P([...ye(a, u, d), p]); }, rgb(r) { const [a, u, d, p] = j(r); return U([...ye(a, u, d), p]); }, hsv(r) { const [a, u, d, p] = j(r); return O([...Pe(a, u, d), p]); } }, hsv: { hex(r) { const [a, u, d, p] = X(r); return P([...B(a, u, d), p]); }, rgb(r) { const [a, u, d, p] = X(r); return U([...B(a, u, d), p]); }, hsl(r) { const [a, u, d, p] = X(r); return _([...ne(a, u, d), p]); } } }[t][l](e) : void 0; } function $l(e) { let { value: l, mode: t } = e; return t || (t = "hex", /^[a-zA-Z]+$/.test(l) ? l = wl(l) : (console.warn("color-picker", `color ${l} in swatches is invalid.`), l = "#000000")), t === m.value ? l : Ie(l, m.value, t); } function Sl(e) { g($l(e), "input"); } function Al() { D("confirm", i.value), ue.value.hide(); } function Cl() { g(void 0, "input"), D("clear"), ue.value.hide(); } return (e, l) => (b(), J(z(zl), { ref_key: "tooltipRef", ref: ue, style: v(`width: ${He.value}; height: ${de.value};`), "max-width": "none", arrow: !1, placement: "bottom", "bg-color": "#fff", "tooltip-style": { width: "240px", padding: 0, borderRadius: "4px", color: "rgba(0, 0, 0, 0.88)", ...e.tooltipStyle }, "content-style": { width: "100%", height: "100%" }, trigger: "click", "transition-duration": 200 }, { tooltip: ae(() => [ e.disabled ? H("", !0) : (b(), R(be, { key: 0 }, [ n("div", { class: Hl(["color-picker-panel", { "panel-with-actions": e.actions.length }]) }, [ n("div", { ref_key: "palleteRef", ref: Q, class: "color-picker-pallete", onMousedown: sl }, [ n("div", { class: "color-picker-pallete-layer", style: v({ backgroundImage: `linear-gradient(90deg, white, hsl(${E.value}, 100%, 50%))` }) }, null, 4), l[1] || (l[1] = n("div", { class: "color-picker-pallete-layer pallete-layer-shadowed", style: { "background-image": "linear-gradient(180deg, rgba(0, 0, 0, 0%), rgba(0, 0, 0, 100%))" } }, null, -1)), n("div", { class: "color-picker-handle", style: v(We.value) }, [ n("div", { class: "color-picker-handle-fill", style: v(qe.value) }, null, 4) ], 4) ], 544), n("div", Vl, [ n("div", Il, [ n("div", { class: "color-picker-slider", style: v(Me.value) }, [ n("div", { ref_key: "hueRailRef", ref: ee, style: v(Je.value), onMousedown: ul }, [ n("div", { style: v(`position: absolute; top: 0px; bottom: 0; left: ${f}; right: ${f}`) }, [ n("div", { class: "color-picker-handle", style: v(Ke.value) }, [ n("div", { class: "color-picker-handle-fill", style: v(Qe.value) }, null, 4) ], 4) ], 4) ], 36) ], 4), e.showAlpha ? (b(), R("div", { key: 0, ref_key: "alphaRailRef", ref: ie, class: "color-picker-slider", style: v(Me.value), onMousedown: dl }, [ n("div", { style: v(` border-radius: ${f}; position: absolute; top: 0px; bottom: 0; left: 0px; right: 0px; overflow: hidden; `) }, [ l[2] || (l[2] = n("div", { class: "color-picker-checkboard" }, null, -1)), n("div", { class: "color-picker-slider-image", style: v(`background-image: ${el.value}`) }, null, 4) ], 4), h.value ? (b(), R("div", { key: 0, style: v(`position: absolute; top: 0px; bottom: 0; left: ${f}; right: ${f}`) }, [ n("div", { class: "color-picker-handle", style: v(ll.value) }, [ n("div", { class: "color-picker-handle-fill", style: v(tl.value) }, null, 4) ], 4) ], 4)) : H("", !0) ], 36)) : H("", !0) ]), e.showPreview ? (b(), R("div", Ll, [ n("span", { class: "color-picker-circle-fill", style: v(`background: ${Re.value || "#000000"};`) }, null, 4), n("input", { class: "color-picker-circle-input", type: "color", value: Re.value, onChange: El(pl, ["stop"]) }, null, 40, Nl) ])) : H("", !0) ]), n("div", Bl, [ n("div", { class: "color-picker-input-mode", style: v({ cursor: e.modes.length === 1 ? "" : "pointer" }), onClick: hl }, ge(W.value), 5), n("div", Pl, [ m.value === "hex" ? (b(), J(z(_e), me({ key: 0, size: "small", placeholder: W.value, value: te.value, "onUpdate:value": l[0] || (l[0] = (t) => te.value = t), valueModifiers: { lazy: !0 }, onChange: Ve }, e.inputProps), null, 16, ["placeholder", "value"])) : (b(!0), R(be, { key: 1 }, Be(ol.value, (t, o) => (b(), J(z(_e), me({ size: "small", style: `${t === "A" ? "flex-grow: 1.25" : ""}`, key: o, placeholder: t, value: V.value[o], "onUpdate:value": (S) => V.value[o] = S, valueModifiers: { lazy: !0 }, onChange: (S) => Ve(S, o) }, { ref_for: !0 }, e.inputProps), null, 16, ["style", "placeholder", "value", "onUpdate:value", "onChange"]))), 128)) ]) ]), Ee.value.length ? (b(), R("div", _l, [ (b(!0), R(be, null, Be(Ee.value, (t, o) => (b(), R("div", { tabindex: "0", class: "color-picker-swatch", key: o, onClick: (S) => Sl(t) }, [ n("div", { class: "color-picker-swatch-fill", style: v(`background: ${t.legalValue};`) }, null, 4) ], 8, Dl))), 128)) ])) : H("", !0) ], 2), e.actions.length ? (b(), R("div", Fl, [ e.actions.includes("confirm") ? (b(), J(z(De), { key: 0, type: "primary", size: "small", onClick: Al }, { default: ae(() => [...l[3] || (l[3] = [ se("确认", -1) ])]), _: 1 })) : H("", !0), e.actions.includes("clear") ? (b(), J(z(De), { key: 1, size: "small", onClick: Cl }, { default: ae(() => [...l[4] || (l[4] = [ se("清除", -1) ])]), _: 1 })) : H("", !0) ])) : H("", !0), Ge.value ? (b(), R("div", Xl, [ Ne(e.$slots, "footer", {}, () => [ se(ge(e.footer), 1) ], !0) ])) : H("", !0) ], 64)) ]), default: ae(() => [ n("div", me({ tabindex: "1", class: ["color-picker-display", [`color-picker-${e.size}`, { "color-picker-disabled": e.disabled }]], style: ` --color-picker-width: ${He.value}; --color-picker-height: ${de.value}; --color-picker-block-size: ${rl.value}; --color-picker-primary-color-hover: ${z(Ce)[4]}; --color-picker-primary-color-focus: ${z(Ce)[4]}; --color-picker-primary-shadow-color: ${z(Te)}; ` }, e.$attrs), [ n("div", Ol, [ n("div", Zl, [ l[5] || (l[5] = n("div", { class: "color-picker-checkboard" }, null, -1)), n("div", { style: v(`position: absolute; left: 0; right: 0; top: 0; bottom: 0; background-color: ${F.value ? z(_)(F.value) : ""};`) }, null, 4), i.value && F.value ? (b(), R("div", { key: 0, class: "color-picker-value", style: v({ color: F.value[2] > 50 || F.value[3] < 0.5 ? "black" : "white" }) }, [ Ne(e.$slots, "label", { color: i.value }, () => [ se(ge(e.label ? e.label(i.value) : i.value), 1) ], !0) ], 4)) : H("", !0) ]) ]) ], 16) ]), _: 3 }, 8, ["style", "tooltip-style"])); } }); export { Jl as default };