-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
directives.ts
188 lines (157 loc) · 5.48 KB
/
directives.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import type { ResolvedClicksInfo } from '@slidev/types'
import type { App, DirectiveBinding, InjectionKey } from 'vue'
import { computed, watchEffect } from 'vue'
import {
CLASS_VCLICK_CURRENT,
CLASS_VCLICK_FADE,
CLASS_VCLICK_HIDDEN,
CLASS_VCLICK_HIDDEN_EXP,
CLASS_VCLICK_PRIOR,
CLASS_VCLICK_TARGET,
injectionClicksContext,
} from '../constants'
import { safeParseNumber } from '../logic/utils'
function dirInject<T = unknown>(dir: DirectiveBinding<any>, key: InjectionKey<T> | string, defaultValue?: T): T | undefined {
return (dir.instance?.$ as any).provides[key as any] ?? defaultValue
}
export default function createDirectives() {
return {
install(app: App) {
app.directive('click', {
// @ts-expect-error extra prop
name: 'v-click',
mounted(el: HTMLElement, dir) {
const resolved = resolveClick(el, dir)
if (resolved == null)
return
el.classList.toggle(CLASS_VCLICK_TARGET, true)
watchEffect(() => {
const active = resolved.isActive.value
const current = resolved.isCurrent.value
const prior = active && !current
if (resolved.clickHide) {
el.classList.toggle(resolved.CLASS_HIDE, active)
el.classList.toggle(CLASS_VCLICK_HIDDEN_EXP, active)
}
else {
el.classList.toggle(resolved.CLASS_HIDE, !active)
}
el.classList.toggle(CLASS_VCLICK_CURRENT, current)
el.classList.toggle(CLASS_VCLICK_PRIOR, prior)
})
},
unmounted,
})
app.directive('after', {
// @ts-expect-error extra prop
name: 'v-after',
mounted(el: HTMLElement, dir) {
const resolved = resolveClick(el, dir, true)
if (resolved == null)
return
el.classList.toggle(CLASS_VCLICK_TARGET, true)
watchEffect(() => {
const active = resolved.isActive.value
const current = resolved.isCurrent.value
const prior = active && !current
if (resolved.clickHide) {
el.classList.toggle(resolved.CLASS_HIDE, active)
el.classList.toggle(CLASS_VCLICK_HIDDEN_EXP, active)
}
else {
el.classList.toggle(resolved.CLASS_HIDE, !active)
}
el.classList.toggle(CLASS_VCLICK_CURRENT, current)
el.classList.toggle(CLASS_VCLICK_PRIOR, prior)
})
},
unmounted,
})
app.directive('click-hide', {
// @ts-expect-error extra prop
name: 'v-click-hide',
mounted(el: HTMLElement, dir) {
const resolved = resolveClick(el, dir, false, true)
if (resolved == null)
return
el.classList.toggle(CLASS_VCLICK_TARGET, true)
watchEffect(() => {
const active = resolved.isActive.value
const current = resolved.isCurrent.value
const prior = active && !current
el.classList.toggle(resolved.CLASS_HIDE, active)
el.classList.toggle(CLASS_VCLICK_HIDDEN_EXP, active)
el.classList.toggle(CLASS_VCLICK_CURRENT, current)
el.classList.toggle(CLASS_VCLICK_PRIOR, prior)
})
},
unmounted,
})
},
}
}
function isActive(thisClick: number | [number, number], clicks: number) {
return Array.isArray(thisClick)
? thisClick[0] <= clicks && clicks < thisClick[1]
: thisClick <= clicks
}
function isCurrent(thisClick: number | [number, number], clicks: number) {
return Array.isArray(thisClick)
? thisClick[0] === clicks
: thisClick === clicks
}
type ClickInfo = null | (Required<ResolvedClicksInfo> & {
clickHide: boolean
CLASS_HIDE: string
})
function resolveClick(el: Element, dir: DirectiveBinding<any>, clickAfter = false, clickHide = false): ClickInfo {
const ctx = dirInject(dir, injectionClicksContext)?.value
if (!el || !ctx || ctx.disabled)
return null
let value = dir.value
if (value === false || value === 'false')
return null
clickHide ||= dir.modifiers.hide !== false && dir.modifiers.hide != null
const fade = dir.modifiers.fade !== false && dir.modifiers.fade != null
if (clickAfter)
value = '+0'
else if (value == null || value === true || value === 'true')
value = '+1'
let relativeDelta: number
let thisClick: number | [number, number]
let maxClick: number
if (typeof value === 'string' && '+-'.includes(value[0])) {
// flow
relativeDelta = safeParseNumber(value)
thisClick = ctx.currentOffset + relativeDelta
maxClick = thisClick
}
else if (Array.isArray(value)) {
// range (absolute)
relativeDelta = 0
thisClick = value as [number, number]
maxClick = value[1]
}
else {
// since (absolute)
relativeDelta = 0
thisClick = safeParseNumber(value)
maxClick = thisClick
}
const resolved = {
max: maxClick,
relativeDelta,
isActive: computed(() => isActive(thisClick, ctx.current)),
isCurrent: computed(() => isCurrent(thisClick, ctx.current)),
isShown: computed(() => clickHide ? !isActive(thisClick, ctx.current) : isActive(thisClick, ctx.current)),
clickHide,
CLASS_HIDE: fade ? CLASS_VCLICK_FADE : CLASS_VCLICK_HIDDEN,
}
ctx.register(el, resolved)
return resolved
}
function unmounted(el: HTMLElement, dir: DirectiveBinding<any>) {
el.classList.toggle(CLASS_VCLICK_TARGET, false)
const ctx = dirInject(dir, injectionClicksContext)?.value
ctx?.unregister(el)
}