-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommond_2.js
More file actions
63 lines (63 loc) · 1.95 KB
/
Commond_2.js
File metadata and controls
63 lines (63 loc) · 1.95 KB
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
61
62
63
/**
* Commond Pattern:invoker通过将行为委托给Commond,commond再委托给具体实现类。将Commond抽离出来方便历史管理,以及回退操作;
* 与Memonto相比,记录的是命令而不是snapshot,更加节省内存。
*/
var MakeBoldCommond = /** @class */ (function () {
function MakeBoldCommond(htmlEditor, history) {
this.htmlEditor = htmlEditor;
this.history = history;
}
MakeBoldCommond.prototype.excute = function () {
this.prevContent = this.htmlEditor.getContent();
this.htmlEditor.makeBold();
this.history.push(this);
};
MakeBoldCommond.prototype.unexcute = function () {
this.htmlEditor.setContent(this.prevContent);
};
return MakeBoldCommond;
}());
var UndoCommond = /** @class */ (function () {
function UndoCommond(history) {
this.history = history;
}
UndoCommond.prototype.excute = function () {
var last = this.history.pop();
last.unexcute();
};
return UndoCommond;
}());
var EditHistory = /** @class */ (function () {
function EditHistory() {
this.history = [];
}
EditHistory.prototype.push = function (commond) {
this.history.push(commond);
};
EditHistory.prototype.pop = function () {
return this.history.pop();
};
return EditHistory;
}());
var HtmlEditor = /** @class */ (function () {
function HtmlEditor() {
}
HtmlEditor.prototype.setContent = function (content) {
this.content = content;
};
HtmlEditor.prototype.getContent = function () {
return this.content;
};
HtmlEditor.prototype.makeBold = function () {
this.content = "<b>".concat(this.content, "<b/>");
};
return HtmlEditor;
}());
var editor = new HtmlEditor();
editor.setContent("hello world");
var h = new EditHistory();
var c = new MakeBoldCommond(editor, h);
c.excute();
var undo = new UndoCommond(h);
undo.excute();
console.log(editor.getContent());