Files
Newton-Editor/src/app/editor/tabs/tabs.component.ts

115 lines
3.7 KiB
TypeScript
Raw Normal View History

import { Component, ChangeDetectorRef, inject } from '@angular/core';
2025-06-05 00:07:21 -05:00
import { CommonModule } from '@angular/common';
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: {
'class': 'tabs scroller'
2025-05-27 21:10:45 -05:00
}
})
export class TabsComponent {
private unsubscribe: Subject<void> = new Subject();
private tabsService: TabsService = inject(TabsService);
private changeDetectorRef: ChangeDetectorRef = inject(ChangeDetectorRef);
2025-05-27 21:10:45 -05:00
tabs: any[] = this.tabsService.tabs;
2025-05-27 21:10:45 -05:00
constructor() {
2025-05-27 21:10:45 -05:00
}
private ngAfterViewInit(): void {
this.loadSubscribers();
}
private ngOnDestroy(): void {
this.unsubscribe.next();
this.unsubscribe.complete();
}
private loadSubscribers() {
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-05-27 21:10:45 -05:00
});
}
protected handleAction(event: any): void {
2025-06-05 00:07:21 -05:00
let target = event.target;
if ( target.classList.contains("tab") ) {
this.tabsService.sendEditorsServiceAMessage(
"set-tab-to-editor",
2025-06-05 00:07:21 -05:00
event.srcElement.getAttribute("title")
);
2025-06-05 00:07:21 -05:00
} else if ( target.classList.contains("title") ) {
this.tabsService.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.tabsService.closeTab(
2025-06-05 00:07:21 -05:00
event.srcElement.parentElement.getAttribute("title")
);
}
}
public createTab(title: string, uuid: string, path: string): void {
this.tabsService.push({title: title, uuid: uuid, path: path});
this.changeDetectorRef.detectChanges();
}
private moved(event: any): void {
2025-06-05 00:07:21 -05:00
let target = event.event.target;
let fpath = "";
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")
)
this.tabsService.setNewTargetIndex(fpath);
2025-06-05 00:07:21 -05:00
}
protected dropped(event: CdkDragDrop<any>): void {
this.tabsService.move(event.previousIndex);
}
2025-06-12 02:45:17 -05:00
}