1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-18 17:22:05 +00:00
seahub/frontend/src/components/dialog/create-file-dialog.js

191 lines
5.9 KiB
JavaScript
Raw Normal View History

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';
import { gettext, isDocs } from '../../utils/constants';
2019-01-25 07:44:04 +00:00
import { Utils } from '../../utils/utils';
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,
toggleDialog: PropTypes.func.isRequired,
2018-10-16 10:19:51 +00:00
};
class CreateFile extends React.Component {
constructor(props) {
super(props);
this.state = {
parentPath: '',
2019-04-18 03:03:25 +00:00
childName: props.fileType || '',
isMarkdownDraft: false,
isSdocDraft: false,
2019-01-25 07:44:04 +00:00
errMessage: '',
isSubmitBtnActive: false,
};
2018-10-16 10:19:51 +00:00
this.newInput = React.createRef();
}
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
}
}
handleChange = (e) => {
if (!e.target.value.trim()) {
this.setState({isSubmitBtnActive: false});
} else {
this.setState({isSubmitBtnActive: true});
}
this.setState({
childName: e.target.value,
2018-10-16 10:19:51 +00:00
}) ;
};
handleSubmit = () => {
2019-05-17 06:17:52 +00:00
if (!this.state.isSubmitBtnActive) {
return;
}
2019-01-25 07:44:04 +00:00
let isDuplicated = this.checkDuplicatedName();
let newName = this.state.childName;
2019-01-25 07:44:04 +00:00
if (isDuplicated) {
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;
const { isMarkdownDraft, isSdocDraft } = this.state;
this.props.onAddFile(path, isMarkdownDraft, isSdocDraft);
this.props.toggleDialog();
2019-01-25 07:44:04 +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-08-27 09:12:27 +00:00
2018-10-24 06:37:41 +00:00
handleCheck = () => {
let pos = this.state.childName.lastIndexOf('.');
if (this.state.isMarkdownDraft) {
2018-10-24 06:37:41 +00:00
// 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,
isMarkdownDraft: !this.state.isMarkdownDraft
});
2018-10-24 06:37:41 +00:00
} else {
// don't change file name
this.setState({
isMarkdownDraft: !this.state.isMarkdownDraft
});
2018-10-24 06:37:41 +00:00
}
}
if (!this.state.isMarkdownDraft) {
2018-10-24 06:37:41 +00:00
// 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,
isMarkdownDraft: !this.state.isMarkdownDraft
});
2018-10-24 06:37:41 +00:00
} else if (pos === 0 ) {
this.setState({
childName: '(draft)' + this.state.childName,
isMarkdownDraft: !this.state.isMarkdownDraft
});
2018-10-24 06:37:41 +00:00
} else {
this.setState({
isMarkdownDraft: !this.state.isMarkdownDraft
});
}
2018-10-24 06:37:41 +00:00
}
};
2018-10-24 06:37:41 +00:00
2019-01-25 07:44:04 +00:00
checkDuplicatedName = () => {
let isDuplicated = this.props.checkDuplicatedName(this.state.childName);
return isDuplicated;
};
onAfterModelOpened = () => {
if (!this.newInput.current) return;
this.newInput.current.focus();
this.newInput.current.setSelectionRange(0,0);
};
toggleMarkSdocDraft = (e) => {
this.setState({
isSdocDraft: e.target.checked
});
};
render() {
const { isSdocDraft } = this.state;
const { toggleDialog } = this.props;
return (
<Modal isOpen={true} toggle={toggleDialog} onOpened={this.onAfterModelOpened}>
<ModalHeader toggle={toggleDialog}>{gettext('New File')}</ModalHeader>
<ModalBody>
<Form>
2018-12-17 03:54:25 +00:00
<FormGroup>
<Label for="fileName">{gettext('Name')}</Label>
<Input
id="fileName"
onKeyPress={this.handleKeyPress}
innerRef={this.newInput}
value={this.state.childName}
2018-12-17 03:54:25 +00:00
onChange={this.handleChange}
/>
</FormGroup>
{/*this.props.fileType == '.md' && isDocs && (
<FormGroup check>
<Label check>
2019-01-08 06:37:27 +00:00
<Input type="checkbox" onChange={this.handleCheck}/>{' '}{gettext('This is a draft')}
</Label>
</FormGroup>
)*/}
{/*this.props.fileType == '.sdoc' && (
<FormGroup check>
<Label check>
<Input type="checkbox" checked={isSdocDraft} onChange={this.toggleMarkSdocDraft}/>
<span>{gettext('Mark as draft')}</span>
</Label>
</FormGroup>
)*/}
</Form>
2019-01-25 07:44:04 +00:00
{this.state.errMessage && <Alert color="danger" className="mt-2">{this.state.errMessage}</Alert>}
</ModalBody>
<ModalFooter>
<Button color="secondary" onClick={toggleDialog}>{gettext('Cancel')}</Button>
<Button color="primary" onClick={this.handleSubmit} disabled={!this.state.isSubmitBtnActive}>{gettext('Submit')}</Button>
</ModalFooter>
</Modal>
2018-10-16 10:19:51 +00:00
);
}
}
2018-10-16 10:19:51 +00:00
CreateFile.propTypes = propTypes;
export default CreateFile;