2018-02-07 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-07-21 04:33:41 +00:00
|
|
|
import InlineSVG from 'react-inlinesvg';
|
2017-02-22 19:29:35 +00:00
|
|
|
import classNames from 'classnames';
|
2018-07-30 16:20:57 +00:00
|
|
|
import { Console as ConsoleFeed } from 'console-feed';
|
|
|
|
import { CONSOLE_FEED_WITHOUT_ICONS, CONSOLE_FEED_LIGHT_STYLES, CONSOLE_FEED_DARK_STYLES, CONSOLE_FEED_CONTRAST_STYLES } from '../../../styles/components/_console-feed.scss';
|
|
|
|
import warnLightUrl from '../../../images/console-warn-light.svg';
|
|
|
|
import warnDarkUrl from '../../../images/console-warn-dark.svg';
|
|
|
|
import errorLightUrl from '../../../images/console-error-light.svg';
|
|
|
|
import errorDarkUrl from '../../../images/console-error-dark.svg';
|
|
|
|
import debugLightUrl from '../../../images/console-debug-light.svg';
|
|
|
|
import debugDarkUrl from '../../../images/console-debug-dark.svg';
|
|
|
|
import infoLightUrl from '../../../images/console-info-light.svg';
|
|
|
|
import infoDarkUrl from '../../../images/console-info-dark.svg';
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2016-07-21 04:33:41 +00:00
|
|
|
const upArrowUrl = require('../../../images/up-arrow.svg');
|
|
|
|
const downArrowUrl = require('../../../images/down-arrow.svg');
|
2016-07-17 23:06:43 +00:00
|
|
|
|
|
|
|
class Console extends React.Component {
|
2018-07-30 16:20:57 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
2017-02-22 19:29:35 +00:00
|
|
|
this.consoleMessages.scrollTop = this.consoleMessages.scrollHeight;
|
2018-07-30 16:20:57 +00:00
|
|
|
if (this.props.theme !== prevProps.theme) {
|
|
|
|
this.props.clearConsole();
|
|
|
|
this.props.dispatchConsoleEvent(this.props.consoleEvents);
|
|
|
|
}
|
2018-09-12 05:00:29 +00:00
|
|
|
|
2018-09-13 02:00:19 +00:00
|
|
|
if (this.props.fontSize !== prevProps.fontSize) {
|
2018-09-12 05:00:29 +00:00
|
|
|
this.props.clearConsole();
|
|
|
|
this.props.dispatchConsoleEvent(this.props.consoleEvents);
|
|
|
|
}
|
2018-07-30 16:20:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getConsoleFeedStyle(theme, times) {
|
|
|
|
const style = {};
|
|
|
|
const CONSOLE_FEED_LIGHT_ICONS = {
|
|
|
|
LOG_WARN_ICON: `url(${warnLightUrl})`,
|
|
|
|
LOG_ERROR_ICON: `url(${errorLightUrl})`,
|
|
|
|
LOG_DEBUG_ICON: `url(${debugLightUrl})`,
|
2018-09-13 18:14:58 +00:00
|
|
|
LOG_INFO_ICON: `url(${infoLightUrl})`
|
2018-07-30 16:20:57 +00:00
|
|
|
};
|
|
|
|
const CONSOLE_FEED_DARK_ICONS = {
|
|
|
|
LOG_WARN_ICON: `url(${warnDarkUrl})`,
|
|
|
|
LOG_ERROR_ICON: `url(${errorDarkUrl})`,
|
|
|
|
LOG_DEBUG_ICON: `url(${debugDarkUrl})`,
|
2018-09-13 18:14:58 +00:00
|
|
|
LOG_INFO_ICON: `url(${infoDarkUrl})`
|
2018-09-13 15:50:07 +00:00
|
|
|
};
|
2018-09-13 18:14:58 +00:00
|
|
|
const CONSOLE_FEED_SIZES = {
|
2018-09-23 06:56:39 +00:00
|
|
|
TREENODE_LINE_HEIGHT: 1.2,
|
2018-09-14 16:44:41 +00:00
|
|
|
BASE_FONT_SIZE: this.props.fontSize,
|
2018-09-13 18:14:58 +00:00
|
|
|
ARROW_FONT_SIZE: this.props.fontSize,
|
2018-09-14 16:44:41 +00:00
|
|
|
LOG_ICON_WIDTH: this.props.fontSize,
|
2018-09-23 06:56:39 +00:00
|
|
|
LOG_ICON_HEIGHT: 1.45 * this.props.fontSize,
|
2018-07-30 16:20:57 +00:00
|
|
|
};
|
2018-09-13 15:50:07 +00:00
|
|
|
|
2018-07-30 16:20:57 +00:00
|
|
|
if (times > 1) {
|
|
|
|
Object.assign(style, CONSOLE_FEED_WITHOUT_ICONS);
|
|
|
|
}
|
|
|
|
switch (theme) {
|
|
|
|
case 'light':
|
2018-09-13 18:14:58 +00:00
|
|
|
return Object.assign(CONSOLE_FEED_LIGHT_STYLES, CONSOLE_FEED_LIGHT_ICONS, CONSOLE_FEED_SIZES, style);
|
2018-07-30 16:20:57 +00:00
|
|
|
case 'dark':
|
2018-09-13 18:14:58 +00:00
|
|
|
return Object.assign(CONSOLE_FEED_DARK_STYLES, CONSOLE_FEED_DARK_ICONS, CONSOLE_FEED_SIZES, style);
|
2018-07-30 16:20:57 +00:00
|
|
|
case 'contrast':
|
2018-09-13 18:14:58 +00:00
|
|
|
return Object.assign(CONSOLE_FEED_CONTRAST_STYLES, CONSOLE_FEED_DARK_ICONS, CONSOLE_FEED_SIZES, style);
|
2018-07-30 16:20:57 +00:00
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formatData(args) {
|
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
return Array.of(args);
|
|
|
|
}
|
|
|
|
return args;
|
2016-07-21 04:05:47 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 00:49:57 +00:00
|
|
|
render() {
|
2016-07-21 04:33:41 +00:00
|
|
|
const consoleClass = classNames({
|
|
|
|
'preview-console': true,
|
|
|
|
'preview-console--collapsed': !this.props.isExpanded
|
|
|
|
});
|
2016-07-17 23:06:43 +00:00
|
|
|
|
|
|
|
return (
|
2018-05-09 02:10:52 +00:00
|
|
|
<div className={consoleClass} role="main" title="console">
|
2016-07-21 04:33:41 +00:00
|
|
|
<div className="preview-console__header">
|
2017-01-11 19:13:49 +00:00
|
|
|
<h2 className="preview-console__header-title">Console</h2>
|
2017-01-09 23:09:23 +00:00
|
|
|
<div className="preview-console__header-buttons">
|
2017-02-22 19:29:35 +00:00
|
|
|
<button className="preview-console__clear" onClick={this.props.clearConsole} aria-label="clear console">
|
2017-01-09 23:09:23 +00:00
|
|
|
Clear
|
2017-02-22 19:29:35 +00:00
|
|
|
</button>
|
2017-06-06 02:33:32 +00:00
|
|
|
<button
|
|
|
|
className="preview-console__collapse"
|
|
|
|
onClick={this.props.collapseConsole}
|
|
|
|
aria-label="collapse console"
|
|
|
|
>
|
2017-01-09 23:09:23 +00:00
|
|
|
<InlineSVG src={downArrowUrl} />
|
|
|
|
</button>
|
|
|
|
<button className="preview-console__expand" onClick={this.props.expandConsole} aria-label="expand console">
|
|
|
|
<InlineSVG src={upArrowUrl} />
|
|
|
|
</button>
|
|
|
|
</div>
|
2016-07-21 04:33:41 +00:00
|
|
|
</div>
|
2017-02-22 19:29:35 +00:00
|
|
|
<div ref={(element) => { this.consoleMessages = element; }} className="preview-console__messages">
|
|
|
|
{this.props.consoleEvents.map((consoleEvent) => {
|
2018-07-30 16:20:57 +00:00
|
|
|
const { arguments: args, method, times } = consoleEvent;
|
|
|
|
const { theme } = this.props;
|
|
|
|
Object.assign(consoleEvent, { data: this.formatData(args) });
|
2017-01-11 19:13:49 +00:00
|
|
|
if (Object.keys(args).length === 0) {
|
|
|
|
return (
|
2018-07-30 16:20:57 +00:00
|
|
|
<div key={consoleEvent.id} className="preview-console__message preview-console__message--undefined">
|
2018-05-05 00:22:39 +00:00
|
|
|
<span key={`${consoleEvent.id}-0`}>undefined</span>
|
2017-01-11 19:13:49 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
2018-07-30 16:20:57 +00:00
|
|
|
<div key={consoleEvent.id} className={`preview-console__message preview-console__message--${method}`}>
|
|
|
|
{ times > 1 &&
|
2018-09-16 08:57:05 +00:00
|
|
|
<div className="preview-console__logged-times" style={{ fontSize: this.props.fontSize, borderRadius: this.props.fontSize / 2 }}>{times}</div>
|
2018-07-30 16:20:57 +00:00
|
|
|
}
|
|
|
|
<ConsoleFeed
|
|
|
|
styles={this.getConsoleFeedStyle(theme, times)}
|
|
|
|
logs={Array.of(consoleEvent)}
|
|
|
|
/>
|
2017-01-11 19:13:49 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
2016-07-21 04:05:47 +00:00
|
|
|
</div>
|
2016-07-17 23:06:43 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.propTypes = {
|
2017-02-22 19:29:35 +00:00
|
|
|
consoleEvents: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
method: PropTypes.string.isRequired,
|
|
|
|
args: PropTypes.arrayOf(PropTypes.string)
|
|
|
|
})),
|
2016-07-21 04:33:41 +00:00
|
|
|
isExpanded: PropTypes.bool.isRequired,
|
|
|
|
collapseConsole: PropTypes.func.isRequired,
|
2016-10-06 17:01:48 +00:00
|
|
|
expandConsole: PropTypes.func.isRequired,
|
2018-07-30 16:20:57 +00:00
|
|
|
clearConsole: PropTypes.func.isRequired,
|
|
|
|
dispatchConsoleEvent: PropTypes.func.isRequired,
|
2018-09-12 05:24:05 +00:00
|
|
|
theme: PropTypes.string.isRequired,
|
2018-09-13 02:12:50 +00:00
|
|
|
fontSize: PropTypes.number.isRequired
|
2016-07-17 23:06:43 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
Console.defaultProps = {
|
|
|
|
consoleEvents: []
|
|
|
|
};
|
|
|
|
|
2016-07-17 23:06:43 +00:00
|
|
|
export default Console;
|