Skip to content

Commit

Permalink
支持更多的Markdown功能
Browse files Browse the repository at this point in the history
  • Loading branch information
DanKE123abc committed Jul 5, 2024
1 parent e95b893 commit 2edbd8a
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 9 deletions.
10 changes: 7 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ function createWindow() {
});

win.webContents.setIgnoreMenuShortcuts(false);

remote.initialize();
remote.enable(win.webContents);
win.loadFile('index.html'); // 确保您的HTML文件名与此处匹配

win.loadFile('index.html');

win.on('close', function (e) {
// 阻止默认关闭事件
Expand Down Expand Up @@ -78,7 +80,9 @@ app.whenReady().then(() => {
menu.emitter.on("set-title", (args) => {
win.webContents.send('set-title', args);
});

menu.emitter.on("edit",(args) => {
win.webContents.send('edit', args);
});

win.setIcon(path.join(__dirname, 'assets/icon.ico'));

Expand All @@ -87,7 +91,7 @@ app.whenReady().then(() => {

if (filePath && fs.existsSync(filePath)) {
console.log(`文件路径已找到: ${filePath}`);
ipcMain.on('isInit', function() {
ipcMain.on('isInit', function () {
win.webContents.send('open-cmd', filePath);
});
} else {
Expand Down
131 changes: 127 additions & 4 deletions menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,60 @@ const menuTemplate = [
click: function () {
emitter.emit('set-title', 6);
}
}
},
{ type: 'separator' },
{
label: '公式块',
click: function () {
emitter.emit('edit','set-formula');
}
},
{
label: '代码块',
click: function () {
emitter.emit('edit','set-code');
}
},
{ type: 'separator' },
{
label: '引用',
click: function () {
emitter.emit('edit','set-quote');
}
},
{ type: 'separator' },
{
label: '有序列表',
click: function () {
emitter.emit('editor','ordered-list');
}
},
{
label: '无序列表',
click: function () {
emitter.emit('editor','unordered-list');
}
},
{
label: '任务列表',
click: function () {
emitter.emit('editor','task-list');
}
},
{ type: 'separator' },
{
label: '脚注',
click: function () {
emitter.emit('edit','set-footnote');
}
},
{ type: 'separator' },
{
label: '水平分割线',
click: function () {
emitter.emit('edit','set-divider');
}
},
]
},
{
Expand All @@ -135,6 +188,26 @@ const menuTemplate = [
emitter.emit('editor','underline');
}
},
{
label: '代码',
click: function () {
emitter.emit('editor','code');
}
},
{ type: 'separator' },
{
label: '删除线',
click: function () {
emitter.emit('editor','deleteline');
}
},
{
label: '注释',
click: function () {
emitter.emit('editor','comment');
}
},

]
},
{
Expand Down Expand Up @@ -248,25 +321,46 @@ const contextMenuTemplate = [
submenu: [
{
label: '加粗',
accelerator: 'CmdOrCtrl+B',
click: function () {
emitter.emit('editor','bold');
}
},
{
label: '斜体',
accelerator: 'CmdOrCtrl+I',
click: function () {
emitter.emit('editor','italic');
}
},
{
label: '下划线',
accelerator: 'CmdOrCtrl+U',
click: function () {
emitter.emit('editor','underline');
}
},
{
label: '引用',
click: function () {
emitter.emit('edit','set-quote');
}
},
{
label: '有序列表',
click: function () {
emitter.emit('editor','ordered-list');
}
},
{
label: '无序列表',
click: function () {
emitter.emit('editor','unordered-list');
}
},
{
label: '任务列表',
click: function () {
emitter.emit('editor','task-list');
}
},
]
},
{
Expand Down Expand Up @@ -317,7 +411,36 @@ const contextMenuTemplate = [
]

},
{
label: '插入',
submenu: [
{
label: '脚注',
click: function () {
emitter.emit('edit','set-footnote');
}
},
{
label: '水平分割线',
click: function () {
emitter.emit('edit','set-divider');
}
},
{
label: '代码块',
click: function () {
emitter.emit('edit','set-code');
}
},
{
label: '公式块',
click: function () {
emitter.emit('edit','set-formula');
}
},

]
},
];

const aboutDialog = {
Expand Down
85 changes: 83 additions & 2 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ function handleEditorCommand(args) {
const underlineContent = "<u>" + vditor.getSelection() + "</u>";
document.execCommand('insertText', false, underlineContent);
break;
case 'code':
const codeContent = "`" + vditor.getSelection() + "`";
document.execCommand('insertText', false, codeContent);
break;
case 'deleteline':
const deletelineContent = "~~" + vditor.getSelection() + "~~";
document.execCommand('insertText', false, deletelineContent);
break;
case 'comment':
const commentContent = "<!--" + vditor.getSelection() + "-->";
document.execCommand('insertText', false, commentContent);
break;
case 'ordered-list':
const orderedlistContent = "1. " + vditor.getSelection();
document.execCommand('insertText', false, orderedlistContent);
break;
case 'unordered-list':
const unorderedlistContent = "- " + vditor.getSelection();
document.execCommand('insertText', false, unorderedlistContent);
break;
case 'task-list':
const tasklistContent = "- [ ] " + vditor.getSelection();
document.execCommand('insertText', false, tasklistContent);
break;
case 'set-quote':
setQuote();
break;
case 'copy2text':
const content = vditor.getSelection();
if (content) {
Expand Down Expand Up @@ -154,7 +181,8 @@ ipcRenderer.on('action',(event, args) =>{
}
});

menu.emitter.on("set-title", (numberOfHashes) => {// 段落

menu.emitter.on("set-title", (numberOfHashes) => {
setTitle(numberOfHashes)
});
ipcRenderer.on('set-title', (event,numberOfHashes) => {
Expand All @@ -167,6 +195,59 @@ function setTitle(numberOfHashes) {
document.execCommand('insertText', false, content);
}


ipcRenderer.on('edit', (event,args) => {
handleEditCommand(args);
});
menu.emitter.on("edit", handleEditCommand);
function handleEditCommand(args) {
switch (args) {
case 'set-formula':
setFormula();
break;
case 'set-code':
setCode();
break;
case 'set-quote':
setQuote();
break;
case 'set-footnote':
setFootnote();
break;
case 'set-divider':
setDivider();
break;
}
}
function setDivider() { //水平分割线
const content = '______';
vditor.focus();
document.execCommand('insertText', false, content);
}
function setCode() { //代码块
const content = '```';
vditor.focus();
document.execCommand('insertText', false, content);
}
function setFormula() { //公式块
const content = '$$';
vditor.focus();
document.execCommand('insertText', false, content);
}
function setFootnote() {
const hashes = '[^]:';
const content = hashes + " " + vditor.getSelection();
vditor.focus();
document.execCommand('insertText', false, content);
}
function setQuote() {
const hashes = '>';
const content = hashes + " " + vditor.getSelection();
vditor.focus();
document.execCommand('insertText', false, content);
}


ipcRenderer.on('open-cmd', (event, currentFilePath) => {
currentFile = currentFilePath;
const txtRead = readText(currentFile);
Expand Down Expand Up @@ -363,7 +444,6 @@ function saveCurrentMd() {
const txtSave = vditor.getValue();
saveText(currentFile, txtSave);
isSave = true;
originalContent = vditor.getValue();
document.title = currentFile + " - Hypora";
}

Expand All @@ -372,6 +452,7 @@ function saveCurrentMd() {
function saveText( file, text ) {
const fs = require('fs');
fs.writeFileSync( file, text );
originalContent = vditor.getValue();
}

// 选择文档路径
Expand Down

0 comments on commit 2edbd8a

Please sign in to comment.