command.js
2.87 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
135
/**
* @author NHN Ent. FE Development Team <dl_javascript@nhn.com>
* @fileoverview Command interface
*/
import snippet from 'tui-code-snippet';
import errorMessage from '../factory/errorMessage';
const createMessage = errorMessage.create;
const errorTypes = errorMessage.types;
/**
* Command class
* @class
* @param {{name:function, execute: function, undo: function,
* executeCallback: function, undoCallback: function}} actions - Command actions
* @param {Array} args - passing arguments on execute, undo
* @ignore
*/
class Command {
constructor(actions, args) {
/**
* command name
* @type {string}
*/
this.name = actions.name;
/**
* arguments
* @type {Array}
*/
this.args = args;
/**
* Execute function
* @type {function}
*/
this.execute = actions.execute;
/**
* Undo function
* @type {function}
*/
this.undo = actions.undo;
/**
* executeCallback
* @type {function}
*/
this.executeCallback = actions.executeCallback || null;
/**
* undoCallback
* @type {function}
*/
this.undoCallback = actions.undoCallback || null;
/**
* data for undo
* @type {Object}
*/
this.undoData = {};
}
/**
* Execute action
* @param {Object.<string, Component>} compMap - Components injection
* @abstract
*/
execute() {
throw new Error(createMessage(errorTypes.UN_IMPLEMENTATION, 'execute'));
}
/**
* Undo action
* @param {Object.<string, Component>} compMap - Components injection
* @abstract
*/
undo() {
throw new Error(createMessage(errorTypes.UN_IMPLEMENTATION, 'undo'));
}
/**
* command for redo if undoData exists
* @returns {boolean} isRedo
*/
get isRedo() {
return Object.keys(this.undoData).length;
}
/**
* Set undoData action
* @param {Object} undoData - maked undo data
* @param {Object} cachedUndoDataForSilent - cached undo data
* @param {boolean} isSilent - is silent execution or not
* @returns {Object} cachedUndoDataForSilent
*/
setUndoData(undoData, cachedUndoDataForSilent, isSilent) {
if (cachedUndoDataForSilent) {
undoData = cachedUndoDataForSilent;
}
if (!isSilent) {
snippet.extend(this.undoData, undoData);
cachedUndoDataForSilent = null;
} else if (!cachedUndoDataForSilent) {
cachedUndoDataForSilent = undoData;
}
return cachedUndoDataForSilent;
}
/**
* Attach execute callabck
* @param {function} callback - Callback after execution
* @returns {Command} this
*/
setExecuteCallback(callback) {
this.executeCallback = callback;
return this;
}
/**
* Attach undo callback
* @param {function} callback - Callback after undo
* @returns {Command} this
*/
setUndoCallback(callback) {
this.undoCallback = callback;
return this;
}
}
export default Command;