酷云的csdn 2020-06-16
//在文件管理器中打开 const { ..., shell } = require(‘electron‘); const showFile = () => { if (!filePath) { return alert(‘This file has not been saved to the file system.‘); } shell.showItemInFolder(filePath); }; //使用默认程序打开 const openInDefaultApplication = () => { if (!filePath) { return alert(‘This file has not been saved to the file system.‘); } //electron-9.x 已经不存在了 shell.openItem(fullPath) };
{ type: ‘separator‘ }, { label: ‘Show File‘, enabled: hasFilePath, click(item, focusedWindow) { if (!focusedWindow) { return dialog.showErrorBox( ‘Cannot Show File\‘s Location‘, ‘There is currently no active document show.‘ ); } focusedWindow.webContents.send(‘show-file‘); }, }, { label: ‘Open in Default Application‘, enabled: hasFilePath, click(item, focusedWindow) { if (!focusedWindow) { return dialog.showErrorBox( ‘Cannot Open File in Default Application‘, ‘There is currently no active document to open.‘ ); } focusedWindow.webContents.send(‘open-in-default‘); }, },
ipcRenderer.on(‘show-file‘, showFile); ipcRenderer.on(‘open-in-default‘, openInDefaultApplication);
//上下文菜单 const markdownContextMenu = Menu.buildFromTemplate([ ... { label: ‘Show File in Folder‘, click: showFile, enabled: !!filePath }, { label: ‘Open in Default‘, click: openInDefaultApplication, enabled: !!filePath }, { type: ‘separator‘ }, ... ]); markdownView.addEventListener(‘contextmenu‘, (event) => { event.preventDefault(); markdownContextMenu.popup(); });