11import { 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 ( / % 0 A ( .) / g, "%0A-$1" ) ;
61- } else if ( line . startsWith ( '+' ) ) {
62- lines [ index ] = line . replace ( / % 0 A ( .) / 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}
0 commit comments