p5.js-web-editor/client/modules/IDE/components/Toolbar.jsx

219 lines
6.8 KiB
React
Raw Normal View History

import PropTypes from 'prop-types';
import React from 'react';
import { connect } from 'react-redux';
2016-08-17 20:09:20 +00:00
import { Link } from 'react-router';
import classNames from 'classnames';
import * as IDEActions from '../actions/ide';
import * as preferenceActions from '../actions/preferences';
import * as projectActions from '../actions/project';
2020-04-29 22:34:37 +00: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-23 22:29:55 +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
handleKeyPress(event) {
if (event.key === 'Enter') {
this.props.hideEditProjectName();
}
}
2016-07-31 02:46:48 +00:00
handleProjectNameChange(event) {
this.props.setProjectName(event.target.value);
}
validateProjectName() {
if ((this.props.project.name.trim()).length === 0) {
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;
}
render() {
const playButtonClass = classNames({
'toolbar__play-button': true,
'toolbar__play-button--selected': this.props.isPlaying
});
const stopButtonClass = classNames({
'toolbar__stop-button': true,
'toolbar__stop-button--selected': !this.props.isPlaying
});
const preferencesButtonClass = classNames({
'toolbar__preferences-button': true,
'toolbar__preferences-button--selected': this.props.preferencesIsVisible
});
const nameContainerClass = classNames({
'toolbar__project-name-container': true,
'toolbar__project-name-container--editing': this.props.project.isEditingName
});
const canEditProjectName = this.canEditProjectName();
return (
<div className="toolbar">
2016-08-16 01:09:47 +00:00
<button
className="toolbar__play-sketch-button"
Remove accessibility CSS; Auto focus (#583) * Change accessibility example links * added library to iframe * changed preview to add accessible elements to iframe * add library only when accesible output is seleceted * focus on iframe when plaing * css * deleted accessibleOutput.jsx and edited IDEView to integrate accessibility library * deleted comments * fix package * Moved CSS to library and removed section from file.js (#2) * Remove gitmodule (#509) * remove git modules * removed submodule and replaced interceptor for library * removed submodule and replaced interceptor for library (#510) * deleted comments * deleted jquery * deleted interceptor folder * delete interceptor * added jquery * removed jquery and updated accessible library cdn * Fixes #508 (#539) * removed submodule and replaced interceptor for library * deleted comments * deleted jquery * deleted interceptor folder * delete interceptor * added jquery * removed jquery and updated accessible library cdn * remove empty divs from files.js * fix merge error * remove empty divs from files.js * Fixes #508 (#545) * removed submodule and replaced interceptor for library * deleted comments * deleted jquery * deleted interceptor folder * delete interceptor * added jquery * removed jquery and updated accessible library cdn * remove empty divs from files.js * fix merge error * remove empty divs from files.js * moved accessible output css * removed css, added aria-label, preliminary cdn update * removed section from iframe * updated cdn * add autofocus; remove CSS
2018-03-01 18:28:43 +00:00
onClick={() => {
this.props.startAccessibleSketch();
this.props.setTextOutput(true);
this.props.setGridOutput(true);
}}
aria-label="Play sketch"
disabled={this.props.infiniteLoop}
2016-08-16 01:09:47 +00:00
>
<PlayIcon focusable="false" aria-hidden="true" />
2016-08-25 21:59:02 +00:00
</button>
2017-01-31 21:45:47 +00:00
<button
className={playButtonClass}
onClick={this.props.startSketch}
aria-label="Play only visual sketch"
2017-01-31 21:45:47 +00:00
disabled={this.props.infiniteLoop}
>
<PlayIcon focusable="false" aria-hidden="true" />
2016-08-16 01:09:47 +00:00
</button>
<button
className={stopButtonClass}
onClick={this.props.stopSketch}
aria-label="Stop sketch"
2016-08-16 01:09:47 +00:00
>
<StopIcon focusable="false" aria-hidden="true" />
</button>
<div className="toolbar__autorefresh">
2016-09-28 17:15:50 +00:00
<input
id="autorefresh"
type="checkbox"
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>
</div>
<div className={nameContainerClass}>
<button
className="toolbar__project-name"
onClick={() => {
if (canEditProjectName) {
2016-11-04 22:54:14 +00:00
this.originalProjectName = this.props.project.name;
this.props.showEditProjectName();
setTimeout(() => this.projectNameInput.focus(), 0);
2016-11-04 22:54:14 +00:00
}
}}
disabled={!canEditProjectName}
aria-label="Edit sketch name"
2016-11-04 22:54:14 +00:00
>
<span>{this.props.project.name}</span>
2017-06-06 02:33:32 +00:00
{
canEditProjectName &&
<EditProjectNameIcon
className="toolbar__edit-name-button"
focusable="false"
aria-hidden="true"
/>
2017-06-06 02:33:32 +00:00
}
</button>
<input
type="text"
2019-10-08 21:16:21 +00:00
maxLength="128"
className="toolbar__project-name-input"
value={this.props.project.name}
onChange={this.handleProjectNameChange}
ref={(element) => { this.projectNameInput = element; }}
onBlur={() => {
this.validateProjectName();
this.props.hideEditProjectName();
if (this.props.project.id) {
this.props.saveProject();
}
}}
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>
);
}
})()}
</div>
<button
className={preferencesButtonClass}
onClick={this.props.openPreferences}
aria-label="Open Preferences"
2016-06-27 19:08:25 +00:00
>
<PreferencesIcon focusable="false" aria-hidden="true" />
</button>
2016-06-23 22:29:55 +00:00
</div>
);
}
2016-06-23 22:29:55 +00:00
}
2016-06-27 19:08:25 +00:00
Toolbar.propTypes = {
isPlaying: PropTypes.bool.isRequired,
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
}),
project: PropTypes.shape({
name: PropTypes.string.isRequired,
isEditingName: PropTypes.bool,
id: PropTypes.string,
}).isRequired,
showEditProjectName: PropTypes.func.isRequired,
hideEditProjectName: PropTypes.func.isRequired,
infiniteLoop: PropTypes.bool.isRequired,
autorefresh: PropTypes.bool.isRequired,
setAutorefresh: PropTypes.func.isRequired,
Remove accessibility CSS; Auto focus (#583) * Change accessibility example links * added library to iframe * changed preview to add accessible elements to iframe * add library only when accesible output is seleceted * focus on iframe when plaing * css * deleted accessibleOutput.jsx and edited IDEView to integrate accessibility library * deleted comments * fix package * Moved CSS to library and removed section from file.js (#2) * Remove gitmodule (#509) * remove git modules * removed submodule and replaced interceptor for library * removed submodule and replaced interceptor for library (#510) * deleted comments * deleted jquery * deleted interceptor folder * delete interceptor * added jquery * removed jquery and updated accessible library cdn * Fixes #508 (#539) * removed submodule and replaced interceptor for library * deleted comments * deleted jquery * deleted interceptor folder * delete interceptor * added jquery * removed jquery and updated accessible library cdn * remove empty divs from files.js * fix merge error * remove empty divs from files.js * Fixes #508 (#545) * removed submodule and replaced interceptor for library * deleted comments * deleted jquery * deleted interceptor folder * delete interceptor * added jquery * removed jquery and updated accessible library cdn * remove empty divs from files.js * fix merge error * remove empty divs from files.js * moved accessible output css * removed css, added aria-label, preliminary cdn update * removed section from iframe * updated cdn * add autofocus; remove CSS
2018-03-01 18:28:43 +00:00
setTextOutput: PropTypes.func.isRequired,
setGridOutput: PropTypes.func.isRequired,
startSketch: PropTypes.func.isRequired,
startAccessibleSketch: PropTypes.func.isRequired,
2016-11-04 22:54:14 +00:00
saveProject: PropTypes.func.isRequired,
currentUser: PropTypes.string
2016-06-27 19:08:25 +00:00
};
Toolbar.defaultProps = {
owner: undefined,
currentUser: undefined
};
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 16:17:59 +00:00
export const ToolbarComponent = Toolbar;
export default connect(mapStateToProps, mapDispatchToProps)(Toolbar);