判断内容Dirty

This commit is contained in:
life
2017-01-20 15:00:15 +08:00
parent 21f5306f79
commit 2ea0816ef4
4 changed files with 85 additions and 58 deletions

View File

@@ -0,0 +1,8 @@
# 判断内容Dirty
page.js 绑定tinymce keydown事件, 在not readonly下, 只要按键就设为dirty = true
在切换笔记前, dirty=false
保存后, dirty=false
ace editor
只要keydown就是dirty

View File

@@ -303,6 +303,9 @@ Note.curHasChanged = function(force) {
//===========
// 内容的比较
// 是mardown编辑器, 或者富文本编辑器已Dirty
if (cacheNote.IsMarkdown || editorIsDirty()) {
// 如果是markdown返回[content, preview]
var contents = getEditorContent(cacheNote.IsMarkdown);
var content, preview;
@@ -322,6 +325,7 @@ Note.curHasChanged = function(force) {
// markdown编辑器还没准备好
throw new Error('markdown编辑器还没准备好');
}
// 富文本编辑器
else {
content = contents;
}
@@ -341,6 +345,12 @@ Note.curHasChanged = function(force) {
}
}
// 已保存了, 不再Dirty
setEditorIsDirty(false);
} else {
console.log('内容无修改', 'isMarkdown:' + cacheNote.IsMarkdown, 'isDirty:' + editorIsDirty());
}
if (hasChanged.hasChanged) {
return hasChanged;
}
@@ -446,6 +456,7 @@ Note.getImgSrc = function(content) {
};
Note.setNoteDirty = function (noteId, isDirty) {
console.trace('setNoteDirty');
var $leftNoteNav = $(tt('#noteItemList [noteId="?"]', noteId));
if (!isDirty) {
$leftNoteNav.removeClass('item-err');
@@ -533,7 +544,7 @@ Note.curChangedSaveIt = function(force, callback) {
content = content[0];
}
NoteService.addNoteHistory(me.curNoteId, content, callback);
console.log('已保存到历史中')
// console.log('已保存到历史中')
}
else {
console.log('不用保存 (^_^)');
@@ -2575,6 +2586,7 @@ var Attach = {
var src = EvtService.getAttachLocalUrl(attachId); // + "/attach/download?attachId=" + attachId;
// http://leanote.com/attach/download?attachId=54f7481638f4112ff000170f
Note.toggleWriteable();
if(LEA.isMarkdownEditor() && MD) {
MD.insertLink(src, attach.Title);
} else {
@@ -2582,26 +2594,6 @@ var Attach = {
}
});
// make all link
self.linkAllBtnO.on("click",function(e) {
// 暂不支持
return;
e.stopPropagation();
var note = Note.getCurNote();
if(!note) {
return;
}
var src = EvtService.getAllAttachLocalUrl(note.NoteId); // UrlPrefix + "/attach/downloadAll?noteId=" + Note.curNoteId
// src = 'http://leanote.com/attach/downloadAll?noteId=' + note.NoteId;
var title = note.Title ? note.Title + ".tar.gz" : "all.tar.gz";
if(LEA.isMarkdownEditor() && MD) {
MD.insertLink(src, title);
} else {
tinymce.activeEditor.insertContent('<a target="_blank" href="' + src + '">' + title + '</a>');
}
});
// 添加Attach
$('#chooseFile').click(function() {
gui.dialog.showOpenDialog(gui.getCurrentWindow(),

View File

@@ -1,6 +1,7 @@
// 主页渲染
//-------------
var noCharCodes = [37, 38, 39, 40, 16, 17, 18, 91];
var Resize;
// 写作模式
@@ -421,9 +422,8 @@ function initEditor() {
ed.on('keydown', function(e) {
var num = e.which ? e.which : e.keyCode;
// 如果是readony, 则不能做任何操作, 除了v, x, z
if(Note.readOnly && (
(e.ctrlKey || e.metaKey) && (num == 88 || num == 86 || num == 90)
)
if(Note.readOnly
&& ( (e.ctrlKey || e.metaKey) && (num == 88 || num == 86 || num == 90) )
) {
console.log('keydown preventDefault')
e.preventDefault();
@@ -438,6 +438,13 @@ function initEditor() {
return;
}
// 设置dirty, ctrl+c都设了
if (!Note.readOnly) {
if (noCharCodes.indexOf(num) >= 0) {
setEditorIsDirty(true);
}
}
/*
var num = e.which ? e.which : e.keyCode;
if(e.ctrlKey || e.metaKey) {
@@ -1183,7 +1190,13 @@ LeaAce = {
// 当ace里没有内容时, 连续删除则把ace remove掉
// keydown的delete事件没有
var lastDeleteTime;
// 上38下40左37右39
// shift16,ctrl17,option18,meta91
$("#editorContent").on('keyup', 'pre', function(e) {
if (LEA.readOnly) {
return;
}
var keyCode = e.keyCode;
// console.log('keyup');
if(keyCode == 8 || keyCode == 46) { // BackSpace || Delete
@@ -1206,6 +1219,12 @@ LeaAce = {
}
// console.log($(this));
}
if (noCharCodes.indexOf(keyCode) < 0) {
console.log('ace setEditorIsDirty')
setEditorIsDirty(true);
} else {
console.log('noCharCodes');
}
});
}
};

View File

@@ -361,6 +361,12 @@ function setEditorContent(content, isMarkdown, preview, callback) {
_setEditorContent(content, isMarkdown, preview, callback);
// });
}
function setEditorIsDirty(isDirty) {
tinymce.activeEditor.isNotDirty = !isDirty; // isDirty = false
}
function editorIsDirty() {
return !LEA.readOnly && tinymce.activeEditor.isDirty();
}
function _setEditorContent(content, isMarkdown, preview, callback) {
if(!content) {
content = "";
@@ -387,6 +393,8 @@ function _setEditorContent(content, isMarkdown, preview, callback) {
if(typeof tinymce != "undefined" && tinymce.activeEditor) {
var editor = tinymce.activeEditor;
editor.setContent(content);
setEditorIsDirty(false);
// editor.isNotDirty = true; // isDirty = false
callback && callback(); // Note.toggleReadOnly();