diff --git a/node_modules/api.js b/node_modules/api.js index 03134b1a..9fb2fe53 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -498,11 +498,11 @@ var Api = { Title: note.Title, NotebookId: serverNotebookId, Content: note.Content, - IsMarkdown: note.isMarkdown, + IsMarkdown: note.IsMarkdown, Tags: note.Tags, IsBlog: note.IsBlog, Files: note.Files, - FileDatas: note.FileDatas + FileDatas: note.FileDatas, } // log('add note'); // log(data); @@ -543,6 +543,9 @@ var Api = { updateNote: function(note, callback) { var me = this; Notebook.getServerNotebookIdByNotebookId(note.NotebookId, function(serverNotebookId) { + if(!note.Tags || note.Tags.length == 0) { + note.Tags = ['']; + } var data = { noteId: note.ServerNoteId, notebookId: serverNotebookId || "", @@ -551,7 +554,8 @@ var Api = { isTrash: note.IsTrash, content: note.Content, Files: note.Files, - FileDatas: note.FileDatas + FileDatas: note.FileDatas, + tags: note.Tags, // 新添加 } console.log('update note :'); console.log(data); @@ -573,6 +577,8 @@ var Api = { var ret = resp.body; log('update note ret:'); log(ret); + // 没有传IsMarkdown, 后台会传过来总为false + delete ret['IsMarkdown']; callback(ret); /* if(Common.isOk(ret)) { @@ -630,11 +636,12 @@ var Api = { return callback && callback(false); } var ret = resp.body; - log('add tag ret =========='); - log(ret); + console.log('add tag ret =========='); + console.log(ret); if(Common.isOk(ret)) { - // 以后不要再发了 - Tag.setNotDirty(title); + // Tag.setNotDirty(title); + // 更新, 添加usn + Tag.setNotDirtyAndUsn(title, ret.Usn); callback && callback(ret); } else { callback && callback(false); diff --git a/node_modules/note.js b/node_modules/note.js index 8a5bf10a..b5cd910b 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -44,7 +44,8 @@ var Note = { var me = this; var userId = User.getCurActiveUserId(); noteOrContent['UserId'] = userId; - console.log('updateNoteOrContent: '); + console.error("updateNoteOrContent") + console.trace('updateNoteOrContent: '); console.log(noteOrContent); var date = new Date(); noteOrContent.UpdatedTime = date; @@ -92,25 +93,46 @@ var Note = { needUpdate = true; } } - if('Content' in updateFields) { + if('Content' in updates) { // 内容更新了, 因为内容是以后才从远程获取的, 获取内容后改变ContentIsDirty=false noteOrContent['ContentIsDirty'] = true; } if(needUpdate) { - updates['IsDirty'] = true; - updates['LocalIsDelete'] = false; - updates.UpdatedTime = date; - // Set an existing field's value - Notes.update({NoteId: noteOrContent.NoteId}, { $set: updates }, {}, function (err, numReplaced) { - if(err) { - callback && callback(false); - } else { - callback && callback(noteOrContent); - - if('Content' in updates) { - me.addNoteHistory(noteOrContent.NoteId, noteOrContent.Content); + var isDirty = false; + me.getNote(noteOrContent.NoteId, function(dbNote) { + // 只有title, Content, Tags修改了才算是IsDirty + if('Content' in updates && dbNote['Content'] != updates['Content']) { + isDirty = true; + } else if('Title' in updates && dbNote['Title'] != updates['Title']) { + isDirty = true; + } else if('Tags' in updates) { + var dbTags = dbNote['Tags'] || []; + var nowTags = updates['Tags'] || []; + if(dbTags.join(',') != nowTags.join(',')) { + isDirty = true; } } + + console.log('update note isDirty: ' + noteOrContent.NoteId); + console.error(isDirty); + + updates['IsDirty'] = isDirty; + updates['LocalIsDelete'] = false; + updates.UpdatedTime = date; + + // Set an existing field's value + Notes.update({NoteId: noteOrContent.NoteId}, { $set: updates }, {}, function (err, numReplaced) { + if(err) { + callback && callback(false); + } else { + callback && callback(noteOrContent); + + if('Content' in updates) { + me.addNoteHistory(noteOrContent.NoteId, noteOrContent.Content); + } + } + }); + }); } } @@ -198,7 +220,8 @@ var Note = { searchNote: function(key, callback) { var reg = new RegExp(key); - Notes.find({IsTrash: false, LocalIsDelete: false, $or: [{Title: reg}, {Content: reg}]}).sort({'UpdatedTime': -1}).exec(function(err, notes) { + var userId = User.getCurActiveUserId(); + Notes.find({UserId: userId, IsTrash: false, LocalIsDelete: false, $or: [{Title: reg}, {Content: reg}]}).sort({'UpdatedTime': -1}).exec(function(err, notes) { if(!err && notes) { console.log('search ' + key + ' result: ' + notes.length); callback(notes); @@ -209,7 +232,8 @@ var Note = { }, searchNoteByTag: function(tag, callback) { - Notes.find({IsTrash: false, LocalIsDelete: false, Tags: {$in: [tag]}}).sort({'UpdatedTime': -1}).exec(function(err, notes) { + var userId = User.getCurActiveUserId(); + Notes.find({UserId: userId, IsTrash: false, LocalIsDelete: false, Tags: {$in: [tag]}}).sort({'UpdatedTime': -1}).exec(function(err, notes) { if(!err && notes) { console.log('search by tag: ' + tag + ' result: ' + notes.length); callback(notes); @@ -332,10 +356,10 @@ var Note = { content = content.replace(reg, Server.localUrl + '/api/file/getImage'); var reg2 = new RegExp(Evt.leanoteUrl + '/api/file/getAttach', 'g'); - content = content.replace(reg, Evt.localUrl + '/api/file/getAttach'); + content = content.replace(reg2, Evt.localUrl + '/api/file/getAttach'); var reg3 = new RegExp(Evt.leanoteUrl + '/api/file/getAllAttach', 'g'); - content = content.replace(reg, Evt.localUrl + '/api/file/getAllAttach'); + content = content.replace(reg3, Evt.localUrl + '/api/file/getAllAttach'); return content; }, @@ -345,15 +369,16 @@ var Note = { if(!content) { return content; } - console.log(Evt.localUrl + '/api/file/getImage'); + // console.log(Evt.localUrl + '/api/file/getImage'); + // console.log(content); var reg = new RegExp(Evt.localUrl + '/api/file/getImage', 'g'); content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage'); var reg2 = new RegExp(Evt.localUrl + '/api/file/getAttach', 'g'); - content = content.replace(reg, Evt.leanoteUrl + '/api/file/getAttach'); + content = content.replace(reg2, Evt.leanoteUrl + '/api/file/getAttach'); var reg3 = new RegExp(Evt.localUrl + '/api/file/getAllAttach', 'g'); - content = content.replace(reg, Evt.leanoteUrl + '/api/file/getAllAttach'); + content = content.replace(reg3, Evt.leanoteUrl + '/api/file/getAllAttach'); return content; }, @@ -452,12 +477,17 @@ var Note = { getNoteByServerNoteId: function(noteId, callback) { var me = this; - Notes.findOne({ServerNoteId: noteId}, function(err, doc) { + Notes.find({ServerNoteId: noteId}, function(err, doc) { // console.log(doc.length + '...'); + if(doc.length > 1) { + console.error(doc.length + '. ..'); + } + console.log('note length: ' + doc.length + '. ..'); if(err || !doc) { log('getNoteByServerNoteId 不存在' + noteId); callback && callback(false); } else { + doc = doc[0]; callback && callback(doc); } }); @@ -617,6 +647,8 @@ var Note = { console.log("updateNoteForce 后的") console.log(note); + console.log(note.ServerNoteId + " " + note.IsDirty); + console.log('ever note'); console.log(everNote.NoteId); console.log(everNote); @@ -632,6 +664,11 @@ var Note = { console.log('强制更新...'); callback && callback(note); + me.getNoteByServerNoteId(note.ServerNoteId, function(t) { + console.log('强制更新后的...'); + console.log(t); + }); + // 下载内容, 图片, 附件 me.syncContentAndImagesAndAttachs(note); } @@ -750,7 +787,7 @@ var Note = { // 新Id delete note['_id']; delete note['ServerNoteId']; - note.NoteId = Common.objectId(); + note.NoteId = Common.objectId(); // 新生成一个NoteId note.ConflictNoteId = noteId; // 与noteId有冲突 note.ConflictTime = new Date(); // 发生冲突时间 note.ConflictFixed = false; // 冲突未解决 @@ -794,7 +831,7 @@ var Note = { }, function() { note.Attachs = newAttachs; console.log('conflict 复制后的'); - console.log(note.Attachs); + console.log(note); Notes.insert(note, function(err, newNote) { if(err) { callback(false); @@ -888,6 +925,7 @@ var Note = { async.eachSeries(conflictNotes, function(note, cb) { // note是服务器上最新的, note.NoteId, ServerNoteId已转换 var noteId = note.NoteId; // 本地noteId // 复制一份, 本地的复制一份, 然后服务器上的替换本地的 + // newNote其实是现有的复制一份得到的 me.copyNoteForConfict(noteId, function(newNote) { if(newNote) { // 更新之前的 diff --git a/node_modules/sync.js b/node_modules/sync.js index f08e0004..efb0f056 100644 --- a/node_modules/sync.js +++ b/node_modules/sync.js @@ -278,7 +278,7 @@ var Sync = { Note.getNoteByServerNoteId(noteId, function(noteLocal) { // 2.1 本地没有, 表示是新建 if(!noteLocal) { - log('add: ...') + console.log('add: ...'); Note.addNoteForce(note, function(note) { me._syncInfo.note.adds.push(note); return canCall(); @@ -287,7 +287,10 @@ var Sync = { // 2.2 本地是否修改了, 冲突, 报告给前端, 前端处理 // 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的 if(noteLocal.IsDirty) { - log('note 冲突....') + console.log('note 冲突.... serverNoteId: ' + noteId); + console.log(noteLocal.NoteId); + console.log(noteLocal.IsDirty); + console.log(noteLocal); note.ServerNoteId = note.NoteId; note.NoteId = noteLocal.NoteId; me._syncInfo.note.conflicts.push(note); @@ -823,6 +826,7 @@ var Sync = { return cb(); } me._syncInfo.note.changeAdds.push(newTag); + // Tag.updateTagForce(newTag); me.checkNeedIncSyncAgain(newTag.Usn); cb(); }); @@ -831,6 +835,9 @@ var Sync = { Api.deleteTag(tag, function(ret) { if(Common.isOk(ret)) { me.checkNeedIncSyncAgain(ret.Usn); + } else { + // 有问题, 可能本地不存在 + Tag.setNotDirty(tag); } return cb(); }); diff --git a/node_modules/tag.js b/node_modules/tag.js index 6cf2315b..502ae672 100644 --- a/node_modules/tag.js +++ b/node_modules/tag.js @@ -97,6 +97,16 @@ var Tag = { }); }, + // 添加tag后的返回, 更新usn + updateTagForce: function(tag, callback) { + var me = this; + tag.IsDirty = false; + var userId = User.getCurActiveUserId(); + Tags.update({UserId: userId, Tag: tag.Tag}, {$set: tag}, function() { + callback && callback(); + }); + }, + // 服务器上更新过来, 已经存在了 setNotDirty: function(title) { var me = this; diff --git a/public/js/app/note.js b/public/js/app/note.js index 44e35be9..54ac8f47 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -39,7 +39,7 @@ Note.intervalTime = 600000; // 600s, 10mins Note.startInterval = function() { Note.interval = setInterval(function() { log("自动保存开始..."); - changedNote = Note.curChangedSaveIt(false); + // changedNote = Note.curChangedSaveIt(false); }, Note.intervalTime); // 600s, 10mins } // 停止, 当切换note时 @@ -177,6 +177,15 @@ Note.curNoteIsDirtied = function() { } }; +// 保存后不dirty +Note.curNoteIsNotDirtied = function() { + var me = this; + var note = me.getCurNote(); + if(note) { + note.isDirty = false; + } +}; + // called by Notebook // render 所有notes, 和第一个note的content Note.renderNotesAndFirstOneContent = function(ret) { @@ -237,17 +246,18 @@ Note.curHasChanged = function(force) { IsMarkdown: cacheNote.IsMarkdown, // 是否是markdown笔记 FromUserId: cacheNote.FromUserId, // 是否是共享新建的 NoteId: cacheNote.NoteId, - NotebookId: cacheNote.NotebookId, - Version: cacheNote.Version || 0, // 版本控制 + NotebookId: cacheNote.NotebookId }; if(hasChanged.IsNew) { $.extend(hasChanged, cacheNote); } else { - if(!cacheNote.isDirty) { - log("no dirty"); + if(!cacheNote.isDirty) { // 不是dirty + console.log("no dirty"); hasChanged.hasChanged = false; return hasChanged; + } else { + console.log("is dirty"); } } @@ -287,7 +297,7 @@ Note.curHasChanged = function(force) { hasChanged["UserId"] = cacheNote["UserId"] || ""; return hasChanged; -} +}; // 由content生成desc // 换行不要替换 @@ -435,6 +445,8 @@ Note.curChangedSaveIt = function(force, callback) { me.saveInProcess[hasChanged.NoteId] = true; + console.error('保存当前的笔记: ' + hasChanged.NoteId); + NoteService.updateNoteOrContent(hasChanged, function(ret) { me.saveInProcess[hasChanged.NoteId] = false; if(hasChanged.IsNew) { @@ -442,6 +454,9 @@ Note.curChangedSaveIt = function(force, callback) { ret.IsNew = false; Note.setNoteCache(ret, false); + // 设置不为dirty + Note.curNoteIsNotDirtied(hasChanged.NoteId); + // 新建笔记也要change history Pjax.changeNote(ret); } @@ -2372,7 +2387,9 @@ Note.fixSyncConflict = function(note, newNote) { // 如果当前笔记在笔记列表中, 那么生成一个新笔记放在这个笔记上面 if(target.length > 0) { var newHtmlObject = Note._getNoteHtmlObjct(note); - newHtmlObject.insertBefore(target); + if(newHtmlObject) { + newHtmlObject.insertBefore(target); + } } // 当前这个换成新复制的 target.attr('noteId', newNote.NoteId); diff --git a/test.js b/test.js index 744c7d4d..46681dcd 100755 --- a/test.js +++ b/test.js @@ -5,6 +5,7 @@ var Note = require('note'); var Api = require('api'); var User = require('user'); var Server = require('server'); +var Evt = require('evt'); /* Notebook.addNotebook("1", "life"); @@ -19,10 +20,14 @@ Api.addNotebook({ // Api.uploadImage(); User.userId = '54bdc65599c37b0da9000002'; User.userId = '54d7620d99c37b030600002c'; -User.userId = '54d7620d99c37b030600002c'; +User.userId = '54f08a1699c37b877b0001ee'; // 54d7624205fcd105da00005 +// var reg = new RegExp(Evt.localUrl + '/api/file/getImage', 'g'); +// content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage'); + + User.init(function() { /* @@ -31,15 +36,15 @@ User.init(function() { }); */ - Note.getNoteByServerNoteId('54d7624205fcd105da000005', function(note) { + + Note.getNoteByServerNoteId('54f1a1f899c37b4faf000001', function(note) { console.log(note); }); - -/* + Note.getDirtyNotes(function(ret) { - // console.log(ret); + console.log('getDirtyNotes: '); + console.log(ret); }); -*/ }); @@ -93,11 +98,19 @@ while((result = reg.exec(content)) != null) { console.log("??"); */ +/* var a = '