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

275 lines
7.2 KiB
TypeScript
Raw Normal View History

import React from "react";
import { withTranslation, WithTranslation } from "react-i18next";
import classNames from "classnames";
import styled from "styled-components";
2019-10-21 14:10:48 +02:00
// @ts-ignore
2019-10-21 10:57:56 +02:00
import { Change, Diff as DiffComponent, getChangeKey, Hunk } from "react-diff-view";
import { Button, ButtonGroup } from "../buttons";
import Tag from "../Tag";
import Icon from "../Icon";
import { File, Hunk as HunkType, DiffObjectProps } from "./DiffTypes";
2019-02-26 15:00:05 +01:00
type Props = DiffObjectProps &
WithTranslation & {
file: File;
defaultCollapse?: boolean;
};
type Collapsible = {
collapsed?: boolean;
2019-02-27 11:56:50 +01:00
};
2019-02-26 15:00:05 +01:00
type State = Collapsible & {
2019-12-19 09:51:44 +01:00
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: Collapsible) => (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)`
margin-left: 0.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> {
static defaultProps: Partial<Props> = {
defaultCollapse: false
2019-10-10 11:37:14 +02:00
};
2019-02-26 15:00:05 +01:00
constructor(props: Props) {
super(props);
this.state = {
collapsed: !!this.props.defaultCollapse,
2019-12-19 09:51:44 +01:00
sideBySide: props.sideBySide
2019-02-26 15:00:05 +01:00
};
}
2019-10-10 11:37:14 +02:00
// collapse diff by clicking collapseDiffs button
componentDidUpdate(prevProps: Props) {
2019-10-10 11:37:14 +02:00
const { defaultCollapse } = this.props;
if (prevProps.defaultCollapse !== defaultCollapse) {
this.setState({
collapsed: defaultCollapse
2019-10-10 11:37:14 +02:00
});
}
}
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
});
};
createHunkHeader = (hunk: HunkType, 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: HunkType) => {
2019-02-27 11:56:50 +01:00
const { annotationFactory, file } = this.props;
if (annotationFactory) {
return annotationFactory({
hunk,
file
2019-02-27 11:56:50 +01:00
});
}
};
handleClickEvent = (change: Change, hunk: HunkType) => {
2019-02-27 11:56:50 +01:00
const { file, onClick } = this.props;
const context = {
changeId: getChangeKey(change),
change,
hunk,
file
2019-02-27 11:56:50 +01:00
};
if (onClick) {
onClick(context);
2019-02-26 15:00:05 +01:00
}
2019-02-27 11:56:50 +01:00
};
createCustomEvents = (hunk: HunkType) => {
2019-02-27 11:56:50 +01:00
const { onClick } = this.props;
if (onClick) {
return {
gutter: {
onClick: (change: Change) => {
this.handleClickEvent(change, hunk);
}
}
2019-02-27 11:56:50 +01:00
};
}
};
renderHunk = (hunk: HunkType, i: number) => {
2019-02-27 11:56:50 +01:00
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: File) => {
2019-10-21 10:57:56 +02:00
if (file.oldPath !== file.newPath && (file.type === "copy" || file.type === "rename")) {
2019-02-27 11:56:50 +01:00
return (
<>
2019-10-21 10:57:56 +02:00
{file.oldPath} <Icon name="arrow-right" color="inherit" /> {file.newPath}
2019-02-27 11:56:50 +01:00
</>
);
} else if (file.type === "delete") {
2019-02-26 15:00:05 +01:00
return file.oldPath;
}
return file.newPath;
};
hoverFileTitle = (file: File): string => {
2019-10-21 10:57:56 +02:00
if (file.oldPath !== file.newPath && (file.type === "copy" || file.type === "rename")) {
return `${file.oldPath} > ${file.newPath}`;
} else if (file.type === "delete") {
2019-05-26 14:10:59 +02:00
return file.oldPath;
}
return file.newPath;
};
renderChangeTag = (file: File) => {
const { t } = this.props;
if (!file.type) {
return;
}
const key = "diff.changes." + file.type;
2019-02-26 15:00:05 +01:00
let value = t(key);
if (key === value) {
value = file.type;
}
const color =
2019-10-21 10:57:56 +02:00
value === "added" ? "success is-outlined" : value === "deleted" ? "danger is-outlined" : "info is-outlined";
2019-10-21 10:57:56 +02:00
return <ChangeTypeTag className={classNames("is-rounded", "has-text-weight-normal")} color={color} label={value} />;
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;
const viewType = sideBySide ? "split" : "unified";
2019-02-26 15:00:05 +01:00
let body = null;
let icon = "angle-right";
2019-02-26 15:00:05 +01:00
if (!collapsed) {
2019-10-21 10:57:56 +02:00
const fileAnnotations = fileAnnotationFactory ? fileAnnotationFactory(file) : null;
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-21 10:57:56 +02:00
const collapseIcon = file && !file.isBinary ? <Icon name={icon} color="inherit" /> : null;
2019-02-26 15:00:05 +01:00
2019-10-21 10:57:56 +02:00
const fileControls = fileControlFactory ? fileControlFactory(file, this.setCollapse) : null;
return (
2019-10-21 10:57:56 +02:00
<DiffFilePanel className={classNames("panel", "is-size-6")} collapsed={(file && file.isBinary) || collapsed}>
<div className="panel-heading">
<FlexWrapLevel className="level">
<FullWidthTitleHeader
2019-10-21 10:57:56 +02:00
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}
2019-10-21 10:57:56 +02:00
<TitleWrapper className={classNames("is-ellipsis-overflow", "is-size-6")}>
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}
icon={sideBySide ? "align-left" : "columns"}
label={t(sideBySide ? "diff.combined" : "diff.sideBySide")}
2019-10-10 11:37:14 +02:00
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 withTranslation("repos")(DiffFile);