diff --git a/src/app/common/services/editor/tabs/tabs.service.ts b/src/app/common/services/editor/tabs/tabs.service.ts index cf04e7e..7f89510 100644 --- a/src/app/common/services/editor/tabs/tabs.service.ts +++ b/src/app/common/services/editor/tabs/tabs.service.ts @@ -1,6 +1,9 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; +import { moveItemInArray } from '@angular/cdk/drag-drop'; import { BehaviorSubject, ReplaySubject, Observable } from 'rxjs'; +import { EditorsService } from '../editors.service'; + import { ServiceMessage } from '../../../types/service-message.type'; @@ -10,9 +13,103 @@ import { ServiceMessage } from '../../../types/service-message.type'; export class TabsService { private messageSubject: ReplaySubject = new ReplaySubject(1); + private editorsService: EditorsService = inject(EditorsService); + + tabs: any[] = []; + newIndex: number = -1; + constructor() {} + public push(tabData: {}): void { + this.tabs.push(tabData); + } + + public closeTab(fpath: string): void { + this.sendEditorsServiceAMessage("close-tab", fpath); + + for (let i = 0; i < this.tabs.length; i++) { + if (this.tabs[i].path == fpath) { + this.splice(i); + break; + } + } + } + + public sendEditorsServiceAMessage(action: string, fpath: string) { + let message = new ServiceMessage(); + message.action = action; + message.filePath = fpath; + + this.editorsService.sendMessage(message); + } + + public getLeftSiblingTab(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 getRightSiblingTab(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 setNewTargetIndex(fpath: string): void { + for (let i = 0; i < this.tabs.length; i++) { + if (this.tabs[i].path == fpath) { + this.newIndex = i; + break; + } + } + } + + 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); } diff --git a/src/app/editor/code-view/view.base.ts b/src/app/editor/code-view/view.base.ts index 1b68e2f..1b1ce5a 100644 --- a/src/app/editor/code-view/view.base.ts +++ b/src/app/editor/code-view/view.base.ts @@ -150,7 +150,7 @@ export class CodeViewBase { } public destroySession() { - this.editor.session.destroy(); + this.tabsService.closeTab(this.activeFile.path); } public toggleFullScreen() { diff --git a/src/app/editor/editors.component.ts b/src/app/editor/editors.component.ts index 7e5bf24..f6cb9ce 100644 --- a/src/app/editor/editors.component.ts +++ b/src/app/editor/editors.component.ts @@ -77,7 +77,17 @@ export class EditorsComponent { siblingComponent.assignSession(editorComponent.activeFile); - editorComponent.newSession(); + let targetPath = this.tabsService.getRightSiblingTab( + 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.getRightSiblingTab( + 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(); diff --git a/src/app/editor/tabs/tabs.component.ts b/src/app/editor/tabs/tabs.component.ts index 7b750ff..314bfdc 100644 --- a/src/app/editor/tabs/tabs.component.ts +++ b/src/app/editor/tabs/tabs.component.ts @@ -1,9 +1,8 @@ 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'; import { TabsService } from '../../common/services/editor/tabs/tabs.service'; import { ServiceMessage } from '../../common/types/service-message.type'; @@ -27,13 +26,10 @@ import { ServiceMessage } from '../../common/types/service-message.type'; export class TabsComponent { private unsubscribe: Subject = new Subject(); - private editorsService: EditorsService = inject(EditorsService); private tabsService: TabsService = inject(TabsService); private changeDetectorRef: ChangeDetectorRef = inject(ChangeDetectorRef); - activeTab!: string; - tabs: any[] = []; - newIndex: number = -1; + tabs: any[] = this.tabsService.tabs; constructor() { @@ -74,18 +70,18 @@ export class TabsComponent { let target = event.target; if ( target.classList.contains("tab") ) { - this.sendEditorsServiceAMessage( + this.tabsService.sendEditorsServiceAMessage( "set-tab-to-editor", event.srcElement.getAttribute("title") ); } else if ( target.classList.contains("title") ) { - this.sendEditorsServiceAMessage( + this.tabsService.sendEditorsServiceAMessage( "set-tab-to-editor", event.srcElement.parentElement.getAttribute("title") ); } else if ( target.classList.contains("close-button") ) { - this.closeTab( + this.tabsService.closeTab( event.srcElement.parentElement.getAttribute("title") ); } @@ -93,21 +89,10 @@ 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(); } - public closeTab(fpath: string): void { - 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); - } - } - - } - private moved(event: any): void { let target = event.event.target; let fpath = ""; @@ -120,31 +105,11 @@ export class TabsComponent { fpath = target.getAttribute("title") ) - for (let i = 0; i < this.tabs.length; i++) { - if (this.tabs[i].path == fpath) { - this.newIndex = i; - } - } - + this.tabsService.setNewTargetIndex(fpath); } protected dropped(event: CdkDragDrop): 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); - } - - - private sendEditorsServiceAMessage(action: string, fpath: string) { - let message = new ServiceMessage(); - message.action = action; - message.filePath = fpath; - - this.editorsService.sendMessage(message); + this.tabsService.move(event.previousIndex); } } \ No newline at end of file