Skip to content

Commit 5a9775f

Browse files
update
1 parent f1fd78a commit 5a9775f

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

ui/react-example/src/components/MainContent.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ import {
1414
getThemeColor,
1515
Container,
1616
} from "@mantine/core";
17-
import { useMemo } from "react";
17+
import { useMemo, useState } from "react";
1818

1919
import { MainContentDiffConfig } from "./MainContentDiffConfig";
2020
import { MainContentDiffExample } from "./MainContentDiffExample";
2121

22+
import type { DiffFile } from "@git-diff-view/react";
23+
2224
export const MainContent = () => {
2325
const theme = useMantineTheme();
2426

27+
const [d, setD] = useState<DiffFile>();
28+
2529
const { colorScheme } = useMantineColorScheme();
2630

2731
const color = useMemo(() => alpha(getThemeColor("yellow", theme), 0.5), [theme]);
@@ -44,14 +48,14 @@ export const MainContent = () => {
4448
<Space h="12" />
4549
<Title order={4}>Diff View Config</Title>
4650
<Space h="4" />
47-
<MainContentDiffConfig />
51+
<MainContentDiffConfig diffFile={d} />
4852
</Stack>
4953

5054
<Box
5155
className={`relative h-[calc(100vh-200px)] transform-gpu overflow-hidden rounded-md border border-solid ${colorScheme === "light" ? "border-gray-200" : "border-gray-600"}`}
5256
w={{ base: "100%", sm: "60%" }}
5357
>
54-
<MainContentDiffExample />
58+
<MainContentDiffExample onUpdate={setD} />
5559
</Box>
5660
</Flex>
5761
</Container>

ui/react-example/src/components/MainContentDiffConfig.tsx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,44 @@
11
import { DiffModeEnum } from "@git-diff-view/react";
22
import { Button, Group, Tooltip } from "@mantine/core";
3+
import { useEffect, useState } from "react";
34

45
import { useDiffConfig } from "../hooks/useDiffConfig";
56

6-
export const MainContentDiffConfig = () => {
7+
import type { DiffFile } from "@git-diff-view/react";
8+
9+
export const MainContentDiffConfig = ({ diffFile }: { diffFile?: DiffFile }) => {
710
const { mode, setEngine, setHighlight, setMode, setWrap, wrap, engine, highlight } = useDiffConfig();
811

12+
const [expandAll, setExpandAll] = useState(false);
13+
14+
useEffect(() => {
15+
if (diffFile) {
16+
if (mode === DiffModeEnum.Unified) {
17+
setExpandAll(diffFile.hasExpandUnifiedAll);
18+
} else {
19+
setExpandAll(diffFile.hasExpandSplitAll);
20+
}
21+
}
22+
}, [diffFile, mode]);
23+
24+
const toggleExpandAll = () => {
25+
if (diffFile) {
26+
if (mode === DiffModeEnum.Unified) {
27+
if (expandAll) {
28+
diffFile.onAllCollapse("unified");
29+
} else {
30+
diffFile.onAllExpand("unified");
31+
}
32+
} else {
33+
if (expandAll) {
34+
diffFile.onAllCollapse("split");
35+
} else {
36+
diffFile.onAllExpand("split");
37+
}
38+
}
39+
}
40+
};
41+
942
return (
1043
<Group>
1144
<Tooltip label="diff view mode">
@@ -24,6 +57,16 @@ export const MainContentDiffConfig = () => {
2457
{engine}
2558
</Button>
2659
</Tooltip>
60+
<Tooltip label="expand all lines">
61+
<Button
62+
onClick={() => {
63+
toggleExpandAll();
64+
setExpandAll(!expandAll);
65+
}}
66+
>
67+
{expandAll ? "collapse all" : "expand all"}
68+
</Button>
69+
</Tooltip>
2770
</Group>
2871
);
2972
};

ui/react-example/src/components/MainContentDiffExample.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { generateDiffFile, DiffFile } from "@git-diff-view/file";
22
import { Button, ButtonGroup, Card, FloatingIndicator, Group, LoadingOverlay, Popover, Tooltip } from "@mantine/core";
33
import { useDisclosure, useMounted } from "@mantine/hooks";
44
import { IconCode, IconPlayerPlay, IconRefresh, IconBrandReact, IconBrandVue } from "@tabler/icons-react";
5-
import { useState, startTransition, useCallback, forwardRef, useMemo } from "react";
5+
import { useState, startTransition, useCallback, forwardRef, useMemo, useEffect } from "react";
66

77
import { useDiffHighlighter } from "../hooks/useDiffHighlighter";
88

@@ -22,7 +22,7 @@ const getNewDiffFile = () => {
2222
return instance;
2323
};
2424

25-
export const MainContentDiffExample = () => {
25+
export const MainContentDiffExample = ({ onUpdate }: { onUpdate?: (diffFile: DiffFile) => void }) => {
2626
const [code, { open, close }] = useDisclosure();
2727

2828
const isMounted = useMounted();
@@ -56,6 +56,12 @@ export const MainContentDiffExample = () => {
5656

5757
const [controlsRefs, setControlsRefs] = useState<Record<string, HTMLButtonElement | null>>({});
5858

59+
useEffect(() => {
60+
if (onUpdate) {
61+
onUpdate(diffFile);
62+
}
63+
}, [diffFile]);
64+
5965
const Element = useMemo(
6066
() =>
6167
forwardRef<HTMLButtonElement, any>(({ onClick, ...props }, ref) => (

0 commit comments

Comments
 (0)