diff --git a/src/app/editor/editor.component.html b/src/app/editor/editor.component.html index f832d7f..82440b8 100644 --- a/src/app/editor/editor.component.html +++ b/src/app/editor/editor.component.html @@ -1,12 +1,13 @@

{{ selectedMessage().name }}

- @for (item of selectedMessage().values; track $index) { + @if(values()) { @for (item of selectedMessage().values; track $index) { - } + } }
-
{{fileContents()}}
+
diff --git a/src/app/editor/editor.component.ts b/src/app/editor/editor.component.ts index f3a9b6b..c91037e 100644 --- a/src/app/editor/editor.component.ts +++ b/src/app/editor/editor.component.ts @@ -2,6 +2,7 @@ import { CommonModule } from '@angular/common'; import { Component, ElementRef, + SecurityContext, computed, effect, input, @@ -11,6 +12,7 @@ import { import hljs from 'highlight.js/lib/core'; import { ProtoMessage } from '../model/proto-message.model'; import { ProtoFieldComponent } from '../proto-field/proto-field.component'; +import { DomSanitizer } from '@angular/platform-browser'; @Component({ selector: 'app-editor', @@ -24,19 +26,44 @@ export class EditorComponent { fileContents = input(); selectedMessage = input.required(); - protected values = computed(() => { - const message = this.selectedMessage(); - return message.values.map((value) => null); - }); + protected values = signal(undefined); private code = viewChild>('code'); - constructor() { + constructor(private sanitizer: DomSanitizer) { effect(() => { const element = this.code()?.nativeElement; if (element) { - hljs.highlightElement(element); + if (!this.values()) { + return; + } + const json = JSON.stringify(this.values(), undefined, 2); + const highlighted = hljs.highlightAuto(json, ['json']); + const sanitized = sanitizer.sanitize( + SecurityContext.HTML, + highlighted.value + ); + if (sanitized) { + element.innerHTML = sanitized; + } } }); + effect( + () => { + const message = this.selectedMessage(); + this.values.set( + Object.fromEntries( + message.values.map((value) => [[value.name, null]]) + ) + ); + }, + { allowSignalWrites: true } + ); + } + + protected updateValue(key: string, value: any) { + const existingValue = { ...this.values() }; + existingValue[key] = value; + this.values.set(existingValue); } } diff --git a/src/app/list-field/list-field.component.spec.ts b/src/app/list-field/list-field.component.spec.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/app/list-field/list-field.component.ts b/src/app/list-field/list-field.component.ts index 17f68e6..d9461f1 100644 --- a/src/app/list-field/list-field.component.ts +++ b/src/app/list-field/list-field.component.ts @@ -28,6 +28,7 @@ import { ProtoFieldComponent } from '../proto-field/proto-field.component';