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

313 lines
7.1 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-02-26 15:00:05 +01:00
2019-02-27 11:56:50 +01:00
type Props = DiffObjectProps & {
file: File,
collapsible: true,
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`
${props =>
props.file &&
props.file.isBinary && {
borderBottom: "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)`
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> {
constructor(props: Props) {
super(props);
this.state = {
collapsed: false,
sideBySide: false
2019-02-26 15:00:05 +01:00
};
}
toggleCollapse = () => {
if (this.props.collapsable) {
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 (
<>
{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;
};
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() {
const {
file,
fileControlFactory,
fileAnnotationFactory,
collapsible,
t
} = this.props;
const { collapsed, sideBySide } = this.state;
2019-02-26 15:00:05 +01:00
const viewType = sideBySide ? "split" : "unified";
let body = null;
let icon = "fa fa-angle-right";
if (!collapsed) {
const fileAnnotations = fileAnnotationFactory
? fileAnnotationFactory(file)
: null;
2019-02-26 15:00:05 +01:00
icon = "fa fa-angle-down";
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-09-16 17:39:22 +02:00
const collapseIcon = collapsible ? <i className={icon} /> : 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")}>
<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}
className="reduced-mobile"
>
<span className="icon is-small">
<i
className={classNames(
"fas",
sideBySide ? "fa-align-left" : "fa-columns"
)}
/>
</span>
<span>
{t(sideBySide ? "diff.combined" : "diff.sideBySide")}
</span>
</Button>
{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);