@@ -2,13 +2,63 @@ import PropTypes from 'prop-types';
2
2
import React from 'react' ;
3
3
import InlineSVG from 'react-inlinesvg' ;
4
4
import classNames from 'classnames' ;
5
+ import { Console as ConsoleFeed } from 'console-feed' ;
6
+ import { CONSOLE_FEED_WITHOUT_ICONS , CONSOLE_FEED_LIGHT_STYLES , CONSOLE_FEED_DARK_STYLES , CONSOLE_FEED_CONTRAST_STYLES } from '../../../styles/components/_console-feed.scss' ;
7
+ import warnLightUrl from '../../../images/console-warn-light.svg' ;
8
+ import warnDarkUrl from '../../../images/console-warn-dark.svg' ;
9
+ import errorLightUrl from '../../../images/console-error-light.svg' ;
10
+ import errorDarkUrl from '../../../images/console-error-dark.svg' ;
11
+ import debugLightUrl from '../../../images/console-debug-light.svg' ;
12
+ import debugDarkUrl from '../../../images/console-debug-dark.svg' ;
13
+ import infoLightUrl from '../../../images/console-info-light.svg' ;
14
+ import infoDarkUrl from '../../../images/console-info-dark.svg' ;
5
15
6
16
const upArrowUrl = require ( '../../../images/up-arrow.svg' ) ;
7
17
const downArrowUrl = require ( '../../../images/down-arrow.svg' ) ;
8
18
9
19
class Console extends React . Component {
10
- componentDidUpdate ( ) {
20
+ componentDidUpdate ( prevProps ) {
11
21
this . consoleMessages . scrollTop = this . consoleMessages . scrollHeight ;
22
+ if ( this . props . theme !== prevProps . theme ) {
23
+ this . props . clearConsole ( ) ;
24
+ this . props . dispatchConsoleEvent ( this . props . consoleEvents ) ;
25
+ }
26
+ }
27
+
28
+ getConsoleFeedStyle ( theme , times ) {
29
+ const style = { } ;
30
+ const CONSOLE_FEED_LIGHT_ICONS = {
31
+ LOG_WARN_ICON : `url(${ warnLightUrl } )` ,
32
+ LOG_ERROR_ICON : `url(${ errorLightUrl } )` ,
33
+ LOG_DEBUG_ICON : `url(${ debugLightUrl } )` ,
34
+ LOG_INFO_ICON : `url(${ infoLightUrl } )`
35
+ } ;
36
+ const CONSOLE_FEED_DARK_ICONS = {
37
+ LOG_WARN_ICON : `url(${ warnDarkUrl } )` ,
38
+ LOG_ERROR_ICON : `url(${ errorDarkUrl } )` ,
39
+ LOG_DEBUG_ICON : `url(${ debugDarkUrl } )` ,
40
+ LOG_INFO_ICON : `url(${ infoDarkUrl } )`
41
+ } ;
42
+ if ( times > 1 ) {
43
+ Object . assign ( style , CONSOLE_FEED_WITHOUT_ICONS ) ;
44
+ }
45
+ switch ( theme ) {
46
+ case 'light' :
47
+ return Object . assign ( CONSOLE_FEED_LIGHT_STYLES , CONSOLE_FEED_LIGHT_ICONS , style ) ;
48
+ case 'dark' :
49
+ return Object . assign ( CONSOLE_FEED_DARK_STYLES , CONSOLE_FEED_DARK_ICONS , style ) ;
50
+ case 'contrast' :
51
+ return Object . assign ( CONSOLE_FEED_CONTRAST_STYLES , CONSOLE_FEED_DARK_ICONS , style ) ;
52
+ default :
53
+ return '' ;
54
+ }
55
+ }
56
+
57
+ formatData ( args ) {
58
+ if ( ! Array . isArray ( args ) ) {
59
+ return Array . of ( args ) ;
60
+ }
61
+ return args ;
12
62
}
13
63
14
64
render ( ) {
@@ -39,17 +89,25 @@ class Console extends React.Component {
39
89
</ div >
40
90
< div ref = { ( element ) => { this . consoleMessages = element ; } } className = "preview-console__messages" >
41
91
{ this . props . consoleEvents . map ( ( consoleEvent ) => {
42
- const { arguments : args , method } = consoleEvent ;
92
+ const { arguments : args , method, times } = consoleEvent ;
93
+ const { theme } = this . props ;
94
+ Object . assign ( consoleEvent , { data : this . formatData ( args ) } ) ;
43
95
if ( Object . keys ( args ) . length === 0 ) {
44
96
return (
45
- < div key = { consoleEvent . id } className = "preview-console__undefined " >
97
+ < div key = { consoleEvent . id } className = "preview-console__message preview-console__message--undefined " >
46
98
< span key = { `${ consoleEvent . id } -0` } > undefined</ span >
47
99
</ div >
48
100
) ;
49
101
}
50
102
return (
51
- < div key = { consoleEvent . id } className = { `preview-console__${ method } ` } >
52
- { Object . keys ( args ) . map ( key => < span key = { `${ consoleEvent . id } -${ key } ` } > { args [ key ] } </ span > ) }
103
+ < div key = { consoleEvent . id } className = { `preview-console__message preview-console__message--${ method } ` } >
104
+ { times > 1 &&
105
+ < div className = "preview-console__logged-times" > { times } </ div >
106
+ }
107
+ < ConsoleFeed
108
+ styles = { this . getConsoleFeedStyle ( theme , times ) }
109
+ logs = { Array . of ( consoleEvent ) }
110
+ />
53
111
</ div >
54
112
) ;
55
113
} ) }
@@ -67,7 +125,9 @@ Console.propTypes = {
67
125
isExpanded : PropTypes . bool . isRequired ,
68
126
collapseConsole : PropTypes . func . isRequired ,
69
127
expandConsole : PropTypes . func . isRequired ,
70
- clearConsole : PropTypes . func . isRequired
128
+ clearConsole : PropTypes . func . isRequired ,
129
+ dispatchConsoleEvent : PropTypes . func . isRequired ,
130
+ theme : PropTypes . string . isRequired
71
131
} ;
72
132
73
133
Console . defaultProps = {
0 commit comments