-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-paste.js
229 lines (169 loc) · 4.82 KB
/
copy-paste.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import BpmnModeler from 'bpmn-js/lib/Modeler';
import sampleDiagram from './sample.bpmn';
import {
insertCSS
} from './helper';
import fileDrop from 'file-drops';
import fileOpen from 'file-open';
import download from 'downloadjs';
import fileDropCSS from './file-drops.css';
import diagramCSS from 'bpmn-js/dist/assets/diagram-js.css';
import bpmnCSS from 'bpmn-js/dist/assets/bpmn-js.css';
import bpmnFontCSS from 'bpmn-js/dist/assets/bpmn-font/css/bpmn-embedded.css';
import {
isPaste
} from 'diagram-js/lib/features/keyboard/KeyboardUtil';
insertCSS('file-drops.css', fileDropCSS);
insertCSS('diagram-js.css', diagramCSS);
insertCSS('bpmn-font.css', bpmnFontCSS);
insertCSS('bpmn-js.css', bpmnCSS);
const nativeCopyModule = {
__init__: [ 'nativeCopyPaste' ],
nativeCopyPaste: [ 'type', function(
keyboard, eventBus,
moddle, clipboard
) {
// persist into local storage whenever
// copy took place
eventBus.on('copyPaste.elementsCopied', event => {
const { tree } = event;
console.log('PUT localStorage', tree);
// persist in local storage, encoded as json
localStorage.setItem('bpmnClipboard', JSON.stringify(tree));
});
// intercept global paste keybindings and
// inject reified pasted stack
keyboard.addListener(2000, event => {
const { keyEvent } = event;
if (!isPaste(keyEvent)) {
return;
}
// retrieve from local storage
const serializedCopy = localStorage.getItem('bpmnClipboard');
if (!serializedCopy) {
return;
}
// parse tree, reinstantiating contained objects
const parsedCopy = JSON.parse(serializedCopy, createReviver(moddle));
console.log('GET localStorage', parsedCopy);
// put into clipboard
clipboard.set(parsedCopy);
});
} ]
}
describe('copy-paste', function() {
it('should copy/paste', async function() {
const modeler = new BpmnModeler({
container: testContainer(),
additionalModules: [
nativeCopyModule
],
keyboard: {
bindTo: document.body
}
});
await modeler.importXML(sampleDiagram);
// with app like behavior
setupApp(modeler, 'sample.bpmn');
});
});
/////////////// helpers //////////////////////////
function setupApp(modeler, fileName) {
function openDiagram(diagram) {
return modeler.importXML(diagram)
.then(({ warnings }) => {
if (warnings.length) {
console.warn(warnings);
}
})
.catch(err => {
console.error(err);
});
}
function openFile(files) {
// files = [ { name, contents }, ... ]
if (!files.length) {
return;
}
fileName = files[0].name;
openDiagram(files[0].contents);
}
function downloadDiagram() {
modeler.saveXML({ format: true }, function(err, xml) {
if (!err) {
download(xml, fileName, 'application/xml');
}
});
}
const handleDragOver = fileDrop('Open BPMN diagram', openFile);
const handleKeys = (event) => {
if (event.code === 'KeyS' && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
downloadDiagram();
}
if (event.code === 'KeyO' && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
fileOpen().then(openFile);
}
};
document.body.addEventListener('keydown', handleKeys);
document.body.addEventListener('dragover', handleDragOver);
return () => {
document.body.removeEventListener('keydown', handleKeys);
document.body.removeEventListener('dragover', handleDragOver);
};
}
/**
* A factory function that returns a reviver to be
* used with JSON#parse to reinstantiate moddle instances.
*
* @param {Moddle} moddle
*
* @return {Function}
*/
function createReviver(moddle) {
var elCache = {};
/**
* The actual reviewer that creates model instances
* for elements with a $type attribute.
*
* Elements with ids will be re-used, if already
* created.
*
* @param {String} key
* @param {Object} object
*
* @return {Object} actual element
*/
return function(key, object) {
if (typeof object === 'object' && typeof object.$type === 'string') {
var objectId = object.id;
if (objectId && elCache[objectId]) {
return elCache[objectId];
}
var type = object.$type;
var attrs = Object.assign({}, object);
delete attrs.$type;
var newEl = moddle.create(type, attrs);
if (objectId) {
elCache[objectId] = newEl;
}
return newEl;
}
return object;
};
}
/**
* Create a full-screen test container.
*
* @return {Element}
*/
function testContainer() {
var el = document.createElement('div');
el.style.width = '100%';
el.style.height = '100vh';
el.style.margin = '-10px';
el.style.position = 'absolute';
document.body.appendChild(el);
return el;
}