9
9
EventEmitter ,
10
10
ElementRef
11
11
} from '@angular/core' ;
12
+ import { animationFrameScheduler } from 'rxjs' ;
12
13
import { HighlightJS } from './highlight.service' ;
13
14
import { HIGHLIGHT_OPTIONS , HighlightOptions , HighlightResult } from './highlight.model' ;
14
- import { animationFrameScheduler } from 'rxjs' ;
15
15
16
16
@Directive ( {
17
17
host : {
@@ -24,6 +24,9 @@ export class Highlight implements OnChanges {
24
24
// Highlighted Code
25
25
private readonly _nativeElement : HTMLElement ;
26
26
27
+ // Temp observer to observe when line numbers has been added to code element
28
+ private _lineNumbersObs : any ;
29
+
27
30
// Highlight code input
28
31
@Input ( 'highlight' ) code ! : string ;
29
32
@@ -67,26 +70,38 @@ export class Highlight implements OnChanges {
67
70
this . setCode ( res . value ) ;
68
71
// Check if user want to show line numbers
69
72
if ( this . lineNumbers && this . _options && this . _options . lineNumbers ) {
70
- animationFrameScheduler . schedule ( ( ) => {
71
- // Add line numbers
72
- this . _hljs . lineNumbersBlock ( this . _nativeElement ) . subscribe ( ) ;
73
- // If code lines is only 1, the library will not add numbers
74
- // Observe changes to add 'hljs-line-numbers' class only when line numbers is added to the code element
75
- let obs = new MutationObserver ( ( ) => {
76
- if ( this . _nativeElement . firstElementChild . tagName . toUpperCase ( ) === 'TABLE' ) {
77
- this . _nativeElement . classList . add ( 'hljs-line-numbers' ) ;
78
- }
79
- obs . disconnect ( ) ;
80
- obs = null ;
81
- } ) ;
82
- obs . observe ( this . _nativeElement , { childList : true } ) ;
83
- } ) ;
73
+ this . addLineNumbers ( ) ;
84
74
}
85
75
// Forward highlight response to the highlighted output
86
76
this . highlighted . emit ( res ) ;
87
77
} ) ;
88
78
}
89
79
80
+ private addLineNumbers ( ) {
81
+ // Clean up line numbers observer
82
+ this . destroyLineNumbersObserver ( ) ;
83
+ animationFrameScheduler . schedule ( ( ) => {
84
+ // Add line numbers
85
+ this . _hljs . lineNumbersBlock ( this . _nativeElement ) . subscribe ( ) ;
86
+ // If lines count is 1, the line numbers library will not add numbers
87
+ // Observe changes to add 'hljs-line-numbers' class only when line numbers is added to the code element
88
+ this . _lineNumbersObs = new MutationObserver ( ( ) => {
89
+ if ( this . _nativeElement . firstElementChild . tagName . toUpperCase ( ) === 'TABLE' ) {
90
+ this . _nativeElement . classList . add ( 'hljs-line-numbers' ) ;
91
+ }
92
+ this . destroyLineNumbersObserver ( ) ;
93
+ } ) ;
94
+ this . _lineNumbersObs . observe ( this . _nativeElement , { childList : true } ) ;
95
+ } ) ;
96
+ }
97
+
98
+ private destroyLineNumbersObserver ( ) {
99
+ if ( this . _lineNumbersObs ) {
100
+ this . _lineNumbersObs . disconnect ( ) ;
101
+ this . _lineNumbersObs = null ;
102
+ }
103
+ }
104
+
90
105
private setCode ( content : string ) {
91
106
animationFrameScheduler . schedule ( ( ) => this . _nativeElement . innerHTML = content ) ;
92
107
}
0 commit comments