@varlet/use 2.8.6 → 2.8.7-alpha.1678160786574
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -1
- package/lib/useChildren.d.ts +11 -0
- package/lib/useChildren.js +52 -0
- package/lib/useParent.d.ts +12 -0
- package/lib/useParent.js +46 -0
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ComponentInternalInstance, type ComputedRef } from 'vue';
|
|
2
|
+
export interface UseChildrenBaseProvider<C> {
|
|
3
|
+
childInstances: ComponentInternalInstance[];
|
|
4
|
+
collect(instance: ComponentInternalInstance, childProvider: C): void;
|
|
5
|
+
clear(instance: ComponentInternalInstance, childProvider: C): void;
|
|
6
|
+
}
|
|
7
|
+
export declare function useChildren<P, C>(key: symbol): {
|
|
8
|
+
length: ComputedRef<number>;
|
|
9
|
+
childProviders: C[];
|
|
10
|
+
bindChildren: (parentProvider: P) => void;
|
|
11
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { removeItem } from '@varlet/shared';
|
|
2
|
+
import { getCurrentInstance, computed, provide, reactive, isVNode, } from 'vue';
|
|
3
|
+
function flatVNodes(subTree) {
|
|
4
|
+
const vNodes = [];
|
|
5
|
+
const flat = (subTree) => {
|
|
6
|
+
if (subTree === null || subTree === void 0 ? void 0 : subTree.component) {
|
|
7
|
+
flat(subTree === null || subTree === void 0 ? void 0 : subTree.component.subTree);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (Array.isArray(subTree === null || subTree === void 0 ? void 0 : subTree.children)) {
|
|
11
|
+
subTree.children.forEach((child) => {
|
|
12
|
+
if (isVNode(child)) {
|
|
13
|
+
vNodes.push(child);
|
|
14
|
+
flat(child);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
flat(subTree);
|
|
20
|
+
return vNodes;
|
|
21
|
+
}
|
|
22
|
+
export function useChildren(key) {
|
|
23
|
+
const parentInstance = getCurrentInstance();
|
|
24
|
+
const childInstances = reactive([]);
|
|
25
|
+
const childProviders = [];
|
|
26
|
+
const length = computed(() => childInstances.length);
|
|
27
|
+
const sortInstances = () => {
|
|
28
|
+
const vNodes = flatVNodes(parentInstance.subTree);
|
|
29
|
+
childInstances.sort((a, b) => {
|
|
30
|
+
return vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode);
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
const collect = (childInstance, childProvider) => {
|
|
34
|
+
childInstances.push(childInstance);
|
|
35
|
+
childProviders.push(childProvider);
|
|
36
|
+
sortInstances();
|
|
37
|
+
};
|
|
38
|
+
const clear = (childInstance, childProvider) => {
|
|
39
|
+
removeItem(childInstances, childInstance);
|
|
40
|
+
removeItem(childProviders, childProvider);
|
|
41
|
+
};
|
|
42
|
+
const bindChildren = (parentProvider) => {
|
|
43
|
+
provide(key, Object.assign({ childInstances,
|
|
44
|
+
collect,
|
|
45
|
+
clear }, parentProvider));
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
length,
|
|
49
|
+
childProviders,
|
|
50
|
+
bindChildren,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ComputedRef } from 'vue';
|
|
2
|
+
import { type UseChildrenBaseProvider } from './useChildren.js';
|
|
3
|
+
export declare function keyInProvides(key: symbol): boolean;
|
|
4
|
+
export declare function useParent<P, C>(key: symbol): {
|
|
5
|
+
index: null;
|
|
6
|
+
parentProvider: null;
|
|
7
|
+
bindParent: null;
|
|
8
|
+
} | {
|
|
9
|
+
index: ComputedRef<number>;
|
|
10
|
+
parentProvider: Omit<P & UseChildrenBaseProvider<C>, "childInstances" | "collect" | "clear">;
|
|
11
|
+
bindParent: (childProvider: C) => void;
|
|
12
|
+
};
|
package/lib/useParent.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { getCurrentInstance, inject, onMounted, onBeforeUnmount, nextTick, computed, } from 'vue';
|
|
13
|
+
export function keyInProvides(key) {
|
|
14
|
+
const instance = getCurrentInstance();
|
|
15
|
+
return key in instance.provides;
|
|
16
|
+
}
|
|
17
|
+
export function useParent(key) {
|
|
18
|
+
if (!keyInProvides(key)) {
|
|
19
|
+
return {
|
|
20
|
+
index: null,
|
|
21
|
+
parentProvider: null,
|
|
22
|
+
bindParent: null,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const provider = inject(key);
|
|
26
|
+
const { childInstances, collect, clear } = provider, parentProvider = __rest(provider, ["childInstances", "collect", "clear"]);
|
|
27
|
+
const childInstance = getCurrentInstance();
|
|
28
|
+
const index = computed(() => childInstances.indexOf(childInstance));
|
|
29
|
+
const bindParent = (childProvider) => {
|
|
30
|
+
onMounted(() => {
|
|
31
|
+
nextTick().then(() => {
|
|
32
|
+
collect(childInstance, childProvider);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
onBeforeUnmount(() => {
|
|
36
|
+
nextTick().then(() => {
|
|
37
|
+
clear(childInstance, childProvider);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
index,
|
|
43
|
+
parentProvider,
|
|
44
|
+
bindParent,
|
|
45
|
+
};
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@varlet/use",
|
|
3
|
-
"version": "2.8.
|
|
3
|
+
"version": "2.8.7-alpha.1678160786574",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"url": "https://github.com/varletjs/varlet/issues"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@varlet/shared": "2.8.
|
|
27
|
+
"@varlet/shared": "2.8.7-alpha.1678160786574"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"typescript": "^4.4.4",
|