vnode.mjs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { isArray } from "../types.mjs";
  2. import { camelize } from "../strings.mjs";
  3. import { hasOwn } from "../objects.mjs";
  4. import { debugWarn } from "../error.mjs";
  5. import { Comment, Fragment, Text, createBlock, createCommentVNode, isVNode, openBlock } from "vue";
  6. //#region ../../packages/utils/vue/vnode.ts
  7. const SCOPE = "utils/vue/vnode";
  8. let PatchFlags = /* @__PURE__ */ function(PatchFlags) {
  9. PatchFlags[PatchFlags["TEXT"] = 1] = "TEXT";
  10. PatchFlags[PatchFlags["CLASS"] = 2] = "CLASS";
  11. PatchFlags[PatchFlags["STYLE"] = 4] = "STYLE";
  12. PatchFlags[PatchFlags["PROPS"] = 8] = "PROPS";
  13. PatchFlags[PatchFlags["FULL_PROPS"] = 16] = "FULL_PROPS";
  14. PatchFlags[PatchFlags["HYDRATE_EVENTS"] = 32] = "HYDRATE_EVENTS";
  15. PatchFlags[PatchFlags["STABLE_FRAGMENT"] = 64] = "STABLE_FRAGMENT";
  16. PatchFlags[PatchFlags["KEYED_FRAGMENT"] = 128] = "KEYED_FRAGMENT";
  17. PatchFlags[PatchFlags["UNKEYED_FRAGMENT"] = 256] = "UNKEYED_FRAGMENT";
  18. PatchFlags[PatchFlags["NEED_PATCH"] = 512] = "NEED_PATCH";
  19. PatchFlags[PatchFlags["DYNAMIC_SLOTS"] = 1024] = "DYNAMIC_SLOTS";
  20. PatchFlags[PatchFlags["HOISTED"] = -1] = "HOISTED";
  21. PatchFlags[PatchFlags["BAIL"] = -2] = "BAIL";
  22. return PatchFlags;
  23. }({});
  24. function isFragment(node) {
  25. return isVNode(node) && node.type === Fragment;
  26. }
  27. function isText(node) {
  28. return isVNode(node) && node.type === Text;
  29. }
  30. function isComment(node) {
  31. return isVNode(node) && node.type === Comment;
  32. }
  33. const TEMPLATE = "template";
  34. function isTemplate(node) {
  35. return isVNode(node) && node.type === TEMPLATE;
  36. }
  37. function isValidElementNode(node) {
  38. return isVNode(node) && !isFragment(node) && !isComment(node);
  39. }
  40. /**
  41. * get a valid child node (not fragment nor comment)
  42. * @param node {VNode} node to be searched
  43. * @param depth {number} depth to be searched
  44. */
  45. function getChildren(node, depth) {
  46. if (isComment(node)) return;
  47. if (isFragment(node) || isTemplate(node)) return depth > 0 ? getFirstValidNode(node.children, depth - 1) : void 0;
  48. return node;
  49. }
  50. const getFirstValidNode = (nodes, maxDepth = 3) => {
  51. if (isArray(nodes)) return getChildren(nodes[0], maxDepth);
  52. else return getChildren(nodes, maxDepth);
  53. };
  54. function renderIf(condition, ...args) {
  55. return condition ? renderBlock(...args) : createCommentVNode("v-if", true);
  56. }
  57. function renderBlock(...args) {
  58. return openBlock(), createBlock(...args);
  59. }
  60. const getNormalizedProps = (node) => {
  61. if (!isVNode(node)) {
  62. debugWarn(SCOPE, "[getNormalizedProps] must be a VNode");
  63. return {};
  64. }
  65. const raw = node.props || {};
  66. const type = (isVNode(node.type) ? node.type.props : void 0) || {};
  67. const props = {};
  68. Object.keys(type).forEach((key) => {
  69. if (hasOwn(type[key], "default")) props[key] = type[key].default;
  70. });
  71. Object.keys(raw).forEach((key) => {
  72. props[camelize(key)] = raw[key];
  73. });
  74. return props;
  75. };
  76. const flattedChildren = (children) => {
  77. const vNodes = isArray(children) ? children : [children];
  78. const result = [];
  79. vNodes.forEach((child) => {
  80. if (isArray(child)) result.push(...flattedChildren(child));
  81. else if (isVNode(child) && child.component?.subTree) result.push(child, ...flattedChildren(child.component.subTree));
  82. else if (isVNode(child) && isArray(child.children)) result.push(...flattedChildren(child.children));
  83. else if (isVNode(child) && child.shapeFlag === 2) result.push(...flattedChildren(child.type()));
  84. else result.push(child);
  85. });
  86. return result;
  87. };
  88. //#endregion
  89. export { PatchFlags, flattedChildren, getFirstValidNode, getNormalizedProps, isComment, isFragment, isTemplate, isText, isValidElementNode, renderBlock, renderIf };
  90. //# sourceMappingURL=vnode.mjs.map