/
CopyCutPaste.js
132 lines (104 loc) · 2.57 KB
/
CopyCutPaste.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
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
import { isArray } from 'min-dash';
import CutHandler from './cmd/CutHandler';
import PasteHandler from './cmd/PasteHandler';
import { createDescriptor } from './DescriptorUtil';
export default class CutPaste {
constructor(
clipboard, commandStack, eventBus,
modeling, sheet, rules) {
this._clipboard = clipboard;
this._commandStack = commandStack;
this._eventBus = eventBus;
this._modeling = modeling;
this._sheet = sheet;
this._rules = rules;
commandStack.registerHandler('cut', CutHandler);
commandStack.registerHandler('paste', PasteHandler);
}
/**
* Copy elements.
*
* @param {Array} elements - Elements to be copied.
*/
copy(elements) {
if (!isArray(elements)) {
elements = [ elements ];
}
const data = {
elements: createDescriptor(elements)
};
this._eventBus.fire('copyCutPaste.copy', { data });
this._clipboard.set(data);
}
/**
* Cut elements thereby removing them temporarily.
*
* @param {Array} elements - Elements to be cut.
*/
cut(elements) {
if (!isArray(elements)) {
elements = [ elements ];
}
const data = {
elements: createDescriptor(elements),
keepIds: true
};
const context = {
elements,
data
};
this._eventBus.fire('copyCutPaste.cut', { data });
this._commandStack.execute('cut', context);
}
/**
* Paste rows or cols before row or col.
*
* @param {Row|Col} element - Row or col to paste elements before.
*/
pasteBefore(element) {
return this._paste(element, { before: true });
}
/**
* Paste rows or cols after row or col.
*
* @param {Row|Col} element - Row or col to paste elements after.
*/
pasteAfter(element) {
return this._paste(element, { after: true });
}
/**
* Basic paste onto given target element.
*/
_paste(target, position) {
const clipboardData = this._clipboard.get();
if (!clipboardData) {
return undefined;
}
const allowed = this._rules.allowed('paste', {
data: clipboardData.elements,
target
});
if (!allowed) {
return false;
}
this._commandStack.execute('paste', {
element: target,
...position
});
return true;
}
/**
* Paste elements at.
*/
pasteAt(element) {
// TODO: implement for pasting cells
}
}
CutPaste.$inject = [
'clipboard',
'commandStack',
'eventBus',
'modeling',
'sheet',
'rules'
];