Skip to content

Commit d1f8dc1

Browse files
committed
fix: now comparing all text at once and showing diff
1 parent 10fb1f0 commit d1f8dc1

File tree

2 files changed

+42
-60
lines changed

2 files changed

+42
-60
lines changed

pages/diff/_diff.vue

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<button
66
type="button"
77
@click="copyUrlToClipboard"
8-
class="inline-flex justify-center px-4 py-2 text-white transition-transform transform rounded-md shadow-lg outline-none copy-uri-button align-center focus:ring-4 active:scale-y-75"
8+
class="inline-flex justify-center px-4 py-2 transition-transform transform rounded-md shadow-lg outline-none copy-uri-button align-center focus:ring-4 active:scale-y-75"
99
v-bind:class="{
1010
'bg-blue-600 text-white': !copied,
1111
'bg-green-500 text-gray-800': copied,
@@ -51,20 +51,26 @@
5151
<main>
5252
<div class="flex items-start w-full gap-4">
5353
<div
54-
class="relative flex-1 px-4 py-2 overflow-y-auto border-2 rounded-sm max-h-screen--nav line-numbers min-h-80"
54+
class="relative flex-1 px-4 py-2 overflow-y-auto border-2 rounded-sm max-h-screen--nav line-number-gutter min-h-80"
5555
>
5656
<RTStickyCopyButton :clickHandler="copyTextToClipboard" />
57-
<p v-for="(lineDiff, index) in lhsDiff" :key="index">
58-
<span v-html="lineDiff"></span>
59-
</p>
57+
<div v-for="(lineDiff, index) in lhsDiff" :key="index">
58+
<p
59+
class="font-mono break-all whitespace-pre-wrap"
60+
v-html="lineDiff"
61+
></p>
62+
</div>
6063
</div>
6164
<div
62-
class="relative flex-1 px-4 py-2 overflow-y-auto border-2 rounded-sm min-h-80 line-numbers max-h-screen--nav"
65+
class="relative flex-1 px-4 py-2 overflow-y-auto border-2 rounded-sm min-h-80 line-number-gutter max-h-screen--nav"
6366
>
6467
<RTStickyCopyButton :clickHandler="copyTextToClipboard" />
65-
<p v-for="(lineDiff, index) in rhsDiff" :key="index">
66-
<span v-html="lineDiff"></span>
67-
</p>
68+
<div v-for="(lineDiff, index) in rhsDiff" :key="index">
69+
<p
70+
class="font-mono break-all whitespace-pre-wrap"
71+
v-html="lineDiff"
72+
></p>
73+
</div>
6874
</div>
6975
</div>
7076
</main>
@@ -81,33 +87,29 @@ export default {
8187
const gunzip = pako.ungzip(Buffer.from(undoUrlSafeBase64(_diff), 'base64'))
8288
const diff = JSON.parse(Buffer.from(gunzip).toString('utf8'))
8389
this.lhsDiff = diff
84-
.map((lineDiff) => {
85-
return lineDiff
86-
.map((item) => {
87-
if (item[0] === -1 || item[0] === 0) {
88-
const className = item[0] === -1 ? 'bg-red-300' : ''
89-
return `<span class="whitespace-pre-wrap font-mono break-all inline mr-[-4px] p-0 m-0 ${className}">${item[1]}</span>`
90-
}
91-
return ''
92-
})
93-
.filter(Boolean)
94-
.join('\n')
90+
.map((item) => {
91+
const hunkState = item[0]
92+
if (hunkState === -1 || hunkState === 0) {
93+
const className = hunkState === -1 ? 'bg-red-400' : ''
94+
return `<span class="font-mono break-all inline p-0 m-0 ${className}">${item[1]}</span>`
95+
}
96+
return false
9597
})
9698
.filter(Boolean)
99+
.join('')
100+
.split('\n')
97101
this.rhsDiff = diff
98-
.map((lineDiff) => {
99-
return lineDiff
100-
.map((item) => {
101-
if (item[0] === 1 || item[0] === 0) {
102-
const className = item[0] === 1 ? 'bg-green-300' : ''
103-
return `<span class="whitespace-pre-wrap font-mono break-all inline mr-[-4px] p-0 m-0 ${className}">${item[1]}</span>`
104-
}
105-
return ''
106-
})
107-
.filter(Boolean)
108-
.join('\n')
102+
.map((item) => {
103+
const hunkState = item[0]
104+
if (hunkState === 1 || hunkState === 0) {
105+
const className = hunkState === 1 ? 'bg-green-400' : ''
106+
return `<span class="font-mono break-all inline p-0 m-0 ${className}">${item[1]}</span>`
107+
}
108+
return false
109109
})
110110
.filter(Boolean)
111+
.join('')
112+
.split('\n')
111113
const maxLineCount =
112114
this.lhsDiff.length > this.rhsDiff.length
113115
? this.lhsDiff.length
@@ -177,19 +179,22 @@ export default {
177179
}
178180
179181
/* line numbers in diff view */
180-
.line-numbers {
182+
.line-number-gutter {
181183
counter-reset: line-numbers;
184+
185+
--line-number-gutter-width: calc(var(--max-line-number-characher) + 10px);
182186
@apply relative;
183187
p {
184-
padding-left: calc(var(--max-line-number-characher) - 4px);
188+
padding-left: calc(var(--line-number-gutter-width) - 10px);
189+
line-height: 1.65;
185190
@apply relative;
186191
&:hover {
187192
@apply bg-gray-200;
188193
}
189194
&::before {
190195
counter-increment: line-numbers;
191196
content: counter(line-numbers);
192-
width: calc(var(--max-line-number-characher) + 4px);
197+
width: var(--line-number-gutter-width);
193198
@apply absolute left-0 top-0 -mx-4 bottom-0 text-center bg-gray-200 text-gray-500 flex justify-center items-center text-sm;
194199
}
195200
&:first-of-type {

pages/index.vue

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,9 @@ export default Vue.extend({
7474
})
7575
return
7676
}
77-
const originalLhs = lhs.split('\n')
78-
const originalRhs = rhs.split('\n')
79-
const diff = originalLhs.map((x, i) => {
80-
if (!originalRhs[i]) {
81-
originalRhs[i] = ''
82-
}
83-
return dmp.diff_main(x, originalRhs[i])
84-
})
85-
if (originalLhs.length > originalRhs.length) {
86-
for (
87-
let i = originalLhs.length - diff.length;
88-
i < originalLhs.length;
89-
i++
90-
) {
91-
diff.push([[-1, originalLhs[i]]])
92-
}
93-
}
94-
if (originalLhs.length < originalRhs.length) {
95-
for (
96-
let i = originalRhs.length - diff.length;
97-
i < originalRhs.length;
98-
i++
99-
) {
100-
diff.push([[1, originalRhs[i]]])
101-
}
102-
}
77+
const originalLhs = lhs
78+
const originalRhs = rhs
79+
const diff = dmp.diff_main(originalLhs, originalRhs)
10380
const gzip = Buffer.from(pako.gzip(JSON.stringify(diff))).toString(
10481
'base64'
10582
)

0 commit comments

Comments
 (0)