Fixed className leak in undo/redo buttons

This commit is contained in:
benweet 2014-05-23 23:58:18 +01:00
parent 349d43bd0d
commit 492f5ffbc0
3 changed files with 461 additions and 447 deletions

View File

@ -27,6 +27,7 @@ define([
var pagedownEditor; var pagedownEditor;
var refreshPreviewLater = (function() { var refreshPreviewLater = (function() {
var elapsedTime = 0; var elapsedTime = 0;
var timeoutId;
var refreshPreview = function() { var refreshPreview = function() {
var startTime = Date.now(); var startTime = Date.now();
pagedownEditor.refreshPreview(); pagedownEditor.refreshPreview();
@ -36,7 +37,8 @@ define([
return _.debounce(refreshPreview, 500); return _.debounce(refreshPreview, 500);
} }
return function() { return function() {
setTimeout(refreshPreview, elapsedTime < 2000 ? elapsedTime : 2000); clearTimeout(timeoutId);
timeoutId = setTimeout(refreshPreview, elapsedTime < 2000 ? elapsedTime : 2000);
}; };
})(); })();
eventMgr.addListener('onPagedownConfigure', function(pagedownEditorParam) { eventMgr.addListener('onPagedownConfigure', function(pagedownEditorParam) {
@ -174,9 +176,10 @@ define([
var min = Math.min(this.selectionStart, this.selectionEnd); var min = Math.min(this.selectionStart, this.selectionEnd);
var max = Math.max(this.selectionStart, this.selectionEnd); var max = Math.max(this.selectionStart, this.selectionEnd);
var range = this.createRange(min, max); var range = this.createRange(min, max);
var selection = document.getSelection(); var selection = rangy.getSelection();
selection.removeAllRanges(); selection.removeAllRanges();
selection.addRange(range); selection.addRange(range, this.selectionStart > this.selectionEnd);
selection.detach();
range.detach(); range.detach();
}; };
this.setSelectionStartEnd = function(start, end) { this.setSelectionStartEnd = function(start, end) {
@ -203,7 +206,7 @@ define([
var selectionStart = self.selectionStart; var selectionStart = self.selectionStart;
var selectionEnd = self.selectionEnd; var selectionEnd = self.selectionEnd;
var range; var range;
var selection = document.getSelection(); var selection = rangy.getSelection();
if(selection.rangeCount > 0) { if(selection.rangeCount > 0) {
var selectionRange = selection.getRangeAt(0); var selectionRange = selection.getRangeAt(0);
var element = selectionRange.startContainer; var element = selectionRange.startContainer;
@ -220,7 +223,7 @@ define([
element = container = container.parentNode; element = container = container.parentNode;
} while(element && element != inputElt); } while(element && element != inputElt);
if(false) { if(selection.isBackwards()) {
selectionStart = offset + (range + '').length; selectionStart = offset + (range + '').length;
selectionEnd = offset; selectionEnd = offset;
} }
@ -231,6 +234,7 @@ define([
} }
selectionRange.detach(); selectionRange.detach();
} }
selection.detach();
self.setSelectionStartEnd(selectionStart, selectionEnd); self.setSelectionStartEnd(selectionStart, selectionEnd);
} }
undoMgr.saveSelectionState(); undoMgr.saveSelectionState();

View File

@ -40,6 +40,7 @@ define([
}; };
var yList = []; var yList = [];
function setCommentEltCoordinates(commentElt, y, isNew) { function setCommentEltCoordinates(commentElt, y, isNew) {
y = Math.round(y); y = Math.round(y);
var yListIndex = y - 21; var yListIndex = y - 21;
@ -72,6 +73,7 @@ define([
this.fileDesc = fileDesc; this.fileDesc = fileDesc;
this.discussionIndex = commentElt.discussionIndex; this.discussionIndex = commentElt.discussionIndex;
} }
Context.prototype.getDiscussion = function() { Context.prototype.getDiscussion = function() {
if(!this.discussionIndex) { if(!this.discussionIndex) {
return this.fileDesc.newDiscussion; return this.fileDesc.newDiscussion;
@ -82,6 +84,7 @@ define([
return document.querySelector('.comments-popover .popover:last-child'); return document.querySelector('.comments-popover .popover:last-child');
}; };
var currentContext; var currentContext;
function movePopover(commentElt) { function movePopover(commentElt) {
// Move popover in the margin // Move popover in the margin
var popoverElt = currentContext.getPopoverElt(); var popoverElt = currentContext.getPopoverElt();
@ -116,6 +119,7 @@ define([
var discussionList = _.sortBy(currentFileDesc.discussionList, function(discussion) { var discussionList = _.sortBy(currentFileDesc.discussionList, function(discussion) {
return discussion.selectionEnd; return discussion.selectionEnd;
}); });
function refreshOne() { function refreshOne() {
var coordinates; var coordinates;
if(discussionList.length === 0) { if(discussionList.length === 0) {
@ -171,6 +175,7 @@ define([
} }
refreshTimeoutId = setTimeout(refreshOne, 5); refreshTimeoutId = setTimeout(refreshOne, 5);
} }
clearTimeout(refreshTimeoutId); clearTimeout(refreshTimeoutId);
refreshTimeoutId = setTimeout(refreshOne, 5); refreshTimeoutId = setTimeout(refreshOne, 5);
}, 50); }, 50);
@ -199,7 +204,8 @@ define([
cssApplier.undoToRange(context.rangyRange); cssApplier.undoToRange(context.rangyRange);
context.rangyRange.detach(); context.rangyRange.detach();
} }
catch(e) {} catch(e) {
}
var discussion = context.getDiscussion(); var discussion = context.getDiscussion();
context.selectionRange = selectionMgr.createRange(discussion.selectionStart, discussion.selectionEnd); context.selectionRange = selectionMgr.createRange(discussion.selectionStart, discussion.selectionEnd);
@ -431,8 +437,12 @@ define([
storage['author.name'] = currentContext.$authorInputElt.val(); storage['author.name'] = currentContext.$authorInputElt.val();
// Remove highlight // Remove highlight
try {
cssApplier.undoToRange(currentContext.rangyRange); cssApplier.undoToRange(currentContext.rangyRange);
currentContext.rangyRange.detach(); currentContext.rangyRange.detach();
}
catch(e) {
}
currentContext = undefined; currentContext = undefined;
delete currentFileDesc.newDiscussion; delete currentFileDesc.newDiscussion;
}); });

View File

@ -1425,6 +1425,7 @@
var disabledYShift = "-20px"; var disabledYShift = "-20px";
var highlightYShift = "-40px"; var highlightYShift = "-40px";
var image = button.getElementsByTagName("span")[0]; var image = button.getElementsByTagName("span")[0];
button.className = button.className.replace(/ disabled/g, "");
if (isEnabled) { if (isEnabled) {
image.style.backgroundPosition = button.XShift + " " + normalYShift; image.style.backgroundPosition = button.XShift + " " + normalYShift;
button.onmouseover = function () { button.onmouseover = function () {
@ -1454,7 +1455,6 @@
return false; return false;
} }
} }
button.className = button.className.replace(/ disabled/g, "");
} }
else { else {
image.style.backgroundPosition = button.XShift + " " + disabledYShift; image.style.backgroundPosition = button.XShift + " " + disabledYShift;