-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRichTextDiffView.tsx
More file actions
41 lines (36 loc) · 1.11 KB
/
RichTextDiffView.tsx
File metadata and controls
41 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import React, { useMemo } from 'react';
import { diffWordsWithSpace } from 'diff';
const escapeHtml = (value: string) =>
value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
interface RichTextDiffViewProps {
baseline: string;
current: string;
}
const RichTextDiffView: React.FC<RichTextDiffViewProps> = ({ baseline, current }) => {
const diffMarkup = useMemo(() => {
const parts = diffWordsWithSpace(baseline, current);
return parts.map((part, index) => {
const className = part.added
? 'bg-success/10 text-success'
: part.removed
? 'bg-destructive-bg text-destructive-text'
: 'text-text-main';
return (
<span
key={`${index}-${part.value.length}`}
className={`whitespace-pre-wrap ${className}`}
dangerouslySetInnerHTML={{ __html: escapeHtml(part.value) }}
/>
);
});
}, [baseline, current]);
return (
<div className="h-full w-full overflow-auto bg-background text-sm font-mono px-4 py-3 space-x-1">
{diffMarkup}
</div>
);
};
export default RichTextDiffView;