From beb1dc123eddd712e28e8bd911404898b820cd97 Mon Sep 17 00:00:00 2001 From: life Date: Mon, 4 May 2015 23:39:40 +0800 Subject: [PATCH] =?UTF-8?q?sync=20=E9=97=AE=E9=A2=98=20leanote#103,=20?= =?UTF-8?q?=E7=82=B9=E5=87=BBsync=E5=9B=BE=E6=A0=87=E4=B8=8Dsync=E9=97=AE?= =?UTF-8?q?=E9=A2=98,=20=E5=9B=A0=E4=B8=BA=E5=BD=93=E5=89=8D=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E7=AC=94=E8=AE=B0,=20=E4=B8=8D=E8=83=BD=E4=BF=9D?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/node_modules/api.js | 1 - src/node_modules/note.js | 17 +++++++--- src/node_modules/notebook.js | 20 ++++++++++- src/node_modules/sync.js | 62 +++++++++++++++++++++++++---------- src/node_modules/web.js | 1 - src/public/config.js | 2 +- src/public/js/app/note.js | 5 +++ src/public/js/app/notebook.js | 23 +++++++++++-- src/public/js/app/page.js | 2 +- 9 files changed, 104 insertions(+), 29 deletions(-) diff --git a/src/node_modules/api.js b/src/node_modules/api.js index 558f2c70..9591b5db 100644 --- a/src/node_modules/api.js +++ b/src/node_modules/api.js @@ -390,7 +390,6 @@ var Api = { console.log(data); needle.post(me.getUrl('notebook/addNotebook'), data, {}, function(err, resp) { me.checkError(err, resp); - if(err) { return callback(false); } diff --git a/src/node_modules/note.js b/src/node_modules/note.js index bc3908ea..272c7f29 100644 --- a/src/node_modules/note.js +++ b/src/node_modules/note.js @@ -304,6 +304,12 @@ var Note = { }); }); }, + // 是新的, 又是deleted的, 则删除之 + deleteLocalNote: function(noteId, callback) { + Notes.remove({NoteId: noteId}, function() { + callback && callback(); + }); + }, // 彻底删除笔记, 如果有tags, 则需要更新tags's count deleteTrash: function(noteId, callback) { var me = this; @@ -325,15 +331,15 @@ var Note = { } }); + /* Notes.update({NoteId: noteId}, {$set: {IsDirty: true, LocalIsDelete: true}}, function(err, n) { if(err || !n) { callback(false); } else { - - callback(true); } }); + */ }, // 移动note @@ -624,11 +630,14 @@ var Note = { var me = this; note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步 note.IsDirty = false; + note.LocalIsDelete = false; + note.IsTrash = false; + note.ServerNoteId = note.NoteId; note.NoteId = Common.objectId(); - note.LocalIsDelete = false; + + console.error('add note force' + note.Title + Common.goNowToDate(note.CreatedTime)); - console.error('add note' + note.Title + Common.goNowToDate(note.CreatedTime)); note.CreatedTime = Common.goNowToDate(note.CreatedTime); note.UpdatedTime = Common.goNowToDate(note.UpdatedTime); diff --git a/src/node_modules/notebook.js b/src/node_modules/notebook.js index 56999376..e8095b6f 100644 --- a/src/node_modules/notebook.js +++ b/src/node_modules/notebook.js @@ -112,6 +112,7 @@ var Notebook = { }, // 新建笔记本 + // 这里, 之前有个大BUG, pull过来后添加到tree上设为IsNew, 导致添加大量重复的notebook addNotebook: function(notebookId, title, parentNotebookId, callback) { var notebook = { Title: title, @@ -135,6 +136,7 @@ var Notebook = { }); }, + // 修改笔记本标题 updateNotebookTitle: function(notebookId, title, callback) { NB.update({NotebookId: notebookId}, {$set: @@ -200,6 +202,13 @@ var Notebook = { */ }, + // 删除本地的笔记本, 是New又是Delete + deleteLocalNotebook: function(notebookId, callback) { + NB.remove({NotebookId: notebookId}, function(err, n) { + callback && callback(); + }); + }, + // addNote, 删除note, 移动note // 重新统计笔记本的笔记数据 reCountNotebookNumberNotes: function(notebookId) { @@ -298,6 +307,10 @@ var Notebook = { notebook.CreatedTime = Common.goNowToDate(notebook.CreatedTime); notebook.UpdatedTime = Common.goNowToDate(notebook.UpdatedTime); + notebook.IsDirty = false; + notebook.LocalIsNew = false; + notebook.LocalIsDelete = false; + NB.insert(notebook, function (err, newDoc) { // Callback is optional if(err) { console.log(err); @@ -342,6 +355,7 @@ var Notebook = { // 更新笔记本, NotebookId可能也要更改 // notebook是服务器传过来的 updateNotebookForceForSendChange: function(notebookId, notebook, callback) { + // console.log('updateNotebookForceForSendChange notebook是服务器传过来的'); var me = this; notebook.IsDirty = false; notebook.LocalIsNew = false; @@ -350,7 +364,11 @@ var Notebook = { me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) { notebook.ParentNotebookId = parentNotebookId; - NB.update({NotebookId: notebookId}, {$set: notebook}, {}, function (err, n) { + // console.log(notebook2); + // notebook2.Title += " H-"; + // multi, 因为历史原因, 导致大量重复notebookId的元素 + NB.update({NotebookId: notebookId}, {$set: notebook}, {multi: true}, function (err, n) { + // console.log('updateNotebookForceForSendChange end' + notebookId + ' ' + n); if(err) { console.log(err); callback && callback(false); diff --git a/src/node_modules/sync.js b/src/node_modules/sync.js index 0467afd8..30e1396a 100644 --- a/src/node_modules/sync.js +++ b/src/node_modules/sync.js @@ -185,7 +185,7 @@ var Sync = { Notebook.getNotebookByServerNotebookId(notebookId, function(notebookLocal) { // 2.1 本地没有, 表示是新建 if(!notebookLocal) { - console.log('add: ...') + console.log('add addNotebookForce...') // TODO Notebook.addNotebookForce(notebook, function(notebook) { me._syncInfo.notebook.adds.push(notebook); @@ -697,6 +697,15 @@ var Sync = { // 一个一个同步执行, 因为要有 async.eachSeries(notebooks, function(notebook, cb) { var api = Api.updateNotebook; + + // 本地既是新的, 又是删除的, 删除本地的, 不要同步 + // 5/4 + if(notebook.LocalIsNew && notebook.LocalIsDelete) { + console.log('笔记本既是新的, 又是删除的, 不同步, 直接删除本地的'); + Notebook.deleteLocalNotebook(notebook.NotebookId); + return cb(); + } + if(notebook.LocalIsNew) { api = Api.addNotebook; } else if(notebook.LocalIsDelete) { @@ -793,27 +802,43 @@ var Sync = { // 调api, 所有执行后再callback(); // 一个一个同步执行, 因为要有 async.eachSeries(notes, function(note, cb) { - if(note.LocalIsNew && !note.IsTrash) { // 是新的, 且不是trash - // 添加, newNote的返回不会很多值(server端) - Api.addNote(note, function(newNote) { - if(!Common.isOk(newNote)) { - return cb(); - } + if(note.LocalIsNew) { + // 是新的, 且不是trash和删除的 + if(!note.IsTrash && !note.LocalIsDelete) { + // 添加, newNote的返回不会很多值(server端) + Api.addNote(note, function(newNote) { + if(!Common.isOk(newNote)) { + return cb(); + } - newNote.ServerNoteId = newNote.NoteId; - newNote.NoteId = note.NoteId; + newNote.ServerNoteId = newNote.NoteId; + newNote.NoteId = note.NoteId; - newNote.IsBlog = note.IsBlog; // 前端要用 - me._syncInfo.note.changeAdds.push(newNote); + newNote.IsBlog = note.IsBlog; // 前端要用 + me._syncInfo.note.changeAdds.push(newNote); - Note.updateNoteForceForSendChange(newNote, true); + Note.updateNoteForceForSendChange(newNote, true); - // 这里 - me.checkNeedIncSyncAgain(newNote.Usn); + // 这里 + me.checkNeedIncSyncAgain(newNote.Usn); - cb(); - }); - } else if(note.LocalIsDelete) { + cb(); + }); + } + // 5/4 + // 本地已经删除了, 则彻底删除之 + else if(note.LocalIsDelete) { + console.log('既是新的, 又是删除的, 则删除笔记的'); + Note.deleteLocalNote(note.NoteId); + return cb(); + } + // isTrash, 不同步, 不删除 + else { + console.log('既是新的, 又是trash的, 不要同步'); + return cb(); + } + } + else if(note.LocalIsDelete) { // 删除, 不管它了 // TODO Api.deleteTrash(note, function(ret) { @@ -822,7 +847,8 @@ var Sync = { } return cb(); }); - } else { + } + else { // 更新 Api.updateNote(note, function(ret) { if(!Common.isOk(ret)) { diff --git a/src/node_modules/web.js b/src/node_modules/web.js index ab74e65d..bd630a96 100644 --- a/src/node_modules/web.js +++ b/src/node_modules/web.js @@ -153,7 +153,6 @@ var Web = { updateNotebookNumberNotes: function(notebookId, count) { var me = this; me.Notebook.updateNotebookNumberNotes(notebookId, count); - } }; module.exports = Web; diff --git a/src/public/config.js b/src/public/config.js index f4e47496..6cc8b40b 100644 --- a/src/public/config.js +++ b/src/public/config.js @@ -19,6 +19,6 @@ var Config = { "name": "繁体中文" } ], - "lang": "en-us", + "lang": "zh-hk", "theme": "water" }; \ No newline at end of file diff --git a/src/public/js/app/note.js b/src/public/js/app/note.js index b5526ff5..15d1e632 100644 --- a/src/public/js/app/note.js +++ b/src/public/js/app/note.js @@ -465,6 +465,7 @@ Note.curChangedSaveIt = function(force, callback) { var me = this; // 如果当前没有笔记, 不保存 if(!Note.curNoteId || Note.isReadOnly) { + callback && callback(); return; } @@ -2815,6 +2816,10 @@ Note.addSync = function(notes) { for(var i in notes) { var note = notes[i]; Note.addNoteCache(note); + + // 很可能其笔记本也是新添加的, 此时重新render notebooks' numberNotes + Notebook.reRenderNotebookNumberNotesIfIsNewNotebook(note.NotebookId); + // alert(note.ServerNoteId); // 添加到当前的笔记列表中 var newHtmlObject = Note._getNoteHtmlObjct(note); diff --git a/src/public/js/app/notebook.js b/src/public/js/app/notebook.js index 691e4b09..ab194830 100644 --- a/src/public/js/app/notebook.js +++ b/src/public/js/app/notebook.js @@ -25,11 +25,28 @@ Notebook.getCurNotebook = function() { return Notebook.cache[Notebook.curNotebookId]; }; +// 为什么可能会没有? 因为可能是新加的笔记本, 此时该笔记本又有笔记, 一起同步过来 +// 还没显示到web上 +// 放在这里, 让addNote时调用 +Notebook._newNotebookNumberNotes = {}; // notebookId => count +Notebook.reRenderNotebookNumberNotesIfIsNewNotebook = function(notebookId) { + var count = Notebook._newNotebookNumberNotes[notebookId]; + if(count) { + delete Notebook._newNotebookNumberNotes[notebookId]; + } + else { + return; + } + Notebook.updateNotebookNumberNotes(notebookId, count); +}; // 为了server Web调用 Notebook.updateNotebookNumberNotes = function(notebookId, count) { var self = this; var notebook = self.getNotebook(notebookId); + // 为什么可能会没有? 因为可能是新加的笔记本, 此时该笔记本又有笔记, 一起同步过来 + // 还没显示到web上 if(!notebook) { + Notebook._newNotebookNumberNotes[notebookId] = count; return; } notebook.NumberNotes = count; @@ -1033,11 +1050,13 @@ Notebook.addSync = function(notebooks) { if(isEmpty(notebooks)) { return; } - log('add sync notebook'); + console.log('web add sync notebook'); for(var i in notebooks) { var notebook = notebooks[i]; Notebook.setCache(notebook); - me.tree.addNodes(me.tree.getNodeByTId(notebook.ParentNotebookId), {Title: notebook.Title, NotebookId: notebook.NotebookId, IsNew: true}, true, true, false); + me.tree.addNodes(me.tree.getNodeByTId(notebook.ParentNotebookId), + {Title: notebook.Title, NotebookId: notebook.NotebookId, IsNew: false}, // IsNew: false啊!!! + true, true, false); } } // 本地 -> 添加到服务器上的 diff --git a/src/public/js/app/page.js b/src/public/js/app/page.js index caaacece..3170dedd 100644 --- a/src/public/js/app/page.js +++ b/src/public/js/app/page.js @@ -1878,11 +1878,11 @@ function userMenu() { this.menu.append(new gui.MenuItem({ type: 'separator' })); - var mores = new gui.Menu(); this.sync = new gui.MenuItem({ label: getMsg('Sync now'), click: function(e) { + console.log('sync now'); incrSync(true); } });