@@ -44,6 +44,11 @@ export class Diff2HtmlUI {
44
44
45
45
constructor ( target : HTMLElement , diffInput ?: string | DiffFile [ ] , config : Diff2HtmlUIConfig = { } , hljs ?: HLJSApi ) {
46
46
this . config = { ...defaultDiff2HtmlUIConfig , ...config } ;
47
+
48
+ if ( config . lazy && ( config . fileListStartVisible ?? true ) ) {
49
+ this . config . fileListStartVisible = true ;
50
+ }
51
+
47
52
this . diffFiles = typeof diffInput === 'string' ? parse ( diffInput , this . config ) : diffInput ?? [ ] ;
48
53
this . diffHtml = diffInput !== undefined ? html ( this . diffFiles , this . config ) : target . innerHTML ;
49
54
this . targetElement = target ;
@@ -85,13 +90,18 @@ export class Diff2HtmlUI {
85
90
const fileListItems : NodeListOf < HTMLElement > = this . targetElement . querySelectorAll ( '.d2h-file-name' ) ;
86
91
fileListItems . forEach ( ( i , idx ) =>
87
92
i . addEventListener ( 'click' , ( ) => {
88
- console . log ( 'HERE' ) ;
93
+ const fileId = i . getAttribute ( 'href' ) ;
94
+ if ( fileId && this . targetElement . querySelector ( fileId ) ) {
95
+ return ;
96
+ }
89
97
90
98
const tmpDiv = document . createElement ( 'div' ) ;
91
99
tmpDiv . innerHTML = htmlFile ( this . diffFiles [ idx ] , this . config ) ;
100
+ const fileElem = tmpDiv . querySelector ( '.d2h-file-wrapper' ) ;
92
101
93
- if ( tmpDiv . firstChild ) {
94
- this . targetElement . querySelector ( '.d2h-wrapper' ) ?. appendChild ( tmpDiv . firstChild ) ;
102
+ if ( fileElem ) {
103
+ this . targetElement . querySelector ( '.d2h-wrapper' ) ?. appendChild ( fileElem ) ;
104
+ this . highlightFile ( fileElem ) ;
95
105
}
96
106
} ) ,
97
107
) ;
@@ -159,43 +169,49 @@ export class Diff2HtmlUI {
159
169
160
170
// Collect all the diff files and execute the highlight on their lines
161
171
const files = this . targetElement . querySelectorAll ( '.d2h-file-wrapper' ) ;
162
- files . forEach ( file => {
172
+ files . forEach ( this . highlightFile ) ;
173
+ }
174
+
175
+ highlightFile ( file : Element ) : void {
176
+ if ( this . hljs === null ) {
177
+ throw new Error ( 'Missing a `highlight.js` implementation. Please provide one when instantiating Diff2HtmlUI.' ) ;
178
+ }
179
+
180
+ // HACK: help Typescript know that `this.hljs` is defined since we already checked it
181
+ if ( this . hljs === null ) return ;
182
+ const language = file . getAttribute ( 'data-lang' ) ;
183
+ const hljsLanguage = language ? getLanguage ( language ) : 'plaintext' ;
184
+
185
+ // Collect all the code lines and execute the highlight on them
186
+ const codeLines = file . querySelectorAll ( '.d2h-code-line-ctn' ) ;
187
+ codeLines . forEach ( line => {
163
188
// HACK: help Typescript know that `this.hljs` is defined since we already checked it
164
189
if ( this . hljs === null ) return ;
165
- const language = file . getAttribute ( 'data-lang' ) ;
166
- const hljsLanguage = language ? getLanguage ( language ) : 'plaintext' ;
167
-
168
- // Collect all the code lines and execute the highlight on them
169
- const codeLines = file . querySelectorAll ( '.d2h-code-line-ctn' ) ;
170
- codeLines . forEach ( line => {
171
- // HACK: help Typescript know that `this.hljs` is defined since we already checked it
172
- if ( this . hljs === null ) return ;
173
-
174
- const text = line . textContent ;
175
- const lineParent = line . parentNode ;
176
-
177
- if ( text === null || lineParent === null || ! this . isElement ( lineParent ) ) return ;
178
-
179
- const result : HighlightResult = closeTags (
180
- this . hljs . highlight ( text , {
181
- language : hljsLanguage ,
182
- ignoreIllegals : true ,
183
- } ) ,
184
- ) ;
185
-
186
- const originalStream = nodeStream ( line ) ;
187
- if ( originalStream . length ) {
188
- const resultNode = document . createElementNS ( 'http://www.w3.org/1999/xhtml' , 'div' ) ;
189
- resultNode . innerHTML = result . value ;
190
- result . value = mergeStreams ( originalStream , nodeStream ( resultNode ) , text ) ;
191
- }
192
190
193
- line . classList . add ( 'hljs' ) ;
194
- if ( result . language ) {
195
- line . classList . add ( result . language ) ;
196
- }
197
- line . innerHTML = result . value ;
198
- } ) ;
191
+ const text = line . textContent ;
192
+ const lineParent = line . parentNode ;
193
+
194
+ if ( text === null || lineParent === null || ! this . isElement ( lineParent ) ) return ;
195
+
196
+ const result : HighlightResult = closeTags (
197
+ this . hljs . highlight ( text , {
198
+ language : hljsLanguage ,
199
+ ignoreIllegals : true ,
200
+ } ) ,
201
+ ) ;
202
+
203
+ const originalStream = nodeStream ( line ) ;
204
+ if ( originalStream . length ) {
205
+ const resultNode = document . createElementNS ( 'http://www.w3.org/1999/xhtml' , 'div' ) ;
206
+ resultNode . innerHTML = result . value ;
207
+ result . value = mergeStreams ( originalStream , nodeStream ( resultNode ) , text ) ;
208
+ }
209
+
210
+ line . classList . add ( 'hljs' ) ;
211
+ if ( result . language ) {
212
+ line . classList . add ( result . language ) ;
213
+ }
214
+ line . innerHTML = result . value ;
199
215
} ) ;
200
216
}
201
217
0 commit comments