2018-02-07 19:06:07 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-07-08 20:57:22 +02:00
|
|
|
import classNames from 'classnames';
|
2017-02-22 20:29:35 +01:00
|
|
|
import ConnectedFileNode from './FileNode';
|
|
|
|
|
2020-04-30 00:34:37 +02:00
|
|
|
import DownArrowIcon from '../../../images/down-filled-triangle.svg';
|
2016-07-06 21:09:05 +02:00
|
|
|
|
2016-08-03 21:11:59 +02:00
|
|
|
class Sidebar extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.resetSelectedFile = this.resetSelectedFile.bind(this);
|
2017-01-09 22:11:01 +01:00
|
|
|
this.toggleProjectOptions = this.toggleProjectOptions.bind(this);
|
2019-03-27 20:52:57 +01:00
|
|
|
this.onBlurComponent = this.onBlurComponent.bind(this);
|
|
|
|
this.onFocusComponent = this.onFocusComponent.bind(this);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isFocused: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onBlurComponent() {
|
|
|
|
this.setState({ isFocused: false });
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!this.state.isFocused) {
|
|
|
|
this.props.closeProjectOptions();
|
|
|
|
}
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
onFocusComponent() {
|
|
|
|
this.setState({ isFocused: true });
|
2016-08-03 21:11:59 +02:00
|
|
|
}
|
2016-07-14 18:47:54 +02:00
|
|
|
|
2016-08-03 21:11:59 +02:00
|
|
|
resetSelectedFile() {
|
2016-08-23 19:52:31 +02:00
|
|
|
this.props.setSelectedFile(this.props.files[1].id);
|
2016-08-03 21:11:59 +02:00
|
|
|
}
|
|
|
|
|
2017-01-09 22:11:01 +01:00
|
|
|
toggleProjectOptions(e) {
|
|
|
|
e.preventDefault();
|
2017-01-09 18:47:32 +01:00
|
|
|
if (this.props.projectOptionsVisible) {
|
|
|
|
this.props.closeProjectOptions();
|
|
|
|
} else {
|
2017-02-22 20:29:35 +01:00
|
|
|
this.sidebarOptions.focus();
|
2017-01-09 18:47:32 +01:00
|
|
|
this.props.openProjectOptions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 02:06:04 +02:00
|
|
|
userCanEditProject() {
|
|
|
|
let canEdit;
|
|
|
|
if (!this.props.owner) {
|
|
|
|
canEdit = true;
|
|
|
|
} else if (this.props.user.authenticated && this.props.owner.id === this.props.user.id) {
|
|
|
|
canEdit = true;
|
|
|
|
} else {
|
|
|
|
canEdit = false;
|
|
|
|
}
|
|
|
|
return canEdit;
|
|
|
|
}
|
|
|
|
|
2016-08-03 21:11:59 +02:00
|
|
|
render() {
|
2019-03-14 21:10:50 +01:00
|
|
|
const canEditProject = this.userCanEditProject();
|
2016-08-03 21:11:59 +02:00
|
|
|
const sidebarClass = classNames({
|
2017-02-22 20:29:35 +01:00
|
|
|
'sidebar': true,
|
2016-08-30 05:23:10 +02:00
|
|
|
'sidebar--contracted': !this.props.isExpanded,
|
2017-04-12 02:06:04 +02:00
|
|
|
'sidebar--project-options': this.props.projectOptionsVisible,
|
2019-03-14 21:10:50 +01:00
|
|
|
'sidebar--cant-edit': !canEditProject
|
2016-08-03 21:11:59 +02:00
|
|
|
});
|
2019-10-08 22:39:47 +02:00
|
|
|
const rootFile = this.props.files.filter(file => file.name === 'root')[0];
|
2016-08-03 21:11:59 +02:00
|
|
|
|
|
|
|
return (
|
2018-05-09 04:10:52 +02:00
|
|
|
<nav className={sidebarClass} title="file-navigation" >
|
2017-01-09 22:11:01 +01:00
|
|
|
<div className="sidebar__header" onContextMenu={this.toggleProjectOptions}>
|
2016-08-25 23:32:27 +02:00
|
|
|
<h3 className="sidebar__title">
|
2019-06-14 19:30:13 +02:00
|
|
|
<span>Sketch Files</span>
|
2016-08-25 23:32:27 +02:00
|
|
|
</h3>
|
2016-08-03 21:11:59 +02:00
|
|
|
<div className="sidebar__icons">
|
|
|
|
<button
|
2020-05-06 01:03:58 +02:00
|
|
|
aria-label="Toggle open/close sketch file options"
|
2016-08-03 21:11:59 +02:00
|
|
|
className="sidebar__add"
|
2016-10-19 19:03:19 +02:00
|
|
|
tabIndex="0"
|
2017-02-22 20:29:35 +01:00
|
|
|
ref={(element) => { this.sidebarOptions = element; }}
|
2017-01-09 22:11:01 +01:00
|
|
|
onClick={this.toggleProjectOptions}
|
2019-03-27 20:52:57 +01:00
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
2016-08-03 21:11:59 +02:00
|
|
|
>
|
2020-05-06 01:03:58 +02:00
|
|
|
<DownArrowIcon focusable="false" aria-hidden="true" />
|
2016-08-03 21:11:59 +02:00
|
|
|
</button>
|
2016-08-30 05:23:10 +02:00
|
|
|
<ul className="sidebar__project-options">
|
|
|
|
<li>
|
2019-03-21 22:08:46 +01:00
|
|
|
<button
|
|
|
|
aria-label="add folder"
|
|
|
|
onClick={() => {
|
2019-10-08 22:39:47 +02:00
|
|
|
this.props.newFolder(rootFile.id);
|
2019-03-21 22:08:46 +01:00
|
|
|
setTimeout(this.props.closeProjectOptions, 0);
|
|
|
|
}}
|
2019-03-27 20:52:57 +01:00
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
2019-03-21 22:08:46 +01:00
|
|
|
>
|
2019-09-26 21:06:43 +02:00
|
|
|
Create folder
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
2016-08-30 05:23:10 +02:00
|
|
|
</li>
|
|
|
|
<li>
|
2019-03-21 22:08:46 +01:00
|
|
|
<button
|
|
|
|
aria-label="add file"
|
|
|
|
onClick={() => {
|
2019-10-08 22:39:47 +02:00
|
|
|
this.props.newFile(rootFile.id);
|
2019-03-21 22:08:46 +01:00
|
|
|
setTimeout(this.props.closeProjectOptions, 0);
|
|
|
|
}}
|
2019-03-27 20:52:57 +01:00
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
2019-03-21 22:08:46 +01:00
|
|
|
>
|
2019-09-11 00:42:23 +02:00
|
|
|
Create file
|
2017-02-22 20:29:35 +01:00
|
|
|
</button>
|
2016-08-30 05:23:10 +02:00
|
|
|
</li>
|
2020-04-16 22:16:19 +02:00
|
|
|
{
|
|
|
|
this.props.user.authenticated &&
|
|
|
|
<li>
|
|
|
|
<button
|
|
|
|
aria-label="upload file"
|
|
|
|
onClick={() => {
|
|
|
|
this.props.openUploadFileModal(rootFile.id);
|
|
|
|
setTimeout(this.props.closeProjectOptions, 0);
|
|
|
|
}}
|
|
|
|
onBlur={this.onBlurComponent}
|
|
|
|
onFocus={this.onFocusComponent}
|
|
|
|
>
|
|
|
|
Upload file
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
}
|
2016-08-30 05:23:10 +02:00
|
|
|
</ul>
|
2016-08-03 21:11:59 +02:00
|
|
|
</div>
|
2016-07-14 18:47:54 +02:00
|
|
|
</div>
|
2019-03-14 21:10:50 +01:00
|
|
|
<ConnectedFileNode
|
2019-10-08 22:39:47 +02:00
|
|
|
id={rootFile.id}
|
2019-03-14 21:10:50 +01:00
|
|
|
canEdit={canEditProject}
|
|
|
|
/>
|
2016-08-03 21:11:59 +02:00
|
|
|
</nav>
|
|
|
|
);
|
|
|
|
}
|
2016-07-06 21:09:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Sidebar.propTypes = {
|
2017-02-22 20:29:35 +01:00
|
|
|
files: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
id: PropTypes.string.isRequired
|
|
|
|
})).isRequired,
|
2016-08-03 21:11:59 +02:00
|
|
|
setSelectedFile: PropTypes.func.isRequired,
|
|
|
|
isExpanded: PropTypes.bool.isRequired,
|
2016-08-30 05:23:10 +02:00
|
|
|
projectOptionsVisible: PropTypes.bool.isRequired,
|
2016-08-03 21:11:59 +02:00
|
|
|
newFile: PropTypes.func.isRequired,
|
2016-08-30 05:23:10 +02:00
|
|
|
openProjectOptions: PropTypes.func.isRequired,
|
|
|
|
closeProjectOptions: PropTypes.func.isRequired,
|
2017-04-12 02:06:04 +02:00
|
|
|
newFolder: PropTypes.func.isRequired,
|
2019-08-12 17:21:42 +02:00
|
|
|
openUploadFileModal: PropTypes.func.isRequired,
|
2017-04-12 02:06:04 +02:00
|
|
|
owner: PropTypes.shape({
|
|
|
|
id: PropTypes.string
|
|
|
|
}),
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
authenticated: PropTypes.bool.isRequired
|
|
|
|
}).isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
Sidebar.defaultProps = {
|
|
|
|
owner: undefined
|
2016-07-06 21:09:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Sidebar;
|