笔记本数量问题 , APP端的笔记不能自动保存, APP端的笔记不能自动保存. [ok] 同步删除笔记问题

笔记本数量问题 [ok]
两次粘贴问题 [ok]
APP端的笔记不能自动保存. [ok]
同步删除笔记问题 [ok]
        执行以下操作:APP内添加两篇笔记,手动同步到网页,网页可见.此时在网页中删掉其中一篇笔记后保存,网页可
达到预期效果.回到APP后手动同步, APP内的笔记仍然是2条,未作任何删改.已反复尝试证实. [ok]
This commit is contained in:
life
2015-03-15 01:34:16 +08:00
parent b00c3080b8
commit aba15ad087
11 changed files with 196 additions and 77 deletions

16
node_modules/api.js generated vendored
View File

@@ -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) {}

58
node_modules/note.js generated vendored
View File

@@ -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,18 +261,33 @@ 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) {
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) {
var me = this;
me.getNote(noteId, function(note) {
if(!note) {
callback(false);
}
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
deleteTrash: function(noteId, callback) {
@@ -561,13 +586,21 @@ var Note = {
// 有可能服务器上删除了是误删 ?
deleteNoteForce: function(noteId, callback) {
var me = this;
Notes.remove({ServerNoteId: noteId}, function(err, n) {
me.getNoteByServerNoteId(noteId, function(note) {
if(!note) {
callback && callback(false);
return;
}
Notes.remove({_id: note._id}, function(err, n) {
if(err) {
callback && callback(false);
} else {
Notebook.reCountNotebookNumberNotes(note.NotebookId);
callback && callback(true);
}
});
});
},
// 添加笔记本, note object
// note是服务器传过来的, 需要处理下fix
@@ -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);
}
@@ -1187,6 +1226,9 @@ var Note = {
setNotDirty: function(noteId) {
Notes.update({NoteId: noteId}, {$set: {IsDirty: false}})
},
removeNote: function(noteId) {
Notes.remove({NoteId: noteId});
},
// 在send delete笔记时有冲突, 设为不删除
setNotDirtyNotDelete: function(noteId) {
Notes.update({NoteId: noteId}, {$set:{IsDirty: false, LocalIsDelete: false}})

14
node_modules/notebook.js generated vendored
View File

@@ -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;

4
node_modules/sync.js generated vendored
View File

@@ -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;
// 证明可能还有要同步的

7
node_modules/web.js generated vendored
View File

@@ -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;

View File

@@ -8,7 +8,7 @@
"toolbar": true,
"frame": true,
"transparent": true,
"transparent": false,
"min_width": 258,
"min_height": 326,

View File

@@ -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() {
Note.interval = setInterval(function() {
log("自动保存开始...");
// changedNote = Note.curChangedSaveIt(false);
}, Note.intervalTime); // 600s, 10mins
console.error("??");
if(Note.interval) {
clearInterval(Note.interval);
}
Note.interval = setInterval(function() {
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);
// 是否自动启动, 默认是自动启动
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)) {
@@ -325,6 +338,10 @@ Note.curHasChanged = function(force) {
console.log(cacheNote.Content == content);
}
console.error('hasChanged');
console.log(Note.curNoteId);
console.log(hasChanged);
hasChanged["UserId"] = cacheNote["UserId"] || "";
return hasChanged;
@@ -447,9 +464,11 @@ Note.curChangedSaveIt = function(force, callback) {
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,6 +675,10 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) {
var target = $(tt('[noteId="?"]', selectNoteId))
Note.selectTarget(target);
// 如果 inChangeNoteId == selectNoteId, 表示之前的note的content还在加载中, 此时保存笔记肯定出错
// if(Note.inChangeNoteId != Note.curNoteId) {
// 1 之前的note, 判断是否已改变, 改变了就要保存之
// 这里, 在搜索的时候总是保存, 搜索的话, 比较快, 肯定没有变化, 就不要执行该操作
if(needSaveChanged == undefined) {
@@ -664,6 +687,7 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) {
if(needSaveChanged) {
var changedNote = Note.curChangedSaveIt();
}
// }
// 2. 设空, 防止在内容得到之前又发生保存
Note.curNoteId = "";
@@ -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() {
@@ -2534,21 +2574,12 @@ $(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) {

View File

@@ -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);
}
}
};

View File

@@ -1183,6 +1183,9 @@ var State = {
// 判断是否登录
function initPage() {
win.on('close', function() {
// 先保存之前改变的
Note.curChangedSaveIt();
// 保存状态
State.saveCurState(function() {
win.close(true);
});

View File

@@ -459,6 +459,7 @@ function previewIsEmpty(preview) {
}
// 有tinymce得到的content有<html>包围
// 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(), '<div>' + $("#preview-contents").html() + '</div>']
} else {
return false;
}
}
}

14
test.js
View File

@@ -21,7 +21,7 @@ Api.addNotebook({
// Api.uploadImage();
User.userId = '54bdc65599c37b0da9000002';
User.userId = '54d7620d99c37b030600002c';
User.userId = '545b26ad38f4116d08000029';
User.userId = '5503c84c99c37b22a4000003';
// 54d7624205fcd105da00005
@@ -38,18 +38,22 @@ User.init(function() {
});
*/
/*
Notebook.getDirtyNotebooks(function(notebooks) {
console.log(notebooks);
})
});
*/
// Note.getNoteByServerNoteId('54f1a1f899c37b4faf000001', function(note) {
// console.log(note);
// });
// Note.getNotes('', function(ret) {
// console.log(ret);
// });
Note.getTrashNotes(function(ret) {
console.log(ret);
console.log(ret.length);
});
});