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"},
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"},

View File

@ -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;
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 {

View File

@ -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();
}

View File

@ -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(

View File

@ -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 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);
});
}