2018-02-07 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2019-03-01 21:21:00 +00:00
|
|
|
import { connect } from 'react-redux';
|
2016-08-17 20:09:20 +00:00
|
|
|
import { Link } from 'react-router';
|
2017-02-22 19:29:35 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import InlineSVG from 'react-inlinesvg';
|
|
|
|
|
2019-03-01 21:21:00 +00:00
|
|
|
import * as IDEActions from '../actions/ide';
|
|
|
|
import * as preferenceActions from '../actions/preferences';
|
|
|
|
import * as projectActions from '../actions/project';
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
const playUrl = require('../../../images/play.svg');
|
|
|
|
const stopUrl = require('../../../images/stop.svg');
|
|
|
|
const preferencesUrl = require('../../../images/preferences.svg');
|
2016-10-26 21:40:10 +00:00
|
|
|
const editProjectNameUrl = require('../../../images/pencil.svg');
|
2016-06-23 22:29:55 +00:00
|
|
|
|
2016-08-15 16:42:13 +00:00
|
|
|
class Toolbar extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.handleKeyPress = this.handleKeyPress.bind(this);
|
|
|
|
this.handleProjectNameChange = this.handleProjectNameChange.bind(this);
|
|
|
|
}
|
2016-06-23 22:29:55 +00:00
|
|
|
|
2016-08-15 16:42:13 +00:00
|
|
|
handleKeyPress(event) {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
this.props.hideEditProjectName();
|
|
|
|
}
|
|
|
|
}
|
2016-07-31 02:46:48 +00:00
|
|
|
|
2016-08-15 16:42:13 +00:00
|
|
|
handleProjectNameChange(event) {
|
|
|
|
this.props.setProjectName(event.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
validateProjectName() {
|
2020-04-01 17:47:08 +00:00
|
|
|
if ((this.props.project.name.trim()).length === 0) {
|
2016-08-15 16:42:13 +00:00
|
|
|
this.props.setProjectName(this.originalProjectName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 22:54:14 +00:00
|
|
|
canEditProjectName() {
|
|
|
|
return (this.props.owner && this.props.owner.username
|
|
|
|
&& this.props.owner.username === this.props.currentUser)
|
|
|
|
|| !this.props.owner || !this.props.owner.username;
|
|
|
|
}
|
|
|
|
|
2016-08-15 16:42:13 +00:00
|
|
|
render() {
|
2017-02-22 19:29:35 +00:00
|
|
|
const playButtonClass = classNames({
|
2016-08-15 16:42:13 +00:00
|
|
|
'toolbar__play-button': true,
|
|
|
|
'toolbar__play-button--selected': this.props.isPlaying
|
|
|
|
});
|
2017-02-22 19:29:35 +00:00
|
|
|
const stopButtonClass = classNames({
|
2016-08-15 16:42:13 +00:00
|
|
|
'toolbar__stop-button': true,
|
|
|
|
'toolbar__stop-button--selected': !this.props.isPlaying
|
|
|
|
});
|
2017-02-22 19:29:35 +00:00
|
|
|
const preferencesButtonClass = classNames({
|
2016-08-15 16:42:13 +00:00
|
|
|
'toolbar__preferences-button': true,
|
|
|
|
'toolbar__preferences-button--selected': this.props.preferencesIsVisible
|
|
|
|
});
|
2017-02-22 19:29:35 +00:00
|
|
|
const nameContainerClass = classNames({
|
2016-08-15 16:42:13 +00:00
|
|
|
'toolbar__project-name-container': true,
|
|
|
|
'toolbar__project-name-container--editing': this.props.project.isEditingName
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="toolbar">
|
2016-08-16 01:09:47 +00:00
|
|
|
<button
|
|
|
|
className="toolbar__play-sketch-button"
|
2018-03-01 18:28:43 +00:00
|
|
|
onClick={() => {
|
|
|
|
this.props.startAccessibleSketch();
|
|
|
|
this.props.setTextOutput(true);
|
|
|
|
this.props.setGridOutput(true);
|
|
|
|
}}
|
2016-08-25 21:59:02 +00:00
|
|
|
aria-label="play sketch"
|
2016-09-24 04:46:06 +00:00
|
|
|
disabled={this.props.infiniteLoop}
|
2016-08-16 01:09:47 +00:00
|
|
|
>
|
2016-08-25 21:59:02 +00:00
|
|
|
<InlineSVG src={playUrl} alt="Play Sketch" />
|
|
|
|
</button>
|
2017-01-31 21:45:47 +00:00
|
|
|
<button
|
|
|
|
className={playButtonClass}
|
2017-09-14 21:57:09 +00:00
|
|
|
onClick={this.props.startSketch}
|
2017-01-31 21:45:47 +00:00
|
|
|
aria-label="play only visual sketch"
|
|
|
|
disabled={this.props.infiniteLoop}
|
|
|
|
>
|
2016-08-25 21:59:02 +00:00
|
|
|
<InlineSVG src={playUrl} alt="Play only visual Sketch" />
|
2016-08-16 01:09:47 +00:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className={stopButtonClass}
|
2017-09-14 21:57:09 +00:00
|
|
|
onClick={this.props.stopSketch}
|
2016-08-16 01:09:47 +00:00
|
|
|
aria-label="stop sketch"
|
|
|
|
>
|
2016-08-15 16:42:13 +00:00
|
|
|
<InlineSVG src={stopUrl} alt="Stop Sketch" />
|
|
|
|
</button>
|
2016-10-06 19:45:26 +00:00
|
|
|
<div className="toolbar__autorefresh">
|
2016-09-28 17:15:50 +00:00
|
|
|
<input
|
|
|
|
id="autorefresh"
|
|
|
|
type="checkbox"
|
2016-09-28 18:12:01 +00:00
|
|
|
checked={this.props.autorefresh}
|
|
|
|
onChange={(event) => {
|
|
|
|
this.props.setAutorefresh(event.target.checked);
|
|
|
|
}}
|
2016-09-28 17:15:50 +00:00
|
|
|
/>
|
|
|
|
<label htmlFor="autorefresh" className="toolbar__autorefresh-label">
|
|
|
|
Auto-refresh
|
|
|
|
</label>
|
2016-10-06 19:45:26 +00:00
|
|
|
</div>
|
2016-08-15 16:42:13 +00:00
|
|
|
<div className={nameContainerClass}>
|
|
|
|
<a
|
|
|
|
className="toolbar__project-name"
|
2016-12-01 22:12:34 +00:00
|
|
|
href={this.props.owner ? `/${this.props.owner.username}/sketches/${this.props.project.id}` : ''}
|
2016-11-04 22:54:14 +00:00
|
|
|
onClick={(e) => {
|
|
|
|
if (this.canEditProjectName()) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.originalProjectName = this.props.project.name;
|
|
|
|
this.props.showEditProjectName();
|
2017-02-22 19:29:35 +00:00
|
|
|
setTimeout(() => this.projectNameInput.focus(), 0);
|
2016-11-04 22:54:14 +00:00
|
|
|
}
|
2016-08-15 16:42:13 +00:00
|
|
|
}}
|
2016-11-04 22:54:14 +00:00
|
|
|
>
|
2020-01-08 17:33:36 +00:00
|
|
|
<span>{this.props.project.name}</span>
|
2017-06-06 02:33:32 +00:00
|
|
|
{
|
|
|
|
this.canEditProjectName() &&
|
|
|
|
<InlineSVG className="toolbar__edit-name-button" src={editProjectNameUrl} alt="Edit Project Name" />
|
|
|
|
}
|
2016-11-04 22:54:14 +00:00
|
|
|
</a>
|
2016-08-15 16:42:13 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
2019-10-08 21:16:21 +00:00
|
|
|
maxLength="128"
|
2016-08-15 16:42:13 +00:00
|
|
|
className="toolbar__project-name-input"
|
|
|
|
value={this.props.project.name}
|
|
|
|
onChange={this.handleProjectNameChange}
|
2017-02-22 19:29:35 +00:00
|
|
|
ref={(element) => { this.projectNameInput = element; }}
|
2016-08-15 16:42:13 +00:00
|
|
|
onBlur={() => {
|
|
|
|
this.validateProjectName();
|
|
|
|
this.props.hideEditProjectName();
|
2016-10-19 15:47:58 +00:00
|
|
|
if (this.props.project.id) {
|
|
|
|
this.props.saveProject();
|
|
|
|
}
|
2016-08-15 16:42:13 +00:00
|
|
|
}}
|
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
/>
|
|
|
|
{(() => { // eslint-disable-line
|
|
|
|
if (this.props.owner) {
|
|
|
|
return (
|
2016-08-17 20:09:20 +00:00
|
|
|
<p className="toolbar__project-owner">
|
|
|
|
by <Link to={`/${this.props.owner.username}/sketches`}>{this.props.owner.username}</Link>
|
|
|
|
</p>
|
2016-08-15 16:42:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
className={preferencesButtonClass}
|
|
|
|
onClick={this.props.openPreferences}
|
2016-08-25 21:59:02 +00:00
|
|
|
aria-label="preferences"
|
2016-06-27 19:08:25 +00:00
|
|
|
>
|
2016-08-25 21:59:02 +00:00
|
|
|
<InlineSVG src={preferencesUrl} alt="Preferences" />
|
2016-08-15 16:42:13 +00:00
|
|
|
</button>
|
2016-06-23 22:29:55 +00:00
|
|
|
</div>
|
2016-08-15 16:42:13 +00:00
|
|
|
);
|
|
|
|
}
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 19:08:25 +00:00
|
|
|
Toolbar.propTypes = {
|
|
|
|
isPlaying: PropTypes.bool.isRequired,
|
2016-08-01 17:55:49 +00:00
|
|
|
preferencesIsVisible: PropTypes.bool.isRequired,
|
2016-06-27 19:08:25 +00:00
|
|
|
stopSketch: PropTypes.func.isRequired,
|
|
|
|
setProjectName: PropTypes.func.isRequired,
|
2016-07-15 15:54:47 +00:00
|
|
|
openPreferences: PropTypes.func.isRequired,
|
2016-07-15 17:11:50 +00:00
|
|
|
owner: PropTypes.shape({
|
|
|
|
username: PropTypes.string
|
2016-08-15 16:42:13 +00:00
|
|
|
}),
|
|
|
|
project: PropTypes.shape({
|
|
|
|
name: PropTypes.string.isRequired,
|
2016-10-19 15:47:58 +00:00
|
|
|
isEditingName: PropTypes.bool,
|
2017-05-03 15:46:12 +00:00
|
|
|
id: PropTypes.string,
|
2016-08-15 16:42:13 +00:00
|
|
|
}).isRequired,
|
|
|
|
showEditProjectName: PropTypes.func.isRequired,
|
2016-09-24 04:46:06 +00:00
|
|
|
hideEditProjectName: PropTypes.func.isRequired,
|
2016-09-28 18:12:01 +00:00
|
|
|
infiniteLoop: PropTypes.bool.isRequired,
|
|
|
|
autorefresh: PropTypes.bool.isRequired,
|
2016-09-29 04:54:35 +00:00
|
|
|
setAutorefresh: PropTypes.func.isRequired,
|
2018-03-01 18:28:43 +00:00
|
|
|
setTextOutput: PropTypes.func.isRequired,
|
|
|
|
setGridOutput: PropTypes.func.isRequired,
|
2017-09-14 21:57:09 +00:00
|
|
|
startSketch: PropTypes.func.isRequired,
|
|
|
|
startAccessibleSketch: PropTypes.func.isRequired,
|
2016-11-04 22:54:14 +00:00
|
|
|
saveProject: PropTypes.func.isRequired,
|
2017-09-14 21:57:09 +00:00
|
|
|
currentUser: PropTypes.string
|
2016-06-27 19:08:25 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
Toolbar.defaultProps = {
|
|
|
|
owner: undefined,
|
|
|
|
currentUser: undefined
|
|
|
|
};
|
|
|
|
|
2019-03-01 21:21:00 +00:00
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
autorefresh: state.preferences.autorefresh,
|
|
|
|
currentUser: state.user.username,
|
|
|
|
infiniteLoop: state.ide.infiniteLoop,
|
|
|
|
isPlaying: state.ide.isPlaying,
|
|
|
|
owner: state.project.owner,
|
|
|
|
preferencesIsVisible: state.ide.preferencesIsVisible,
|
|
|
|
project: state.project,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = {
|
|
|
|
...IDEActions,
|
|
|
|
...preferenceActions,
|
|
|
|
...projectActions,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Toolbar);
|