v-code-diff 1.3.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -3,7 +3,7 @@ import type { Change } from 'diff'
3
3
  import { DIFF_DELETE, DIFF_INSERT, diff_match_patch as DiffMatchPatch } from 'diff-match-patch'
4
4
  import hljs from './highlight'
5
5
  import { 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'
7
7
 
8
8
  const MODIFIED_START_TAG = '<code-diff-modified>'
9
9
  const MODIFIED_CLOSE_TAG = '</code-diff-modified>'
@@ -168,6 +168,7 @@ export function createSplitDiff(
168
168
  const rawChanges: SplitLineChange[] = []
169
169
  const result: SplitViewerChange = {
170
170
  changes: rawChanges,
171
+ collector: [],
171
172
  stat: calcDiffStat(changes),
172
173
  }
173
174
 
@@ -208,7 +209,6 @@ export function createSplitDiff(
208
209
  left = newEmptySplitDiff()
209
210
  right = newSplitDiff(DiffType.ADD, addNum, highlightCode)
210
211
  }
211
-
212
212
  rawChanges.push({ left, right })
213
213
  }
214
214
  break
@@ -300,17 +300,35 @@ export function createSplitDiff(
300
300
  line.fold = true
301
301
  }
302
302
 
303
- const processedChanges = []
303
+ const processedChanges: SplitViewerChange['changes'] = []
304
+ let unchanges: SplitLineUnchanges['lines'] = [] // collector for unchanged lines.
305
+
304
306
  for (let i = 0; i < rawChanges.length; i++) {
305
307
  const line = rawChanges[i]
306
308
  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
+ }
307
317
  processedChanges.push(line)
308
318
  continue
309
319
  }
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 = []
314
332
  }
315
333
  result.changes = processedChanges
316
334
 
@@ -333,6 +351,7 @@ export function createUnifiedDiff(
333
351
  const rawChanges: UnifiedLineChange[] = []
334
352
  const result: UnifiedViewerChange = {
335
353
  changes: rawChanges,
354
+ collector: [],
336
355
  stat: calcDiffStat(changes),
337
356
  }
338
357
 
@@ -449,16 +468,37 @@ export function createUnifiedDiff(
449
468
  }
450
469
 
451
470
  const processedChanges = []
471
+ let unchanges: UnifiedLineUnchanges['lines'] = [] // collector for unchanged lines.
472
+
452
473
  for (let i = 0; i < rawChanges.length; i++) {
453
474
  const line = rawChanges[i]
454
475
  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
+ }
455
486
  processedChanges.push(line)
456
487
  continue
457
488
  }
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)
461
492
  }
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 = []
462
502
  }
463
503
  result.changes = processedChanges
464
504