perf: only run getSelection() check if we have to (#817)

This commit is contained in:
Nolan Lawson 2018-12-15 19:21:14 -08:00 committed by GitHub
parent 3a335a9f4a
commit f2f5508144
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 11 deletions

View File

@ -1,7 +1,5 @@
// Delegate certain events to the global document for perf purposes.
import { mark, stop } from './marks'
const callbacks = {}
if (process.browser && process.env.NODE_ENV !== 'production') {
@ -14,14 +12,6 @@ function onEvent (e) {
// we're not interested in any non-click or non-Enter events
return
}
if (type === 'click') {
let selection = window.getSelection()
let selectionStr = selection && selection.toString()
if (selectionStr && selectionStr.length && target.contains(selection.anchorNode)) {
return // ignore if the user is selecting text inside the clickable area
}
}
mark('delegate onEvent')
let key
let element = target
while (element) {
@ -31,9 +21,15 @@ function onEvent (e) {
element = element.parentElement
}
if (key && callbacks[key]) {
if (type === 'click') {
let selection = window.getSelection()
let selectionStr = selection && selection.toString()
if (selectionStr && selectionStr.length && target.contains(selection.anchorNode)) {
return // ignore if the user is selecting text inside the clickable area
}
}
callbacks[key](e)
}
stop('delegate onEvent')
}
export function registerClickDelegates (component, delegates) {