Moved tab component logic to service; handle move session to select next tab

This commit is contained in:
2025-07-02 21:26:24 -05:00
parent 051e42bfa3
commit 8661f2a21a
3 changed files with 97 additions and 19 deletions

View File

@@ -1,3 +1,4 @@
import { moveItemInArray } from '@angular/cdk/drag-drop';
import { Injectable } from '@angular/core';
import { BehaviorSubject, ReplaySubject, Observable } from 'rxjs';
@@ -10,9 +11,73 @@ import { ServiceMessage } from '../../../types/service-message.type';
export class TabsService {
private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
tabs: any[] = [];
newIndex: number = -1;
constructor() {}
public push(tabData: {}): void {
this.tabs.push(tabData);
}
public getLeftSibling(fpath: string): string {
let size = this.tabs.length;
let i = 0;
for (; i < size; i++) {
if (this.tabs[i].path == fpath) {
break;
}
}
if ( !(size > 1) ) {
return "";
}
if ( i === 0 ) {
return this.tabs[i + 1].path;
}
return this.tabs[i - 1].path;
}
public getRightSibling(fpath: string): string {
let size = this.tabs.length;
let i = 0;
for (; i < size; i++) {
if (this.tabs[i].path == fpath) {
break;
}
}
if ( !(size > 1) ) {
return "";
}
if ( i === (size - 1) ) {
return this.tabs[i - 1].path;
}
return this.tabs[i + 1].path;
}
public move(oldIndex: number): void {
if (this.newIndex == -1) return;
moveItemInArray(this.tabs, oldIndex, this.newIndex);
this.newIndex = -1;
// event.currentIndex not updating for some reason...
// moveItemInArray(this.tabs, event.previousIndex, event.currentIndex);
}
public splice(index: number): void {
this.tabs.splice(index, 1);
}
public sendMessage(data: ServiceMessage): void {
this.messageSubject.next(data);
}

View File

@@ -77,7 +77,17 @@ export class EditorsComponent {
siblingComponent.assignSession(editorComponent.activeFile);
editorComponent.newSession();
let targetPath = this.tabsService.getRightSibling(
editorComponent.activeFile.path
)
if (targetPath) {
editorComponent.assignSession(
this.filesService.get(targetPath)
);
} else {
editorComponent.newSession();
}
siblingComponent.editor.focus()
} else if (message.action === "move-session-right") {
let editorComponent = this.editorsService.get(message.editorUUID);
@@ -91,7 +101,17 @@ export class EditorsComponent {
siblingComponent.assignSession(editorComponent.activeFile);
editorComponent.newSession();
let targetPath = this.tabsService.getRightSibling(
editorComponent.activeFile.path
)
if (targetPath) {
editorComponent.assignSession(
this.filesService.get(targetPath)
);
} else {
editorComponent.newSession();
}
siblingComponent.editor.focus()
} else if (message.action === "set-active-editor") {
this.editorsService.getActiveEditorComponent().removeActiveStyling();

View File

@@ -1,6 +1,6 @@
import { Component, ChangeDetectorRef, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CdkDrag, CdkDragDrop, CdkDropList, moveItemInArray } from '@angular/cdk/drag-drop';
import { CdkDrag, CdkDragDrop, CdkDropList } from '@angular/cdk/drag-drop';
import { Subject, takeUntil } from 'rxjs';
import { EditorsService } from '../../common/services/editor/editors.service';
@@ -31,9 +31,7 @@ export class TabsComponent {
private tabsService: TabsService = inject(TabsService);
private changeDetectorRef: ChangeDetectorRef = inject(ChangeDetectorRef);
activeTab!: string;
tabs: any[] = [];
newIndex: number = -1;
tabs: any[] = this.tabsService.tabs;
constructor() {
@@ -93,7 +91,7 @@ export class TabsComponent {
}
public createTab(title: string, uuid: string, path: string): void {
this.tabs.push({title: title, uuid: uuid, path: path});
this.tabsService.push({title: title, uuid: uuid, path: path});
this.changeDetectorRef.detectChanges();
}
@@ -101,8 +99,9 @@ export class TabsComponent {
this.sendEditorsServiceAMessage("close-tab", fpath);
for (let i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].path == fpath) {
this.tabs.splice(i, 1);
if (this.tabsService.tabs[i].path == fpath) {
this.tabsService.splice(i);
break;
}
}
@@ -121,24 +120,18 @@ export class TabsComponent {
)
for (let i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].path == fpath) {
this.newIndex = i;
if (this.tabsService.tabs[i].path == fpath) {
this.tabsService.newIndex = i;
break;
}
}
}
protected dropped(event: CdkDragDrop<any>): void {
if (this.newIndex == -1) return;
moveItemInArray(this.tabs, event.previousIndex, this.newIndex);
this.newIndex = -1;
// event.currentIndex not updating for some reason...
// moveItemInArray(this.tabs, event.previousIndex, event.currentIndex);
this.tabsService.move(event.previousIndex);
}
private sendEditorsServiceAMessage(action: string, fpath: string) {
let message = new ServiceMessage();
message.action = action;