|
|
@@ -1,70 +1,64 @@
|
|
|
-import {
|
|
|
- createCompilerError,
|
|
|
- createSimpleExpression,
|
|
|
- ErrorCodes,
|
|
|
- ExpressionNode,
|
|
|
- isStaticExp,
|
|
|
- NodeTypes,
|
|
|
-} from '@vue/compiler-core'
|
|
|
+import { createCompilerError, ErrorCodes } from '@vue/compiler-core'
|
|
|
import type { DirectiveTransform } from '../transform'
|
|
|
-import { IRNodeTypes } from '../ir'
|
|
|
+import { IRNodeTypes, KeyOverride } from '../ir'
|
|
|
import { resolveModifiers } from '@vue/compiler-dom'
|
|
|
|
|
|
export const transformVOn: DirectiveTransform = (dir, node, context) => {
|
|
|
- const { arg, exp, loc, modifiers } = dir
|
|
|
+ let { arg, exp, loc, modifiers } = dir
|
|
|
if (!exp && !modifiers.length) {
|
|
|
context.options.onError(
|
|
|
createCompilerError(ErrorCodes.X_V_ON_NO_EXPRESSION, loc),
|
|
|
)
|
|
|
- return
|
|
|
}
|
|
|
|
|
|
if (!arg) {
|
|
|
// TODO support v-on="{}"
|
|
|
return
|
|
|
- } else if (exp === undefined) {
|
|
|
- // TODO X_V_ON_NO_EXPRESSION error
|
|
|
- return
|
|
|
}
|
|
|
|
|
|
- const handlerKey = `on${arg.content}`
|
|
|
const { keyModifiers, nonKeyModifiers, eventOptionModifiers } =
|
|
|
- resolveModifiers(handlerKey, modifiers, null, loc)
|
|
|
+ resolveModifiers(
|
|
|
+ arg.isStatic ? `on${arg.content}` : arg,
|
|
|
+ modifiers,
|
|
|
+ null,
|
|
|
+ loc,
|
|
|
+ )
|
|
|
+
|
|
|
+ let keyOverride: KeyOverride | undefined
|
|
|
|
|
|
// normalize click.right and click.middle since they don't actually fire
|
|
|
- let name = arg.content
|
|
|
+
|
|
|
+ const isStaticClick = arg.isStatic && arg.content.toLowerCase() === 'click'
|
|
|
+
|
|
|
if (nonKeyModifiers.includes('right')) {
|
|
|
- name = transformClick(arg, 'contextmenu')
|
|
|
+ if (isStaticClick) {
|
|
|
+ arg = { ...arg, content: 'contextmenu' }
|
|
|
+ } else if (!arg.isStatic) {
|
|
|
+ keyOverride = ['click', 'contextmenu']
|
|
|
+ }
|
|
|
}
|
|
|
if (nonKeyModifiers.includes('middle')) {
|
|
|
- name = transformClick(arg, 'mouseup')
|
|
|
+ if (keyOverride) {
|
|
|
+ // TODO error here
|
|
|
+ }
|
|
|
+ if (isStaticClick) {
|
|
|
+ arg = { ...arg, content: 'mouseup' }
|
|
|
+ } else if (!arg.isStatic) {
|
|
|
+ keyOverride = ['click', 'mouseup']
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // TODO reactive
|
|
|
context.registerOperation({
|
|
|
type: IRNodeTypes.SET_EVENT,
|
|
|
loc,
|
|
|
element: context.reference(),
|
|
|
- key: createSimpleExpression(name, true, arg.loc),
|
|
|
+ key: arg,
|
|
|
value: exp,
|
|
|
modifiers: {
|
|
|
keys: keyModifiers,
|
|
|
nonKeys: nonKeyModifiers,
|
|
|
options: eventOptionModifiers,
|
|
|
},
|
|
|
+ keyOverride,
|
|
|
})
|
|
|
}
|
|
|
-
|
|
|
-function transformClick(key: ExpressionNode, event: string) {
|
|
|
- const isStaticClick =
|
|
|
- isStaticExp(key) && key.content.toLowerCase() === 'click'
|
|
|
-
|
|
|
- if (isStaticClick) {
|
|
|
- return event
|
|
|
- } else if (key.type !== NodeTypes.SIMPLE_EXPRESSION) {
|
|
|
- // TODO: handle CompoundExpression
|
|
|
- return 'TODO'
|
|
|
- } else {
|
|
|
- return key.content.toLowerCase()
|
|
|
- }
|
|
|
-}
|