UNPKG

winbox-ui-next

Version:

We Design Vue 2.0: A Vue.js 3 UI Library

267 lines (266 loc) 9.34 kB
import { cloneVNode, isVNode, Fragment } from "vue"; import { isString, isArray, isFunction } from "./is.js"; var ShapeFlags; (function(ShapeFlags2) { ShapeFlags2[ShapeFlags2["ELEMENT"] = 1] = "ELEMENT"; ShapeFlags2[ShapeFlags2["FUNCTIONAL_COMPONENT"] = 2] = "FUNCTIONAL_COMPONENT"; ShapeFlags2[ShapeFlags2["STATEFUL_COMPONENT"] = 4] = "STATEFUL_COMPONENT"; ShapeFlags2[ShapeFlags2["COMPONENT"] = 6] = "COMPONENT"; ShapeFlags2[ShapeFlags2["TEXT_CHILDREN"] = 8] = "TEXT_CHILDREN"; ShapeFlags2[ShapeFlags2["ARRAY_CHILDREN"] = 16] = "ARRAY_CHILDREN"; ShapeFlags2[ShapeFlags2["SLOTS_CHILDREN"] = 32] = "SLOTS_CHILDREN"; ShapeFlags2[ShapeFlags2["TELEPORT"] = 64] = "TELEPORT"; ShapeFlags2[ShapeFlags2["SUSPENSE"] = 128] = "SUSPENSE"; ShapeFlags2[ShapeFlags2["COMPONENT_SHOULD_KEEP_ALIVE"] = 256] = "COMPONENT_SHOULD_KEEP_ALIVE"; ShapeFlags2[ShapeFlags2["COMPONENT_KEPT_ALIVE"] = 512] = "COMPONENT_KEPT_ALIVE"; })(ShapeFlags || (ShapeFlags = {})); var PatchFlags; (function(PatchFlags2) { PatchFlags2[PatchFlags2["TEXT"] = 1] = "TEXT"; PatchFlags2[PatchFlags2["CLASS"] = 2] = "CLASS"; PatchFlags2[PatchFlags2["STYLE"] = 4] = "STYLE"; PatchFlags2[PatchFlags2["PROPS"] = 8] = "PROPS"; PatchFlags2[PatchFlags2["FULL_PROPS"] = 16] = "FULL_PROPS"; PatchFlags2[PatchFlags2["HYDRATE_EVENTS"] = 32] = "HYDRATE_EVENTS"; PatchFlags2[PatchFlags2["STABLE_FRAGMENT"] = 64] = "STABLE_FRAGMENT"; PatchFlags2[PatchFlags2["KEYED_FRAGMENT"] = 128] = "KEYED_FRAGMENT"; PatchFlags2[PatchFlags2["UNKEYED_FRAGMENT"] = 256] = "UNKEYED_FRAGMENT"; PatchFlags2[PatchFlags2["NEED_PATCH"] = 512] = "NEED_PATCH"; PatchFlags2[PatchFlags2["DYNAMIC_SLOTS"] = 1024] = "DYNAMIC_SLOTS"; PatchFlags2[PatchFlags2["DEV_ROOT_FRAGMENT"] = 2048] = "DEV_ROOT_FRAGMENT"; PatchFlags2[PatchFlags2["HOISTED"] = -1] = "HOISTED"; PatchFlags2[PatchFlags2["BAIL"] = -2] = "BAIL"; })(PatchFlags || (PatchFlags = {})); const getValueFromSlotsOrProps = (name, props, slots) => { if (slots == null ? void 0 : slots[name]) { return slots[name]; } if (props == null ? void 0 : props[name]) { return () => props[name]; } return void 0; }; const isElement = (vn) => { return Boolean(vn && vn.shapeFlag & 1); }; const isComponent = (vn, type) => { return Boolean(vn && vn.shapeFlag & 6); }; const isNamedComponent = (child, name) => { return isComponent(child, child.type) && child.type.name === name; }; const isTextChildren = (child, children) => { return Boolean(child && child.shapeFlag & 8); }; const isArrayChildren = (vn, children) => { return Boolean(vn && vn.shapeFlag & 16); }; const isSlotsChildren = (vn, children) => { return Boolean(vn && vn.shapeFlag & 32); }; const getFirstComponent = (children) => { var _a, _b; if (!children) { return void 0; } for (const child of children) { if (isElement(child) || isComponent(child)) { return child; } if (isArrayChildren(child, child.children)) { const result = getFirstComponent(child.children); if (result) return result; } else if (isSlotsChildren(child, child.children)) { const children2 = (_b = (_a = child.children).default) == null ? void 0 : _b.call(_a); if (children2) { const result = getFirstComponent(children2); if (result) return result; } } else if (isArray(child)) { const result = getFirstComponent(child); if (result) return result; } } return void 0; }; const getComponentNumber = (vNodes, componentName) => { let count = 0; for (const item of vNodes) { if (isComponent(item, item.type) && item.type.name === componentName) { count++; } else if (isArrayChildren(item, item.children)) { count += getComponentNumber(item.children, componentName); } } return count; }; const isEmptyChildren = (children) => { if (!children) { return true; } for (const item of children) { if (item.children) { return false; } } return true; }; const getChildrenComponents = (children, name, props, startIndex = 0) => { var _a, _b, _c; const result = []; for (const item of children) { if (isNamedComponent(item, name)) { if (props) { const index = startIndex + result.length; const extraProps = isFunction(props) ? props(item, index) : props; result.push(cloneVNode(item, extraProps, true)); } else { result.push(item); } } else if (isArrayChildren(item, item.children)) { result.push(...getChildrenComponents(item.children, name, props, result.length)); } else if (isSlotsChildren(item, item.children)) { const defaultChildren = (_c = (_b = (_a = item.children).default) == null ? void 0 : _b.call(_a)) != null ? _c : []; result.push(...getChildrenComponents(defaultChildren, name, props, result.length)); } } return result; }; const mergeFirstChild = (children, extraProps) => { if (children && children.length > 0) { for (let i = 0; i < children.length; i++) { const child = children[i]; if (isElement(child) || isComponent(child)) { const props = isFunction(extraProps) ? extraProps(child) : extraProps; children[i] = cloneVNode(child, props, true); return true; } const _children = getChildrenArray(child); if (_children && _children.length > 0) { const result = mergeFirstChild(_children, extraProps); if (result) return true; } } } return false; }; const getChildrenArray = (vn) => { if (isArrayChildren(vn, vn.children)) { return vn.children; } if (isArray(vn)) { return vn; } return void 0; }; const getFirstElementFromVNode = (vn) => { var _a, _b; if (isElement(vn)) { return vn.el; } if (isComponent(vn)) { if (((_a = vn.el) == null ? void 0 : _a.nodeType) === 1) { return vn.el; } if ((_b = vn.component) == null ? void 0 : _b.subTree) { const ele = getFirstElementFromVNode(vn.component.subTree); if (ele) return ele; } } else { const children = getChildrenArray(vn); return getFirstElementFromChildren(children); } return void 0; }; const getFirstElementFromChildren = (children) => { if (children && children.length > 0) { for (const child of children) { const element = getFirstElementFromVNode(child); if (element) return element; } } return void 0; }; const getBooleanProp = (value) => { return !!(value || isString(value)); }; const getAllElements = (children, includeText = false) => { var _a, _b; const results = []; for (const item of children != null ? children : []) { if (isElement(item) || isComponent(item) || includeText && isTextChildren(item, item.children)) { results.push(item); } else if (isArrayChildren(item, item.children)) { results.push(...getAllElements(item.children, includeText)); } else if (isSlotsChildren(item, item.children)) { results.push(...getAllElements((_b = (_a = item.children).default) == null ? void 0 : _b.call(_a), includeText)); } else if (isArray(item)) { results.push(...getAllElements(item, includeText)); } } return results; }; function unFragment(nodeList) { function loop(nodes) { const unFragmentNodeList = []; nodes.forEach((node) => { var _a, _b; if (isVNode(node) && node.type === Fragment) { if (isSlotsChildren(node, node.children)) { unFragmentNodeList.push(...loop(((_b = (_a = node.children).default) == null ? void 0 : _b.call(_a)) || [])); } else if (isArrayChildren(node, node.children)) { unFragmentNodeList.push(...loop(node.children)); } else if (isString(node.children)) { unFragmentNodeList.push(node.children); } } else { unFragmentNodeList.push(node); } }); return unFragmentNodeList; } return loop(nodeList); } const getSlotFunction = (param) => { if (param) { if (isFunction(param)) return param; return () => param; } return void 0; }; const getComponentsFromVNode = (vn, name) => { var _a; const components = []; if (isComponent(vn, vn.type)) { if (vn.type.name === name) { if (vn.component) { components.push(vn.component.uid); } } else if ((_a = vn.component) == null ? void 0 : _a.subTree) { components.push(...getComponentsFromVNode(vn.component.subTree, name)); } } else { const children = getChildrenArray(vn); if (children) { components.push(...getComponentsFromChildren(children, name)); } } return components; }; const getComponentsFromChildren = (children, name) => { const components = []; if (children && children.length > 0) { for (const child of children) { components.push(...getComponentsFromVNode(child, name)); } } return components; }; export { PatchFlags, ShapeFlags, getAllElements, getBooleanProp, getChildrenArray, getChildrenComponents, getComponentNumber, getComponentsFromChildren, getComponentsFromVNode, getFirstComponent, getFirstElementFromChildren, getFirstElementFromVNode, getSlotFunction, getValueFromSlotsOrProps, isArrayChildren, isComponent, isElement, isEmptyChildren, isNamedComponent, isSlotsChildren, isTextChildren, mergeFirstChild, unFragment };