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';
|
2019-01-25 07:44:04 +00:00
|
|
|
import { Button, Modal, ModalHeader, Input, ModalBody, ModalFooter, Form, FormGroup, Label, Alert } from 'reactstrap';
|
2019-03-29 10:15:39 +00:00
|
|
|
import { gettext, isDocs } from '../../utils/constants';
|
2019-01-25 07:44:04 +00:00
|
|
|
import { Utils } from '../../utils/utils';
|
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,
|
2019-01-25 07:44:04 +00:00
|
|
|
checkDuplicatedName: PropTypes.func.isRequired,
|
2018-10-16 10:19:51 +00:00
|
|
|
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: '',
|
2019-04-18 03:03:25 +00:00
|
|
|
childName: props.fileType || '',
|
2018-10-24 06:37:41 +00:00
|
|
|
isDraft: false,
|
2019-01-25 07:44:04 +00:00
|
|
|
errMessage: '',
|
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
|
|
|
}
|
|
|
|
|
2019-01-25 07:44:04 +00:00
|
|
|
componentDidMount() {
|
|
|
|
let parentPath = this.props.parentPath;
|
|
|
|
if (parentPath[parentPath.length - 1] === '/') { // mainPanel
|
|
|
|
this.setState({parentPath: parentPath});
|
|
|
|
} else {
|
|
|
|
this.setState({parentPath: parentPath + '/'}); // sidePanel
|
|
|
|
}
|
|
|
|
this.newInput.focus();
|
|
|
|
this.newInput.setSelectionRange(0,0);
|
|
|
|
}
|
|
|
|
|
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 = () => {
|
2019-01-25 07:44:04 +00:00
|
|
|
let isDuplicated = this.checkDuplicatedName();
|
2019-01-31 09:37:02 +00:00
|
|
|
let newName = this.state.childName;
|
2019-05-17 02:01:57 +00:00
|
|
|
|
|
|
|
if (!newName.trim()) {
|
|
|
|
let errMessage = gettext('The file name is empty');
|
|
|
|
this.setState({errMessage: errMessage});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-25 07:44:04 +00:00
|
|
|
if (isDuplicated) {
|
2019-01-29 03:32:54 +00:00
|
|
|
let errMessage = gettext('The name "{name}" is already taken. Please choose a different name.');
|
2019-01-25 07:44:04 +00:00
|
|
|
errMessage = errMessage.replace('{name}', Utils.HTMLescape(newName));
|
|
|
|
this.setState({errMessage: errMessage});
|
|
|
|
} else {
|
|
|
|
let path = this.state.parentPath + newName;
|
|
|
|
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();
|
2019-01-25 07:44:04 +00:00
|
|
|
e.preventDefault();
|
2018-08-27 09:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 06:37:41 +00:00
|
|
|
handleCheck = () => {
|
2018-10-25 05:36:06 +00:00
|
|
|
let pos = this.state.childName.lastIndexOf('.');
|
2018-10-24 06:37:41 +00:00
|
|
|
|
|
|
|
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
|
2018-10-25 05:36:06 +00:00
|
|
|
});
|
2018-10-24 06:37:41 +00:00
|
|
|
} else {
|
|
|
|
// don't change file name
|
|
|
|
this.setState({
|
|
|
|
isDraft: !this.state.isDraft
|
2018-10-25 05:36:06 +00:00
|
|
|
});
|
2018-10-24 06:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-10-25 05:36:06 +00:00
|
|
|
});
|
2018-10-24 06:37:41 +00:00
|
|
|
} else if (pos === 0 ) {
|
|
|
|
this.setState({
|
2018-11-22 03:26:00 +00:00
|
|
|
childName: '(draft)' + this.state.childName,
|
2018-10-24 06:37:41 +00:00
|
|
|
isDraft: !this.state.isdraft
|
2018-10-25 05:36:06 +00:00
|
|
|
});
|
2018-10-24 06:37:41 +00:00
|
|
|
} else {
|
2018-10-25 05:36:06 +00:00
|
|
|
this.setState({
|
2018-10-24 06:37:41 +00:00
|
|
|
isDraft: !this.state.isdraft
|
2018-10-25 05:36:06 +00:00
|
|
|
});
|
2018-10-24 06:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-25 07:44:04 +00:00
|
|
|
checkDuplicatedName = () => {
|
|
|
|
let isDuplicated = this.props.checkDuplicatedName(this.state.childName);
|
|
|
|
return isDuplicated;
|
2018-08-22 08:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2018-12-17 03:54:25 +00:00
|
|
|
<FormGroup>
|
|
|
|
<Label for="fileName">{gettext('Name')}</Label>
|
|
|
|
<Input
|
2018-12-17 05:16:13 +00:00
|
|
|
id="fileName"
|
2018-12-17 03:54:25 +00:00
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
innerRef={input => {this.newInput = input;}}
|
|
|
|
value={this.state.childName}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
/>
|
2018-08-22 08:39:42 +00:00
|
|
|
</FormGroup>
|
2019-03-29 10:15:39 +00:00
|
|
|
{this.props.fileType == '.md' && isDocs && (
|
2018-12-21 10:05:40 +00:00
|
|
|
<FormGroup check>
|
|
|
|
<Label check>
|
2019-01-08 06:37:27 +00:00
|
|
|
<Input type="checkbox" onChange={this.handleCheck}/>{' '}{gettext('This is a draft')}
|
2018-12-21 10:05:40 +00:00
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
2018-08-22 08:39:42 +00:00
|
|
|
</Form>
|
2019-01-25 07:44:04 +00:00
|
|
|
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
|
2018-08-22 08:39:42 +00:00
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
2018-10-16 10:19:51 +00:00
|
|
|
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
|
2018-12-17 03:54:25 +00:00
|
|
|
<Button color="primary" onClick={this.handleSubmit}>{gettext('Submit')}</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;
|