fix moving lines with folds
This commit is contained in:
parent
bf2015bf7e
commit
43854d7a35
2 changed files with 91 additions and 76 deletions
2
build
2
build
|
|
@ -1 +1 @@
|
||||||
Subproject commit 75ba742f4f00cefadfef1ae451cfb2a4280ad367
|
Subproject commit 1e3407af692eaf661800083897c73d46d0a4bc46
|
||||||
|
|
@ -1364,6 +1364,38 @@ var EditSession = function(text, mode) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.$moveLines = function(firstRow, lastRow, dir) {
|
||||||
|
var firstRow = this.getRowFoldStart(firstRow);
|
||||||
|
var lastRow = this.getRowFoldEnd(lastRow);
|
||||||
|
if (dir < 0) {
|
||||||
|
var row = this.getRowFoldStart(firstRow + dir);
|
||||||
|
if (row < 0) return 0;
|
||||||
|
var diff = row-firstRow;
|
||||||
|
} else if (dir > 0) {
|
||||||
|
var row = this.getRowFoldEnd(lastRow + dir);
|
||||||
|
if (row > this.doc.getLength()-1) return 0;
|
||||||
|
var diff = row-lastRow;
|
||||||
|
} else {
|
||||||
|
firstRow = this.$clipRowToDocument(firstRow);
|
||||||
|
lastRow = this.$clipRowToDocument(lastRow);
|
||||||
|
var diff = lastRow - firstRow + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var range = new Range(firstRow, 0, lastRow, Number.MAX_VALUE);
|
||||||
|
var folds = this.getFoldsInRange(range).map(function(x){
|
||||||
|
x = x.clone();
|
||||||
|
x.start.row += diff;
|
||||||
|
x.end.row += diff;
|
||||||
|
return x;
|
||||||
|
});
|
||||||
|
|
||||||
|
var lines = dir == 0
|
||||||
|
? this.doc.getLines(firstRow, lastRow)
|
||||||
|
: this.doc.removeLines(firstRow, lastRow);
|
||||||
|
this.doc.insertLines(firstRow+diff, lines);
|
||||||
|
folds.length && this.addFolds(folds);
|
||||||
|
return diff;
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* Shifts all the lines in the document up one, starting from `firstRow` and ending at `lastRow`.
|
* Shifts all the lines in the document up one, starting from `firstRow` and ending at `lastRow`.
|
||||||
* @param {Number} firstRow The starting row to move up
|
* @param {Number} firstRow The starting row to move up
|
||||||
|
|
@ -1374,11 +1406,7 @@ var EditSession = function(text, mode) {
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.moveLinesUp = function(firstRow, lastRow) {
|
this.moveLinesUp = function(firstRow, lastRow) {
|
||||||
if (firstRow <= 0) return 0;
|
return this.$moveLines(firstRow, lastRow, -1);
|
||||||
|
|
||||||
var removed = this.doc.removeLines(firstRow, lastRow);
|
|
||||||
this.doc.insertLines(firstRow - 1, removed);
|
|
||||||
return -1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1387,16 +1415,10 @@ var EditSession = function(text, mode) {
|
||||||
* @param {Number} lastRow The final row to move down
|
* @param {Number} lastRow The final row to move down
|
||||||
* @returns {Number} If `firstRow` is less-than or equal to 0, this function returns 0. Otherwise, on success, it returns -1.
|
* @returns {Number} If `firstRow` is less-than or equal to 0, this function returns 0. Otherwise, on success, it returns -1.
|
||||||
*
|
*
|
||||||
*
|
|
||||||
*
|
|
||||||
* @related Document.insertLines
|
* @related Document.insertLines
|
||||||
**/
|
**/
|
||||||
this.moveLinesDown = function(firstRow, lastRow) {
|
this.moveLinesDown = function(firstRow, lastRow) {
|
||||||
if (lastRow >= this.doc.getLength()-1) return 0;
|
return this.$moveLines(firstRow, lastRow, 1);
|
||||||
|
|
||||||
var removed = this.doc.removeLines(firstRow, lastRow);
|
|
||||||
this.doc.insertLines(firstRow+1, removed);
|
|
||||||
return 1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1408,14 +1430,7 @@ var EditSession = function(text, mode) {
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.duplicateLines = function(firstRow, lastRow) {
|
this.duplicateLines = function(firstRow, lastRow) {
|
||||||
var firstRow = this.$clipRowToDocument(firstRow);
|
return this.$moveLines(firstRow, lastRow, 0);
|
||||||
var lastRow = this.$clipRowToDocument(lastRow);
|
|
||||||
|
|
||||||
var lines = this.getLines(firstRow, lastRow);
|
|
||||||
this.doc.insertLines(firstRow, lines);
|
|
||||||
|
|
||||||
var addedRows = lastRow - firstRow + 1;
|
|
||||||
return addedRows;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue