1
1
import PropTypes from 'prop-types' ;
2
- import React from 'react' ;
2
+ import React , { useEffect , useRef } from 'react' ;
3
+
3
4
import { bindActionCreators } from 'redux' ;
4
5
5
6
import { useSelector , useDispatch } from 'react-redux' ;
@@ -28,61 +29,64 @@ import DownArrowIcon from '../../../images/down-arrow.svg';
28
29
import * as IDEActions from '../../IDE/actions/ide' ;
29
30
import * as ConsoleActions from '../../IDE/actions/console' ;
30
31
31
- class ConsoleComponent extends React . Component {
32
- componentDidUpdate ( prevProps ) {
33
- this . consoleMessages . scrollTop = this . consoleMessages . scrollHeight ;
34
- if ( this . props . theme !== prevProps . theme ) {
35
- this . props . clearConsole ( ) ;
36
- this . props . dispatchConsoleEvent ( this . props . consoleEvents ) ;
37
- }
32
+ const useDidUpdate = ( callback , deps ) => {
33
+ const hasMount = useRef ( false ) ;
38
34
39
- if ( this . props . fontSize !== prevProps . fontSize ) {
40
- this . props . clearConsole ( ) ;
41
- this . props . dispatchConsoleEvent ( this . props . consoleEvents ) ;
35
+ useEffect ( ( ) => {
36
+ if ( hasMount . current ) {
37
+ callback ( ) ;
38
+ } else {
39
+ hasMount . current = true ;
42
40
}
41
+ } , deps ) ;
42
+ } ;
43
+
44
+ const getConsoleFeedStyle = ( theme , times , fontSize ) => {
45
+ const style = { } ;
46
+ const CONSOLE_FEED_LIGHT_ICONS = {
47
+ LOG_WARN_ICON : `url(${ warnLightUrl } )` ,
48
+ LOG_ERROR_ICON : `url(${ errorLightUrl } )` ,
49
+ LOG_DEBUG_ICON : `url(${ debugLightUrl } )` ,
50
+ LOG_INFO_ICON : `url(${ infoLightUrl } )`
51
+ } ;
52
+ const CONSOLE_FEED_DARK_ICONS = {
53
+ LOG_WARN_ICON : `url(${ warnDarkUrl } )` ,
54
+ LOG_ERROR_ICON : `url(${ errorDarkUrl } )` ,
55
+ LOG_DEBUG_ICON : `url(${ debugDarkUrl } )` ,
56
+ LOG_INFO_ICON : `url(${ infoDarkUrl } )`
57
+ } ;
58
+ const CONSOLE_FEED_CONTRAST_ICONS = {
59
+ LOG_WARN_ICON : `url(${ warnContrastUrl } )` ,
60
+ LOG_ERROR_ICON : `url(${ errorContrastUrl } )` ,
61
+ LOG_DEBUG_ICON : `url(${ debugContrastUrl } )` ,
62
+ LOG_INFO_ICON : `url(${ infoContrastUrl } )`
63
+ } ;
64
+ const CONSOLE_FEED_SIZES = {
65
+ TREENODE_LINE_HEIGHT : 1.2 ,
66
+ BASE_FONT_SIZE : fontSize ,
67
+ ARROW_FONT_SIZE : fontSize ,
68
+ LOG_ICON_WIDTH : fontSize ,
69
+ LOG_ICON_HEIGHT : 1.45 * fontSize ,
70
+ } ;
71
+
72
+ if ( times > 1 ) {
73
+ Object . assign ( style , CONSOLE_FEED_WITHOUT_ICONS ) ;
43
74
}
75
+ switch ( theme ) {
76
+ case 'light' :
77
+ return Object . assign ( CONSOLE_FEED_LIGHT_STYLES , CONSOLE_FEED_LIGHT_ICONS , CONSOLE_FEED_SIZES , style ) ;
78
+ case 'dark' :
79
+ return Object . assign ( CONSOLE_FEED_DARK_STYLES , CONSOLE_FEED_DARK_ICONS , CONSOLE_FEED_SIZES , style ) ;
80
+ case 'contrast' :
81
+ return Object . assign ( CONSOLE_FEED_CONTRAST_STYLES , CONSOLE_FEED_CONTRAST_ICONS , CONSOLE_FEED_SIZES , style ) ;
82
+ default :
83
+ return '' ;
84
+ }
85
+ } ;
44
86
45
- getConsoleFeedStyle ( theme , times ) {
46
- const style = { } ;
47
- const CONSOLE_FEED_LIGHT_ICONS = {
48
- LOG_WARN_ICON : `url(${ warnLightUrl } )` ,
49
- LOG_ERROR_ICON : `url(${ errorLightUrl } )` ,
50
- LOG_DEBUG_ICON : `url(${ debugLightUrl } )` ,
51
- LOG_INFO_ICON : `url(${ infoLightUrl } )`
52
- } ;
53
- const CONSOLE_FEED_DARK_ICONS = {
54
- LOG_WARN_ICON : `url(${ warnDarkUrl } )` ,
55
- LOG_ERROR_ICON : `url(${ errorDarkUrl } )` ,
56
- LOG_DEBUG_ICON : `url(${ debugDarkUrl } )` ,
57
- LOG_INFO_ICON : `url(${ infoDarkUrl } )`
58
- } ;
59
- const CONSOLE_FEED_CONTRAST_ICONS = {
60
- LOG_WARN_ICON : `url(${ warnContrastUrl } )` ,
61
- LOG_ERROR_ICON : `url(${ errorContrastUrl } )` ,
62
- LOG_DEBUG_ICON : `url(${ debugContrastUrl } )` ,
63
- LOG_INFO_ICON : `url(${ infoContrastUrl } )`
64
- } ;
65
- const CONSOLE_FEED_SIZES = {
66
- TREENODE_LINE_HEIGHT : 1.2 ,
67
- BASE_FONT_SIZE : this . props . fontSize ,
68
- ARROW_FONT_SIZE : this . props . fontSize ,
69
- LOG_ICON_WIDTH : this . props . fontSize ,
70
- LOG_ICON_HEIGHT : 1.45 * this . props . fontSize ,
71
- } ;
72
-
73
- if ( times > 1 ) {
74
- Object . assign ( style , CONSOLE_FEED_WITHOUT_ICONS ) ;
75
- }
76
- switch ( theme ) {
77
- case 'light' :
78
- return Object . assign ( CONSOLE_FEED_LIGHT_STYLES , CONSOLE_FEED_LIGHT_ICONS , CONSOLE_FEED_SIZES , style ) ;
79
- case 'dark' :
80
- return Object . assign ( CONSOLE_FEED_DARK_STYLES , CONSOLE_FEED_DARK_ICONS , CONSOLE_FEED_SIZES , style ) ;
81
- case 'contrast' :
82
- return Object . assign ( CONSOLE_FEED_CONTRAST_STYLES , CONSOLE_FEED_CONTRAST_ICONS , CONSOLE_FEED_SIZES , style ) ;
83
- default :
84
- return '' ;
85
- }
87
+ class ConsoleComponent extends React . Component {
88
+ componentDidUpdate ( prevProps ) {
89
+ this . consoleMessages . scrollTop = this . consoleMessages . scrollHeight ;
86
90
}
87
91
88
92
render ( ) {
@@ -91,6 +95,8 @@ class ConsoleComponent extends React.Component {
91
95
'preview-console--collapsed' : ! this . props . isExpanded
92
96
} ) ;
93
97
98
+ console . log ( this . props . isExpanded ) ;
99
+
94
100
return (
95
101
< section className = { consoleClass } >
96
102
< header className = "preview-console__header" >
@@ -126,7 +132,7 @@ class ConsoleComponent extends React.Component {
126
132
</ div >
127
133
}
128
134
< ConsoleFeed
129
- styles = { this . getConsoleFeedStyle ( theme , times ) }
135
+ styles = { getConsoleFeedStyle ( theme , times , this . props . fontSize ) }
130
136
logs = { [ consoleEvent ] }
131
137
/>
132
138
</ div >
@@ -147,7 +153,6 @@ ConsoleComponent.propTypes = {
147
153
collapseConsole : PropTypes . func . isRequired ,
148
154
expandConsole : PropTypes . func . isRequired ,
149
155
clearConsole : PropTypes . func . isRequired ,
150
- dispatchConsoleEvent : PropTypes . func . isRequired ,
151
156
theme : PropTypes . string . isRequired ,
152
157
fontSize : PropTypes . number . isRequired
153
158
} ;
@@ -165,6 +170,11 @@ const Console = () => {
165
170
collapseConsole, expandConsole, clearConsole, dispatchConsoleEvent
166
171
} = bindActionCreators ( { ...IDEActions , ...ConsoleActions } , useDispatch ( ) ) ;
167
172
173
+ useDidUpdate ( ( ) => {
174
+ clearConsole ( ) ;
175
+ dispatchConsoleEvent ( consoleEvents ) ;
176
+ } , [ theme , fontSize ] ) ;
177
+
168
178
return (
169
179
< ConsoleComponent
170
180
consoleEvents = { consoleEvents }
@@ -179,19 +189,4 @@ const Console = () => {
179
189
) ;
180
190
} ;
181
191
182
- // const Console = connect(
183
- // state => ({
184
- // consoleEvents: state.console,
185
- // isExpanded: state.ide.consoleIsExpanded,
186
- // theme: state.preferences.theme,
187
- // fontSize: state.preferences.fontSize
188
- // }),
189
- // dispatch => ({
190
- // collapseConsole: () => dispatch(IDEActions.collapseConsole()),
191
- // expandConsole: () => dispatch(IDEActions.expandConsole()),
192
- // clearConsole: () => dispatch(ConsoleActions.clearConsole()),
193
- // dispatchConsoleEvent: msgs => dispatch(ConsoleActions.dispatchConsoleEvent(msgs)),
194
- // })
195
- // )(ConsoleComponent);
196
-
197
192
export default Console ;
0 commit comments