2018-08-22 08:39:42 +00:00
|
|
|
import React from 'react';
|
2018-10-16 10:19:51 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-08-22 08:39:42 +00:00
|
|
|
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Col, FormText } from 'reactstrap';
|
2018-10-16 06:27:21 +00:00
|
|
|
import { gettext } from '../../utils/constants';
|
2018-08-22 08:39:42 +00:00
|
|
|
|
2018-10-16 10:19:51 +00:00
|
|
|
const propTypes = {
|
|
|
|
fileType: PropTypes.string,
|
|
|
|
parentPath: PropTypes.string.isRequired,
|
|
|
|
onAddFile: PropTypes.func.isRequired,
|
|
|
|
addFileCancel: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2018-10-16 06:27:21 +00:00
|
|
|
class CreateFile extends React.Component {
|
2018-08-22 08:39:42 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
parentPath: '',
|
2018-10-16 06:27:21 +00:00
|
|
|
childName: props.fileType,
|
2018-10-24 06:37:41 +00:00
|
|
|
isDraft: false,
|
2018-08-22 08:39:42 +00:00
|
|
|
};
|
2018-10-16 10:19:51 +00:00
|
|
|
this.newInput = React.createRef();
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = (e) => {
|
|
|
|
this.setState({
|
|
|
|
childName: e.target.value,
|
2018-10-16 10:19:51 +00:00
|
|
|
}) ;
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
2018-10-16 10:19:51 +00:00
|
|
|
let path = this.state.parentPath + this.state.childName;
|
2018-10-24 06:37:41 +00:00
|
|
|
let isDraft = this.state.isDraft;
|
|
|
|
this.props.onAddFile(path, isDraft);
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 09:12:27 +00:00
|
|
|
handleKeyPress = (e) => {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
this.handleSubmit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 06:37:41 +00:00
|
|
|
handleCheck = () => {
|
|
|
|
let pos = this.state.childName.lastIndexOf(".");
|
|
|
|
|
|
|
|
if (this.state.isDraft) {
|
|
|
|
// from draft to not draft
|
|
|
|
// case 1, normally, the file name is ended with `(draft)`, like `test(draft).md`
|
|
|
|
// case 2, the file name is not ended with `(draft)`, the user has deleted some characters, like `test(dra.md`
|
|
|
|
let p = this.state.childName.substring(pos-7, pos);
|
|
|
|
let fileName = this.state.childName.substring(0, pos-7);
|
|
|
|
let fileType = this.state.childName.substring(pos);
|
|
|
|
if (p === '(draft)') {
|
|
|
|
// remove `(draft)` from file name
|
|
|
|
this.setState({
|
|
|
|
childName: fileName + fileType,
|
|
|
|
isDraft: !this.state.isDraft
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// don't change file name
|
|
|
|
this.setState({
|
|
|
|
isDraft: !this.state.isDraft
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.isDraft) {
|
|
|
|
// from not draft to draft
|
|
|
|
// case 1, test.md ===> test(draft).md
|
|
|
|
// case 2, .md ===> (draft).md
|
|
|
|
// case 3, no '.' in the file name, don't change the file name
|
|
|
|
if (pos > 0) {
|
|
|
|
let fileName = this.state.childName.substring(0, pos);
|
|
|
|
let fileType = this.state.childName.substring(pos);
|
|
|
|
this.setState({
|
|
|
|
childName: fileName + '(draft)' + fileType,
|
|
|
|
isDraft: !this.state.isDraft
|
|
|
|
})
|
|
|
|
} else if (pos === 0 ) {
|
|
|
|
this.setState({
|
|
|
|
childName: '(draft)' + this.state.childname,
|
|
|
|
isDraft: !this.state.isdraft
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
isDraft: !this.state.isdraft
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
toggle = () => {
|
2018-10-16 06:27:21 +00:00
|
|
|
this.props.addFileCancel();
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-10-16 10:19:51 +00:00
|
|
|
if (this.props.parentPath === '/') {
|
2018-10-16 06:27:21 +00:00
|
|
|
this.setState({parentPath: this.props.parentPath});
|
2018-08-22 08:39:42 +00:00
|
|
|
} else {
|
2018-10-16 10:19:51 +00:00
|
|
|
this.setState({parentPath: this.props.parentPath + '/'});
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
this.newInput.focus();
|
|
|
|
this.newInput.setSelectionRange(0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Modal isOpen={true} toggle={this.toggle}>
|
2018-10-16 10:19:51 +00:00
|
|
|
<ModalHeader toggle={this.toggle}>{gettext('New File')}</ModalHeader>
|
2018-08-22 08:39:42 +00:00
|
|
|
<ModalBody>
|
|
|
|
<Form>
|
|
|
|
<FormGroup row>
|
|
|
|
<Label sm={3}>Parent path: </Label>
|
|
|
|
<Col sm={9} className="parent-path"><FormText>{this.state.parentPath}</FormText></Col>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup row>
|
2018-10-16 10:19:51 +00:00
|
|
|
<Label for="fileName" sm={3}>{gettext('Name')}: </Label>
|
2018-08-22 08:39:42 +00:00
|
|
|
<Col sm={9}>
|
2018-10-16 10:19:51 +00:00
|
|
|
<Input onKeyPress={this.handleKeyPress} innerRef={input => {this.newInput = input;}} id="fileName" placeholder={gettext('newName')} value={this.state.childName} onChange={this.handleChange}/>
|
2018-08-22 08:39:42 +00:00
|
|
|
</Col>
|
|
|
|
</FormGroup>
|
2018-10-24 06:37:41 +00:00
|
|
|
<FormGroup row>
|
|
|
|
<Label sm={3} check />
|
|
|
|
<Col sm={9}>
|
|
|
|
<Input type="checkbox" onChange={this.handleCheck}/>{' '}{gettext("This is a draft.")}
|
|
|
|
</Col>
|
|
|
|
</FormGroup>
|
|
|
|
|
2018-08-22 08:39:42 +00:00
|
|
|
</Form>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2018-10-16 10:19:51 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</Button>
|
|
|
|
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
|
2018-08-22 08:39:42 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
2018-10-16 10:19:51 +00:00
|
|
|
);
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:19:51 +00:00
|
|
|
CreateFile.propTypes = propTypes;
|
|
|
|
|
2018-10-16 06:27:21 +00:00
|
|
|
export default CreateFile;
|