|
| 1 | +import { |
| 2 | + ErrorCodes, |
| 3 | + createCompilerError, |
| 4 | + resolveModifiers, |
| 5 | +} from '@vue/compiler-dom' |
| 6 | +import { extend, makeMap } from '@vue/shared' |
| 7 | +import { IRNodeTypes, type KeyOverride, type SetEventIRNode } from '../ir' |
| 8 | +import { |
| 9 | + isComponentNode, |
| 10 | + resolveExpression, |
| 11 | + resolveLocation, |
| 12 | + resolveSimpleExpression, |
| 13 | +} from '../utils' |
| 14 | +import { EMPTY_EXPRESSION } from './utils' |
| 15 | +import type { DirectiveTransform } from '../transform' |
| 16 | + |
| 17 | +const delegatedEvents = /*#__PURE__*/ makeMap( |
| 18 | + 'beforeinput,click,dblclick,contextmenu,focusin,focusout,input,keydown,' + |
| 19 | + 'keyup,mousedown,mousemove,mouseout,mouseover,mouseup,pointerdown,' + |
| 20 | + 'pointermove,pointerout,pointerover,pointerup,touchend,touchmove,' + |
| 21 | + 'touchstart', |
| 22 | +) |
| 23 | + |
| 24 | +export const transformVOn: DirectiveTransform = (dir, node, context) => { |
| 25 | + const { name, loc, value } = dir |
| 26 | + if (name.type === 'JSXNamespacedName') return |
| 27 | + const isComponent = isComponentNode(node) |
| 28 | + |
| 29 | + const [nameString, ...modifiers] = name.name |
| 30 | + .replace(/^on([A-Z])/, (_, $1) => $1.toLowerCase()) |
| 31 | + .split('_') |
| 32 | + |
| 33 | + if (!value && !modifiers.length) { |
| 34 | + context.options.onError( |
| 35 | + createCompilerError( |
| 36 | + ErrorCodes.X_V_ON_NO_EXPRESSION, |
| 37 | + resolveLocation(loc, context), |
| 38 | + ), |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + let arg = resolveSimpleExpression(nameString, true, dir.name.loc) |
| 43 | + const exp = resolveExpression(dir.value, context) |
| 44 | + |
| 45 | + const { keyModifiers, nonKeyModifiers, eventOptionModifiers } = |
| 46 | + resolveModifiers( |
| 47 | + arg.isStatic ? `on${nameString}` : arg, |
| 48 | + modifiers, |
| 49 | + null, |
| 50 | + resolveLocation(loc, context), |
| 51 | + ) |
| 52 | + |
| 53 | + let keyOverride: KeyOverride | undefined |
| 54 | + const isStaticClick = arg.isStatic && arg.content.toLowerCase() === 'click' |
| 55 | + const delegate = |
| 56 | + arg.isStatic && !eventOptionModifiers.length && delegatedEvents(arg.content) |
| 57 | + |
| 58 | + // normalize click.right and click.middle since they don't actually fire |
| 59 | + if (nonKeyModifiers.includes('middle')) { |
| 60 | + if (keyOverride) { |
| 61 | + // TODO error here |
| 62 | + } |
| 63 | + if (isStaticClick) { |
| 64 | + arg = extend({}, arg, { content: 'mouseup' }) |
| 65 | + } else if (!arg.isStatic) { |
| 66 | + keyOverride = ['click', 'mouseup'] |
| 67 | + } |
| 68 | + } |
| 69 | + if (nonKeyModifiers.includes('right')) { |
| 70 | + if (isStaticClick) { |
| 71 | + arg = extend({}, arg, { content: 'contextmenu' }) |
| 72 | + } else if (!arg.isStatic) { |
| 73 | + keyOverride = ['click', 'contextmenu'] |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (isComponent) { |
| 78 | + const handler = exp || EMPTY_EXPRESSION |
| 79 | + return { |
| 80 | + key: arg, |
| 81 | + value: handler, |
| 82 | + handler: true, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + const operation: SetEventIRNode = { |
| 87 | + type: IRNodeTypes.SET_EVENT, |
| 88 | + element: context.reference(), |
| 89 | + key: arg, |
| 90 | + value: exp, |
| 91 | + modifiers: { |
| 92 | + keys: keyModifiers, |
| 93 | + nonKeys: nonKeyModifiers, |
| 94 | + options: eventOptionModifiers, |
| 95 | + }, |
| 96 | + keyOverride, |
| 97 | + delegate, |
| 98 | + effect: !arg.isStatic, |
| 99 | + } |
| 100 | + |
| 101 | + context.registerEffect([arg], operation) |
| 102 | +} |
0 commit comments