forgeplus-react/src/forge/Main/CoderRootFileDetail.js

171 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-03-16 14:12:11 +08:00
import React , { Component } from "react";
2020-03-26 18:12:53 +08:00
// import Editor from "react-monaco-editor";
2020-03-16 14:12:11 +08:00
import { Popconfirm, Result } from 'antd';
import './list.css';
import axios from 'axios';
2020-03-27 18:29:00 +08:00
2020-03-29 18:16:58 +08:00
import CodeMirror from 'react-codemirror';
2020-03-27 18:29:00 +08:00
require('codemirror/lib/codemirror.css');
require('codemirror/mode/javascript/javascript');
require('codemirror/mode/xml/xml');
require('codemirror/mode/markdown/markdown');
2020-03-16 14:12:11 +08:00
function bytesToSize(bytes) {
if (bytes === 0) return '0 B';
let k = 1024,
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)). toFixed(2) + ' ' + sizes[i];
}
class CoderRootFileDetail extends Component{
constructor(props){
super(props);
this.state={
readOnly:true,
value:undefined
}
}
componentDidMount=()=>{
const { detail } = this.props;
this.setState({
value: detail.content
})
}
EditFile=()=>{
this.setState({
readOnly:false
})
}
CancelEdit=()=>{
this.setState({
readOnly:true
})
}
// 编辑文件
2020-03-29 18:16:58 +08:00
2020-03-26 18:12:53 +08:00
changeMmirror=(e,e1,value)=>{
2020-03-16 14:12:11 +08:00
this.setState({
2020-03-26 18:12:53 +08:00
value
2020-03-16 14:12:11 +08:00
})
}
deleteFile=()=>{
2020-03-26 18:12:53 +08:00
const { branch , detail }= this.props;
2020-03-16 14:12:11 +08:00
const { projectsId } = this.props.match.params;
2020-03-23 17:19:44 +08:00
const url = `/repositories/${projectsId}/delete_file.json`;
2020-03-16 14:12:11 +08:00
axios.delete(url,{
params:{
filepath:detail.path,
2020-03-24 10:30:25 +08:00
branch,
sha:detail.sha
2020-03-16 14:12:11 +08:00
}
}).then(result=>{
if(result){
this.props.showNotification("删除成功!");
2020-03-24 12:01:12 +08:00
this.props.history.push(`/projects/${projectsId}`);
2020-03-16 14:12:11 +08:00
}
}).catch(error=>{
console.log(error);
})
}
// 确认修改文件
UpdateFile=()=>{
console.log("user", this.props)
const { author , branch , detail}= this.props;
const { projectsId } = this.props.match.params;
const { value } = this.state;
2020-03-23 17:19:44 +08:00
const url =`/repositories/${projectsId}/update_file.json`;
2020-03-16 14:12:11 +08:00
axios.put(url,{
filepath:detail.path,
branch,
content:value,
sha:detail.sha,
}).then(result=>{
if(result){
this.props.showNotification("修改成功!");
this.setState({
readOnly:true
})
}
}).catch(error=>{
console.log(error);
})
}
2020-03-27 18:29:00 +08:00
updateCode=(value)=> {
this.setState({
value,
});
}
2020-03-16 14:12:11 +08:00
render(){
2020-03-25 15:14:20 +08:00
const { detail , current_user , isManager , isReporter , isDeveloper } = this.props;
2020-03-16 14:12:11 +08:00
const { readOnly ,value } = this.state;
2020-03-30 15:06:30 +08:00
let flag = current_user && current_user.login && (isManager || isDeveloper);
2020-03-27 18:29:00 +08:00
var options = {
lineNumbers: true,
mode: 'javascript',
readOnly:readOnly?'nocursor':false,
2020-03-31 15:07:27 +08:00
autofocus:readOnly?false:true,
styleActiveLine:true
2020-03-27 18:29:00 +08:00
};
2020-03-16 14:12:11 +08:00
return(
<div>
<div className="branchTable">
<p className="branchTitle f-wrap-alignCenter f-wrap-between">
<span>{bytesToSize(detail && detail.size)}</span>
{
2020-03-25 15:14:20 +08:00
flag &&
<span>
{
readOnly ?
<a onClick={this.EditFile} className="ml20"><i className="iconfont icon-bianji font-15 color-grey-6"></i></a>
:
<React.Fragment>
<button type="button" className="ant-btn ant-btn-sm mr10" onClick={this.CancelEdit}><span> </span>
</button>
<button type="button" className="ant-btn ant-btn-primary ant-btn-sm" onClick={this.UpdateFile}><span> </span>
</button>
</React.Fragment>
}
<Popconfirm title="确认删除这个文件?" className="ml20" okText="确定" cancelText="取消" onConfirm={this.deleteFile}>
<a><i className="iconfont icon-shanchu font-15 color-grey-6"></i></a>
</Popconfirm>
</span>
2020-03-16 14:12:11 +08:00
}
</p>
</div>
<div className="content-file">
{
detail.direct_download ?
<div className="text-center">
<a href={detail.download_url} className="color-blue font-15">下载原始文件</a>
</div>
:
2020-03-27 18:29:00 +08:00
<CodeMirror ref="editor" value={detail.content} onChange={this.updateCode} options={options} />
2020-03-26 18:12:53 +08:00
// <Editor
// height="300px"
// theme={"vs-dark"}
// value={value}
// onChange={this.changeContent}
// readOnly={readOnly}
// />
2020-03-16 14:12:11 +08:00
}
</div>
</div>
)
}
}
2020-03-29 18:16:58 +08:00
export default CoderRootFileDetail;