diff --git a/frontend/src/components/file-view/file-toolbar.js b/frontend/src/components/file-view/file-toolbar.js index a6a7bc5387..f68df5143b 100644 --- a/frontend/src/components/file-view/file-toolbar.js +++ b/frontend/src/components/file-view/file-toolbar.js @@ -1,6 +1,6 @@ import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; -import { ButtonGroup, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap'; +import { ButtonGroup } from 'reactstrap'; import IconButton from '../icon-button'; import { gettext, siteRoot } from '../../utils/constants'; import { Utils } from '../../utils/utils'; @@ -31,7 +31,6 @@ class FileToolbar extends React.Component { constructor(props) { super(props); this.state = { - dropdownOpen: false, isShareDialogOpen: false }; } @@ -40,15 +39,8 @@ class FileToolbar extends React.Component { this.setState({isShareDialogOpen: !this.state.isShareDialogOpen}); } - toggle = () => { - this.setState({ - dropdownOpen: !this.state.dropdownOpen - }); - } - render() { const { isLocked, lockedByMe } = this.props; - let showLockUnlockBtn = false; let lockUnlockText, lockUnlockIcon; if (canLockUnlockFile) { @@ -71,7 +63,7 @@ class FileToolbar extends React.Component { return ( - + )} - {(canEditFile && !err) && ( - - )} + {(canEditFile && !err) && + ( this.props.isSaving ? + : + ( + this.props.isContentChangedButNotSaved ? + : + + ) + )} {canDownloadFile && ( - - - - - - - {gettext('Open parent folder')} - - - {showLockUnlockBtn && ( - - {lockUnlockText} - - )} - {showShareBtn && ( - - {gettext('Share')} - - )} - {filePerm == 'rw' && ( - - - {gettext('History')} - - - )} - {(canEditFile && !err) && ( - - - {gettext('Edit')} - - - )} - {canDownloadFile && ( - - - {gettext('Download')} - - - )} - {enableComment && ( - - {gettext('Comment')} - - )} - - - {this.state.isShareDialogOpen && -
+
diff --git a/frontend/src/view-file-text.js b/frontend/src/view-file-text.js index 3f697e7b17..53269eb401 100644 --- a/frontend/src/view-file-text.js +++ b/frontend/src/view-file-text.js @@ -1,8 +1,12 @@ import React from 'react'; import ReactDOM from 'react-dom'; +import PropTypes from 'prop-types'; +import toaster from './components/toast'; import { Utils } from './utils/utils'; +import { gettext } from './utils/constants'; import FileView from './components/file-view/file-view'; import FileViewTip from './components/file-view/file-view-tip'; +import { seafileAPI } from './utils/seafile-api'; import CodeMirror from 'react-codemirror'; import 'codemirror/mode/javascript/javascript'; @@ -20,7 +24,7 @@ import 'codemirror/lib/codemirror.css'; import './css/text-file-view.css'; const { - err, fileExt, fileContent + err, fileExt, fileContent, repoID, filePath, fileName } = window.app.pageOptions; const options = { @@ -30,18 +34,76 @@ const options = { theme: 'default', textWrapping: true, lineWrapping: true, - readOnly: true, - cursorBlinkRate: -1 // hide the cursor + readOnly: false, // set false to let user edit directly }; class ViewFileText extends React.Component { + constructor(props) { + super(props); + this.state = { + content: fileContent, + isContentChangedButNotSaved: false, + isSaving: false, + }; + this.onSaveChangedContent=this.onSaveChangedContent.bind(this); + } + + + updateContent = (newContent) => { + this.setState({ + isContentChangedButNotSaved: true, + content: newContent, + }); + } + + onSaveChangedContent () { + let dirPath = '/'; + return ( + seafileAPI.getUpdateLink(repoID, dirPath).then((res) => { + const uploadLink = res.data; + this.setState({ + isSaving: true + }); + return seafileAPI.updateFile( + uploadLink, + filePath, + fileName, + this.state.content + ).then(() => { + toaster.success(gettext('Successfully saved'), { + duration: 3 + }); + this.setState({ + isSaving: false, + isContentChangedButNotSaved: false + }); + }); + }) + ); + } + render() { return ( - } /> + + } + isSaving={this.state.isSaving} + isContentChangedButNotSaved={this.state.isContentChangedButNotSaved} + onSaveChangedContent={this.onSaveChangedContent} + /> ); } } +const propTypes = { + updateContent: PropTypes.func.isRequired, + content: PropTypes.string.isRequired, +}; + class FileContent extends React.Component { render() { if (err) { @@ -51,14 +113,17 @@ class FileContent extends React.Component {
); } } +FileContent.propTypes = propTypes; + ReactDOM.render ( , document.getElementById('wrapper')