omi-vueify
Version:
Bridge between omi and vue
88 lines (87 loc) • 2.81 kB
JavaScript
import { defineComponent as L, ref as E, onMounted as R, onBeforeUnmount as U, h as K, watch as T, isRef as j, isReactive as w, toRaw as Z } from "vue";
function x(e, n) {
return L({
name: e,
inheritAttrs: !1,
setup(s, { emit: h, attrs: u, slots: o, expose: p }) {
const c = E(null), { methodNames: O } = n, A = {};
O.forEach((t) => {
A[t] = (...r) => {
var f, i;
if (c.value)
return (i = (f = c.value)[t]) == null ? void 0 : i.call(f, ...r);
};
}), p(A);
const b = V(u), z = Object.keys(u).filter((t) => t.match(/^on[A-Za-z]/)).map((t) => g(t)), d = /* @__PURE__ */ new Map();
return R(() => {
z.forEach((t) => {
var i;
const r = y(t);
if (!M(r) && t !== r) return;
const f = (a) => {
h(r, a);
};
d.set(t, f), (i = c.value) == null || i.addEventListener(t, f);
}), Object.entries(b.value).forEach(([t, r]) => {
typeof r == "function" && (c.value[C(t)] = r, delete b.value[t]);
});
}), U(() => {
d.forEach((t, r) => {
var f;
(f = c.value) == null || f.removeEventListener(r, t);
}), d.clear();
}), () => {
const t = [];
return o.default && t.push(...o.default()), Object.keys(o).forEach((r) => {
var i;
if (r === "default") return;
const f = (i = o[r]) == null ? void 0 : i.call(o);
f && f.forEach((a) => {
Array.isArray(a) ? a.forEach((l) => {
l && typeof l == "object" && (l.props = { ...l.props || {}, slot: r });
}) : a && typeof a == "object" && (a.props = { ...a.props || {}, slot: r }), t.push(a);
});
}), K(
e,
{
ref: c,
...s,
...b.value
},
t
);
};
}
});
}
const g = (e) => {
const n = e.slice(2);
return n[0].toLowerCase() + n.slice(1);
}, M = (e) => e.includes("-"), y = (e) => e.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), C = (e) => e.replace(/-([a-z])/g, (n, s) => s.toUpperCase()), m = (e) => {
if (j(e)) return m(e.value);
if (w(e) && (e = Z(e)), Array.isArray(e)) return e.map(m);
if (e && typeof e == "object") {
const n = {};
for (const s in e) n[s] = m(e[s]);
return n;
}
return e;
}, V = (e) => {
const n = E({});
return T(
() => Object.entries(e),
(s) => {
const h = {};
s.forEach(([u, o]) => {
if (!u.match(/^on[A-Za-z]/)) {
let p = u, c = o;
(j(o) || w(o)) && (c = m(o)), c && typeof c == "object" ? p = C(u) : p = y(u), h[p] = c;
}
}), n.value = h;
},
{ immediate: !0, deep: !0 }
), n;
};
export {
x as omiVueify
};