Skip to content

Commit 16bbeda

Browse files
authored
Merge pull request #667 from shinytang6/feature-2/interactive-console
Make console interactive
2 parents 835ec3c + ac5c376 commit 16bbeda

25 files changed

+560
-105
lines changed

client/components/useAsModal.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import styled from 'styled-components';
3-
import { useModalBehavior } from '../utils/custom-hooks';
3+
import { useModalBehavior } from '../modules/IDE/hooks/custom-hooks';
44

55
const BackgroundOverlay = styled.div`
66
position: fixed;
+13
Loading
+13
Loading
+13
Loading
+22
Loading

client/images/console-result-dark.svg

+22
Loading
+22
Loading

client/modules/App/App.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class App extends React.Component {
4141
render() {
4242
return (
4343
<div className="app">
44-
{false && this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
44+
{this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
4545
{this.props.children}
4646
</div>
4747
);

client/modules/IDE/components/Console.jsx

+63-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef } from 'react';
1+
import React, { useRef, useEffect } from 'react';
22
import PropTypes from 'prop-types';
33
import { withTranslation } from 'react-i18next';
44

@@ -23,40 +23,58 @@ import debugContrastUrl from '../../../images/console-debug-contrast.svg?byUrl';
2323
import infoLightUrl from '../../../images/console-info-light.svg?byUrl';
2424
import infoDarkUrl from '../../../images/console-info-dark.svg?byUrl';
2525
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';
2634

2735
import UpArrowIcon from '../../../images/up-arrow.svg';
2836
import DownArrowIcon from '../../../images/down-arrow.svg';
2937

3038
import * as IDEActions from '../../IDE/actions/ide';
3139
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';
3343

3444
const getConsoleFeedStyle = (theme, times, fontSize) => {
35-
const style = {};
45+
const style = {
46+
BASE_FONT_FAMILY: 'Inconsolata, monospace',
47+
};
3648
const CONSOLE_FEED_LIGHT_ICONS = {
3749
LOG_WARN_ICON: `url(${warnLightUrl})`,
3850
LOG_ERROR_ICON: `url(${errorLightUrl})`,
3951
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})`
4155
};
4256
const CONSOLE_FEED_DARK_ICONS = {
4357
LOG_WARN_ICON: `url(${warnDarkUrl})`,
4458
LOG_ERROR_ICON: `url(${errorDarkUrl})`,
4559
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})`
4763
};
4864
const CONSOLE_FEED_CONTRAST_ICONS = {
4965
LOG_WARN_ICON: `url(${warnContrastUrl})`,
5066
LOG_ERROR_ICON: `url(${errorContrastUrl})`,
5167
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})`
5371
};
5472
const CONSOLE_FEED_SIZES = {
5573
TREENODE_LINE_HEIGHT: 1.2,
5674
BASE_FONT_SIZE: fontSize,
5775
ARROW_FONT_SIZE: fontSize,
5876
LOG_ICON_WIDTH: fontSize,
59-
LOG_ICON_HEIGHT: 1.45 * fontSize,
77+
LOG_ICON_HEIGHT: 1.45 * fontSize
6078
};
6179

6280
if (times > 1) {
@@ -77,21 +95,25 @@ const getConsoleFeedStyle = (theme, times, fontSize) => {
7795
const Console = ({ t }) => {
7896
const consoleEvents = useSelector(state => state.console);
7997
const isExpanded = useSelector(state => state.ide.consoleIsExpanded);
98+
const isPlaying = useSelector(state => state.ide.isPlaying);
8099
const { theme, fontSize } = useSelector(state => state.preferences);
81100

82101
const {
83102
collapseConsole, expandConsole, clearConsole, dispatchConsoleEvent
84103
} = bindActionCreators({ ...IDEActions, ...ConsoleActions }, useDispatch());
85104

86-
useDidUpdate(() => {
87-
clearConsole();
88-
dispatchConsoleEvent(consoleEvents);
89-
}, [theme, fontSize]);
90-
91105
const cm = useRef({});
92106

93107
useDidUpdate(() => { cm.current.scrollTop = cm.current.scrollHeight; });
94108

109+
const handleMessageEvent = useHandleMessageEvent();
110+
useEffect(() => {
111+
const unsubscribe = listen(handleMessageEvent);
112+
return function cleanup() {
113+
unsubscribe();
114+
};
115+
});
116+
95117
const consoleClass = classNames({
96118
'preview-console': true,
97119
'preview-console--collapsed': !isExpanded
@@ -117,26 +139,36 @@ const Console = ({ t }) => {
117139
</button>
118140
</div>
119141
</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+
/>
131161
</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+
}
140172
</div>
141173
</section>
142174
);

0 commit comments

Comments
 (0)