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

320 lines
12 KiB
React
Raw Normal View History

import PropTypes from 'prop-types';
import React from 'react';
2016-08-23 06:10:42 +02:00
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import InlineSVG from 'react-inlinesvg';
import classNames from 'classnames';
import * as IDEActions from '../actions/ide';
import * as FileActions from '../actions/files';
const downArrowUrl = require('../../../images/down-filled-triangle.svg');
const folderRightUrl = require('../../../images/triangle-arrow-right.svg');
const folderDownUrl = require('../../../images/triangle-arrow-down.svg');
const fileUrl = require('../../../images/file.svg');
2016-08-23 06:10:42 +02:00
export class FileNode extends React.Component {
constructor(props) {
super(props);
this.renderChild = this.renderChild.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
this.handleFileNameChange = this.handleFileNameChange.bind(this);
this.validateFileName = this.validateFileName.bind(this);
this.handleFileClick = this.handleFileClick.bind(this);
this.toggleFileOptions = this.toggleFileOptions.bind(this);
this.hideFileOptions = this.hideFileOptions.bind(this);
this.showEditFileName = this.showEditFileName.bind(this);
this.hideEditFileName = this.hideEditFileName.bind(this);
this.onBlurComponent = this.onBlurComponent.bind(this);
this.onFocusComponent = this.onFocusComponent.bind(this);
this.state = {
isOptionsOpen: false,
isEditingName: false,
isFocused: false,
};
}
onFocusComponent() {
this.setState({ isFocused: true });
}
onBlurComponent() {
this.setState({ isFocused: false });
setTimeout(() => {
if (!this.state.isFocused) {
this.hideFileOptions();
}
}, 200);
}
getName() {
const { updatedName, name } = this.props;
return updatedName || name;
}
handleFileClick(e) {
e.stopPropagation();
if (this.getName() !== 'root' && !this.isDeleting) {
2016-08-25 00:52:08 +02:00
this.props.setSelectedFile(this.props.id);
}
2016-08-23 06:10:42 +02:00
}
2016-08-23 06:10:42 +02:00
handleFileNameChange(event) {
this.props.updateFileName(this.props.id, event.target.value, this.getName());
2016-08-23 06:10:42 +02:00
}
handleKeyPress(event) {
if (event.key === 'Enter') {
this.hideEditFileName();
2016-08-23 06:10:42 +02:00
}
}
validateFileName() {
const oldFileExtension = this.originalFileName.match(/\.[0-9a-z]+$/i);
const newFileExtension = this.getName().match(/\.[0-9a-z]+$/i);
const hasPeriod = this.getName().match(/\.+/);
const newFileName = this.getName();
const hasNoExtension = oldFileExtension && !newFileExtension;
const hasExtensionIfFolder = this.props.fileType === 'folder' && hasPeriod;
const notSameExtension = oldFileExtension && newFileExtension
&& oldFileExtension[0].toLowerCase() !== newFileExtension[0].toLowerCase();
const hasEmptyFilename = newFileName === '';
const hasOnlyExtension = newFileExtension && newFileName === newFileExtension[0];
if (hasEmptyFilename || hasNoExtension || notSameExtension || hasOnlyExtension || hasExtensionIfFolder) {
this.props.updateFileName(this.props.id, this.originalFileName, this.getName());
}
2016-08-23 06:10:42 +02:00
}
toggleFileOptions(e) {
e.preventDefault();
if (!this.props.canEdit) {
return;
}
if (this.state.isOptionsOpen) {
this.setState({ isOptionsOpen: false });
} else {
this[`fileOptions-${this.props.id}`].focus();
this.setState({ isOptionsOpen: true });
}
}
hideFileOptions() {
this.setState({ isOptionsOpen: false });
}
showEditFileName() {
this.setState({ isEditingName: true });
}
hideEditFileName() {
this.setState({ isEditingName: false });
}
2016-08-23 06:10:42 +02:00
renderChild(childId) {
return (
<li key={childId}>
<ConnectedFileNode id={childId} parentId={this.props.id} canEdit={this.props.canEdit} />
2016-08-23 06:10:42 +02:00
</li>
);
}
render() {
const itemClass = classNames({
'sidebar__root-item': this.getName() === 'root',
'sidebar__file-item': this.getName() !== 'root',
'sidebar__file-item--selected': this.props.isSelectedFile,
'sidebar__file-item--open': this.state.isOptionsOpen,
'sidebar__file-item--editing': this.state.isEditingName,
'sidebar__file-item--closed': this.props.isFolderClosed
2016-08-23 06:10:42 +02:00
});
2016-08-23 06:10:42 +02:00
return (
<div className={itemClass}>
{(() => { // eslint-disable-line
if (this.getName() !== 'root') {
return (
<div className="file-item__content" onContextMenu={this.toggleFileOptions}>
2016-08-30 20:39:37 +02:00
<span className="file-item__spacer"></span>
{(() => { // eslint-disable-line
2016-08-30 05:23:10 +02:00
if (this.props.fileType === 'file') {
return (
<span className="sidebar__file-item-icon">
<InlineSVG src={fileUrl} />
</span>
);
2016-08-31 03:08:50 +02:00
}
return (
<div className="sidebar__file-item--folder">
<button
2016-08-31 03:08:50 +02:00
className="sidebar__file-item-closed"
onClick={() => this.props.showFolderChildren(this.props.id)}
>
2016-08-31 03:08:50 +02:00
<InlineSVG className="folder-right" src={folderRightUrl} />
</button>
<button
2016-08-31 03:08:50 +02:00
className="sidebar__file-item-open"
onClick={() => this.props.hideFolderChildren(this.props.id)}
>
<InlineSVG className="folder-down" src={folderDownUrl} />
</button>
2016-08-31 03:08:50 +02:00
</div>
);
})()}
<button className="sidebar__file-item-name" onClick={this.handleFileClick}>{this.getName()}</button>
<input
type="text"
className="sidebar__file-item-input"
value={this.getName()}
2020-04-06 18:46:17 +02:00
maxLength="128"
onChange={this.handleFileNameChange}
ref={(element) => { this.fileNameInput = element; }}
onBlur={() => {
this.validateFileName();
this.hideEditFileName();
}}
onKeyPress={this.handleKeyPress}
/>
<button
className="sidebar__file-item-show-options"
aria-label="view file options"
ref={(element) => { this[`fileOptions-${this.props.id}`] = element; }}
2016-10-19 19:03:19 +02:00
tabIndex="0"
onClick={this.toggleFileOptions}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
>
<InlineSVG src={downArrowUrl} />
</button>
<div className="sidebar__file-item-options">
<ul title="file options">
2016-08-30 05:43:02 +02:00
{(() => { // eslint-disable-line
if (this.props.fileType === 'folder') {
return (
<li>
<button
aria-label="add file"
onClick={() => {
this.props.newFile(this.props.id);
setTimeout(() => this.hideFileOptions(), 0);
}}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
className="sidebar__file-item-option"
>
2016-08-30 05:43:02 +02:00
Add File
</button>
2016-08-30 05:43:02 +02:00
</li>
);
}
})()}
{(() => { // eslint-disable-line
if (this.props.fileType === 'folder') {
return (
<li>
<button
aria-label="add folder"
onClick={() => {
this.props.newFolder(this.props.id);
setTimeout(() => this.hideFileOptions(), 0);
}}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
className="sidebar__file-item-option"
>
2016-08-30 05:43:02 +02:00
Add Folder
</button>
2016-08-30 05:43:02 +02:00
</li>
);
}
})()}
<li>
<button
onClick={() => {
this.originalFileName = this.getName();
this.showEditFileName();
setTimeout(() => this.fileNameInput.focus(), 0);
setTimeout(() => this.hideFileOptions(), 0);
}}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
className="sidebar__file-item-option"
>
Rename
</button>
</li>
<li>
<button
onClick={() => {
if (window.confirm(`Are you sure you want to delete ${this.getName()}?`)) {
2016-08-25 00:52:08 +02:00
this.isDeleting = true;
this.props.resetSelectedFile(this.props.id);
setTimeout(() => this.props.deleteFile(this.props.id, this.props.parentId), 100);
}
}}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
className="sidebar__file-item-option"
>
Delete
</button>
</li>
</ul>
</div>
</div>
);
}
})()}
2016-08-23 06:10:42 +02:00
{(() => { // eslint-disable-line
if (this.props.children) {
return (
2016-08-30 20:39:37 +02:00
<ul className="file-item__children">
2016-08-23 06:10:42 +02:00
{this.props.children.map(this.renderChild)}
</ul>
);
}
})()}
</div>
);
}
}
FileNode.propTypes = {
id: PropTypes.string.isRequired,
2016-08-24 22:06:28 +02:00
parentId: PropTypes.string,
children: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
2016-08-23 06:10:42 +02:00
name: PropTypes.string.isRequired,
updatedName: PropTypes.string,
2016-08-30 05:23:10 +02:00
fileType: PropTypes.string.isRequired,
isSelectedFile: PropTypes.bool,
isFolderClosed: PropTypes.bool,
2016-08-23 06:10:42 +02:00
setSelectedFile: PropTypes.func.isRequired,
deleteFile: PropTypes.func.isRequired,
updateFileName: PropTypes.func.isRequired,
2016-08-30 05:43:02 +02:00
resetSelectedFile: PropTypes.func.isRequired,
newFile: PropTypes.func.isRequired,
newFolder: PropTypes.func.isRequired,
showFolderChildren: PropTypes.func.isRequired,
hideFolderChildren: PropTypes.func.isRequired,
canEdit: PropTypes.bool.isRequired
2016-08-23 06:10:42 +02:00
};
FileNode.defaultProps = {
parentId: '0',
isSelectedFile: false,
isFolderClosed: false,
updatedName: '',
};
2016-08-23 06:10:42 +02:00
function mapStateToProps(state, ownProps) {
2018-05-05 02:22:39 +02:00
// this is a hack, state is updated before ownProps
return state.files.find(file => file.id === ownProps.id) || { name: 'test', fileType: 'file' };
2016-08-23 06:10:42 +02:00
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(Object.assign(FileActions, IDEActions), dispatch);
2016-08-23 06:10:42 +02:00
}
const ConnectedFileNode = connect(mapStateToProps, mapDispatchToProps)(FileNode);
export default ConnectedFileNode;