UNPKG

@opensig/opendesign

Version:

207 lines (206 loc) 5.21 kB
import { Comment, toValue, isRef, watch, Fragment, isVNode, camelize, capitalize, h } from "vue"; import { isArray, isString, isFunction, isNil } from "./is.mjs"; import { isHtmlElement } from "./dom.mjs"; import { log } from "./log.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 queryElement = (el) => { if (typeof el === "string") { return document.querySelector(el); } else if (isHtmlElement(el)) { return el; } return null; }; const getHtmlElement = (elRef) => { const elQuery = toValue(elRef); if (isComponentPublicInstance(elQuery)) { return elQuery.$el; } else { return queryElement(elQuery); } }; const resolveHtmlElement = (elRef) => { return new Promise((resolve) => { if (isRef(elRef) && !elRef.value) { const closeWatch = watch(elRef, (el, oldEl) => { if (el) { resolve(getHtmlElement(el)); closeWatch(); } else { log.warn( `resolveHtmlElement: elRef value is falsy, this might be a bug and could cause the promise to remain pending. Please check elRef.value: ${oldEl} -> ${el}` ); } }); } else { resolve(getHtmlElement(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); return keys.filter((item) => names.includes(item)); } function mergeClass(...classList) { let rlt = []; classList.forEach((item) => { if (isArray(item)) { rlt = rlt.concat(item); } else if (item) { rlt.push(item); } }); return rlt; } function getRenderableComponent(content) { if (isNil(content)) { return null; } if (typeof content === "string") { return () => content; } if (isVNode(content)) { return () => content; } if (typeof content === "function" || typeof content === "object") { return () => h(content); } return () => content.toString(); } const isVNodeOfType = (vn, type) => { if (isString(type)) { if (isString(vn.type)) { return vn.type.toLowerCase() === type.toLowerCase(); } if (isComponent(vn, vn.type)) { const selfName = isFunction(vn.type) ? vn.type.displayName : vn.type.name || vn.type.__name; return selfName && (selfName === type || selfName === camelize(type) || selfName === capitalize(camelize(type))); } } return vn.type === type; }; const flatComponentVNode = (vn, type) => { const res = []; const _vn = isArray(vn) ? vn : [vn]; _vn.forEach((child) => { var _a; if (isArray(child)) { res.push(...flatComponentVNode(child, type)); return; } if (!isVNode(child)) { return; } if (isVNodeOfType(child, type)) { res.push(child); } if ((_a = child.component) == null ? void 0 : _a.subTree) { res.push(...flatComponentVNode(child.component.subTree, type)); } else if (child.children) { res.push(...flatComponentVNode(child.children, type)); } }); return res; }; export { filterSlots, flatComponentVNode, getFirstComponent, getHtmlElement, getRenderableComponent, isArrayChildren, isComponent, isComponentPublicInstance, isElement, isEmptySlot, isSlotsChildren, isTextElement, isVNodeOfType, mergeClass, resolveHtmlElement };