Allow files in browser to be loaded, fix overflow on message list, show errors to user

This commit is contained in:
2024-08-17 15:01:10 +09:30
parent 3adbcfdc1c
commit dd6a4536c6
5 changed files with 47 additions and 19 deletions

View File

@@ -48,7 +48,7 @@
<app-editor <app-editor
[selectedMessage]="selectedMessage()!" [selectedMessage]="selectedMessage()!"
[showRaw]="previewOpen()" [showRaw]="previewOpen()"
[selectedFile]="selectedFilePath()" [selectedFile]="selectedFile()"
></app-editor> ></app-editor>
} }
</mat-sidenav-content> </mat-sidenav-content>

View File

@@ -46,8 +46,6 @@ 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)
@@ -61,13 +59,6 @@ export class AppComponent {
} }
protected async fileSelected(file: FileOrFolder) { protected async fileSelected(file: FileOrFolder) {
try { this.selectedFile.set(file);
this.selectedFile.set(file);
} catch (err) {
console.error(err);
alert(
'Failed to read selected file, please ensure you have appropriate permissions and try again.'
);
}
} }
} }

View File

@@ -21,6 +21,7 @@ import { save } from '@tauri-apps/api/dialog';
import { MatButtonToggleModule } from '@angular/material/button-toggle'; import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MonacoEditorModule } from 'ngx-monaco-editor-v2'; import { MonacoEditorModule } from 'ngx-monaco-editor-v2';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { FileOrFolder } from '../file-tree/file-tree.component';
declare const __TAURI__: any; declare const __TAURI__: any;
@@ -42,7 +43,7 @@ type PreviewType = 'raw' | 'edit' | 'diff';
styleUrl: './editor.component.scss', styleUrl: './editor.component.scss',
}) })
export class EditorComponent { export class EditorComponent {
selectedFile = input<string>(); selectedFile = input<FileOrFolder>();
selectedMessage = input.required<ProtoMessage>(); selectedMessage = input.required<ProtoMessage>();
indentSize = input<number>(2); indentSize = input<number>(2);
showRaw = input<boolean>(true); showRaw = input<boolean>(true);
@@ -111,10 +112,27 @@ export class EditorComponent {
effect(async () => { effect(async () => {
const selectedFile = this.selectedFile(); const selectedFile = this.selectedFile();
if (selectedFile) { let filePath: string | null = null;
const fileContents = await readTextFile(selectedFile); let fileContents: string | null = null;
try {
if (selectedFile?.browserFile) {
fileContents = await selectedFile.browserFile.text();
filePath = selectedFile.browserFile.webkitRelativePath;
} else if (selectedFile?.path) {
fileContents = await readTextFile(selectedFile.path);
filePath = selectedFile.path;
}
} catch (err) {
console.error(err);
this.snackBar.open(
'Failed to read file contents, please check you have the correct permissions and the file still exists',
'Dismiss',
{ duration: 5000 }
);
}
if (filePath && fileContents) {
this.currentFileContents.set(fileContents); this.currentFileContents.set(fileContents);
this.updateValuesFromText(fileContents, selectedFile); this.updateValuesFromText(fileContents, filePath);
} }
}); });
} }
@@ -126,6 +144,11 @@ export class EditorComponent {
} catch (err) { } catch (err) {
if (selectedFile) { if (selectedFile) {
console.error(`Failed to parse contents of file ${selectedFile}`, err); console.error(`Failed to parse contents of file ${selectedFile}`, err);
this.snackBar.open(
'Failed to parse file contents as JSON, please ensure this is a valid JSON formatted file',
'Dismiss',
{ duration: 5000 }
);
} }
} }
} }
@@ -149,8 +172,8 @@ export class EditorComponent {
const serialised = this.serialisedValues(); const serialised = this.serialisedValues();
try { try {
// TODO: Tauri is bugged on mac atm, remove this when resolved: https://github.com/tauri-apps/tauri/issues/4633 // TODO: Tauri is bugged on mac atm, remove this when resolved: https://github.com/tauri-apps/tauri/issues/4633
if (selectedFile) { if (!selectedFile?.browserFile && selectedFile?.path) {
await writeTextFile(selectedFile, serialised); await writeTextFile(selectedFile.path, serialised);
} else { } else {
const filePath = await save({ const filePath = await save({
defaultPath: `${this.selectedMessage().name}.json`, defaultPath: `${this.selectedMessage().name}.json`,

View File

@@ -27,6 +27,7 @@ export interface FileOrFolder {
name: string; name: string;
children?: FileOrFolder[]; children?: FileOrFolder[];
path?: string; path?: string;
browserFile?: File;
} }
interface FileNode { interface FileNode {
@@ -159,15 +160,27 @@ export class FileTreeComponent implements OnInit, OnDestroy {
matchingChild = { matchingChild = {
isDirectory: true, isDirectory: true,
name: relativePath, name: relativePath,
path: file.webkitRelativePath,
children: [], children: [],
browserFile: file,
}; };
currentChildren?.push(matchingChild); currentChildren?.push(matchingChild);
} }
currentChildren = matchingChild.children; currentChildren = matchingChild.children;
} }
currentChildren?.push({ isDirectory: false, name: file.name }); currentChildren?.push({
isDirectory: false,
name: file.name,
path: file.webkitRelativePath,
browserFile: file,
});
} else { } else {
mappedFiles.push({ isDirectory: false, name: file.name }); mappedFiles.push({
isDirectory: false,
name: file.name,
path: file.webkitRelativePath,
browserFile: file,
});
} }
} }
this.recursiveSort(mappedFiles); this.recursiveSort(mappedFiles);

View File

@@ -5,6 +5,7 @@
height: 100%; height: 100%;
box-sizing: border-box; box-sizing: border-box;
transition: background-color 100ms linear; transition: background-color 100ms linear;
overflow: auto;
} }
:host.droppable * { :host.droppable * {