@@ -3,7 +3,7 @@ import type { Change } from 'diff'
33import { DIFF_DELETE , DIFF_INSERT , diff_match_patch as DiffMatchPatch } from 'diff-match-patch'
44import hljs from './highlight'
55import { DiffType } from './types'
6- import type { DiffLine , DiffStat , SplitLineChange , SplitViewerChange , UnifiedLineChange , UnifiedViewerChange } from './types'
6+ import type { DiffLine , DiffStat , SplitLineChange , SplitLineUnchanges , SplitViewerChange , UnifiedLineChange , UnifiedLineUnchanges , UnifiedViewerChange } from './types'
77
88const MODIFIED_START_TAG = '<code-diff-modified>'
99const MODIFIED_CLOSE_TAG = '</code-diff-modified>'
@@ -168,6 +168,7 @@ export function createSplitDiff(
168168 const rawChanges : SplitLineChange [ ] = [ ]
169169 const result : SplitViewerChange = {
170170 changes : rawChanges ,
171+ collector : [ ] ,
171172 stat : calcDiffStat ( changes ) ,
172173 }
173174
@@ -208,7 +209,6 @@ export function createSplitDiff(
208209 left = newEmptySplitDiff ( )
209210 right = newSplitDiff ( DiffType . ADD , addNum , highlightCode )
210211 }
211-
212212 rawChanges . push ( { left, right } )
213213 }
214214 break
@@ -300,17 +300,35 @@ export function createSplitDiff(
300300 line . fold = true
301301 }
302302
303- const processedChanges = [ ]
303+ const processedChanges : SplitViewerChange [ 'changes' ] = [ ]
304+ let unchanges : SplitLineUnchanges [ 'lines' ] = [ ] // collector for unchanged lines.
305+
304306 for ( let i = 0 ; i < rawChanges . length ; i ++ ) {
305307 const line = rawChanges [ i ]
306308 if ( line . fold === false ) {
309+ if ( unchanges . length ) {
310+ unchanges [ 0 ] . hideIndex = result . collector . length
311+ result . collector . push ( {
312+ lines : unchanges ,
313+ fold : true ,
314+ } )
315+ unchanges = [ ]
316+ }
307317 processedChanges . push ( line )
308318 continue
309319 }
310- if ( line . fold === true ) {
311- if ( processedChanges [ processedChanges . length - 1 ] ?. fold !== true )
312- processedChanges . push ( line )
313- }
320+
321+ line . hide = true
322+ unchanges . push ( line )
323+ processedChanges . push ( line )
324+ }
325+ if ( unchanges . length ) {
326+ unchanges [ 0 ] . hideIndex = result . collector . length
327+ result . collector . push ( {
328+ lines : unchanges ,
329+ fold : true ,
330+ } )
331+ unchanges = [ ]
314332 }
315333 result . changes = processedChanges
316334
@@ -333,6 +351,7 @@ export function createUnifiedDiff(
333351 const rawChanges : UnifiedLineChange [ ] = [ ]
334352 const result : UnifiedViewerChange = {
335353 changes : rawChanges ,
354+ collector : [ ] ,
336355 stat : calcDiffStat ( changes ) ,
337356 }
338357
@@ -449,16 +468,37 @@ export function createUnifiedDiff(
449468 }
450469
451470 const processedChanges = [ ]
471+ let unchanges : UnifiedLineUnchanges [ 'lines' ] = [ ] // collector for unchanged lines.
472+
452473 for ( let i = 0 ; i < rawChanges . length ; i ++ ) {
453474 const line = rawChanges [ i ]
454475 if ( line . fold === false ) {
476+ if ( unchanges . length ) {
477+ unchanges [ 0 ] . hideIndex = result . collector . length
478+ // Keeps "hideIndex" in first element of collector
479+ // for delegating lines to expand.
480+ result . collector . push ( {
481+ lines : unchanges ,
482+ fold : true ,
483+ } )
484+ unchanges = [ ]
485+ }
455486 processedChanges . push ( line )
456487 continue
457488 }
458- if ( line . fold === true ) {
459- if ( i === 0 || processedChanges [ processedChanges . length - 1 ] ?. fold !== true )
460- processedChanges . push ( line )
489+ if ( line . type === 'equal' ) {
490+ line . hide = true
491+ unchanges . push ( line )
461492 }
493+ processedChanges . push ( line )
494+ }
495+ if ( unchanges . length ) {
496+ unchanges [ 0 ] . hideIndex = result . collector . length
497+ result . collector . push ( {
498+ lines : unchanges ,
499+ fold : true ,
500+ } )
501+ unchanges = [ ]
462502 }
463503 result . changes = processedChanges
464504
0 commit comments