Skip to content

Commit

Permalink
更改保存逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
DanKE123abc committed Jul 5, 2024
1 parent 40d4357 commit e95b893
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 40 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions OpenSourceLicense.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ Licensed under Node.js license

[github.com/chromium/chromium](https://github.com/chromium/chromium)

BSD 3 Clause license
Licensed under BSD 3 Clause license

## electron-builder

[github.com/electron-userland/electron-builder](https://github.com/electron-userland/electron-builder)

Licensed underMIT license
Licensed under MIT license

## vditor

Expand All @@ -41,3 +41,7 @@ Licensed under MIT license
[github.com/twbs/bootstrap](https://github.com/twbs/bootstrap)

Licensed under MIT license



Licensed under MIT license
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

一款Markdown超文本编辑器,类似于Typora


该README页面正在施工,待补充...

—— 使用Hypora书写 ——

#### 本项目在开发时,使用了部分优秀的开源项目,详见:[开放源代码许可](https://github.com/DanKE123abc/Hypora/blob/main/OpenSourceLicense.md)


## [开放源代码许可](https://github.com/DanKE123abc/Hypora/blob/main/OpenSourceLicense.md)
—— 使用Hypora书写 ——
22 changes: 16 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function createWindow() {
win.on('close', function (e) {
// 阻止默认关闭事件
e.preventDefault();
win.webContents.send("quit");
win.webContents.send("action", "quit");
});
}

Expand All @@ -40,13 +40,13 @@ app.whenReady().then(() => {
switch(args) {
case 'new': // 新建
newWindow();
win.webContents.send('new');
win.webContents.send("action",'new');
break;
case 'new-win':
newWindow();
break;
default:
win.webContents.send(args);
win.webContents.send("action", args);
}
});
menu.emitter.on("editor", (args) => {
Expand All @@ -64,11 +64,22 @@ app.whenReady().then(() => {
win.webContents.send('theme', args);
});
menu.emitter.on("help", (args) => {
win.webContents.send('help', args);
switch(args) {
case 'opensource':
ipcMain.on('isInit', function() {

});
break;
default:
win.webContents.send('help', args);win.webContents.send("action", args);
}

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


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

win.once('ready-to-show', () => {
Expand All @@ -77,7 +88,7 @@ app.whenReady().then(() => {
if (filePath && fs.existsSync(filePath)) {
console.log(`文件路径已找到: ${filePath}`);
ipcMain.on('isInit', function() {
win.webContents.send('open_cmd', filePath);
win.webContents.send('open-cmd', filePath);
});
} else {
console.error('未找到有效的文件路径参数');
Expand Down Expand Up @@ -111,4 +122,3 @@ function newWindow(){
}



6 changes: 6 additions & 0 deletions menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ const menuTemplate = [
emitter.emit('help','about');
}
},
//{
// label: '开源许可证',
// click: function () {
// emitter.emit('help','opensource');
// }
//},
]
},
];
Expand Down
73 changes: 44 additions & 29 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ let isSave = true; // 初始状态无需保存
let currentFile = null; // 初始状态无文件路径
let isQuit = true;
let isInit = false;

let originalContent = null;

let isOutlineVisible = false;
Expand All @@ -34,7 +33,7 @@ var config = {
position: "left"
},
typewriterMode: false,
cdn: "./vditor@3.10.4",
cdn: "./node_modules/vditor",
after: () => {
if (!isInit){
initMd();
Expand All @@ -53,16 +52,14 @@ var config = {

var vditor = new Vditor('vditor', config);


// 创建菜单
const contextMenu = Menu.buildFromTemplate(menu.contextMenuTemplate);
window.addEventListener('contextmenu', (e) => {
e.preventDefault(); // 阻止默认的右键菜单
contextMenu.popup(BrowserWindow.getFocusedWindow());
});
menu.emitter.on("editor", handleEditorCommand);
ipcRenderer.on('editor', (event, args) => {
handleEditorCommand(args);
});
function handleEditorCommand(args) {
switch (args) {
case 'bold':
Expand All @@ -89,6 +86,10 @@ function handleEditorCommand(args) {
}
}

ipcRenderer.on('editor', (event, args) => {
handleEditorCommand(args);
});

ipcRenderer.on('view', (event, args) => {
switch (args) {
case 'toggleOutline':
Expand Down Expand Up @@ -128,6 +129,31 @@ ipcRenderer.on('help', (event, args) => {
}
});

ipcRenderer.on('action',(event, args) =>{
switch (args) {
case 'new':
initMd();
break;
case 'open':
askSaveNeed();
openFile();
break;
case 'save':
saveCurrentMd();
break;
case 'save-as':
currentFile = null;
saveCurrentMd();
break;
case 'quit':
askSaveNeed();
if(isQuit) {
ipcRenderer.send('exit');
}
break;
}
});

menu.emitter.on("set-title", (numberOfHashes) => {// 段落
setTitle(numberOfHashes)
});
Expand All @@ -141,32 +167,12 @@ function setTitle(numberOfHashes) {
document.execCommand('insertText', false, content);
}


ipcRenderer.on('new', (event, content) => {
initMd();
});
ipcRenderer.on('open', (event, content) => {
askSaveNeed();
openFile();
});
ipcRenderer.on('save', (event, content) => {
saveCurrentMd();
});
ipcRenderer.on('save-as', (event, content) => {
currentFile = null;
saveCurrentMd();
});
ipcRenderer.on('quit', (event, content) => {
askSaveNeed();
if(isQuit) {
ipcRenderer.send('exit');
}
});
ipcRenderer.on('open_cmd', (event, currentFilePath) => {
ipcRenderer.on('open-cmd', (event, currentFilePath) => {
currentFile = currentFilePath;
const txtRead = readText(currentFile);
vditor.setValue(txtRead);
document.title = currentFile + ' - Hypora';
originalContent = vditor.getValue();
isSave = true;
isInit = true;
});
Expand Down Expand Up @@ -308,6 +314,7 @@ function initMd() {
document.title = 'Untitled - Hypora';
isSave = true;
isInit = true;
originalContent = vditor.getValue();
ipcRenderer.send('isInit');
}

Expand Down Expand Up @@ -356,6 +363,7 @@ function saveCurrentMd() {
const txtSave = vditor.getValue();
saveText(currentFile, txtSave);
isSave = true;
originalContent = vditor.getValue();
document.title = currentFile + " - Hypora";
}

Expand Down Expand Up @@ -383,6 +391,7 @@ function openFile() {
const txtRead = readText(currentFile);
vditor.setValue(txtRead);
document.title = currentFile + ' - Hypora';
originalContent = vditor.getValue();
isSave = true;
isInit = true;
}
Expand All @@ -398,8 +407,14 @@ function readText(file) {
function contentModification() {
if (!(vditor === null)) {
if (isSave) {
isSave = false;
document.title = document.title.replace(/ - /g, '● - ');
if(originalContent !== null)
{
if(vditor.getValue() !== originalContent)
{
isSave = false;
document.title = document.title.replace(/ - /g, '● - ');
}
}
}
}
}

0 comments on commit e95b893

Please sign in to comment.