assistant-note/src/comment/TopMenu.js

168 lines
5.5 KiB
JavaScript
Raw Normal View History

2024-01-24 21:07:09 -05:00
const {app, Menu, shell, dialog} = require('electron')
const {stat, readdir} = require("fs/promises");
2024-01-05 03:24:35 -05:00
const isMac = process.platform === 'darwin'
2024-02-04 21:59:35 -05:00
const readDirLocal=async (filePath) => {
const files = await readdir(filePath);
const fileStateList = []
const fileChildList = []
fileStateList.push({
"fileName": filePath,
"filePath": filePath,
"dirFlag": true,
"childList": fileChildList
})
for (let i = 0; i < files.length; i++) {
const state = await stat(filePath + '/' + files[i]);
if (state.isDirectory()
|| files[i].endsWith(".md")
|| files[i].endsWith(".html")
|| files[i].endsWith(".lexical")) {
fileChildList.push({
'fileName': files[i],
"filePath": filePath + '/' + files[i],
'dirFlag': state.isDirectory(),
"childList":[]
})
}
}
return fileStateList
}
2024-01-24 21:07:09 -05:00
exports.menuRebuild = (mainWindow) => {
return template = [
// { role: 'appMenu' }
...(isMac
? [{
label: app.name,
submenu: [
2024-01-24 21:07:09 -05:00
{role: 'about'},
{type: 'separator'},
{role: 'services'},
{type: 'separator'},
{role: 'hide'},
{role: 'hideOthers'},
{role: 'unhide'},
{type: 'separator'},
{role: 'quit'}
]
}]
: []),
// { role: 'fileMenu' }
{
2024-01-27 22:48:48 -05:00
label: '文件',
2024-01-05 03:24:35 -05:00
submenu: [
2024-01-24 21:07:09 -05:00
{
label: '打开目录',
click: async () => {
2024-01-24 21:07:09 -05:00
const {dialog} = require('electron')
dialog.showOpenDialog({
properties: ['openDirectory']
}).then(async result => {
// 不取消就发送目录
if (!result.canceled) {
2024-01-24 21:07:09 -05:00
console.log('result.filePaths', result.filePaths)
try {
2024-02-04 21:59:35 -05:00
mainWindow.webContents.send('openDirectory', readDirLocal(result.filePaths[0]))
} catch (err) {
console.error(err);
}
}
}).catch(err => {
console.log(err)
})
}
},
2024-01-31 21:36:30 -05:00
{
label: '保存文件',
accelerator: 'Ctrl+S',
click: () => { // console.log('Electron rocks!')
}
},
2024-01-24 21:07:09 -05:00
{
label: '全部应用',
2024-01-24 01:50:27 -05:00
click: () => {
mainWindow.webContents.send('redirectUrl', '/GateWay')
}
},
2024-01-24 21:07:09 -05:00
isMac ? {role: 'close'} : {role: 'quit'},
]
},
// { role: 'editMenu' }
{
label: 'Edit',
submenu: [
2024-01-24 21:07:09 -05:00
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},// 分割线
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
...(isMac
? [
2024-01-24 21:07:09 -05:00
{role: 'pasteAndMatchStyle'},
{role: 'delete'},
{role: 'selectAll'},
{type: 'separator'},
{
label: 'Speech',
submenu: [
2024-01-24 21:07:09 -05:00
{role: 'startSpeaking'},
{role: 'stopSpeaking'}
]
}
]
: [
2024-01-24 21:07:09 -05:00
{role: 'delete'},
{type: 'separator'},
{role: 'selectAll'}
])
]
},
// { role: 'viewMenu' }
{
label: 'View',
submenu: [
2024-01-24 21:07:09 -05:00
{label: '界面布局'},
{role: 'reload'},
{role: 'forceReload'},
{role: 'toggleDevTools'},
{type: 'separator'},
{role: 'resetZoom'},
{role: 'zoomIn'},
{role: 'zoomOut'},
{type: 'separator'},
{role: 'togglefullscreen'}
2024-01-05 03:24:35 -05:00
]
},
// { role: 'windowMenu' }
{
label: 'Window',
submenu: [
2024-01-24 21:07:09 -05:00
{role: 'minimize'},
{role: 'zoom'},
...(isMac
? [
2024-01-24 21:07:09 -05:00
{type: 'separator'},
{role: 'front'},
{type: 'separator'},
{role: 'window'}
]
: [
2024-01-24 21:07:09 -05:00
{role: 'close'}
])
]
},
{
role: 'help',
submenu: [
{
label: 'Learn More',
click: async () => {
2024-01-24 21:07:09 -05:00
const {shell} = require('electron')
await shell.openExternal('http://www.huaruyu.com')
2024-01-05 03:24:35 -05:00
}
}
]
}
];
}