1- import { Component , OnInit } from '@angular/core' ;
2- import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
1+ import { AfterViewInit , Component , DestroyRef , OnInit , inject } from '@angular/core' ;
2+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
3+ import { FormGroup , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
34import { MatSidenavModule } from '@angular/material/sidenav' ;
45import { MtxSplitModule } from '@ng-matero/extensions/split' ;
56
@@ -21,14 +22,20 @@ import { languages } from '@codemirror/language-data';
2122 templateUrl : './home.component.html' ,
2223 styleUrl : './home.component.scss' ,
2324} )
24- export class HomeComponent implements OnInit {
25+ export class HomeComponent implements OnInit , AfterViewInit {
26+ private readonly destroyRef = inject ( DestroyRef ) ;
27+
2528 languages = languages ;
2629
30+ form = new FormGroup ( { } ) ;
31+
2732 config : GuiFields = {
2833 language : {
2934 type : 'select' ,
3035 name : 'Language' ,
31- options : languages . map ( lang => ( { label : lang . name , value : lang . name . toLowerCase ( ) } ) ) ,
36+ options : languages
37+ . map ( lang => ( { label : lang . name , value : lang . name . toLowerCase ( ) } ) )
38+ . sort ( ( a , b ) => a . label . localeCompare ( b . label ) ) ,
3239 } ,
3340 theme : {
3441 type : 'buttonToggle' ,
@@ -108,14 +115,36 @@ export class HomeComponent implements OnInit {
108115 indentUnit : '' ,
109116 lineWrapping : false ,
110117 highlightWhitespace : false ,
111- height : { value : 200 , unit : 'px ' } ,
118+ height : { value : 100 , unit : '% ' } ,
112119 } ;
113120
114- code = 'console.log("Hello world") ' ;
121+ code = '' ;
115122
116123 log ( e : any ) {
117124 console . log ( e ) ;
118125 }
119126
120- ngOnInit ( ) : void { }
127+ ngOnInit ( ) : void {
128+ this . getLangSample ( 'javascript' ) ;
129+ }
130+
131+ ngAfterViewInit ( ) : void {
132+ this . form
133+ . get ( 'language' )
134+ ?. valueChanges . pipe ( takeUntilDestroyed ( this . destroyRef ) )
135+ . subscribe ( ( lang : string ) => {
136+ console . log ( lang ) ;
137+ this . getLangSample ( lang . replace ( ' ' , '_' ) . replace ( '#' , 'sharp' ) ) ;
138+ } ) ;
139+ }
140+
141+ getLangSample ( lang : string ) {
142+ fetch ( `/assets/lang_samples/${ lang } .txt` ) . then ( async response => {
143+ if ( response . ok ) {
144+ this . code = await response . text ( ) ;
145+ } else {
146+ this . code = '' ;
147+ }
148+ } ) ;
149+ }
121150}
0 commit comments