Skip to content

Commit

Permalink
refactor(main): 删除无用接口,简单重构部分方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Blore-lzn authored and Brucezhuu committed May 1, 2023
1 parent 6869530 commit 75a78a4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 151 deletions.
47 changes: 7 additions & 40 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import installExtension, { VUEJS3_DEVTOOLS } from 'electron-devtools-installer'
import {
deleteFile, deleteFolder,
getFileFromUser,
getFolderFromUser, linkToFile, newFileFromDialog,
newFileFromSidebar, newFolderFromDialog, newFolderFromSidebar, renameFileOrFolder,
linkToFile, newFileFromDialog,
newFileFromSidebar, newFolder, renameFileOrFolder,
saveFile,
saveToTarget,
saveToPDFTarget,
readFile, paste, move
} from './main/filesystem/fileManipulate'
import {
addTag2File,
deleteTag,
findTags, getCiteInfo,
findTags,
getCiteInfo,
getLinks,
initFromFolder,
refresh,
sendTags
} from '@/main/filesystem/database'
refresh
} from './main/filesystem/database'
import EAU from './main/update'

import path from 'path'
Expand Down Expand Up @@ -197,8 +195,6 @@ app.on('ready', async () => {

ipcMain.handle('paste', async (e, userSelect, tarPath, projPath) => {
await paste(userSelect, tarPath, projPath)
// const win = BrowserWindow.fromWebContents(e.sender)
// win.webContents.send('refreshTree', newChildren)
})

ipcMain.handle('readFile', (e, filePath) => {
Expand Down Expand Up @@ -230,11 +226,6 @@ app.on('ready', async () => {
return await refresh(projPath)
})

ipcMain.handle('sendTags', async (e, projPath) => {
const tags = await sendTags(projPath)
return tags
})

ipcMain.handle('linkToFile', async (e, filePath, citingPath) => {
return await linkToFile(filePath, citingPath)
})
Expand All @@ -253,51 +244,27 @@ app.on('ready', async () => {
})

ipcMain.handle('newFolderFromSidebar', (e, filePath, fileName) => {
newFolderFromSidebar(filePath, fileName)
newFolder(filePath, fileName)
})

ipcMain.handle('newFileFromDialog', async (e, projPath) => {
const tree = await newFileFromDialog(projPath)
// console.log(fileObjs)
return tree
})

ipcMain.handle('newFolderFromDialog', async (e, projPath) => {
const tree = await newFolderFromDialog(projPath)
// console.log(fileObjs)
return tree
})

ipcMain.handle('delete_tag', async (e, filePath, tagName, folderPath) => {
deleteTag(tagName, folderPath, filePath)
})

ipcMain.handle('find_tags', async (e, tagName, folderPath) => {
const tags = await findTags(tagName, folderPath)
// console.log(fileObjs)
return tags
})

ipcMain.handle('addTagToFile', async (e, filePath, tagName, isNewTag, folderPath) => {
addTag2File(filePath, tagName, isNewTag, folderPath)
})

ipcMain.handle('newProject', async (e, data) => {
const relation = await initFromFolder(data)
return relation
})
ipcMain.handle('dialog:openFile', async (e) => {
const fileObjs = await getFileFromUser()
// console.log(fileObjs)
return fileObjs
})
ipcMain.handle('dialog:openFolder', async (e) => {
const folderObj = await getFolderFromUser()
console.log(folderObj.children.children[0])
console.log(folderObj.absolutePath)
ficusPath = folderObj.absolutePath
return folderObj
})
ipcMain.handle('save_file', (e, path, content) => {
saveFile(path, content)
})
Expand Down
12 changes: 5 additions & 7 deletions src/main/filesystem/database.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { app, dialog } = require('electron')
const path = require('path')
const { getTree } = require('./getFileTree')
const { getProject } = require('./getFileTree')
const linkManager = require('./linkManager')
const { default: accessor } = require('../accessor')

Expand Down Expand Up @@ -38,9 +38,7 @@ exports.updateFile = (filepath) => {
}

exports.refresh = async (projPath) => {
const pathSplit = projPath.split(path.sep)
const folderName = pathSplit[pathSplit.length - 1]
const tree = await getTree(projPath, folderName)
const tree = await getProject(projPath)
return tree.children
}

Expand All @@ -60,15 +58,15 @@ exports.initFromFolder = async () => {
console.log(result.filePaths[0])
const folderName = path.basename(result.filePaths[0])
accessor.menu.addRecentlyUsedDocument(folderName)
const tree = await getTree(result.filePaths[0], folderName)
const relation = {
const tree = await getProject(result.filePaths[0])
const projectStat = {
version: 1,
root: {
path: result.filePaths[0],
tree: tree.children,
folderName
}
}
return { relation, error: 0 }
return { relation: projectStat, error: 0 }
})
}
128 changes: 36 additions & 92 deletions src/main/filesystem/fileManipulate.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
const { app, dialog } = require('electron')
const fs = require('fs-extra')
const { getTree, makeMarkdownFileStat, makeFileStat } = require('./getFileTree')
const { getProject, makeMarkdownFileStat, makeFileStat } = require('./getFileTree')
const path = require('path')
const linkManager = require('./linkManager')
const { isValidMarkdownFilePath, isFileInDirectory, isValidFolderPath, isValidFilePath } = require('../helper/path')

/**
* 用于 showOpenDialog
* 指定应用程序应该能够打开那些类型的文件
*/
const markdownFilters = [
{ name: 'Markdown Files', extensions: ['md', 'markdown'] }
]

/**
* 跳转到引用(cite)
* @param {string} filePath
* @param {string} citingPath
* @returns
*/
exports.linkToFile = async (filePath, citingPath) => {
console.log(path.resolve(filePath, '..', citingPath))
return makeMarkdownFileStat(path.resolve(filePath, '..', citingPath))
}

Expand Down Expand Up @@ -47,82 +54,51 @@ exports.newFileFromDialog = async (projPath) => {
buttonLabel: '新建',
defaultPath: app.getPath('desktop'),
properties: ['showHiddenFiles'],
filters: [ // filters属性允许我们指定应用程序应该能够打开那些类型的文件,并禁止不符合我们标准的任何文件。
{ name: 'Text Files', extensions: ['txt'] },
{ name: 'Markdown Files', extensions: ['md', 'markdown'] },
{ name: 'images', extensions: ['jpg', 'png'] }
]
filters: markdownFilters
}).then(async (result) => {
if (result.canceled === true) {
return []
}

fs.createFileSync(result.filePath)
const file = makeFileStat(result.filePath)

const fd = fs.openSync(result.filePath, 'w')
fs.close(fd, (err) => {
if (err) { console.error('Failed to close file', err) }
})
if (isFileInDirectory(result.filePath, projPath)) {
return [{ file, in: true }]
} else {
return [{ file, in: false }]
}
})
}

// 新建文件2
exports.newFileFromSidebar = async (filePath, fileName) => {
const pos = path.join(filePath, fileName)
const fd = fs.openSync(pos, 'w')
fs.close(fd, (err) => {
if (err) { console.error('Failed to close file', err) }
})
exports.newFileFromSidebar = async (folderPath, fileName) => {
const filePath = path.resolve(folderPath, fileName)
fs.createFileSync(filePath)
}

// 新建文件夹1
// exports.newFolderFromDialog = async (projPath) => {
// return await dialog.showOpenDialog({
// buttonLabel: '新建',
// defaultPath: app.getPath('desktop'),
// properties: ['showHiddenFiles', 'createDirectory', 'openDirectory'],
// filters: [ // filters属性允许我们指定应用程序应该能够打开那些类型的文件,并禁止不符合我们标准的任何文件。
// { name: 'Text Files', extensions: ['txt'] },
// { name: 'Markdown Files', extensions: ['md', 'markdown'] },
// { name: 'images', extensions: ['jpg', 'png'] }
// ]
// }).then(async (result) => {
// if (result.canceled === true) {
// return []
// }
// if (isFileInDirectory(result.filePaths[0], projPath)) {
// const pathSplit = projPath.split(path.sep)
// const folderName = pathSplit[pathSplit.length - 1]
// const tree = await getTree(projPath, folderName)
// return tree.children
// } else {
// return []
// }
// })
// }

// 新建文件夹2
exports.newFolderFromSidebar = async (folderPath, folderName) => {
/**
* 新建文件夹
* @param {string} folderPath
* @param {string} folderName
*/
exports.newFolder = async (folderPath, folderName) => {
const basePath = path.join(folderPath, folderName)
fs.mkdir(basePath, { recursive: true }, err => {
if (err) console.log(`mkdir path: ${basePath} err`)
})
}

// 打开文件:
/**
* 打开一个或多个md文件
* @returns {[object]}
*/
exports.getFileFromUser = async () => {
return await dialog.showOpenDialog({
buttonLabel: '选择',
defaultPath: app.getPath('desktop'),
properties: ['multiSelections', 'createDirectory', 'openFile'],
filters: [ // filters属性允许我们指定应用程序应该能够打开那些类型的文件,并禁止不符合我们标准的任何文件。
{ name: 'Text Files', extensions: ['txt'] },
{ name: 'Markdown Files', extensions: ['md', 'markdown'] },
{ name: 'images', extensions: ['jpg', 'png'] }
]
filters: markdownFilters
}).then((result) => {
if (result.canceled === true) {
return []
Expand All @@ -138,41 +114,12 @@ exports.getFileFromUser = async () => {
return fileObjs
})
}
// 打开文件夹:
exports.getFolderFromUser = async () => {
return await dialog.showOpenDialog({
buttonLabel: '选择',
defaultPath: app.getPath('desktop'),
properties: ['createDirectory', 'openDirectory'],
filters: [ // filters属性允许我们指定应用程序应该能够打开那些类型的文件,并禁止不符合我们标准的任何文件。
{ name: 'Text Files', extensions: ['txt'] },
{ name: 'Markdown Files', extensions: ['md', 'markdown'] },
{ name: 'images', extensions: ['jpg', 'png'] }
]
}).then(async (result) => {
if (result.canceled === true) {
return []
}
const folderPath = result.filePaths[0]
const pathSplit = folderPath.split(path.sep)
const folderName = pathSplit[pathSplit.length - 1]
const tree = await getTree(folderPath, folderName)
const folder = {
name: folderName, // 文件名
curChild: -1, // 直接填充-1即可
path: folderPath, // 绝对路径
absolutePath: pathSplit, // 希望将绝对路径分割成数组
offset: -1, // 直接填充-1即可
children: tree.children, // 对于文件没有子节点则填充空数组,对于文件夹则嵌套文件
type: 'folder'
}
return folder
})
}

function readMarkdownFile (filePath) {
if (isValidMarkdownFilePath(filePath)) {
return fs.readFileSync(filePath).toString()
} else {
return undefined
}
}

Expand Down Expand Up @@ -218,20 +165,14 @@ exports.saveToTarget = async (fileContent, projPath) => {
buttonLabel: '保存',
defaultPath: app.getPath('desktop'),
properties: ['showHiddenFiles', 'createDirectory'],
filters: [ // filters属性允许我们指定应用程序应该能够打开那些类型的文件,并禁止不符合我们标准的任何文件。
{ name: 'Text Files', extensions: ['txt'] },
{ name: 'Markdown Files', extensions: ['md', 'markdown'] },
{ name: 'images', extensions: ['jpg', 'png'] }
]
filters: markdownFilters
}).then(async (result) => {
if (result.canceled === true) {
return []
} else {
fs.writeFileSync(result.filePath, fileContent)
if (isFileInDirectory(result.filePath, projPath)) {
const pathSplit = projPath.split(path.sep)
const folderName = pathSplit[pathSplit.length - 1]
const tree = await getTree(projPath, folderName)
const tree = await getProject(projPath)
return tree.children
} else {
return []
Expand All @@ -240,16 +181,19 @@ exports.saveToTarget = async (fileContent, projPath) => {
})
}

/**
* 导出为PDF
* @param {*} fileContent
*/
exports.saveToPDFTarget = (fileContent) => {
dialog.showSaveDialog({
buttonLabel: '保存',
defaultPath: app.getPath('desktop'),
properties: ['showHiddenFiles', 'createDirectory'],
filters: [ // filters属性允许我们指定应用程序应该能够打开那些类型的文件,并禁止不符合我们标准的任何文件。
filters: [
{ name: 'PDF Files', extensions: ['pdf'] }
]
}).then((result) => {
console.log(result)
fs.writeFileSync(result.filePath, fileContent)
})
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/filesystem/getFileTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ function makeFolderStat (dirPath) {
}
}

async function getTree (folderPath, folderName) {
/**
* 打开一个项目(会刷新links信息)
* @param {string} folderPath
* @returns
*/
async function getProject (folderPath) {
linkManager.resetValidFilePaths()
const dirPath = path.resolve(folderPath)
const fileJson = makeFolderStat(dirPath)
Expand All @@ -101,7 +106,7 @@ async function getTree (folderPath, folderName) {

module.exports = {
makeFileStat,
getTree,
makeFolderStat,
makeMarkdownFileStat
makeMarkdownFileStat,
getProject
}
Loading

0 comments on commit 75a78a4

Please sign in to comment.