vnode.js 3.9 KB

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