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