Files
SCM-Manager/scm-ui-components/packages/ui-components/src/repos/DiffFile.js

179 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-02-26 15:00:05 +01:00
//@flow
import React from "react";
2019-02-27 11:56:50 +01:00
import {
Hunk,
Diff as DiffComponent,
getChangeKey,
Change,
DiffObjectProps,
File
} from "react-diff-view";
2019-02-26 15:00:05 +01:00
import injectSheets from "react-jss";
import classNames from "classnames";
2019-02-27 11:56:50 +01:00
import { translate } from "react-i18next";
2019-02-26 15:00:05 +01:00
const styles = {
panel: {
fontSize: "1rem"
},
header: {
cursor: "pointer"
},
title: {
marginLeft: ".25rem",
fontSize: "1rem"
},
hunkDivider: {
margin: ".5rem 0"
}
};
2019-02-27 11:56:50 +01:00
type Props = DiffObjectProps & {
file: File,
2019-02-26 15:00:05 +01:00
// context props
classes: any,
t: string => string
2019-02-27 11:56:50 +01:00
};
2019-02-26 15:00:05 +01:00
type State = {
collapsed: boolean
2019-02-27 11:56:50 +01:00
};
2019-02-26 15:00:05 +01:00
class DiffFile extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
collapsed: false
};
}
toggleCollapse = () => {
2019-02-27 11:56:50 +01:00
this.setState(state => ({
collapsed: !state.collapsed
2019-02-26 15:00:05 +01:00
}));
};
2019-02-27 11:56:50 +01:00
createHunkHeader = (hunk: Hunk, i: number) => {
2019-02-26 15:00:05 +01:00
const { classes } = this.props;
if (i > 0) {
2019-02-27 11:56:50 +01:00
return <hr className={classes.hunkDivider} />;
}
return null;
};
collectHunkAnnotations = (hunk: Hunk) => {
const { annotationFactory, file } = this.props;
if (annotationFactory) {
return annotationFactory({
hunk,
file
});
}
};
handleClickEvent = (change: Change, hunk: Hunk) => {
const { file, onClick } = this.props;
const context = {
changeId: getChangeKey(change),
change,
hunk,
file
};
if (onClick) {
onClick(context);
2019-02-26 15:00:05 +01:00
}
2019-02-27 11:56:50 +01:00
};
createCustomEvents = (hunk: Hunk) => {
const { onClick } = this.props;
if (onClick) {
return {
gutter: {
onClick: (change: Change) => {
this.handleClickEvent(change, hunk);
}
}
};
}
};
renderHunk = (hunk: Hunk, i: number) => {
return (
<Hunk
key={hunk.content}
hunk={hunk}
header={this.createHunkHeader(hunk, i)}
widgets={this.collectHunkAnnotations(hunk)}
customEvents={this.createCustomEvents(hunk)}
/>
);
2019-02-26 15:00:05 +01:00
};
renderFileTitle = (file: any) => {
2019-02-27 11:56:50 +01:00
if (
file.oldPath !== file.newPath &&
(file.type === "copy" || file.type === "rename")
) {
return (
<>
{file.oldPath} <i className="fa fa-arrow-right" /> {file.newPath}
</>
);
2019-02-26 15:00:05 +01:00
} else if (file.type === "delete") {
return file.oldPath;
}
return file.newPath;
};
renderChangeTag = (file: any) => {
const { t } = this.props;
const key = "diff.changes." + file.type;
let value = t(key);
if (key === value) {
value = file.type;
}
2019-02-27 11:56:50 +01:00
return <span className="tag is-info has-text-weight-normal">{value}</span>;
2019-02-26 15:00:05 +01:00
};
render() {
const { file, sideBySide, classes } = this.props;
const { collapsed } = this.state;
const viewType = sideBySide ? "split" : "unified";
let body = null;
let icon = "fa fa-angle-right";
if (!collapsed) {
icon = "fa fa-angle-down";
body = (
<div className="panel-block is-paddingless is-size-7">
<DiffComponent viewType={viewType}>
2019-02-27 11:56:50 +01:00
{file.hunks.map(this.renderHunk)}
2019-02-26 15:00:05 +01:00
</DiffComponent>
</div>
);
}
return (
<div className={classNames("panel", classes.panel)}>
2019-02-27 11:56:50 +01:00
<div
className={classNames("panel-heading", classes.header)}
onClick={this.toggleCollapse}
>
2019-02-26 15:00:05 +01:00
<div className="level">
<div className="level-left">
2019-02-27 11:56:50 +01:00
<i className={icon} />
<span className={classes.title}>
{this.renderFileTitle(file)}
</span>
2019-02-26 15:00:05 +01:00
</div>
2019-02-27 11:56:50 +01:00
<div className="level-right">{this.renderChangeTag(file)}</div>
2019-02-26 15:00:05 +01:00
</div>
</div>
{body}
</div>
);
}
}
export default injectSheets(styles)(translate("repos")(DiffFile));