-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandPattern.js
More file actions
134 lines (124 loc) · 3.97 KB
/
Copy pathCommandPattern.js
File metadata and controls
134 lines (124 loc) · 3.97 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"use strict";
/**
* Command Pattern - encapsulates a request as an object, which provides the
* client the benefit of having multiple different requests. The request will
* be executed by another entity that knows how to interact with it.
*
* Example - The text editor has a set of commands we sometimes use, the cut,
* copy, paste and undo. The user does not know how these commands are executed
* or the algorithims inside it. The user just need to send the command to the
* text editor and the text editor will handle it for him/her.
*
*/
function forceRequiredMethods(_class, methods) {
for (let eachMethod of methods) {
if(_class[eachMethod] === undefined || typeof _class[eachMethod] !== "function") {
throw new TypeError(eachMethod + " is required and has to be overriden.");
}
}
}
class TextEditor {
constructor(value) {
this.value = value;
this.selection = [0, 0];
this.clipboard = "";
}
copy() {
this.clipboard = this.value.substring(this.selection[0], this.selection[1]);
this.selection = [0, 0];
}
cut() {
let valueOutsideSelection = this.valueOutsideSelection();
this.copy();
this.value = valueOutsideSelection[0] + valueOutsideSelection[1];
this.selection = [0, 0];
}
paste() {
let valueOutsideSelection = this.valueOutsideSelection();
this.value = valueOutsideSelection[0] + this.clipboard + valueOutsideSelection[1];
this.clipboard = "";
this.selection = [0, 0];
}
valueOutsideSelection() {
let valueAfterSelection = this.value.substr(this.selection[1]);
let valueBeforeSelection = this.value.substr(0, this.selection[0]);
return [valueBeforeSelection, valueAfterSelection];
}
currentSettings() {
return {
value: this.value,
clipboard: this.clipboard,
selection: this.selection
}
}
loadSettings(settings) {
this.value = settings["value"];
this.clipboard = settings["clipboard"];
this.selection = settings["selection"];
}
}
class Command {
constructor() {
if(new.target === Command) {
throw new TypeError("This is an abstract class");
}
let _class = this;
forceRequiredMethods(_class, ["execute", "undo"]);
}
}
class CutCommand extends Command {
constructor(textEditor, selection) {
super();
this.textEditor = textEditor;
this.textEditor.selection = selection;
this.previousSettings = this.textEditor.currentSettings();
}
execute() {
this.textEditor.cut();
}
undo() {
this.textEditor.loadSettings(this.previousSettings);
}
}
class CopyCommand extends Command {
constructor(textEditor, selection) {
super();
this.textEditor = textEditor;
this.textEditor.selection = selection;
this.previousSettings = this.textEditor.currentSettings();
}
execute() {
this.textEditor.copy();
}
undo() {
this.textEditor.loadSettings(this.previousSettings);
}
}
class PasteCommand extends Command {
constructor(textEditor, selection) {
super();
this.textEditor = textEditor;
this.textEditor.selection = selection;
this.previousSettings = this.textEditor.currentSettings();
}
execute() {
this.textEditor.paste();
}
undo() {
this.textEditor.loadSettings(this.previousSettings);
}
}
let textFile = new TextEditor("Let's see how this string of words can go with all the commands");
let cutSomeWords = new CutCommand(textFile, [5, 10]);
cutSomeWords.execute();
console.log(textFile);
cutSomeWords.undo();
console.log(textFile);
let copySomeWords = new CopyCommand(textFile, [5, 10]);
console.log(textFile);
copySomeWords.execute();
console.log(textFile);
let pasteSomeWords = new PasteCommand(textFile, [10, 10]);
console.log(textFile);
pasteSomeWords.execute();
console.log(textFile);