2020-07-21 00:08:03 +02:00
|
|
|
import React, { useRef } from 'react';
|
2020-08-19 11:40:05 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { withTranslation } from 'react-i18next';
|
2020-07-17 21:52:45 +02:00
|
|
|
|
2020-07-17 21:33:15 +02:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
|
2020-07-21 00:08:03 +02:00
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
2017-02-22 20:29:35 +01:00
|
|
|
import classNames from 'classnames';
|
2018-07-30 18:20:57 +02:00
|
|
|
import { Console as ConsoleFeed } from 'console-feed';
|
2018-10-05 21:35:31 +02:00
|
|
|
import {
|
|
|
|
CONSOLE_FEED_WITHOUT_ICONS, CONSOLE_FEED_LIGHT_STYLES,
|
|
|
|
CONSOLE_FEED_DARK_STYLES, CONSOLE_FEED_CONTRAST_STYLES
|
|
|
|
} from '../../../styles/components/_console-feed.scss';
|
2020-04-30 00:34:37 +02:00
|
|
|
import warnLightUrl from '../../../images/console-warn-light.svg?byUrl';
|
|
|
|
import warnDarkUrl from '../../../images/console-warn-dark.svg?byUrl';
|
|
|
|
import warnContrastUrl from '../../../images/console-warn-contrast.svg?byUrl';
|
|
|
|
import errorLightUrl from '../../../images/console-error-light.svg?byUrl';
|
|
|
|
import errorDarkUrl from '../../../images/console-error-dark.svg?byUrl';
|
|
|
|
import errorContrastUrl from '../../../images/console-error-contrast.svg?byUrl';
|
|
|
|
import debugLightUrl from '../../../images/console-debug-light.svg?byUrl';
|
|
|
|
import debugDarkUrl from '../../../images/console-debug-dark.svg?byUrl';
|
|
|
|
import debugContrastUrl from '../../../images/console-debug-contrast.svg?byUrl';
|
|
|
|
import infoLightUrl from '../../../images/console-info-light.svg?byUrl';
|
|
|
|
import infoDarkUrl from '../../../images/console-info-dark.svg?byUrl';
|
|
|
|
import infoContrastUrl from '../../../images/console-info-contrast.svg?byUrl';
|
2017-02-22 20:29:35 +01:00
|
|
|
|
2020-04-30 00:34:37 +02:00
|
|
|
import UpArrowIcon from '../../../images/up-arrow.svg';
|
|
|
|
import DownArrowIcon from '../../../images/down-arrow.svg';
|
2016-07-18 01:06:43 +02:00
|
|
|
|
2020-07-15 23:17:01 +02:00
|
|
|
import * as IDEActions from '../../IDE/actions/ide';
|
|
|
|
import * as ConsoleActions from '../../IDE/actions/console';
|
2020-07-21 00:08:03 +02:00
|
|
|
import { useDidUpdate } from '../../../utils/custom-hooks';
|
2020-07-17 21:52:45 +02:00
|
|
|
|
|
|
|
const getConsoleFeedStyle = (theme, times, fontSize) => {
|
|
|
|
const style = {};
|
|
|
|
const CONSOLE_FEED_LIGHT_ICONS = {
|
|
|
|
LOG_WARN_ICON: `url(${warnLightUrl})`,
|
|
|
|
LOG_ERROR_ICON: `url(${errorLightUrl})`,
|
|
|
|
LOG_DEBUG_ICON: `url(${debugLightUrl})`,
|
|
|
|
LOG_INFO_ICON: `url(${infoLightUrl})`
|
|
|
|
};
|
|
|
|
const CONSOLE_FEED_DARK_ICONS = {
|
|
|
|
LOG_WARN_ICON: `url(${warnDarkUrl})`,
|
|
|
|
LOG_ERROR_ICON: `url(${errorDarkUrl})`,
|
|
|
|
LOG_DEBUG_ICON: `url(${debugDarkUrl})`,
|
|
|
|
LOG_INFO_ICON: `url(${infoDarkUrl})`
|
|
|
|
};
|
|
|
|
const CONSOLE_FEED_CONTRAST_ICONS = {
|
|
|
|
LOG_WARN_ICON: `url(${warnContrastUrl})`,
|
|
|
|
LOG_ERROR_ICON: `url(${errorContrastUrl})`,
|
|
|
|
LOG_DEBUG_ICON: `url(${debugContrastUrl})`,
|
|
|
|
LOG_INFO_ICON: `url(${infoContrastUrl})`
|
|
|
|
};
|
|
|
|
const CONSOLE_FEED_SIZES = {
|
|
|
|
TREENODE_LINE_HEIGHT: 1.2,
|
|
|
|
BASE_FONT_SIZE: fontSize,
|
|
|
|
ARROW_FONT_SIZE: fontSize,
|
|
|
|
LOG_ICON_WIDTH: fontSize,
|
|
|
|
LOG_ICON_HEIGHT: 1.45 * fontSize,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (times > 1) {
|
|
|
|
Object.assign(style, CONSOLE_FEED_WITHOUT_ICONS);
|
2018-07-30 18:20:57 +02:00
|
|
|
}
|
2020-07-17 21:52:45 +02:00
|
|
|
switch (theme) {
|
|
|
|
case 'light':
|
|
|
|
return Object.assign(CONSOLE_FEED_LIGHT_STYLES, CONSOLE_FEED_LIGHT_ICONS, CONSOLE_FEED_SIZES, style);
|
|
|
|
case 'dark':
|
|
|
|
return Object.assign(CONSOLE_FEED_DARK_STYLES, CONSOLE_FEED_DARK_ICONS, CONSOLE_FEED_SIZES, style);
|
|
|
|
case 'contrast':
|
|
|
|
return Object.assign(CONSOLE_FEED_CONTRAST_STYLES, CONSOLE_FEED_CONTRAST_ICONS, CONSOLE_FEED_SIZES, style);
|
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
2018-07-30 18:20:57 +02:00
|
|
|
|
2020-08-19 11:40:05 +02:00
|
|
|
const Console = ({ t }) => {
|
2020-07-15 23:26:34 +02:00
|
|
|
const consoleEvents = useSelector(state => state.console);
|
2020-07-20 22:56:35 +02:00
|
|
|
const isExpanded = useSelector(state => state.ide.consoleIsExpanded);
|
2020-07-15 23:26:34 +02:00
|
|
|
const { theme, fontSize } = useSelector(state => state.preferences);
|
|
|
|
|
2020-07-17 21:33:15 +02:00
|
|
|
const {
|
|
|
|
collapseConsole, expandConsole, clearConsole, dispatchConsoleEvent
|
|
|
|
} = bindActionCreators({ ...IDEActions, ...ConsoleActions }, useDispatch());
|
2020-07-15 23:26:34 +02:00
|
|
|
|
2020-07-17 21:52:45 +02:00
|
|
|
useDidUpdate(() => {
|
|
|
|
clearConsole();
|
|
|
|
dispatchConsoleEvent(consoleEvents);
|
2020-07-21 00:02:30 +02:00
|
|
|
}, [theme, fontSize]);
|
2020-07-17 21:52:45 +02:00
|
|
|
|
2020-07-20 23:37:50 +02:00
|
|
|
const cm = useRef({});
|
2020-08-17 21:55:34 +02:00
|
|
|
|
|
|
|
useDidUpdate(() => { cm.current.scrollTop = cm.current.scrollHeight; });
|
2020-07-20 23:37:50 +02:00
|
|
|
|
2020-07-20 22:56:35 +02:00
|
|
|
const consoleClass = classNames({
|
|
|
|
'preview-console': true,
|
2020-07-21 00:02:30 +02:00
|
|
|
'preview-console--collapsed': !isExpanded
|
2020-07-20 22:56:35 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 23:26:34 +02:00
|
|
|
return (
|
2020-07-20 22:56:35 +02:00
|
|
|
<section className={consoleClass} >
|
|
|
|
<header className="preview-console__header">
|
2020-08-19 11:40:05 +02:00
|
|
|
<h2 className="preview-console__header-title">{t('Console.Title')}</h2>
|
2020-07-20 22:56:35 +02:00
|
|
|
<div className="preview-console__header-buttons">
|
2020-08-19 11:40:05 +02:00
|
|
|
<button className="preview-console__clear" onClick={clearConsole} aria-label={t('Console.ClearARIA')}>
|
|
|
|
{t('Console.Clear')}
|
2020-07-20 22:56:35 +02:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className="preview-console__collapse"
|
|
|
|
onClick={collapseConsole}
|
2020-08-19 11:40:05 +02:00
|
|
|
aria-label={t('Console.CloseARIA')}
|
2020-07-20 22:56:35 +02:00
|
|
|
>
|
|
|
|
<DownArrowIcon focusable="false" aria-hidden="true" />
|
|
|
|
</button>
|
2020-08-19 11:40:05 +02:00
|
|
|
<button className="preview-console__expand" onClick={expandConsole} aria-label={t('Console.OpenARIA')} >
|
2020-07-20 22:56:35 +02:00
|
|
|
<UpArrowIcon focusable="false" aria-hidden="true" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</header>
|
2020-07-20 23:37:50 +02:00
|
|
|
<div ref={cm} className="preview-console__messages">
|
2020-07-20 22:56:35 +02:00
|
|
|
{consoleEvents.map((consoleEvent) => {
|
|
|
|
const { method, times } = consoleEvent;
|
|
|
|
return (
|
|
|
|
<div key={consoleEvent.id} className={`preview-console__message preview-console__message--${method}`}>
|
|
|
|
{ times > 1 &&
|
|
|
|
<div
|
|
|
|
className="preview-console__logged-times"
|
|
|
|
style={{ fontSize, borderRadius: fontSize / 2 }}
|
|
|
|
>
|
|
|
|
{times}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
<ConsoleFeed
|
|
|
|
styles={getConsoleFeedStyle(theme, times, fontSize)}
|
|
|
|
logs={[consoleEvent]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</section>
|
2020-07-15 23:26:34 +02:00
|
|
|
);
|
|
|
|
};
|
2020-07-17 21:33:15 +02:00
|
|
|
|
2020-08-19 11:40:05 +02:00
|
|
|
Console.propTypes = {
|
|
|
|
t: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2020-07-20 22:56:35 +02:00
|
|
|
|
2020-08-19 11:40:05 +02:00
|
|
|
export default withTranslation()(Console);
|