Moved injection out of constructor; refactored nedton-editor component and saveAs logic

This commit is contained in:
itdominator 2025-06-20 23:52:22 -05:00
parent 4193d46d0d
commit 598a66f517
11 changed files with 195 additions and 168 deletions

View File

@ -89,14 +89,14 @@ const saveFile = (fpath, content) => {
}); });
} }
const saveFileAs = (content) => { const saveFileAs = () => {
dialog.showSaveDialog().then((response) => { return dialog.showSaveDialog().then((response) => {
if (response.canceled) { if (response.canceled) {
console.debug("You didn't save the file"); console.debug("You didn't save the file");
return; return;
} }
saveFile(response.filePath, content); return response.filePath;
}); });
} }

View File

@ -18,7 +18,7 @@ contextBridge.exposeInMainWorld('fs', {
getFileContents: (path) => ipcRenderer.invoke("getFileContents", path), getFileContents: (path) => ipcRenderer.invoke("getFileContents", path),
openFiles: (startPath) => ipcRenderer.invoke("openFiles", startPath), openFiles: (startPath) => ipcRenderer.invoke("openFiles", startPath),
saveFile: (path, content) => ipcRenderer.invoke("saveFile", path, content), saveFile: (path, content) => ipcRenderer.invoke("saveFile", path, content),
saveFileAs: (content) => ipcRenderer.invoke("saveFileAs", content), saveFileAs: () => ipcRenderer.invoke("saveFileAs"),
closeFile: (path) => ipcRenderer.invoke("closeFile", path), closeFile: (path) => ipcRenderer.invoke("closeFile", path),
getPathForFile: (file) => webUtils.getPathForFile(file), getPathForFile: (file) => webUtils.getPathForFile(file),
onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, paths) => callback(paths)), onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, paths) => callback(paths)),

View File

@ -1,4 +1,4 @@
import { Injectable } from '@angular/core'; import { Injectable, inject } from '@angular/core';
import { ReplaySubject, Observable } from 'rxjs'; import { ReplaySubject, Observable } from 'rxjs';
import { EditSession } from 'ace-builds'; import { EditSession } from 'ace-builds';
@ -17,12 +17,12 @@ import { ServiceMessage } from '../../types/service-message.type';
export class FilesService { export class FilesService {
private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1); private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
private tabsService: TabsService = inject(TabsService);
files: Map<string, NewtonFile>; files: Map<string, NewtonFile>;
constructor( constructor() {
private tabsService: TabsService,
) {
this.files = new Map<string, NewtonFile>(); this.files = new Map<string, NewtonFile>();
} }
@ -64,15 +64,17 @@ export class FilesService {
return files[ files.length - 1 ]; return files[ files.length - 1 ];
} }
async addFile(path: string, file: NewtonFile): Promise<void> { async addFile(path: string, file: NewtonFile, loadFileContents: boolean = true, data: string = ""): Promise<void> {
try { try {
let pathParts = path.split("/"); let pathParts = path.split("/");
file.fname = pathParts[ pathParts.length - 1 ]; file.fname = pathParts[ pathParts.length - 1 ];
file.path = path; file.path = path;
file.hash = btoa(file.path); file.hash = btoa(file.path);
let data = await window.fs.getFileContents(file.path);
file.session = new EditSession(data);
if (loadFileContents)
data = await window.fs.getFileContents(file.path);
file.session = new EditSession(data);
file.session.setMode( file.session.setMode(
getModeForPath( file.path ).mode getModeForPath( file.path ).mode
); );

View File

@ -2,8 +2,8 @@ import { EditSession } from 'ace-builds';
export interface NewtonFile extends File { export interface NewtonFile extends File {
fname: string, fname?: string,
path: string, path?: string,
hash: string, hash?: string,
session: EditSession session?: EditSession,
} }

View File

@ -1,4 +1,4 @@
import { Component, ElementRef, ViewChild, TemplateRef, ComponentRef, ViewContainerRef } from '@angular/core'; import { Component, ViewChild, ViewContainerRef, inject } from '@angular/core';
import { Subject, takeUntil } from 'rxjs'; import { Subject, takeUntil } from 'rxjs';
import { NewtonEditorComponent } from "./newton-editor/newton-editor.component"; import { NewtonEditorComponent } from "./newton-editor/newton-editor.component";
@ -27,17 +27,17 @@ import { ServiceMessage } from '../common/types/service-message.type';
} }
}) })
export class EditorsComponent { export class EditorsComponent {
private unsubscribe = new Subject<void>(); private unsubscribe: Subject<void> = new Subject();
private editorsService: EditorsService = inject(EditorsService);
private tabsService: TabsService = inject(TabsService);
private filesService: FilesService = inject(FilesService);
@ViewChild('containerRef', {read: ViewContainerRef}) containerRef!: ViewContainerRef; @ViewChild('containerRef', {read: ViewContainerRef}) containerRef!: ViewContainerRef;
activeEditor!: string; activeEditor!: string;
constructor( constructor() {
private editorsService: EditorsService,
private tabsService: TabsService,
private filesService: FilesService
) {
} }

View File

@ -1,4 +1,4 @@
import { Component } from '@angular/core'; import { Component, inject } from '@angular/core';
import { Subject, takeUntil } from 'rxjs'; import { Subject, takeUntil } from 'rxjs';
import { InfoBarService } from '../../common/services/editor/info-bar/info-bar.service'; import { InfoBarService } from '../../common/services/editor/info-bar/info-bar.service';
@ -17,7 +17,9 @@ import { InfoBarService } from '../../common/services/editor/info-bar/info-bar.s
} }
}) })
export class InfoBarComponent { export class InfoBarComponent {
private unsubscribe = new Subject<void>(); private unsubscribe: Subject<void> = new Subject();
private infoBarService: InfoBarService = inject(InfoBarService);
fpath: string = ""; fpath: string = "";
path: string = ""; path: string = "";
@ -26,9 +28,7 @@ export class InfoBarComponent {
ftype: string = ""; ftype: string = "";
constructor( constructor() {}
private infoBarService: InfoBarService
) {}
public ngAfterViewInit(): void { public ngAfterViewInit(): void {

View File

@ -1,4 +1,4 @@
import { Component } from "@angular/core"; import { Component, inject } from "@angular/core";
import { CommonModule } from "@angular/common"; import { CommonModule } from "@angular/common";
import { Subject, takeUntil } from 'rxjs'; import { Subject, takeUntil } from 'rxjs';
@ -25,16 +25,16 @@ import { ServiceMessage } from '../../common/types/service-message.type';
} }
}) })
export class FilesModalComponent { export class FilesModalComponent {
private unsubscribe = new Subject<void>(); private unsubscribe: Subject<void> = new Subject();
private filesModalService: FilesModalService = inject(FilesModalService);
private tabsService: TabsService = inject(TabsService);
filesModal!: bootstrap.Modal; filesModal!: bootstrap.Modal;
files: any[] = []; files: any[] = [];
constructor( constructor() {
private filesModalService: FilesModalService,
private tabsService: TabsService
) {
} }

View File

@ -1,31 +1,80 @@
import { Directive, ElementRef, Input, ViewChild } from '@angular/core'; import { Directive, ElementRef, Input, ViewChild, inject } from '@angular/core';
import * as uuid from 'uuid'; import * as uuid from 'uuid';
import { InfoBarService } from '../../common/services/editor/info-bar/info-bar.service';
import { FilesModalService } from '../../common/services/editor/modals/files-modal.service';
import { LSPService } from '../../common/services/lsp.service';
import { TabsService } from '../../common/services/editor/tabs/tabs.service';
import { EditorsService } from '../../common/services/editor/editors.service';
import { FilesService } from '../../common/services/editor/files.service';
import { EditorSettings } from "../../common/configs/editor.config"; import { EditorSettings } from "../../common/configs/editor.config";
import { NewtonFile } from '../../common/types/file.type'; import { NewtonFile } from '../../common/types/file.type';
import { ServiceMessage } from '../../common/types/service-message.type';
@Directive() @Directive()
export class NewtonEditorBase { export class NewtonEditorBase {
public uuid: string = uuid.v4();;
public isDefault: boolean = false;
public leftSiblingUUID!: string;
public rightSiblingUUID!: string;
protected infoBarService: InfoBarService = inject(InfoBarService);
protected filesModalService: FilesModalService = inject(FilesModalService);
protected lspService: LSPService = inject(LSPService);
protected tabsService: TabsService = inject(TabsService);
protected editorsService: EditorsService = inject(EditorsService);
protected filesService: FilesService = inject(FilesService);
@ViewChild('editor') editorElm!: ElementRef; @ViewChild('editor') editorElm!: ElementRef;
@Input() editorSettings!: typeof EditorSettings; @Input() editorSettings!: typeof EditorSettings;
editor!: any;
uuid!: string; public editor!: any;
leftSiblingUUID!: string; public activeFile!: NewtonFile;
rightSiblingUUID!: string;
cutBuffer: string = ""; public cutBuffer: string = "";
timerId: number = -1; public timerId: number = -1;
activeFile!: NewtonFile;
isDefault: boolean = false;
constructor( constructor() {
) {
this.uuid = uuid.v4();
} }
public selectLeftEditor() {
let message = new ServiceMessage();
message.action = "select-left-editor";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
}
public selectRightEditor() {
let message = new ServiceMessage();
message.action = "select-right-editor";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
}
public moveSessionLeft() {
let message = new ServiceMessage();
message.action = "move-session-left";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
}
public moveSessionRight() {
let message = new ServiceMessage();
message.action = "move-session-right";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
}
public addActiveStyling() { public addActiveStyling() {
this.editorElm.nativeElement.classList.add("active-editor") this.editorElm.nativeElement.classList.add("active-editor")
} }
@ -46,61 +95,43 @@ export class NewtonEditorBase {
this.editor.showKeyboardShortcuts(); this.editor.showKeyboardShortcuts();
} }
protected search() { public search() {
console.log(this.editor.session.getMode()["$id"]); console.log(this.editor.session.getMode()["$id"]);
} }
protected destroySession() { public destroySession() {
this.editor.session.destroy(); this.editor.session.destroy();
} }
protected quit() { public toggleFullScreen() {
window.main.quit();
}
protected toggleFullScreen() {
window.main.toggleFullScreen(); window.main.toggleFullScreen();
} }
protected openFiles() { public zoomIn() {
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() {
const text = this.editor.session.getValue();
window.fs.saveFileAs(text);
}
protected zoomIn() {
this.editor.setFontSize( this.editor.setFontSize(
parseInt(this.editor.getFontSize()) + 1 parseInt(this.editor.getFontSize()) + 1
) )
} }
protected zoomOut() { public zoomOut() {
this.editor.setFontSize( this.editor.setFontSize(
parseInt(this.editor.getFontSize()) - 1 parseInt(this.editor.getFontSize()) - 1
) )
} }
protected cutText() { public movelinesUp() {
this.editor.execCommand("movelinesup");
}
public movelinesDown() {
this.editor.execCommand("movelinesdown");
}
public duplicateLines() {
this.editor.execCommand("copylinesdown");
}
public cutText() {
let cutText = this.editor.getSelectedText(); let cutText = this.editor.getSelectedText();
this.editor.remove(); this.editor.remove();
navigator.clipboard.writeText(cutText).catch(() => { navigator.clipboard.writeText(cutText).catch(() => {
@ -108,54 +139,30 @@ export class NewtonEditorBase {
}); });
} }
protected copyText() { public copyText() {
let copyText = this.editor.getSelectedText(); let copyText = this.editor.getSelectedText();
navigator.clipboard.writeText(copyText).catch(() => { navigator.clipboard.writeText(copyText).catch(() => {
console.error("Unable to copy text..."); console.error("Unable to copy text...");
}); });
} }
protected pasteText() { public pasteText() {
navigator.clipboard.readText().then((pasteText) => { navigator.clipboard.readText().then((pasteText) => {
this.editor.insert(pasteText, true); this.editor.insert(pasteText, true);
}); });
} }
protected movelinesUp() { protected updateInfoBar() {
this.editor.execCommand("movelinesup"); this.infoBarService.setInfoBarFPath(this.activeFile?.path)
this.infoBarService.setInfoBarCursorPos(
this.editor.getCursorPosition()
);
this.infoBarService.setInfoBarFType(
this.editor.session.getMode()["$id"]
);
} }
protected movelinesDown() { private quit() {
this.editor.execCommand("movelinesdown"); window.main.quit();
} }
protected duplicateLines() {
this.editor.execCommand("copylinesdown");
}
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);
}
} }

View File

@ -9,14 +9,9 @@ import "ace-builds/src-noconflict/ext-language_tools";
import "ace-builds/src-noconflict/theme-one_dark"; import "ace-builds/src-noconflict/theme-one_dark";
import "ace-builds/src-noconflict/theme-dracula"; import "ace-builds/src-noconflict/theme-dracula";
import { InfoBarService } from '../../common/services/editor/info-bar/info-bar.service';
import { FilesModalService } from '../../common/services/editor/modals/files-modal.service';
import { LSPService } from '../../common/services/lsp.service';
import { TabsService } from '../../common/services/editor/tabs/tabs.service';
import { EditorsService } from '../../common/services/editor/editors.service';
import { NewtonEditorBase } from './newton-editor.base'; import { NewtonEditorBase } from './newton-editor.base';
import { NewtonFile } from '../../common/types/file.type';
import { ServiceMessage } from '../../common/types/service-message.type'; import { ServiceMessage } from '../../common/types/service-message.type';
@ -35,13 +30,7 @@ import { ServiceMessage } from '../../common/types/service-message.type';
export class NewtonEditorComponent extends NewtonEditorBase { export class NewtonEditorComponent extends NewtonEditorBase {
constructor( constructor() {
private infoBarService: InfoBarService,
private editorsService: EditorsService,
private lspService: LSPService,
private tabsService: TabsService,
private filesModalService: FilesModalService
) {
super(); super();
} }
@ -141,15 +130,6 @@ export class NewtonEditorComponent extends NewtonEditorBase {
}); });
} }
public updateInfoBar() {
this.infoBarService.setInfoBarFPath(this.activeFile?.path)
this.infoBarService.setInfoBarCursorPos(
this.editor.getCursorPosition()
);
this.infoBarService.setInfoBarFType(
this.editor.session.getMode()["$id"]
);
}
public newBuffer() { public newBuffer() {
let buffer = ace.createEditSession([""]); let buffer = ace.createEditSession([""]);
@ -158,36 +138,74 @@ export class NewtonEditorComponent extends NewtonEditorBase {
this.updateInfoBar(); this.updateInfoBar();
} }
public selectLeftEditor() { protected openFiles() {
let message = new ServiceMessage(); let startDir = "";
message.action = "select-left-editor"; if (this.activeFile) {
message.editorUUID = this.uuid; let pathParts = this.activeFile.path.split("/");
pathParts.pop();
startDir = pathParts.join( '/' );
}
this.editorsService.sendMessage(message); window.fs.openFiles(startDir);
} }
public selectRightEditor() { protected saveFile() {
let message = new ServiceMessage(); if (!this.activeFile) {
message.action = "select-right-editor"; this.saveFileAs();
message.editorUUID = this.uuid; return;
}
this.editorsService.sendMessage(message); const text = this.activeFile.session.getValue();
window.fs.saveFile(this.activeFile.path, text);
} }
public moveSessionLeft() { protected saveFileAs() {
let message = new ServiceMessage(); window.fs.saveFileAs().then((path: string) => {
message.action = "move-session-left"; if (!path) return;
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message); let file: NewtonFile = new File([""], path, {
type: "text/plain",
});
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);
});
});
} }
public moveSessionRight() { protected cutToBuffer() {
let message = new ServiceMessage(); if (this.timerId) { clearTimeout(this.timerId); }
message.action = "move-session-right";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message); 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);
} }
} }

View File

@ -1,4 +1,4 @@
import { Component, ChangeDetectorRef } from '@angular/core'; import { Component, ChangeDetectorRef, inject } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { CdkDrag, CdkDragDrop, CdkDropList, moveItemInArray } from '@angular/cdk/drag-drop'; import { CdkDrag, CdkDragDrop, CdkDropList, moveItemInArray } from '@angular/cdk/drag-drop';
import { Subject, takeUntil } from 'rxjs'; import { Subject, takeUntil } from 'rxjs';
@ -25,18 +25,18 @@ import { ServiceMessage } from '../../common/types/service-message.type';
} }
}) })
export class TabsComponent { export class TabsComponent {
private unsubscribe = new Subject<void>(); private unsubscribe: Subject<void> = new Subject();
private editorsService: EditorsService = inject(EditorsService);
private tabsService: TabsService = inject(TabsService);
private changeDetectorRef: ChangeDetectorRef = inject(ChangeDetectorRef);
activeTab!: string; activeTab!: string;
tabs: any[] = []; tabs: any[] = [];
newIndex: number = -1; newIndex: number = -1;
constructor( constructor() {
private editorsService: EditorsService,
private tabsService: TabsService,
private changeDetectorRef: ChangeDetectorRef
) {
} }
public ngAfterViewInit(): void { public ngAfterViewInit(): void {

View File

@ -28,7 +28,7 @@ declare global {
getFileContents: (arg0: any) => Promise<string>, getFileContents: (arg0: any) => Promise<string>,
openFiles: (arg0) => Promise<string>, openFiles: (arg0) => Promise<string>,
saveFile: (arg0: any, arg1: any) => Promise<string>, saveFile: (arg0: any, arg1: any) => Promise<string>,
saveFileAs: (arg0: any) => Promise<string>, saveFileAs: () => Promise<string>,
closeFile: (arg0: any) => Promise<string>, closeFile: (arg0: any) => Promise<string>,
getPathForFile: any, getPathForFile: any,
onLoadFiles: (arg0: any) => Promise<string>, onLoadFiles: (arg0: any) => Promise<string>,