diff --git a/node_modules/api.js b/node_modules/api.js index e65a7c25..f155669a 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -611,23 +611,27 @@ var Api = { return callback(false); } var ret = resp.body; - log('delete note ret'); - log(ret); + console.error('delete note ret'); + console.log('delete note ret'); + console.log(ret); if(Common.isOk(ret)) { // 以后不要再发了 - Note.setNotDirty(note.NoteId); + Note.removeNote(note.NoteId); callback(ret); } else { callback(false); try { - log('delete note conflict'); + console.log('delete note conflict'); // 代表冲突了, 那么本地的删除无效, 设为IsDirty为false, 不删除 // 待以后同步 if(ret.Msg == 'conflict') { - log('delete note conflict: setNotDirtyNotDelete'); + console.log('delete note conflict: setNotDirtyNotDelete'); Note.setNotDirtyNotDelete(note.NoteId); + } else if(ret.Msg == 'notExists') { + console.log('delete note conflict: remove not exists'); + Note.removeNote(note.NoteId); } else { - log('delete note conflict: setNotDirty'); + console.log('delete note conflict: setNotDirty'); Note.setNotDirty(note.NoteId); } } catch(e) {} diff --git a/node_modules/note.js b/node_modules/note.js index 24872935..bdc4b72b 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -44,9 +44,11 @@ var Note = { var me = this; var userId = User.getCurActiveUserId(); noteOrContent['UserId'] = userId; - console.error("updateNoteOrContent") - console.trace('updateNoteOrContent: '); - console.log(noteOrContent); + + // console.error("updateNoteOrContent") + // console.trace('updateNoteOrContent: '); + // console.log(noteOrContent); + var date = new Date(); noteOrContent.UpdatedTime = date; @@ -103,28 +105,36 @@ var Note = { // 只有title, Content, Tags修改了才算是IsDirty if('Content' in updates && dbNote['Content'] != updates['Content']) { isDirty = true; + console.error(' content not same'); + console.log(dbNote['Content']); + console.log(updates['Content']); } else if('Title' in updates && dbNote['Title'] != updates['Title']) { isDirty = true; + console.error(' title not same'); } else if('Tags' in updates) { var dbTags = dbNote['Tags'] || []; var nowTags = updates['Tags'] || []; if(dbTags.join(',') != nowTags.join(',')) { isDirty = true; + console.error(' tag not same'); } } - console.log('update note isDirty: ' + noteOrContent.NoteId); - console.error(isDirty); + // console.log('update note isDirty: ' + noteOrContent.NoteId); + // console.error(isDirty); updates['IsDirty'] = isDirty; if(isDirty) { - console.error("NONO----------"); + console.error("NONO---is dirty-------"); // Web.alertWeb("??NONO"); } updates['LocalIsDelete'] = false; updates.UpdatedTime = date; + console.log('finally update:'); + console.log(updates); + // Set an existing field's value Notes.update({NoteId: noteOrContent.NoteId}, { $set: updates }, {}, function (err, numReplaced) { if(err) { @@ -251,17 +261,32 @@ var Note = { clearTrash: function(callback) { var me = this; var userId = User.getCurActiveUserId(); - Notes.update({UserId: userId, IsTrash: true}, {$set: {LocalIsDelete: true, IsDirty: true}}, {multi: true}, function(err, n) { - callback && callback(); + Notes.update( + {UserId: userId, IsTrash: true}, + {$set: {LocalIsDelete: true, IsDirty: true}}, + {multi: true}, + function(err, n) { + // Web.alertWeb(n); + callback && callback(); }); }, + deleteNote: function(noteId, callback) { - Notes.update({NoteId: noteId}, {$set: {IsTrash: true, IsDirty: true}}, function(err, n) { - if(err || !n) { + var me = this; + me.getNote(noteId, function(note) { + if(!note) { callback(false); - } else { - callback(true); } + Notes.update({NoteId: noteId}, {$set: {IsTrash: true, IsDirty: true}}, function(err, n) { + if(err || !n) { + callback(false); + } else { + callback(true); + + // 重新统计 + Notebook.reCountNotebookNumberNotes(note.NotebookId); + } + }); }); }, // 彻底删除笔记, 如果有tags, 则需要更新tags's count @@ -561,12 +586,20 @@ var Note = { // 有可能服务器上删除了是误删 ? deleteNoteForce: function(noteId, callback) { var me = this; - Notes.remove({ServerNoteId: noteId}, function(err, n) { - if(err) { + me.getNoteByServerNoteId(noteId, function(note) { + if(!note) { callback && callback(false); - } else { - callback && callback(true); + return; } + + Notes.remove({_id: note._id}, function(err, n) { + if(err) { + callback && callback(false); + } else { + Notebook.reCountNotebookNumberNotes(note.NotebookId); + callback && callback(true); + } + }); }); }, // 添加笔记本, note object @@ -610,6 +643,9 @@ var Note = { // console.log(note.CreatedTime); callback && callback(newDoc); + // 重新统计 + Notebook.reCountNotebookNumberNotes(note.NotebookId); + // 下载内容, 图片, 附件 me.syncContentAndImagesAndAttachs(newDoc); } @@ -721,6 +757,9 @@ var Note = { console.log(t); }); + // 重新统计之 + Notebook.reCountNotebookNumberNotes(note.NotebookId); + // 下载内容, 图片, 附件 me.syncContentAndImagesAndAttachs(note); } @@ -1185,7 +1224,10 @@ var Note = { // 在send delete笔记时成功 setNotDirty: function(noteId) { - Notes.update({NoteId: noteId}, {$set:{IsDirty: false}}) + Notes.update({NoteId: noteId}, {$set: {IsDirty: false}}) + }, + removeNote: function(noteId) { + Notes.remove({NoteId: noteId}); }, // 在send delete笔记时有冲突, 设为不删除 setNotDirtyNotDelete: function(noteId) { diff --git a/node_modules/notebook.js b/node_modules/notebook.js index aca1f493..56999376 100644 --- a/node_modules/notebook.js +++ b/node_modules/notebook.js @@ -3,6 +3,7 @@ var async = require('async'); var User = require('user'); var NB = db.notebooks; var Common = require('common'); +var Web = require('web'); function log(o) {console.log(o);} @@ -199,6 +200,7 @@ var Notebook = { */ }, + // addNote, 删除note, 移动note // 重新统计笔记本的笔记数据 reCountNotebookNumberNotes: function(notebookId) { db.notes.count({NotebookId: notebookId, IsTrash: false, LocalIsDelete: false}, function(err, count) { @@ -206,9 +208,7 @@ var Notebook = { log(err); return; } - log("count"); - log(notebookId); - log(count); + Web.updateNotebookNumberNotes(notebookId, count); NB.update({NotebookId: notebookId}, {$set: {NumberNotes: count}}, {}) }); }, @@ -404,14 +404,12 @@ var Notebook = { // notes是服务器的数据, 与本地的有冲突 // 1) 将本地的note复制一份 // 2) 服务器替换之前 - fixConflicts: function(notebookSyncInfo, notebookWeb, callback) { + fixConflicts: function(notebookSyncInfo, callback) { var me = this; - if(!notebookWeb) { - return callback && callback(); - } + // 处理冲突 var conflictNotebooks = notebookSyncInfo.conflicts || []; - log('fix notebook conflicts'); + console.log('fix notebook conflicts'); async.eachSeries(conflictNotebooks, function(notebook, cb) { /* var noteId = note.NoteId; diff --git a/node_modules/sync.js b/node_modules/sync.js index 554cac84..10323b20 100644 --- a/node_modules/sync.js +++ b/node_modules/sync.js @@ -339,8 +339,8 @@ var Sync = { } Api.getSyncNotes(afterUsn, me._noteMaxEntry, function(notes) { - log('syncNote') - log(notes); + console.log('syncNote---') + console.log(notes); if(Common.isOk(notes)) { me._totalSyncNoteNum += notes.length; // 证明可能还有要同步的 diff --git a/node_modules/web.js b/node_modules/web.js index fff641b3..4d51463d 100644 --- a/node_modules/web.js +++ b/node_modules/web.js @@ -129,5 +129,12 @@ var Web = { me.Note.syncProcess('In sync tags...'); } */ + + // 重新统计后, 显示到web上 + updateNotebookNumberNotes: function(notebookId, count) { + var me = this; + me.Notebook.updateNotebookNumberNotes(notebookId, count); + + } }; module.exports = Web; diff --git a/package.json b/package.json index ae396110..f4a5ffe4 100755 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "toolbar": true, "frame": true, - "transparent": true, + "transparent": false, "min_width": 258, "min_height": 326, diff --git a/public/js/app/note.js b/public/js/app/note.js index 94681c49..3cfa46f0 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -35,22 +35,31 @@ Note.notebookIds = {}; // notebookId => true Note.isReadOnly = false; // 定时保存信息 -Note.intervalTime = 600000; // 600s, 10mins +Note.intervalTime = 10 * 1000; // 600s, 10mins Note.startInterval = function() { + console.error("??"); + if(Note.interval) { + clearInterval(Note.interval); + } Note.interval = setInterval(function() { - log("自动保存开始..."); - // changedNote = Note.curChangedSaveIt(false); + console.log("start save interval..."); + changedNote = Note.curChangedSaveIt(false); + }, Note.intervalTime); // 600s, 10mins -} +}; + // 停止, 当切换note时 // 但过5000后自动启动 -Note.stopInterval = function() { +Note.stopInterval = function(notStartAuto) { clearInterval(Note.interval); - setTimeout(function() { - Note.startInterval(); - }, Note.intervalTime); -} + // 是否自动启动, 默认是自动启动 + if(!notStartAuto) { + setTimeout(function() { + Note.startInterval(); + }, Note.intervalTime); + } +}; // note = {NoteId, Desc, UserId,...} Note.addNoteCache = function(note) { @@ -246,6 +255,10 @@ Note.curHasChanged = function(force) { // 如果是markdown返回[content, preview] var contents = getEditorContent(cacheNote.IsMarkdown); + if(contents === false) { + // 表示编辑器未初始化, 此时肯定不能保存 + return false; + } var content, preview; var contentText; if (isArray(contents)) { @@ -324,6 +337,10 @@ Note.curHasChanged = function(force) { console.log("text相同"); console.log(cacheNote.Content == content); } + + console.error('hasChanged'); + console.log(Note.curNoteId); + console.log(hasChanged); hasChanged["UserId"] = cacheNote["UserId"] || ""; @@ -446,10 +463,12 @@ Note.curChangedSaveIt = function(force, callback) { if(!Note.curNoteId || Note.isReadOnly) { return; } + + console.error(">>"); var hasChanged = Note.curHasChanged(force); - if(hasChanged.hasChanged || hasChanged.IsNew) { + if(hasChanged && (hasChanged.hasChanged || hasChanged.IsNew)) { // 把已改变的渲染到左边 item-list Note.renderChangedNote(hasChanged); @@ -479,8 +498,8 @@ Note.curChangedSaveIt = function(force, callback) { // console.error('保存当前的笔记: ' + hasChanged.NoteId); // - console.error("why===================="); - console.trace("why"); + // console.error("why===================="); + // console.trace("why"); NoteService.updateNoteOrContent(hasChanged, function(ret) { me.saveInProcess[hasChanged.NoteId] = false; @@ -656,15 +675,20 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) { var target = $(tt('[noteId="?"]', selectNoteId)) Note.selectTarget(target); - // 1 之前的note, 判断是否已改变, 改变了就要保存之 - // 这里, 在搜索的时候总是保存, 搜索的话, 比较快, 肯定没有变化, 就不要执行该操作 - if(needSaveChanged == undefined) { - needSaveChanged = true; - } - if(needSaveChanged) { - var changedNote = Note.curChangedSaveIt(); - } - + + // 如果 inChangeNoteId == selectNoteId, 表示之前的note的content还在加载中, 此时保存笔记肯定出错 + // if(Note.inChangeNoteId != Note.curNoteId) { + + // 1 之前的note, 判断是否已改变, 改变了就要保存之 + // 这里, 在搜索的时候总是保存, 搜索的话, 比较快, 肯定没有变化, 就不要执行该操作 + if(needSaveChanged == undefined) { + needSaveChanged = true; + } + if(needSaveChanged) { + var changedNote = Note.curChangedSaveIt(); + } + // } + // 2. 设空, 防止在内容得到之前又发生保存 Note.curNoteId = ""; Note.inChangeNoteId = selectNoteId; @@ -679,7 +703,7 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) { isShare = true; } } - var hasPerm = !isShare || Share.hasUpdatePerm(selectNoteId); // 不是共享, 或者是共享但有权限 + var hasPerm = true; // !isShare || Share.hasUpdatePerm(selectNoteId); // 不是共享, 或者是共享但有权限 // 有权限 if(hasPerm) { @@ -687,7 +711,8 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) { Note.renderNote(cacheNote); // 这里要切换编辑器 - switchEditor(cacheNote.IsMarkdown) + switchEditor(cacheNote.IsMarkdown); + Note.hideEditorMask(); } else { Note.renderNoteReadOnly(cacheNote); } @@ -850,8 +875,11 @@ Note.renderNote = function(note) { Note.renderNoteContent = function(content) { // console.error('---------------- note:' + note.Title); // console.trace(); + setEditorContent(content.Content, content.IsMarkdown, content.Preview); + // console.log(content.NoteId + " => " + content.Content); + // 只有在renderNoteContent时才设置curNoteId Note.setCurNoteId(content.NoteId); @@ -1153,10 +1181,22 @@ Note._syncWarningE = $('#syncWarning'); Note.showSpin = function() { var me = this; me._syncRefreshE.addClass('fa-spin'); + + // 如果超过30秒还在转, 证明有问题了 + setTimeout(function() { + if(me._syncRefreshE.hasClass('fa-spin')) { + me._syncRefreshE.removeClass('fa-spin'); + } + }, 30 * 1000); + + // 禁止自动保存 + me.stopInterval(true); }; Note.hideSpin = function() { var me = this; me._syncRefreshE.removeClass('fa-spin'); + // 开始自动保存 + me.startInterval(); }; // nodejs调用 Note.syncFinished = function() { @@ -2533,22 +2573,13 @@ var Attach = { $(function() { // 附件初始化 Attach.init(); - - //----------------- - // 点击笔记展示之 - // 避免iphone, ipad两次点击 - // http://stackoverflow.com/questions/3038898/ipad-iphone-hover-problem-causes-the-user-to-double-click-a-link - $("#noteItemList").on("mouseenter", ".item", function(event) { - if(LEA.isIpad || LEA.isIphone) { - $(this).trigger("click"); - } - }); + $("#noteItemList").on("click", ".item", function(event) { // event.stopPropagation(); var noteId = $(this).attr("noteId"); // 手机端处理 - Mobile.changeNote(noteId); + // Mobile.changeNote(noteId); if(!noteId) { return; @@ -2740,19 +2771,33 @@ Note.updateSync = function(notes) { if(isEmpty(notes)) { return; } + + var curNotebookIsTrash = Notebook.curNotebookIsTrash(); + for(var i in notes) { var note = notes[i]; note.InitSync = true; // 需要重新获取内容 Note.addNoteCache(note); // 如果当前修改的是本笔记, 那么重新render之 - console.log('->>>'); - console.log(Note.curNoteId); + // console.log('->>>'); + // console.log(Note.curNoteId); if(Note.curNoteId == note.NoteId) { // 这里, 如果当前就是更新的, 则重新render, 有个问题, server新内容已经在服务器上了 Note.reRenderNote(Note.curNoteId); } + + // 如果是trash, 且当前不在trash目录下, 且有该笔记, 则删除之 + if(!curNotebookIsTrash && note.IsTrash) { + var target = $(tt('[noteId="?"]', note.NoteId)); + if(target.length) { + if(Note.curNoteId == note.NoteId) { + Note.changeToNext(target); + } + target.remove(); + } + } } -} +}; // 删除 Note.deleteSync = function(notes) { diff --git a/public/js/app/notebook.js b/public/js/app/notebook.js index b92ec83b..8d1d238c 100644 --- a/public/js/app/notebook.js +++ b/public/js/app/notebook.js @@ -25,6 +25,17 @@ Notebook.getCurNotebook = function() { return Notebook.cache[Notebook.curNotebookId]; }; +// 为了server Web调用 +Notebook.updateNotebookNumberNotes = function(notebookId, count) { + var self = this; + var notebook = self.getNotebook(notebookId); + if(!notebook) { + return; + } + notebook.NumberNotes = count; + $("#numberNotes_" + notebookId).html(count); +}; + // 笔记本的笔记数量更新 Notebook._updateNotebookNumberNotes = function(notebookId, n) { var self = this; @@ -1188,4 +1199,4 @@ Notebook.deleteSync = function(notebooks) { // 删除 me.deleteNotebookFromTree(notebookId); } -} \ No newline at end of file +}; diff --git a/public/js/app/page.js b/public/js/app/page.js index 17d76903..f748e574 100644 --- a/public/js/app/page.js +++ b/public/js/app/page.js @@ -1183,6 +1183,9 @@ var State = { // 判断是否登录 function initPage() { win.on('close', function() { + // 先保存之前改变的 + Note.curChangedSaveIt(); + // 保存状态 State.saveCurState(function() { win.close(true); }); diff --git a/public/js/common.js b/public/js/common.js index 952c2d93..bdadca55 100644 --- a/public/js/common.js +++ b/public/js/common.js @@ -459,6 +459,7 @@ function previewIsEmpty(preview) { } // 有tinymce得到的content有包围 +// false表示编辑器未初始化 function getEditorContent(isMarkdown) { if(!isMarkdown) { var editor = tinymce.activeEditor; @@ -513,11 +514,15 @@ function getEditorContent(isMarkdown) { } } return content; + } else { + return false; } } else { // return [$("#wmd-input").val(), $("#wmd-preview").html()] if(MD) { return [MD.getContent(), '