blob: 6f89d6a977b50398bd4941d8b46e6eb55a492836 (
plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import { type Component, createMemo, createSignal, onMount } from "solid-js"
import { diffLines, type ChangeObject } from "diff"
import CodeBlock from "./CodeBlock"
import styles from "./diffview.module.css"
type DiffRow = {
left: string
right: string
type: "added" | "removed" | "unchanged"
}
interface DiffViewProps {
changes: ChangeObject<string>[]
lang?: string
class?: string
}
const DiffView: Component<DiffViewProps> = (props) => {
const rows = createMemo(() => {
const diffRows: DiffRow[] = []
for (const item of props.changes) {
const lines = item.value.split(/\r?\n/)
if (lines.at(-1) === "") lines.pop()
for (const line of lines) {
diffRows.push({
left: item.removed ? line : item.added ? "" : line,
right: item.added ? line : item.removed ? "" : line,
type: item.added ? "added" : item.removed ? "removed" : "unchanged",
})
}
}
return diffRows
})
return (
<div class={`${styles.diff} ${props.class ?? ""}`}>
<div class={styles.column}>
{rows().map((r) => (
<CodeBlock
code={r.left}
lang={props.lang}
data-section="cell"
data-diff-type={r.type === "removed" ? "removed" : ""}
/>
))}
</div>
<div class={styles.column}>
{rows().map((r) => (
<CodeBlock
code={r.right}
lang={props.lang}
data-section="cell"
data-diff-type={r.type === "added" ? "added" : ""}
/>
))}
</div>
</div>
)
}
export default DiffView
|