From 6ae0de9aa4b656737fe5227f4c6d71c74e7f23c4 Mon Sep 17 00:00:00 2001 From: Seth Hilder Date: Sun, 26 Sep 2021 13:44:01 +1000 Subject: [PATCH 1/3] #108 - Create alternative file --- src/controllers/WebIPC.ts | 159 ++++++++++++++++++++++++++++++++++++++ src/index.html | 1 + 2 files changed, 160 insertions(+) create mode 100644 src/controllers/WebIPC.ts diff --git a/src/controllers/WebIPC.ts b/src/controllers/WebIPC.ts new file mode 100644 index 0000000..4e4ec23 --- /dev/null +++ b/src/controllers/WebIPC.ts @@ -0,0 +1,159 @@ +import { ipcRenderer } from "electron"; +import { YarnFile } from "../models/YarnFile"; +import { YarnFileManager } from "../models/YarnFileManager"; +import { EditorController } from "./EditorController"; +import { setActiveFile, addFileToDisplay } from "./DomHelpers"; + +export class WebIPC +{ + yarnFileManager: YarnFileManager + editor: EditorController + + constructor(fileManager: YarnFileManager, editor: EditorController) + { + this.yarnFileManager = fileManager; + this.editor = editor; + + ipcRenderer.on("openFile", (event, files: { path: string, contents: string, name: string }[]) => + { + files.forEach(openedFileDetails => + { + if (!openedFileDetails.name) + { + openedFileDetails.name = "New File"; + } + + const openedFile = new YarnFile(openedFileDetails.path, openedFileDetails.contents, openedFileDetails.name, Date.now()); + this.yarnFileManager.addToFiles(openedFile); + this.yarnFileManager.setCurrentOpenYarnFile(openedFile.getUniqueIdentifier()); + addFileToDisplay(openedFile); + editor.setValue(this.yarnFileManager.getCurrentOpenFile().getContents()); + editor.setReadOnly(false); + + }); + }); + + + ipcRenderer.on("fileSaveResponse", (event, response, filePath, fileName) => + { + if (response) + { + if (filePath) + { + this.yarnFileManager.getCurrentOpenFile().setFilePath(filePath); + } + + if (fileName) + { + this.yarnFileManager.getCurrentOpenFile().setName(fileName); + + const workingDetailDiv = document.getElementById(this.yarnFileManager.getCurrentOpenFile().getUniqueIdentifier().toString()); + + if (workingDetailDiv) + { + workingDetailDiv.children[0].innerHTML = this.yarnFileManager.getCurrentOpenFile().getName(); + } + } + + this.yarnFileManager.getCurrentOpenFile().fileSaved(); + + } + else + { + console.error("File save error occurred"); + } + }); + + ipcRenderer.on("setOpenFile", (event, uid) => + { + this.yarnFileManager.setCurrentOpenYarnFile(uid); + editor.setValue(this.yarnFileManager.getCurrentOpenFile().getContents()); + editor.setReadOnly(false); + setActiveFile(uid); + }); + + ipcRenderer.on("setFileSaved", (event, uid) => + { + this.yarnFileManager.getYarnFile(uid).fileSaved(); + const workingDetailDiv = document.getElementById(uid.toString()); + + if (workingDetailDiv) + { + workingDetailDiv.children[0].innerHTML = this.yarnFileManager.getYarnFile(uid).getName(); + } + }); + } + + + /** + * Creates a new file and shows it in the display. + * + * @returns {void} + */ + createNewFile() : void + { + // this.yarnFileManager.createEmptyFile(); + addFileToDisplay(this.yarnFileManager.createEmptyFile()); + this.editor.setValue(this.yarnFileManager.getCurrentOpenFile().getContents()); + this.editor.setReadOnly(false); + } + + /** + * Emits an event containing the contents of the editor, instructing the main process to perform the Save As function. + * + * @returns {void} + */ + saveAsEmitter() : void + { + ipcRenderer.send("fileSaveToMain", null, this.yarnFileManager.getCurrentOpenFile().getContents()); + } + + /** + * Emits an event containing the contents of the editor, instructing the main process to perform the Save As function. + * + * @returns {void} + */ + saveEmitter() : void + { + ipcRenderer.send("fileSaveToMain", this.yarnFileManager.getCurrentOpenFile().getPath(), this.yarnFileManager.getCurrentOpenFile().getContents()); + } + + /** + * Creates a list of unsaved files open in the editor and sends the info to main. + * + * @returns {void} + */ + getUnsavedFiles() : void + { + const unsaved: string[][] = [[], [], [], []]; + + this.yarnFileManager.getFiles().forEach((value) => + { + console.log(value); + if (!value.getSaved()) + { + unsaved[0].push(value.getUniqueIdentifier().toString()); + unsaved[1].push(value.getName()); + unsaved[2].push(value.getPath()); + unsaved[3].push(value.getContents()); + } + }); + console.log(unsaved); + ipcRenderer.send("returnUnsavedFiles", unsaved); + } + + + /** + * Emits an event to request that main opens a file. + * + * @param {string} filepath file path if available + * @returns {void} + */ + openFileEmitter(filepath?: string[]) : void + { + document.getElementById('file-input').addEventListener('change', readSingleFile, false); + document.getElementById('file-input').click(); + + } + +} diff --git a/src/index.html b/src/index.html index b0bcce6..2f55735 100644 --- a/src/index.html +++ b/src/index.html @@ -168,6 +168,7 @@ + From 0644e9e27ba9d46d715366fcdc3f2b20af526353 Mon Sep 17 00:00:00 2001 From: Seth Hilder Date: Sun, 26 Sep 2021 21:40:16 +1000 Subject: [PATCH 2/3] #108 - Replace opening files to use web apis --- src/controllers/WebIPC.ts | 42 ++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/controllers/WebIPC.ts b/src/controllers/WebIPC.ts index 4e4ec23..4db9fcf 100644 --- a/src/controllers/WebIPC.ts +++ b/src/controllers/WebIPC.ts @@ -8,12 +8,15 @@ export class WebIPC { yarnFileManager: YarnFileManager editor: EditorController + fileOpenCount : number constructor(fileManager: YarnFileManager, editor: EditorController) { this.yarnFileManager = fileManager; this.editor = editor; + this.fileOpenCount = 0; + ipcRenderer.on("openFile", (event, files: { path: string, contents: string, name: string }[]) => { files.forEach(openedFileDetails => @@ -90,7 +93,7 @@ export class WebIPC * * @returns {void} */ - createNewFile() : void + createNewFile(): void { // this.yarnFileManager.createEmptyFile(); addFileToDisplay(this.yarnFileManager.createEmptyFile()); @@ -103,7 +106,7 @@ export class WebIPC * * @returns {void} */ - saveAsEmitter() : void + saveAsEmitter(): void { ipcRenderer.send("fileSaveToMain", null, this.yarnFileManager.getCurrentOpenFile().getContents()); } @@ -113,7 +116,7 @@ export class WebIPC * * @returns {void} */ - saveEmitter() : void + saveEmitter(): void { ipcRenderer.send("fileSaveToMain", this.yarnFileManager.getCurrentOpenFile().getPath(), this.yarnFileManager.getCurrentOpenFile().getContents()); } @@ -123,7 +126,7 @@ export class WebIPC * * @returns {void} */ - getUnsavedFiles() : void + getUnsavedFiles(): void { const unsaved: string[][] = [[], [], [], []]; @@ -149,11 +152,36 @@ export class WebIPC * @param {string} filepath file path if available * @returns {void} */ - openFileEmitter(filepath?: string[]) : void + openFileEmitter(filepath?: string[]): void + { + const _self = this; + + document.getElementById("file-input").addEventListener("change", this.readFile.bind(_self), false); + document.getElementById("file-input").click(); + } + + readFile(e : any) { - document.getElementById('file-input').addEventListener('change', readSingleFile, false); - document.getElementById('file-input').click(); + + const file = e.target.files[0]; + if (!file) + { + return; + } + + const reader = new FileReader(); + reader.onload = (e) => + { + const contents = e.target.result; + const newFile = new YarnFile(file.path, contents.toString(), file.name, Date.now()); + this.yarnFileManager.addToFiles(newFile); + this.yarnFileManager.setCurrentOpenYarnFile(newFile.getUniqueIdentifier()); + addFileToDisplay(newFile); + this.editor.setValue(this.yarnFileManager.getCurrentOpenFile().getContents()); + this.editor.setReadOnly(false); + }; + reader.readAsText(file); } } From aebb6a0b96373016e7e39bb5e8f66c774fe1b11d Mon Sep 17 00:00:00 2001 From: Seth Hilder Date: Mon, 27 Sep 2021 09:39:51 +1000 Subject: [PATCH 3/3] #108 - Fix tests --- src/controllers/WebIPC.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/controllers/WebIPC.ts b/src/controllers/WebIPC.ts index 4db9fcf..a021824 100644 --- a/src/controllers/WebIPC.ts +++ b/src/controllers/WebIPC.ts @@ -102,17 +102,17 @@ export class WebIPC } /** - * Emits an event containing the contents of the editor, instructing the main process to perform the Save As function. + * Web has no differentiation between save and save as. * * @returns {void} */ saveAsEmitter(): void { - ipcRenderer.send("fileSaveToMain", null, this.yarnFileManager.getCurrentOpenFile().getContents()); + this.saveEmitter(); } /** - * Emits an event containing the contents of the editor, instructing the main process to perform the Save As function. + * Emits an event containing the contents of the editor, instructing the main process to perform the Save function. * * @returns {void} */ @@ -149,21 +149,21 @@ export class WebIPC /** * Emits an event to request that main opens a file. * - * @param {string} filepath file path if available + * @param {string} filePath file path if available * @returns {void} */ - openFileEmitter(filepath?: string[]): void + openFileEmitter(filePath?: string[]): void { - const _self = this; - - document.getElementById("file-input").addEventListener("change", this.readFile.bind(_self), false); + console.log(filePath); + document.getElementById("file-input").addEventListener("change", this.readFile.bind(this), false); document.getElementById("file-input").click(); } - readFile(e : any) + readFile(e : Event) : void { - const file = e.target.files[0]; + const target = e.target as HTMLInputElement; + const file = target.files[0]; if (!file) { return;