|
| 1 | +import { |
| 2 | + escapeHtml, |
| 3 | + setTransformForTemplateContent, |
| 4 | + resetTransform, |
| 5 | + setEnableFastDiffTemplate, |
| 6 | + resetEnableFastDiffTemplate, |
| 7 | +} from "@git-diff-view/core"; |
| 8 | +import { Button, Group } from "@mantine/core"; |
| 9 | +import { useEffect } from "react"; |
| 10 | + |
| 11 | +import { useDiffConfig } from "../../hooks/useDiffConfig"; |
| 12 | + |
| 13 | +const transform = (str: string) => |
| 14 | + escapeHtml(str) |
| 15 | + .replace(/ /g, '<span class="diff-symbol diff-symbol-space"> </span>') |
| 16 | + .replace(/\t/g, '<span class="diff-symbol diff-symbol-tab">\t</span>'); |
| 17 | + |
| 18 | +export const MainContentDiffAdvance = () => { |
| 19 | + const { tabSpace, setFastDiff, setTabSpace, fastDiff } = useDiffConfig((s) => ({ |
| 20 | + tabSpace: s.tabSpace, |
| 21 | + setTabSpace: s.setTabSpace, |
| 22 | + fastDiff: s.fastDiff, |
| 23 | + setFastDiff: s.setFastDiff, |
| 24 | + })); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (tabSpace) { |
| 28 | + setTransformForTemplateContent(transform); |
| 29 | + } else { |
| 30 | + resetTransform(); |
| 31 | + } |
| 32 | + }, [tabSpace]); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (fastDiff) { |
| 36 | + setEnableFastDiffTemplate(true); |
| 37 | + } else { |
| 38 | + resetEnableFastDiffTemplate(); |
| 39 | + } |
| 40 | + }, [fastDiff]); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + return () => { |
| 44 | + resetTransform(); |
| 45 | + resetEnableFastDiffTemplate(); |
| 46 | + } |
| 47 | + }, []); |
| 48 | + |
| 49 | + return ( |
| 50 | + <Group> |
| 51 | + <Button onClick={() => setTabSpace(!tabSpace)}> |
| 52 | + {tabSpace ? "disable tab/space symbol" : "enable tab/space symbol"} |
| 53 | + </Button> |
| 54 | + <Button onClick={() => setFastDiff(!fastDiff)}> |
| 55 | + {fastDiff ? "disable fast diff template" : "enable fast diff template"} |
| 56 | + </Button> |
| 57 | + </Group> |
| 58 | + ); |
| 59 | +}; |
0 commit comments