UNPKG

@opensig/opendesign

Version:

207 lines (206 loc) 5.61 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const is = require("./is.js"); const dom = require("./dom.js"); const log = require("./log.js"); 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 (is.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 !== vue.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 (dom.isHtmlElement(el)) { return el; } return null; }; const getHtmlElement = (elRef) => { const elQuery = vue.toValue(elRef); if (isComponentPublicInstance(elQuery)) { return elQuery.$el; } else { return queryElement(elQuery); } }; const resolveHtmlElement = (elRef) => { return new Promise((resolve) => { if (vue.isRef(elRef) && !elRef.value) { const closeWatch = vue.watch(elRef, (el, oldEl) => { if (el) { resolve(getHtmlElement(el)); closeWatch(); } else { log.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 === vue.Comment) { return true; } if (children[0].type === vue.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 (is.isArray(item)) { rlt = rlt.concat(item); } else if (item) { rlt.push(item); } }); return rlt; } function getRenderableComponent(content) { if (is.isNil(content)) { return null; } if (typeof content === "string") { return () => content; } if (vue.isVNode(content)) { return () => content; } if (typeof content === "function" || typeof content === "object") { return () => vue.h(content); } return () => content.toString(); } const isVNodeOfType = (vn, type) => { if (is.isString(type)) { if (is.isString(vn.type)) { return vn.type.toLowerCase() === type.toLowerCase(); } if (isComponent(vn, vn.type)) { const selfName = is.isFunction(vn.type) ? vn.type.displayName : vn.type.name || vn.type.__name; return selfName && (selfName === type || selfName === vue.camelize(type) || selfName === vue.capitalize(vue.camelize(type))); } } return vn.type === type; }; const flatComponentVNode = (vn, type) => { const res = []; const _vn = is.isArray(vn) ? vn : [vn]; _vn.forEach((child) => { var _a; if (is.isArray(child)) { res.push(...flatComponentVNode(child, type)); return; } if (!vue.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; }; exports.filterSlots = filterSlots; exports.flatComponentVNode = flatComponentVNode; exports.getFirstComponent = getFirstComponent; exports.getHtmlElement = getHtmlElement; exports.getRenderableComponent = getRenderableComponent; exports.isArrayChildren = isArrayChildren; exports.isComponent = isComponent; exports.isComponentPublicInstance = isComponentPublicInstance; exports.isElement = isElement; exports.isEmptySlot = isEmptySlot; exports.isSlotsChildren = isSlotsChildren; exports.isTextElement = isTextElement; exports.isVNodeOfType = isVNodeOfType; exports.mergeClass = mergeClass; exports.resolveHtmlElement = resolveHtmlElement;