@@ -4,52 +4,68 @@ import classnames from 'classnames';
4
4
5
5
import '../../css/playground.css!' ;
6
6
import PlaygroundStore from '../stores/PlaygroundStore' ;
7
+ import PlaygroundSettingsStore from '../stores/PlaygroundSettingsStore' ;
7
8
import PlaygroundActions from '../actions/PlaygroundActions' ;
8
9
9
10
11
+ import PlaygroundHeader from './PlaygroundHeader' ;
10
12
import EditorTextarea from './EditorTextarea' ;
11
13
12
14
13
- function getPlaygroundState ( props , state ) {
15
+
16
+ function gatherPlaygroundStoreState ( props , state ) {
14
17
var newOutputResult = PlaygroundStore . getOutputResult ( ) ;
15
18
16
19
return {
20
+ postcssInputText : PlaygroundStore . getInputText ( ) ,
17
21
postcssOutputResult : newOutputResult ,
18
22
// 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 ( )
20
32
} ;
21
33
}
22
34
23
35
export default class PlaygroundApp extends React . Component {
24
36
constructor ( props ) {
25
37
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
+ ) ;
30
42
}
31
43
32
44
componentDidMount ( ) {
33
- PlaygroundStore . addChangeListener ( this . _onStoreChange . bind ( this ) ) ;
45
+ PlaygroundStore . addChangeListener ( this . _onPlaygroundStoreChange . bind ( this ) ) ;
46
+ PlaygroundSettingsStore . addChangeListener ( this . _onPlaygroundSettingsStoreChange . bind ( this ) ) ;
34
47
35
48
document . addEventListener ( 'keyup' , this . _handleKeyUp . bind ( this ) ) ;
49
+
50
+ // Initialize the application
51
+ PlaygroundActions . init ( ) ;
36
52
}
37
53
38
54
componentWillUnmount ( ) {
39
- PlaygroundStore . removeChangeListener ( this . _onStoreChange ) ;
55
+ PlaygroundStore . removeChangeListener ( this . _onPlaygroundStoreChange . bind ( this ) ) ;
56
+ PlaygroundSettingsStore . removeChangeListener ( this . _onPlaygroundSettingsStoreChange . bind ( this ) ) ;
40
57
41
58
document . removeEventListener ( 'keyup' , this . _handleKeyUp . bind ( this ) ) ;
42
59
}
43
60
44
61
render ( ) {
45
62
//console.log('render', this.state);
46
63
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' ) ;
50
66
51
67
var parsingErrorMarkup ;
52
- if ( this . state . postcssOutputResult . error ) {
68
+ if ( this . state . postcssOutputResult . get ( ' error' ) ) {
53
69
parsingErrorMarkup = (
54
70
React . createElement ( "div" , {
55
71
className : "postcss-editor-pane-error" ,
@@ -60,40 +76,23 @@ export default class PlaygroundApp extends React.Component {
60
76
React . createElement ( "div" , {
61
77
className : "postcss-editor-pane-error-message"
62
78
} ,
63
- this . state . postcssOutputResult . error . toString ( )
79
+ this . state . postcssOutputResult . get ( ' error' ) . toString ( )
64
80
)
65
81
)
66
82
) ;
67
83
}
68
84
85
+
86
+ var tabWidthStyleValue = this . state . tabWidth === 'inherit' ? this . state . tabWidth : this . state . tabWidth + 'ch' ;
87
+
69
88
return (
70
89
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 }
97
96
) ,
98
97
99
98
React . createElement ( "div" , { className : "postcss-editor-area" } ,
@@ -107,7 +106,11 @@ export default class PlaygroundApp extends React.Component {
107
106
ref : "postcssInputTextarea" ,
108
107
className : "postcss-textarea" ,
109
108
onChange : this . _onInputChanged . bind ( this ) ,
110
- value : this . state . postcssInputText }
109
+ value : this . state . postcssInputText ,
110
+
111
+ style : {
112
+ tabSize : tabWidthStyleValue
113
+ } }
111
114
)
112
115
) ,
113
116
React . createElement ( "div" , { className : "postcss-editor-pane" } ,
@@ -119,7 +122,11 @@ export default class PlaygroundApp extends React.Component {
119
122
React . createElement ( EditorTextarea , {
120
123
ref : "postcssOutputTextarea" ,
121
124
className : classnames ( 'postcss-textarea' , { 'is-not-current' : doesInputHaveError } ) ,
122
- value : output }
125
+ value : output ,
126
+
127
+ style : {
128
+ tabSize : tabWidthStyleValue
129
+ } }
123
130
) ,
124
131
parsingErrorMarkup
125
132
)
@@ -128,36 +135,25 @@ export default class PlaygroundApp extends React.Component {
128
135
) ;
129
136
}
130
137
131
- _onSave ( ) {
132
- //console.log('saving');
133
- PlaygroundActions . updateInput ( this . state . postcssInputText ) ;
134
- }
135
-
136
138
_onInputChanged ( text ) {
137
139
//console.log('input changed');
138
- this . setState ( {
139
- postcssInputText : text
140
- } ) ;
140
+ PlaygroundActions . updateInput ( text ) ;
141
141
142
142
// Defaults to true if undefined
143
143
if ( this . state . shouldLiveReload ) {
144
- PlaygroundActions . updateInput ( text ) ;
144
+ PlaygroundActions . processInput ( ) ;
145
145
}
146
146
}
147
147
148
- _onLiveReloadCheckboxChanged ( ) {
149
- this . setState ( {
150
- shouldLiveReload : event . target . checked
151
- } ) ;
152
- }
153
-
154
148
_handleKeyUp ( e ) {
155
149
//console.log(e);
156
150
157
151
// escape
158
152
if ( e . keyCode === 27 ) {
159
153
// Unfocus/blur the currently focused elemnt
160
154
document . activeElement . blur ( ) ;
155
+
156
+ PlaygroundActions . keyboardActionFired ( ) ;
161
157
}
162
158
163
159
// If nothing is focused currently
@@ -166,23 +162,31 @@ export default class PlaygroundApp extends React.Component {
166
162
if ( e . keyCode === 73 ) {
167
163
//console.log('focus input');
168
164
React . findDOMNode ( this . refs . postcssInputTextarea ) . focus ( ) ;
165
+
166
+ PlaygroundActions . keyboardActionFired ( ) ;
169
167
}
170
168
// o
171
169
else if ( e . keyCode === 79 ) {
172
170
//console.log('focus output');
173
171
React . findDOMNode ( this . refs . postcssOutputTextarea ) . focus ( ) ;
172
+
173
+ PlaygroundActions . keyboardActionFired ( ) ;
174
174
}
175
175
}
176
176
}
177
177
178
- _onStoreChange ( ) {
178
+
179
+ _onPlaygroundStoreChange ( ) {
179
180
//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 ) ) ;
181
187
}
182
188
}
183
189
184
190
PlaygroundApp . propTypes = {
185
- // Defaults to true
186
- initialShouldLiveReload : React . PropTypes . bool ,
187
- initialPostcssInputText : React . PropTypes . string
191
+ // ...
188
192
} ;
0 commit comments