v-code-diff 1.0.0 → 1.2.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
@@ -2,7 +2,7 @@ import * as Diff from 'diff'
2
2
  import type { Change } from 'diff'
3
3
  import hljs from './highlight'
4
4
  import { DiffType } from './types'
5
- import type { DiffLine, SplitViewerChange, UnifiedViewerChange } from './types'
5
+ import type { DiffLine, DiffStat, SplitDiffLine, SplitViewerChange, UnifiedLine, UnifiedViewerChange } from './types'
6
6
 
7
7
  const MODIFIED_START_TAG = '<code-diff-modified>'
8
8
  const MODIFIED_CLOSE_TAG = '</code-diff-modified>'
@@ -113,13 +113,28 @@ const getHighlightCode = (language: string, code: string) => {
113
113
  .replace(new RegExp(closeEntity, 'g'), '</span>')
114
114
  }
115
115
 
116
+ function calcDiffStat(changes: Change[]): DiffStat {
117
+ const count = (s: string, c: string) => (s.match(new RegExp(c, 'g')) || []).length
118
+
119
+ let additionsNum = 0
120
+ let deletionsNum = 0
121
+ for (const change of changes) {
122
+ if (change.added)
123
+ additionsNum += count(change.value.trim(), '\n') + 1
124
+
125
+ if (change.removed)
126
+ deletionsNum += count(change.value.trim(), '\n') + 1
127
+ }
128
+ return { additionsNum, deletionsNum }
129
+ }
130
+
116
131
  export function createSplitDiff(
117
132
  oldString: string,
118
133
  newString: string,
119
134
  language = 'plaintext',
120
135
  diffStyle = 'word',
121
136
  context = 10,
122
- ) {
137
+ ): SplitViewerChange {
123
138
  const newEmptySplitDiff = (): DiffLine => ({ type: DiffType.EMPTY })
124
139
  const newSplitDiff = (type: DiffType, num: number, code: string): DiffLine => ({ type, num, code })
125
140
 
@@ -129,7 +144,12 @@ export function createSplitDiff(
129
144
  let addNum = 0
130
145
  let skip = false
131
146
 
132
- const result: SplitViewerChange = []
147
+ const rawChanges: SplitDiffLine[] = []
148
+ const result: SplitViewerChange = {
149
+ changes: rawChanges,
150
+ stat: calcDiffStat(changes),
151
+ }
152
+
133
153
  for (let i = 0; i < changes.length; i++) {
134
154
  if (skip) {
135
155
  skip = false
@@ -168,7 +188,7 @@ export function createSplitDiff(
168
188
  right = newSplitDiff(DiffType.ADD, addNum, highlightCode)
169
189
  }
170
190
 
171
- result.push({ left, right })
191
+ rawChanges.push({ left, right })
172
192
  }
173
193
  break
174
194
  }
@@ -181,7 +201,7 @@ export function createSplitDiff(
181
201
  addNum++
182
202
 
183
203
  const highlightCode = getHighlightCode(language, line)
184
- result.push({
204
+ rawChanges.push({
185
205
  left: newSplitDiff(DiffType.EQUAL, delNum, highlightCode),
186
206
  right: newSplitDiff(DiffType.EQUAL, addNum, highlightCode),
187
207
  })
@@ -195,7 +215,7 @@ export function createSplitDiff(
195
215
  for (const line of curLines) {
196
216
  delNum++
197
217
 
198
- result.push({
218
+ rawChanges.push({
199
219
  left: newSplitDiff(DiffType.DELETE, delNum, getHighlightCode(language, line)),
200
220
  right: newEmptySplitDiff(),
201
221
  })
@@ -225,7 +245,7 @@ export function createSplitDiff(
225
245
  ? newSplitDiff(DiffType.ADD, addNum, getHighlightCode(language, rightLine))
226
246
  : newEmptySplitDiff()
227
247
 
228
- result.push({ left, right })
248
+ rawChanges.push({ left, right })
229
249
  }
230
250
  }
231
251
  }
@@ -233,7 +253,7 @@ export function createSplitDiff(
233
253
  if (curType === DiffType.ADD) {
234
254
  for (const line of curLines) {
235
255
  addNum++
236
- result.push({
256
+ rawChanges.push({
237
257
  left: newEmptySplitDiff(),
238
258
  right: newSplitDiff(DiffType.ADD, addNum, getHighlightCode(language, line)),
239
259
  })
@@ -242,37 +262,38 @@ export function createSplitDiff(
242
262
  }
243
263
 
244
264
  if (oldString === newString) {
245
- for (let i = 0; i < result.length; i++)
246
- result[i].fold = false
265
+ for (let i = 0; i < rawChanges.length; i++)
266
+ rawChanges[i].fold = false
247
267
 
248
268
  return result
249
269
  }
250
270
 
251
- for (let i = 0; i < result.length; i++) {
252
- const line = result[i]
271
+ for (let i = 0; i < rawChanges.length; i++) {
272
+ const line = rawChanges[i]
253
273
  if (line.left.type === DiffType.DELETE || line.right.type === DiffType.ADD) {
254
- const [start, end] = [Math.max(i - context, 0), Math.min(i + context + 1, result.length)]
274
+ const [start, end] = [Math.max(i - context, 0), Math.min(i + context + 1, rawChanges.length)]
255
275
  for (let j = start; j < end; j++)
256
- result[j].fold = false
276
+ rawChanges[j].fold = false
257
277
  }
258
278
  if (line.fold === undefined)
259
279
  line.fold = true
260
280
  }
261
281
 
262
- const realResult = []
263
- for (let i = 0; i < result.length; i++) {
264
- const line = result[i]
282
+ const processedChanges = []
283
+ for (let i = 0; i < rawChanges.length; i++) {
284
+ const line = rawChanges[i]
265
285
  if (line.fold === false) {
266
- realResult.push(line)
286
+ processedChanges.push(line)
267
287
  continue
268
288
  }
269
289
  if (line.fold === true) {
270
- if (realResult[realResult.length - 1]?.fold !== true)
271
- realResult.push(line)
290
+ if (processedChanges[processedChanges.length - 1]?.fold !== true)
291
+ processedChanges.push(line)
272
292
  }
273
293
  }
294
+ result.changes = processedChanges
274
295
 
275
- return realResult
296
+ return result
276
297
  }
277
298
 
278
299
  export function createUnifiedDiff(
@@ -281,14 +302,19 @@ export function createUnifiedDiff(
281
302
  language = 'plaintext',
282
303
  diffStyle = 'word',
283
304
  context = 2,
284
- ) {
305
+ ): UnifiedViewerChange {
285
306
  const changes = Diff.diffLines(oldString, newString)
286
307
 
287
308
  let delNum = 0
288
309
  let addNum = 0
289
310
  let skip = false
290
311
 
291
- const result: UnifiedViewerChange = []
312
+ const rawChanges: UnifiedLine[] = []
313
+ const result: UnifiedViewerChange = {
314
+ changes: rawChanges,
315
+ stat: calcDiffStat(changes),
316
+ }
317
+
292
318
  for (let i = 0; i < changes.length; i++) {
293
319
  if (skip) {
294
320
  skip = false
@@ -315,7 +341,7 @@ export function createUnifiedDiff(
315
341
 
316
342
  const code = getHighlightCode(language, line)
317
343
 
318
- result.push({
344
+ rawChanges.push({
319
345
  type: curType,
320
346
  code,
321
347
  addNum: curType === DiffType.DELETE ? undefined : addNum,
@@ -333,7 +359,7 @@ export function createUnifiedDiff(
333
359
  addNum++
334
360
  const code = getHighlightCode(language, line)
335
361
 
336
- result.push({ type: DiffType.EQUAL, code, delNum, addNum })
362
+ rawChanges.push({ type: DiffType.EQUAL, code, delNum, addNum })
337
363
  }
338
364
  }
339
365
 
@@ -348,7 +374,7 @@ export function createUnifiedDiff(
348
374
  delNum++
349
375
 
350
376
  const code = getHighlightCode(language, renderWords(nextLine, curLine, diffStyle))
351
- result.push({ type: DiffType.DELETE, code, delNum })
377
+ rawChanges.push({ type: DiffType.DELETE, code, delNum })
352
378
  }
353
379
 
354
380
  for (let j = 0; j < nextLines.length; j++) {
@@ -357,7 +383,7 @@ export function createUnifiedDiff(
357
383
  addNum++
358
384
 
359
385
  const code = getHighlightCode(language, renderWords(curLine, nextLine, diffStyle))
360
- result.push({ type: DiffType.ADD, code, addNum })
386
+ rawChanges.push({ type: DiffType.ADD, code, addNum })
361
387
  }
362
388
 
363
389
  skip = true
@@ -368,7 +394,7 @@ export function createUnifiedDiff(
368
394
  delNum++
369
395
 
370
396
  const code = getHighlightCode(language, line)
371
- result.push({ type: DiffType.DELETE, code, delNum })
397
+ rawChanges.push({ type: DiffType.DELETE, code, delNum })
372
398
  }
373
399
  }
374
400
  }
@@ -378,41 +404,42 @@ export function createUnifiedDiff(
378
404
  addNum++
379
405
  const code = getHighlightCode(language, line)
380
406
 
381
- result.push({ type: DiffType.ADD, code, addNum })
407
+ rawChanges.push({ type: DiffType.ADD, code, addNum })
382
408
  }
383
409
  }
384
410
  }
385
411
 
386
- for (let i = 0; i < result.length; i++) {
387
- const line = result[i]
412
+ for (let i = 0; i < rawChanges.length; i++) {
413
+ const line = rawChanges[i]
388
414
  if (line.type === DiffType.DELETE || line.type === DiffType.ADD) {
389
- const [start, end] = [Math.max(i - context, 0), Math.min(i + context + 1, result.length)]
415
+ const [start, end] = [Math.max(i - context, 0), Math.min(i + context + 1, rawChanges.length)]
390
416
  for (let j = start; j < end; j++)
391
- result[j].fold = false
417
+ rawChanges[j].fold = false
392
418
  }
393
419
  if (line.fold === undefined)
394
420
  line.fold = true
395
421
  }
396
422
 
397
423
  if (oldString === newString) {
398
- for (let i = 0; i < result.length; i++)
399
- result[i].fold = false
424
+ for (let i = 0; i < rawChanges.length; i++)
425
+ rawChanges[i].fold = false
400
426
 
401
427
  return result
402
428
  }
403
429
 
404
- const realResult = []
405
- for (let i = 0; i < result.length; i++) {
406
- const line = result[i]
430
+ const processedChanges = []
431
+ for (let i = 0; i < rawChanges.length; i++) {
432
+ const line = rawChanges[i]
407
433
  if (line.fold === false) {
408
- realResult.push(line)
434
+ processedChanges.push(line)
409
435
  continue
410
436
  }
411
437
  if (line.fold === true) {
412
- if (i === 0 || realResult[realResult.length - 1]?.fold !== true)
413
- realResult.push(line)
438
+ if (i === 0 || processedChanges[processedChanges.length - 1]?.fold !== true)
439
+ processedChanges.push(line)
414
440
  }
415
441
  }
442
+ result.changes = processedChanges
416
443
 
417
- return realResult
444
+ return result
418
445
  }
package/src/var.scss CHANGED
@@ -1,4 +1,4 @@
1
- .file {
1
+ .code-diff-view {
2
2
  --color-canvas-default-transparent: rgba(255,255,255,0);
3
3
  --color-page-header-bg: #f6f8fa;
4
4
  --color-marketing-icon-primary: #218bff;
@@ -436,4 +436,4 @@
436
436
  --color-scale-coral-7: #801f0f;
437
437
  --color-scale-coral-8: #691105;
438
438
  --color-scale-coral-9: #510901;
439
- }
439
+ }