@opensig/opendesign
Version:
<p align="center"><img src="../docs/public/opendesign-logo-light.png"/></p> <h1 align="center">opendesign</h1> <p align="center">一个 Vue 3 组件库</p> <p align="center"><b>皮肤可定制,使用 TypeScript</b></p>
135 lines (134 loc) • 3.18 kB
JavaScript
import { Comment, isRef, watchEffect, Fragment } from "vue";
import { isArray } from "./is.mjs";
import { isHtmlElement } from "./dom.mjs";
const isElement = (vnode) => {
return Boolean(
vnode && vnode.shapeFlag & 1
/* ELEMENT */
);
};
const isTextElement = (vnode) => {
return Boolean(
vnode && vnode.shapeFlag & 8
/* TEXT_CHILDREN */
);
};
function isComponent(vnode, _type) {
return Boolean(
vnode && vnode.shapeFlag & 6
/* COMPONENT */
);
}
const isSlotsChildren = (vnode, _children) => {
return Boolean(
vnode && vnode.shapeFlag & 32
/* SLOTS_CHILDREN */
);
};
const isArrayChildren = (vn, _children) => {
return Boolean(
vn && vn.shapeFlag & 16
/* ARRAY_CHILDREN */
);
};
function isComponentPublicInstance(val) {
return Boolean(val == null ? void 0 : val.$el);
}
function getFirstComponent(vn) {
var _a, _b;
if (isArray(vn)) {
for (const child of vn) {
const result = getFirstComponent(child);
if (result) {
return result;
}
}
} else if (isElement(vn) || isComponent(vn) || isTextElement(vn) && vn.type !== Comment) {
return vn;
} else if (isArrayChildren(vn, vn.children)) {
for (const child of vn.children) {
const result = getFirstComponent(child);
if (result) {
return result;
}
}
} else if (isSlotsChildren(vn, vn.children)) {
const children = (_b = (_a = vn.children).default) == null ? void 0 : _b.call(_a);
if (children) {
const result = getFirstComponent(children);
if (result) {
return result;
}
}
}
return null;
}
const resolveHtmlElement = (elRef) => {
const queryElement = (el) => {
if (typeof el === "string") {
return document.querySelector(el);
} else if (isHtmlElement(el)) {
return el;
}
return null;
};
return new Promise((resolve) => {
if (isRef(elRef)) {
watchEffect(() => {
const { value } = elRef;
if (value) {
if (isComponentPublicInstance(value)) {
resolve(value.$el);
} else {
resolve(queryElement(value));
}
}
});
} else if (isComponentPublicInstance(elRef)) {
resolve(elRef.$el);
} else {
resolve(queryElement(elRef));
}
});
};
const isEmptySlot = (slot) => {
var _a;
if (!slot) {
return true;
}
const children = slot();
if (children.length > 1) {
return false;
}
if (children.length === 0) {
return true;
}
if (isTextElement(children[0]) && !children[0].children) {
return true;
}
if (children[0].type === Comment) {
return true;
}
if (children[0].type === Fragment) {
return !((_a = children[0].children) == null ? void 0 : _a.length);
}
return false;
};
function filterSlots(slots, slotNames) {
const names = Object.values(slotNames);
const keys = Object.keys(slots);
const r = keys.filter((item) => names.includes(item));
return r || [];
}
export {
filterSlots,
getFirstComponent,
isArrayChildren,
isComponent,
isComponentPublicInstance,
isElement,
isEmptySlot,
isSlotsChildren,
isTextElement,
resolveHtmlElement
};