Add button to open files directly, fix tab closing
All checks were successful
build / build (push) Successful in 1m36s

This commit is contained in:
2025-06-16 11:07:06 +09:30
parent b9c574eda4
commit 1cc0585015
2 changed files with 62 additions and 23 deletions

View File

@@ -1,4 +1,3 @@
<!-- Panel menu for files/sheets in workspace: https://primeng.org/panelmenu -->
<p-toolbar> <p-toolbar>
<ng-template #start></ng-template> <ng-template #start></ng-template>
<ng-template #center> <ng-template #center>
@@ -21,7 +20,7 @@
<p-splitter layout="vertical" [panelSizes]="[70, 30]"> <p-splitter layout="vertical" [panelSizes]="[70, 30]">
<ng-template #panel> <ng-template #panel>
<div <div
class="flex flex-col w-full" class="flex flex-col w-full justify-center"
(dragenter)="dragEnter($event)" (dragenter)="dragEnter($event)"
(dragleave)="dragLeave($event)" (dragleave)="dragLeave($event)"
(dragover)="$event.preventDefault()" (dragover)="$event.preventDefault()"
@@ -51,6 +50,14 @@
class="flex w-full flex-1" class="flex w-full flex-1"
[file]="selectedFile()" [file]="selectedFile()"
></app-file-viewer> ></app-file-viewer>
} @else {
<p-button
label="Open files"
class="self-center"
variant="outlined"
severity="secondary"
(onClick)="fileSelector.click()"
></p-button>
} }
</div> </div>
</ng-template> </ng-template>
@@ -65,3 +72,11 @@
<div class="flex items-center justify-center h-full">Panel 4</div> <div class="flex items-center justify-center h-full">Panel 4</div>
</ng-template> </ng-template>
</p-splitter> </p-splitter>
<input
#fileSelector
multiple
class="hidden"
type="file"
accept="text/csv"
(change)="fileChange($event)"
/>

View File

@@ -1,4 +1,10 @@
import { Component, effect, signal, viewChild } from '@angular/core'; import {
Component,
ElementRef,
effect,
signal,
viewChild,
} from '@angular/core';
import { ButtonModule } from 'primeng/button'; import { ButtonModule } from 'primeng/button';
import { SplitterModule } from 'primeng/splitter'; import { SplitterModule } from 'primeng/splitter';
import { TabsModule } from 'primeng/tabs'; import { TabsModule } from 'primeng/tabs';
@@ -33,28 +39,13 @@ export class AppComponent {
protected selectedTab = signal(0); protected selectedTab = signal(0);
protected dragging = signal(false); protected dragging = signal(false);
private fileInput = viewChild<ElementRef<HTMLInputElement>>('fileSelector');
constructor() { constructor() {
effect(() => { effect(() => {
const selectedFile = this.selectedFile(); const selectedFile = this.selectedFile();
if (selectedFile) { if (selectedFile) {
if ( this.addFile(selectedFile);
this.tabs().find(
(tab) =>
tab.webkitRelativePath === selectedFile.webkitRelativePath &&
tab.name === selectedFile.name,
)
) {
this.selectedTab.set(
this.tabs().findIndex(
(tab) =>
tab.webkitRelativePath === selectedFile.webkitRelativePath &&
tab.name === selectedFile.name,
),
);
} else {
this.tabs.update((tabs) => [...tabs, selectedFile]);
this.selectedTab.set(this.tabs().length - 1);
}
} }
}); });
@@ -72,7 +63,7 @@ export class AppComponent {
protected removeTab(index: number) { protected removeTab(index: number) {
this.tabs.update((tabs) => { this.tabs.update((tabs) => {
const copy = tabs.slice(); const copy = tabs.slice();
copy.splice(index); copy.splice(index, 1);
return copy; return copy;
}); });
if (this.selectedTab() === index) { if (this.selectedTab() === index) {
@@ -94,7 +85,7 @@ export class AppComponent {
if (files) { if (files) {
for (const file of files) { for (const file of files) {
if (file.type === 'text/csv') { if (file.type === 'text/csv') {
this.selectedFile.set(file); this.addFile(file);
} }
} }
} }
@@ -118,4 +109,37 @@ export class AppComponent {
event.preventDefault(); event.preventDefault();
this.dragging.set(false); this.dragging.set(false);
} }
protected fileChange(event: Event) {
const files = this.fileInput()?.nativeElement.files;
if (files) {
for (const file of files) {
if (file.type === 'text/csv') {
this.addFile(file);
}
}
this.selectedTab.set(this.tabs().length - 1);
}
}
private addFile(file: File) {
if (
this.tabs().find(
(tab) =>
tab.webkitRelativePath === file.webkitRelativePath &&
tab.name === file.name,
)
) {
this.selectedTab.set(
this.tabs().findIndex(
(tab) =>
tab.webkitRelativePath === file.webkitRelativePath &&
tab.name === file.name,
),
);
} else {
this.tabs.update((tabs) => [...tabs, file]);
this.selectedTab.set(this.tabs().length - 1);
}
}
} }