@@ -3,9 +3,13 @@ import {hot} from 'react-hot-loader';
33import plotly from 'plotly.js/dist/plotly' ;
44import '../src/styles/main.scss' ;
55import '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' ;
78import Select from 'react-select' ;
89import 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
1115import 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 >
0 commit comments