Use range detach as much as possible

This commit is contained in:
benweet 2014-05-23 19:19:07 +01:00
parent c0b1c9107d
commit 349d43bd0d
2 changed files with 16 additions and 12 deletions

View File

@ -170,15 +170,14 @@ define([
adjustScroll = adjustScroll || adjustScrollParam; adjustScroll = adjustScroll || adjustScrollParam;
debouncedUpdateCursorCoordinates(); debouncedUpdateCursorCoordinates();
}; };
this.updateSelectionRange = function(range) { this.updateSelectionRange = function() {
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);
if(!range) { var range = this.createRange(min, max);
range = this.createRange(min, max); var selection = document.getSelection();
}
var selection = rangy.getSelection();
selection.removeAllRanges(); selection.removeAllRanges();
selection.addRange(range, this.selectionStart > this.selectionEnd); selection.addRange(range);
range.detach();
}; };
this.setSelectionStartEnd = function(start, end) { this.setSelectionStartEnd = function(start, end) {
if(start === undefined) { if(start === undefined) {
@ -204,7 +203,7 @@ define([
var selectionStart = self.selectionStart; var selectionStart = self.selectionStart;
var selectionEnd = self.selectionEnd; var selectionEnd = self.selectionEnd;
var range; var range;
var selection = rangy.getSelection(); var selection = document.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;
@ -221,7 +220,7 @@ define([
element = container = container.parentNode; element = container = container.parentNode;
} while(element && element != inputElt); } while(element && element != inputElt);
if(selection.isBackwards()) { if(false) {
selectionStart = offset + (range + '').length; selectionStart = offset + (range + '').length;
selectionEnd = offset; selectionEnd = offset;
} }
@ -230,6 +229,7 @@ define([
selectionEnd = offset + (range + '').length; selectionEnd = offset + (range + '').length;
} }
} }
selectionRange.detach();
} }
self.setSelectionStartEnd(selectionStart, selectionEnd); self.setSelectionStartEnd(selectionStart, selectionEnd);
} }
@ -349,6 +349,7 @@ define([
var range = selectionMgr.createRange(startOffset, textContent.length - endOffset); var range = selectionMgr.createRange(startOffset, textContent.length - endOffset);
range.deleteContents(); range.deleteContents();
range.insertNode(document.createTextNode(replacement)); range.insertNode(document.createTextNode(replacement));
range.detach();
} }
editor.setValue = setValue; editor.setValue = setValue;
@ -364,6 +365,7 @@ define([
} }
range.deleteContents(); range.deleteContents();
range.insertNode(document.createTextNode(replacement)); range.insertNode(document.createTextNode(replacement));
range.detach();
offset = offset - text.length + replacement.length; offset = offset - text.length + replacement.length;
selectionMgr.setSelectionStartEnd(offset, offset); selectionMgr.setSelectionStartEnd(offset, offset);
selectionMgr.updateSelectionRange(); selectionMgr.updateSelectionRange();

View File

@ -16,7 +16,7 @@ define([
'<div class="comment-block<%= reply ? \' reply\' : \'\' %>">', '<div class="comment-block<%= reply ? \' reply\' : \'\' %>">',
' <div class="comment-author"><i class="icon-comment"></i> <%= author %></div>', ' <div class="comment-author"><i class="icon-comment"></i> <%= author %></div>',
' <div class="comment-content"><%= content %></div>', ' <div class="comment-content"><%= content %></div>',
'</div>', '</div>'
].join(''); ].join('');
var popoverTitleTmpl = [ var popoverTitleTmpl = [
'<span class="clearfix">', '<span class="clearfix">',
@ -24,7 +24,7 @@ define([
' <i class="icon-trash"></i>', ' <i class="icon-trash"></i>',
' </a>', ' </a>',
' “<%- title %>”', ' “<%- title %>”',
'</span>', '</span>'
].join(''); ].join('');
var eventMgr; var eventMgr;
@ -197,6 +197,7 @@ define([
} }
try { try {
cssApplier.undoToRange(context.rangyRange); cssApplier.undoToRange(context.rangyRange);
context.rangyRange.detach();
} }
catch(e) {} catch(e) {}
var discussion = context.getDiscussion(); var discussion = context.getDiscussion();
@ -289,12 +290,12 @@ define([
title += '...'; title += '...';
} }
return _.template(popoverTitleTmpl, { return _.template(popoverTitleTmpl, {
title: title, title: title
}); });
}, },
content: function() { content: function() {
var content = _.template(commentsPopoverContentHTML, { var content = _.template(commentsPopoverContentHTML, {
commentList: getDiscussionComments(), commentList: getDiscussionComments()
}); });
return content; return content;
}, },
@ -431,6 +432,7 @@ define([
// Remove highlight // Remove highlight
cssApplier.undoToRange(currentContext.rangyRange); cssApplier.undoToRange(currentContext.rangyRange);
currentContext.rangyRange.detach();
currentContext = undefined; currentContext = undefined;
delete currentFileDesc.newDiscussion; delete currentFileDesc.newDiscussion;
}); });