-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdiff.html
113 lines (105 loc) · 4.24 KB
/
diff.html
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🤖</text></svg>">
<title>Diff App</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.row { display: flex; }
.column {
width: 500px;
margin: 2px;
}
.column textarea {
box-sizing: border-box;
width: 100%;
}
.add { background-color: #B5EFDB; }
.remove { background-color: #FFC4C1; }
.merged .remove { text-decoration: line-through; }
</style>
</head>
<body>
<script type="text/javascript" src="diff.min.js"></script>
<script type="text/javascript" src="van-latest.nomodule.min.js"></script>
<script>
const {button, div, input, span, textarea} = van.tags
const autoGrow = e => {
e.target.style.height = "5px"
e.target.style.height = (e.target.scrollHeight + 5) + "px"
}
const Line = ({diff, skipAdd, skipRemove}) => div(
{class: "column", style: "white-space: pre-wrap;"},
diff.filter(d => !(skipAdd && d.added || skipRemove && d.removed)).map(d =>
span({class: d.added ? "add" : (d.removed ? "remove" : "")}, d.value)),
)
const DiffLine = (oldLine, newLine, showMerged) => {
const diff = Diff.diffWords(oldLine, newLine)
return div({class: "row" + (showMerged ? " merged" : "")},
showMerged ?
Line({diff}) : [Line({diff, skipAdd: true}), Line({diff, skipRemove: true})],
)
}
const DiffApp = () => {
const oldTextDom = textarea({oninput: autoGrow, rows: 1})
const newTextDom = textarea({oninput: autoGrow, rows: 1})
const diff = van.state([])
const showMerged = van.state(true)
return div(
div({class: "row"},
div({class: "column"}, oldTextDom), div({class: "column"}, newTextDom),
),
div({class: "row"},
button({onclick: () => diff.val = Diff.diffLines(oldTextDom.value, newTextDom.value)},
"Diff",
),
input({type: "checkbox", checked: showMerged,
oninput: e => showMerged.val = e.target.checked}),
"show merged result"
),
() => {
const diffVal = diff.val, showMergedVal = showMerged.val, resultDom = div()
for (let i = 0; i < diffVal.length; ) {
let line
if (diffVal[i].added && diffVal[i + 1].removed) {
line = DiffLine(diffVal[i + 1].value, diffVal[i].value, showMergedVal)
i += 2
} else if (diffVal[i].removed && diffVal[i + 1].added) {
line = DiffLine(diffVal[i].value, diffVal[i + 1].value, showMergedVal)
i += 2
} else if (diffVal[i].added) {
line = showMergedVal ? div({class: "merged add row"},
div({class: "column", style: "white-space: pre-wrap;"}, diffVal[i].value),
) : div({class: "row"},
div({class: "column"}),
div({class: "add column", style: "white-space: pre-wrap;"}, diffVal[i].value),
)
++i
} else if (diffVal[i].removed) {
line = showMergedVal ? div({class: "merged remove row"},
div({class: "column", style: "white-space: pre-wrap;"}, diffVal[i].value),
) : div({class: "row"},
div({class: "remove column", style: "white-space: pre-wrap;"}, diffVal[i].value),
)
++i
} else {
line = div({class: "row", style: "white-space: pre-wrap;"},
showMergedVal ? div({class: "merged column"}, diffVal[i].value) :
[
div({class: "column"}, diffVal[i].value),
div({class: "column"}, diffVal[i].value),
],
)
++i
}
van.add(resultDom, line)
}
return resultDom
},
)
}
document.body.appendChild(DiffApp())
</script>
</body>
</html>