Skip to content

Commit 6542847

Browse files
committed
fix component tree navigation (fix #283)
1 parent 5d7bd9c commit 6542847

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/devtools/views/components/ComponentTree.vue

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<div slot="scroll" class="tree">
1010
<component-instance
1111
v-for="instance in instances"
12+
ref="instances"
1213
:key="instance.id"
1314
:instance="instance"
1415
:depth="0">
@@ -25,6 +26,7 @@ import ComponentInstance from './ComponentInstance.vue'
2526
import keyNavMixin from '../../mixins/key-nav'
2627
2728
export default {
29+
mixins: [keyNavMixin],
2830
components: {
2931
ScrollPane,
3032
ActionHeader,
@@ -33,11 +35,11 @@ export default {
3335
props: {
3436
instances: Array
3537
},
36-
mixins: [keyNavMixin],
3738
methods: {
3839
filterInstances (e) {
3940
bridge.send('filter-instances', e.target.value)
4041
},
42+
4143
onKeyNav (dir) {
4244
// somewhat hacky key navigation, but it works!
4345
const currentEl = this.$el.querySelector('.instance.selected')
@@ -54,43 +56,38 @@ export default {
5456
}
5557
} else if (dir === 'right') {
5658
if (current.expanded && current.$children.length) {
57-
current = findByOffset(current, 1)
59+
current = this.findByOffset(current, 1)
5860
current.select()
5961
} else {
6062
current.expand()
6163
}
6264
} else if (dir === 'up') {
63-
current = findByOffset(current, -1)
65+
current = this.findByOffset(current, -1)
6466
current.select()
6567
} else {
66-
current = findByOffset(current, 1)
68+
current = this.findByOffset(current, 1)
6769
current.select()
6870
}
71+
},
72+
73+
findByOffset (current, offset) {
74+
const all = getAllInstances(this.$refs.instances)
75+
const index = all.indexOf(current) + offset
76+
if (index < 0) {
77+
return all[0]
78+
} else if (index >= all.length) {
79+
return all[all.length - 1]
80+
} else {
81+
return all[index]
82+
}
6983
}
7084
}
7185
}
7286
73-
function getAllInstances () {
74-
const nodes = [...document.querySelectorAll('.instance')]
75-
return nodes.map(n => n.__vue__)
76-
}
77-
78-
function findByOffset (current, offset) {
79-
const all = getAllInstances()
80-
let currentIndex = -1
81-
all.forEach((el, index) => {
82-
if (current === el) {
83-
currentIndex = index
84-
}
85-
})
86-
offset = currentIndex + offset
87-
if (offset < 0) {
88-
return all[0]
89-
} else if (offset >= all.length) {
90-
return all[all.length - 1]
91-
} else {
92-
return all[offset]
93-
}
87+
function getAllInstances (list) {
88+
return Array.prototype.concat.apply([], list.map(instance => {
89+
return [instance, ...getAllInstances(instance.$children)]
90+
}))
9491
}
9592
</script>
9693

0 commit comments

Comments
 (0)