changing keybinding names; fixed close tab issue; moved editor component methods to base

This commit is contained in:
itdominator 2025-07-03 09:28:20 -05:00
parent f94ac677a9
commit d0c73fe4da
5 changed files with 112 additions and 96 deletions

View File

@ -37,13 +37,9 @@ export const Keybindings: Array<{}> = [
bindKey: {win: "ctrl-r", mac: "ctrl-r"}, bindKey: {win: "ctrl-r", mac: "ctrl-r"},
readOnly: false readOnly: false
}, { }, {
name: "newSession", name: "newFile",
bindKey: {win: "ctrl-t", mac: "ctrl-t"}, bindKey: {win: "ctrl-t", mac: "ctrl-t"},
readOnly: true readOnly: true
}, {
name: "destroySession",
bindKey: {win: "ctrl-w", mac: "ctrl-w"},
readOnly: false
}, { }, {
name: "openFiles", name: "openFiles",
bindKey: {win: "ctrl-o", mac: "ctrl-o"}, bindKey: {win: "ctrl-o", mac: "ctrl-o"},
@ -56,6 +52,10 @@ export const Keybindings: Array<{}> = [
name: "saveFileAs", name: "saveFileAs",
bindKey: {win: "ctrl-shift-s", mac: "ctrl-shift-s"}, bindKey: {win: "ctrl-shift-s", mac: "ctrl-shift-s"},
readOnly: false readOnly: false
}, {
name: "closeFile",
bindKey: {win: "ctrl-w", mac: "ctrl-w"},
readOnly: false
}, { }, {
name: "selectLeftEditor", name: "selectLeftEditor",
bindKey: {win: "ctrl-pageup", mac: "ctrl-pageup"}, bindKey: {win: "ctrl-pageup", mac: "ctrl-pageup"},

View File

@ -63,7 +63,7 @@ export class EditorsService {
rightEditor.leftSiblingUUID = leftEditor.uuid; rightEditor.leftSiblingUUID = leftEditor.uuid;
} }
public async setSession(file: NewtonFile | undefined | null) { public setSession(file: NewtonFile | undefined | null) {
if ( !file ) return; if ( !file ) return;
let editorComponent = this.getActiveEditorComponent(); let editorComponent = this.getActiveEditorComponent();
@ -78,13 +78,21 @@ export class EditorsService {
return editor.getSession(); return editor.getSession();
} }
public setActiveEditor(activeEditor: string) { public async setActiveEditor(activeEditor: string) {
this.activeEditor = activeEditor; this.activeEditor = activeEditor;
let editorComponent = this.getActiveEditorComponent(); let editorComponent = this.getActiveEditorComponent();
if (!this.miniMapView) return; 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 { public getActiveEditorComponent(): any {

View File

@ -32,12 +32,13 @@ export class CodeViewBase {
@Input() editorSettings!: typeof EditorSettings; @Input() editorSettings!: typeof EditorSettings;
public editor!: any; public editor!: any;
public aceApi!: any;
public activeFile!: NewtonFile; public activeFile!: NewtonFile;
public cutBuffer: string = ""; public cutBuffer: string = "";
public timerId: number = -1; public timerId: number = -1;
public debounceId: number = -1; public debounceId: number = -1;
public debounceWait: number = 2000; public debounceWait: number = 800;
constructor() { constructor() {
@ -149,7 +150,7 @@ export class CodeViewBase {
} }
public destroySession() { public closeFile() {
this.tabsService.closeTab(this.activeFile.path); 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() { private quit() {
window.main.quit(); window.main.quit();
} }

View File

@ -36,6 +36,8 @@ export class CodeViewComponent extends CodeViewBase {
constructor() { constructor() {
super(); super();
this.aceApi = ace;
} }
@ -139,15 +141,16 @@ export class CodeViewComponent extends CodeViewBase {
}); });
this.editor.on("change", () => { this.editor.on("change", () => {
if (!this.activeFile) return;
if (this.debounceId) { clearTimeout(this.debounceId); } if (this.debounceId) { clearTimeout(this.debounceId); }
this.setDebounceTimeout();
if (!this.activeFile) return;
let message = new ServiceMessage(); let message = new ServiceMessage();
message.action = "file-changed"; message.action = "file-changed";
message.filePath = this.activeFile.path; message.filePath = this.activeFile.path;
this.tabsService.sendMessage(message); this.tabsService.sendMessage(message);
this.setDebounceTimeout();
}); });
this.editor.on("changeSession", (session) => { 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) { public assignSession(file: NewtonFile) {
if (!file) return; if (!file) return;
@ -173,80 +170,12 @@ export class CodeViewComponent extends CodeViewBase {
if (!file) return; if (!file) return;
this.activeFile = file; this.activeFile = file;
let session = ace.createEditSession(file.session.getValue()); let session = this.aceApi.createEditSession(file.session.getValue());
session.setMode( file.session.getMode()["$id"] ); session.setMode( file.session.getMode()["$id"] );
this.editor.setSession(session); 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) { private setDebounceTimeout(timeout: number = null) {
this.debounceId = setTimeout(() => { this.debounceId = setTimeout(() => {
this.editorsService.miniMapView.editor.session.setValue( this.editorsService.miniMapView.editor.session.setValue(

View File

@ -85,7 +85,7 @@ export class EditorsComponent {
this.filesService.get(targetPath) this.filesService.get(targetPath)
); );
} else { } else {
editorComponent.newSession(); editorComponent.newFile();
} }
siblingComponent.editor.focus() siblingComponent.editor.focus()
@ -109,7 +109,7 @@ export class EditorsComponent {
this.filesService.get(targetPath) this.filesService.get(targetPath)
); );
} else { } else {
editorComponent.newSession(); editorComponent.newFile();
} }
siblingComponent.editor.focus() siblingComponent.editor.focus()
@ -125,17 +125,17 @@ export class EditorsComponent {
editorComponent.assignSession(file); editorComponent.assignSession(file);
this.editorsService.miniMapView.cloneSession(file); this.editorsService.miniMapView.cloneSession(file);
} else if (message.action === "close-tab") { } else if (message.action === "close-tab") {
let file = this.filesService.get(message.filePath); let activeComponent = this.editorsService.getActiveEditorComponent();
let editors = this.editorsService.getEditorsAsArray(); let editors = this.editorsService.getEditorsAsArray();
let file = this.filesService.get(message.filePath);
for (let i = 0; i < editors.length; i++) { for (let i = 0; i < editors.length; i++) {
let editorComponent = editors[i]; let editorComponent = editors[i];
if (editorComponent.editor.session == file.session) { if (editorComponent.editor.session == file.session) {
editorComponent.newSession(); if (activeComponent == editorComponent) {
this.editorsService.miniMapView.editor.setSession( this.editorsService.miniMapView.newFile();
editorComponent.editor.getSession() }
); editorComponent.newFile();
this.editorsService.miniMapView.activeFile = null;
} }
} }
@ -240,6 +240,10 @@ export class EditorsComponent {
protected onFileDropped(files: any) { protected onFileDropped(files: any) {
this.filesService.loadFilesList(files).then((file: NewtonFile | undefined | null) => { 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); this.editorsService.setSession(file);
}); });
} }