-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathrenderHelper.js
56 lines (51 loc) · 1.6 KB
/
renderHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { ComponentStructure } from "./componentStructure";
import { isHtmlTag, isTransition } from "../util/tags";
import { resolveComponent, TransitionGroup } from "vue";
function getSlot(slots, key) {
const slotValue = slots[key];
return slotValue ? slotValue() : [];
}
function computeNodes({ $slots, realList, getKey }) {
const normalizedList = realList || [];
const [header, footer] = ["header", "footer"].map(name =>
getSlot($slots, name)
);
const { item } = $slots;
if (!item) {
throw new Error("draggable element must have an item slot");
}
const defaultNodes = normalizedList.flatMap((element, index) =>
item({ element, index }).filter((node) => String(node.type) !== "Symbol(Comment)").map(node => {
node.key = getKey(element);
node.props = { ...(node.props || {}), "data-draggable": true };
return node;
})
);
if (defaultNodes.length !== normalizedList.length) {
throw new Error("Item slot must have only one child");
}
return {
header,
footer,
default: defaultNodes
};
}
function getRootInformation(tag) {
const transition = isTransition(tag);
const externalComponent = !isHtmlTag(tag) && !transition;
return {
transition,
externalComponent,
tag: externalComponent
? resolveComponent(tag)
: transition
? TransitionGroup
: tag
};
}
function computeComponentStructure({ $slots, tag, realList, getKey }) {
const nodes = computeNodes({ $slots, realList, getKey });
const root = getRootInformation(tag);
return new ComponentStructure({ nodes, root, realList });
}
export { computeComponentStructure };