1- import { Directive , ElementRef , Input , OnDestroy , AfterViewInit , Inject , Optional , NgZone } from '@angular/core' ;
1+ import { Directive , ElementRef , OnDestroy , AfterViewInit , NgZone , input , model , inject } from '@angular/core' ;
22import { DOCUMENT } from '@angular/common' ;
33import { NgModel } from '@angular/forms' ;
44import { Subscription } from 'rxjs' ;
5- import { HighlightJsConfig , HIGHLIGHTJS_CONFIG } from './highlight-js.config' ;
5+ import { HIGHLIGHTJS_CONFIG } from './highlight-js.config' ;
66import type { HLJSApi , HLJSOptions } from 'highlight.js' ;
77
88declare const ngDevMode : boolean ;
99
1010@Directive ( {
1111 selector : '[highlight-js]' ,
1212 host : {
13- '[style.display]' : `mode === 'simple' ? 'none' : null` ,
13+ '[style.display]' : `mode() === 'simple' ? 'none' : null` ,
1414 } ,
1515 exportAs : 'highlightJs' ,
16- standalone : true ,
1716} )
1817export class HighlightJsDirective implements AfterViewInit , OnDestroy {
19- @ Input ( ) options ?: Partial < HLJSOptions > ;
20- @ Input ( ) lang = 'html' ;
21- @ Input ( ) code ?: string ;
22- @ Input ( ) mode : 'default' | 'simple' = 'simple' ;
18+ readonly options = input < Partial < HLJSOptions > > ( ) ;
19+ readonly l = input < string > ( 'html' , { alias : 'lang' } ) ;
20+ readonly code = model < string > ( ) ;
21+ readonly mode = input < 'default' | 'simple' > ( 'simple' ) ;
2322
2423 protected codeEl ?: HTMLElement ;
2524 protected parentEl ?: HTMLElement ;
2625 private modelValue$ ?: Subscription ;
2726 private observer ?: MutationObserver ;
27+ private el = inject < ElementRef < HTMLElement > > ( ElementRef ) ;
28+ private ngModel = inject < NgModel > ( NgModel , { optional : true } ) ;
29+ private doc = inject ( DOCUMENT ) ;
30+ private cog = inject ( HIGHLIGHTJS_CONFIG , { optional : true } ) ;
31+ private ngZone = inject ( NgZone ) ;
2832
29- constructor (
30- private el : ElementRef < HTMLElement > ,
31- @Optional ( ) private ngModel : NgModel ,
32- @Inject ( DOCUMENT ) private doc : any ,
33- @Optional ( ) @Inject ( HIGHLIGHTJS_CONFIG ) cog : HighlightJsConfig ,
34- private ngZone : NgZone ,
35- ) {
36- Object . assign ( this , cog ) ;
33+ constructor ( ) {
34+ Object . assign ( this , this . cog ) ;
3735 }
3836
3937 private escapeHTML ( str : string ) : string {
@@ -44,15 +42,16 @@ export class HighlightJsDirective implements AfterViewInit, OnDestroy {
4442 this . ngZone . runOutsideAngular ( ( ) => {
4543 this . destroy ( ) ;
4644 const el = this . el . nativeElement ;
47- const code = this . code || '' + el . innerHTML . trim ( ) ;
45+ const code = this . code ( ) ?? '' + el . innerHTML . trim ( ) ;
4846 const doc = this . doc as Document ;
49- this . codeEl = doc . createElement ( this . mode === 'default' ? 'div' : 'pre' ) as HTMLElement ;
47+ this . codeEl = doc . createElement ( this . mode ( ) === 'default' ? 'div' : 'pre' ) as HTMLElement ;
5048 if ( this . codeEl == null ) return ;
5149
52- const isSimple = this . mode === 'simple' ;
50+ const isSimple = this . mode ( ) === 'simple' ;
5351 if ( isSimple ) {
54- if ( this . lang ) {
55- this . codeEl . className = this . lang ;
52+ const lang = this . l ( ) ;
53+ if ( lang ) {
54+ this . codeEl . className = lang ;
5655 }
5756 this . parentEl = el . parentNode as HTMLElement ;
5857 this . parentEl . insertBefore ( this . codeEl , el . nextSibling ) ;
@@ -70,7 +69,7 @@ export class HighlightJsDirective implements AfterViewInit, OnDestroy {
7069 return ;
7170 }
7271
73- hljs . configure ( { ...this . options } ) ;
72+ hljs . configure ( { ...this . options ( ) } ) ;
7473
7574 if ( isSimple ) {
7675 hljs . highlightElement ( this . codeEl ) ;
@@ -93,7 +92,7 @@ export class HighlightJsDirective implements AfterViewInit, OnDestroy {
9392 this . init ( ) ;
9493 if ( this . ngModel ) {
9594 this . modelValue$ = this . ngModel . valueChanges ?. subscribe ( ( res ) => {
96- this . code = this . escapeHTML ( res ) ;
95+ this . code . set ( this . escapeHTML ( res ) ) ;
9796 this . init ( ) ;
9897 } ) ;
9998 } else {
0 commit comments