Skip to content

Commit 2473dc5

Browse files
committed
chore: refactor
1 parent 79f04f5 commit 2473dc5

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

src/directives/on.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,46 @@ const modifierGuards: Record<
3030
}
3131

3232
export const on: Directive = ({ el, get, exp, arg, modifiers }) => {
33-
if (arg) {
34-
let handler = simplePathRE.test(exp)
35-
? get(`(e => ${exp}(e))`)
36-
: get(`($event => { ${exp} })`)
33+
if (!arg) {
34+
if (import.meta.env.DEV) {
35+
console.error(`v-on="obj" syntax is not supported in petite-vue.`)
36+
}
37+
return
38+
}
39+
40+
let handler = simplePathRE.test(exp)
41+
? get(`(e => ${exp}(e))`)
42+
: get(`($event => { ${exp} })`)
43+
44+
// special lifecycle events
45+
if (arg === 'mounted') {
46+
nextTick(handler)
47+
return
48+
} else if (arg === 'unmounted') {
49+
return () => handler()
50+
}
3751

38-
// special lifecycle events
39-
if (arg === 'mounted') {
40-
nextTick(handler)
41-
return
42-
} else if (arg === 'unmounted') {
43-
return () => handler()
52+
if (modifiers) {
53+
// map modifiers
54+
if (arg === 'click') {
55+
if (modifiers.right) arg = 'contextmenu'
56+
if (modifiers.middle) arg = 'mouseup'
4457
}
4558

46-
if (modifiers) {
47-
// map modifiers
48-
if (arg === 'click') {
49-
if (modifiers.right) arg = 'contextmenu'
50-
if (modifiers.middle) arg = 'mouseup'
59+
const raw = handler
60+
handler = (e: Event) => {
61+
if ('key' in e && !(hyphenate((e as KeyboardEvent).key) in modifiers)) {
62+
return
5163
}
52-
53-
const raw = handler
54-
handler = (e: Event) => {
55-
if ('key' in e && !(hyphenate((e as KeyboardEvent).key) in modifiers)) {
64+
for (const key in modifiers) {
65+
const guard = modifierGuards[key]
66+
if (guard && guard(e, modifiers)) {
5667
return
5768
}
58-
for (const key in modifiers) {
59-
const guard = modifierGuards[key]
60-
if (guard && guard(e, modifiers)) {
61-
return
62-
}
63-
}
64-
return raw(e)
6569
}
70+
return raw(e)
6671
}
67-
68-
listen(el, arg, handler, modifiers)
69-
} else if (import.meta.env.DEV) {
70-
console.error(`v-on="obj" syntax is not supported in petite-vue.`)
7172
}
73+
74+
listen(el, arg, handler, modifiers)
7275
}

0 commit comments

Comments
 (0)