Skip to content

Commit fe7ce8b

Browse files
update
1 parent 3f134a6 commit fe7ce8b

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ const transform = (str: string) =>
1616
.replace(/\t/g, '<span class="diff-symbol diff-symbol-tab">\t</span>');
1717

1818
export const MainContentDiffAdvance = () => {
19-
const { tabSpace, setFastDiff, setTabSpace, fastDiff } = useDiffConfig((s) => ({
19+
const { tabSpace, setFastDiff, setTabSpace, fastDiff, shadowDOM, setShadowDOM } = useDiffConfig((s) => ({
2020
tabSpace: s.tabSpace,
2121
setTabSpace: s.setTabSpace,
2222
fastDiff: s.fastDiff,
2323
setFastDiff: s.setFastDiff,
24+
shadowDOM: s.shadowDOM,
25+
setShadowDOM: s.setShadowDOM,
2426
}));
2527

2628
useEffect(() => {
@@ -54,6 +56,9 @@ export const MainContentDiffAdvance = () => {
5456
<Button onClick={() => setFastDiff(!fastDiff)}>
5557
{fastDiff ? "disable fast diff template" : "enable fast diff template"}
5658
</Button>
59+
<Button onClick={() => setShadowDOM(!shadowDOM)}>
60+
{shadowDOM ? "disable shadow DOM" : "enable shadow DOM"}
61+
</Button>
5762
</Group>
5863
);
5964
};

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

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

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

99
import { MainContentDiffExampleCode } from "./MainContentDiffExampleCode";
1010
import { temp1, temp2 } from "./MainContentDiffExampleData";
11-
import { MainContentDiffExampleView } from "./MainContentDiffExampleView";
11+
import { MainContentDiffExampleViewWrapper } from "./MainContentDiffExampleViewWrapper";
1212

1313
const _diffFile = generateDiffFile("temp1.tsx", temp1, "temp2.tsx", temp2, "tsx", "tsx");
1414

@@ -39,14 +39,14 @@ export const MainContentDiffExample = ({ onUpdate }: { onUpdate?: (diffFile: Dif
3939

4040
const [loading, setLoading] = useState(false);
4141

42-
const refreshFile = () => {
42+
const refreshFile = useCallbackRef(() => {
4343
setLoading(true);
4444
setDiffFile(getNewDiffFile());
4545
// simulate loading
4646
setTimeout(() => {
4747
startTransition(() => setLoading(false));
4848
}, 800);
49-
};
49+
});
5050

5151
const [diffFile, setDiffFile] = useState(() => getNewDiffFile());
5252

@@ -174,7 +174,8 @@ export const MainContentDiffExample = ({ onUpdate }: { onUpdate?: (diffFile: Dif
174174
{code ? (
175175
<MainContentDiffExampleCode type={platform} />
176176
) : (
177-
<MainContentDiffExampleView diffFile={diffFile} highlighter={highlighter} refreshDiffFile={refreshFile} />
177+
<MainContentDiffExampleViewWrapper diffFile={diffFile} highlighter={highlighter} refreshDiffFile={refreshFile} />
178+
// <MainContentDiffExampleView diffFile={diffFile} highlighter={highlighter} refreshDiffFile={refreshFile} />
178179
)}
179180
</>
180181
);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import diffStyle from "@git-diff-view/react/styles/diff-view-pure.css?inline";
2+
import { MantineProvider } from "@mantine/core";
3+
import mantineStyle from "@mantine/core/styles.css?inline";
4+
import overlayscrollbarsStyle from "overlayscrollbars/overlayscrollbars.css?inline";
5+
import { useEffect, useRef, useState } from "react";
6+
import { createRoot } from "react-dom/client";
7+
8+
import { useDiffConfig } from "../../hooks/useDiffConfig";
9+
import styles from "../../index.css?inline";
10+
import { theme } from "../../theme";
11+
12+
import { MainContentDiffExampleView } from "./MainContentDiffExampleView";
13+
14+
import type { DiffFile, DiffViewProps } from "@git-diff-view/react";
15+
import type { Root } from "react-dom/client";
16+
17+
export const MainContentDiffExampleViewWrapper = ({
18+
diffFile,
19+
highlighter,
20+
refreshDiffFile,
21+
}: {
22+
diffFile: DiffFile;
23+
highlighter?: DiffViewProps<string[]>["registerHighlighter"];
24+
refreshDiffFile: () => void;
25+
}) => {
26+
const ref = useRef<HTMLDivElement>(null);
27+
28+
const [root, setRoot] = useState<Root>();
29+
30+
const shadow = useDiffConfig((s) => s.shadowDOM);
31+
32+
useEffect(() => {
33+
if (shadow) {
34+
const shadowRoot = ref.current?.shadowRoot || ref.current?.attachShadow({ mode: "open" });
35+
if (shadowRoot) {
36+
const reactRoot = createRoot(shadowRoot);
37+
setRoot(reactRoot);
38+
}
39+
}
40+
}, [shadow]);
41+
42+
useEffect(() => {
43+
if (root) {
44+
root.render(
45+
<>
46+
<style>{styles}</style>
47+
<style>{mantineStyle}</style>
48+
<style>{overlayscrollbarsStyle}</style>
49+
<style>{diffStyle}</style>
50+
<MantineProvider theme={theme}>
51+
<MainContentDiffExampleView
52+
diffFile={diffFile}
53+
highlighter={highlighter}
54+
refreshDiffFile={refreshDiffFile}
55+
/>
56+
</MantineProvider>
57+
</>
58+
);
59+
}
60+
}, [root, diffFile, highlighter, refreshDiffFile]);
61+
62+
useEffect(() => {
63+
return () => {
64+
if (root) {
65+
root.unmount();
66+
}
67+
};
68+
}, [root]);
69+
70+
if (shadow) {
71+
return <div ref={ref} className="h-full overflow-auto" />;
72+
} else {
73+
return (
74+
<MainContentDiffExampleView diffFile={diffFile} highlighter={highlighter} refreshDiffFile={refreshDiffFile} />
75+
);
76+
}
77+
};

ui/react-example/src/hooks/useDiffConfig.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type DiffConfig = {
99
engine: "lowlight" | "shiki";
1010
tabSpace: boolean;
1111
fastDiff: boolean;
12+
shadowDOM: boolean;
1213
};
1314

1415
export const useDiffConfig = createState(
@@ -21,6 +22,7 @@ export const useDiffConfig = createState(
2122
engine: "lowlight",
2223
tabSpace: false,
2324
fastDiff: false,
25+
shadowDOM: false,
2426
}) as DiffConfig,
2527
{
2628
withActions(state) {
@@ -59,7 +61,12 @@ export const useDiffConfig = createState(
5961
if (v !== state.fastDiff) {
6062
state.fastDiff = v;
6163
}
62-
}
64+
},
65+
setShadowDOM: (v: boolean) => {
66+
if (v !== state.shadowDOM) {
67+
state.shadowDOM = v;
68+
}
69+
},
6370
};
6471
},
6572
withNamespace: "diffConfig",

0 commit comments

Comments
 (0)