Add file save
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
<mat-toolbar data-tauri-drag-region color="secondary">
|
<mat-toolbar data-tauri-drag-region color="secondary">
|
||||||
<h1>BufPiv</h1>
|
<h1>BufPiv</h1>
|
||||||
|
<span>{{ selectedFile()?.name }}</span>
|
||||||
<span>
|
<span>
|
||||||
<button
|
<button
|
||||||
mat-icon-button
|
mat-icon-button
|
||||||
@@ -47,7 +48,7 @@
|
|||||||
<app-editor
|
<app-editor
|
||||||
[selectedMessage]="selectedMessage()!"
|
[selectedMessage]="selectedMessage()!"
|
||||||
[showRaw]="previewOpen()"
|
[showRaw]="previewOpen()"
|
||||||
[selectedFile]="selectedFile()"
|
[selectedFile]="selectedFilePath()"
|
||||||
></app-editor>
|
></app-editor>
|
||||||
}
|
}
|
||||||
</mat-sidenav-content>
|
</mat-sidenav-content>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
|
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { Component, signal } from '@angular/core';
|
import { Component, computed, signal } from '@angular/core';
|
||||||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
||||||
import { MatButtonModule } from '@angular/material/button';
|
import { MatButtonModule } from '@angular/material/button';
|
||||||
import { MatIconModule } from '@angular/material/icon';
|
import { MatIconModule } from '@angular/material/icon';
|
||||||
@@ -36,7 +36,7 @@ const mobileBreakpoints = [Breakpoints.Handset, Breakpoints.TabletPortrait];
|
|||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
protected selectedFile = signal<string | undefined>(undefined);
|
protected selectedFile = signal<FileOrFolder | undefined>(undefined);
|
||||||
protected selectedMessage = signal<ProtoMessage | undefined>(undefined);
|
protected selectedMessage = signal<ProtoMessage | undefined>(undefined);
|
||||||
protected rightSideOpen = signal(true);
|
protected rightSideOpen = signal(true);
|
||||||
protected leftSideOpen = signal(true);
|
protected leftSideOpen = signal(true);
|
||||||
@@ -46,6 +46,8 @@ export class AppComponent {
|
|||||||
this.breakpointObserver.isMatched(mobileBreakpoints)
|
this.breakpointObserver.isMatched(mobileBreakpoints)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
protected selectedFilePath = computed(() => this.selectedFile()?.path);
|
||||||
|
|
||||||
constructor(private breakpointObserver: BreakpointObserver) {
|
constructor(private breakpointObserver: BreakpointObserver) {
|
||||||
breakpointObserver
|
breakpointObserver
|
||||||
.observe(mobileBreakpoints)
|
.observe(mobileBreakpoints)
|
||||||
@@ -55,11 +57,12 @@ export class AppComponent {
|
|||||||
|
|
||||||
protected selectMessage(message: ProtoMessage) {
|
protected selectMessage(message: ProtoMessage) {
|
||||||
this.selectedMessage.set(message);
|
this.selectedMessage.set(message);
|
||||||
|
this.selectedFile.set(undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async fileSelected(file: FileOrFolder) {
|
protected async fileSelected(file: FileOrFolder) {
|
||||||
try {
|
try {
|
||||||
this.selectedFile.set(file.path);
|
this.selectedFile.set(file);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
alert(
|
alert(
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
<h2>{{ selectedMessage().name }}</h2>
|
<div class="proto-title">
|
||||||
<div>
|
<h2>{{ selectedMessage().name }}</h2>
|
||||||
|
<button mat-icon-button matTooltip="Save configuration" (click)="save()">
|
||||||
|
<mat-icon>save</mat-icon>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="editor-items">
|
||||||
@if(values()) { @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"
|
||||||
|
|||||||
@@ -5,15 +5,20 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
pre,
|
pre,
|
||||||
div {
|
.editor-items {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
div {
|
.editor-items {
|
||||||
padding: 16px;
|
padding: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.actions {
|
.actions {
|
||||||
flex: 0 0 auto;
|
flex: 0 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.proto-title {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,21 +11,30 @@ import {
|
|||||||
import { MatButtonModule } from '@angular/material/button';
|
import { MatButtonModule } from '@angular/material/button';
|
||||||
import { MatSnackBar } from '@angular/material/snack-bar';
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||||
import { DomSanitizer } from '@angular/platform-browser';
|
import { DomSanitizer } from '@angular/platform-browser';
|
||||||
import { readTextFile } from '@tauri-apps/api/fs';
|
import { readTextFile, writeTextFile } from '@tauri-apps/api/fs';
|
||||||
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 { MatIconModule } from '@angular/material/icon';
|
||||||
|
import { MatTooltipModule } from '@angular/material/tooltip';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-editor',
|
selector: 'app-editor',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
imports: [CommonModule, ProtoFieldComponent, MatButtonModule],
|
imports: [
|
||||||
|
CommonModule,
|
||||||
|
ProtoFieldComponent,
|
||||||
|
MatButtonModule,
|
||||||
|
MatIconModule,
|
||||||
|
MatTooltipModule,
|
||||||
|
],
|
||||||
templateUrl: './editor.component.html',
|
templateUrl: './editor.component.html',
|
||||||
styleUrl: './editor.component.scss',
|
styleUrl: './editor.component.scss',
|
||||||
})
|
})
|
||||||
export class EditorComponent {
|
export class EditorComponent {
|
||||||
selectedFile = input<string>();
|
selectedFile = input<string>();
|
||||||
selectedMessage = input.required<ProtoMessage>();
|
selectedMessage = input.required<ProtoMessage>();
|
||||||
|
indentSize = input<number>(2);
|
||||||
|
|
||||||
showRaw = input<boolean>(true);
|
showRaw = input<boolean>(true);
|
||||||
|
|
||||||
@@ -40,7 +49,11 @@ export class EditorComponent {
|
|||||||
if (!this.values()) {
|
if (!this.values()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const json = JSON.stringify(this.values(), undefined, 2);
|
const json = JSON.stringify(
|
||||||
|
this.values(),
|
||||||
|
undefined,
|
||||||
|
this.indentSize()
|
||||||
|
);
|
||||||
const highlighted = hljs.highlightAuto(json, ['json']);
|
const highlighted = hljs.highlightAuto(json, ['json']);
|
||||||
const sanitized = sanitizer.sanitize(
|
const sanitized = sanitizer.sanitize(
|
||||||
SecurityContext.HTML,
|
SecurityContext.HTML,
|
||||||
@@ -94,10 +107,25 @@ export class EditorComponent {
|
|||||||
|
|
||||||
protected async copyToClipboard() {
|
protected async copyToClipboard() {
|
||||||
await navigator.clipboard.writeText(
|
await navigator.clipboard.writeText(
|
||||||
JSON.stringify(this.values(), undefined, 2)
|
JSON.stringify(this.values(), undefined, this.indentSize())
|
||||||
);
|
);
|
||||||
this.snackBar.open('Successully copied to clipboard', 'Close', {
|
this.snackBar.open('Successully copied to clipboard', 'Close', {
|
||||||
duration: 2000,
|
duration: 2000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async save() {
|
||||||
|
const selectedFile = this.selectedFile();
|
||||||
|
if (!selectedFile) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await writeTextFile(
|
||||||
|
selectedFile,
|
||||||
|
JSON.stringify(this.values(), undefined, this.indentSize())
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to write to file ${this.selectedFile()}', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ html {
|
|||||||
@include mat.list-theme(theme.$rose-theme);
|
@include mat.list-theme(theme.$rose-theme);
|
||||||
@include mat.select-theme(theme.$rose-theme);
|
@include mat.select-theme(theme.$rose-theme);
|
||||||
@include mat.snack-bar-theme(theme.$rose-theme);
|
@include mat.snack-bar-theme(theme.$rose-theme);
|
||||||
|
@include mat.tooltip-theme(theme.$rose-theme);
|
||||||
@include custom-colours(theme.$rose-theme);
|
@include custom-colours(theme.$rose-theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user