Skip to content

Commit

Permalink
selection API - return selection accrding to direction for both tree …
Browse files Browse the repository at this point in the history
…and text modes
  • Loading branch information
meirotstein committed Apr 17, 2018
1 parent be0e781 commit dfc03fa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/js/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ Node.onDrag = function (nodes, event) {
nodePrev = Node.getNodeFromTarget(trPrev);

var isDraggedNode = nodes.some(function (node) {
return node === nodePrev || nodePrev._isChildOf(node);
return node === nodePrev || nodePrev.isDescendantOf(node);
});

if (isDraggedNode) {
Expand Down Expand Up @@ -1851,12 +1851,12 @@ Node.onDragEnd = function (nodes, event) {
};

/**
* Test if this node is a child of an other node
* Test if this node is a sescendant of an other node
* @param {Node} node
* @return {boolean} isChild
* @return {boolean} isDescendant
* @private
*/
Node.prototype._isChildOf = function (node) {
Node.prototype.isDescendantOf = function (node) {
var n = this.parent;
while (n) {
if (n == node) {
Expand Down
102 changes: 72 additions & 30 deletions src/js/textmode.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ textmode._onChange = function () {
* @private
*/
textmode._onSelect = function () {
if(this.options.statusBar) {
this._updateCursorInfoDisplay();
}
this._updateCursorInfo();
this._emitSelectionChange();
};

Expand Down Expand Up @@ -370,7 +368,7 @@ textmode._onKeyDown = function (event) {
event.stopPropagation();
}

this._updateCursorInfoDisplay();
this._updateCursorInfo();
this._emitSelectionChange();
};

Expand All @@ -380,7 +378,7 @@ textmode._onKeyDown = function (event) {
* @private
*/
textmode._onMouseDown = function (event) {
this._updateCursorInfoDisplay();
this._updateCursorInfo();
this._emitSelectionChange();
};

Expand All @@ -390,36 +388,59 @@ textmode._onMouseDown = function (event) {
* @private
*/
textmode._onBlur = function (event) {
this._updateCursorInfoDisplay();
this._updateCursorInfo();
this._emitSelectionChange();
};

/**
* Update the status bar cursor info
* Update the cursor info and the status bar, if presented
*/
textmode._updateCursorInfoDisplay = function () {
textmode._updateCursorInfo = function () {
var me = this;
var line, col, count;

if(this.options.statusBar) {
if (this.textarea) {
setTimeout(function() { //this to verify we get the most updated textarea cursor selection
var selectionRange = util.getInputSelection(me.textarea);
if (this.textarea) {
setTimeout(function() { //this to verify we get the most updated textarea cursor selection
var selectionRange = util.getInputSelection(me.textarea);

if (selectionRange.startIndex !== selectionRange.endIndex) {
count = selectionRange.endIndex - selectionRange.startIndex;
}

if (count && me.cursorInfo && me.cursorInfo.line === selectionRange.end.row && me.cursorInfo.column === selectionRange.end.column) {
line = selectionRange.start.row;
col = selectionRange.start.column;
} else {
line = selectionRange.end.row;
col = selectionRange.end.column;
if (selectionRange.startIndex !== selectionRange.endIndex) {
count = selectionRange.endIndex - selectionRange.startIndex;
}
updateDisplay();
},0);
}

} else if (this.aceEditor && this.curserInfoElements) {
var curserPos = this.aceEditor.getCursorPosition();
var selectedText = this.aceEditor.getSelectedText();
me.cursorInfo = {
line: line,
column: col,
count: count
}

if(me.options.statusBar) {
updateDisplay();
}
},0);

} else if (this.aceEditor && this.curserInfoElements) {
var curserPos = this.aceEditor.getCursorPosition();
var selectedText = this.aceEditor.getSelectedText();

line = curserPos.row + 1;
col = curserPos.column + 1;
count = selectedText.length;

me.cursorInfo = {
line: line,
column: col,
count: count
}

line = curserPos.row + 1;
col = curserPos.column + 1;
count = selectedText.length;
if(this.options.statusBar) {
updateDisplay();
}
}
Expand Down Expand Up @@ -677,26 +698,47 @@ textmode.validate = function () {
* @returns {{start:{row:Number, column:Number},end:{row:Number, column:Number},text:String}}
*/
textmode.getTextSelection = function () {
var selection = {};
if (this.textarea) {
var selectionRange = util.getInputSelection(this.textarea);

if (this.cursorInfo && this.cursorInfo.line === selectionRange.end.row && this.cursorInfo.column === selectionRange.end.column) {
//selection direction is bottom => up
selection.start = selectionRange.end;
selection.end = selectionRange.start;
} else {
selection = selectionRange;
}

return {
start: selectionRange.start,
end: selectionRange.end,
start: selection.start,
end: selection.end,
text: this.textarea.value.substring(selectionRange.startIndex, selectionRange.endIndex)
}
}

if (this.aceEditor) {
var curserPos = this.aceEditor.getSelectionRange();
var aceSelection = this.aceEditor.getSelection();
var selectedText = this.aceEditor.getSelectedText();
var range = aceSelection.getRange();
var lead = aceSelection.getSelectionLead();

if (lead.row === range.end.row && lead.column === range.end.column) {
selection = range;
} else {
//selection direction is bottom => up
selection.start = range.end;
selection.end = range.start;
}

return {
start: {
row: curserPos.start.row + 1,
column: curserPos.start.column + 1
row: selection.start.row + 1,
column: selection.start.column + 1
},
end: {
row: curserPos.end.row + 1,
column: curserPos.end.column + 1
row: selection.end.row + 1,
column: selection.end.column + 1
},
text: selectedText
};
Expand Down
16 changes: 12 additions & 4 deletions src/js/treemode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,10 +1364,18 @@ treemode.showContextMenu = function (anchor, onClose) {
treemode.getSelection = function () {
var selection = {};
if (this.multiselection.nodes && this.multiselection.nodes.length) {
selection.start = this.multiselection.nodes[0].serialize();

if (this.multiselection.nodes.length > 1) {
selection.end = this.multiselection.nodes[this.multiselection.nodes.length - 1].serialize();
if (this.multiselection.nodes.length === 1) {
selection.start = selection.end = this.multiselection.nodes[0].serialize();
} else {
var selection1 = this.multiselection.nodes[0];
var selection2 = this.multiselection.nodes[this.multiselection.nodes.length - 1];
if (this.multiselection.start === selection1 || this.multiselection.start.isDescendantOf(selection1)) {
selection.start = selection1.serialize();
selection.end = selection2.serialize();
} else {
selection.start = selection2.serialize();
selection.end = selection1.serialize();
}
}
}
return selection;
Expand Down

0 comments on commit dfc03fa

Please sign in to comment.