From 9af08581860f13167b89ca01034767b2d6a86fd1 Mon Sep 17 00:00:00 2001 From: benweet Date: Thu, 17 Apr 2014 19:51:41 +0100 Subject: [PATCH] Fixed comments extensions --- public/res/editor.js | 1 + public/res/eventMgr.js | 2 +- public/res/extensions/comments.js | 27 ++++++------ .../{scrollLink.js => scrollSync.js} | 44 +++++++++---------- 4 files changed, 38 insertions(+), 36 deletions(-) rename public/res/extensions/{scrollLink.js => scrollSync.js} (89%) diff --git a/public/res/editor.js b/public/res/editor.js index 2ce0e607..286f2a62 100644 --- a/public/res/editor.js +++ b/public/res/editor.js @@ -317,6 +317,7 @@ define([ }; })(); eventMgr.addListener('onLayoutResize', adjustCursorPosition); + editor.adjustCursorPosition = adjustCursorPosition; var textContent; function setValue(value) { diff --git a/public/res/eventMgr.js b/public/res/eventMgr.js index eec8f904..f8843eef 100644 --- a/public/res/eventMgr.js +++ b/public/res/eventMgr.js @@ -28,7 +28,7 @@ define([ "extensions/toc", "extensions/mathJax", "extensions/emailConverter", - "extensions/scrollLink", + "extensions/scrollSync", "extensions/buttonSync", "extensions/buttonPublish", "extensions/buttonStat", diff --git a/public/res/extensions/comments.js b/public/res/extensions/comments.js index df673f94..305184d4 100644 --- a/public/res/extensions/comments.js +++ b/public/res/extensions/comments.js @@ -69,11 +69,6 @@ define([ var newCommentElt = crel('a', { class: 'discussion icon-comment new' }); - var cursorY; - comments.onCursorCoordinates = function(x, y) { - cursorY = y; - setCommentEltCoordinates(newCommentElt, cursorY); - }; function Context(commentElt, fileDesc) { this.commentElt = commentElt; @@ -124,6 +119,7 @@ define([ offsetMap = {}; var discussionList = _.values(currentFileDesc.discussionList); function refreshOne() { + var coordinates; if(discussionList.length === 0) { // Remove outdated commentElt _.filter(commentEltMap, function(commentElt, discussionIndex) { @@ -133,8 +129,9 @@ define([ delete commentEltMap[commentElt.discussionIndex]; }); // Move newCommentElt - setCommentEltCoordinates(newCommentElt, cursorY); if(currentContext && !currentContext.discussionIndex) { + coordinates = selectionMgr.getCoordinates(currentContext.getDiscussion().selectionEnd); + setCommentEltCoordinates(newCommentElt, coordinates.y); inputElt.scrollTop += parseInt(newCommentElt.style.top) - inputElt.scrollTop - inputElt.offsetHeight * 3 / 4; movePopover(newCommentElt); } @@ -163,7 +160,7 @@ define([ className += isReplied ? ' replied' : ' added'; commentElt.className = className; commentElt.discussionIndex = discussion.discussionIndex; - var coordinates = selectionMgr.getCoordinates(discussion.selectionEnd); + coordinates = selectionMgr.getCoordinates(discussion.selectionEnd); var lineIndex = setCommentEltCoordinates(commentElt, coordinates.y); offsetMap[lineIndex] = (offsetMap[lineIndex] || 0) + 1; @@ -325,8 +322,10 @@ define([ commentList: [] }; currentFileDesc.newDiscussion = discussion; + var coordinates = selectionMgr.getCoordinates(selectionStart); + setCommentEltCoordinates(newCommentElt, coordinates.y); } - context.selectionRange = selectionMgr.setSelectionStartEnd(discussion.selectionStart, discussion.selectionEnd, undefined, true); + context.selectionRange = selectionMgr.createRange(discussion.selectionStart, discussion.selectionEnd); inputElt.scrollTop += parseInt(evt.target.style.top) - inputElt.scrollTop - inputElt.offsetHeight * 3 / 4; }).on('shown.bs.popover', '#wmd-input > .editor-margin', function(evt) { @@ -351,7 +350,8 @@ define([ case 27: evt.preventDefault(); closeCurrentPopover(); - inputElt.focus(); + editor.focus(); + editor.adjustCursorPosition(); return; } }); @@ -445,7 +445,7 @@ define([ var $commentElt = $newCommentElt; if(currentContext) { if(!currentContext.discussionIndex) { - $commentElt = $(_.first(sortedCommentEltList) || newCommentElt); + $commentElt = $(_.first(sortedCommentEltList)); } else { var curentIndex = -1; @@ -455,13 +455,14 @@ define([ return true; } }); - $commentElt = $(sortedCommentEltList[(curentIndex + 1) % sortedCommentEltList.length]); + $commentElt = $(sortedCommentEltList[(curentIndex + 1)]); } } - if(currentContext && currentContext.commentElt === $commentElt[0]) { + if($commentElt.length === 0) { // Close the popover properly closeCurrentPopover(); - inputElt.focus(); + editor.focus(); + editor.adjustCursorPosition(); } else { $commentElt.click(); diff --git a/public/res/extensions/scrollLink.js b/public/res/extensions/scrollSync.js similarity index 89% rename from public/res/extensions/scrollLink.js rename to public/res/extensions/scrollSync.js index cc46f2f4..0b8ba669 100644 --- a/public/res/extensions/scrollLink.js +++ b/public/res/extensions/scrollSync.js @@ -2,23 +2,23 @@ define([ "jquery", "underscore", "classes/Extension", - "text!html/scrollLinkSettingsBlock.html" -], function($, _, Extension, scrollLinkSettingsBlockHTML) { + "text!html/scrollSyncSettingsBlock.html" +], function($, _, Extension, scrollSyncSettingsBlockHTML) { - var scrollLink = new Extension("scrollLink", "Scroll Link", true, true); - scrollLink.settingsBlock = scrollLinkSettingsBlockHTML; + var scrollSync = new Extension("scrollSync", "Scroll Link", true, true); + scrollSync.settingsBlock = scrollSyncSettingsBlockHTML; $.easing.easeOutSine = function( p ) { return Math.cos((1 - p) * Math.PI / 2 ); }; var sectionList; - scrollLink.onSectionsCreated = function(sectionListParam) { + scrollSync.onSectionsCreated = function(sectionListParam) { sectionList = sectionListParam; }; var isPreviewVisible = true; - scrollLink.onPreviewToggle = function(isOpen) { + scrollSync.onPreviewToggle = function(isOpen) { isPreviewVisible = isOpen; }; @@ -91,7 +91,7 @@ define([ // apply Scroll Link (-10 to have a gap > 9px) lastEditorScrollTop = -10; lastPreviewScrollTop = -10; - doScrollLink(); + doScrollSync(); }, 500); var isScrollEditor = false; @@ -99,7 +99,7 @@ define([ var isEditorMoving = false; var isPreviewMoving = false; var scrollingHelper = $('
'); - var doScrollLink = _.throttle(function() { + var doScrollSync = _.throttle(function() { if(!isPreviewVisible || mdSectionList.length === 0 || mdSectionList.length !== htmlSectionList.length) { return; } @@ -140,12 +140,12 @@ define([ lastPreviewScrollTop = previewScrollTop; return; } - scrollingHelper.stop('scrollLinkFx', true).css('value', 0).animate({ + scrollingHelper.stop('scrollSyncFx', true).css('value', 0).animate({ value: destScrollTop - previewScrollTop }, { easing: 'easeOutSine', duration: 200, - queue: 'scrollLinkFx', + queue: 'scrollSyncFx', step: function(now) { isPreviewMoving = true; lastPreviewScrollTop = previewScrollTop + now; @@ -156,7 +156,7 @@ define([ isPreviewMoving = false; }, 10); }, - }).dequeue('scrollLinkFx'); + }).dequeue('scrollSyncFx'); } else if(isScrollPreview === true) { @@ -176,12 +176,12 @@ define([ lastEditorScrollTop = editorScrollTop; return; } - scrollingHelper.stop('scrollLinkFx', true).css('value', 0).animate({ + scrollingHelper.stop('scrollSyncFx', true).css('value', 0).animate({ value: destScrollTop - editorScrollTop }, { easing: 'easeOutSine', duration: 200, - queue: 'scrollLinkFx', + queue: 'scrollSyncFx', step: function(now) { isEditorMoving = true; lastEditorScrollTop = editorScrollTop + now; @@ -192,21 +192,21 @@ define([ isEditorMoving = false; }, 10); }, - }).dequeue('scrollLinkFx'); + }).dequeue('scrollSyncFx'); } }, 100); - scrollLink.onLayoutResize = function() { + scrollSync.onLayoutResize = function() { isScrollEditor = true; buildSections(); }; - scrollLink.onFileClosed = function() { + scrollSync.onFileClosed = function() { mdSectionList = []; }; var scrollAdjust = false; - scrollLink.onReady = function() { + scrollSync.onReady = function() { $previewElt = $(".preview-container"); $editorElt = $("#wmd-input"); @@ -214,7 +214,7 @@ define([ if(isPreviewMoving === false && scrollAdjust === false) { isScrollPreview = true; isScrollEditor = false; - doScrollLink(); + doScrollSync(); } scrollAdjust = false; }); @@ -222,13 +222,13 @@ define([ if(isEditorMoving === false) { isScrollEditor = true; isScrollPreview = false; - doScrollLink(); + doScrollSync(); } }); }; var $previewContentsElt; - scrollLink.onPagedownConfigure = function(editor) { + scrollSync.onPagedownConfigure = function(editor) { $previewContentsElt = $("#preview-contents"); editor.getConverter().hooks.chain("postConversion", function(text) { // To avoid losing scrolling position before elements are fully loaded @@ -237,7 +237,7 @@ define([ }); }; - scrollLink.onPreviewFinished = function() { + scrollSync.onPreviewFinished = function() { // Now set the correct height var previousHeight = $previewContentsElt.height(); $previewContentsElt.height("auto"); @@ -250,5 +250,5 @@ define([ buildSections(); }; - return scrollLink; + return scrollSync; });