@@ -4,52 +4,68 @@ import classnames from 'classnames';
44
55import '../../css/playground.css!' ;
66import PlaygroundStore from '../stores/PlaygroundStore' ;
7+ import PlaygroundSettingsStore from '../stores/PlaygroundSettingsStore' ;
78import PlaygroundActions from '../actions/PlaygroundActions' ;
89
910
11+ import PlaygroundHeader from './PlaygroundHeader' ;
1012import EditorTextarea from './EditorTextarea' ;
1113
1214
13- function getPlaygroundState ( props , state ) {
15+
16+ function gatherPlaygroundStoreState ( props , state ) {
1417 var newOutputResult = PlaygroundStore . getOutputResult ( ) ;
1518
1619 return {
20+ postcssInputText : PlaygroundStore . getInputText ( ) ,
1721 postcssOutputResult : newOutputResult ,
1822 // If there was an error in parsing, then use the last known good one
19- prevSuccessfulPostcssOutputResult : newOutputResult . error ? state . postcssOutputResult : newOutputResult
23+ prevSuccessfulPostcssOutputResult : newOutputResult . get ( 'error' ) ? state . postcssOutputResult : newOutputResult
24+ } ;
25+ }
26+
27+ function gatherPlaygroundSettingsStoreState ( props , state ) {
28+ return {
29+ postcssCssVariablesPreserve : PlaygroundSettingsStore . getPluginSettings ( ) . getIn ( [ 'postcss-css-variables' , 'preserve' ] ) ,
30+ shouldLiveReload : PlaygroundSettingsStore . getShouldLiveReload ( ) ,
31+ tabWidth : PlaygroundSettingsStore . getTabWidth ( )
2032 } ;
2133}
2234
2335export default class PlaygroundApp extends React . Component {
2436 constructor ( props ) {
2537 super ( props ) ;
26- this . state = assign ( getPlaygroundState ( props , ( this . state || { } ) ) , {
27- shouldLiveReload : props . initialShouldLiveReload || true ,
28- postcssInputText : props . initialPostcssInputText || '' ,
29- } ) ;
38+ this . state = assign (
39+ gatherPlaygroundStoreState ( props , ( this . state || { } ) ) ,
40+ gatherPlaygroundSettingsStoreState ( props , ( this . state || { } ) )
41+ ) ;
3042 }
3143
3244 componentDidMount ( ) {
33- PlaygroundStore . addChangeListener ( this . _onStoreChange . bind ( this ) ) ;
45+ PlaygroundStore . addChangeListener ( this . _onPlaygroundStoreChange . bind ( this ) ) ;
46+ PlaygroundSettingsStore . addChangeListener ( this . _onPlaygroundSettingsStoreChange . bind ( this ) ) ;
3447
3548 document . addEventListener ( 'keyup' , this . _handleKeyUp . bind ( this ) ) ;
49+
50+ // Initialize the application
51+ PlaygroundActions . init ( ) ;
3652 }
3753
3854 componentWillUnmount ( ) {
39- PlaygroundStore . removeChangeListener ( this . _onStoreChange ) ;
55+ PlaygroundStore . removeChangeListener ( this . _onPlaygroundStoreChange . bind ( this ) ) ;
56+ PlaygroundSettingsStore . removeChangeListener ( this . _onPlaygroundSettingsStoreChange . bind ( this ) ) ;
4057
4158 document . removeEventListener ( 'keyup' , this . _handleKeyUp . bind ( this ) ) ;
4259 }
4360
4461 render ( ) {
4562 //console.log('render', this.state);
4663
47- var doesInputHaveError = ! ! this . state . postcssOutputResult . error ;
48- var output = doesInputHaveError ? this . state . prevSuccessfulPostcssOutputResult . result : this . state . postcssOutputResult . result ;
49-
64+ var doesInputHaveError = ! ! this . state . postcssOutputResult . get ( 'error' ) ;
65+ var output = doesInputHaveError ? this . state . prevSuccessfulPostcssOutputResult . get ( 'output' ) : this . state . postcssOutputResult . get ( 'output' ) ;
5066
5167 var parsingErrorMarkup ;
52- if ( this . state . postcssOutputResult . error ) {
68+ if ( this . state . postcssOutputResult . get ( ' error' ) ) {
5369 parsingErrorMarkup = (
5470 React . createElement ( "div" , {
5571 className : "postcss-editor-pane-error" ,
@@ -60,40 +76,23 @@ export default class PlaygroundApp extends React.Component {
6076 React . createElement ( "div" , {
6177 className : "postcss-editor-pane-error-message"
6278 } ,
63- this . state . postcssOutputResult . error . toString ( )
79+ this . state . postcssOutputResult . get ( ' error' ) . toString ( )
6480 )
6581 )
6682 ) ;
6783 }
6884
85+
86+ var tabWidthStyleValue = this . state . tabWidth === 'inherit' ? this . state . tabWidth : this . state . tabWidth + 'ch' ;
87+
6988 return (
7089 React . createElement ( "div" , { className : "playground-app-wrapper" } ,
71- React . createElement ( "header" , {
72- className : "playground-header"
73- } ,
74- React . createElement ( "h1" , {
75- className : "playground-header-heading"
76- } ,
77- React . createElement ( "a" , { className : "playground-header-heading-primary-title" , href : "https://github.com/MadLittleMods/postcss-css-variables" } , "postcss-css-variables" ) , " Playground - " , React . createElement ( "a" , { href : "https://github.com/postcss/postcss" } , "PostCSS" )
78- ) ,
79- React . createElement ( "label" , {
80- className : "live-reload-toggle-checkbox" ,
81- htmlFor : "id-live-reload-checkbox" ,
82- onChange : this . _onLiveReloadCheckboxChanged . bind ( this )
83- } ,
84- React . createElement ( "input" , {
85- type : "checkbox" ,
86- id : "id-live-reload-checkbox" ,
87- checked : this . state . shouldLiveReload ,
88- onChange : this . _onLiveReloadCheckboxChanged . bind ( this ) }
89- )
90- ) ,
91- React . createElement ( "button" , {
92- className : "playground-header-save-button" ,
93- onClick : this . _onSave . bind ( this )
94- } ,
95- "Save"
96- )
90+
91+ React . createElement ( PlaygroundHeader , {
92+ tabWidth : this . state . tabWidth ,
93+ shouldLiveReload : this . state . shouldLiveReload ,
94+
95+ postcssCssVariablesPreserve : this . state . postcssCssVariablesPreserve }
9796 ) ,
9897
9998 React . createElement ( "div" , { className : "postcss-editor-area" } ,
@@ -107,7 +106,11 @@ export default class PlaygroundApp extends React.Component {
107106 ref : "postcssInputTextarea" ,
108107 className : "postcss-textarea" ,
109108 onChange : this . _onInputChanged . bind ( this ) ,
110- value : this . state . postcssInputText }
109+ value : this . state . postcssInputText ,
110+
111+ style : {
112+ tabSize : tabWidthStyleValue
113+ } }
111114 )
112115 ) ,
113116 React . createElement ( "div" , { className : "postcss-editor-pane" } ,
@@ -119,7 +122,11 @@ export default class PlaygroundApp extends React.Component {
119122 React . createElement ( EditorTextarea , {
120123 ref : "postcssOutputTextarea" ,
121124 className : classnames ( 'postcss-textarea' , { 'is-not-current' : doesInputHaveError } ) ,
122- value : output }
125+ value : output ,
126+
127+ style : {
128+ tabSize : tabWidthStyleValue
129+ } }
123130 ) ,
124131 parsingErrorMarkup
125132 )
@@ -128,36 +135,25 @@ export default class PlaygroundApp extends React.Component {
128135 ) ;
129136 }
130137
131- _onSave ( ) {
132- //console.log('saving');
133- PlaygroundActions . updateInput ( this . state . postcssInputText ) ;
134- }
135-
136138 _onInputChanged ( text ) {
137139 //console.log('input changed');
138- this . setState ( {
139- postcssInputText : text
140- } ) ;
140+ PlaygroundActions . updateInput ( text ) ;
141141
142142 // Defaults to true if undefined
143143 if ( this . state . shouldLiveReload ) {
144- PlaygroundActions . updateInput ( text ) ;
144+ PlaygroundActions . processInput ( ) ;
145145 }
146146 }
147147
148- _onLiveReloadCheckboxChanged ( ) {
149- this . setState ( {
150- shouldLiveReload : event . target . checked
151- } ) ;
152- }
153-
154148 _handleKeyUp ( e ) {
155149 //console.log(e);
156150
157151 // escape
158152 if ( e . keyCode === 27 ) {
159153 // Unfocus/blur the currently focused elemnt
160154 document . activeElement . blur ( ) ;
155+
156+ PlaygroundActions . keyboardActionFired ( ) ;
161157 }
162158
163159 // If nothing is focused currently
@@ -166,23 +162,31 @@ export default class PlaygroundApp extends React.Component {
166162 if ( e . keyCode === 73 ) {
167163 //console.log('focus input');
168164 React . findDOMNode ( this . refs . postcssInputTextarea ) . focus ( ) ;
165+
166+ PlaygroundActions . keyboardActionFired ( ) ;
169167 }
170168 // o
171169 else if ( e . keyCode === 79 ) {
172170 //console.log('focus output');
173171 React . findDOMNode ( this . refs . postcssOutputTextarea ) . focus ( ) ;
172+
173+ PlaygroundActions . keyboardActionFired ( ) ;
174174 }
175175 }
176176 }
177177
178- _onStoreChange ( ) {
178+
179+ _onPlaygroundStoreChange ( ) {
179180 //console.log('change in PlaygroundStore');
180- this . setState ( getPlaygroundState ( this . props , this . state ) ) ;
181+ this . setState ( gatherPlaygroundStoreState ( this . props , this . state ) ) ;
182+ }
183+
184+ _onPlaygroundSettingsStoreChange ( ) {
185+ //console.log('change in PlaygroundSettingsStore');
186+ this . setState ( gatherPlaygroundSettingsStoreState ( this . props , this . state ) ) ;
181187 }
182188}
183189
184190PlaygroundApp . propTypes = {
185- // Defaults to true
186- initialShouldLiveReload : React . PropTypes . bool ,
187- initialPostcssInputText : React . PropTypes . string
191+ // ...
188192} ;
0 commit comments