Can now move multiple lines up/down

Also added comments to help with understanding
This commit is contained in:
Matt Pass
2013-08-09 09:06:31 +01:00
parent e5fac3efa3
commit e715d02ea1
3 changed files with 94 additions and 72 deletions

View File

@@ -191,20 +191,41 @@ var ICEcoder = {
},
// Move current line up/down
moveLine: function(dir) {
var cM, line, swapLineNo, swapLine;
moveLines: function(dir) {
var cM, lineStart, lineEnd, swapLineNo, swapLine;
cM = top.ICEcoder.getcMInstance();
line = cM.getCursor().line;
if (dir=="up" && line>0) {swapLineNo = line-1}
if (dir=="down" && line<cM.lineCount()-1) {swapLineNo = line+1}
// Get start & end lines plus the line we'll swap with
lineStart = cM.getCursor('start');
lineEnd = cM.getCursor('end');
if (dir=="up" && lineStart.line>0) {swapLineNo = lineStart.line-1}
if (dir=="down" && lineEnd.line<cM.lineCount()-1) {swapLineNo = lineEnd.line+1}
// If we have a line to swap with
if (!isNaN(swapLineNo)) {
// Get the content of the swap line and carry out the swap in a single operation
swapLine = cM.getLine(swapLineNo);
cM.operation(function() {
cM.setLine(swapLineNo,cM.getLine(line));
cM.setLine(line,swapLine);
// Move lines in turn up
if (dir=="up") {
for (var i=lineStart.line; i<=lineEnd.line; i++) {
cM.setLine(i-1,cM.getLine(i));
}
// ...or down
} else {
for (var i=lineEnd.line; i>=lineStart.line; i--) {
cM.setLine(i+1,cM.getLine(i));
}
}
// Now swap our final line with the swap line to complete the move
cM.setLine(dir=="up" ? lineEnd.line : lineStart.line,swapLine);
// Finally set the moved selection
cM.setSelection(
{line: lineStart.line+(dir=="up" ? -1 : 1), ch: lineStart.ch},
{line: lineEnd.line+(dir=="up" ? -1 : 1), ch: lineEnd.ch}
);
});
ICEcoder.highlightLine(swapLineNo);
}
},
@@ -2296,12 +2317,12 @@ var ICEcoder = {
// CTRL/Cmd+up arrow (Move line up)
} else if(key==38 && (evt.ctrlKey||top.ICEcoder.cmdKey) && area=="content") {
top.ICEcoder.moveLine('up');
top.ICEcoder.moveLines('up');
return false;
// CTRL/Cmd+down arrow (Move line down)
} else if(key==40 && (evt.ctrlKey||top.ICEcoder.cmdKey) && area=="content") {
top.ICEcoder.moveLine('down');
top.ICEcoder.moveLines('down');
return false;
// CTRL/Cmd+numeric plus (New tab)