// package.json
build: {
appId: 'com.gitee.xxx',
fileAssociations: {
ext: 'md',
role: 'Viewer',
},
}
// background.js
let openFilePath = '';
app.on('open-file', (event, path) => {
// 当用户想要在应用中打开一个文件时发出。
// 事件通常在应用已经打开,并且系统要再次使用该应用打开文件时发出。
// 也会在一个文件被拖到 dock 并且还没有运行的时候发出。
event.preventDefault();
if (win) SendMsgToRenderer(win.webContents, 'user-open-file', path);
else openFilePath = path;
});
app.on('ready', async () => {
win = createWindow();
openFilePath &&
SendMsgToRenderer(win.webContents, 'user-open-file', openFilePath);
});
export function SendMsgToRenderer<T extends keyof NoticeChannel>(
webContents: Electron.WebContents,
cmd: T,
params: NoticeChannel[T],
): void {
if (webContents.isLoading())
webContents.on('did-finish-load', () => {
webContents.send(cmd, params);
});
else webContents.send(cmd, params);
}
// 渲染进程
ipcRenderer.on('user-open-file', (e: unknown, newfilepath: string) => {
this.OpenDB(newfilepath);
});
参考: https://blog.csdn.net/WuQingLaoXingXing/article/details/105841278
网友评论