I'm playing with electron for the first time. Trying to create a text editor
In render I'm sending a message to indicated the content has changed and needs saving:
document.getElementById('content').onkeyup = e => { ipcRenderer.send('SAVE_NEEDED', { content: e.target.innerHTML, fileDir }) }
Then ipcMain
receives it no problem. On the menu I have this:
{ label: 'Save', click: _ => { saveFile(message) // trying: // ipcMain.send('SAVED', 'File Saved') }, accelerator: 'cmd+S', // shortcut }
So that the user knows the files has have. But that doesn't seem to work. Is there any other way to do this? I would have thought "save" would be a pre-created role (sort of)
To send a message back to the renderer you would use: win. webContents. send('asynchronous-message', {'SAVED': 'File Saved'});
Electron uses multi-process architecture to manage the application state and user interface. The main process controls the state of the application while the renderer process controls the user interface. Electron uses GPU process to perform graphically intensive tasks but it's optional.
In Electron, processes communicate by passing messages through developer-defined "channels" with the ipcMain and ipcRenderer modules. These channels are arbitrary (you can name them anything you want) and bidirectional (you can use the same channel name for both modules).
To send a message back to the renderer you would use:
win.webContents.send('asynchronous-message', {'SAVED': 'File Saved'});
And receive it like this:
ipcRenderer.on('asynchronous-message', function (evt, message) { console.log(message); // Returns: {'SAVED': 'File Saved'} });
Where asynchronous-message
is simply the channel you're sending it to. It can literally be anything.
webContents.send
Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With