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

316 lines
7.3 KiB
JavaScript
Raw Normal View History

2019-02-26 15:00:05 +01:00
//@flow
import React from "react";
import { translate } from "react-i18next";
import classNames from "classnames";
import styled from "styled-components";
2019-09-16 17:39:22 +02:00
import {
Change,
Diff as DiffComponent,
DiffObjectProps,
File,
getChangeKey,
Hunk
} from "react-diff-view";
import { Button, ButtonGroup } from "../buttons";
import Tag from "../Tag";
2019-10-10 11:37:14 +02:00
import Icon from "../Icon";
2019-02-26 15:00:05 +01:00
2019-02-27 11:56:50 +01:00
type Props = DiffObjectProps & {
file: File,
2019-10-10 11:37:14 +02:00
defaultCollapse: boolean,
2019-02-26 15:00:05 +01:00
// context props
t: string => string
2019-02-27 11:56:50 +01:00
};
2019-02-26 15:00:05 +01:00
type State = {
collapsed: boolean,
sideBySide: boolean
2019-02-27 11:56:50 +01:00
};
2019-02-26 15:00:05 +01:00
const DiffFilePanel = styled.div`
2019-10-10 11:37:14 +02:00
/* remove bottom border for collapsed panels */
${props => (props.collapsed ? "border-bottom: none;" : "")};
`;
const FlexWrapLevel = styled.div`
/* breaks into a second row
when buttons and title become too long */
flex-wrap: wrap;
`;
const FullWidthTitleHeader = styled.div`
max-width: 100%;
`;
const TitleWrapper = styled.span`
margin-left: 0.25rem;
`;
const ButtonWrapper = styled.div`
/* align child to right */
margin-left: auto;
`;
const HunkDivider = styled.hr`
margin: 0.5rem 0;
`;
const ChangeTypeTag = styled(Tag)`
2019-10-10 11:37:14 +02:00
marginleft: ".75rem";
`;
const ModifiedDiffComponent = styled(DiffComponent)`
/* column sizing */
> colgroup .diff-gutter-col {
width: 3.25rem;
}
/* prevent following content from moving down */
> .diff-gutter:empty:hover::after {
font-size: 0.7rem;
}
/* smaller font size for code */
& .diff-line {
font-size: 0.75rem;
}
/* comment padding for sidebyside view */
&.split .diff-widget-content .is-indented-line {
padding-left: 3.25rem;
}
/* comment padding for combined view */
&.unified .diff-widget-content .is-indented-line {
padding-left: 6.5rem;
}
`;
2019-02-26 15:00:05 +01:00
class DiffFile extends React.Component<Props, State> {
2019-10-10 11:37:14 +02:00
static defaultProps = {
defaultCollapse: false
};
2019-02-26 15:00:05 +01:00
constructor(props: Props) {
super(props);
this.state = {
2019-10-10 11:37:14 +02:00
collapsed: this.props.defaultCollapse,
sideBySide: false
2019-02-26 15:00:05 +01:00
};
}
2019-10-10 11:37:14 +02:00
// collapse diff by clicking collapseDiffs button
componentDidUpdate(prevProps) {
const { defaultCollapse } = this.props;
if (prevProps.defaultCollapse !== defaultCollapse) {
this.setState({
collapsed: defaultCollapse
});
}
}
2019-02-26 15:00:05 +01:00
toggleCollapse = () => {
2019-10-10 11:37:14 +02:00
const { file } = this.props;
if (file && !file.isBinary) {
this.setState(state => ({
collapsed: !state.collapsed
}));
}
2019-02-26 15:00:05 +01:00
};
toggleSideBySide = () => {
this.setState(state => ({
sideBySide: !state.sideBySide
}));
};
setCollapse = (collapsed: boolean) => {
this.setState({
collapsed
});
};
2019-02-27 11:56:50 +01:00
createHunkHeader = (hunk: Hunk, i: number) => {
2019-02-26 15:00:05 +01:00
if (i > 0) {
return <HunkDivider />;
2019-02-27 11:56:50 +01:00
}
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 (
<>
2019-10-10 11:37:14 +02:00
{file.oldPath} <Icon name="arrow-right" color="inherit" />{" "}
{file.newPath}
2019-02-27 11:56:50 +01:00
</>
);
2019-02-26 15:00:05 +01:00
} else if (file.type === "delete") {
return file.oldPath;
}
return file.newPath;
};
2019-05-26 14:10:59 +02:00
hoverFileTitle = (file: any) => {
if (
file.oldPath !== file.newPath &&
(file.type === "copy" || file.type === "rename")
) {
return (
<>
{file.oldPath} > {file.newPath}
</>
);
} else if (file.type === "delete") {
return file.oldPath;
}
return file.newPath;
};
2019-02-26 15:00:05 +01:00
renderChangeTag = (file: any) => {
const { t } = this.props;
if (!file.type) {
return;
}
2019-02-26 15:00:05 +01:00
const key = "diff.changes." + file.type;
let value = t(key);
if (key === value) {
value = file.type;
}
const color =
value === "added"
? "success is-outlined"
: value === "deleted"
? "danger is-outlined"
: "info is-outlined";
2019-05-26 14:10:59 +02:00
return (
<ChangeTypeTag
className={classNames("is-rounded", "has-text-weight-normal")}
2019-09-16 17:39:22 +02:00
color={color}
label={value}
/>
2019-05-26 14:10:59 +02:00
);
2019-02-26 15:00:05 +01:00
};
render() {
2019-10-10 11:37:14 +02:00
const { file, fileControlFactory, fileAnnotationFactory, t } = this.props;
const { collapsed, sideBySide } = this.state;
2019-02-26 15:00:05 +01:00
const viewType = sideBySide ? "split" : "unified";
let body = null;
2019-10-10 11:37:14 +02:00
let icon = "angle-right";
2019-02-26 15:00:05 +01:00
if (!collapsed) {
const fileAnnotations = fileAnnotationFactory
? fileAnnotationFactory(file)
: null;
2019-10-10 11:37:14 +02:00
icon = "angle-down";
2019-02-26 15:00:05 +01:00
body = (
<div className="panel-block is-paddingless">
{fileAnnotations}
<ModifiedDiffComponent className={viewType} viewType={viewType}>
2019-02-27 11:56:50 +01:00
{file.hunks.map(this.renderHunk)}
</ModifiedDiffComponent>
2019-02-26 15:00:05 +01:00
</div>
);
}
2019-10-10 11:37:14 +02:00
const collapseIcon = file && !file.isBinary ? (
<Icon name={icon} color="inherit" />
) : null;
2019-02-26 15:00:05 +01:00
const fileControls = fileControlFactory
? fileControlFactory(file, this.setCollapse)
: null;
return (
<DiffFilePanel
className={classNames("panel", "is-size-6")}
2019-10-10 11:37:14 +02:00
collapsed={(file && file.isBinary) || collapsed}
>
<div className="panel-heading">
<FlexWrapLevel className="level">
<FullWidthTitleHeader
className={classNames(
"level-left",
"is-flex",
"has-cursor-pointer"
)}
onClick={this.toggleCollapse}
2019-05-26 14:10:59 +02:00
title={this.hoverFileTitle(file)}
>
{collapseIcon}
<TitleWrapper
className={classNames("is-ellipsis-overflow", "is-size-6")}
2019-05-26 14:10:59 +02:00
>
2019-02-27 11:56:50 +01:00
{this.renderFileTitle(file)}
</TitleWrapper>
2019-05-26 14:10:59 +02:00
{this.renderChangeTag(file)}
</FullWidthTitleHeader>
<ButtonWrapper className={classNames("level-right", "is-flex")}>
2019-06-20 14:57:00 +02:00
<ButtonGroup>
2019-06-20 14:33:16 +02:00
<Button
action={this.toggleSideBySide}
2019-10-10 11:37:14 +02:00
icon={sideBySide ? "align-left" : "columns"}
label={t(sideBySide ? "diff.combined" : "diff.sideBySide")}
reducedMobile={true}
/>
2019-06-20 14:33:16 +02:00
{fileControls}
</ButtonGroup>
</ButtonWrapper>
</FlexWrapLevel>
2019-02-26 15:00:05 +01:00
</div>
{body}
</DiffFilePanel>
);
2019-02-26 15:00:05 +01:00
}
}
export default translate("repos")(DiffFile);