Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #119 from setho246/108-Create-web-ipc-shim
Browse files Browse the repository at this point in the history
108 create web ipc shim
  • Loading branch information
setho246 authored Sep 27, 2021
2 parents e38adfb + aebb6a0 commit d072e69
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
187 changes: 187 additions & 0 deletions src/controllers/WebIPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
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
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 =>
{
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);
}

/**
* Web has no differentiation between save and save as.
*
* @returns {void}
*/
saveAsEmitter(): void
{
this.saveEmitter();
}

/**
* Emits an event containing the contents of the editor, instructing the main process to perform the Save 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
{
console.log(filePath);
document.getElementById("file-input").addEventListener("change", this.readFile.bind(this), false);
document.getElementById("file-input").click();
}

readFile(e : Event) : void
{

const target = e.target as HTMLInputElement;
const file = 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);
}

}
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
</div>

</div>
<input id="file-input" type="file" name="name" style="display: none;" />

<!-- You can also require other files to run in this process -->
</body>
Expand Down

0 comments on commit d072e69

Please sign in to comment.