1
- import React , { useRef } from 'react' ;
1
+ import React , { useRef , useEffect } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import { withTranslation } from 'react-i18next' ;
4
4
@@ -23,40 +23,58 @@ import debugContrastUrl from '../../../images/console-debug-contrast.svg?byUrl';
23
23
import infoLightUrl from '../../../images/console-info-light.svg?byUrl' ;
24
24
import infoDarkUrl from '../../../images/console-info-dark.svg?byUrl' ;
25
25
import infoContrastUrl from '../../../images/console-info-contrast.svg?byUrl' ;
26
+ import ConsoleInput from './ConsoleInput' ;
27
+
28
+ import commandLightUrl from '../../../images/console-command-light.svg?byUrl' ;
29
+ import resultLightUrl from '../../../images/console-result-light.svg?byUrl' ;
30
+ import commandDarkUrl from '../../../images/console-command-dark.svg?byUrl' ;
31
+ import resultDarkUrl from '../../../images/console-result-dark.svg?byUrl' ;
32
+ import commandContrastUrl from '../../../images/console-command-contrast.svg?byUrl' ;
33
+ import resultContrastUrl from '../../../images/console-result-contrast.svg?byUrl' ;
26
34
27
35
import UpArrowIcon from '../../../images/up-arrow.svg' ;
28
36
import DownArrowIcon from '../../../images/down-arrow.svg' ;
29
37
30
38
import * as IDEActions from '../../IDE/actions/ide' ;
31
39
import * as ConsoleActions from '../../IDE/actions/console' ;
32
- import { useDidUpdate } from '../../../utils/custom-hooks' ;
40
+ import { useDidUpdate } from '../hooks/custom-hooks' ;
41
+ import useHandleMessageEvent from '../hooks/useHandleMessageEvent' ;
42
+ import { listen } from '../../../utils/dispatcher' ;
33
43
34
44
const getConsoleFeedStyle = ( theme , times , fontSize ) => {
35
- const style = { } ;
45
+ const style = {
46
+ BASE_FONT_FAMILY : 'Inconsolata, monospace' ,
47
+ } ;
36
48
const CONSOLE_FEED_LIGHT_ICONS = {
37
49
LOG_WARN_ICON : `url(${ warnLightUrl } )` ,
38
50
LOG_ERROR_ICON : `url(${ errorLightUrl } )` ,
39
51
LOG_DEBUG_ICON : `url(${ debugLightUrl } )` ,
40
- LOG_INFO_ICON : `url(${ infoLightUrl } )`
52
+ LOG_INFO_ICON : `url(${ infoLightUrl } )` ,
53
+ LOG_COMMAND_ICON : `url(${ commandLightUrl } )` ,
54
+ LOG_RESULT_ICON : `url(${ resultLightUrl } )`
41
55
} ;
42
56
const CONSOLE_FEED_DARK_ICONS = {
43
57
LOG_WARN_ICON : `url(${ warnDarkUrl } )` ,
44
58
LOG_ERROR_ICON : `url(${ errorDarkUrl } )` ,
45
59
LOG_DEBUG_ICON : `url(${ debugDarkUrl } )` ,
46
- LOG_INFO_ICON : `url(${ infoDarkUrl } )`
60
+ LOG_INFO_ICON : `url(${ infoDarkUrl } )` ,
61
+ LOG_COMMAND_ICON : `url(${ commandDarkUrl } )` ,
62
+ LOG_RESULT_ICON : `url(${ resultDarkUrl } )`
47
63
} ;
48
64
const CONSOLE_FEED_CONTRAST_ICONS = {
49
65
LOG_WARN_ICON : `url(${ warnContrastUrl } )` ,
50
66
LOG_ERROR_ICON : `url(${ errorContrastUrl } )` ,
51
67
LOG_DEBUG_ICON : `url(${ debugContrastUrl } )` ,
52
- LOG_INFO_ICON : `url(${ infoContrastUrl } )`
68
+ LOG_INFO_ICON : `url(${ infoContrastUrl } )` ,
69
+ LOG_COMMAND_ICON : `url(${ commandContrastUrl } )` ,
70
+ LOG_RESULT_ICON : `url(${ resultContrastUrl } )`
53
71
} ;
54
72
const CONSOLE_FEED_SIZES = {
55
73
TREENODE_LINE_HEIGHT : 1.2 ,
56
74
BASE_FONT_SIZE : fontSize ,
57
75
ARROW_FONT_SIZE : fontSize ,
58
76
LOG_ICON_WIDTH : fontSize ,
59
- LOG_ICON_HEIGHT : 1.45 * fontSize ,
77
+ LOG_ICON_HEIGHT : 1.45 * fontSize
60
78
} ;
61
79
62
80
if ( times > 1 ) {
@@ -77,21 +95,25 @@ const getConsoleFeedStyle = (theme, times, fontSize) => {
77
95
const Console = ( { t } ) => {
78
96
const consoleEvents = useSelector ( state => state . console ) ;
79
97
const isExpanded = useSelector ( state => state . ide . consoleIsExpanded ) ;
98
+ const isPlaying = useSelector ( state => state . ide . isPlaying ) ;
80
99
const { theme, fontSize } = useSelector ( state => state . preferences ) ;
81
100
82
101
const {
83
102
collapseConsole, expandConsole, clearConsole, dispatchConsoleEvent
84
103
} = bindActionCreators ( { ...IDEActions , ...ConsoleActions } , useDispatch ( ) ) ;
85
104
86
- useDidUpdate ( ( ) => {
87
- clearConsole ( ) ;
88
- dispatchConsoleEvent ( consoleEvents ) ;
89
- } , [ theme , fontSize ] ) ;
90
-
91
105
const cm = useRef ( { } ) ;
92
106
93
107
useDidUpdate ( ( ) => { cm . current . scrollTop = cm . current . scrollHeight ; } ) ;
94
108
109
+ const handleMessageEvent = useHandleMessageEvent ( ) ;
110
+ useEffect ( ( ) => {
111
+ const unsubscribe = listen ( handleMessageEvent ) ;
112
+ return function cleanup ( ) {
113
+ unsubscribe ( ) ;
114
+ } ;
115
+ } ) ;
116
+
95
117
const consoleClass = classNames ( {
96
118
'preview-console' : true ,
97
119
'preview-console--collapsed' : ! isExpanded
@@ -117,26 +139,36 @@ const Console = ({ t }) => {
117
139
</ button >
118
140
</ div >
119
141
</ header >
120
- < div ref = { cm } className = "preview-console__messages" >
121
- { consoleEvents . map ( ( consoleEvent ) => {
122
- const { method, times } = consoleEvent ;
123
- return (
124
- < div key = { consoleEvent . id } className = { `preview-console__message preview-console__message--${ method } ` } >
125
- { times > 1 &&
126
- < div
127
- className = "preview-console__logged-times"
128
- style = { { fontSize, borderRadius : fontSize / 2 } }
129
- >
130
- { times }
142
+ < div className = "preview-console__body" >
143
+ < div ref = { cm } className = "preview-console__messages" >
144
+ { consoleEvents . map ( ( consoleEvent ) => {
145
+ const { method, times } = consoleEvent ;
146
+ return (
147
+ < div key = { consoleEvent . id } className = { `preview-console__message preview-console__message--${ method } ` } >
148
+ { times > 1 &&
149
+ < div
150
+ className = "preview-console__logged-times"
151
+ style = { { fontSize, borderRadius : fontSize / 2 } }
152
+ >
153
+ { times }
154
+ </ div >
155
+ }
156
+ < ConsoleFeed
157
+ styles = { getConsoleFeedStyle ( theme , times , fontSize ) }
158
+ logs = { [ consoleEvent ] }
159
+ key = { `${ consoleEvent . id } -${ theme } -${ fontSize } ` }
160
+ />
131
161
</ div >
132
- }
133
- < ConsoleFeed
134
- styles = { getConsoleFeedStyle ( theme , times , fontSize ) }
135
- logs = { [ consoleEvent ] }
136
- />
137
- </ div >
138
- ) ;
139
- } ) }
162
+ ) ;
163
+ } ) }
164
+ </ div >
165
+ { isExpanded && isPlaying &&
166
+ < ConsoleInput
167
+ theme = { theme }
168
+ dispatchConsoleEvent = { dispatchConsoleEvent }
169
+ fontSize = { fontSize }
170
+ />
171
+ }
140
172
</ div >
141
173
</ section >
142
174
) ;
0 commit comments