Skip to content

Commit 1b07b78

Browse files
committed
refactor: make sure observer used to add the line numbers is destroyed
1 parent 1a1cae7 commit 1b07b78

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

projects/ngx-highlightjs/src/lib/highlight.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
EventEmitter,
1010
ElementRef
1111
} from '@angular/core';
12+
import { animationFrameScheduler } from 'rxjs';
1213
import { HighlightJS } from './highlight.service';
1314
import { HIGHLIGHT_OPTIONS, HighlightOptions, HighlightResult } from './highlight.model';
14-
import { animationFrameScheduler } from 'rxjs';
1515

1616
@Directive({
1717
host: {
@@ -24,6 +24,9 @@ export class Highlight implements OnChanges {
2424
// Highlighted Code
2525
private readonly _nativeElement: HTMLElement;
2626

27+
// Temp observer to observe when line numbers has been added to code element
28+
private _lineNumbersObs: any;
29+
2730
// Highlight code input
2831
@Input('highlight') code!: string;
2932

@@ -67,26 +70,38 @@ export class Highlight implements OnChanges {
6770
this.setCode(res.value);
6871
// Check if user want to show line numbers
6972
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();
8474
}
8575
// Forward highlight response to the highlighted output
8676
this.highlighted.emit(res);
8777
});
8878
}
8979

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+
90105
private setCode(content: string) {
91106
animationFrameScheduler.schedule(() => this._nativeElement.innerHTML = content);
92107
}

0 commit comments

Comments
 (0)