Skip to content

Commit 7767d96

Browse files
committed
feat(export): replace diff library to diff-match-patch
- new DiffTextOutput components
1 parent 9b01f4b commit 7767d96

4 files changed

Lines changed: 127 additions & 10 deletions

File tree

app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
"@togglecorp/toggle-request": "^1.0.0-beta.3",
5252
"@turf/bbox": "^6.5.0",
5353
"@turf/buffer": "^6.5.0",
54+
"@types/diff-match-patch": "^1.0.36",
55+
"diff-match-patch": "^1.0.5",
5456
"exceljs": "^4.4.0",
55-
"diff": "^8.0.2",
5657
"file-saver": "^2.0.5",
5758
"html-to-image": "^1.11.11",
5859
"mapbox-gl": "^1.13.0",
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import {
2+
Fragment,
3+
useMemo,
4+
} from 'react';
5+
import {
6+
_cs,
7+
isNotDefined,
8+
} from '@togglecorp/fujs';
9+
import DiffMatchPatch from 'diff-match-patch';
10+
11+
import styles from './styles.module.css';
12+
13+
const ADDED = 1;
14+
const REMOVED = -1;
15+
16+
interface Props {
17+
value?: string | null;
18+
className?: string;
19+
withDiff?: boolean;
20+
prevValue?: string | null;
21+
}
22+
23+
function DiffTextOutput(props: Props) {
24+
const {
25+
className,
26+
value,
27+
prevValue,
28+
withDiff = false,
29+
} = props;
30+
31+
const diff = useMemo(() => {
32+
if (!withDiff) {
33+
return undefined;
34+
}
35+
const diffMatch = new DiffMatchPatch();
36+
return diffMatch.diff_main(prevValue ?? '', value ?? '');
37+
}, [withDiff, value, prevValue]);
38+
39+
if (isNotDefined(diff)) {
40+
return (
41+
<div className={_cs(styles.diffTextOutput, className)}>
42+
{value}
43+
</div>
44+
);
45+
}
46+
47+
return (
48+
<div
49+
className={_cs(
50+
styles.diffTextOutput,
51+
styles.withDiffView,
52+
className,
53+
)}
54+
>
55+
{diff.map(([changeType, content], index) => {
56+
if (changeType === ADDED) {
57+
return (
58+
<span
59+
className={styles.added}
60+
// eslint-disable-next-line react/no-array-index-key
61+
key={index}
62+
>
63+
{content}
64+
</span>
65+
);
66+
}
67+
68+
if (changeType === REMOVED) {
69+
return (
70+
<span
71+
className={styles.removed}
72+
// eslint-disable-next-line react/no-array-index-key
73+
key={index}
74+
>
75+
{content}
76+
</span>
77+
);
78+
}
79+
80+
return (
81+
// eslint-disable-next-line react/no-array-index-key
82+
<Fragment key={index}>
83+
{content}
84+
</Fragment>
85+
);
86+
})}
87+
</div>
88+
);
89+
}
90+
91+
export default DiffTextOutput;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.diff-text-output {
2+
text-align: justify;
3+
white-space: pre-wrap;
4+
overflow-wrap: break-word;
5+
6+
&.with-diff-view {
7+
.added {
8+
background-color: color-mix(in srgb, var(--go-ui-color-green) 10%, transparent);
9+
color: var(--go-ui-color-green);
10+
}
11+
12+
.removed {
13+
background-color: color-mix(in srgb, var(--go-ui-color-red) 10%, transparent);
14+
text-decoration: line-through;
15+
color: var(--go-ui-color-red);
16+
}
17+
}
18+
}

pnpm-lock.yaml

Lines changed: 16 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)