1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-01 23:20:51 +00:00

change the mechanism of saving txt file, use md file's way of saving

This commit is contained in:
LeoSirius
2019-04-18 18:18:11 +08:00
parent 56bb4fa5fe
commit cdc9d4865e
3 changed files with 76 additions and 15 deletions

View File

@@ -10,6 +10,7 @@ import ShareDialog from '../dialog/share-dialog';
const propTypes = { const propTypes = {
isLocked: PropTypes.bool.isRequired, isLocked: PropTypes.bool.isRequired,
lockedByMe: PropTypes.bool.isRequired, lockedByMe: PropTypes.bool.isRequired,
onSaveChangedContent: PropTypes.func.isRequired,
toggleLockFile: PropTypes.func.isRequired, toggleLockFile: PropTypes.func.isRequired,
toggleCommentPanel: PropTypes.func.isRequired toggleCommentPanel: PropTypes.func.isRequired
}; };
@@ -38,7 +39,6 @@ class FileToolbar extends React.Component {
render() { render() {
const { isLocked, lockedByMe } = this.props; const { isLocked, lockedByMe } = this.props;
let showLockUnlockBtn = false; let showLockUnlockBtn = false;
let lockUnlockText, lockUnlockIcon; let lockUnlockText, lockUnlockIcon;
if (canLockUnlockFile) { if (canLockUnlockFile) {
@@ -94,14 +94,17 @@ class FileToolbar extends React.Component {
href={`${siteRoot}repo/file_revisions/${repoID}/?p=${encodeURIComponent(filePath)}&referer=${encodeURIComponent(location.href)}`} href={`${siteRoot}repo/file_revisions/${repoID}/?p=${encodeURIComponent(filePath)}&referer=${encodeURIComponent(location.href)}`}
/> />
)} )}
{(canEditFile && !err) && ( {(canEditFile && !err) &&
<IconButton (<IconButton
id="edit" text={gettext('Save')}
icon="fa fa-edit" id={'saveButton'}
text={gettext('Edit')} icon={'fa fa-save'}
tag="a" // button imported in this file does not have functionalities of
href={`${siteRoot}repo/${repoID}/file/edit/?p=${encodeURIComponent(filePath)}&file_enc=${encodeURIComponent(fileEnc)}`} // disabled, isActive as button imported in markdowneditor has
/> //disabled={isContentChanged}
//isActive={!isContentChanged}
onClick={this.props.onSaveChangedContent}
/>
)} )}
{canDownloadFile && ( {canDownloadFile && (
<IconButton <IconButton

View File

@@ -10,6 +10,7 @@ import CommentPanel from './comment-panel';
import '../../css/file-view.css'; import '../../css/file-view.css';
const propTypes = { const propTypes = {
onSaveChangedContent: PropTypes.func.isRequired,
content: PropTypes.object.isRequired content: PropTypes.object.isRequired
}; };
@@ -17,6 +18,7 @@ const { isStarred, isLocked, lockedByMe,
repoID, filePath, enableWatermark, userNickName repoID, filePath, enableWatermark, userNickName
} = window.app.pageOptions; } = window.app.pageOptions;
class FileView extends React.Component { class FileView extends React.Component {
constructor(props) { constructor(props) {
@@ -25,7 +27,7 @@ class FileView extends React.Component {
isStarred: isStarred, isStarred: isStarred,
isLocked: isLocked, isLocked: isLocked,
lockedByMe: lockedByMe, lockedByMe: lockedByMe,
isCommentPanelOpen: false isCommentPanelOpen: false,
}; };
} }
@@ -81,6 +83,7 @@ class FileView extends React.Component {
<FileToolbar <FileToolbar
isLocked={this.state.isLocked} isLocked={this.state.isLocked}
lockedByMe={this.state.lockedByMe} lockedByMe={this.state.lockedByMe}
onSaveChangedContent={this.props.onSaveChangedContent}
toggleLockFile={this.toggleLockFile} toggleLockFile={this.toggleLockFile}
toggleCommentPanel={this.toggleCommentPanel} toggleCommentPanel={this.toggleCommentPanel}
/> />

View File

@@ -1,8 +1,13 @@
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import toaster from './components/toast';
import { Utils } from './utils/utils'; import { Utils } from './utils/utils';
import { gettext } from './utils/constants';
import FileView from './components/file-view/file-view'; import FileView from './components/file-view/file-view';
import FileViewTip from './components/file-view/file-view-tip'; import FileViewTip from './components/file-view/file-view-tip';
import { seafileAPI } from './utils/seafile-api';
import CodeMirror from 'react-codemirror'; import CodeMirror from 'react-codemirror';
import 'codemirror/mode/javascript/javascript'; import 'codemirror/mode/javascript/javascript';
@@ -20,7 +25,7 @@ import 'codemirror/lib/codemirror.css';
import './css/text-file-view.css'; import './css/text-file-view.css';
const { const {
err, fileExt, fileContent err, fileExt, fileContent, repoID, filePath, fileName
} = window.app.pageOptions; } = window.app.pageOptions;
const options = { const options = {
@@ -30,18 +35,65 @@ const options = {
theme: 'default', theme: 'default',
textWrapping: true, textWrapping: true,
lineWrapping: true, lineWrapping: true,
readOnly: true, readOnly: false, // set false to let user edit direclty
cursorBlinkRate: -1 // hide the cursor //cursorBlinkRate: -1 to hide the cursor. The default blink rate is 530ms.
}; };
class ViewFileText extends React.Component { class ViewFileText extends React.Component {
constructor(props) {
super(props);
this.state = {
content: fileContent
};
this.onSaveChangedContent=this.onSaveChangedContent.bind(this);
}
updateContent = (newContent) => {
this.setState({
content: newContent
});
}
onSaveChangedContent () {
let dirPath = '/';
return (
seafileAPI.getUpdateLink(repoID, dirPath).then((res) => {
const uploadLink = res.data;
return seafileAPI.updateFile(
uploadLink,
filePath,
fileName,
this.state.content
).then(() => {
toaster.success(gettext('Successfully saved'), {
duration: 3
});
})
})
);
}
render() { render() {
return ( return (
<FileView content={<FileContent />} /> <FileView
content={
<FileContent
content={this.state.content}
updateContent={this.updateContent}
/>
}
onSaveChangedContent={this.onSaveChangedContent}
/>
); );
} }
} }
const propTypes = {
updateContent: PropTypes.func.isRequired,
content: PropTypes.string.isRequired,
};
class FileContent extends React.Component { class FileContent extends React.Component {
render() { render() {
if (err) { if (err) {
@@ -51,14 +103,17 @@ class FileContent extends React.Component {
<div className="file-view-content flex-1 text-file-view"> <div className="file-view-content flex-1 text-file-view">
<CodeMirror <CodeMirror
ref="code-mirror-editor" ref="code-mirror-editor"
value={fileContent} value={this.props.content}
options={options} options={options}
onChange={this.props.updateContent}
/> />
</div> </div>
); );
} }
} }
FileContent.propTypes = propTypes;
ReactDOM.render ( ReactDOM.render (
<ViewFileText />, <ViewFileText />,
document.getElementById('wrapper') document.getElementById('wrapper')