mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-12-15 21:09:47 +01:00
Add possibility to change redering of diff tree items
Squash commits of branch feature/diff_tree_extension: - Add optional extension for diff file tree - Make it a wrapper - Make extension more complex - Log change
This commit is contained in:
committed by
Florian Scholdei
parent
d5096cf025
commit
b9746d8633
2
gradle/changelog/diff_tree_extension.yaml
Normal file
2
gradle/changelog/diff_tree_extension.yaml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
- type: added
|
||||||
|
description: API to change diff tree
|
||||||
@@ -14,9 +14,9 @@
|
|||||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ReactNode } from "react";
|
import { FC, ReactNode } from "react";
|
||||||
import { DefaultCollapsed } from "./defaultCollapsed";
|
import { DefaultCollapsed } from "./defaultCollapsed";
|
||||||
import { Change, Hunk, FileDiff as File } from "@scm-manager/ui-types";
|
import { Change, Hunk, FileDiff as File, FileChangeType } from "@scm-manager/ui-types";
|
||||||
|
|
||||||
export type ChangeEvent = {
|
export type ChangeEvent = {
|
||||||
change: Change;
|
change: Change;
|
||||||
@@ -31,6 +31,18 @@ export type AnnotationFactoryContext = BaseContext;
|
|||||||
|
|
||||||
export type FileAnnotationFactory = (file: File) => ReactNode[];
|
export type FileAnnotationFactory = (file: File) => ReactNode[];
|
||||||
|
|
||||||
|
export type FileTreeNodeWrapper = FC<{
|
||||||
|
name: string;
|
||||||
|
path: string;
|
||||||
|
changeType?: FileChangeType;
|
||||||
|
iconName: string;
|
||||||
|
iconColor: string;
|
||||||
|
isFile: boolean;
|
||||||
|
originalIcon: ReactNode;
|
||||||
|
originalLabel: ReactNode;
|
||||||
|
isCurrentFile: boolean;
|
||||||
|
}>;
|
||||||
|
|
||||||
// key = change id, value = react component
|
// key = change id, value = react component
|
||||||
export type AnnotationFactory = (context: AnnotationFactoryContext) => {
|
export type AnnotationFactory = (context: AnnotationFactoryContext) => {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
|||||||
@@ -18,12 +18,19 @@ import React, { FC } from "react";
|
|||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import { FileTree } from "@scm-manager/ui-types";
|
import { FileChangeType, FileTree } from "@scm-manager/ui-types";
|
||||||
import { FileDiffContent, StackedSpan, StyledIcon } from "./styledElements";
|
import { FileDiffContent, StackedSpan, StyledIcon } from "./styledElements";
|
||||||
|
import { FileTreeNodeWrapper } from "../DiffTypes";
|
||||||
|
|
||||||
type Props = { tree: FileTree; currentFile: string; setCurrentFile: (path: string) => void; gap?: number };
|
type Props = {
|
||||||
|
tree: FileTree;
|
||||||
|
currentFile: string;
|
||||||
|
setCurrentFile: (path: string) => void;
|
||||||
|
gap?: number;
|
||||||
|
FileTreeNodeWrapper?: FileTreeNodeWrapper;
|
||||||
|
};
|
||||||
|
|
||||||
const DiffFileTree: FC<Props> = ({ tree, currentFile, setCurrentFile, gap = 15 }) => {
|
const DiffFileTree: FC<Props> = ({ tree, currentFile, setCurrentFile, gap = 15, FileTreeNodeWrapper }) => {
|
||||||
return (
|
return (
|
||||||
<FileDiffContent gap={gap}>
|
<FileDiffContent gap={gap}>
|
||||||
{Object.keys(tree.children).map((key) => (
|
{Object.keys(tree.children).map((key) => (
|
||||||
@@ -33,6 +40,7 @@ const DiffFileTree: FC<Props> = ({ tree, currentFile, setCurrentFile, gap = 15 }
|
|||||||
parentPath=""
|
parentPath=""
|
||||||
currentFile={currentFile}
|
currentFile={currentFile}
|
||||||
setCurrentFile={setCurrentFile}
|
setCurrentFile={setCurrentFile}
|
||||||
|
FileTreeNodeWrapper={FileTreeNodeWrapper}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</FileDiffContent>
|
</FileDiffContent>
|
||||||
@@ -41,9 +49,13 @@ const DiffFileTree: FC<Props> = ({ tree, currentFile, setCurrentFile, gap = 15 }
|
|||||||
|
|
||||||
export default DiffFileTree;
|
export default DiffFileTree;
|
||||||
|
|
||||||
type ChangeType = "add" | "modify" | "delete" | "rename" | "copy";
|
type NodeProps = {
|
||||||
|
node: FileTree;
|
||||||
type NodeProps = { node: FileTree; parentPath: string; currentFile: string; setCurrentFile: (path: string) => void };
|
parentPath: string;
|
||||||
|
currentFile: string;
|
||||||
|
setCurrentFile: (path: string) => void;
|
||||||
|
FileTreeNodeWrapper?: FileTreeNodeWrapper;
|
||||||
|
};
|
||||||
|
|
||||||
const addPath = (parentPath: string, path: string) => {
|
const addPath = (parentPath: string, path: string) => {
|
||||||
if ("" === parentPath) {
|
if ("" === parentPath) {
|
||||||
@@ -52,16 +64,31 @@ const addPath = (parentPath: string, path: string) => {
|
|||||||
return parentPath + "/" + path;
|
return parentPath + "/" + path;
|
||||||
};
|
};
|
||||||
|
|
||||||
const TreeNode: FC<NodeProps> = ({ node, parentPath, currentFile, setCurrentFile }) => {
|
const TreeNode: FC<NodeProps> = ({ node, parentPath, currentFile, setCurrentFile, FileTreeNodeWrapper }) => {
|
||||||
const [t] = useTranslation("repos");
|
const [t] = useTranslation("repos");
|
||||||
|
|
||||||
|
FileTreeNodeWrapper = FileTreeNodeWrapper || (({ children }) => <>{children}</>);
|
||||||
|
|
||||||
|
const label = <div className="ml-1">{node.nodeName}</div>;
|
||||||
|
const icon = <StyledIcon alt={t("diff.showContent")}>folder</StyledIcon>;
|
||||||
return (
|
return (
|
||||||
<li>
|
<li>
|
||||||
{Object.keys(node.children).length > 0 ? (
|
{Object.keys(node.children).length > 0 ? (
|
||||||
<ul className="py-1 pl-3">
|
<ul className="py-1 pl-3">
|
||||||
<li className="is-flex has-text-grey">
|
<li className="is-flex has-text-grey">
|
||||||
<StyledIcon alt={t("diff.showContent")}>folder</StyledIcon>
|
<FileTreeNodeWrapper
|
||||||
<div className="ml-1">{node.nodeName}</div>
|
path={addPath(parentPath, node.nodeName)}
|
||||||
|
isFile={false}
|
||||||
|
isCurrentFile={false}
|
||||||
|
name={node.nodeName}
|
||||||
|
iconName={"folder"}
|
||||||
|
iconColor={"grey"}
|
||||||
|
originalIcon={icon}
|
||||||
|
originalLabel={label}
|
||||||
|
>
|
||||||
|
{icon}
|
||||||
|
{label}
|
||||||
|
</FileTreeNodeWrapper>
|
||||||
</li>
|
</li>
|
||||||
{Object.keys(node.children).map((key) => (
|
{Object.keys(node.children).map((key) => (
|
||||||
<TreeNode
|
<TreeNode
|
||||||
@@ -70,23 +97,25 @@ const TreeNode: FC<NodeProps> = ({ node, parentPath, currentFile, setCurrentFile
|
|||||||
parentPath={addPath(parentPath, node.nodeName)}
|
parentPath={addPath(parentPath, node.nodeName)}
|
||||||
currentFile={currentFile}
|
currentFile={currentFile}
|
||||||
setCurrentFile={setCurrentFile}
|
setCurrentFile={setCurrentFile}
|
||||||
|
FileTreeNodeWrapper={FileTreeNodeWrapper}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
) : (
|
) : (
|
||||||
<TreeFile
|
<TreeFile
|
||||||
changeType={node.changeType.toLowerCase() as ChangeType}
|
changeType={node.changeType.toLowerCase() as FileChangeType}
|
||||||
path={node.nodeName}
|
path={node.nodeName}
|
||||||
parentPath={parentPath}
|
parentPath={parentPath}
|
||||||
currentFile={currentFile}
|
currentFile={currentFile}
|
||||||
setCurrentFile={setCurrentFile}
|
setCurrentFile={setCurrentFile}
|
||||||
|
FileTreeNodeWrapper={FileTreeNodeWrapper}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getColor = (changeType: ChangeType) => {
|
const getColor = (changeType: FileChangeType) => {
|
||||||
switch (changeType) {
|
switch (changeType) {
|
||||||
case "add":
|
case "add":
|
||||||
return "success";
|
return "success";
|
||||||
@@ -99,7 +128,7 @@ const getColor = (changeType: ChangeType) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getIcon = (changeType: ChangeType) => {
|
const getIcon = (changeType: FileChangeType) => {
|
||||||
switch (changeType) {
|
switch (changeType) {
|
||||||
case "add":
|
case "add":
|
||||||
case "copy":
|
case "copy":
|
||||||
@@ -113,14 +142,22 @@ const getIcon = (changeType: ChangeType) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type FileProps = {
|
type FileProps = {
|
||||||
changeType: ChangeType;
|
changeType: FileChangeType;
|
||||||
path: string;
|
path: string;
|
||||||
parentPath: string;
|
parentPath: string;
|
||||||
currentFile: string;
|
currentFile: string;
|
||||||
setCurrentFile: (path: string) => void;
|
setCurrentFile: (path: string) => void;
|
||||||
|
FileTreeNodeWrapper: FileTreeNodeWrapper;
|
||||||
};
|
};
|
||||||
|
|
||||||
const TreeFile: FC<FileProps> = ({ changeType, path, parentPath, currentFile, setCurrentFile }) => {
|
const TreeFile: FC<FileProps> = ({
|
||||||
|
changeType,
|
||||||
|
path,
|
||||||
|
parentPath,
|
||||||
|
currentFile,
|
||||||
|
setCurrentFile,
|
||||||
|
FileTreeNodeWrapper,
|
||||||
|
}) => {
|
||||||
const [t] = useTranslation("repos");
|
const [t] = useTranslation("repos");
|
||||||
const completePath = addPath(parentPath, path);
|
const completePath = addPath(parentPath, path);
|
||||||
|
|
||||||
@@ -128,35 +165,50 @@ const TreeFile: FC<FileProps> = ({ changeType, path, parentPath, currentFile, se
|
|||||||
return currentFile === completePath;
|
return currentFile === completePath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const iconName = getIcon(changeType);
|
||||||
|
|
||||||
|
const icon = (
|
||||||
|
<StackedSpan className="fa-stack">
|
||||||
|
<StyledIcon
|
||||||
|
className={classNames("fa-stack-2x", `has-text-${getColor(changeType)}`)}
|
||||||
|
key={completePath + "file"}
|
||||||
|
type="fas"
|
||||||
|
alt={t("diff.showContent")}
|
||||||
|
>
|
||||||
|
file
|
||||||
|
</StyledIcon>
|
||||||
|
<StyledIcon
|
||||||
|
className={classNames("fa-stack-1x", "is-relative", "has-text-secondary-least")}
|
||||||
|
isSmaller={iconName === "circle"}
|
||||||
|
key={changeType}
|
||||||
|
alt={t(`diff.changes.${changeType}`)}
|
||||||
|
>
|
||||||
|
{iconName}
|
||||||
|
</StyledIcon>
|
||||||
|
</StackedSpan>
|
||||||
|
);
|
||||||
|
const label = <div className={classNames("ml-1", isCurrentFile() ? "has-text-weight-bold" : "")}>{path}</div>;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
className="is-flex py-1 pl-3 has-cursor-pointer"
|
className="is-flex py-1 pl-3 has-cursor-pointer"
|
||||||
onClick={() => setCurrentFile(completePath)}
|
onClick={() => setCurrentFile(completePath)}
|
||||||
to={`#diff-${encodeURIComponent(completePath)}`}
|
to={`#diff-${encodeURIComponent(completePath)}`}
|
||||||
>
|
>
|
||||||
<StackedSpan className="fa-stack">
|
<FileTreeNodeWrapper
|
||||||
<StyledIcon
|
name={path}
|
||||||
className={classNames("fa-stack-2x", `has-text-${getColor(changeType)}`)}
|
path={completePath}
|
||||||
key={completePath + "file"}
|
changeType={changeType}
|
||||||
type={isCurrentFile() ? "fas" : "far"}
|
isFile={true}
|
||||||
alt={t("diff.showContent")}
|
iconName={iconName}
|
||||||
>
|
iconColor={getColor(changeType)}
|
||||||
file
|
originalIcon={icon}
|
||||||
</StyledIcon>
|
originalLabel={label}
|
||||||
<StyledIcon
|
isCurrentFile={isCurrentFile()}
|
||||||
className={classNames(
|
>
|
||||||
"fa-stack-1x",
|
{icon}
|
||||||
"is-relative",
|
{label}
|
||||||
isCurrentFile() ? "has-text-secondary-least" : `has-text-${getColor(changeType)}`
|
</FileTreeNodeWrapper>
|
||||||
)}
|
|
||||||
isSmaller={getIcon(changeType) === "circle"}
|
|
||||||
key={changeType}
|
|
||||||
alt={t(`diff.changes.${changeType}`)}
|
|
||||||
>
|
|
||||||
{getIcon(changeType)}
|
|
||||||
</StyledIcon>
|
|
||||||
</StackedSpan>
|
|
||||||
<div className="ml-1">{path}</div>
|
|
||||||
</Link>
|
</Link>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user