Fix value, value highlighting in editor

This commit is contained in:
2024-07-05 19:56:32 +09:30
parent a22b6943aa
commit 38b0b51d40
4 changed files with 52 additions and 16 deletions

View File

@@ -1,12 +1,13 @@
<h2>{{ selectedMessage().name }}</h2> <h2>{{ selectedMessage().name }}</h2>
<div> <div>
@for (item of selectedMessage().values; track $index) { @if(values()) { @for (item of selectedMessage().values; track $index) {
<app-proto-field <app-proto-field
[label]="item.name" [label]="item.name"
[configuration]="item.configuration" [configuration]="item.configuration"
[(value)]="values()[$index]" [value]="values()[item.name]"
(valueChange)="updateValue(item.name, $event)"
></app-proto-field> ></app-proto-field>
} } }
</div> </div>
<pre><code class="language-json" #code>{{fileContents()}}</code></pre> <pre><code #code></code></pre>

View File

@@ -2,6 +2,7 @@ import { CommonModule } from '@angular/common';
import { import {
Component, Component,
ElementRef, ElementRef,
SecurityContext,
computed, computed,
effect, effect,
input, input,
@@ -11,6 +12,7 @@ import {
import hljs from 'highlight.js/lib/core'; import hljs from 'highlight.js/lib/core';
import { ProtoMessage } from '../model/proto-message.model'; import { ProtoMessage } from '../model/proto-message.model';
import { ProtoFieldComponent } from '../proto-field/proto-field.component'; import { ProtoFieldComponent } from '../proto-field/proto-field.component';
import { DomSanitizer } from '@angular/platform-browser';
@Component({ @Component({
selector: 'app-editor', selector: 'app-editor',
@@ -24,19 +26,44 @@ export class EditorComponent {
fileContents = input<string>(); fileContents = input<string>();
selectedMessage = input.required<ProtoMessage>(); selectedMessage = input.required<ProtoMessage>();
protected values = computed(() => { protected values = signal<any>(undefined);
const message = this.selectedMessage();
return message.values.map((value) => null);
});
private code = viewChild<ElementRef<HTMLElement>>('code'); private code = viewChild<ElementRef<HTMLElement>>('code');
constructor() { constructor(private sanitizer: DomSanitizer) {
effect(() => { effect(() => {
const element = this.code()?.nativeElement; const element = this.code()?.nativeElement;
if (element) { 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);
} }
} }

View File

@@ -28,6 +28,7 @@ import { ProtoFieldComponent } from '../proto-field/proto-field.component';
<app-proto-field <app-proto-field
[configuration]="configuration().subConfiguration" [configuration]="configuration().subConfiguration"
[value]="value" [value]="value"
(valueChange)="updateValue($index, $event)"
></app-proto-field> ></app-proto-field>
<button mat-icon-button (click)="remove($index)"> <button mat-icon-button (click)="remove($index)">
<mat-icon>remove</mat-icon> <mat-icon>remove</mat-icon>
@@ -44,18 +45,25 @@ export class ListFieldComponent {
values = model<any[]>(); values = model<any[]>();
add() { add() {
const newValues = this.values(); const existingValues = this.values();
if (newValues) { if (existingValues) {
newValues.push(null); const newValues = [...existingValues];
newValues.push(undefined);
this.values.set(newValues); this.values.set(newValues);
} else { } else {
this.values.set([null]); this.values.set([undefined]);
} }
} }
remove(index: number) { remove(index: number) {
const newValues = this.values(); const newValues = [...this.values()!];
newValues?.splice(index, 1); newValues.splice(index, 1);
this.values.set(newValues); this.values.set(newValues);
} }
updateValue(index: number, value: any) {
let existing = [...this.values()!];
existing[index] = value;
this.values.set(existing);
}
} }