Skip to content

Commit d2aed3e

Browse files
committed
?inspector={element} and Entity.inspect() helpers to reduce inspector friction
1 parent 14d7ad6 commit d2aed3e

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/components/scene/inspector.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* global AFRAME */
22
var AFRAME_INJECTED = require('../../constants').AFRAME_INJECTED;
3-
var bind = require('../../utils/bind');
43
var pkg = require('../../../package');
54
var registerComponent = require('../../core/component').registerComponent;
5+
var utils = require('../../utils/');
66

77
/**
88
* 0.4.2 to 0.4.x
@@ -26,13 +26,24 @@ module.exports.Component = registerComponent('inspector', {
2626
},
2727

2828
init: function () {
29-
this.onKeydown = bind(this.onKeydown, this);
30-
this.onMessage = bind(this.onMessage, this);
29+
this.firstPlay = true;
30+
this.onKeydown = this.onKeydown.bind(this);
31+
this.onMessage = this.onMessage.bind(this);
3132
this.initOverlay();
3233
window.addEventListener('keydown', this.onKeydown);
3334
window.addEventListener('message', this.onMessage);
3435
},
3536

37+
play: function () {
38+
var urlParam;
39+
if (!this.firstPlay) { return; }
40+
urlParam = utils.getUrlParameter('inspector');
41+
if (urlParam !== 'false' && !!urlParam) {
42+
this.openInspector();
43+
this.firstPlay = false;
44+
}
45+
},
46+
3647
initOverlay: function () {
3748
var dotsHTML = '<span class="dots"><span>.</span><span>.</span><span>.</span></span>';
3849
this.loadingMessageEl = document.createElement('div');
@@ -49,8 +60,8 @@ module.exports.Component = registerComponent('inspector', {
4960
*/
5061
onKeydown: function (evt) {
5162
var shortcutPressed = evt.keyCode === 73 && evt.ctrlKey && evt.altKey;
52-
if (!this.data || !shortcutPressed) { return; }
53-
this.injectInspector();
63+
if (!shortcutPressed) { return; }
64+
this.openInspector();
5465
},
5566

5667
showLoader: function () {
@@ -65,14 +76,18 @@ module.exports.Component = registerComponent('inspector', {
6576
* postMessage. aframe.io uses this to create a button on examples to open Inspector.
6677
*/
6778
onMessage: function (evt) {
68-
if (evt.data === 'INJECT_AFRAME_INSPECTOR') { this.injectInspector(); }
79+
if (evt.data === 'INJECT_AFRAME_INSPECTOR') { this.openInspector(); }
6980
},
7081

71-
injectInspector: function () {
82+
openInspector: function (focusEl) {
7283
var self = this;
7384
var script;
7485

75-
if (AFRAME.INSPECTOR || AFRAME.inspectorInjected) { return; }
86+
// Already injected. Open.
87+
if (AFRAME.INSPECTOR || AFRAME.inspectorInjected) {
88+
AFRAME.INSPECTOR.open(focusEl);
89+
return;
90+
}
7691

7792
this.showLoader();
7893

@@ -82,7 +97,7 @@ module.exports.Component = registerComponent('inspector', {
8297
script.setAttribute('data-name', 'aframe-inspector');
8398
script.setAttribute(AFRAME_INJECTED, '');
8499
script.onload = function () {
85-
AFRAME.INSPECTOR.open();
100+
AFRAME.INSPECTOR.open(focusEl);
86101
self.hideLoader();
87102
self.removeEventListeners();
88103
};

src/core/a-entity.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,15 @@ var proto = Object.create(ANode.prototype, {
814814
value: function (state) {
815815
return this.states.indexOf(state) !== -1;
816816
}
817+
},
818+
819+
/**
820+
* Open Inspector to this entity.
821+
*/
822+
inspect: {
823+
value: function () {
824+
this.sceneEl.components.inspector.openInspector(this);
825+
}
817826
}
818827
});
819828

0 commit comments

Comments
 (0)