2019-12-19 09:51:44 +01:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
|
import { storiesOf } from "@storybook/react";
|
|
|
|
|
import Diff from "./Diff";
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
import parser from "gitdiff-parser";
|
|
|
|
|
import simpleDiff from "../__resources__/Diff.simple";
|
2020-01-08 09:57:57 +01:00
|
|
|
import hunksDiff from "../__resources__/Diff.hunks";
|
2020-01-09 16:31:28 +01:00
|
|
|
import binaryDiff from "../__resources__/Diff.binary";
|
2019-12-19 09:51:44 +01:00
|
|
|
import Button from "../buttons/Button";
|
2020-01-22 12:08:28 +01:00
|
|
|
import { DiffEventContext, File } from "./DiffTypes";
|
2019-12-19 09:51:44 +01:00
|
|
|
import Toast from "../toast/Toast";
|
2020-01-22 12:08:28 +01:00
|
|
|
import { getPath } from "./diffs";
|
2019-12-19 09:51:44 +01:00
|
|
|
|
|
|
|
|
const diffFiles = parser.parse(simpleDiff);
|
|
|
|
|
|
|
|
|
|
storiesOf("Diff", module)
|
|
|
|
|
.add("Default", () => <Diff diff={diffFiles} />)
|
|
|
|
|
.add("Side-By-Side", () => <Diff diff={diffFiles} sideBySide={true} />)
|
|
|
|
|
.add("Collapsed", () => <Diff diff={diffFiles} defaultCollapse={true} />)
|
|
|
|
|
.add("File Controls", () => <Diff diff={diffFiles} fileControlFactory={() => <Button>Custom Control</Button>} />)
|
|
|
|
|
.add("File Annotation", () => (
|
2020-01-06 14:48:32 +01:00
|
|
|
<Diff
|
|
|
|
|
diff={diffFiles}
|
|
|
|
|
fileAnnotationFactory={file => [<p key={file.newPath}>Custom File annotation for {file.newPath}</p>]}
|
|
|
|
|
/>
|
2019-12-19 09:51:44 +01:00
|
|
|
))
|
|
|
|
|
.add("Line Annotation", () => (
|
|
|
|
|
<Diff
|
|
|
|
|
diff={diffFiles}
|
|
|
|
|
annotationFactory={ctx => {
|
|
|
|
|
return {
|
2020-01-06 14:48:32 +01:00
|
|
|
N2: <p key="N2">Line Annotation</p>
|
2019-12-19 09:51:44 +01:00
|
|
|
};
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
))
|
|
|
|
|
.add("OnClick", () => {
|
2020-01-08 14:07:50 +01:00
|
|
|
const OnClickDemo = () => {
|
2019-12-19 09:51:44 +01:00
|
|
|
const [changeId, setChangeId] = useState();
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const interval = setInterval(() => setChangeId(undefined), 2000);
|
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
|
});
|
|
|
|
|
const onClick = (context: DiffEventContext) => setChangeId(context.changeId);
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{changeId && <Toast type="info" title={"Change " + changeId} />}
|
|
|
|
|
<Diff diff={diffFiles} onClick={onClick} />
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
return <OnClickDemo />;
|
2020-01-08 09:57:57 +01:00
|
|
|
})
|
|
|
|
|
.add("Hunks", () => {
|
|
|
|
|
const hunkDiffFiles = parser.parse(hunksDiff);
|
|
|
|
|
return <Diff diff={hunkDiffFiles} />;
|
2020-01-09 16:31:28 +01:00
|
|
|
})
|
|
|
|
|
.add("Binaries", () => {
|
|
|
|
|
const binaryDiffFiles = parser.parse(binaryDiff);
|
|
|
|
|
return <Diff diff={binaryDiffFiles} />;
|
2020-01-22 12:08:28 +01:00
|
|
|
})
|
|
|
|
|
.add("SyntaxHighlighting", () => {
|
|
|
|
|
const filesWithLanguage = diffFiles.map((file: File) => {
|
|
|
|
|
const ext = getPath(file).split(".")[1];
|
|
|
|
|
if (ext === "tsx") {
|
|
|
|
|
file.language = "typescript";
|
|
|
|
|
} else {
|
|
|
|
|
file.language = ext;
|
|
|
|
|
}
|
|
|
|
|
return file;
|
|
|
|
|
});
|
|
|
|
|
return <Diff diff={filesWithLanguage} />;
|
2020-02-07 14:27:21 +01:00
|
|
|
})
|
|
|
|
|
.add("CollapsingWithFunction", () => (
|
|
|
|
|
<Diff diff={diffFiles} defaultCollapse={(oldPath, newPath) => oldPath.endsWith(".java")} />
|
|
|
|
|
));
|