From 349d43bd0d1f150712c0a52f60f8061e0e4d8e3b Mon Sep 17 00:00:00 2001 From: benweet Date: Fri, 23 May 2014 19:19:07 +0100 Subject: [PATCH] Use range detach as much as possible --- public/res/editor.js | 18 ++++++++++-------- public/res/extensions/comments.js | 10 ++++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/public/res/editor.js b/public/res/editor.js index c8e4ac3c..273bff3b 100644 --- a/public/res/editor.js +++ b/public/res/editor.js @@ -170,15 +170,14 @@ define([ adjustScroll = adjustScroll || adjustScrollParam; debouncedUpdateCursorCoordinates(); }; - this.updateSelectionRange = function(range) { + this.updateSelectionRange = function() { var min = Math.min(this.selectionStart, this.selectionEnd); var max = Math.max(this.selectionStart, this.selectionEnd); - if(!range) { - range = this.createRange(min, max); - } - var selection = rangy.getSelection(); + var range = this.createRange(min, max); + var selection = document.getSelection(); selection.removeAllRanges(); - selection.addRange(range, this.selectionStart > this.selectionEnd); + selection.addRange(range); + range.detach(); }; this.setSelectionStartEnd = function(start, end) { if(start === undefined) { @@ -204,7 +203,7 @@ define([ var selectionStart = self.selectionStart; var selectionEnd = self.selectionEnd; var range; - var selection = rangy.getSelection(); + var selection = document.getSelection(); if(selection.rangeCount > 0) { var selectionRange = selection.getRangeAt(0); var element = selectionRange.startContainer; @@ -221,7 +220,7 @@ define([ element = container = container.parentNode; } while(element && element != inputElt); - if(selection.isBackwards()) { + if(false) { selectionStart = offset + (range + '').length; selectionEnd = offset; } @@ -230,6 +229,7 @@ define([ selectionEnd = offset + (range + '').length; } } + selectionRange.detach(); } self.setSelectionStartEnd(selectionStart, selectionEnd); } @@ -349,6 +349,7 @@ define([ var range = selectionMgr.createRange(startOffset, textContent.length - endOffset); range.deleteContents(); range.insertNode(document.createTextNode(replacement)); + range.detach(); } editor.setValue = setValue; @@ -364,6 +365,7 @@ define([ } range.deleteContents(); range.insertNode(document.createTextNode(replacement)); + range.detach(); offset = offset - text.length + replacement.length; selectionMgr.setSelectionStartEnd(offset, offset); selectionMgr.updateSelectionRange(); diff --git a/public/res/extensions/comments.js b/public/res/extensions/comments.js index a89d1c39..4c5c14c4 100644 --- a/public/res/extensions/comments.js +++ b/public/res/extensions/comments.js @@ -16,7 +16,7 @@ define([ '
', '
<%= author %>
', '
<%= content %>
', - '
', + '' ].join(''); var popoverTitleTmpl = [ '', @@ -24,7 +24,7 @@ define([ ' ', ' ', ' “<%- title %>”', - '', + '' ].join(''); var eventMgr; @@ -197,6 +197,7 @@ define([ } try { cssApplier.undoToRange(context.rangyRange); + context.rangyRange.detach(); } catch(e) {} var discussion = context.getDiscussion(); @@ -289,12 +290,12 @@ define([ title += '...'; } return _.template(popoverTitleTmpl, { - title: title, + title: title }); }, content: function() { var content = _.template(commentsPopoverContentHTML, { - commentList: getDiscussionComments(), + commentList: getDiscussionComments() }); return content; }, @@ -431,6 +432,7 @@ define([ // Remove highlight cssApplier.undoToRange(currentContext.rangyRange); + currentContext.rangyRange.detach(); currentContext = undefined; delete currentFileDesc.newDiscussion; });