assistant-note/elsrc/TopMenu.js

122 lines
4.0 KiB
JavaScript

const {app, Menu, shell, dialog} = require('electron')
const {stat, readdir} = require("fs/promises");
const pathOp = require('path')
const isMac = process.platform === 'darwin'
const readDirLocal=async (filePath) => {
const files = await readdir(filePath);
const fileStateList = []
const fileChildList = []
fileStateList.push({
"fileName": filePath,
"filePath": filePath,
"dirFlag": true,
"children": fileChildList
})
for (let i = 0; i < files.length; i++) {
const state = await stat(filePath + pathOp.sep + files[i]);
if (state.isDirectory()
|| files[i].endsWith(".md")
|| files[i].endsWith(".html")
|| files[i].endsWith(".lexical")) {
fileChildList.push({
'fileName': files[i],
"filePath": filePath + pathOp.sep + files[i],
'dirFlag': state.isDirectory(),
"children":[]
})
}
}
return fileStateList
}
exports.menuRebuild = (mainWindow,uploadUtil) => {
return template = [
{
label: '文件',
submenu: [
{
label: '打开目录',
click: async () => {
const {dialog} = require('electron')
dialog.showOpenDialog({
properties: ['openDirectory']
}).then(async result => {
// 不取消就发送目录
if (!result.canceled) {
console.log('result.filePaths', result.filePaths)
try {
readDirLocal(result.filePaths[0]).then(dirDirectory=>{
mainWindow.webContents.send('openDirectory', dirDirectory)
})
} catch (err) {
console.error(err);
}
}
}).catch(err => {
console.log(err)
})
}
},
{
label: '保存文件',
accelerator: 'Ctrl+S',
click: () => { // console.log('Electron rocks!')
}
},
{
label: '全部应用',
click: () => {
mainWindow.webContents.send('redirectUrl', '/GateWay')
}
},
isMac ? {role: 'close'} : {role: 'quit'},
]
},
{
label: '编辑',
submenu: [
{label: '撤销'},
{label: '取消撤销'},
{type: 'separator'},// 分割线
{label: '剪切'},
{label: '复制'},
{label: '粘贴'},
]
},
// { role: 'viewMenu' }
{
label: '视图',
submenu: [
{label: '界面布局'},
]
},
{
label: '帮助',
submenu: [
{
label: '了解更多',
click: async () => {
await shell.openExternal('http://www.huaruyu.com')
}
}
]
},
{
label: '云服务',
submenu: [
{
label: '同步上传',
click: () => {
uploadUtil.upLoadFileUtil(uploadUtil.getActiveFile())
}
},
{
label: '同步下载',
click: () => {
uploadUtil.downLoadFileUtil(uploadUtil.getActiveFile())
}
}
]
}
];
}