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

133 lines
3.3 KiB
JavaScript
Raw Normal View History

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';
import 'codemirror/mode/css/css';
import 'codemirror/mode/clike/clike';
import 'codemirror/mode/php/php';
import 'codemirror/mode/sql/sql';
import 'codemirror/mode/vue/vue';
import 'codemirror/mode/xml/xml';
import 'codemirror/mode/go/go';
import 'codemirror/mode/python/python';
import 'codemirror/mode/htmlmixed/htmlmixed';
import 'codemirror/lib/codemirror.css';
import './css/text-file-view.css';
const {
err, fileExt, fileContent, repoID, filePath, fileName
} = window.app.pageOptions;
const options = {
lineNumbers: true,
mode: Utils.chooseLanguage(fileExt),
extraKeys: {'Ctrl': 'autocomplete'},
theme: 'default',
textWrapping: true,
lineWrapping: true,
readOnly: false, // set false to let user edit direclty
//cursorBlinkRate: -1 to hide the cursor. The default blink rate is 530ms.
};
class ViewFileText extends React.Component {
constructor(props) {
super(props);
this.state = {
2019-04-19 03:57:07 +00:00
content: fileContent,
isContentChangedButNotSaved: false,
isSaving: false,
};
this.onSaveChangedContent=this.onSaveChangedContent.bind(this);
}
updateContent = (newContent) => {
this.setState({
2019-04-19 03:57:07 +00:00
isContentChangedButNotSaved: true,
content: newContent,
});
}
onSaveChangedContent () {
let dirPath = '/';
return (
seafileAPI.getUpdateLink(repoID, dirPath).then((res) => {
const uploadLink = res.data;
2019-04-19 03:57:07 +00:00
this.setState({
isSaving: true
});
return seafileAPI.updateFile(
uploadLink,
filePath,
fileName,
this.state.content
).then(() => {
toaster.success(gettext('Successfully saved'), {
duration: 3
});
2019-04-19 03:57:07 +00:00
this.setState({
isSaving: false,
isContentChangedButNotSaved: false
});
})
})
);
}
render() {
return (
<FileView
content={
<FileContent
content={this.state.content}
updateContent={this.updateContent}
/>
}
2019-04-19 03:57:07 +00:00
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) {
return <FileViewTip />;
}
return (
<div className="file-view-content flex-1 text-file-view">
<CodeMirror
ref="code-mirror-editor"
value={this.props.content}
options={options}
onChange={this.props.updateContent}
/>
</div>
);
}
}
FileContent.propTypes = propTypes;
ReactDOM.render (
<ViewFileText />,
document.getElementById('wrapper')
2019-02-18 12:26:55 +00:00
);