Skip to content

Commit

Permalink
feat(client): add file right-click menu to *.md and directly read f…
Browse files Browse the repository at this point in the history
…ile content (#717)

* chore: Only retain **始终按所有用户(每台计算机)安装**

* feat(client): directly open files in Mac
  • Loading branch information
RSS1102 authored Feb 13, 2024
1 parent 539f77f commit 18b650a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 31 deletions.
14 changes: 11 additions & 3 deletions client/electron-builder.json5
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @see https://www.electron.build/configuration/configuration
*/
{
appId: 'com.electron.cherry-noted',
appId: 'cherry-noted',
productName: 'Cherry Noted',
asar: true,
icon: 'public/favicon.ico',
Expand All @@ -25,8 +25,16 @@
},
nsis: {
oneClick: false,
perMachine: false,
"perMachine": true,
allowToChangeInstallationDirectory: true,
deleteAppDataOnUninstall: false,
},
fileAssociations: [
{
ext: ['md'],
name: 'Cherry Noted',
description: 'Cherry Noted file',
role: 'Editor',
icon: './public/favicon.ico',
},
],
}
51 changes: 48 additions & 3 deletions client/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { app, BrowserWindow, shell, ipcMain, Menu } from 'electron';
import { release } from 'node:os';
import { join } from 'node:path';
import { menuConfig } from '../preload/menu';
import fs from 'fs';

// The built directory structure
//
Expand Down Expand Up @@ -65,9 +66,51 @@ async function createWindow() {
win.loadFile(indexHtml);
}

// Test actively push message to the Electron-Renderer
win.webContents.on('did-finish-load', () => {
win?.webContents.send('main-process-message', new Date().toLocaleString());
/**
* @description Windows 直接打开文件
* @param status -1 读取文件失败; -2 取消读取; 0 读取成功
* @param message 读取文件失败的原因
* @param data 读取成功的文件内容
* @param filePath 读取成功的文件路径
*/
try {
if (process.argv.length > 1 && fs.statSync(process.argv[1]).isFile() && process.argv[1].endsWith('.md')) {
win?.webContents.send('open_file', {
status: 0,
message: 'success',
data: fs.readFileSync(process.argv[1]).toString(),
filePath: process.argv[1]
});
}
} catch (error) {
win?.webContents.send('open_file', {
status: -1,
message: error.message,
data: '',
filePath: ''
});
}
});

// mac 打开文件
app.on('open-file', (event, path) => {
event.preventDefault();
if (path.endsWith('.md')) {
win?.webContents.send('open_file', {
status: 0,
message: 'success',
data: fs.readFileSync(path).toString(),
filePath: path
});
}else{
win?.webContents.send('open_file', {
status: -1,
message: '文件格式不支持',
data: '',
filePath: ''
});
}
});

// Make all links open with the browser, not with the application
Expand All @@ -80,7 +123,9 @@ async function createWindow() {
Menu.setApplicationMenu(menu);
}

app.whenReady().then(createWindow);
app.whenReady().then(() => {
createWindow();
});

app.on('window-all-closed', () => {
win = null;
Expand Down
45 changes: 36 additions & 9 deletions client/electron/preload/menu/category/file.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BrowserWindow, dialog, ipcMain, Menu } from "electron"
import fs from "node:fs"


/**
* @description 新建文件
*/
Expand All @@ -12,7 +11,10 @@ export const newFile = () => {

/**
* @description 打开文件
* @param status -2 -1 读取文件失败; -3 取消读取; 1 读取成功
* @param status -1 读取文件失败; -2 取消读取; 0 读取成功
* @param message 读取文件失败的原因
* @param data 读取成功的文件内容
* @param filePath 读取成功的文件路径
*/
export const openFile = () => {
// 获取第一个窗口
Expand All @@ -24,26 +26,49 @@ export const openFile = () => {
],
}).then(result => {
if (result.canceled) {
win.webContents.send('open_file', { status: -3, message: 'open file canceled' })
win.webContents.send('open_file', {
status: -2,
message: 'open file canceled',
data: '',
filePath: ''
})
return;
};

let filePath = result.filePaths[0];
fs.readFile(filePath, 'utf-8', (err, data) => {
if (err) {
win.webContents.send('open_file', { status: -1, message: err.message })
win.webContents.send('open_file', {
status: -1,
message: err.message,
data: '',
filePath: ''
})
return;
}
win.webContents.send('open_file', { status: 1, message: 'open file success', data: data, filePath: filePath })
win.webContents.send('open_file', {
status: 0,
message: 'open file success',
data: data,
filePath: filePath
})
});
}).catch(err => {
win.webContents.send('open_file', { status: -2, message: err })
win.webContents.send('open_file', {
status: -1,
message: err.message,
data: '',
filePath: ''
})
});
}

/**
* @description 另存为...
* @description status -2 -1 读取文件失败; 0 取消读取; 1 读取成功
* @description status -2 -1 保存文件失败; 0 取消读取; 1 读取成功
* @description message 保存文件失败的原因
* @description filePath 保存文件成功的路径
*
*/
export const saveFileAs = () => {
const win = BrowserWindow.getAllWindows()[0];
Expand All @@ -63,7 +88,7 @@ ipcMain.on('save-file-as-info', async (event, arg: { data: string }) => {
if (err) {
event.reply('save-file-as-reply', { status: -1, message: err.message, filePath: '' })
} else {
event.reply('save-file-as-reply', { status: 1, message: 'save file as success', filePath: filePath })
event.reply('save-file-as-reply', { status: 0, message: 'save file as success', filePath: filePath })
const menu = Menu.getApplicationMenu();
const saveFileBtn = menu.getMenuItemById('save-file');
saveFileBtn.enabled = false
Expand All @@ -77,6 +102,8 @@ ipcMain.on('save-file-as-info', async (event, arg: { data: string }) => {

/**
* @description 保存文件
* @description status 0 保存成功; -1 保存失败
* @description message 保存失败的原因
*/
export const saveFile = () => {
// 询问是否有文件路径->如没有则调用另存为文件功能
Expand All @@ -91,7 +118,7 @@ ipcMain.on('sava-file-type', (event, arg: { filePath: string, data: string }) =>
event.reply('save-file-reply', { status: -1, message: err.message, });
} else {
console.error('success');
event.reply('save-file-reply', { status: 1, message: 'save file as success' });
event.reply('save-file-reply', { status: 0, message: 'save file as success' });
const menu = Menu.getApplicationMenu();
const saveFileBtn = menu.getMenuItemById('save-file');
saveFileBtn.enabled = false
Expand Down
4 changes: 2 additions & 2 deletions client/electron/preload/menu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ const menuConfig: Array<MenuItemConstructorOptions | MenuItem> = [
]
},
]

isPackaged ? [] : menuConfig.push({
// isPackaged ? [] :
menuConfig.push({
label: "打开DevTools",
role: 'toggleDevTools'
})
Expand Down
2 changes: 1 addition & 1 deletion client/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<Home/>
<Home />
</template>

<script setup lang="ts">
Expand Down
22 changes: 9 additions & 13 deletions client/src/components/CherryMarkdown/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@ ipcRenderer.on('new_file', () => {
/**
* @description 打开文档
* @description 仅当 status=1,data、filePath 存在
*/
ipcRenderer.on('open_file', (event, arg: { status: -2 | -1 | -3 | 1, message: string, data: string, filePath: string }) => {
ipcRenderer.on('open_file', (_event, arg: { status: -2 | -1 | 0, message: string, data: string, filePath: string }) => {
switch (arg.status) {
case -2:
TMessagePlugin.error(arg.message);
break;
case -1:
TMessagePlugin.error(arg.message);
break;
case -3:
TMessagePlugin.warning(arg.message);
break;
case 1:
storeCherry.cherry?.setMarkdown(arg.data)
storeElectronMenu.saveFilePath = arg.filePath
case 0:
storeCherry.cherry?.setMarkdown(arg.data);
storeElectronMenu.saveFilePath = arg.filePath;
break;
}
Expand All @@ -49,14 +45,14 @@ ipcRenderer.on('open_file', (event, arg: { status: -2 | -1 | -3 | 1, message: st
ipcRenderer.on('save-file-as',
() => ipcRenderer.send('save-file-as-info', { data: storeCherry.cherry?.getMarkdown() }))
ipcRenderer.on('save-file-as-reply', (event, arg: { status: -2 | -1 | 1, message: string, filePath: string }) => {
ipcRenderer.on('save-file-as-reply', (event, arg: { status: -2 | -1 | 0, message: string, filePath: string }) => {
switch (arg.status) {
case 0:
storeElectronMenu.saveFilePath = arg.filePath
break;
case -1:
TMessagePlugin.error(arg.message);
break;
case 1:
storeElectronMenu.saveFilePath = arg.filePath
break;
case -2:
TMessagePlugin.warning(arg.message);
break;
Expand All @@ -70,7 +66,7 @@ ipcRenderer.on('save-file', () => {
ipcRenderer.send('sava-file-type', { filePath: storeElectronMenu.saveFilePath, data: storeCherry.cherry?.getMarkdown() })
})
ipcRenderer.on('save-file-reply', (event, arg: { status: -1 | 1, message: string }) => {
ipcRenderer.on('save-file-reply', (event, arg: { status: -1 | 0, message: string }) => {
if (arg.status === -1) {
TMessagePlugin.error(arg.message);
}
Expand Down

0 comments on commit 18b650a

Please sign in to comment.