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

123 lines
3.4 KiB
TypeScript
Raw Normal View History

import { Component, ChangeDetectorRef } 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: {
'class': 'tabs scroller'
2025-05-27 21:10:45 -05:00
}
})
export class TabsComponent {
private unsubscribe = new Subject<void>();
activeTab!: string;
2025-06-05 00:07:21 -05:00
tabs: any[] = [];
newIndex: number = -1;
2025-05-27 21:10:45 -05:00
2025-06-05 00:07:21 -05:00
constructor(
private editorsService: EditorsService,
private tabsService: TabsService,
private changeDetectorRef: ChangeDetectorRef
2025-06-05 00:07:21 -05:00
) {
2025-05-27 21:10:45 -05:00
}
public ngAfterViewInit(): void {
this.tabsService.getData$().pipe(
takeUntil(this.unsubscribe)
).subscribe((data: ServiceMessage) => {
if (data.action === "create-tab") {
this.createTab(data.message, data.uuid, data.data);
}
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 {
this.tabs.push({title: title, path: path, uuid: uuid});
this.changeDetectorRef.detectChanges();
2025-06-05 00:07:21 -05:00
}
handleAction(event: any): void {
let target = event.target;
if ( target.classList.contains("tab") ) {
this.editorsService.setTabToEditor(
event.srcElement.getAttribute("title")
);
} else if ( target.classList.contains("title") ) {
this.editorsService.setTabToEditor(
event.srcElement.parentElement.getAttribute("title")
);
} else if ( target.classList.contains("close-button") ) {
this.closeTab(
event.srcElement.parentElement.getAttribute("title")
);
}
}
closeTab(fpath: string): void {
this.editorsService.closeTab(fpath);
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 = "";
if ( target.classList.contains("title") ) {
fpath = target.parentElement.getAttribute("title")
} else if ( target.classList.contains("close-button") ) {
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-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-12 02:45:17 -05:00
}