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

336 lines
9.7 KiB
JavaScript
Raw Normal View History

2020-05-27 17:07:36 +08:00
import React, { Component } from "react";
2021-10-09 13:37:56 +08:00
import { Popconfirm , Select , Dropdown , Spin , Anchor } from "antd";
2021-08-12 11:45:56 +08:00
import "./list.scss";
2020-05-27 17:07:36 +08:00
import axios from "axios";
import Meditor from "../Newfile/m_editor";
2020-07-02 16:41:06 +08:00
import RenderHtml from "../../components/render-html";
2021-10-09 13:37:56 +08:00
import ReadmeCatelogue from "./sub/ReadmeCatelogue";
2020-07-02 16:41:06 +08:00
2021-10-09 13:37:56 +08:00
const $ = window.$;
2020-03-16 14:12:11 +08:00
function bytesToSize(bytes) {
2020-05-27 17:07:36 +08:00
if (bytes === 0) return "0 B";
2020-03-16 14:12:11 +08:00
let k = 1024,
2020-05-27 17:07:36 +08:00
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];
2020-03-16 14:12:11 +08:00
}
2020-05-27 17:07:36 +08:00
class CoderRootFileDetail extends Component {
constructor(props) {
2020-03-16 14:12:11 +08:00
super(props);
2020-05-27 17:07:36 +08:00
this.state = {
value: undefined,
2020-06-24 14:30:02 +08:00
language: undefined,
2020-06-24 15:25:19 +08:00
languages: undefined,
2021-10-09 13:37:56 +08:00
description: props.detail.content,
menuList:undefined
2020-05-27 17:07:36 +08:00
};
2020-03-16 14:12:11 +08:00
}
2020-05-27 17:07:36 +08:00
componentDidMount = () => {
window.scrollTo(0, 0);
2021-04-20 15:31:49 +08:00
const { detail , mdFlag } = this.props;
2020-03-16 14:12:11 +08:00
this.setState({
2020-04-13 17:45:36 +08:00
value: detail.content,
2020-05-27 17:07:36 +08:00
});
2020-06-24 14:30:02 +08:00
this.languages_total();
};
componentDidUpdate=(prevProps)=>{
const { content } = this.props && this.props.detail;
const prevcontent = prevProps.detail && prevProps.detail.content;
if (content && prevcontent) {
if (prevcontent !== content){
this.setState({
description: content
});
}
}
}
2020-06-24 14:30:02 +08:00
languages_total = () => {
const { detail } = this.props;
2020-06-24 15:25:19 +08:00
const file_name = detail.path.split("/").pop().split(".").pop();
2020-06-24 14:30:02 +08:00
let languages = [];
let default_language = "javascript";
let all_languages = {
apex: ["apex", "apxc"],
azcli: ["azcli"],
bat: ["bat"],
clojure: ["clj"],
coffee: ["coffee"],
cpp: ["cpp"],
csharp: ["cs"],
csp: ["csp"],
css: ["css"],
dockerfile: ["dockerfile", "docker", "yml"],
fsharp: ["fs"],
go: ["go"],
html: ["html", "htm", "erb"],
ini: ["ini"],
java: ["java", "class"],
javascript: ["js"],
json: ["json"],
less: ["less"],
lua: ["lua"],
markdown: ["markdown", "md", "rmd"],
msdax: ["dax"],
mysql: ["sql"],
objective: ["m", "mm", "o", "out"],
perl: ["perl"],
pgsql: ["sql"],
php: ["php"],
postiats: ["postiats"],
powerquery: [""],
powershell: ["ps1"],
pug: ["pug"],
python: ["py"],
r: ["r"],
razor: ["cshtml"],
redis: ["rdb"],
ruby: ["rb"],
rust: ["rs"],
sb: ["sb"],
scheme: ["scm", "ss"],
scss: ["scss"],
shell: ["sh"],
solidity: ["sol"],
sql: ["sql"],
st: ["st"],
swift: ["swift"],
typescript: ["ts"],
vb: ["vbp", "frm", "frx", "bas", "cls"],
xml: ["xml"],
yaml: ["yml"],
};
2020-06-24 15:25:19 +08:00
for (var item in all_languages) {
languages.push(item);
let item_values = all_languages[item];
if (item_values.indexOf(file_name) !== -1) {
default_language = item;
2020-06-24 14:30:02 +08:00
}
}
2020-09-30 16:34:03 +08:00
this.setState({
languages,
language: default_language
})
2020-05-27 17:07:36 +08:00
};
2020-06-24 14:30:02 +08:00
2020-05-27 17:07:36 +08:00
select_language = (e) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-27 17:07:36 +08:00
language: e,
});
};
2020-06-11 18:00:32 +08:00
EditFile = (flag) => {
const { onEdit } = this.props;
onEdit && onEdit(flag);
2020-05-27 17:07:36 +08:00
};
2020-03-16 14:12:11 +08:00
2020-09-30 16:34:03 +08:00
DownLoadFile = (url) => {
2020-09-29 14:25:16 +08:00
let download_url = "/attachments/entries/get_file?download_url=" + url
window.open(download_url)
}
2020-03-16 14:12:11 +08:00
// 编辑文件
2020-03-29 18:16:58 +08:00
2020-05-27 17:07:36 +08:00
changeMmirror = (e, e1, value) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-05-27 17:07:36 +08:00
value,
});
};
2020-03-16 14:12:11 +08:00
2020-05-27 17:07:36 +08:00
deleteFile = () => {
const { branch, detail } = this.props;
2020-09-30 16:34:03 +08:00
const { projectsId, owner } = this.props.match.params;
2020-03-16 14:12:11 +08:00
2020-08-13 18:10:08 +08:00
const url = `/${owner}/${projectsId}/delete_file.json`;
2020-09-30 16:34:03 +08:00
axios.delete(url, {
params: {
filepath: detail.path,
branch,
sha: detail.sha,
},
})
2020-05-27 17:07:36 +08:00
.then((result) => {
if (result) {
this.props.showNotification("删除成功!");
2021-09-01 09:33:22 +08:00
this.props.history.push(`/${owner}/${projectsId}`);
2020-05-27 17:07:36 +08:00
}
})
.catch((error) => {
console.log(error);
});
};
2020-03-16 14:12:11 +08:00
2020-05-27 17:07:36 +08:00
updateCode = (value) => {
this.setState({
value,
});
};
2020-03-16 14:12:11 +08:00
2020-07-02 16:41:06 +08:00
onContentChange = (value) => {
this.setState({
description: value,
});
};
2021-10-09 13:37:56 +08:00
renderMenulist=()=>{
const { description } = this.state;
if(description){
const items = $.map($("#files-md").find("h1,h2,h3,h4,h5,h6"), function (el, _) {
const anchor = el.id;
const level = el.tagName.replace("H", "");
const href = `#${anchor}`;
return { href:`${href}`,text:el.textContent , level:level }
});
return items;
}
return [];
}
menu=()=>{
const menuList = this.renderMenulist();
if(menuList && menuList.length > 0){
return(
<ReadmeCatelogue menuList={menuList} hash={this.props.history.location.hash}/>
)
}else{
return <Spin />
}
}
2020-05-27 17:07:36 +08:00
render() {
2020-06-24 14:30:02 +08:00
const {
readOnly,
detail,
current_user,
isManager,
isDeveloper,
2020-10-22 22:24:49 +08:00
currentBranch,
platform,
2021-04-20 15:31:49 +08:00
md,
type
2020-06-24 14:30:02 +08:00
} = this.props;
2020-09-30 16:34:03 +08:00
const { language, languages, description } = this.state;
2020-03-30 15:06:30 +08:00
let flag = current_user && current_user.login && (isManager || isDeveloper);
2020-05-27 17:07:36 +08:00
const Option = Select.Option;
return (
2021-03-18 17:28:53 +08:00
<React.Fragment>
2021-11-09 16:38:53 +08:00
<Anchor className="griditemAnchor" offsetTop={58}>
2021-10-09 13:37:56 +08:00
<div className="griditemCate">
{
md && readOnly &&
<Dropdown overlay={this.menu()} trigger={['hover']} overlayClassName="menuslist">
<span className="catelogue mr20">
<i className="iconfont icon-muluicon font-12 mr5"></i>
<span>目录</span>
</span>
</Dropdown>
}
<span className="color-grey-6 font-16">
2020-06-24 14:30:02 +08:00
{bytesToSize(detail && detail.size)}
</span>
2020-05-27 17:07:36 +08:00
</div>
<p className="text-right">
2021-10-09 13:37:56 +08:00
{flag && platform && (
<div>
{readOnly ? (
<span>
{
!detail.direct_download?
<span>
<a onClick={() => this.DownLoadFile(detail.download_url)} className="ml20">
<i className="iconfont icon-xiazai1 font-15 color-grey-6"></i>
</a>
2021-10-09 13:37:56 +08:00
{
type !==2 &&
<a onClick={() => this.EditFile(false)} className="ml20">
<i className="iconfont icon-bianji1 font-15 color-grey-6"></i>
</a>
}
</span>:""
}
</span>
) : (
<React.Fragment>
<Select
showSearch={true}
placeholder={"请选择文本语言"}
style={{ width: 200 }}
value={language}
onChange={this.select_language}
>
<Option value={undefined}>请选择文本语言</Option>
{languages &&
languages.map((item, key) => {
return (
<Option value={item} key={key}>
{item}
</Option>
);
})}
</Select>
<button
type="button"
className="ant-btn ant-btn-sm ml20"
onClick={() => this.EditFile(true)}
>
<span> </span>
</button>
</React.Fragment>
)}
{
type !==2 &&
<Popconfirm
title="确认删除这个文件?"
className="ml20"
okText="确定"
cancelText="取消"
onConfirm={this.deleteFile}
>
<a>
<i className="iconfont icon-shanchu font-15 color-grey-6"></i>
</a>
</Popconfirm>
}
</div>
)}
</p>
</Anchor>
<div>
2020-06-24 14:59:19 +08:00
{detail.image_type ? (
2020-06-05 11:46:56 +08:00
<div className="edu-txt-center pt20 pb20">
2021-04-21 16:54:47 +08:00
<img alt="" src={detail.download_url} style={{ maxWidth: "80%" }} />
2020-05-27 17:07:36 +08:00
</div>
2020-06-24 15:25:19 +08:00
) : detail.direct_download ? (
2020-06-24 15:29:31 +08:00
<div className="mt20 text-center">
2020-06-24 15:25:19 +08:00
<a href={detail.download_url} className="color-blue font-15">
下载原始文件
</a>
</div>
2020-05-27 17:07:36 +08:00
) : (
2020-10-16 10:35:51 +08:00
md && readOnly ?
2021-10-09 13:37:56 +08:00
<div className="files-md" id="files-md">
2020-12-15 18:01:16 +08:00
<RenderHtml className="file-md imageLayerParent" value={description} url={this.props.history.location}/>
2020-10-16 10:35:51 +08:00
</div>
:
<Meditor
{...this.props}
{...this.state}
language={language ? language : "javascript"}
filepath={`/${detail.path}`}
content={description}
2020-10-16 10:35:51 +08:00
readOnly={readOnly}
editorType="update"
currentBranch={currentBranch}
descName={detail && `Update ${detail.name}`}
2020-10-16 10:35:51 +08:00
></Meditor>
)}
2020-03-16 14:12:11 +08:00
</div>
2021-03-18 17:28:53 +08:00
</React.Fragment>
2020-05-27 17:07:36 +08:00
);
2020-03-16 14:12:11 +08:00
}
}
2020-03-29 18:16:58 +08:00
export default CoderRootFileDetail;