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 = $('