You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
9.0 KiB

{"version":3,"file":"vnode.mjs","sources":["../../../../../packages/utils/vue/vnode.ts"],"sourcesContent":["import {\n Comment,\n Fragment,\n Text,\n createBlock,\n createCommentVNode,\n isVNode,\n openBlock,\n} from 'vue'\nimport { camelize } from '../strings'\nimport { isArray } from '../types'\nimport { hasOwn } from '../objects'\nimport { debugWarn } from '../error'\nimport type {\n VNode,\n VNodeArrayChildren,\n VNodeChild,\n VNodeNormalizedChildren,\n} from 'vue'\n\nconst SCOPE = 'utils/vue/vnode'\n\nexport enum PatchFlags {\n TEXT = 1,\n CLASS = 2,\n STYLE = 4,\n PROPS = 8,\n FULL_PROPS = 16,\n HYDRATE_EVENTS = 32,\n STABLE_FRAGMENT = 64,\n KEYED_FRAGMENT = 128,\n UNKEYED_FRAGMENT = 256,\n NEED_PATCH = 512,\n DYNAMIC_SLOTS = 1024,\n HOISTED = -1,\n BAIL = -2,\n}\n\nexport type VNodeChildAtom = Exclude<VNodeChild, Array<any>>\nexport type RawSlots = Exclude<\n VNodeNormalizedChildren,\n Array<any> | null | string\n>\n\nexport function isFragment(node: VNode): boolean\nexport function isFragment(node: unknown): node is VNode\nexport function isFragment(node: unknown): node is VNode {\n return isVNode(node) && node.type === Fragment\n}\n\nexport function isText(node: VNode): boolean\nexport function isText(node: unknown): node is VNode\nexport function isText(node: unknown): node is VNode {\n return isVNode(node) && node.type === Text\n}\n\nexport function isComment(node: VNode): boolean\nexport function isComment(node: unknown): node is VNode\nexport function isComment(node: unknown): node is VNode {\n return isVNode(node) && node.type === Comment\n}\n\nconst TEMPLATE = 'template'\nexport function isTemplate(node: VNode): boolean\nexport function isTemplate(node: unknown): node is VNode\nexport function isTemplate(node: unknown): node is VNode {\n return isVNode(node) && node.type === TEMPLATE\n}\n\n/**\n * determine if the element is a valid element type rather than fragments and comment e.g. <template> v-if\n * @param node {VNode} node to be tested\n */\nexport function isValidElementNode(node: VNode): boolean\nexport function isValidElementNode(node: unknown): node is VNode\nexport function isValidElementNode(node: unknown): node is VNode {\n return isVNode(node) && !isFragment(node) && !isComment(node)\n}\n\n/**\n * get a valid child node (not fragment nor comment)\n * @param node {VNode} node to be searched\n * @param depth {number} depth to be searched\n */\nfunction getChildren(\n node: VNodeNormalizedChildren | VNodeChild,\n depth: number\n): VNodeNormalizedChildren | VNodeChild {\n if (isComment(node)) return\n if (isFragment(node) || isTemplate(node)) {\n return depth > 0 ? getFirstValidNode(node.children, depth - 1) : undefined\n }\n return node\n}\n\nexport const getFirstValidNode = (\n nodes: VNodeNormalizedChildren,\n maxDepth = 3\n) => {\n if (Array.isArray(nodes)) {\n return getChildren(nodes[0], maxDepth)\n } else {\n return getChildren(nodes, maxDepth)\n }\n}\n\nexport function renderIf(\n condition: boolean,\n ...args: Parameters<typeof createBlock>\n) {\n return condition ? renderBlock(...args) : createCommentVNode('v-if', true)\n}\n\nexport function renderBlock(...args: Parameters<typeof createBlock>) {\n return openBlock(), createBlock(...args)\n}\n\nexport const getNormalizedProps = (node: VNode) => {\n if (!isVNode(node)) {\n debugWarn(SCOPE, '[getNormalizedProps] must be a VNode')\n return {}\n }\n\n const raw = node.props || {}\n const type = (isVNode(node.type) ? node.type.props : undefined) || {}\n const props: Record<string, any> = {}\n\n Object.keys(type).forEach((key) => {\n if (hasOwn(type[key], 'default')) {\n props[key] = type[key].default\n }\n })\n\n Object.keys(raw).forEach((key) => {\n props[camelize(key)] = raw[key]\n })\n\n return props\n}\n\nexport const ensureOnlyChild = (children: VNodeArrayChildren | undefined) => {\n if (!isArray(children) || children.length > 1) {\n throw new Error('expect to receive a single Vue element child')\n }\n return children[0]\n}\n\nexport type FlattenVNodes = Array<VNodeChildAtom | RawSlots>\n\nexport const flattedChildren = (\n children: FlattenVNodes | VNode | VNodeNormalizedChildren\n): FlattenVNodes => {\n const vNodes = isArray(children) ? children : [children]\n const result: FlattenVNodes = []\n\n vNodes.forEach((child) => {\n if (isArray(child)) {\n result.push(...flattedChildren(child))\n } else if (isVNode(child) && isArray(child.children)) {\n result.push(...flattedChildren(child.children))\n } else {\n result.push(child)\n if (isVNode(child) && child.component?.subTree) {\n result.push(...flattedChildren(child.component.subTree))\n }\n }\n })\n return result\n}\n"],"names":[],"mappings":";;;;;;;AAaA,MAAM,KAAK,GAAG,iBAAiB,CAAC;AACtB,IAAC,UAAU,mBAAmB,CAAC,CAAC,WAAW,KAAK;AAC1D,EAAE,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC;AAChD,EAAE,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC;AAClD,EAAE,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC;AAClD,EAAE,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC;AAClD,EAAE,WAAW,CAAC,WAAW,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC,GAAG,YAAY,CAAC;AAC7D,EAAE,WAAW,CAAC,WAAW,CAAC,gBAAgB,CAAC,GAAG,EAAE,CAAC,GAAG,gBAAgB,CAAC;AACrE,EAAE,WAAW,CAAC,WAAW,CAAC,iBAAiB,CAAC,GAAG,EAAE,CAAC,GAAG,iBAAiB,CAAC;AACvE,EAAE,WAAW,CAAC,WAAW,CAAC,gBAAgB,CAAC,GAAG,GAAG,CAAC,GAAG,gBAAgB,CAAC;AACtE,EAAE,WAAW,CAAC,WAAW,CAAC,kBAAkB,CAAC,GAAG,GAAG,CAAC,GAAG,kBAAkB,CAAC;AAC1E,EAAE,WAAW,CAAC,WAAW,CAAC,YAAY,CAAC,GAAG,GAAG,CAAC,GAAG,YAAY,CAAC;AAC9D,EAAE,WAAW,CAAC,WAAW,CAAC,eAAe,CAAC,GAAG,IAAI,CAAC,GAAG,eAAe,CAAC;AACrE,EA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