Skip to content

Commit c574c94

Browse files
committed
Scroll component tree to selected item
When an item is inspected via the context menu we try to put it into view in the component tree. We make an educated guess as to how far down we should scroll based on the item's index and a magic height number
1 parent 0420a80 commit c574c94

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

app/controllers/component-tree.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ const flattenSearchTree = (
7272
export default Controller.extend({
7373
application: controller(),
7474
queryParams: ['pinnedObjectId'],
75+
76+
/**
77+
* The entry in the component tree corresponding to the pinnedObjectId
78+
* will be selected
79+
*/
7580
pinnedObjectId: null,
7681
inspectingViews: false,
7782
components: true,
@@ -140,13 +145,34 @@ export default Controller.extend({
140145
this.set('expandedStateCache', {});
141146
},
142147

148+
/**
149+
* Expands the component tree so that entry for the given view will
150+
* be shown. Recursively expands the entry's parents up to the root.
151+
* @param {*} objectId The id of the ember view to show
152+
*/
143153
expandToNode(objectId) {
144154
let node = this.get('filteredArray').find(item => item.get('id') === objectId);
145155
if (node) {
146156
node.expandParents();
147157
}
148158
},
149159

160+
/**
161+
* This method is basically a trick to get the `{{vertical-collection}}` in the vicinity
162+
* of the item that's been selected. We can't directly scroll to the element but we
163+
* can guess at how far down the list the item is. Then we can manually set the scrollTop
164+
* of the virtual scroll.
165+
*/
166+
scrollTreeToItem(objectId) {
167+
let selectedItemIndex = this.get('displayedList').findIndex(item => item.view.objectId === objectId);
168+
169+
if (selectedItemIndex) {
170+
const averageItemHeight = 22;
171+
document.querySelector('.js-component-tree').scrollTop = averageItemHeight * selectedItemIndex;
172+
}
173+
},
174+
175+
150176
actions: {
151177
previewLayer({
152178
view: {
@@ -201,12 +227,16 @@ export default Controller.extend({
201227
if (objectId) {
202228
this.set('pinnedObjectId', objectId);
203229
this.expandToNode(objectId);
230+
this.scrollTreeToItem(objectId);
204231
this.get('port').send('objectInspector:inspectById', {
205232
objectId,
206233
});
207234
}
208235
},
209236

237+
/**
238+
* Scrolls the main page to put the selected element into view
239+
*/
210240
scrollToElement(elementId) {
211241
this.get('port').send('view:scrollToElement', {
212242
elementId

app/routes/application.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ export default Route.extend({
3333
},
3434

3535
inspectComponent({ viewId }) {
36-
const isActive = this.get('controller.active');
37-
if (!isActive) {
38-
//TODO: Tell the user to open the Ember devtools panel
39-
}
4036
this.transitionTo('component-tree', {
4137
queryParams: {
4238
pinnedObjectId: viewId

app/templates/component-tree.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="list__content" style="height: 100%;">
1+
<div class="list__content js-component-tree" style="height: 100%;">
22
{{#vertical-collection displayedList estimateHeight=20 as |item i|}}
33
<div onmouseenter={{action "previewLayer" item}}
44
onmouseleave={{action "hidePreview"}}>

0 commit comments

Comments
 (0)