start to break out console into its own reducer

This commit is contained in:
Cassie Tarakajian 2017-01-09 18:09:23 -05:00
parent 1e670e9f9a
commit d1c1279061
8 changed files with 65 additions and 30 deletions

View file

@ -47,6 +47,7 @@ export const EXPAND_SIDEBAR = 'EXPAND_SIDEBAR';
export const COLLAPSE_SIDEBAR = 'COLLAPSE_SIDEBAR'; export const COLLAPSE_SIDEBAR = 'COLLAPSE_SIDEBAR';
export const CONSOLE_EVENT = 'CONSOLE_EVENT'; export const CONSOLE_EVENT = 'CONSOLE_EVENT';
export const CLEAR_CONSOLE = 'CLEAR_CONSOLE';
export const EXPAND_CONSOLE = 'EXPAND_CONSOLE'; export const EXPAND_CONSOLE = 'EXPAND_CONSOLE';
export const COLLAPSE_CONSOLE = 'COLLAPSE_CONSOLE'; export const COLLAPSE_CONSOLE = 'COLLAPSE_CONSOLE';

View file

@ -0,0 +1,14 @@
import * as ActionTypes from '../../../constants';
export function clearConsole() {
return {
type: ActionTypes.CLEAR_CONSOLE
};
}
export function dispatchConsoleEvent(...args) {
return {
type: ActionTypes.CONSOLE_EVENT,
event: args[0].data
};
}

View file

@ -61,13 +61,6 @@ export function resetSelectedFile(previousId) {
}; };
} }
export function dispatchConsoleEvent(...args) {
return {
type: ActionTypes.CONSOLE_EVENT,
event: args[0].data
};
}
export function newFile() { export function newFile() {
return { return {
type: ActionTypes.SHOW_MODAL type: ActionTypes.SHOW_MODAL

View file

@ -14,13 +14,8 @@ class Console extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
/**
* An array of React Elements that include previous console messages
* @type {Array}
*/
this.children = [];
this.appendConsoleEvent = this.appendConsoleEvent.bind(this); this.appendConsoleEvent = this.appendConsoleEvent.bind(this);
this.clearConsole = this.clearConsole.bind(this);
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
@ -41,11 +36,12 @@ class Console extends React.Component {
} }
} }
shouldComponentUpdate(nextProps) { // shouldComponentUpdate(nextProps) {
return (nextProps.consoleEvent !== this.props.consoleEvent) // return (nextProps.consoleEvent !== this.props.consoleEvent)
|| (nextProps.isPlaying && !this.props.isPlaying) // || (nextProps.isPlaying && !this.props.isPlaying)
|| (this.props.isExpanded !== nextProps.isExpanded); // || (this.props.isExpanded !== nextProps.isExpanded)
} // || ;
// }
componentDidUpdate() { componentDidUpdate() {
this.refs.console_messages.scrollTop = this.refs.console_messages.scrollHeight; this.refs.console_messages.scrollTop = this.refs.console_messages.scrollHeight;
@ -71,6 +67,10 @@ class Console extends React.Component {
this.children.push(nextChild); this.children.push(nextChild);
} }
clearConsole() {
this.children = [];
}
render() { render() {
const childrenToDisplay = this.children.slice(-consoleMax); const childrenToDisplay = this.children.slice(-consoleMax);
const consoleClass = classNames({ const consoleClass = classNames({
@ -82,12 +82,17 @@ class Console extends React.Component {
<div ref="console" className={consoleClass} role="main" tabIndex="0" title="console"> <div ref="console" className={consoleClass} role="main" tabIndex="0" title="console">
<div className="preview-console__header"> <div className="preview-console__header">
<h2 className="preview-console__header-title">console</h2> <h2 className="preview-console__header-title">console</h2>
<button className="preview-console__collapse" onClick={this.props.collapseConsole} aria-label="collapse console"> <div className="preview-console__header-buttons">
<InlineSVG src={downArrowUrl} /> <button className="preview-console__clear" onClick={this.clearConsole} aria-label="clear console">
</button> Clear
<button className="preview-console__expand" onClick={this.props.expandConsole} aria-label="expand console"> </button>
<InlineSVG src={upArrowUrl} /> <button className="preview-console__collapse" onClick={this.props.collapseConsole} aria-label="collapse console">
</button> <InlineSVG src={downArrowUrl} />
</button>
<button className="preview-console__expand" onClick={this.props.expandConsole} aria-label="expand console">
<InlineSVG src={upArrowUrl} />
</button>
</div>
</div> </div>
<div ref="console_messages" className="preview-console__messages"> <div ref="console_messages" className="preview-console__messages">
{childrenToDisplay} {childrenToDisplay}
@ -99,7 +104,7 @@ class Console extends React.Component {
} }
Console.propTypes = { Console.propTypes = {
consoleEvent: PropTypes.array, consoleLines: PropTypes.array,
isPlaying: PropTypes.bool.isRequired, isPlaying: PropTypes.bool.isRequired,
isExpanded: PropTypes.bool.isRequired, isExpanded: PropTypes.bool.isRequired,
collapseConsole: PropTypes.func.isRequired, collapseConsole: PropTypes.func.isRequired,

View file

@ -23,6 +23,7 @@ import * as EditorAccessibilityActions from '../actions/editorAccessibility';
import * as PreferencesActions from '../actions/preferences'; import * as PreferencesActions from '../actions/preferences';
import * as UserActions from '../../User/actions'; import * as UserActions from '../../User/actions';
import * as ToastActions from '../actions/toast'; import * as ToastActions from '../actions/toast';
import * as ConsoleActions from '../actions/console';
import { getHTMLFile } from '../reducers/files'; import { getHTMLFile } from '../reducers/files';
import SplitPane from 'react-split-pane'; import SplitPane from 'react-split-pane';
import Overlay from '../../App/components/Overlay'; import Overlay from '../../App/components/Overlay';
@ -323,7 +324,7 @@ class IDEView extends React.Component {
collapseSidebar={this.props.collapseSidebar} collapseSidebar={this.props.collapseSidebar}
/> />
<Console <Console
consoleEvent={this.props.ide.consoleEvent} consoleLines={this.props.console}
isPlaying={this.props.ide.isPlaying} isPlaying={this.props.ide.isPlaying}
isExpanded={this.props.ide.consoleIsExpanded} isExpanded={this.props.ide.consoleIsExpanded}
expandConsole={this.props.expandConsole} expandConsole={this.props.expandConsole}
@ -586,6 +587,7 @@ IDEView.propTypes = {
resetProject: PropTypes.func.isRequired, resetProject: PropTypes.func.isRequired,
closeForceAuthentication: PropTypes.func.isRequired, closeForceAuthentication: PropTypes.func.isRequired,
openForceAuthentication: PropTypes.func.isRequired, openForceAuthentication: PropTypes.func.isRequired,
console: PropTypes.array.isRequired
}; };
function mapStateToProps(state) { function mapStateToProps(state) {
@ -598,7 +600,8 @@ function mapStateToProps(state) {
editorAccessibility: state.editorAccessibility, editorAccessibility: state.editorAccessibility,
user: state.user, user: state.user,
project: state.project, project: state.project,
toast: state.toast toast: state.toast,
console: state.console
}; };
} }
@ -610,7 +613,8 @@ function mapDispatchToProps(dispatch) {
IDEActions, IDEActions,
PreferencesActions, PreferencesActions,
UserActions, UserActions,
ToastActions), ToastActions,
ConsoleActions),
dispatch); dispatch);
} }

View file

@ -0,0 +1,17 @@
import * as ActionTypes from '../../../constants';
const consoleMax = 200;
const initialState = [];
const console = (state = initialState, action) => {
switch (action.type) {
case ActionTypes.CONSOLE_EVENT:
return [...state, action.event].slice(-consoleMax);
case ActionTypes.CLEAR_CONSOLE:
return [];
default:
return state;
}
};
export default console;

View file

@ -3,7 +3,6 @@ import * as ActionTypes from '../../../constants';
const initialState = { const initialState = {
isPlaying: false, isPlaying: false,
isTextOutputPlaying: false, isTextOutputPlaying: false,
consoleEvent: [],
modalIsVisible: false, modalIsVisible: false,
sidebarIsExpanded: false, sidebarIsExpanded: false,
consoleIsExpanded: true, consoleIsExpanded: true,

View file

@ -7,6 +7,7 @@ import editorAccessibility from './modules/IDE/reducers/editorAccessibility';
import user from './modules/User/reducers'; import user from './modules/User/reducers';
import sketches from './modules/IDE/reducers/projects'; import sketches from './modules/IDE/reducers/projects';
import toast from './modules/IDE/reducers/toast'; import toast from './modules/IDE/reducers/toast';
import console from './modules/IDE/reducers/console';
import { reducer as form } from 'redux-form'; import { reducer as form } from 'redux-form';
const rootReducer = combineReducers({ const rootReducer = combineReducers({
@ -18,7 +19,8 @@ const rootReducer = combineReducers({
project, project,
sketches, sketches,
editorAccessibility, editorAccessibility,
toast toast,
console
}); });
export default rootReducer; export default rootReducer;