From 0c7b548f36872c1a70c39874ec80af8e3da31c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Wed, 28 Aug 2024 10:53:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=EF=BC=9A=E9=99=84?= =?UTF-8?q?=E4=BB=B6=E6=94=AF=E6=8C=81=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/electron/fileHandle.js | 55 ++++++++---- web/src/electron/preload.js | 7 +- web/src/lang/en_us.js | 4 +- web/src/lang/zh_cn.js | 4 +- .../pages/Edit/components/NodeAttachment.vue | 16 +++- .../Edit/components/ToolbarNodeBtnList.vue | 89 ++++++++++++++++++- 6 files changed, 145 insertions(+), 30 deletions(-) diff --git a/web/src/electron/fileHandle.js b/web/src/electron/fileHandle.js index 8739b04c..d4b99e28 100644 --- a/web/src/electron/fileHandle.js +++ b/web/src/electron/fileHandle.js @@ -128,25 +128,34 @@ export const bindFileHandleEvent = ({ mainWindow }) => { }) // 选择本地文件 - ipcMain.handle('selectFile', (event, openDirectory = false) => { - const properties = [] - if (openDirectory) { - properties.push('openDirectory') - } - const res = dialog.showOpenDialogSync({ - title: '选择', - properties - }) - if (res && res[0]) { - console.log(111, res[0]) - return { - file: res[0], - name: path.basename(res[0]) + ipcMain.handle( + 'selectFile', + (event, openDirectory = false, relativePath = '') => { + const properties = ['openFile'] + // 选择目录 + if (openDirectory) { + properties.push('openDirectory') + } + const res = dialog.showOpenDialogSync({ + title: '选择', + properties + }) + if (res && res[0]) { + const name = path.basename(res[0]) + let file = res[0] + if (relativePath) { + // 如果传递了路径,那么返回相对路径 + file = path.relative(relativePath, res[0]) + } + return { + file, + name + } + } else { + return null } - } else { - return null } - }) + ) // 获取文件内容 ipcMain.handle('getFileContent', (event, id) => { @@ -165,6 +174,11 @@ export const bindFileHandleEvent = ({ mainWindow }) => { }) }) + // 获取文件路径 + ipcMain.handle('getFilePath', (event, id) => { + return idToFilePath[id] + }) + // 重命名文件 ipcMain.handle('rename', (event, id, name) => { return new Promise(resolve => { @@ -227,7 +241,7 @@ export const bindFileHandleEvent = ({ mainWindow }) => { const exist = fs.existsSync(file) if (!exist) { removeFileInRecent(file).then(() => { - notifyMainWindowRefreshRecentFileList() + notifyMainWindowRefreshRecentFileList() }) return '文件不存在' } @@ -235,7 +249,10 @@ export const bindFileHandleEvent = ({ mainWindow }) => { }) // 打开指定文件 - ipcMain.handle('openPath', (event, file) => { + ipcMain.handle('openPath', (event, file, relativePath = '') => { + if (!path.isAbsolute(file) && relativePath) { + file = path.resolve(relativePath, file) + } const exist = fs.existsSync(file) if (!exist) { return '文件不存在' diff --git a/web/src/electron/preload.js b/web/src/electron/preload.js index 3738af34..23ce7424 100644 --- a/web/src/electron/preload.js +++ b/web/src/electron/preload.js @@ -11,6 +11,7 @@ contextBridge.exposeInMainWorld('electronAPI', { destroy: () => ipcRenderer.send('destroy'), create: id => ipcRenderer.send('create', id), getFileContent: id => ipcRenderer.invoke('getFileContent', id), + getFilePath: id => ipcRenderer.invoke('getFilePath', id), save: (id, data, fileName) => ipcRenderer.invoke('save', id, data, fileName), rename: (id, name) => ipcRenderer.invoke('rename', id, name), openUrl: url => ipcRenderer.send('openUrl', url), @@ -25,8 +26,10 @@ contextBridge.exposeInMainWorld('electronAPI', { openFile: file => ipcRenderer.invoke('openFile', file), selectOpenFile: () => ipcRenderer.send('selectOpenFile'), copyFile: file => ipcRenderer.invoke('copyFile', file), - selectFile: openDirectory => ipcRenderer.invoke('selectFile', openDirectory), - openPath: path => ipcRenderer.invoke('openPath', path), + selectFile: (openDirectory, relativePath) => + ipcRenderer.invoke('selectFile', openDirectory, relativePath), + openPath: (path, relativePath) => + ipcRenderer.invoke('openPath', path, relativePath), saveClientConfig: config => ipcRenderer.invoke('saveClientConfig', config), getClientConfig: () => ipcRenderer.invoke('getClientConfig') }) diff --git a/web/src/lang/en_us.js b/web/src/lang/en_us.js index e2636fa3..e97e07ab 100644 --- a/web/src/lang/en_us.js +++ b/web/src/lang/en_us.js @@ -295,7 +295,9 @@ export default { newFileTip: 'Please export the currently edited file before creating a new one, Beware of content loss', openFileTip: - 'Please export the currently edited file before opening it, Beware of content loss' + 'Please export the currently edited file before opening it, Beware of content loss', + isRelative: 'Relative path', + selectFolder: 'Select folder' }, edit: { newFeatureNoticeTitle: 'New feature reminder', diff --git a/web/src/lang/zh_cn.js b/web/src/lang/zh_cn.js index 328e4f27..9923c0a7 100644 --- a/web/src/lang/zh_cn.js +++ b/web/src/lang/zh_cn.js @@ -289,7 +289,9 @@ export default { creatingTip: '正在创建文件', directory: '目录', newFileTip: '新建文件前请先导出当前编辑的文件,谨防内容丢失', - openFileTip: '打开文件前请先导出当前编辑的文件,谨防内容丢失' + openFileTip: '打开文件前请先导出当前编辑的文件,谨防内容丢失', + isRelative: '相对路径', + selectFolder: '选择文件夹' }, edit: { newFeatureNoticeTitle: '新特性提醒', diff --git a/web/src/pages/Edit/components/NodeAttachment.vue b/web/src/pages/Edit/components/NodeAttachment.vue index ca2ba575..d18d6721 100644 --- a/web/src/pages/Edit/components/NodeAttachment.vue +++ b/web/src/pages/Edit/components/NodeAttachment.vue @@ -66,11 +66,18 @@ export default { }, methods: { // 选择附件 - async onSelectAttachment(activeNodes) { + async onSelectAttachment(activeNodes, config = {}) { // activeNodes.forEach(node => { // node.setAttachment('/test.md', '我去') // }) - const file = await window.electronAPI.selectFile(true) + const openDirectory = config.openDirectory || false + const isRelative = config.isRelative || false + // 保存相对路径 + let filePath = '' + if (isRelative) { + filePath = await window.electronAPI.getFilePath(this.$route.params.id) + } + const file = await window.electronAPI.selectFile(openDirectory, filePath) if (file) { activeNodes.forEach(node => { node.setAttachment(file.file, file.name) @@ -83,7 +90,10 @@ export default { // console.log(node.getData('attachmentUrl')) const file = node.getData('attachmentUrl') if (!file) return - const error = await window.electronAPI.openPath(file) + const filePath = await window.electronAPI.getFilePath( + this.$route.params.id + ) + const error = await window.electronAPI.openPath(file, filePath) if (error) { this.$message.error(error) } diff --git a/web/src/pages/Edit/components/ToolbarNodeBtnList.vue b/web/src/pages/Edit/components/ToolbarNodeBtnList.vue index c9e4f44b..99da2329 100644 --- a/web/src/pages/Edit/components/ToolbarNodeBtnList.vue +++ b/web/src/pages/Edit/components/ToolbarNodeBtnList.vue @@ -162,10 +162,20 @@ :class="{ disabled: activeNodes.length <= 0 || hasGeneralization }" - @click="selectAttachmentFile" + @click="selectAttachmentFile(false)" > {{ $t('toolbar.attachment') }} +
+
+ {{ + $t('toolbar.isRelative') + }} +
+
+ {{ $t('toolbar.selectFolder') }} +
+