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';
|
|
|
|
import { CdkDrag, CdkDragDrop, CdkDropList, moveItemInArray } from '@angular/cdk/drag-drop';
|
2025-05-27 21:10:45 -05:00
|
|
|
import { Subject, takeUntil } from 'rxjs';
|
|
|
|
|
2025-06-05 00:07:21 -05:00
|
|
|
import { EditorsService } from '../../common/services/editor/editors.service';
|
2025-05-27 21:10:45 -05:00
|
|
|
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 editorsService: EditorsService = inject(EditorsService);
|
|
|
|
private tabsService: TabsService = inject(TabsService);
|
|
|
|
private changeDetectorRef: ChangeDetectorRef = inject(ChangeDetectorRef);
|
2025-05-27 21:10:45 -05:00
|
|
|
|
|
|
|
activeTab!: string;
|
2025-06-20 23:52:22 -05:00
|
|
|
tabs: any[] = [];
|
2025-06-05 00:07:21 -05:00
|
|
|
newIndex: number = -1;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public ngAfterViewInit(): void {
|
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-05 00:07:21 -05:00
|
|
|
ngOnDestroy(): void {
|
2025-05-27 21:10:45 -05:00
|
|
|
this.unsubscribe.next();
|
|
|
|
this.unsubscribe.complete();
|
|
|
|
}
|
|
|
|
|
2025-06-05 00:07:21 -05:00
|
|
|
private createTab(title: string, uuid: string, path: string): void {
|
2025-06-18 22:35:28 -05:00
|
|
|
this.tabs.push({title: title, uuid: uuid, path: path});
|
2025-06-15 17:47:11 -05:00
|
|
|
this.changeDetectorRef.detectChanges();
|
2025-06-05 00:07:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
handleAction(event: any): void {
|
|
|
|
let target = event.target;
|
|
|
|
|
|
|
|
if ( target.classList.contains("tab") ) {
|
2025-06-18 22:35:28 -05:00
|
|
|
this.sendEditorsServiceAMessage(
|
|
|
|
"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-06-18 22:35:28 -05:00
|
|
|
this.sendEditorsServiceAMessage(
|
|
|
|
"set-tab-to-editor",
|
2025-06-05 00:07:21 -05:00
|
|
|
event.srcElement.parentElement.getAttribute("title")
|
|
|
|
);
|
|
|
|
} else if ( target.classList.contains("close-button") ) {
|
|
|
|
this.closeTab(
|
|
|
|
event.srcElement.parentElement.getAttribute("title")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
closeTab(fpath: string): void {
|
2025-06-18 22:35:28 -05:00
|
|
|
this.sendEditorsServiceAMessage("close-tab", fpath);
|
2025-06-05 00:07:21 -05:00
|
|
|
|
|
|
|
for (let i = 0; i < this.tabs.length; i++) {
|
|
|
|
if (this.tabs[i].path == fpath) {
|
|
|
|
this.tabs.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
moved(event: any): void {
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
|
|
|
for (let i = 0; i < this.tabs.length; i++) {
|
|
|
|
if (this.tabs[i].path == fpath) {
|
|
|
|
this.newIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dropped(event: CdkDragDrop<any>): void {
|
|
|
|
if (this.newIndex == -1) return;
|
2025-06-02 00:10:56 -05:00
|
|
|
|
2025-06-05 00:07:21 -05:00
|
|
|
moveItemInArray(this.tabs, event.previousIndex, this.newIndex);
|
|
|
|
this.newIndex = -1;
|
2025-05-27 21:10:45 -05:00
|
|
|
|
2025-06-05 00:07:21 -05:00
|
|
|
// event.currentIndex not updating for some reason...
|
|
|
|
// moveItemInArray(this.tabs, event.previousIndex, event.currentIndex);
|
2025-05-27 21:10:45 -05:00
|
|
|
}
|
|
|
|
|
2025-06-18 22:35:28 -05:00
|
|
|
|
|
|
|
private sendEditorsServiceAMessage(action: string, fpath: string) {
|
|
|
|
let message = new ServiceMessage();
|
|
|
|
message.action = action;
|
|
|
|
message.filePath = fpath;
|
|
|
|
|
|
|
|
this.editorsService.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2025-06-12 02:45:17 -05:00
|
|
|
}
|