10000 added showContext option · AXeL-dev/ngx-diff2html@5fd3a15 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5fd3a15

Browse files
author
Shivendu Amale
committed
added showContext option
1 parent 0a71bf0 commit 5fd3a15

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

projects/demo/src/app/app.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Component } from '@angular/core';
2-
//import { DiffStyle, DiffFormat } from 'ngx-diff2html';
32
import { DiffStyle, DiffFormat } from 'projects/ngx-diff2html/src/public-api';
43

54
@Component({

projects/ngx-diff2html/src/lib/ngx-diff2html.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class NgxDiff2htmlComponent implements OnInit, OnChanges {
1616
@Input() private filename: string = '';
1717
@Input() private format: DiffFormat = 'line-by-line';
1818
@Input() private style: DiffStyle = 'word';
19+
@Input() private showContext: boolean = false;
1920
@Output() diffChange: EventEmitter<string> = new EventEmitter();
2021
private diff: string = null;
2122
diffHTML: string = null;
@@ -27,7 +28,6 @@ export class NgxDiff2htmlComponent implements OnInit, OnChanges {
2728
}
2829

2930
ngOnChanges(changes: SimpleChanges) {
30-
//console.log(changes);
3131
if (this.propHasChanged(changes.left) || this.propHasChanged(changes.right)) {
3232
this.getDiff();
3333
} else if (this.propHasChanged(changes.style) || this.propHasChanged(changes.format)) {
@@ -40,7 +40,7 @@ export class NgxDiff2htmlComponent implements OnInit, OnChanges {
4040
}
4141

4242
getDiff() {
43-
this.diff = this.diffService.getDiff(this.left, this.right, this.filename);
43+
this.diff = this.diffService.getDiff(this.left, this.right, this.filename, this.showContext);
4444
this.refreshDiffHTML();
4545
this.diffChange.emit(this.diff);
4646
}

projects/ngx-diff2html/src/lib/ngx-diff2html.service.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class NgxDiff2htmlService {
1010

1111
constructor() { }
1212

13-
getDiff(text1: string, text2: string, filename: string = '') {
13+
getDiff(text1: string, text2: string, filename: string = '', showContext: boolean) {
1414
// Get diff
1515
const dmp = new diff_match_patch();
1616
const chars = dmp.diff_linesToChars_(text1, text2);
@@ -20,8 +20,12 @@ export class NgxDiff2htmlService {
2020
const diffs = dmp.diff_main(lineText1, lineText2, false);
2121
dmp.diff_charsToLines_(diffs, lineArray);
2222
const patchMake = dmp.patch_make(text1, diffs);
23+
24+
// Keep the context lines if showContext is set true.
25+
if (showContext) {
26+
patchMake[0].diffs = diffs;
27+
}
2328
const patchToText = dmp.patch_toText(patchMake);
24-
// console.info(patchToText);
2529

2630
// Make it look more like a unified diff style
2731
// ToDo: find a non tricky way to do this
@@ -34,12 +38,9 @@ export class NgxDiff2htmlService {
3438
}
3539
});
3640
const unifiedDiff = lines.join("\n");
37-
// console.info(unifiedDiff);
3841

3942
const strInput = "--- " + filename + " \n+++ " + filename + " \n" + unifiedDiff;
4043
const diff = decodeURIComponent(strInput);
41-
// console.info(diff);
42-
4344
// Return diff
4445
return diff;
4546
}

0 commit comments

Comments
 (0)
0