diff --git a/src/app/common/services/editor/tabs/tabs.service.ts b/src/app/common/services/editor/tabs/tabs.service.ts index 7f89510..eedd53e 100644 --- a/src/app/common/services/editor/tabs/tabs.service.ts +++ b/src/app/common/services/editor/tabs/tabs.service.ts @@ -45,45 +45,21 @@ export class TabsService { } public getLeftSiblingTab(fpath: string): string { - let size = this.tabs.length; - let i = 0; + if (this.tabs.length === 0 ) return; - for (; i < size; i++) { - if (this.tabs[i].path == fpath) { - break; - } - } + let i = this.tabs.indexOf(fpath); - if ( !(size > 1) ) { - return ""; - } - - if ( i === 0 ) { - return this.tabs[i + 1].path; - } - - return this.tabs[i - 1].path; + (i === 0) ? i = this.tabs.length - 1 : i -= 1; + return this.tabs[i].path; } public getRightSiblingTab(fpath: string): string { - let size = this.tabs.length; - let i = 0; + if (this.tabs.length === 0 ) return; - for (; i < size; i++) { - if (this.tabs[i].path == fpath) { - break; - } - } + let i = this.tabs.indexOf(fpath); - if ( !(size > 1) ) { - return ""; - } - - if ( i === (size - 1) ) { - return this.tabs[i - 1].path; - } - - return this.tabs[i + 1].path; + (i === (this.tabs.length - 1)) ? i = 0 : i += 1; + return this.tabs[i].path; } public setNewTargetIndex(fpath: string): void { diff --git a/src/app/common/services/files.service.ts b/src/app/common/services/files.service.ts index 32ee31a..0ab0071 100644 --- a/src/app/common/services/files.service.ts +++ b/src/app/common/services/files.service.ts @@ -41,17 +41,19 @@ export class FilesService { public getPreviousFile(path: string): NewtonFile { let paths = this.getAllPaths(); - let i = paths.indexOf(path); + if (paths.length === 0 ) return; - (i == 0) ? i += 1 : i -= 1; + let i = paths.indexOf(path); + (i === 0) ? i = paths.length - 1 : i -= 1; return this.files.get( paths[i] ); } public getNextFile(path: string): NewtonFile { let paths = this.getAllPaths(); - let i = paths.indexOf(path); + if (paths.length === 0 ) return; - (i > paths.length) ? i -= 1 : i += 1; + let i = paths.indexOf(path); + (i === (paths.length - 1)) ? i = 0 : i += 1; return this.files.get( paths[i] ); } diff --git a/src/app/editor/code-view/view.component.ts b/src/app/editor/code-view/view.component.ts index a4f2729..57d8720 100644 --- a/src/app/editor/code-view/view.component.ts +++ b/src/app/editor/code-view/view.component.ts @@ -187,8 +187,7 @@ export class CodeViewComponent extends CodeViewBase { event.preventDefault(); event.stopPropagation(); this.zoomIn(); - } - else if (event.domEvent.ctrlKey && event.domEvent.deltaY > 0) { + } else if (event.domEvent.ctrlKey && event.domEvent.deltaY > 0) { event.preventDefault(); event.stopPropagation(); this.zoomOut(); diff --git a/src/app/editor/editors.component.ts b/src/app/editor/editors.component.ts index 1dec7a8..135fffb 100644 --- a/src/app/editor/editors.component.ts +++ b/src/app/editor/editors.component.ts @@ -55,109 +55,80 @@ export class EditorsComponent { this.editorsService.getMessage$().pipe( takeUntil(this.unsubscribe) ).subscribe((message: ServiceMessage) => { - if (message.action === "select-left-editor") { - let editorComponent = this.editorsService.get(message.editorUUID); - if (!editorComponent.leftSiblingUUID) return; - let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID); - siblingComponent.editor.focus() - } else if (message.action === "select-right-editor") { - let editorComponent = this.editorsService.get(message.editorUUID); - if (!editorComponent.rightSiblingUUID) return; - let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID); - siblingComponent.editor.focus() - } else if (message.action === "move-session-left") { - let editorComponent = this.editorsService.get(message.editorUUID); - if (!editorComponent.leftSiblingUUID) return; - - let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID); - let session = editorComponent.editor.getSession(); - let siblingSession = siblingComponent.editor.getSession(); - - if (session == siblingSession) return; - - siblingComponent.assignSession(editorComponent.activeFile); - - let targetPath = this.tabsService.getRightSiblingTab( - editorComponent.activeFile.path - ) - if (targetPath) { - editorComponent.assignSession( - this.filesService.get(targetPath) - ); - } else { - editorComponent.newFile(); - } - - siblingComponent.editor.focus() - } else if (message.action === "move-session-right") { - let editorComponent = this.editorsService.get(message.editorUUID); - if (!editorComponent.rightSiblingUUID) return; - - let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID); - let session = editorComponent.editor.getSession(); - let siblingSession = siblingComponent.editor.getSession(); - - if (session == siblingSession) return; - - siblingComponent.assignSession(editorComponent.activeFile); - - let targetPath = this.tabsService.getRightSiblingTab( - editorComponent.activeFile.path - ) - if (targetPath) { - editorComponent.assignSession( - this.filesService.get(targetPath) - ); - } else { - editorComponent.newFile(); - } - - siblingComponent.editor.focus() - } else if (message.action === "set-active-editor") { - this.editorsService.getActiveEditorComponent().removeActiveStyling(); - this.editorsService.setActiveEditor(message.editorUUID); - this.editorsService.getActiveEditorComponent().addActiveStyling(); - } else if (message.action === "set-tab-to-editor") { - let file = this.filesService.get(message.filePath); - let editorComponent = this.editorsService.getActiveEditorComponent(); - let editor = editorComponent.editor; - - editorComponent.assignSession(file); - this.editorsService.miniMapView.cloneSession(file); - } else if (message.action === "close-tab") { - let activeComponent = this.editorsService.getActiveEditorComponent(); - let editors = this.editorsService.getEditorsAsArray(); - let file = this.filesService.get(message.filePath); - - for (let i = 0; i < editors.length; i++) { - let editorComponent = editors[i]; - - if (editorComponent.editor.session !== file.session) continue; - - let targetFile = this.filesService.getPreviousFile(file.path) - if (targetFile) { - editorComponent.assignSession(targetFile); - if (activeComponent == editorComponent) { - this.editorsService.miniMapView.cloneSession(targetFile); - } - } else { - editorComponent.newFile(); - if (activeComponent == editorComponent) { - this.editorsService.miniMapView.newFile(); - } - } - - } - - activeComponent.lspManagerService.closeDocument(file.session); - this.filesService.unset(file); + switch ( message.action ) { + case "select-left-editor": + this.selectLeftEditor(message); + break; + case "select-right-editor": + this.selectRightEditor(message); + break; + case "move-session-left": + this.moveSessionLeft(message); + break; + case "move-session-right": + this.moveSessionRight(message); + break; + case "set-active-editor": + this.setActiveEditor(message); + break; + case "set-tab-to-editor": + this.setTabToEditor(message); + break; + case "close-tab": + this.closeTab(message); + break; + default: + break; } - }); } private loadMainSubscribers() { + window.main.onMenuActions(async (action: string) => { + let editorComponent = this.editorsService.getActiveEditorComponent(); + let editor = editorComponent.editor; + + switch ( action ) { + case "new-file": + break; + case "open-files": + editorComponent.openFiles(); + break; + case "save-file": + editorComponent.saveFile(); + break; + case "save-file-as": + editorComponent.saveFileAs(); + break; + case "cut": + editorComponent.cutText(); + break; + case "copy": + editorComponent.copyText(); + break; + case "paste": + editorComponent.pasteText(); + break; + case "zoom-in": + editorComponent.zoomIn() + break; + case "zoom-out": + editorComponent.zoomOut() + break; + case "open-settings": + editor.showSettingsMenu(); + case "show-about": + break; + case "quit": + window.main.quit(); + break; + default: + editor.execCommand(action); + break; + } + }); + window.fs.onLoadFiles(async (paths: []) => { for (let i = 0; i < paths.length; i++) { let file = new File([], "") as NewtonFile; @@ -208,48 +179,6 @@ export class EditorsComponent { // this.filesService.sendMessage(message); }); - window.main.onMenuActions(async (action: string) => { - let editorComponent = this.editorsService.getActiveEditorComponent(); - let editor = editorComponent.editor; - - switch ( action ) { - case "new-file": - break; - case "open-files": - editorComponent.openFiles(); - break; - case "save-file": - editorComponent.saveFile(); - break; - case "save-file-as": - editorComponent.saveFileAs(); - break; - case "cut": - editorComponent.cutText(); - break; - case "copy": - editorComponent.copyText(); - break; - case "paste": - editorComponent.pasteText(); - break; - case "zoom-in": - editorComponent.zoomIn() - break; - case "zoom-out": - editorComponent.zoomOut() - break; - case "open-settings": - editor.showSettingsMenu(); - case "show-about": - break; - case "quit": - window.main.quit(); - break; - default: - editor.execCommand(action); - } - }); } protected onFileDropped(files: any) { @@ -262,4 +191,104 @@ export class EditorsComponent { }); } + + private selectLeftEditor(message: ServiceMessage) { + let editorComponent = this.editorsService.get(message.editorUUID); + if (!editorComponent.leftSiblingUUID) return; + let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID); + siblingComponent.editor.focus(); + } + + private selectRightEditor(message: ServiceMessage) { + let editorComponent = this.editorsService.get(message.editorUUID); + if (!editorComponent.rightSiblingUUID) return; + let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID); + siblingComponent.editor.focus(); + } + + private moveSessionLeft(message: ServiceMessage) { + let editorComponent = this.editorsService.get(message.editorUUID); + if (!editorComponent.leftSiblingUUID) return; + + let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID); + this.moveSession("left", editorComponent, siblingComponent); + } + + private moveSessionRight(message: ServiceMessage) { + let editorComponent = this.editorsService.get(message.editorUUID); + if (!editorComponent.rightSiblingUUID) return; + + let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID); + this.moveSession("right", editorComponent, siblingComponent); + } + + private moveSession( + direction: string, + editorComponent: CodeViewComponent, + siblingComponent: CodeViewComponent + ) { + let session = editorComponent.editor.getSession(); + let siblingSession = siblingComponent.editor.getSession(); + + if (session == siblingSession) return; + + let targetPath: string = this.tabsService.getRightSiblingTab( + editorComponent.activeFile.path + ); + + siblingComponent.assignSession(editorComponent.activeFile); + if (targetPath) { + editorComponent.assignSession( + this.filesService.get(targetPath) + ); + } else { + editorComponent.newFile(); + } + + siblingComponent.editor.focus() + } + + + private setActiveEditor(message: ServiceMessage) { + this.editorsService.getActiveEditorComponent().removeActiveStyling(); + this.editorsService.setActiveEditor(message.editorUUID); + this.editorsService.getActiveEditorComponent().addActiveStyling(); + } + private setTabToEditor(message: ServiceMessage) { + let file = this.filesService.get(message.filePath); + let editorComponent = this.editorsService.getActiveEditorComponent(); + let editor = editorComponent.editor; + + editorComponent.assignSession(file); + this.editorsService.miniMapView.cloneSession(file); + } + + private closeTab(message: ServiceMessage) { + let activeComponent = this.editorsService.getActiveEditorComponent(); + let editors = this.editorsService.getEditorsAsArray(); + let file = this.filesService.get(message.filePath); + + for (let i = 0; i < editors.length; i++) { + let editorComponent = editors[i]; + + if (editorComponent.editor.session !== file.session) continue; + + let targetFile = this.filesService.getPreviousFile(file.path) + if (targetFile) { + editorComponent.assignSession(targetFile); + if (activeComponent == editorComponent) { + this.editorsService.miniMapView.cloneSession(targetFile); + } + } else { + editorComponent.newFile(); + if (activeComponent == editorComponent) { + this.editorsService.miniMapView.newFile(); + } + } + + } + + activeComponent.lspManagerService.closeDocument(file.session); + this.filesService.unset(file); + } } \ No newline at end of file