-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
VClicks.ts
139 lines (120 loc) · 3.76 KB
/
VClicks.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* <v-clicks/> click animations component
*
* Learn more: https://sli.dev/guide/animations.html#click-animations
*/
import { toArray } from '@antfu/utils'
import type { VNode, VNodeArrayChildren } from 'vue'
import { Comment, createVNode, defineComponent, h, isVNode, resolveDirective, withDirectives } from 'vue'
import { normalizeAtProp } from '../logic/clicks'
import VClickGap from './VClickGap.vue'
const listTags = ['ul', 'ol']
export default defineComponent({
props: {
depth: {
type: [Number, String],
default: 1,
},
every: {
type: [Number, String],
default: 1,
},
at: {
type: [Number, String],
default: '+1',
},
hide: {
type: Boolean,
default: false,
},
fade: {
type: Boolean,
default: false,
},
},
render() {
const every = +this.every
const at = normalizeAtProp(this.at)
const isRelative = typeof at === 'string'
const click = resolveDirective('click')!
const applyDirective = (node: VNode, value: number | string) => {
return withDirectives(node, [[
click,
value,
'',
{
hide: this.hide,
fade: this.fade,
},
]])
}
const openAllTopLevelSlots = <T extends VNodeArrayChildren | VNode[]>(children: T): T => {
return children.flatMap((i) => {
if (isVNode(i) && typeof i.type === 'symbol' && Array.isArray(i.children))
return openAllTopLevelSlots(i.children)
else
return [i]
}) as T
}
let defaults = this.$slots.default?.()
if (!defaults)
return
defaults = openAllTopLevelSlots(toArray(defaults))
const mapSubList = (children: VNodeArrayChildren, depth = 1): VNodeArrayChildren => {
const vNodes = openAllTopLevelSlots(children).map((i) => {
if (!isVNode(i))
return i
if (listTags.includes(i.type as string) && Array.isArray(i.children)) {
// eslint-disable-next-line ts/no-use-before-define
const vNodes = mapChildren(i.children, depth + 1)
return h(i, {}, vNodes)
}
return h(i)
})
return vNodes
}
let globalIdx = 1
let execIdx = 0
const mapChildren = (children: VNodeArrayChildren, depth = 1): VNodeArrayChildren => {
const vNodes = openAllTopLevelSlots(children).map((i) => {
if (!isVNode(i) || i.type === Comment)
return i
const thisShowIdx = +at + Math.ceil(globalIdx++ / every) - 1
let vNode
if (depth < +this.depth && Array.isArray(i.children))
vNode = h(i, {}, mapSubList(i.children, depth))
else
vNode = h(i)
const delta = thisShowIdx - execIdx
execIdx = thisShowIdx
return applyDirective(
vNode,
isRelative
? delta >= 0 ? `+${delta}` : `${delta}`
: thisShowIdx,
)
})
return vNodes
}
const lastGap = () => createVNode(VClickGap, {
size: +at + Math.ceil((globalIdx - 1) / every) - 1 - execIdx,
})
// handle ul, ol list
if (defaults.length === 1 && listTags.includes(defaults[0].type as string) && Array.isArray(defaults[0].children))
return h(defaults[0], {}, [mapChildren(defaults[0].children), lastGap()])
if (defaults.length === 1 && defaults[0].type as string === 'table') {
const tableNode = defaults[0]
if (Array.isArray(tableNode.children)) {
return h(tableNode, {}, tableNode.children.map((i) => {
if (!isVNode(i))
return i
else if (i.type === 'tbody' && Array.isArray(i.children))
return h(i, {}, [mapChildren(i.children), lastGap()])
else
return h(i)
}))
}
}
return [mapChildren(defaults)[0], lastGap()]
},
})