Skip to content

Commit

Permalink
fix component tree navigation (fix vuejs#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 15, 2017
1 parent 5d7bd9c commit 6542847
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/devtools/views/components/ComponentTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<div slot="scroll" class="tree">
<component-instance
v-for="instance in instances"
ref="instances"
:key="instance.id"
:instance="instance"
:depth="0">
Expand All @@ -25,6 +26,7 @@ import ComponentInstance from './ComponentInstance.vue'
import keyNavMixin from '../../mixins/key-nav'
export default {
mixins: [keyNavMixin],
components: {
ScrollPane,
ActionHeader,
Expand All @@ -33,11 +35,11 @@ export default {
props: {
instances: Array
},
mixins: [keyNavMixin],
methods: {
filterInstances (e) {
bridge.send('filter-instances', e.target.value)
},
onKeyNav (dir) {
// somewhat hacky key navigation, but it works!
const currentEl = this.$el.querySelector('.instance.selected')
Expand All @@ -54,43 +56,38 @@ export default {
}
} else if (dir === 'right') {
if (current.expanded && current.$children.length) {
current = findByOffset(current, 1)
current = this.findByOffset(current, 1)
current.select()
} else {
current.expand()
}
} else if (dir === 'up') {
current = findByOffset(current, -1)
current = this.findByOffset(current, -1)
current.select()
} else {
current = findByOffset(current, 1)
current = this.findByOffset(current, 1)
current.select()
}
},
findByOffset (current, offset) {
const all = getAllInstances(this.$refs.instances)
const index = all.indexOf(current) + offset
if (index < 0) {
return all[0]
} else if (index >= all.length) {
return all[all.length - 1]
} else {
return all[index]
}
}
}
}
function getAllInstances () {
const nodes = [...document.querySelectorAll('.instance')]
return nodes.map(n => n.__vue__)
}
function findByOffset (current, offset) {
const all = getAllInstances()
let currentIndex = -1
all.forEach((el, index) => {
if (current === el) {
currentIndex = index
}
})
offset = currentIndex + offset
if (offset < 0) {
return all[0]
} else if (offset >= all.length) {
return all[all.length - 1]
} else {
return all[offset]
}
function getAllInstances (list) {
return Array.prototype.concat.apply([], list.map(instance => {
return [instance, ...getAllInstances(instance.$children)]
}))
}
</script>

Expand Down

0 comments on commit 6542847

Please sign in to comment.