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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
////////////////////////////////////////////////////////////////////////
//
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component,
NJUtils = require("js/lib/NJUtils").NJUtils,
World = require("js/lib/drawing/world").World;
var ClipboardController = exports.ClipboardController = Montage.create(Component, {
hasTemplate: {
value: false
},
deserializedFromTemplate: {
value: function() {
document.body.addEventListener("copy", this, false);
document.body.addEventListener("cut", this, false);
document.body.addEventListener("paste", this, false);
//ninja menu events
this.eventManager.addEventListener("executeCut", this, false);
this.eventManager.addEventListener("executeCopy", this, false);
this.eventManager.addEventListener("executePaste", this, false);
}
},
copiedObjects:{
value: []
},
_copyFlag:{
value:false
},
copyFlag:{
get:function(){return this._copyFlag;},
set:function(value){this._copyFlag = value;}
},
_newCopyFlag:{
value:true
},
newCopyFlag:{
get:function(){return this._newCopyFlag;},
set:function(value){this._newCopyFlag = value;}
},
handleExecuteCopy:{
value: function(){document.execCommand('copy',false,null);}
},
handleExecuteCut:{
value: function(){document.execCommand('cut',false,null);}
},
handleExecutePaste:{
value: function(){document.execCommand('paste',false,null);}
},
handleCopy:{
value:function(clipboardEvent, test){
if(this.application.ninja.documentController.activeDocument.currentView === "code") return;
this.copy(clipboardEvent);
clipboardEvent.preventDefault();
}
},
handlePaste:{
value:function(clipboardEvent){
if(this.application.ninja.documentController.activeDocument.currentView === "code") return;
//TODO: return if stage is not focussed
var clipboardData = clipboardEvent.clipboardData,
ninjaData = clipboardData.getData("ninja"),
htmlData = clipboardData.getData("text/html"),
textData = clipboardData.getData("text/plain");
this.pasteItems(ninjaData, htmlData, textData);
clipboardEvent.preventDefault();
}
},
pasteItems:{//todo: change to pasteNinja, pasteHTML, etc
value: function(ninjaData, htmlData, textData){
var i=0,
pasteDataObject=null,
clipboardHelper=this.createClipboardHelper(),
pastedElements = null,
node = null,
styles = null;
if(ninjaData){//=> copy from ninja
//paste using in-memory clipboard helper
//TODO: cleanse HTML
this.application.ninja.selectedElements.length = 0;
NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} );
clipboardHelper.innerHTML = htmlData;//add the copied html to generate the nodes
while(clipboardHelper.hasChildNodes()){
if(clipboardHelper.lastChild.tagName === "META") {
clipboardHelper.removeChild(clipboardHelper.lastChild);//remove unnecesary meta tag
}
else if (clipboardHelper.lastChild.tagName === "CANVAS"){//temporary - we probably won't need to serialize this to the system clipboard
//only handling 1 canvas for POC
//clone copied canvas
var canvas = document.application.njUtils.make("canvas", this.copiedObjects.className, this.application.ninja.currentDocument);
canvas.width = this.copiedObjects.width;
canvas.height = this.copiedObjects.height;
//end - clone copied canvas
if (!canvas.getAttribute( "data-RDGE-id" )) canvas.setAttribute( "data-RDGE-id", NJUtils.generateRandom() );
document.application.njUtils.createModelWithShape(canvas);
styles = canvas.elementModel.data || {};
styles.top = "" + (this.application.ninja.elementMediator.getProperty(this.copiedObjects, "top", parseInt) - 50) + "px";
styles.left = "" + (this.application.ninja.elementMediator.getProperty(this.copiedObjects, "left", parseInt) - 50) + "px";
this.application.ninja.elementMediator.addElements(canvas, styles, false);
var world, worldData = this.copiedObjects.elementModel.shapeModel.GLWorld.exportJSON();
if(worldData)
{
var jObj;
var index = worldData.indexOf( ';' );
if ((worldData[0] === 'v') && (index < 24))
{
// JSON format. separate the version info from the JSON info
var jStr = worldData.substr( index+1 );
jObj = JSON.parse( jStr );
world = new World(canvas, jObj.webGL);
canvas.elementModel.shapeModel.GLWorld = world;
canvas.elementModel.shapeModel.useWebGl = jObj.webGL;
world.importJSON(jObj);
this.application.ninja.currentDocument.buildShapeModel( canvas.elementModel, world );
}
}
NJevent("elementAdded", canvas);
clipboardHelper.removeChild(clipboardHelper.lastChild);
}
else if(clipboardHelper.lastChild.nodeType === 3){//TextNode
node = clipboardHelper.removeChild(clipboardHelper.lastChild);
//USE styles controller to create the styles of the div and span
var doc = this.application.ninja.currentDocument ? this.application.ninja.currentDocument._document : document;
var aspan = doc.createElement("span");
aspan.appendChild(node);
var adiv = doc.createElement("div");
adiv.appendChild(aspan);
styles = {"top":"100px", "left":"100px"};
this.pastePositioned(node, styles);
}
else {
node = clipboardHelper.removeChild(clipboardHelper.lastChild);
if(node.removeAttribute) {node.removeAttribute("style");}//remove the computed styles attribute which is placed only for pasting to external applications
//get class string while copying .... generate styles from class
//styles = {"top":"100px", "left":"100px"};
this.pastePositioned(node, styles);
}
}
this.application.ninja.documentController.activeDocument.needsSave = true;
return;
}
else if(htmlData){
//TODO: cleanse HTML
this.application.ninja.selectedElements.length = 0;
NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} );
clipboardHelper.innerHTML = htmlData;//add the copied html to generate the nodes
while(clipboardHelper.hasChildNodes()){
if(clipboardHelper.lastChild.tagName === "META") {
clipboardHelper.removeChild(clipboardHelper.lastChild);//remove unnecesary meta tag
}
else if (clipboardHelper.lastChild.tagName === "CANVAS"){//temporary - we probably won't need to serialize this to the system clipboard
//only handling 1 canvas for POC
//clone copied canvas
var canvas = document.application.njUtils.make("canvas", this.copiedObjects.className, this.application.ninja.currentDocument);
canvas.width = this.copiedObjects.width;
canvas.height = this.copiedObjects.height;
//end - clone copied canvas
if (!canvas.getAttribute( "data-RDGE-id" )) canvas.setAttribute( "data-RDGE-id", NJUtils.generateRandom() );
document.application.njUtils.createModelWithShape(canvas);
styles = canvas.elementModel.data || {};
styles.top = "" + (this.application.ninja.elementMediator.getProperty(this.copiedObjects, "top", parseInt) - 50) + "px";
styles.left = "" + (this.application.ninja.elementMediator.getProperty(this.copiedObjects, "left", parseInt) - 50) + "px";
this.application.ninja.elementMediator.addElements(canvas, styles, false);
var world, worldData = this.copiedObjects.elementModel.shapeModel.GLWorld.exportJSON();
if(worldData)
{
var jObj;
var index = worldData.indexOf( ';' );
if ((worldData[0] === 'v') && (index < 24))
{
// JSON format. separate the version info from the JSON info
var jStr = worldData.substr( index+1 );
jObj = JSON.parse( jStr );
world = new World(canvas, jObj.webGL);
canvas.elementModel.shapeModel.GLWorld = world;
canvas.elementModel.shapeModel.useWebGl = jObj.webGL;
world.importJSON(jObj);
this.application.ninja.currentDocument.buildShapeModel( canvas.elementModel, world );
}
}
NJevent("elementAdded", canvas);
clipboardHelper.removeChild(clipboardHelper.lastChild);
}
else if(clipboardHelper.lastChild.nodeType === 3){//TextNode
node = clipboardHelper.removeChild(clipboardHelper.lastChild);
//USE styles controller to create the styles of the div and span
var doc = this.application.ninja.currentDocument ? this.application.ninja.currentDocument._document : document;
var aspan = doc.createElement("span");
aspan.appendChild(node);
var adiv = doc.createElement("div");
adiv.appendChild(aspan);
styles = {"top":"100px", "left":"100px"};
this.pastePositioned(node, styles);
}
else {
node = clipboardHelper.removeChild(clipboardHelper.lastChild);
if(node.removeAttribute) {node.removeAttribute("style");}//remove the computed styles attribute which is placed only for pasting to external applications
//get class string while copying .... generate styles from class
//styles = {"top":"100px", "left":"100px"};
this.pastePositioned(node, styles);
}
}
this.application.ninja.documentController.activeDocument.needsSave = true;
}else if(textData){
//USE styles controller to create the styles of the div and span
clipboardHelper.innerHTML = "<div><span>"+ textData +"</span></div>";//add the copied html to generate the nodes
node = clipboardHelper.removeChild(clipboardHelper.lastChild);
styles = {"top":"100px", "left":"100px"};//get real stage center coordinates
this.pastePositioned(node, styles);
}
}
},
pastePositioned:{
value: function(element, styles){// for now can wok for both in-place and centered paste
NJUtils.createModel(element);
this.application.ninja.elementMediator.addElements(element, styles);
}
},
pasteInPlace:{
value: function(element){
NJUtils.createModel(element);
this.application.ninja.elementMediator.addElements(element, null);//does not work now
}
},
handleCut:{
value:function(clipboardEvent){
var clipboardData = clipboardEvent.clipboardData,
htmlData = clipboardData.getData("text/html"),
textData = clipboardData.getData("text/plain");
console.log("$$$ handleCut ", textData);
clipboardEvent.preventDefault();
}
},
createClipboardHelper:{
value:function(){
var doc = this.application.ninja.currentDocument ? this.application.ninja.currentDocument._document : document,
clipboardHelper=doc.getElementById("clipboardHelper");
//dynamically create editable div for execCommand->copy
if(!clipboardHelper){
clipboardHelper = doc.createElement ("div");
clipboardHelper.id = "clipboardHelper";
//clipboardHelper.style.display="none";
clipboardHelper.style.position = "absolute";
clipboardHelper.style.right = "0px";
clipboardHelper.style.top = "0px";
document.body.appendChild (clipboardHelper);
}
return clipboardHelper;
}
},
/*
parameters:
*/
copy:{
value: function(clipboardEvent){
var j=0, htmlToClipboard = "", ninjaClipboardObj = {};
this.copiedObjects.length = 0;
if(clipboardEvent){
for(j=0; j < this.application.ninja.selectedElements.length; j++){//copying from stage
this.copiedObjects.push(this.application.ninja.selectedElements[j]);
if(this.application.ninja.selectedElements[0].tagName === "CANVAS"){
if(!ninjaClipboardObj.canvas){
ninjaClipboardObj.canvas = true;
}
}else{
htmlToClipboard = htmlToClipboard + this.prepareOuterHtml(this.application.ninja.selectedElements[j]);
if(!ninjaClipboardObj.plainHtml){
ninjaClipboardObj.plainHtml = true;
}
}
}
//set clipboard data
clipboardEvent.clipboardData.setData('ninja', '' + JSON.stringify(ninjaClipboardObj));
clipboardEvent.clipboardData.setData('text/html', '<HTML><BODY>' + htmlToClipboard + '</BODY></HTML>');//copying first selected element for POC
}
}
},
prepareOuterHtml:{
value: function(elem){
var computedStyles = null, originalStyleAttr = null, computedStylesStr = "", i=0, stylePropertyName="", outerHtml = "";
originalStyleAttr = elem.getAttribute("style");//preserve the current styles
elem.removeAttribute("style");
//build the computed style attribute
computedStyles = elem.ownerDocument.defaultView.getComputedStyle(elem);
//todo: consider cleaning up the position data [or making position:relative with 0,0] from the computed styles,
// so that the object is pasted onto expernal applicaitons [like gmail] with no offset
for (i = 0; i < computedStyles.length; i++) {
stylePropertyName = computedStyles[i];
computedStylesStr = computedStylesStr + stylePropertyName + ":" + computedStyles.getPropertyValue(stylePropertyName) + ";";
}
elem.setAttribute("style", computedStylesStr);
outerHtml = elem.outerHTML;
elem.setAttribute("style", originalStyleAttr);//reset style after copying to clipboard
return outerHtml;
}
},
copyMontageComponents:{
value: function(){
}
},
/*
parameters:
*/
paste:{
value: function(){
}
},
pasteHTML:{
value: function(){
}
},
pasteImage:{
value:function(){
}
},
pasteCanvasObjects:{
value:function(){
}
},
pasteMontageComponents:{
value: function(){
}
}
});
|