diff --git a/node_modules/note.js b/node_modules/note.js index 1b01fb6e..f6cb5d42 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -208,10 +208,11 @@ var Note = { Histories []EachHistory `Histories` } */ - addNoteHistory: function(noteId, content) { + addNoteHistory: function(noteId, content, callback) { var me = this; db.noteHistories.loadDB(function (ok) { if (!ok) { + callback && callback(false); return; } // 先判断是否存在, 不存在则新建之 @@ -219,7 +220,9 @@ var Note = { var now = new Date(); // 新建之 if(!history) { - db.noteHistories.insert({_id: noteId, Histories: [{Content: content, "UpdatedTime": now}], "UpdatedTime": now}); + db.noteHistories.insert({_id: noteId, Histories: [{Content: content, "UpdatedTime": now}], "UpdatedTime": now}, function () { + callback && callback(true); + }); } // 更新之 else { @@ -235,7 +238,9 @@ var Note = { } } - db.noteHistories.update({_id: noteId}, {$set: {Histories: histories}}); + db.noteHistories.update({_id: noteId}, {$set: {Histories: histories}}, function () { + callback && callback(true); + }); } }); }); diff --git a/public/js/app/note.js b/public/js/app/note.js index 28329b6a..58b10432 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -304,7 +304,6 @@ Note.curHasChanged = function(force) { // 不会出现, 因为刚开始时readOnly=true, 且只有设置内容完成后才setCurNoteId // markdown编辑器还没准备好 throw new Error('markdown编辑器还没准备好'); - alert(3); } else { content = contents; @@ -443,13 +442,32 @@ Note.getImgSrc = function(content) { return ""; }; +// revert 历史记录时强制添加到history中 +Note.curChangedSaveItForRevertHistory = function(callback) { + var me = this; + me.curChangedSaveIt(true, function (ret) { + // 没有保存啊 + if (!ret) { + var note = me.getCurNote(); + var content = getEditorContent(note.IsMarkdown); + if (isArray(content)) { + content = content[0]; + } + NoteService.addNoteHistory(me.curNoteId, content, callback); + } + else { + callback(); + } + }); +}; + // 如果当前的改变了, 就保存它 // 以后要定时调用 // force , 默认是true, 表强校验内容 // 定时保存传false Note.saveInProcess = {}; // noteId => bool, true表示该note正在保存到服务器, 服务器未响应 Note.savePool = {}; // 保存池, 以后的保存先放在pool中, id => note -Note.curChangedSaveIt = function(force, callback) { +Note.curChangedSaveIt= function(force, callback) { var me = Note; // 如果当前没有笔记, 不保存 if(!me.curNoteId) { diff --git a/public/js/dec/history.js b/public/js/dec/history.js index ebe16537..425c0e00 100644 --- a/public/js/dec/history.js +++ b/public/js/dec/history.js @@ -89,15 +89,17 @@ define(function() { $tpl.find('.back').click(function() { if(confirm(getMsg("confirmBackup"))) { // 保存当前版本 - Note.curChangedSaveIt(true); + // 如果没有变化, 肯定不会保存的 + // Note.curChangedSaveIt(true); + Note.curChangedSaveItForRevertHistory(function () { + // 设置之 + note = Note.cache[Note.curNoteId]; + setEditorContent(me.list[me.curIndex].Content, note.IsMarkdown); - // 设置之 - note = Note.cache[Note.curNoteId]; - setEditorContent(me.list[me.curIndex].Content, note.IsMarkdown); - - $tpl.modal('hide'); - // 保存 - Note.curChangedSaveIt(true); + $tpl.modal('hide'); + // 保存 + Note.curChangedSaveIt(true); + }); } });