我是第一次玩电子游戏。尝试创建文本编辑器
在渲染中,我正在发送一条消息,指示内容已更改并且需要保存:
document.getElementById('content').onkeyup = e => {
ipcRenderer.send('SAVE_NEEDED', {
content: e.target.innerHTML,
fileDir
})
}
然后ipcMain
没问题。在菜单上,我有这个:
{
label: 'Save',
click: _ => {
saveFile(message)
// trying:
// ipcMain.send('SAVED', 'File Saved')
},
accelerator: 'cmd+S', // shortcut
}
使用户知道文件已经拥有。但这似乎不起作用。还有其他方法吗? 我本以为“保存”是一个预先创建的角色(某种)
答案 0 :(得分:1)
要将消息发送回渲染器,您可以使用function swipe() {
var largeImage = document.getElementById('myimage');
largeImage.style.display = 'block';
largeImage.style.width=200+"px";
largeImage.style.height=200+"px";
var url=largeImage.getAttribute('src');
window.open(url,'Image','width=largeImage.stylewidth,height=largeImage.style.height,resizable=1');
}
答案 1 :(得分:1)
或者-当您要响应从渲染器进程收到的事件时,可以执行以下操作:
ipcMain.on("eventFromRenderer", (event) => {
event.sender.send("eventFromMain", someReply);
}
答案 2 :(得分:1)
这是我修改的内容(使用 contextBridge
的新方法),我的用途只是让 menuItem 在我的 React 代码中调用导航事件:
// preload.js
const exposedAPI = {
// `(customData: string) => void` is just the typing here
onMenuNav: (cb: (customData: string) => void) => {
// Deliberately strip event as it includes `sender` (note: Not sure about that, I partly pasted it from somewhere)
// Note: The first argument is always event, but you can have as many arguments as you like, one is enough for me.
ipcRenderer.on('your-event', (event, customData) => cb(customData));
}
};
contextBridge.exposeInMainWorld("electron", exposedAPI);
// typing for curious peoples
onMenuNav(cb: ((customData: string) => void)): void;
// renderer.tsx
// Call it in the renderer process, in my case it is called once at the end of renderer.tsx.
window.electron.onMenuNav(customData => {
console.log(customData); // 'something'
});
// in main process
const customData = 'something';
mainWindow.webContents.send('your-event', customData);