1. const {join, basename// 拿到文件名, extname// 文件拓展名, dirname// 文件的路径} = window.require('path')
    2. const {remote} = window.require('electron')
    3. remote.dialog.showOpenDialog({
    4. title: '选择导入的markdown文件',
    5. properties: ['openFile', 'multiSelections'],
    6. filters: [
    7. {
    8. name: 'Markdown files',
    9. extensions: ['md']
    10. }
    11. ]
    12. }).then(res => {
    13. if (Array.isArray(res.filePaths)) {
    14. // filter files had beed shown in fileList
    15. const filteredPaths = res.filePaths.filter(path => {
    16. const alreadyAddedFile = Object.values(files).find(file => {
    17. return file.path === path
    18. })
    19. return !alreadyAddedFile
    20. })
    21. // 转成需要格式的数组
    22. const importFilesArr = filteredPaths.map(path => {
    23. return {
    24. id: uuidv4(),
    25. title: basename(path, extname(path)), // 拿到文件名 没有后缀名
    26. path
    27. }
    28. })
    29. const newFiles = {...files, ...flattenArr(importFilesArr)}
    30. setFiles(newFiles)
    31. saveFilesToStore(newFiles)
    32. if (importFilesArr.length) {
    33. remote.dialog.showMessageBox({
    34. type:'info',
    35. message: `成功导入了${importFilesArr.length}个文件`,
    36. title: '提示'
    37. })
    38. }
    39. }
    40. })