Skip to content

Commit

Permalink
feat(angular): add HttpClient to the host app to demo the saving of t…
Browse files Browse the repository at this point in the history
…he editing code text to API
  • Loading branch information
ls-infra committed Oct 13, 2024
1 parent f0f65d0 commit 9b6370a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
3 changes: 0 additions & 3 deletions verify/angular/src/app/app.component.css

This file was deleted.

13 changes: 6 additions & 7 deletions verify/angular/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<h2>Monaco Language Client Angular Client Two way data binding Example</h2>
<div>
@if(wrapperConfig) {
<h2>Angular Monaco Editor demo with saving code to a mock HTTP server </h2>
<div class="editor-container">
<monaco-angular-wrapper
[wrapperConfig]="wrapperConfig"
[wrapperConfig]="wrapperConfig()"
[monacoEditorId]="editorId"
[editorInlineStyle]="editorInlineStyle()"
(onTextChanged)="onTextChanged($event)"
></monaco-angular-wrapper>

}

{{ codeText() }}
<button class="save-button" (click)="save()">Save</button>
</div>
Empty file.
39 changes: 29 additions & 10 deletions verify/angular/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,50 @@
* Licensed under the MIT License. See LICENSE in the package root for license information.
* ------------------------------------------------------------------------------------------ */

import { AfterViewInit, Component, signal } from '@angular/core';
import { AfterViewInit, Component, inject, signal } from '@angular/core';
import { WrapperConfig } from 'monaco-editor-wrapper';
import { MonacoAngularWrapperComponent } from '../monaco-angular-wrapper.component';
import { MonacoAngularWrapperComponent } from '../monaco-angular-wrapper/monaco-angular-wrapper.component';
import { buildJsonClientUserConfig } from 'monaco-languageclient-examples/json-client';
import { SaveCodeService } from '../save-code.service';
import { firstValueFrom } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"],
standalone: true,
imports: [MonacoAngularWrapperComponent],
})
export class AppComponent implements AfterViewInit {
wrapperConfig: WrapperConfig | undefined;
title = 'angular-client';
private saveCodeService = inject(SaveCodeService);
wrapperConfig = signal<WrapperConfig | undefined>(undefined); // this can be updated at runtime

title = 'angular demo for saving code';
editorId = 'monaco-editor-root'; // this can be parameterized or it can be in a loop to support multiple editors
editorInlineStyle = signal('height: 50vh;');
readonly codeText = signal('');

async ngAfterViewInit(): Promise<void> {
const config = buildJsonClientUserConfig({
htmlContainer: document.getElementById('monaco-editor-root')!,
});
this.wrapperConfig = config;
const editorDom = document.getElementById(this.editorId);
if (editorDom) {
const config = buildJsonClientUserConfig({
htmlContainer: editorDom,
});
this.wrapperConfig.set(config);
}
}

onTextChanged(text: string) {
this.codeText.set(text);
}

save = async () => {
try {
const response = await firstValueFrom(
this.saveCodeService.saveCode(this.codeText())
);
alert('Code saved:' + JSON.stringify(response));
} catch (error) {
console.error('Error saving code:', error);
}
};
}
5 changes: 4 additions & 1 deletion verify/angular/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
* ------------------------------------------------------------------------------------------ */

import { bootstrapApplication } from '@angular/platform-browser';
import { provideHttpClient } from '@angular/common/http';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent);
bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
});
9 changes: 9 additions & 0 deletions verify/angular/src/save-code.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class SaveCodeService {
private http = inject(HttpClient);
saveCode(codeText: string) {
return this.http.post('http://localhost:3003/save-code', { code: codeText });
}
}

0 comments on commit 9b6370a

Please sign in to comment.