index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
  2. require("../../_virtual/_rolldown/runtime.js");
  3. const require_vnode = require("../../utils/vue/vnode.js");
  4. let vue = require("vue");
  5. //#region ../../packages/hooks/use-ordered-children/index.ts
  6. const getOrderedChildren = (vm, childComponentName, children) => {
  7. return require_vnode.flattedChildren(vm.subTree).filter((n) => (0, vue.isVNode)(n) && n.type?.name === childComponentName && !!n.component).map((n) => n.component.uid).map((uid) => children[uid]).filter((p) => !!p);
  8. };
  9. const useOrderedChildren = (vm, childComponentName) => {
  10. const children = (0, vue.shallowRef)({});
  11. const orderedChildren = (0, vue.shallowRef)([]);
  12. const nodesMap = /* @__PURE__ */ new WeakMap();
  13. const addChild = (child) => {
  14. children.value[child.uid] = child;
  15. (0, vue.triggerRef)(children);
  16. (0, vue.onMounted)(() => {
  17. const childNode = child.getVnode().el;
  18. const parentNode = childNode.parentNode;
  19. if (!nodesMap.has(parentNode)) {
  20. nodesMap.set(parentNode, []);
  21. const originalFn = parentNode.insertBefore.bind(parentNode);
  22. parentNode.insertBefore = (node, anchor) => {
  23. if (nodesMap.get(parentNode).some((el) => node === el || anchor === el)) (0, vue.triggerRef)(children);
  24. return originalFn(node, anchor);
  25. };
  26. }
  27. nodesMap.get(parentNode).push(childNode);
  28. });
  29. };
  30. const removeChild = (child) => {
  31. delete children.value[child.uid];
  32. (0, vue.triggerRef)(children);
  33. const childNode = child.getVnode().el;
  34. const parentNode = childNode.parentNode;
  35. const childNodes = nodesMap.get(parentNode);
  36. const index = childNodes.indexOf(childNode);
  37. childNodes.splice(index, 1);
  38. };
  39. const sortChildren = () => {
  40. orderedChildren.value = getOrderedChildren(vm, childComponentName, children.value);
  41. };
  42. const IsolatedRenderer = (props) => {
  43. return props.render();
  44. };
  45. return {
  46. children: orderedChildren,
  47. addChild,
  48. removeChild,
  49. ChildrenSorter: (0, vue.defineComponent)({ setup(_, { slots }) {
  50. return () => {
  51. sortChildren();
  52. return slots.default ? (0, vue.h)(IsolatedRenderer, { render: slots.default }) : null;
  53. };
  54. } })
  55. };
  56. };
  57. //#endregion
  58. exports.useOrderedChildren = useOrderedChildren;
  59. //# sourceMappingURL=index.js.map