Skip to content

Commit 1773e5e

Browse files
committed
Refactoring library code
1 parent faede0c commit 1773e5e

2 files changed

Lines changed: 58 additions & 47 deletions

File tree

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { Component, OnInit, Input, OnChanges, SimpleChanges, SimpleChange, ViewEncapsulation } from '@angular/core';
2-
import { diff_match_patch } from 'diff-match-patch';
3-
import { Diff2Html } from 'diff2html';
2+
import { NgxDiff2htmlService } from './ngx-diff2html.service';
43

54
@Component({
65
selector: 'ngx-diff2html',
76
template: `
8-
<div [innerHtml]="diffOutput"></div>
7+
<div [innerHtml]="diffHTML"></div>
98
`,
109
styleUrls: ['./ngx-diff2html.component.css'],
1110
encapsulation: ViewEncapsulation.None
@@ -15,70 +14,37 @@ export class NgxDiff2htmlComponent implements OnInit, OnChanges {
1514
@Input() private left: string;
1615
@Input() private right: string;
1716
@Input() private filename: string = ' '; // cannot be null or empty
18-
@Input() private style: 'word' | 'char' = 'word';
1917
@Input() private format: 'side-by-side' | 'line-by-line' = 'line-by-line';
18+
@Input() private style: 'word' | 'char' = 'word';
2019
private diff: string = null;
21-
diffOutput: string = null;
20+
diffHTML: string = null;
2221

23-
constructor() { }
22+
constructor(private diffService: NgxDiff2htmlService) { }
2423

2524
ngOnInit() {
26-
this.diffOutput = this.getDiffOutput(this.left, this.right, this.filename);
25+
this.getDiff();
2726
}
2827

2928
ngOnChanges(changes: SimpleChanges) {
3029
//console.log(changes);
3130
if (this.propHasChanged(changes.left) || this.propHasChanged(changes.right)) {
32-
this.diffOutput = this.getDiffOutput(this.left, this.right, this.filename);
31+
this.getDiff();
3332
} else if (this.propHasChanged(changes.style) || this.propHasChanged(changes.format)) {
34-
this.reloadDiff();
33+
this.refreshDiffHTML();
3534
}
3635
}
3736

3837
private propHasChanged(change: SimpleChange) {
3938
return change && !change.isFirstChange() && change.currentValue !== change.previousValue;
4039
}
4140

42-
private getDiffOutput(text1: string, text2: string, filename: string) {
43-
// Get diff
44-
const dmp = new diff_match_patch();
45-
const chars = dmp.diff_linesToChars_(text1, text2);
46-
const lineText1 = chars.chars1;
47-
const lineText2 = chars.chars2;
48-
const lineArray = chars.lineArray;
49-
const diffs = dmp.diff_main(lineText1, lineText2, false);
50-
dmp.diff_charsToLines_(diffs, lineArray);
51-
const patchMake = dmp.patch_make(text1, diffs);
52-
const patchToText = dmp.patch_toText(patchMake);
53-
// console.info(patchToText);
54-
55-
// Make it look more like a unified diff style
56-
// ToDo: find a non tricky way to do this
57-
let lines = patchToText.split("\n");
58-
lines.forEach((line: string, index: number) => {
59-
if (line.startsWith('-')) {
60-
lines[index] = line.replace(/%0A(.)/g, "%0A-$1");
61-
} else if (line.startsWith('+')) {
62-
lines[index] = line.replace(/%0A(.)/g, "%0A+$1");
63-
}
64-
});
65-
const diff = lines.join("\n");
66-
// console.info(diff);
67-
68-
const strInput = "--- " + filename + "\n+++ " + filename + "\n" + diff;
69-
this.diff = decodeURIComponent(strInput); // save diff to use it on reload
70-
// console.info(this.diff);
71-
72-
// Return diff in HTML format
73-
return this.diffToHTML(this.diff);
74-
}
75-
76-
private diffToHTML(diff: string) {
77-
return Diff2Html.getPrettyHtml(diff, {inputFormat: 'diff', matching: 'lines', outputFormat: this.format, diffStyle: this.style});
41+
getDiff() {
42+
this.diff = this.diffService.getDiff(this.left, this.right, this.filename);
43+
this.refreshDiffHTML();
7844
}
7945

80-
reloadDiff() {
81-
this.diffOutput = this.diffToHTML(this.diff);
46+
refreshDiffHTML() {
47+
this.diffHTML = this.diffService.diffToHTML(this.diff, this.format, this.style);
8248
}
8349

8450
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
11
import { Injectable } from '@angular/core';
2+
import { diff_match_patch } from 'diff-match-patch';
3+
import { Diff2Html } from 'diff2html';
24

35
@Injectable({
46
providedIn: 'root'
57
})
68
export class NgxDiff2htmlService {
79

810
constructor() { }
11+
12+
getDiff(text1: string, text2: string, filename: string) {
13+
// Get diff
14+
const dmp = new diff_match_patch();
15+
const chars = dmp.diff_linesToChars_(text1, text2);
16+
const lineText1 = chars.chars1;
17+
const lineText2 = chars.chars2;
18+
const lineArray = chars.lineArray;
19+
const diffs = dmp.diff_main(lineText1, lineText2, false);
20+
dmp.diff_charsToLines_(diffs, lineArray);
21+
const patchMake = dmp.patch_make(text1, diffs);
22+
const patchToText = dmp.patch_toText(patchMake);
23+
// console.info(patchToText);
24+
25+
// Make it look more like a unified diff style
26+
// ToDo: find a non tricky way to do this
27+
let lines = patchToText.split("\n");
28+
lines.forEach((line: string, index: number) => {
29+
if (line.startsWith('-')) {
30+
lines[index] = line.replace(/%0A(.)/g, "%0A-$1");
31+
} else if (line.startsWith('+')) {
32+
lines[index] = line.replace(/%0A(.)/g, "%0A+$1");
33+
}
34+
});
35+
const unifiedDiff = lines.join("\n");
36+
// console.info(unifiedDiff);
37+
38+
const strInput = "--- " + filename + "\n+++ " + filename + "\n" + unifiedDiff;
39+
const diff = decodeURIComponent(strInput);
40+
// console.info(diff);
41+
42+
// Return diff
43+
return diff;
44+
}
45+
46+
diffToHTML(diff: string, format: 'side-by-side' | 'line-by-line', style: 'word' | 'char') {
47+
return Diff2Html.getPrettyHtml(diff, {
48+
inputFormat: 'diff',
49+
matching: 'lines',
50+
outputFormat: format,
51+
diffStyle: style
52+
});
53+
}
954
}

0 commit comments

Comments
 (0)