Skip to content

Commit 388faf2

Browse files
fancier dev tools (#431)
1 parent 256d102 commit 388faf2

File tree

4 files changed

+102
-39
lines changed

4 files changed

+102
-39
lines changed

dev/App.js

+95-38
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import {hot} from 'react-hot-loader';
33
import plotly from 'plotly.js/dist/plotly';
44
import '../src/styles/main.scss';
55
import 'react-select/dist/react-select.css';
6-
import ReactJson from 'react-json-view';
6+
import brace from 'brace'; // eslint-disable-line no-unused-vars
7+
import AceEditor from 'react-ace';
78
import Select from 'react-select';
89
import PlotlyEditor, {DefaultEditor, Panel} from '../src';
10+
import Inspector from 'react-inspector';
11+
import 'brace/mode/json';
12+
import 'brace/theme/textmate';
913

1014
// https://github.com/plotly/react-chart-editor#mapbox-access-tokens
1115
import ACCESS_TOKENS from '../accessTokens';
@@ -39,6 +43,8 @@ class App extends Component {
3943
};
4044

4145
this.loadMock = this.loadMock.bind(this);
46+
this.loadJSON = this.loadJSON.bind(this);
47+
this.updateState = this.updateState.bind(this);
4248
}
4349

4450
componentWillMount() {
@@ -59,15 +65,32 @@ class App extends Component {
5965
)
6066
.then(response => response.json())
6167
.then(figure => {
62-
this.setState({
63-
currentMockIndex: mockIndex,
64-
data: figure.data,
65-
layout: figure.layout,
66-
frames: figure.frames,
67-
});
68+
const {data, layout, frames} = figure;
69+
this.updateState(data, layout, frames, mockIndex);
6870
});
6971
}
7072

73+
updateState(data, layout, frames, currentMockIndex) {
74+
this.setState({
75+
data,
76+
layout,
77+
frames,
78+
currentMockIndex,
79+
full: 'hit refresh',
80+
json_error: false,
81+
json_string: JSON.stringify({data, layout, frames}, null, 2),
82+
});
83+
}
84+
85+
loadJSON() {
86+
try {
87+
const {data, layout, frames} = JSON.parse(this.state.json_string);
88+
this.updateState(data, layout, frames);
89+
} catch (e) {
90+
this.setState({json_error: true});
91+
}
92+
}
93+
7194
render() {
7295
return (
7396
<div className="app">
@@ -79,47 +102,81 @@ class App extends Component {
79102
dataSources={dataSources}
80103
dataSourceOptions={dataSourceOptions}
81104
plotly={plotly}
82-
onUpdate={(data, layout, frames) =>
83-
this.setState({data, layout, frames})
84-
}
105+
onUpdate={this.updateState}
106+
divId="gd"
85107
useResizeHandler
86108
debug
87109
advancedTraceTypeSelector
88110
>
89-
{' '}
90111
<DefaultEditor>
91112
<Panel group="Dev" name="JSON">
92-
<Select
93-
clearable={true}
94-
value={this.state.currentMockIndex}
95-
name="mock-dropdown"
96-
options={this.state.mocks.map((item, i) => ({
97-
label: item,
98-
value: i,
99-
}))}
100-
searchable={true}
101-
searchPromptText="Search for a mock"
102-
onChange={option => this.loadMock(option.value)}
103-
noResultsText={'No Results'}
104-
placeholder={'Search for a mock'}
105-
/>
113+
<div className="mocks">
114+
<Select
115+
clearable={false}
116+
value={this.state.currentMockIndex}
117+
name="mock-dropdown"
118+
options={this.state.mocks.map((item, i) => ({
119+
label: item,
120+
value: i,
121+
}))}
122+
searchable={true}
123+
searchPromptText="Search for a mock"
124+
onChange={option => this.loadMock(option.value)}
125+
noResultsText={'No Results'}
126+
placeholder={'Search for a mock'}
127+
/>
128+
</div>
106129
<br />
107-
<ReactJson
108-
enableClipboard={false}
109-
name={false}
110-
displayDataTypes={false}
111-
displayObjectSize={false}
112-
indentWidth={2}
113-
onAdd={({updated_src}) => this.setState(updated_src)}
114-
onEdit={({updated_src}) => this.setState(updated_src)}
115-
onDelete={({updated_src}) => this.setState(updated_src)}
116-
src={{
117-
data: this.state.data,
118-
layout: this.state.layout,
119-
frames: this.state.frames,
130+
<button
131+
onClick={this.loadJSON}
132+
style={{background: this.state.json_error ? 'pink' : 'white'}}
133+
>
134+
Save
135+
</button>
136+
<br />
137+
<AceEditor
138+
mode="json"
139+
theme="textmate"
140+
onChange={json_string => this.setState({json_string})}
141+
value={this.state.json_string}
142+
name="UNIQUE_ID_OF_DIV"
143+
style={{height: '80vh'}}
144+
setOptions={{
145+
showLineNumbers: false,
146+
tabSize: 2,
120147
}}
148+
commands={[
149+
{
150+
name: 'save',
151+
bindKey: {win: 'Ctrl-s', mac: 'Command-s'},
152+
exec: this.loadJSON,
153+
},
154+
]}
121155
/>
122156
</Panel>
157+
<Panel group="Dev" name="Inspector">
158+
<button
159+
onClick={() => {
160+
const gd = document.getElementById('gd') || {};
161+
this.setState({
162+
full: {
163+
_fullData: gd._fullData || [],
164+
_fullLayout: gd._fullLayout || {},
165+
},
166+
});
167+
}}
168+
>
169+
Refresh
170+
</button>
171+
<br />
172+
<div style={{height: '80vh'}}>
173+
<Inspector
174+
data={{_full: this.state.full}}
175+
expandLevel={2}
176+
sortObjectKeys={true}
177+
/>
178+
</div>
179+
</Panel>
123180
</DefaultEditor>
124181
</PlotlyEditor>
125182
</div>

dev/styles.css

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ body {
99
max-height: 100vh;
1010
}
1111

12+
.mocks * {
13+
z-index: 100;
14+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@
6262
"postcss-remove-root": "^0.0.2",
6363
"prettier": "^1.9.2",
6464
"react": "^16.0.0",
65+
"react-ace": "^5.9.0",
6566
"react-dom": "^16.0.0",
6667
"react-hot-loader": "^4.0.0-beta.21",
67-
"react-json-view": "^1.16.1",
68+
"react-inspector": "^2.2.2",
6869
"react-test-renderer": "^16.2.0",
6970
"sass-loader": "^6.0.6",
7071
"style-loader": "^0.19.1",

src/PlotlyEditor.js

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class PlotlyEditor extends Component {
4040
onInitialized={(fig, graphDiv) => this.setState({graphDiv})}
4141
onUpdate={(fig, graphDiv) => this.setState({graphDiv})}
4242
style={{width: '100%', height: '100%'}}
43+
divId={this.props.divId}
4344
/>
4445
</div>
4546
</div>
@@ -63,6 +64,7 @@ PlotlyEditor.propTypes = {
6364
locale: PropTypes.string,
6465
traceTypesConfig: PropTypes.object,
6566
dictionaries: PropTypes.object,
67+
divId: PropTypes.string,
6668
};
6769

6870
export default PlotlyEditor;

0 commit comments

Comments
 (0)