From 2ea0816ef42047879b7e899cb9491ce20b2c0ac7 Mon Sep 17 00:00:00 2001 From: life Date: Fri, 20 Jan 2017 15:00:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=86=85=E5=AE=B9Dirty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/判断内容Dirty.md | 8 ++++ public/js/app/note.js | 102 +++++++++++++++++++----------------------- public/js/app/page.js | 25 +++++++++-- public/js/common.js | 8 ++++ 4 files changed, 85 insertions(+), 58 deletions(-) create mode 100644 docs/判断内容Dirty.md diff --git a/docs/判断内容Dirty.md b/docs/判断内容Dirty.md new file mode 100644 index 00000000..c922b9de --- /dev/null +++ b/docs/判断内容Dirty.md @@ -0,0 +1,8 @@ +# 判断内容Dirty + +page.js 绑定tinymce keydown事件, 在not readonly下, 只要按键就设为dirty = true +在切换笔记前, dirty=false +保存后, dirty=false + +ace editor +只要keydown就是dirty \ No newline at end of file diff --git a/public/js/app/note.js b/public/js/app/note.js index bd95bb44..462d31a0 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -303,42 +303,52 @@ Note.curHasChanged = function(force) { //=========== // 内容的比较 - // 如果是markdown返回[content, preview] - var contents = getEditorContent(cacheNote.IsMarkdown); - var content, preview; - if (isArray(contents)) { - content = contents[0]; - preview = contents[1]; - // preview可能没来得到及解析 - if (content && previewIsEmpty(preview) && Converter) { - preview = Converter.makeHtml(content); - } - if(!content) { - preview = ""; - } - cacheNote.Preview = preview; // 仅仅缓存在前台 - } else if (cacheNote.IsMarkdown && typeof contents === 'boolean') { - // 不会出现, 因为刚开始时readOnly=true, 且只有设置内容完成后才setCurNoteId - // markdown编辑器还没准备好 - throw new Error('markdown编辑器还没准备好'); - } - else { - content = contents; - } + // 是mardown编辑器, 或者富文本编辑器已Dirty + if (cacheNote.IsMarkdown || editorIsDirty()) { - if (cacheNote.Content != content) { - hasChanged.hasChanged = true; - hasChanged.Content = content; - - // 从html中得到... - var c = preview || content; - - // 不是博客或没有自定义设置的 - if(!cacheNote.HasSelfDefined || !cacheNote.IsBlog) { - hasChanged.Desc = Note.genDesc(c); - hasChanged.ImgSrc = Note.getImgSrc(c); - hasChanged.Abstract = Note.genAbstract(c); + // 如果是markdown返回[content, preview] + var contents = getEditorContent(cacheNote.IsMarkdown); + var content, preview; + if (isArray(contents)) { + content = contents[0]; + preview = contents[1]; + // preview可能没来得到及解析 + if (content && previewIsEmpty(preview) && Converter) { + preview = Converter.makeHtml(content); + } + if(!content) { + preview = ""; + } + cacheNote.Preview = preview; // 仅仅缓存在前台 + } else if (cacheNote.IsMarkdown && typeof contents === 'boolean') { + // 不会出现, 因为刚开始时readOnly=true, 且只有设置内容完成后才setCurNoteId + // markdown编辑器还没准备好 + throw new Error('markdown编辑器还没准备好'); } + // 富文本编辑器 + else { + content = contents; + } + + if (cacheNote.Content != content) { + hasChanged.hasChanged = true; + hasChanged.Content = content; + + // 从html中得到... + var c = preview || content; + + // 不是博客或没有自定义设置的 + if(!cacheNote.HasSelfDefined || !cacheNote.IsBlog) { + hasChanged.Desc = Note.genDesc(c); + hasChanged.ImgSrc = Note.getImgSrc(c); + hasChanged.Abstract = Note.genAbstract(c); + } + } + + // 已保存了, 不再Dirty + setEditorIsDirty(false); + } else { + console.log('内容无修改', 'isMarkdown:' + cacheNote.IsMarkdown, 'isDirty:' + editorIsDirty()); } if (hasChanged.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('' + title + ''); - } - }); - // 添加Attach $('#chooseFile').click(function() { gui.dialog.showOpenDialog(gui.getCurrentWindow(), diff --git a/public/js/app/page.js b/public/js/app/page.js index 60254f50..2815caf5 100644 --- a/public/js/app/page.js +++ b/public/js/app/page.js @@ -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'); + } }); } }; diff --git a/public/js/common.js b/public/js/common.js index 0ae96ddf..f674b3fa 100644 --- a/public/js/common.js +++ b/public/js/common.js @@ -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();