switch project name edit to html5 input

This commit is contained in:
catarak 2016-08-15 12:42:13 -04:00
parent 03d59b159a
commit 17efc42778
7 changed files with 130 additions and 54 deletions

View file

@ -27,6 +27,8 @@ export const PROJECT_SAVE_SUCCESS = 'PROJECT_SAVE_SUCCESS';
export const PROJECT_SAVE_FAIL = 'PROJECT_SAVE_FAIL'; export const PROJECT_SAVE_FAIL = 'PROJECT_SAVE_FAIL';
export const NEW_PROJECT = 'NEW_PROJECT'; export const NEW_PROJECT = 'NEW_PROJECT';
export const RESET_PROJECT = 'RESET_PROJECT'; export const RESET_PROJECT = 'RESET_PROJECT';
export const SHOW_EDIT_PROJECT_NAME = 'SHOW_EDIT_PROJECT_NAME';
export const HIDE_EDIT_PROJECT_NAME = 'HIDE_EDIT_PROJECT_NAME';
export const SET_PROJECT = 'SET_PROJECT'; export const SET_PROJECT = 'SET_PROJECT';
export const SET_PROJECTS = 'SET_PROJECTS'; export const SET_PROJECTS = 'SET_PROJECTS';

View file

@ -41,8 +41,7 @@ export function getProject(id) {
}; };
} }
export function setProjectName(event) { export function setProjectName(name) {
const name = event.target.textContent;
return { return {
type: ActionTypes.SET_PROJECT_NAME, type: ActionTypes.SET_PROJECT_NAME,
name name
@ -171,3 +170,15 @@ export function cloneProject() {
}; };
} }
export function showEditProjectName() {
return {
type: ActionTypes.SHOW_EDIT_PROJECT_NAME
};
}
export function hideEditProjectName() {
return {
type: ActionTypes.HIDE_EDIT_PROJECT_NAME
};
}

View file

@ -16,7 +16,6 @@ class SidebarItem extends React.Component {
} }
handleKeyPress(event) { handleKeyPress(event) {
console.log(event.key);
if (event.key === 'Enter') { if (event.key === 'Enter') {
this.props.hideEditFileName(this.props.file.id); this.props.hideEditFileName(this.props.file.id);
} }

View file

@ -6,57 +6,96 @@ const stopUrl = require('../../../images/stop.svg');
const preferencesUrl = require('../../../images/preferences.svg'); const preferencesUrl = require('../../../images/preferences.svg');
import classNames from 'classnames'; import classNames from 'classnames';
function Toolbar(props) { class Toolbar extends React.Component {
constructor(props) {
super(props);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.handleProjectNameChange = this.handleProjectNameChange.bind(this);
}
handleKeyPress(event) {
if (event.key === 'Enter') {
this.props.hideEditProjectName();
}
}
handleProjectNameChange(event) {
this.props.setProjectName(event.target.value);
}
validateProjectName() {
if (this.props.project.name === '') {
this.props.setProjectName(this.originalProjectName);
}
}
render() {
let playButtonClass = classNames({ let playButtonClass = classNames({
'toolbar__play-button': true, 'toolbar__play-button': true,
'toolbar__play-button--selected': props.isPlaying 'toolbar__play-button--selected': this.props.isPlaying
}); });
let stopButtonClass = classNames({ let stopButtonClass = classNames({
'toolbar__stop-button': true, 'toolbar__stop-button': true,
'toolbar__stop-button--selected': !props.isPlaying 'toolbar__stop-button--selected': !this.props.isPlaying
}); });
let preferencesButtonClass = classNames({ let preferencesButtonClass = classNames({
'toolbar__preferences-button': true, 'toolbar__preferences-button': true,
'toolbar__preferences-button--selected': props.preferencesIsVisible 'toolbar__preferences-button--selected': this.props.preferencesIsVisible
});
let nameContainerClass = classNames({
'toolbar__project-name-container': true,
'toolbar__project-name-container--editing': this.props.project.isEditingName
}); });
return ( return (
<div className="toolbar"> <div className="toolbar">
<img className="toolbar__logo" src={logoUrl} alt="p5js Logo" /> <img className="toolbar__logo" src={logoUrl} alt="p5js Logo" />
<button className={playButtonClass} onClick={props.startSketch} aria-label="play sketch"> <button className={playButtonClass} onClick={this.props.startSketch} aria-label="play sketch">
<InlineSVG src={playUrl} alt="Play Sketch" /> <InlineSVG src={playUrl} alt="Play Sketch" />
</button> </button>
<button className={stopButtonClass} onClick={props.stopSketch} aria-label="stop sketch"> <button className={stopButtonClass} onClick={this.props.stopSketch} aria-label="stop sketch">
<InlineSVG src={stopUrl} alt="Stop Sketch" /> <InlineSVG src={stopUrl} alt="Stop Sketch" />
</button> </button>
<div className="toolbar__project-name-container"> <div className={nameContainerClass}>
<span <a
className="toolbar__project-name" className="toolbar__project-name"
// TODO change this span into an input onClick={() => {
onBlur={props.setProjectName.bind(this)} // eslint-disable-line this.originalProjectName = this.props.project.name;
contentEditable this.props.showEditProjectName();
suppressContentEditableWarning setTimeout(() => this.refs.projectNameInput.focus(), 0);
> }}
{props.projectName} >{this.props.project.name}</a>
</span> <input
type="text"
className="toolbar__project-name-input"
value={this.props.project.name}
onChange={this.handleProjectNameChange}
ref="projectNameInput"
onBlur={() => {
this.validateProjectName();
this.props.hideEditProjectName();
}}
onKeyPress={this.handleKeyPress}
/>
{(() => { // eslint-disable-line {(() => { // eslint-disable-line
if (props.owner) { if (this.props.owner) {
return ( return (
<p className="toolbar__project-owner">by <span>{props.owner.username}</span></p> <p className="toolbar__project-owner">by <span>{this.props.owner.username}</span></p>
); );
} }
})()} })()}
</div> </div>
<button <button
className={preferencesButtonClass} className={preferencesButtonClass}
onClick={props.openPreferences} onClick={this.props.openPreferences}
aria-label="open preferences" aria-label="open preferences"
> >
<InlineSVG src={preferencesUrl} alt="Show Preferences" /> <InlineSVG src={preferencesUrl} alt="Show Preferences" />
</button> </button>
</div> </div>
); );
}
} }
Toolbar.propTypes = { Toolbar.propTypes = {
@ -65,11 +104,16 @@ Toolbar.propTypes = {
startSketch: PropTypes.func.isRequired, startSketch: PropTypes.func.isRequired,
stopSketch: PropTypes.func.isRequired, stopSketch: PropTypes.func.isRequired,
setProjectName: PropTypes.func.isRequired, setProjectName: PropTypes.func.isRequired,
projectName: PropTypes.string.isRequired,
openPreferences: PropTypes.func.isRequired, openPreferences: PropTypes.func.isRequired,
owner: PropTypes.shape({ owner: PropTypes.shape({
username: PropTypes.string username: PropTypes.string
}) }),
project: PropTypes.shape({
name: PropTypes.string.isRequired,
isEditingName: PropTypes.bool
}).isRequired,
showEditProjectName: PropTypes.func.isRequired,
hideEditProjectName: PropTypes.func.isRequired
}; };
export default Toolbar; export default Toolbar;

View file

@ -113,11 +113,13 @@ class IDEView extends React.Component {
isPlaying={this.props.ide.isPlaying} isPlaying={this.props.ide.isPlaying}
startSketch={this.props.startSketch} startSketch={this.props.startSketch}
stopSketch={this.props.stopSketch} stopSketch={this.props.stopSketch}
projectName={this.props.project.name}
setProjectName={this.props.setProjectName} setProjectName={this.props.setProjectName}
showEditProjectName={this.props.showEditProjectName}
hideEditProjectName={this.props.hideEditProjectName}
openPreferences={this.props.openPreferences} openPreferences={this.props.openPreferences}
preferencesIsVisible={this.props.ide.preferencesIsVisible} preferencesIsVisible={this.props.ide.preferencesIsVisible}
owner={this.props.project.owner} owner={this.props.project.owner}
project={this.props.project}
/> />
<Preferences <Preferences
isVisible={this.props.ide.preferencesIsVisible} isVisible={this.props.ide.preferencesIsVisible}
@ -307,7 +309,9 @@ IDEView.propTypes = {
deleteFile: PropTypes.func.isRequired, deleteFile: PropTypes.func.isRequired,
showEditFileName: PropTypes.func.isRequired, showEditFileName: PropTypes.func.isRequired,
hideEditFileName: PropTypes.func.isRequired, hideEditFileName: PropTypes.func.isRequired,
updateFileName: PropTypes.func.isRequired updateFileName: PropTypes.func.isRequired,
showEditProjectName: PropTypes.func.isRequired,
hideEditProjectName: PropTypes.func.isRequired
}; };
function mapStateToProps(state) { function mapStateToProps(state) {

View file

@ -22,6 +22,10 @@ const project = (state = initialState, action) => {
}; };
case ActionTypes.RESET_PROJECT: case ActionTypes.RESET_PROJECT:
return initialState; return initialState;
case ActionTypes.SHOW_EDIT_PROJECT_NAME:
return Object.assign({}, state, { isEditingName: true });
case ActionTypes.HIDE_EDIT_PROJECT_NAME:
return Object.assign({}, state, { isEditingName: false });
default: default:
return state; return state;
} }

View file

@ -53,6 +53,18 @@
&:focus { &:focus {
color: $light-inactive-text-color; color: $light-inactive-text-color;
} }
.toolbar__project-name-container--editing & {
display: none;
}
}
.toolbar__project-name-input {
display: none;
border: 0px;
.toolbar__project-name-container--editing & {
display: block;
}
} }
.toolbar__project-owner { .toolbar__project-owner {