Skip to content

Commit 9b6370a

Browse files
committed
feat(angular): add HttpClient to the host app to demo the saving of the editing code text to API
1 parent f0f65d0 commit 9b6370a

File tree

6 files changed

+48
-21
lines changed

6 files changed

+48
-21
lines changed

verify/angular/src/app/app.component.css

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
<h2>Monaco Language Client Angular Client Two way data binding Example</h2>
2-
<div>
3-
@if(wrapperConfig) {
1+
<h2>Angular Monaco Editor demo with saving code to a mock HTTP server </h2>
2+
<div class="editor-container">
43
<monaco-angular-wrapper
5-
[wrapperConfig]="wrapperConfig"
4+
[wrapperConfig]="wrapperConfig()"
5+
[monacoEditorId]="editorId"
6+
[editorInlineStyle]="editorInlineStyle()"
67
(onTextChanged)="onTextChanged($event)"
78
></monaco-angular-wrapper>
89

9-
}
10-
11-
{{ codeText() }}
10+
<button class="save-button" (click)="save()">Save</button>
1211
</div>

verify/angular/src/app/app.component.scss

Whitespace-only changes.

verify/angular/src/app/app.component.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,50 @@
33
* Licensed under the MIT License. See LICENSE in the package root for license information.
44
* ------------------------------------------------------------------------------------------ */
55

6-
import { AfterViewInit, Component, signal } from '@angular/core';
6+
import { AfterViewInit, Component, inject, signal } from '@angular/core';
77
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';
99
import { buildJsonClientUserConfig } from 'monaco-languageclient-examples/json-client';
10+
import { SaveCodeService } from '../save-code.service';
11+
import { firstValueFrom } from 'rxjs';
1012
@Component({
1113
selector: 'app-root',
12-
templateUrl: './app.component.html',
13-
styleUrls: ['./app.component.css'],
14+
templateUrl: "./app.component.html",
15+
styleUrls: ["./app.component.scss"],
1416
standalone: true,
1517
imports: [MonacoAngularWrapperComponent],
1618
})
1719
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
2022

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;');
2126
readonly codeText = signal('');
2227

2328
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+
}
2836
}
2937

3038
onTextChanged(text: string) {
3139
this.codeText.set(text);
3240
}
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+
};
3352
}

verify/angular/src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
* ------------------------------------------------------------------------------------------ */
55

66
import { bootstrapApplication } from '@angular/platform-browser';
7+
import { provideHttpClient } from '@angular/common/http';
78
import { AppComponent } from './app/app.component';
8-
bootstrapApplication(AppComponent);
9+
bootstrapApplication(AppComponent, {
10+
providers: [provideHttpClient()],
11+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { inject, Injectable } from '@angular/core';
2+
import { HttpClient } from '@angular/common/http';
3+
@Injectable({ providedIn: 'root' })
4+
export class SaveCodeService {
5+
private http = inject(HttpClient);
6+
saveCode(codeText: string) {
7+
return this.http.post('http://localhost:3003/save-code', { code: codeText });
8+
}
9+
}

0 commit comments

Comments
 (0)