take into account that undos can have inserts and removes mixed
This commit is contained in:
parent
8f005b9ed5
commit
bf88abd8d9
1 changed files with 49 additions and 19 deletions
|
|
@ -536,15 +536,7 @@ var EditSession = function(text, mode) {
|
|||
this.doc.revertDeltas(deltas);
|
||||
this.$fromUndo = false;
|
||||
|
||||
// update the selection
|
||||
var firstDelta = deltas[0];
|
||||
var lastDelta = deltas[deltas.length-1];
|
||||
|
||||
this.selection.clearSelection();
|
||||
if (firstDelta.action == "insertText" || firstDelta.action == "insertLines")
|
||||
this.selection.moveCursorToPosition(firstDelta.range.start);
|
||||
if (firstDelta.action == "removeText" || firstDelta.action == "removeLines")
|
||||
this.selection.setSelectionRange(Range.fromPoints(lastDelta.range.start, firstDelta.range.end));
|
||||
this.$setUndoSelection(deltas, true);
|
||||
},
|
||||
|
||||
this.redoChanges = function(deltas) {
|
||||
|
|
@ -555,17 +547,55 @@ var EditSession = function(text, mode) {
|
|||
this.doc.applyDeltas(deltas);
|
||||
this.$fromUndo = false;
|
||||
|
||||
// update the selection
|
||||
var firstDelta = deltas[0];
|
||||
var lastDelta = deltas[deltas.length-1];
|
||||
|
||||
this.selection.clearSelection();
|
||||
if (firstDelta.action == "insertText" || firstDelta.action == "insertLines")
|
||||
this.selection.setSelectionRange(Range.fromPoints(firstDelta.range.start, lastDelta.range.end));
|
||||
if (firstDelta.action == "removeText" || firstDelta.action == "removeLines")
|
||||
this.selection.moveCursorToPosition(lastDelta.range.start);
|
||||
this.$setUndoSelection(deltas, false);
|
||||
},
|
||||
|
||||
this.$setUndoSelection = function(deltas, isUndo) {
|
||||
// invert deltas is they are an undo
|
||||
if (isUndo)
|
||||
deltas = deltas.map(function(delta) {
|
||||
var d = {
|
||||
range: delta.range
|
||||
}
|
||||
if (delta.action == "insertText" || delta.action == "insertLines")
|
||||
d.action = "removeText"
|
||||
else
|
||||
d.action = "insertText"
|
||||
return d;
|
||||
}).reverse();
|
||||
|
||||
|
||||
var actions = [{}];
|
||||
|
||||
// collapse insert and remove operations
|
||||
for (var i=0; i<deltas.length; i++) {
|
||||
var delta = deltas[i];
|
||||
var isInsert = delta.action == "insertText" || delta.action == "insertLines";
|
||||
var action = actions[actions.length-1];
|
||||
if (action.isInsert !== isInsert) {
|
||||
actions.push({
|
||||
isInsert: isInsert,
|
||||
start: isInsert ? delta.range.start : delta.range.end,
|
||||
end: isInsert ? delta.range.end : delta.range.start
|
||||
})
|
||||
}
|
||||
else {
|
||||
if (isInsert)
|
||||
action.end = delta.range.end;
|
||||
else
|
||||
action.start = delta.range.start;
|
||||
}
|
||||
}
|
||||
|
||||
// update selection based on last operation
|
||||
this.selection.clearSelection();
|
||||
var action = actions[actions.length-1];
|
||||
if (action.isInsert)
|
||||
this.selection.setSelectionRange(Range.fromPoints(action.start, action.end));
|
||||
else
|
||||
this.selection.moveCursorToPosition(action.start);
|
||||
},
|
||||
|
||||
this.replace = function(range, text) {
|
||||
return this.doc.replace(range, text);
|
||||
};
|
||||
|
|
@ -1192,4 +1222,4 @@ var EditSession = function(text, mode) {
|
|||
}).call(EditSession.prototype);
|
||||
|
||||
exports.EditSession = EditSession;
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue