UNPKG

@opensig/opendesign

Version:

65 lines (64 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const is = require("../_utils/is.js"); const vueUtils = require("../_utils/vue-utils.js"); const useRunOnceNextTick = require("./useRunOnceNextTick.js"); const useSortedTeleportChildren = (vm, childType) => { const childMap = {}; const children = vue.shallowRef([]); const parentVms = /* @__PURE__ */ new WeakSet(); const runOnceNextTick = useRunOnceNextTick.useRunOnceNextTick(); const sortChildren = () => { const newSortedChildren = []; vueUtils.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 (!is.isArrayEqual(children.value, newSortedChildren, true)) { children.value = newSortedChildren; } }; const OTeleportWrapper = vue.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); vue.nextTick(() => { delete childMap[uid]; }); } }; const addChild = (child) => { const childVm = vue.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); } vue.onBeforeUnmount(() => { removeChild(child.uid); }); childMap[child.uid] = child; runOnceNextTick(sortChildren); }; return { children, childMap, addChild, OTeleportWrapper }; }; exports.useSortedTeleportChildren = useSortedTeleportChildren;