limit mouse event modifiers to mouse events

This commit is contained in:
Evan You 2016-11-21 17:34:03 -05:00
parent bb6089c6e1
commit b45b974a5c
2 changed files with 23 additions and 12 deletions

View File

@ -19,7 +19,11 @@ const keyCodes = {
const modifierCode = { const modifierCode = {
stop: '$event.stopPropagation();', stop: '$event.stopPropagation();',
prevent: '$event.preventDefault();', prevent: '$event.preventDefault();',
self: 'if($event.target !== $event.currentTarget)return;', self: 'if($event.target !== $event.currentTarget)return;'
}
const isMouseEventRE = /^mouse|^pointer|^(click|dblclick|contextmenu|wheel)$/
const mouseEventModifierCode = {
ctrl: 'if(!$event.ctrlKey)return;', ctrl: 'if(!$event.ctrlKey)return;',
shift: 'if(!$event.shiftKey)return;', shift: 'if(!$event.shiftKey)return;',
alt: 'if(!$event.altKey)return;', alt: 'if(!$event.altKey)return;',
@ -29,18 +33,19 @@ const modifierCode = {
export function genHandlers (events: ASTElementHandlers, native?: boolean): string { export function genHandlers (events: ASTElementHandlers, native?: boolean): string {
let res = native ? 'nativeOn:{' : 'on:{' let res = native ? 'nativeOn:{' : 'on:{'
for (const name in events) { for (const name in events) {
res += `"${name}":${genHandler(events[name])},` res += `"${name}":${genHandler(name, events[name])},`
} }
return res.slice(0, -1) + '}' return res.slice(0, -1) + '}'
} }
function genHandler ( function genHandler (
name: string,
handler: ASTElementHandler | Array<ASTElementHandler> handler: ASTElementHandler | Array<ASTElementHandler>
): string { ): string {
if (!handler) { if (!handler) {
return 'function(){}' return 'function(){}'
} else if (Array.isArray(handler)) { } else if (Array.isArray(handler)) {
return `[${handler.map(genHandler).join(',')}]` return `[${handler.map(handler => genHandler(name, handler)).join(',')}]`
} else if (!handler.modifiers) { } else if (!handler.modifiers) {
return fnExpRE.test(handler.value) || simplePathRE.test(handler.value) return fnExpRE.test(handler.value) || simplePathRE.test(handler.value)
? handler.value ? handler.value
@ -48,9 +53,12 @@ function genHandler (
} else { } else {
let code = '' let code = ''
const keys = [] const keys = []
const isMouseEvnet = isMouseEventRE.test(name)
for (const key in handler.modifiers) { for (const key in handler.modifiers) {
if (modifierCode[key]) { if (modifierCode[key]) {
code += modifierCode[key] code += modifierCode[key]
} else if (isMouseEvnet && mouseEventModifierCode[key]) {
code += mouseEventModifierCode[key]
} else { } else {
keys.push(key) keys.push(key)
} }

View File

@ -248,7 +248,7 @@ describe('codegen', () => {
) )
}) })
it('generate events with modifiers', () => { it('generate events with generic modifiers', () => {
assertCodegen( assertCodegen(
'<input @input.stop="onInput">', '<input @input.stop="onInput">',
`with(this){return _h('input',{on:{"input":function($event){$event.stopPropagation();onInput($event)}}})}` `with(this){return _h('input',{on:{"input":function($event){$event.stopPropagation();onInput($event)}}})}`
@ -261,21 +261,24 @@ describe('codegen', () => {
'<input @input.self="onInput">', '<input @input.self="onInput">',
`with(this){return _h('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return;onInput($event)}}})}` `with(this){return _h('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return;onInput($event)}}})}`
) )
})
it('generate events with mouse event modifiers', () => {
assertCodegen( assertCodegen(
'<input @input.ctrl="onInput">', '<input @click.ctrl="onClick">',
`with(this){return _h('input',{on:{"input":function($event){if(!$event.ctrlKey)return;onInput($event)}}})}` `with(this){return _h('input',{on:{"click":function($event){if(!$event.ctrlKey)return;onClick($event)}}})}`
) )
assertCodegen( assertCodegen(
'<input @input.shift="onInput">', '<input @click.shift="onClick">',
`with(this){return _h('input',{on:{"input":function($event){if(!$event.shiftKey)return;onInput($event)}}})}` `with(this){return _h('input',{on:{"click":function($event){if(!$event.shiftKey)return;onClick($event)}}})}`
) )
assertCodegen( assertCodegen(
'<input @input.alt="onInput">', '<input @click.alt="onClick">',
`with(this){return _h('input',{on:{"input":function($event){if(!$event.altKey)return;onInput($event)}}})}` `with(this){return _h('input',{on:{"click":function($event){if(!$event.altKey)return;onClick($event)}}})}`
) )
assertCodegen( assertCodegen(
'<input @input.meta="onInput">', '<input @click.meta="onClick">',
`with(this){return _h('input',{on:{"input":function($event){if(!$event.metaKey)return;onInput($event)}}})}` `with(this){return _h('input',{on:{"click":function($event){if(!$event.metaKey)return;onClick($event)}}})}`
) )
}) })