-
Notifications
You must be signed in to change notification settings - Fork 2
/
history.js
60 lines (54 loc) · 2.25 KB
/
history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/** History for undo/redo **/
Textile.History = Textile.Utils.makeClass({
commands: null,
date: 0,
constructor: function(editor) {
this.editor = editor;
this.commands = [];
},
add: function(command) {
this.commands = this.commands.slice(0, this.date+1);
this.commands.push(command);
this.date = this.commands.length - 1;
},
undo: function() {
var lastCommand = this.commands[this.date--];
if(this.date < -1) this.date = -1;
if(!lastCommand) return; // no more history
if(lastCommand.type == 'i') {
this.editor.model.deleteRight(lastCommand.at, true, lastCommand.txt.length);
this.editor.cursor.toPosition(lastCommand.cursor);
}
if(lastCommand.type == 'd') {
this.editor.model.insert(lastCommand.at, lastCommand.txt, true);
this.editor.cursor.toPosition(lastCommand.cursor);
}
if(lastCommand.type == 'r') {
this.editor.model.replace(lastCommand.from, lastCommand.from + lastCommand.newTxt.length, lastCommand.oldTxt, true);
this.editor.cursor.toPosition(lastCommand.from);
this.editor.selection = lastCommand.selection;
}
this.editor.cursor.focus();
this.editor.paint();
},
redo: function() {
var lastCommand = this.commands[++this.date];
if(this.date > this.commands.length - 1) this.date = this.commands.length - 1;
if(!lastCommand) return; // no more history
if(lastCommand.type == 'i') {
this.editor.model.insert(lastCommand.at, lastCommand.txt, true);
this.editor.cursor.toPosition(lastCommand.cursor + lastCommand.txt.length);
}
if(lastCommand.type == 'd') {
this.editor.model.deleteRight(lastCommand.at, true);
this.editor.cursor.toPosition(lastCommand.cursor - 1);
}
if(lastCommand.type == 'r') {
this.editor.model.replace(lastCommand.from, lastCommand.from + lastCommand.oldTxt.length, lastCommand.newTxt, true);
this.editor.cursor.toPosition(lastCommand.from);
this.editor.selection = null;
}
this.editor.cursor.focus();
this.editor.paint();
}
});