-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Select component from dom tree #476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
00f3024
e7a244c
d1fd11c
777d473
45750bc
22ee46e
e537beb
ec0a931
dcd3577
36fd5d8
6155503
4c81327
e07a773
18ae2fe
3cd74ca
0d8d1ea
0b067b0
12bf866
fdf2ceb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { highlight, unHighlight } from './highlighter' | ||
import { findRelatedComponent } from './utils' | ||
|
||
export default class ComponentSelector { | ||
constructor (bridge, instanceMap) { | ||
const self = this | ||
self.bridge = bridge | ||
self.instanceMap = instanceMap | ||
self.bindMethods() | ||
|
||
bridge.on('start-component-selector', self.startSelecting) | ||
bridge.on('stop-component-selector', self.stopSelecting) | ||
} | ||
|
||
/** | ||
* Adds event listeners for mouseover and mouseup | ||
*/ | ||
startSelecting () { | ||
document.body.addEventListener('mouseover', this.elementMouseOver, true) | ||
document.body.addEventListener('click', this.elementClicked, true) | ||
document.body.addEventListener('mouseout', this.cancelEvent, true) | ||
document.body.addEventListener('mouseenter', this.cancelEvent, true) | ||
document.body.addEventListener('mouseleave', this.cancelEvent, true) | ||
document.body.addEventListener('mousedown', this.cancelEvent, true) | ||
document.body.addEventListener('mouseup', this.cancelEvent, true) | ||
} | ||
|
||
/** | ||
* Removes event listeners | ||
*/ | ||
stopSelecting () { | ||
document.body.removeEventListener('mouseover', this.elementMouseOver, true) | ||
document.body.removeEventListener('click', this.elementClicked, true) | ||
document.body.removeEventListener('mouseout', this.cancelEvent, true) | ||
document.body.removeEventListener('mouseenter', this.cancelEvent, true) | ||
document.body.removeEventListener('mouseleave', this.cancelEvent, true) | ||
document.body.removeEventListener('mousedown', this.cancelEvent, true) | ||
document.body.removeEventListener('mouseup', this.cancelEvent, true) | ||
|
||
unHighlight() | ||
} | ||
|
||
/** | ||
* Highlights a component on element mouse over | ||
* @param {MouseEvent} e | ||
*/ | ||
elementMouseOver (e) { | ||
this.cancelEvent(e) | ||
|
||
const el = e.target | ||
if (el) { | ||
this.selectedInstance = findRelatedComponent(el) | ||
} | ||
|
||
unHighlight() | ||
if (this.selectedInstance) { | ||
highlight(this.selectedInstance) | ||
} | ||
} | ||
|
||
/** | ||
* Selects an instance in the component view | ||
* @param {MouseEvent} e | ||
*/ | ||
elementClicked (e) { | ||
this.cancelEvent(e) | ||
|
||
if (this.selectedInstance) { | ||
this.bridge.send('inspect-instance', this.selectedInstance.__VUE_DEVTOOLS_UID__) | ||
} | ||
|
||
this.stopSelecting() | ||
} | ||
|
||
/** | ||
* Cancel a mouse event | ||
* @param {MouseEvent} e | ||
*/ | ||
cancelEvent (e) { | ||
e.stopImmediatePropagation() | ||
e.preventDefault() | ||
} | ||
|
||
/** | ||
* Bind class methods to the class scope to avoid rebind for event listeners | ||
*/ | ||
bindMethods () { | ||
this.startSelecting = this.startSelecting.bind(this) | ||
this.stopSelecting = this.stopSelecting.bind(this) | ||
this.elementMouseOver = this.elementMouseOver.bind(this) | ||
this.elementClicked = this.elementClicked.bind(this) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function findRelatedComponent (el) { | ||
while (!el.__vue__ && el.parentElement) { | ||
el = el.parentElement | ||
} | ||
return el.__vue__ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
const navMap = { | ||
37: 'left', | ||
38: 'up', | ||
39: 'right', | ||
40: 'down' | ||
} | ||
export const LEFT = 37 | ||
export const UP = 38 | ||
export const RIGHT = 39 | ||
export const DOWN = 40 | ||
export const S = 83 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you also type in Dvorak? 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? I just tried in Dvorak to be sure and it's working fine... 😕 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're completely right. I had recently a problem with keycodes but it's something a bit different, it only happens with the digits row |
||
|
||
const activeInstances = [] | ||
|
||
document.addEventListener('keyup', e => { | ||
if (e.target.tagName === 'INPUT') { | ||
return | ||
} | ||
if (navMap[e.keyCode]) { | ||
activeInstances.forEach(vm => { | ||
if (vm.onKeyNav) { | ||
vm.onKeyNav(navMap[e.keyCode]) | ||
} | ||
}) | ||
} | ||
activeInstances.forEach(vm => { | ||
if (vm.onKeyUp) { | ||
vm.onKeyUp(e) | ||
} | ||
}) | ||
}) | ||
|
||
export default { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would replace this with
since it doesn't look like it's displaying anything otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can still show the component boundaries even if the component doesn't have a name (most certainly using an old version of vue-loader or vue).