2025-06-20 23:52:22 -05:00
|
|
|
import { Component, ChangeDetectorRef, inject } from '@angular/core';
|
2025-06-05 00:07:21 -05:00
|
|
|
import { CommonModule } from '@angular/common';
|
2025-07-02 21:26:24 -05:00
|
|
|
import { CdkDrag, CdkDragDrop, CdkDropList } from '@angular/cdk/drag-drop';
|
2025-05-27 21:10:45 -05:00
|
|
|
import { Subject, takeUntil } from 'rxjs';
|
|
|
|
|
|
|
|
import { TabsService } from '../../common/services/editor/tabs/tabs.service';
|
|
|
|
|
|
|
|
import { ServiceMessage } from '../../common/types/service-message.type';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'tabs',
|
|
|
|
standalone: true,
|
|
|
|
imports: [
|
2025-06-05 00:07:21 -05:00
|
|
|
CommonModule,
|
|
|
|
CdkDropList,
|
|
|
|
CdkDrag,
|
2025-05-27 21:10:45 -05:00
|
|
|
],
|
|
|
|
templateUrl: './tabs.component.html',
|
|
|
|
styleUrl: './tabs.component.css',
|
|
|
|
host: {
|
2025-06-15 00:43:33 -05:00
|
|
|
'class': 'tabs scroller'
|
2025-05-27 21:10:45 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
export class TabsComponent {
|
2025-06-20 23:52:22 -05:00
|
|
|
private unsubscribe: Subject<void> = new Subject();
|
|
|
|
|
|
|
|
private tabsService: TabsService = inject(TabsService);
|
|
|
|
private changeDetectorRef: ChangeDetectorRef = inject(ChangeDetectorRef);
|
2025-05-27 21:10:45 -05:00
|
|
|
|
2025-07-02 21:26:24 -05:00
|
|
|
tabs: any[] = this.tabsService.tabs;
|
2025-05-27 21:10:45 -05:00
|
|
|
|
|
|
|
|
2025-06-20 23:52:22 -05:00
|
|
|
constructor() {
|
2025-05-27 21:10:45 -05:00
|
|
|
}
|
|
|
|
|
2025-06-21 19:49:58 -05:00
|
|
|
private ngAfterViewInit(): void {
|
|
|
|
this.loadSubscribers();
|
|
|
|
}
|
|
|
|
|
|
|
|
private ngOnDestroy(): void {
|
|
|
|
this.unsubscribe.next();
|
|
|
|
this.unsubscribe.complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadSubscribers() {
|
2025-06-18 22:35:28 -05:00
|
|
|
this.tabsService.getMessage$().pipe(
|
2025-05-27 21:10:45 -05:00
|
|
|
takeUntil(this.unsubscribe)
|
2025-06-20 00:50:43 -05:00
|
|
|
).subscribe((message: ServiceMessage) => {
|
|
|
|
if (message.action === "create-tab") {
|
|
|
|
this.createTab(message.fileName, message.fileUUID, message.filePath);
|
|
|
|
} else if (message.action === "file-changed") {
|
|
|
|
let elm = document.querySelectorAll(`[title="${message.filePath}"]`)[1];
|
|
|
|
elm.classList.add("file-changed");
|
|
|
|
elm.classList.remove("file-deleted");
|
|
|
|
} else if (message.action === "file-deleted") {
|
|
|
|
let elm = document.querySelectorAll(`[title="${message.filePath}"]`)[1];
|
|
|
|
elm.classList.add("file-deleted");
|
|
|
|
elm.classList.remove("file-changed");
|
|
|
|
} else if (message.action === "file-saved") {
|
|
|
|
let elm = document.querySelectorAll(`[title="${message.filePath}"]`)[1];
|
|
|
|
elm.classList.remove("file-deleted");
|
|
|
|
elm.classList.remove("file-changed");
|
2025-06-15 00:43:33 -05:00
|
|
|
}
|
2025-05-27 21:10:45 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-06-21 19:49:58 -05:00
|
|
|
protected handleAction(event: any): void {
|
2025-06-05 00:07:21 -05:00
|
|
|
let target = event.target;
|
|
|
|
|
|
|
|
if ( target.classList.contains("tab") ) {
|
2025-07-02 21:26:24 -05:00
|
|
|
this.tabsService.sendEditorsServiceAMessage(
|
2025-06-18 22:35:28 -05:00
|
|
|
"set-tab-to-editor",
|
2025-06-05 00:07:21 -05:00
|
|
|
event.srcElement.getAttribute("title")
|
|
|
|
);
|
2025-06-18 22:35:28 -05:00
|
|
|
|
2025-06-05 00:07:21 -05:00
|
|
|
} else if ( target.classList.contains("title") ) {
|
2025-07-02 21:26:24 -05:00
|
|
|
this.tabsService.sendEditorsServiceAMessage(
|
2025-06-18 22:35:28 -05:00
|
|
|
"set-tab-to-editor",
|
2025-06-05 00:07:21 -05:00
|
|
|
event.srcElement.parentElement.getAttribute("title")
|
|
|
|
);
|
|
|
|
} else if ( target.classList.contains("close-button") ) {
|
2025-07-02 21:26:24 -05:00
|
|
|
this.tabsService.closeTab(
|
2025-06-05 00:07:21 -05:00
|
|
|
event.srcElement.parentElement.getAttribute("title")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-06-21 19:49:58 -05:00
|
|
|
public createTab(title: string, uuid: string, path: string): void {
|
2025-07-02 21:26:24 -05:00
|
|
|
this.tabsService.push({title: title, uuid: uuid, path: path});
|
2025-06-21 19:49:58 -05:00
|
|
|
this.changeDetectorRef.detectChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
private moved(event: any): void {
|
2025-06-05 00:07:21 -05:00
|
|
|
let target = event.event.target;
|
|
|
|
let fpath = "";
|
|
|
|
|
2025-06-18 22:35:28 -05:00
|
|
|
if ( target.classList.contains("title") ||
|
|
|
|
target.classList.contains("close-button")
|
|
|
|
) {
|
2025-06-05 00:07:21 -05:00
|
|
|
fpath = target.parentElement.getAttribute("title")
|
|
|
|
} else (
|
|
|
|
fpath = target.getAttribute("title")
|
|
|
|
)
|
|
|
|
|
2025-07-02 21:26:24 -05:00
|
|
|
this.tabsService.setNewTargetIndex(fpath);
|
2025-06-05 00:07:21 -05:00
|
|
|
}
|
|
|
|
|
2025-06-21 19:49:58 -05:00
|
|
|
protected dropped(event: CdkDragDrop<any>): void {
|
2025-07-02 21:26:24 -05:00
|
|
|
this.tabsService.move(event.previousIndex);
|
2025-06-18 22:35:28 -05:00
|
|
|
}
|
|
|
|
|
2025-06-12 02:45:17 -05:00
|
|
|
}
|