Skip to content

Commit 8ae227c

Browse files
committed
feat(diff hunks): highlighting both side diff on travelling diff hunks
1 parent 97d142c commit 8ae227c

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

pages/v1/diff.vue

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@
159159
v-for="(lineDiff, index) in lhsDiff"
160160
:key="index"
161161
:class="{
162-
'bg-red-200 dark:bg-red-800': lineDiff.includes('isModified'),
162+
'bg-red-200 dark:bg-red-800 rounded-sm':
163+
lineDiff.includes('isModified'),
163164
}"
164165
>
165166
<p class="break-all whitespace-pre-wrap" v-html="lineDiff"></p>
@@ -185,7 +186,7 @@
185186
v-for="(lineDiff, index) in rhsDiff"
186187
:key="index"
187188
:class="{
188-
'bg-green-200 dark:bg-green-700':
189+
'bg-green-200 dark:bg-green-700 rounded-sm':
189190
lineDiff.includes('isModified'),
190191
}"
191192
>
@@ -257,23 +258,35 @@ export default {
257258
}
258259
},
259260
async mounted() {
261+
const isLHSBigger = this.lhsDiff.length > this.rhsDiff.length
262+
const maxLineCount = isLHSBigger ? this.lhsDiff.length : this.rhsDiff.length
263+
document.documentElement.style.setProperty(
264+
'--max-line-number-characher',
265+
`${`${maxLineCount}`.split('').length}ch`
266+
)
267+
const lhsDiffNode = document.getElementById('lhsDiff')
268+
const rhsDiffNode = document.getElementById('rhsDiff')
269+
let comparator, comparer
270+
if (isLHSBigger) {
271+
comparer = lhsDiffNode
272+
comparator = rhsDiffNode
273+
} else {
274+
comparer = rhsDiffNode
275+
comparator = lhsDiffNode
276+
}
277+
this.comparator = comparator
260278
this.treeWalker = document.createTreeWalker(
261-
document.getElementById('lhsDiff'),
279+
comparer,
262280
NodeFilter.SHOW_ELEMENT,
263281
{
264282
acceptNode: (node) => {
265-
return node.classList.contains('bg-red-200')
283+
return (
284+
node.classList.contains('bg-red-200') ||
285+
node.classList.contains('bg-green-200')
286+
)
266287
},
267288
}
268289
)
269-
const maxLineCount =
270-
this.lhsDiff.length > this.rhsDiff.length
271-
? this.lhsDiff.length
272-
: this.rhsDiff.length
273-
document.documentElement.style.setProperty(
274-
'--max-line-number-characher',
275-
`${`${maxLineCount}`.split('').length}ch`
276-
)
277290
const { default: Driver } = await import('driver.js')
278291
const driver = new Driver({
279292
closeBtnText: 'Skip',
@@ -362,20 +375,44 @@ export default {
362375
const currentNode = this.treeWalker.currentNode
363376
const nextNode = this.treeWalker.nextNode()
364377
if (nextNode) {
378+
const currentNodeIndex = Array.prototype.indexOf.call(
379+
currentNode.parentElement.children,
380+
currentNode
381+
)
382+
const nextNodeIndex = Array.prototype.indexOf.call(
383+
nextNode.parentElement.children,
384+
nextNode
385+
)
386+
const comparatorCurrentNode = this.comparator.children[currentNodeIndex]
387+
const comparatorNextNode = this.comparator.children[nextNodeIndex]
365388
currentNode.querySelector('p').classList.remove('selected')
366-
nextNode.focus()
389+
comparatorCurrentNode.querySelector('p').classList.remove('selected')
367390
nextNode.querySelector('p').classList.add('selected')
391+
comparatorNextNode.querySelector('p').classList.add('selected')
368392
nextNode.scrollIntoView()
393+
comparatorNextNode.scrollIntoView()
369394
}
370395
},
371396
goToPreviousDiff() {
372397
const currentNode = this.treeWalker.currentNode
373398
const prevNode = this.treeWalker.previousNode()
374399
if (prevNode) {
400+
const currentNodeIndex = Array.prototype.indexOf.call(
401+
currentNode.parentElement.children,
402+
currentNode
403+
)
404+
const prevNodeIndex = Array.prototype.indexOf.call(
405+
prevNode.parentElement.children,
406+
prevNode
407+
)
408+
const comparatorCurrentNode = this.comparator.children[currentNodeIndex]
409+
const comparatorPrevNode = this.comparator.children[prevNodeIndex]
375410
currentNode.querySelector('p').classList.remove('selected')
376-
prevNode.focus()
411+
comparatorCurrentNode.querySelector('p').classList.remove('selected')
377412
prevNode.querySelector('p').classList.add('selected')
413+
comparatorPrevNode.querySelector('p').classList.add('selected')
378414
prevNode.scrollIntoView()
415+
comparatorPrevNode.scrollIntoView()
379416
}
380417
},
381418
},

0 commit comments

Comments
 (0)