|
3 | 3 | * Licensed under the MIT License. See LICENSE in the package root for license information. |
4 | 4 | * ------------------------------------------------------------------------------------------ */ |
5 | 5 |
|
6 | | -import { AfterViewInit, Component, signal } from '@angular/core'; |
| 6 | +import { AfterViewInit, Component, inject, signal } from '@angular/core'; |
7 | 7 | import { WrapperConfig } from 'monaco-editor-wrapper'; |
8 | | -import { MonacoAngularWrapperComponent } from '../monaco-angular-wrapper.component'; |
| 8 | +import { MonacoAngularWrapperComponent } from '../monaco-angular-wrapper/monaco-angular-wrapper.component'; |
9 | 9 | import { buildJsonClientUserConfig } from 'monaco-languageclient-examples/json-client'; |
| 10 | +import { SaveCodeService } from '../save-code.service'; |
| 11 | +import { firstValueFrom } from 'rxjs'; |
10 | 12 | @Component({ |
11 | 13 | selector: 'app-root', |
12 | | - templateUrl: './app.component.html', |
13 | | - styleUrls: ['./app.component.css'], |
| 14 | + templateUrl: "./app.component.html", |
| 15 | + styleUrls: ["./app.component.scss"], |
14 | 16 | standalone: true, |
15 | 17 | imports: [MonacoAngularWrapperComponent], |
16 | 18 | }) |
17 | 19 | export class AppComponent implements AfterViewInit { |
18 | | - wrapperConfig: WrapperConfig | undefined; |
19 | | - title = 'angular-client'; |
| 20 | + private saveCodeService = inject(SaveCodeService); |
| 21 | + wrapperConfig = signal<WrapperConfig | undefined>(undefined); // this can be updated at runtime |
20 | 22 |
|
| 23 | + title = 'angular demo for saving code'; |
| 24 | + editorId = 'monaco-editor-root'; // this can be parameterized or it can be in a loop to support multiple editors |
| 25 | + editorInlineStyle = signal('height: 50vh;'); |
21 | 26 | readonly codeText = signal(''); |
22 | 27 |
|
23 | 28 | async ngAfterViewInit(): Promise<void> { |
24 | | - const config = buildJsonClientUserConfig({ |
25 | | - htmlContainer: document.getElementById('monaco-editor-root')!, |
26 | | - }); |
27 | | - this.wrapperConfig = config; |
| 29 | + const editorDom = document.getElementById(this.editorId); |
| 30 | + if (editorDom) { |
| 31 | + const config = buildJsonClientUserConfig({ |
| 32 | + htmlContainer: editorDom, |
| 33 | + }); |
| 34 | + this.wrapperConfig.set(config); |
| 35 | + } |
28 | 36 | } |
29 | 37 |
|
30 | 38 | onTextChanged(text: string) { |
31 | 39 | this.codeText.set(text); |
32 | 40 | } |
| 41 | + |
| 42 | + save = async () => { |
| 43 | + try { |
| 44 | + const response = await firstValueFrom( |
| 45 | + this.saveCodeService.saveCode(this.codeText()) |
| 46 | + ); |
| 47 | + alert('Code saved:' + JSON.stringify(response)); |
| 48 | + } catch (error) { |
| 49 | + console.error('Error saving code:', error); |
| 50 | + } |
| 51 | + }; |
33 | 52 | } |
0 commit comments