From d0c73fe4dae9489373198812c043fc90e1934aa4 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Thu, 3 Jul 2025 09:28:20 -0500 Subject: [PATCH] changing keybinding names; fixed close tab issue; moved editor component methods to base --- src/app/common/configs/keybindings.config.ts | 10 +-- .../common/services/editor/editors.service.ts | 14 +++- src/app/editor/code-view/view.base.ts | 79 +++++++++++++++++- src/app/editor/code-view/view.component.ts | 83 ++----------------- src/app/editor/editors.component.ts | 22 +++-- 5 files changed, 112 insertions(+), 96 deletions(-) diff --git a/src/app/common/configs/keybindings.config.ts b/src/app/common/configs/keybindings.config.ts index 1584717..e56d34e 100644 --- a/src/app/common/configs/keybindings.config.ts +++ b/src/app/common/configs/keybindings.config.ts @@ -37,13 +37,9 @@ export const Keybindings: Array<{}> = [ bindKey: {win: "ctrl-r", mac: "ctrl-r"}, readOnly: false }, { - name: "newSession", + name: "newFile", bindKey: {win: "ctrl-t", mac: "ctrl-t"}, readOnly: true - }, { - name: "destroySession", - bindKey: {win: "ctrl-w", mac: "ctrl-w"}, - readOnly: false }, { name: "openFiles", bindKey: {win: "ctrl-o", mac: "ctrl-o"}, @@ -56,6 +52,10 @@ export const Keybindings: Array<{}> = [ name: "saveFileAs", bindKey: {win: "ctrl-shift-s", mac: "ctrl-shift-s"}, readOnly: false + }, { + name: "closeFile", + bindKey: {win: "ctrl-w", mac: "ctrl-w"}, + readOnly: false }, { name: "selectLeftEditor", bindKey: {win: "ctrl-pageup", mac: "ctrl-pageup"}, diff --git a/src/app/common/services/editor/editors.service.ts b/src/app/common/services/editor/editors.service.ts index 844444d..f4a1b94 100644 --- a/src/app/common/services/editor/editors.service.ts +++ b/src/app/common/services/editor/editors.service.ts @@ -63,7 +63,7 @@ export class EditorsService { rightEditor.leftSiblingUUID = leftEditor.uuid; } - public async setSession(file: NewtonFile | undefined | null) { + public setSession(file: NewtonFile | undefined | null) { if ( !file ) return; let editorComponent = this.getActiveEditorComponent(); @@ -78,13 +78,21 @@ export class EditorsService { return editor.getSession(); } - public setActiveEditor(activeEditor: string) { + public async setActiveEditor(activeEditor: string) { this.activeEditor = activeEditor; let editorComponent = this.getActiveEditorComponent(); if (!this.miniMapView) return; - this.miniMapView.cloneSession(editorComponent.activeFile); + if (editorComponent.activeFile) { + this.miniMapView.cloneSession(editorComponent.activeFile); + return; + } + + // Note: likely a new file/buffer + this.miniMapView.editor.session.setValue( + editorComponent.editor.session.getValue() + ); } public getActiveEditorComponent(): any { diff --git a/src/app/editor/code-view/view.base.ts b/src/app/editor/code-view/view.base.ts index 1b1ce5a..6f85316 100644 --- a/src/app/editor/code-view/view.base.ts +++ b/src/app/editor/code-view/view.base.ts @@ -32,12 +32,13 @@ export class CodeViewBase { @Input() editorSettings!: typeof EditorSettings; public editor!: any; + public aceApi!: any; public activeFile!: NewtonFile; public cutBuffer: string = ""; public timerId: number = -1; public debounceId: number = -1; - public debounceWait: number = 2000; + public debounceWait: number = 800; constructor() { @@ -149,7 +150,7 @@ export class CodeViewBase { } - public destroySession() { + public closeFile() { this.tabsService.closeTab(this.activeFile.path); } @@ -248,6 +249,80 @@ export class CodeViewBase { ); } + public newFile() { + this.activeFile = null; + let session = this.aceApi.createEditSession([""]); + this.editor.setSession(session); + } + + protected openFiles() { + let startDir = ""; + if (this.activeFile) { + let pathParts = this.activeFile.path.split("/"); + pathParts.pop(); + startDir = pathParts.join( '/' ); + } + + window.fs.openFiles(startDir); + } + + protected saveFile() { + if (!this.activeFile) { + this.saveFileAs(); + return; + } + + const text = this.activeFile.session.getValue(); + window.fs.saveFile(this.activeFile.path, text); + } + + protected saveFileAs() { + window.fs.saveFileAs().then((path: string) => { + if (!path) return; + + let file: NewtonFile = new File([""], path, {}); + const text = this.editor.session.getValue(); + + window.fs.saveFile(path, text); + this.filesService.addFile( + path, + file, + false, + text + ).then(() => { + this.activeFile = this.filesService.get(path); + this.editor.setSession(this.activeFile.session); + this.filesService.addTab(this.activeFile); + }); + + }); + } + + protected cutToBuffer() { + if (this.timerId) { clearTimeout(this.timerId); } + + const cursorPosition = this.editor.getCursorPosition(); + let lineText = this.editor.session.getLine(cursorPosition.row); + this.cutBuffer += `${lineText}\n`; + + this.editor.session.removeFullLines(cursorPosition.row, cursorPosition.row) + this.setBufferClearTimeout(); + } + + protected pasteCutBuffer() { + if (this.timerId) { clearTimeout(this.timerId); } + + this.editor.insert(this.cutBuffer, true); + this.setBufferClearTimeout(); + } + + private setBufferClearTimeout(timeout: number = 5000) { + this.timerId = setTimeout(() => { + this.cutBuffer = ""; + this.timerId = -1; + }, timeout); + } + private quit() { window.main.quit(); } diff --git a/src/app/editor/code-view/view.component.ts b/src/app/editor/code-view/view.component.ts index e4c68c6..c10d5b1 100644 --- a/src/app/editor/code-view/view.component.ts +++ b/src/app/editor/code-view/view.component.ts @@ -36,6 +36,8 @@ export class CodeViewComponent extends CodeViewBase { constructor() { super(); + + this.aceApi = ace; } @@ -139,15 +141,16 @@ export class CodeViewComponent extends CodeViewBase { }); this.editor.on("change", () => { - if (!this.activeFile) return; if (this.debounceId) { clearTimeout(this.debounceId); } + this.setDebounceTimeout(); + + if (!this.activeFile) return; let message = new ServiceMessage(); message.action = "file-changed"; message.filePath = this.activeFile.path; this.tabsService.sendMessage(message); - this.setDebounceTimeout(); }); this.editor.on("changeSession", (session) => { @@ -156,12 +159,6 @@ export class CodeViewComponent extends CodeViewBase { } - public newSession() { - this.activeFile = null; - let session = ace.createEditSession([""]); - this.editor.setSession(session); - } - public assignSession(file: NewtonFile) { if (!file) return; @@ -173,80 +170,12 @@ export class CodeViewComponent extends CodeViewBase { if (!file) return; this.activeFile = file; - let session = ace.createEditSession(file.session.getValue()); + let session = this.aceApi.createEditSession(file.session.getValue()); session.setMode( file.session.getMode()["$id"] ); this.editor.setSession(session); } - protected openFiles() { - let startDir = ""; - if (this.activeFile) { - let pathParts = this.activeFile.path.split("/"); - pathParts.pop(); - startDir = pathParts.join( '/' ); - } - - window.fs.openFiles(startDir); - } - - protected saveFile() { - if (!this.activeFile) { - this.saveFileAs(); - return; - } - - const text = this.activeFile.session.getValue(); - window.fs.saveFile(this.activeFile.path, text); - } - - protected saveFileAs() { - window.fs.saveFileAs().then((path: string) => { - if (!path) return; - - let file: NewtonFile = new File([""], path, {}); - const text = this.editor.session.getValue(); - - window.fs.saveFile(path, text); - this.filesService.addFile( - path, - file, - false, - text - ).then(() => { - this.activeFile = this.filesService.get(path); - this.editor.setSession(this.activeFile.session); - this.filesService.addTab(this.activeFile); - }); - - }); - } - - protected cutToBuffer() { - if (this.timerId) { clearTimeout(this.timerId); } - - const cursorPosition = this.editor.getCursorPosition(); - let lineText = this.editor.session.getLine(cursorPosition.row); - this.cutBuffer += `${lineText}\n`; - - this.editor.session.removeFullLines(cursorPosition.row, cursorPosition.row) - this.setBufferClearTimeout(); - } - - protected pasteCutBuffer() { - if (this.timerId) { clearTimeout(this.timerId); } - - this.editor.insert(this.cutBuffer, true); - this.setBufferClearTimeout(); - } - - private setBufferClearTimeout(timeout: number = 5000) { - this.timerId = setTimeout(() => { - this.cutBuffer = ""; - this.timerId = -1; - }, timeout); - } - private setDebounceTimeout(timeout: number = null) { this.debounceId = setTimeout(() => { this.editorsService.miniMapView.editor.session.setValue( diff --git a/src/app/editor/editors.component.ts b/src/app/editor/editors.component.ts index f6cb9ce..926cfde 100644 --- a/src/app/editor/editors.component.ts +++ b/src/app/editor/editors.component.ts @@ -85,7 +85,7 @@ export class EditorsComponent { this.filesService.get(targetPath) ); } else { - editorComponent.newSession(); + editorComponent.newFile(); } siblingComponent.editor.focus() @@ -109,7 +109,7 @@ export class EditorsComponent { this.filesService.get(targetPath) ); } else { - editorComponent.newSession(); + editorComponent.newFile(); } siblingComponent.editor.focus() @@ -125,17 +125,17 @@ export class EditorsComponent { editorComponent.assignSession(file); this.editorsService.miniMapView.cloneSession(file); } else if (message.action === "close-tab") { - let file = this.filesService.get(message.filePath); - let editors = this.editorsService.getEditorsAsArray(); + 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) { - editorComponent.newSession(); - this.editorsService.miniMapView.editor.setSession( - editorComponent.editor.getSession() - ); - this.editorsService.miniMapView.activeFile = null; + if (activeComponent == editorComponent) { + this.editorsService.miniMapView.newFile(); + } + editorComponent.newFile(); } } @@ -240,6 +240,10 @@ export class EditorsComponent { protected onFileDropped(files: any) { this.filesService.loadFilesList(files).then((file: NewtonFile | undefined | null) => { + // Note: if we drop an already loaded file the path doesn't get set and + // therefor the last file in drop list might get returned without path. + if (!file.path) return; + this.editorsService.setSession(file); }); }