Tweaked comments extension

This commit is contained in:
benweet 2014-08-28 00:10:36 +01:00
parent 9d038fae7c
commit ba90a57646
3 changed files with 20 additions and 10 deletions

View File

@ -288,14 +288,18 @@ define([
var nextTickAdjustScroll = false; var nextTickAdjustScroll = false;
var debouncedSave = utils.debounce(function() { var debouncedSave = utils.debounce(function() {
save(); save();
if(lastSelectionStart == self.selectionStart && lastSelectionEnd == self.selectionEnd) { if(lastSelectionStart === self.selectionStart && lastSelectionEnd === self.selectionEnd) {
nextTickAdjustScroll = false; nextTickAdjustScroll = false;
} }
self.updateCursorCoordinates(nextTickAdjustScroll); self.updateCursorCoordinates(nextTickAdjustScroll);
nextTickAdjustScroll = false; nextTickAdjustScroll = false;
}); });
return function(debounced, adjustScroll) { return function(debounced, adjustScroll, forceAdjustScroll) {
if(forceAdjustScroll) {
lastSelectionStart = undefined;
lastSelectionEnd = undefined;
}
if(debounced) { if(debounced) {
nextTickAdjustScroll = nextTickAdjustScroll || adjustScroll; nextTickAdjustScroll = nextTickAdjustScroll || adjustScroll;
return debouncedSave(); return debouncedSave();
@ -386,11 +390,11 @@ define([
editor.selectionMgr = selectionMgr; editor.selectionMgr = selectionMgr;
$(document).on('selectionchange', '.editor-content', _.bind(selectionMgr.saveSelectionState, selectionMgr, true, false)); $(document).on('selectionchange', '.editor-content', _.bind(selectionMgr.saveSelectionState, selectionMgr, true, false));
function adjustCursorPosition() { function adjustCursorPosition(force) {
if(inputElt === undefined) { if(inputElt === undefined) {
return; return;
} }
selectionMgr.saveSelectionState(true, true); selectionMgr.saveSelectionState(true, true, force);
} }
editor.adjustCursorPosition = adjustCursorPosition; editor.adjustCursorPosition = adjustCursorPosition;

View File

@ -66,6 +66,7 @@ define([
var newCommentElt = crel('a', { var newCommentElt = crel('a', {
class: 'discussion icon-comment new' class: 'discussion icon-comment new'
}); });
var newCommentEltY;
function Context(commentElt, fileDesc) { function Context(commentElt, fileDesc) {
this.commentElt = commentElt; this.commentElt = commentElt;
@ -131,9 +132,8 @@ define([
delete commentEltMap[commentElt.discussionIndex]; delete commentEltMap[commentElt.discussionIndex];
}); });
// Move newCommentElt // Move newCommentElt
setCommentEltCoordinates(newCommentElt, newCommentEltY, true);
if(currentContext && !currentContext.discussionIndex) { if(currentContext && !currentContext.discussionIndex) {
coordinates = selectionMgr.getCoordinates(currentContext.getDiscussion().selectionEnd);
setCommentEltCoordinates(newCommentElt, coordinates.y, true);
inputElt.scrollTop += parseInt(newCommentElt.style.top) - inputElt.scrollTop - inputElt.offsetHeight * 3 / 4; inputElt.scrollTop += parseInt(newCommentElt.style.top) - inputElt.scrollTop - inputElt.offsetHeight * 3 / 4;
movePopover(newCommentElt); movePopover(newCommentElt);
} }
@ -190,6 +190,11 @@ define([
currentFileDesc === fileDesc && refreshDiscussions(); currentFileDesc === fileDesc && refreshDiscussions();
}; };
comments.onCursorCoordinates = function(x, y) {
newCommentEltY = y;
setCommentEltCoordinates(newCommentElt, y, true);
};
comments.onCommentsChanged = function(fileDesc) { comments.onCommentsChanged = function(fileDesc) {
if(currentFileDesc !== fileDesc) { if(currentFileDesc !== fileDesc) {
return; return;
@ -333,8 +338,6 @@ define([
commentList: [] commentList: []
}; };
currentFileDesc.newDiscussion = discussion; currentFileDesc.newDiscussion = discussion;
var coordinates = selectionMgr.getCoordinates(selectionStart);
setCommentEltCoordinates(newCommentElt, coordinates.y, true);
} }
context.selectionRange = selectionMgr.createRange(discussion.selectionStart, discussion.selectionEnd); context.selectionRange = selectionMgr.createRange(discussion.selectionStart, discussion.selectionEnd);
inputElt.scrollTop += parseInt(evt.target.style.top) - inputElt.scrollTop - inputElt.offsetHeight * 3 / 4; inputElt.scrollTop += parseInt(evt.target.style.top) - inputElt.scrollTop - inputElt.offsetHeight * 3 / 4;
@ -466,11 +469,14 @@ define([
$commentElt = $(sortedCommentEltList[(curentIndex + 1)]); $commentElt = $(sortedCommentEltList[(curentIndex + 1)]);
} }
} }
else if(selectionMgr.selectionStart === selectionMgr.selectionEnd && sortedCommentEltList.length) {
$commentElt = $(_.first(sortedCommentEltList));
}
if($commentElt.length === 0) { if($commentElt.length === 0) {
// Close the popover properly // Close the popover properly
closeCurrentPopover(); closeCurrentPopover();
editor.focus(); editor.focus();
editor.adjustCursorPosition(); editor.adjustCursorPosition(true);
} }
else { else {
$commentElt.click(); $commentElt.click();

View File

@ -2,7 +2,7 @@
<div class="discussion-comment-list"><%= commentList %></div> <div class="discussion-comment-list"><%= commentList %></div>
<div class="new-comment-block"> <div class="new-comment-block">
<div class="form-group"> <div class="form-group">
<i class="icon-comment"></i> <input class="form-control input-comment-author" placeholder="Your name"></input> <i class="icon-comment"></i> <input class="form-control input-comment-author" placeholder="Your name here!"></input>
<textarea class="form-control input-comment-content"></textarea> <textarea class="form-control input-comment-content"></textarea>
</div> </div>
<div class="form-group text-right"> <div class="form-group text-right">