From d179e1c42e73b6d502666f523c0d0f751c2bde4c Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Wed, 10 Dec 2025 06:56:24 +0000 Subject: [PATCH] WIP to handle outside of electron build --- src/app/app.component.ts | 56 ++++++++++++++++++- .../editor/lsp-manager/lsp-manager.service.ts | 2 +- src/app/common/services/files.service.ts | 6 +- src/app/editor/code-view/view.base.ts | 12 ++-- src/app/editor/editors.component.ts | 14 ++--- .../lsp-manager/lsp-manager.component.ts | 2 +- src/polyfills.ts | 36 ++++++------ 7 files changed, 91 insertions(+), 37 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index acbaa81..0fb5a71 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -33,12 +33,64 @@ export class AppComponent { protected ws: WebsocketService = inject(WebsocketService); - constructor() {} + constructor() { + this.checkIfNotElectronMode(); + } - ngOnInit() { + + ngOnInit() {} + + checkIfNotElectronMode() { + if ( + window.electron || + window.main || + window.fs + ) { return; } + + this.setupWebsocket(); + this.setupWindowBindings(); + } + + setupWindowBindings() { + window.electron ??= { + node: () => { return "" }, + chrome: () => { return "" }, + electron: () => { return "" }, + }; + + window.main ??= { + onMenuActions: () => {}, + onTerminalActions: () => {}, + quit: () => {}, + toggleFullScreen: () => {}, + }; + + window.fs ??= { + getLspConfigData: () => { + return new Promise((resolve, reject) => { + resolve("{}"); + }); + }, + getFileContents: () => {}, + openFiles: () => {}, + saveFile: () => {}, + saveFileAs: () => {}, + chooseFolder: () => {}, + closeFile: () => {}, + getPathForFile: () => {}, + onLoadFiles: () => {}, + onUpdateFilePath: () => {}, + onSavedFile: () => {}, + onChangedFile: () => {}, + onDeletedFile: () => {}, + }; + } + + setupWebsocket() { // TODO: Set with dynamic address and port this.ws.connect('ws://localhost:7272').subscribe(msg => { console.log(msg); + console.log(window.fs); // this.ws.send("{ 'text': 'Hello server!' }"); }); } diff --git a/src/app/common/services/editor/lsp-manager/lsp-manager.service.ts b/src/app/common/services/editor/lsp-manager/lsp-manager.service.ts index a062b08..8590c7a 100644 --- a/src/app/common/services/editor/lsp-manager/lsp-manager.service.ts +++ b/src/app/common/services/editor/lsp-manager/lsp-manager.service.ts @@ -42,7 +42,7 @@ export class LspManagerService { } private getLspConfigData(): Promise { - return window.fs.getLspConfigData(); + return window?.fs.getLspConfigData(); } private parseAndReturnLSPConfigData(): {} { diff --git a/src/app/common/services/files.service.ts b/src/app/common/services/files.service.ts index 50ecccb..b9926bc 100644 --- a/src/app/common/services/files.service.ts +++ b/src/app/common/services/files.service.ts @@ -55,7 +55,7 @@ export class FilesService { public unset(file: NewtonFile) { file.session.destroy(); - window.fs.closeFile(file.path); + window?.fs.closeFile(file.path); this.files.delete(file.path); } @@ -76,7 +76,7 @@ export class FilesService { ): Promise { for (let i = 0; i < files.length; i++) { const file = files[i]; - const path = window.fs.getPathForFile(file); + const path = window?.fs.getPathForFile(file); if (!file || !path) continue; if ( this.files.get(path) ) continue; @@ -101,7 +101,7 @@ export class FilesService { file.hash = btoa(file.path); if (loadFileContents) - data = await window.fs.getFileContents(file.path); + data = await window?.fs.getFileContents(file.path); file.session = new EditSession(data); file.session["id"] = path; diff --git a/src/app/editor/code-view/view.base.ts b/src/app/editor/code-view/view.base.ts index 4e76d22..82b5834 100644 --- a/src/app/editor/code-view/view.base.ts +++ b/src/app/editor/code-view/view.base.ts @@ -187,7 +187,7 @@ export class CodeViewBase { } public toggleFullScreen() { - window.main.toggleFullScreen(); + window?.main.toggleFullScreen(); } public setAsReadOnly() { @@ -300,7 +300,7 @@ export class CodeViewBase { startDir = pathParts.join( '/' ); } - window.fs.openFiles(startDir); + window?.fs.openFiles(startDir); } protected saveFile() { @@ -312,18 +312,18 @@ export class CodeViewBase { } const text = this.activeFile.session.getValue(); - window.fs.saveFile(this.activeFile.path, text); + window?.fs.saveFile(this.activeFile.path, text); this.activeFile.session.getUndoManager().markClean(); } protected saveFileAs() { - window.fs.saveFileAs().then((path: string) => { + 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); + window?.fs.saveFile(path, text); this.filesService.addFile( path, file, @@ -364,6 +364,6 @@ export class CodeViewBase { } private quit() { - window.main.quit(); + window?.main.quit(); } } diff --git a/src/app/editor/editors.component.ts b/src/app/editor/editors.component.ts index 4ca9bc6..20943ff 100644 --- a/src/app/editor/editors.component.ts +++ b/src/app/editor/editors.component.ts @@ -76,7 +76,7 @@ export class EditorsComponent { } private loadMainSubscribers() { - window.main.onMenuActions(async (action: string) => { + window?.main.onMenuActions(async (action: string) => { let editorComponent = this.editorsService.getActiveEditorComponent(); let editor = editorComponent.editor; @@ -112,7 +112,7 @@ export class EditorsComponent { case "show-about": break; case "quit": - window.main.quit(); + window?.main.quit(); break; default: editor.execCommand(action); @@ -120,7 +120,7 @@ export class EditorsComponent { } }); - window.fs.onLoadFiles(async (paths: []) => { + window?.fs.onLoadFiles(async (paths: []) => { for (let i = 0; i < paths.length; i++) { let file = new File([], "") as NewtonFile; @@ -135,7 +135,7 @@ export class EditorsComponent { this.editorsService.setSession(file); }); - window.fs.onChangedFile(async (path: string, data: string) => { + window?.fs.onChangedFile(async (path: string, data: string) => { let file = this.filesService.get(path); file.session.setValue(data); @@ -147,7 +147,7 @@ export class EditorsComponent { this.tabsService.sendMessage(message); }); - window.fs.onDeletedFile(async (path: string) => { + window?.fs.onDeletedFile(async (path: string) => { let message = new ServiceMessage(); message.action = "file-deleted"; message.filePath = path; @@ -156,7 +156,7 @@ export class EditorsComponent { this.filesService.sendMessage(message); }); - window.fs.onSavedFile(async (path: string) => { + window?.fs.onSavedFile(async (path: string) => { let message = new ServiceMessage(); message.action = "file-saved"; message.filePath = path; @@ -164,7 +164,7 @@ export class EditorsComponent { this.tabsService.sendMessage(message); }); - window.fs.onUpdateFilePath(async (path: string) => { + window?.fs.onUpdateFilePath(async (path: string) => { console.log("TODO (onUpdateFilePath) :", path); // this.tabsService.sendMessage(message); // this.filesService.sendMessage(message); diff --git a/src/app/editor/lsp-manager/lsp-manager.component.ts b/src/app/editor/lsp-manager/lsp-manager.component.ts index e8360d2..2d71796 100644 --- a/src/app/editor/lsp-manager/lsp-manager.component.ts +++ b/src/app/editor/lsp-manager/lsp-manager.component.ts @@ -134,7 +134,7 @@ export class LspManagerComponent { } public setWorkspaceFolder() { - window.fs.chooseFolder().then((folder: string) => { + window?.fs.chooseFolder().then((folder: string) => { if (!folder) return; this.lspManagerService.workspaceFolder = folder; diff --git a/src/polyfills.ts b/src/polyfills.ts index 1b2e90b..238c839 100644 --- a/src/polyfills.ts +++ b/src/polyfills.ts @@ -11,33 +11,35 @@ import 'zone.js'; // Included with Angular CLI. +// Note: Is set to 'any' b/c of desire to set 'render' +// side if running outside of electron mode. declare global { interface Window { electron: { - node: () => Promise, - chrome: () => Promise, - electron: () => Promise, + node: any, + chrome: any, + electron: any, }, main: { - onMenuActions: (arg0: any) => Promise, - onTerminalActions: (arg0: any) => Promise, + onMenuActions: any, + onTerminalActions: any, quit: any, toggleFullScreen: any, }, fs: { - getLspConfigData: () => Promise, - getFileContents: (arg0: any) => Promise, - openFiles: (arg0) => Promise, - saveFile: (arg0: any, arg1: any) => Promise, - saveFileAs: () => Promise, - chooseFolder: () => Promise, - closeFile: (arg0: any) => Promise, + getLspConfigData: any, + getFileContents: any, + openFiles: any, + saveFile: any, + saveFileAs: any, + chooseFolder: any, + closeFile: any, getPathForFile: any, - onLoadFiles: (arg0: any) => Promise, - onUpdateFilePath: (arg0: any) => Promise, - onSavedFile: (arg0: any) => Promise, - onChangedFile: (arg0: any) => Promise, - onDeletedFile: (arg0: any) => Promise, + onLoadFiles: any, + onUpdateFilePath: any, + onSavedFile: any, + onChangedFile: any, + onDeletedFile: any, } } } \ No newline at end of file