Skip to content

Commit f7311c9

Browse files
committed
refactor: move keyName alias map to compiler
1 parent 1c8e2e8 commit f7311c9

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

src/compiler/codegen/events.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function\s*\(/
44
const simplePathRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*$/
55

6-
// keyCode aliases
6+
// KeyboardEvent.keyCode aliases
77
const keyCodes: { [key: string]: number | Array<number> } = {
88
esc: 27,
99
tab: 9,
@@ -16,6 +16,19 @@ const keyCodes: { [key: string]: number | Array<number> } = {
1616
'delete': [8, 46]
1717
}
1818

19+
// KeyboardEvent.key aliases
20+
const keyNames: { [key: string]: string | Array<string> } = {
21+
esc: 'Escape',
22+
tab: 'Tab',
23+
enter: 'Enter',
24+
space: ' ',
25+
up: 'ArrowUp',
26+
left: 'ArrowLeft',
27+
right: 'ArrowRight',
28+
down: 'ArrowDown',
29+
'delete': ['Backspace', 'Delete']
30+
}
31+
1932
// #4868: modifiers that prevent the execution of the listener
2033
// need to explicitly return null so that we can determine whether to remove
2134
// the listener for .once
@@ -140,11 +153,14 @@ function genFilterCode (key: string): string {
140153
if (keyVal) {
141154
return `$event.keyCode!==${keyVal}`
142155
}
143-
const code = keyCodes[key]
156+
const keyCode = keyCodes[key]
157+
const keyName = keyNames[key]
144158
return (
145159
`_k($event.keyCode,` +
146160
`${JSON.stringify(key)},` +
147-
`${JSON.stringify(code)},` +
148-
`$event.key)`
161+
`${JSON.stringify(keyCode)},` +
162+
`$event.key,` +
163+
`${JSON.stringify(keyName)}` +
164+
`)`
149165
)
150166
}

src/core/instance/render-helpers/check-keycodes.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33
import config from 'core/config'
44
import { hyphenate } from 'shared/util'
55

6-
const keyNames: { [key: string]: string | Array<string> } = {
7-
esc: 'Escape',
8-
tab: 'Tab',
9-
enter: 'Enter',
10-
space: ' ',
11-
up: 'ArrowUp',
12-
left: 'ArrowLeft',
13-
right: 'ArrowRight',
14-
down: 'ArrowDown',
15-
'delete': ['Backspace', 'Delete']
16-
}
17-
186
function isKeyNotMatch<T> (expect: T | Array<T>, actual: T): boolean {
197
if (Array.isArray(expect)) {
208
return expect.indexOf(actual) === -1
@@ -31,15 +19,15 @@ function isKeyNotMatch<T> (expect: T | Array<T>, actual: T): boolean {
3119
export function checkKeyCodes (
3220
eventKeyCode: number,
3321
key: string,
34-
builtInAlias?: number | Array<number>,
35-
eventKeyName?: string
22+
builtInKeyCode?: number | Array<number>,
23+
eventKeyName?: string,
24+
builtInKeyName?: string | Array<string>
3625
): ?boolean {
37-
const keyCodes = config.keyCodes[key] || builtInAlias
38-
const builtInName: string | Array<string> = keyNames[key]
39-
if (builtInName && keyCodes === builtInAlias && eventKeyName) {
40-
return isKeyNotMatch(builtInName, eventKeyName)
41-
} else if (keyCodes) {
42-
return isKeyNotMatch(keyCodes, eventKeyCode)
26+
const mappedKeyCode = config.keyCodes[key] || builtInKeyCode
27+
if (builtInKeyName && eventKeyName && !config.keyCodes[key]) {
28+
return isKeyNotMatch(builtInKeyName, eventKeyName)
29+
} else if (mappedKeyCode) {
30+
return isKeyNotMatch(mappedKeyCode, eventKeyCode)
4331
} else if (eventKeyName) {
4432
return hyphenate(eventKeyName) !== key
4533
}

test/unit/modules/compiler/codegen.spec.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,17 @@ describe('codegen', () => {
265265
it('generate events with keycode', () => {
266266
assertCodegen(
267267
'<input @input.enter="onInput">',
268-
`with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key))return null;onInput($event)}}})}`
268+
`with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;onInput($event)}}})}`
269269
)
270270
// multiple keycodes (delete)
271271
assertCodegen(
272272
'<input @input.delete="onInput">',
273-
`with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"delete",[8,46],$event.key))return null;onInput($event)}}})}`
273+
`with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete"]))return null;onInput($event)}}})}`
274274
)
275275
// multiple keycodes (chained)
276276
assertCodegen(
277277
'<input @keydown.enter.delete="onInput">',
278-
`with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key)&&_k($event.keyCode,"delete",[8,46],$event.key))return null;onInput($event)}}})}`
278+
`with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete"]))return null;onInput($event)}}})}`
279279
)
280280
// number keycode
281281
assertCodegen(
@@ -285,7 +285,7 @@ describe('codegen', () => {
285285
// custom keycode
286286
assertCodegen(
287287
'<input @input.custom="onInput">',
288-
`with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"custom",undefined,$event.key))return null;onInput($event)}}})}`
288+
`with(this){return _c('input',{on:{"input":function($event){if(!('button' in $event)&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;onInput($event)}}})}`
289289
)
290290
})
291291

@@ -308,12 +308,12 @@ describe('codegen', () => {
308308
it('generate events with generic modifiers and keycode correct order', () => {
309309
assertCodegen(
310310
'<input @keydown.enter.prevent="onInput">',
311-
`with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key))return null;$event.preventDefault();onInput($event)}}})}`
311+
`with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();onInput($event)}}})}`
312312
)
313313

314314
assertCodegen(
315315
'<input @keydown.enter.stop="onInput">',
316-
`with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key))return null;$event.stopPropagation();onInput($event)}}})}`
316+
`with(this){return _c('input',{on:{"keydown":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();onInput($event)}}})}`
317317
)
318318
})
319319

@@ -420,7 +420,7 @@ describe('codegen', () => {
420420
// with modifiers
421421
assertCodegen(
422422
`<input @keyup.enter="e=>current++">`,
423-
`with(this){return _c('input',{on:{"keyup":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key))return null;(e=>current++)($event)}}})}`
423+
`with(this){return _c('input',{on:{"keyup":function($event){if(!('button' in $event)&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;(e=>current++)($event)}}})}`
424424
)
425425
})
426426

0 commit comments

Comments
 (0)