Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Diff: Fix does not code block for review on firefox
Browse files Browse the repository at this point in the history
- Change selection check to using range object for support firefox and other modern browser
- Change selection remove to local method
- Change reverse check logic
  • Loading branch information
insanehong committed Apr 28, 2015
1 parent 2875947 commit 8399d8a
Showing 1 changed file with 44 additions and 19 deletions.
63 changes: 44 additions & 19 deletions public/javascripts/common/yobi.CodeCommentBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ yobi.CodeCommentBlock = (function(){
function _attachEvent(){
htElement.welContainer.on("mouseup", _onMouseUpOnDiff);
htElement.welContainer.on("mousedown", "td.code pre", _onMouseDownOnDiff);
htElement.mouseEventStart = {};
}

/**
Expand All @@ -65,8 +66,11 @@ yobi.CodeCommentBlock = (function(){
return;
}

htElement.mouseEventStart = this;
_unwrapAll();
window.getSelection().removeAllRanges();

removeRanges();

}

/**
Expand Down Expand Up @@ -103,8 +107,9 @@ yobi.CodeCommentBlock = (function(){
}

// get anchor, focus row (TR) from selected text node
var welAnchor = $(oSelection.anchorNode.parentElement).closest("tr");
var welFocus = $(oSelection.focusNode.parentElement).closest("tr");
var welAnchor = $(oSelection.getRangeAt(0).startContainer).closest("tr");
var welFocus = $(oSelection.getRangeAt(oSelection.rangeCount-1).endContainer).closest("tr");


// data-line attribute is required on both of anchor and focus
if(typeof welAnchor.data("line") === "undefined" || typeof welFocus.data("line") === "undefined"){
Expand Down Expand Up @@ -146,30 +151,38 @@ yobi.CodeCommentBlock = (function(){
function _setBlockDataBySelection(){
// get anchor, focus row (TR) from selected text node
var oSelection = document.getSelection();
var welAnchor = $(oSelection.anchorNode.parentNode).closest("tr");
var welFocus = $(oSelection.focusNode.parentNode).closest("tr");

var anchor = oSelection.getRangeAt(0);
var focus = (oSelection.rangeCount ===1) ? anchor : oSelection.getRangeAt(oSelection.rangeCount-1);

var welAnchor = $(anchor.startContainer).closest("tr");
var welFocus = $(focus.endContainer).closest("tr");

var welTable = welAnchor.closest("table");

// detect whether is reversed
var nAnchorIndex = welAnchor.index();
var nFocusIndex = welFocus.index();
var nAnchorOffset = oSelection.anchorOffset;
var nFocusOffset = oSelection.focusOffset;
var bIsReversed = (nAnchorIndex > nFocusIndex) ||

var nAnchorOffset = anchor.startOffset;
var nFocusOffset = focus.endOffset;

var startIndex = $(htElement.mouseEventStart).closest("tr").index();

var bIsReversed = (nAnchorIndex < startIndex) ||
(nAnchorIndex === nFocusIndex && nAnchorOffset > nFocusOffset);
var welStartLine = bIsReversed ? welFocus : welAnchor;
var welEndLine = bIsReversed ? welAnchor : welFocus;


htBlockInfo = {
"bIsReversed" : bIsReversed,
"nStartLine" : welStartLine.data("line"),
"sStartType" : welStartLine.data("type"),
"sStartSide" : welStartLine.data("type") === 'remove' ? 'A' : 'B',
"nStartColumn": bIsReversed ? oSelection.focusOffset : oSelection.anchorOffset,
"nEndLine" : welEndLine.data("line"),
"sEndType" : welEndLine.data("type"),
"sEndSide" : welEndLine.data("type") === 'remove' ? 'A' : 'B',
"nEndColumn" : bIsReversed ? oSelection.anchorOffset : oSelection.focusOffset,
"nStartLine" : welAnchor.data("line"),
"sStartType" : welAnchor.data("type"),
"sStartSide" : welAnchor.data("type") === 'remove' ? 'A' : 'B',
"nStartColumn": nAnchorOffset,
"nEndLine" : welFocus.data("line"),
"sEndType" : welFocus.data("type"),
"sEndSide" : welFocus.data("type") === 'remove' ? 'A' : 'B',
"nEndColumn" : nFocusOffset,
"sPathA" : welTable.data("pathA"),
"sPathB" : welTable.data("pathB"),
"sPrevCommitId": welTable.data("commitA"),
Expand Down Expand Up @@ -270,13 +283,16 @@ yobi.CodeCommentBlock = (function(){
function _wrapByOffset(htOffset){
_unwrapAll();
_wrapOnDiff(htOffset);
removeRanges();
}

/**
* @param {Hash Table} htOffset
* @private
*/
function _wrapOnDiff(htOffset){
removeRanges();

var htElements = _getElementsByOffsetOptions(htOffset);
if(!htElements.elStartLine || !htElements.elEndLine){
return false;
Expand Down Expand Up @@ -310,7 +326,7 @@ yobi.CodeCommentBlock = (function(){
oRange.surroundContents(elBlock);
});

document.getSelection().removeAllRanges();

}

/**
Expand Down Expand Up @@ -388,6 +404,7 @@ yobi.CodeCommentBlock = (function(){
$('[data-toggle="comment-block"]').each(_unwrapCommentBlock);

_onUnwrapAllCodeCommentBlock();
removeRanges();
}

/**
Expand Down Expand Up @@ -420,6 +437,14 @@ yobi.CodeCommentBlock = (function(){
return htBlockInfo;
}

function removeRanges() {
if (window.getSelection) { // all browsers, except IE before version 9
window.getSelection().removeAllRanges();
} else {
document.selection.empty();
}
}

// public interface
return {
"init" : _init,
Expand Down

0 comments on commit 8399d8a

Please sign in to comment.