Browse Source

allow modifier key modifiers on keyboard events and warn overwriting in config.keyCodes

dev
Evan You 8 years ago
parent
commit
e8d6bd9dc7
  1. 2
      flow/component.js
  2. 12
      src/compiler/codegen/events.js
  3. 14
      src/core/instance/proxy.js
  4. 4
      src/core/instance/render.js

2
flow/component.js

@ -105,7 +105,7 @@ declare interface Component {
// apply v-bind object
_b: (data: any, value: any, asProp?: boolean) => VNodeData;
// check custom keyCode
_k: (eventKeyCode: number, key: string, buildinAlias: number | Array<number> | void) => boolean;
_k: (eventKeyCode: number, key: string, builtInAlias: number | Array<number> | void) => boolean;
// allow dynamic method registration
[key: string]: any

12
src/compiler/codegen/events.js

@ -19,11 +19,7 @@ const keyCodes = {
const modifierCode = {
stop: '$event.stopPropagation();',
prevent: '$event.preventDefault();',
self: 'if($event.target !== $event.currentTarget)return;'
}
const isMouseEventRE = /^mouse|^pointer|^(click|dblclick|contextmenu|wheel)$/
const mouseEventModifierCode = {
self: 'if($event.target !== $event.currentTarget)return;',
ctrl: 'if(!$event.ctrlKey)return;',
shift: 'if(!$event.shiftKey)return;',
alt: 'if(!$event.altKey)return;',
@ -53,12 +49,9 @@ function genHandler (
} else {
let code = ''
const keys = []
const isMouseEvnet = isMouseEventRE.test(name)
for (const key in handler.modifiers) {
if (modifierCode[key]) {
code += modifierCode[key]
} else if (isMouseEvnet && mouseEventModifierCode[key]) {
code += mouseEventModifierCode[key]
} else {
keys.push(key)
}
@ -74,8 +67,7 @@ function genHandler (
}
function genKeyFilter (keys: Array<string>): string {
const code = keys.map(genFilterCode)
return `if(${code.join('&&')})return;`
return `if(${keys.map(genFilterCode).join('&&')})return;`
}
function genFilterCode (key: number | string): string {

14
src/core/instance/proxy.js

@ -1,5 +1,6 @@
/* not type checking this file because flow doesn't play well with Proxy */
import config from 'core/config'
import { warn, makeMap } from '../util/index'
let initProxy
@ -25,6 +26,19 @@ if (process.env.NODE_ENV !== 'production') {
typeof Proxy !== 'undefined' &&
Proxy.toString().match(/native code/)
if (hasProxy) {
const isBuiltInModifier = makeMap('stop,prevent,self,ctrl,shift,alt,meta')
config.keyCodes = new Proxy(config.keyCodes, {
set (target, key, value) {
if (isBuiltInModifier(key)) {
warn(`Avoid overwriting built-in modifier in config.keyCodes: .${key}`)
} else {
target[key] = value
}
}
})
}
const hasHandler = {
has (target, key) {
const has = key in target

4
src/core/instance/render.js

@ -242,9 +242,9 @@ export function renderMixin (Vue: Class<Component>) {
Vue.prototype._k = function checkKeyCodes (
eventKeyCode: number,
key: string,
buildinAlias: number | Array<number> | void
builtInAlias: number | Array<number> | void
): boolean {
const keyCodes = config.keyCodes[key] || buildinAlias
const keyCodes = config.keyCodes[key] || builtInAlias
if (Array.isArray(keyCodes)) {
return keyCodes.indexOf(eventKeyCode) === -1
} else {

Loading…
Cancel
Save