Add removing folds to undo queue.

This commit is contained in:
Julian Viereck 2011-04-28 21:01:09 +02:00
commit a385637b9e
4 changed files with 54 additions and 19 deletions

View file

@ -157,7 +157,7 @@ exports.launch = function(env) {
docs.svg.addFold("fold...", new Range(1, 0, 7, 0)); docs.svg.addFold("fold...", new Range(1, 0, 7, 0));
docs.plain.addFold("fold", new Range(0, 90, 2, 30)); docs.plain.addFold("fold", new Range(0, 90, 2, 30));
window.s = docs.plain; window.s = docs.js;
window.e = env.editor; window.e = env.editor;
setTimeout(function() { setTimeout(function() {
env.editor.selection.addEventListener("changeCursor", function() { env.editor.selection.addEventListener("changeCursor", function() {

View file

@ -14,9 +14,9 @@
<td align="right"> <td align="right">
<label for="doc">Document:</label> <label for="doc">Document:</label>
<select id="doc" size="1"> <select id="doc" size="1">
<option value="js">JavaScript Document</option>
<option value="plain">Text Document</option> <option value="plain">Text Document</option>
<option value="svg">SVG Document</option> <option value="svg">SVG Document</option>
<option value="js">JavaScript Document</option>
<option value="html">HTML Document</option> <option value="html">HTML Document</option>
<option value="css">CSS Document</option> <option value="css">CSS Document</option>
<option value="coffee">CoffeeScript Document</option> <option value="coffee">CoffeeScript Document</option>

View file

@ -66,11 +66,11 @@ var Document = function(text) {
this.remove(new Range(0, 0, len, this.getLine(len-1).length)); this.remove(new Range(0, 0, len, this.getLine(len-1).length));
this.insert({row: 0, column:0}, text); this.insert({row: 0, column:0}, text);
}; };
this.getValue = function() { this.getValue = function() {
return this.getAllLines().join(this.getNewLineCharacter()); return this.getAllLines().join(this.getNewLineCharacter());
}; };
this.createAnchor = function(row, column) { this.createAnchor = function(row, column) {
return new Anchor(this, row, column); return new Anchor(this, row, column);
}; };
@ -382,6 +382,7 @@ var Document = function(text) {
this.revertDeltas = function(deltas) { this.revertDeltas = function(deltas) {
for (var i=deltas.length-1; i>=0; i--) { for (var i=deltas.length-1; i>=0; i--) {
var delta = deltas[i]; var delta = deltas[i];
var range = Range.fromPoints(delta.range.start, delta.range.end); var range = Range.fromPoints(delta.range.start, delta.range.end);
if (delta.action == "insertLines") if (delta.action == "insertLines")

View file

@ -104,12 +104,20 @@ var EditSession = function(text, mode) {
this.onChange = function(e) { this.onChange = function(e) {
var delta = e.data; var delta = e.data;
this.$modified = true; this.$modified = true;
var removedFolds = this.$updateInternalDataOnChange(e);
if (!this.$fromUndo && this.$undoManager && !delta.ignore) { if (!this.$fromUndo && this.$undoManager && !delta.ignore) {
this.$deltas.push(delta); this.$deltas.push(delta);
if (removedFolds && removedFolds.length != 0) {
this.$deltas.push({
action: "removeFolds",
folds: removedFolds
});
}
this.$informUndoManager.schedule(); this.$informUndoManager.schedule();
} }
this.$updateInternalDataOnChange(e);
this.bgTokenizer.start(delta.range.start.row); this.bgTokenizer.start(delta.range.start.row);
this._dispatchEvent("change", e); this._dispatchEvent("change", e);
}; };
@ -620,26 +628,54 @@ var EditSession = function(text, mode) {
return this.doc.remove(range); return this.doc.remove(range);
}; };
var docActions = [
"insertLines",
"insertText",
"removeLines",
"removeText"
];
function filterDocActions(deltas) {
return deltas.filter(function(delta) {
return docActions.indexOf(delta.action) != -1;
});
}
function filterFoldDeltas(deltas) {
return deltas.filter(function(delta) {
return delta.action == "removeFolds";
});
}
this.undoChanges = function(deltas) { this.undoChanges = function(deltas) {
if (!deltas.length) if (!deltas.length)
return; return;
var docDeltas = filterDocActions(deltas);
var foldDeltas = filterFoldDeltas(deltas);
this.$fromUndo = true; this.$fromUndo = true;
this.doc.revertDeltas(deltas); this.doc.revertDeltas(docDeltas);
foldDeltas.forEach(function(foldDelta) {
foldDelta.folds.forEach(function(fold) {
this.addFold(fold);
}, this);
}, this);
this.$fromUndo = false; this.$fromUndo = false;
this.$setUndoSelection(deltas, true); this.$setUndoSelection(docDeltas, true);
}, },
this.redoChanges = function(deltas) { this.redoChanges = function(deltas) {
if (!deltas.length) if (!deltas.length)
return; return;
var docDeltas = filterDocActions(deltas);
var foldDeltas = filterFoldDeltas(deltas);
this.$fromUndo = true; this.$fromUndo = true;
this.doc.applyDeltas(deltas); this.doc.applyDeltas(docDeltas);
this.$fromUndo = false; this.$fromUndo = false;
this.$setUndoSelection(deltas, false); this.$setUndoSelection(docDeltas, false);
}, },
this.$setUndoSelection = function(deltas, isUndo) { this.$setUndoSelection = function(deltas, isUndo) {
@ -879,7 +915,6 @@ var EditSession = function(text, mode) {
}; };
}; };
// TODO: Really want to keep this name?
this.$updateInternalDataOnChange = function(e) { this.$updateInternalDataOnChange = function(e) {
var useWrapMode = this.$useWrapMode; var useWrapMode = this.$useWrapMode;
var len; var len;
@ -888,8 +923,7 @@ var EditSession = function(text, mode) {
lastRow = e.data.range.end.row, lastRow = e.data.range.end.row,
start = e.data.range.start, start = e.data.range.start,
end = e.data.range.end; end = e.data.range.end;
var removedFolds = null;
// console.log("onChange", action, e.data.range + "");
if (action.indexOf("Lines") != -1) { if (action.indexOf("Lines") != -1) {
if (action == "insertLines") { if (action == "insertLines") {
@ -907,8 +941,8 @@ var EditSession = function(text, mode) {
useWrapMode && this.$wrapData.splice(firstRow, len); useWrapMode && this.$wrapData.splice(firstRow, len);
var foldLines = this.$foldData; var foldLines = this.$foldData;
var folds = this.getFoldsInRange(e.data.range); removedFolds = this.getFoldsInRange(e.data.range);
this.removeFolds(folds); this.removeFolds(removedFolds);
var foldLine = this.getFoldLine(lastRow); var foldLine = this.getFoldLine(lastRow);
var idx = 0; var idx = 0;
@ -949,8 +983,6 @@ var EditSession = function(text, mode) {
var cmp = foldLine.range.compareInside(start.row, start.column) var cmp = foldLine.range.compareInside(start.row, start.column)
// Inside of the foldLine range. Need to split stuff up. // Inside of the foldLine range. Need to split stuff up.
if (cmp == 0) { if (cmp == 0) {
// TODO: Handle case where inseration is inside of fold!
foldLine = foldLine.split(start.row, start.column); foldLine = foldLine.split(start.row, start.column);
foldLine.shiftRow(len); foldLine.shiftRow(len);
foldLine.addRemoveChars( foldLine.addRemoveChars(
@ -979,7 +1011,9 @@ var EditSession = function(text, mode) {
len = Math.abs(e.data.range.start.column - e.data.range.end.column); len = Math.abs(e.data.range.start.column - e.data.range.end.column);
if (action.indexOf("remove") != -1) { if (action.indexOf("remove") != -1) {
// Get all the folds in the change range and remove them. // Get all the folds in the change range and remove them.
this.removeFolds(this.getFoldsInRange(e.data.range)); removedFolds = this.getFoldsInRange(e.data.range);
this.removeFolds(removedFolds);
len = -len; len = -len;
} }
var foldLine = this.getFoldLine(firstRow); var foldLine = this.getFoldLine(firstRow);
@ -992,9 +1026,9 @@ var EditSession = function(text, mode) {
console.error("The length of doc.$lines and $wrapData have to be the same!"); console.error("The length of doc.$lines and $wrapData have to be the same!");
} }
// TODO:
// this.$updateFoldData(firstRow, lastRow);
useWrapMode && this.$updateWrapData(firstRow, lastRow); useWrapMode && this.$updateWrapData(firstRow, lastRow);
return removedFolds;
}; };
this.$updateWrapData = function(firstRow, lastRow) { this.$updateWrapData = function(firstRow, lastRow) {