Fix value, value highlighting in editor
This commit is contained in:
@@ -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<string>();
|
||||
selectedMessage = input.required<ProtoMessage>();
|
||||
|
||||
protected values = computed(() => {
|
||||
const message = this.selectedMessage();
|
||||
return message.values.map((value) => null);
|
||||
});
|
||||
protected values = signal<any>(undefined);
|
||||
|
||||
private code = viewChild<ElementRef<HTMLElement>>('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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user