UNPKG

@opensig/opendesign

Version:

65 lines (64 loc) 2.08 kB
import { shallowRef, defineComponent, getCurrentInstance, onBeforeUnmount, nextTick } from "vue"; import { isArrayEqual } from "../_utils/is.mjs"; import { flatComponentVNode } from "../_utils/vue-utils.mjs"; import { useRunOnceNextTick } from "./useRunOnceNextTick.mjs"; const useSortedTeleportChildren = (vm, childType) => { const childMap = {}; const children = shallowRef([]); const parentVms = /* @__PURE__ */ new WeakSet(); const runOnceNextTick = useRunOnceNextTick(); const sortChildren = () => { const newSortedChildren = []; flatComponentVNode(vm.subTree, childType).forEach((child) => { var _a; if (((_a = child.component) == null ? void 0 : _a.uid) && childMap[child.component.uid]) { newSortedChildren.push(childMap[child.component.uid]); } }); if (!isArrayEqual(children.value, newSortedChildren, true)) { children.value = newSortedChildren; } }; const OTeleportWrapper = defineComponent({ name: "OTeleportWrapper", setup(_, { slots }) { return () => { var _a; runOnceNextTick(sortChildren); return (_a = slots.default) == null ? void 0 : _a.call(slots); }; } }); const removeChild = (uid) => { const child = childMap[uid]; if (child) { runOnceNextTick(sortChildren); nextTick(() => { delete childMap[uid]; }); } }; const addChild = (child) => { const childVm = getCurrentInstance(); const parentVm = childVm.parent; if (!parentVms.has(parentVm) && parentVm.type !== OTeleportWrapper) { const originRender = parentVm.render; if (originRender) { parentVm.render = function(...args) { runOnceNextTick(sortChildren); return originRender.apply(this, args); }; } parentVms.add(parentVm); } onBeforeUnmount(() => { removeChild(child.uid); }); childMap[child.uid] = child; runOnceNextTick(sortChildren); }; return { children, childMap, addChild, OTeleportWrapper }; }; export { useSortedTeleportChildren };