diff options
Diffstat (limited to 'js/clipboard/external-apps-clipboard-agent.js')
-rw-r--r-- | js/clipboard/external-apps-clipboard-agent.js | 164 |
1 files changed, 52 insertions, 112 deletions
diff --git a/js/clipboard/external-apps-clipboard-agent.js b/js/clipboard/external-apps-clipboard-agent.js index 6b9d8090..819f515e 100644 --- a/js/clipboard/external-apps-clipboard-agent.js +++ b/js/clipboard/external-apps-clipboard-agent.js | |||
@@ -38,6 +38,12 @@ var Montage = require("montage/core/core").Montage, | |||
38 | 38 | ||
39 | var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.create(Component, { | 39 | var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.create(Component, { |
40 | 40 | ||
41 | //count how many times pasted | ||
42 | //used to move multiple pastes of same copy | ||
43 | pasteCounter:{ | ||
44 | value: 0 | ||
45 | }, | ||
46 | |||
41 | paste:{ | 47 | paste:{ |
42 | value: function(clipboardEvent){ | 48 | value: function(clipboardEvent){ |
43 | var clipboardData = clipboardEvent.clipboardData, | 49 | var clipboardData = clipboardEvent.clipboardData, |
@@ -46,8 +52,13 @@ var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.cr | |||
46 | i=0, | 52 | i=0, |
47 | imageMime, imageData, imageElement; | 53 | imageMime, imageData, imageElement; |
48 | 54 | ||
49 | //handle image blobs | 55 | if(!!htmlData || !!textData){ |
50 | if(clipboardData.items && (clipboardData.items.length > 0)){ | 56 | try{ |
57 | this.doPasteHtml(htmlData, textData); | ||
58 | }catch(e){ | ||
59 | console.log(""+e.stack); | ||
60 | } | ||
61 | }else if(clipboardData.items && (clipboardData.items.length > 0)){//handle image blobs | ||
51 | for(i=0; i < clipboardData.items.length; i++ ){ | 62 | for(i=0; i < clipboardData.items.length; i++ ){ |
52 | if((clipboardData.items[i].kind === "file") && (clipboardData.items[i].type.indexOf("image") === 0)){//example type -> "image/png" | 63 | if((clipboardData.items[i].kind === "file") && (clipboardData.items[i].type.indexOf("image") === 0)){//example type -> "image/png" |
53 | imageMime = clipboardData.items[i].type; | 64 | imageMime = clipboardData.items[i].type; |
@@ -57,21 +68,10 @@ var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.cr | |||
57 | }catch(e){ | 68 | }catch(e){ |
58 | console.log(""+e.stack); | 69 | console.log(""+e.stack); |
59 | } | 70 | } |
60 | this.application.ninja.selectionController.selectElements(imageElement); | ||
61 | this.application.ninja.currentDocument.model.needsSave = true; | 71 | this.application.ninja.currentDocument.model.needsSave = true; |
62 | |||
63 | } | 72 | } |
64 | } | 73 | } |
65 | } | 74 | } |
66 | |||
67 | try{ | ||
68 | if(!!htmlData || !!textData){ | ||
69 | this.pasteHtml(htmlData, textData); | ||
70 | } | ||
71 | }catch(e){ | ||
72 | console.log(""+e.stack); | ||
73 | } | ||
74 | |||
75 | } | 75 | } |
76 | }, | 76 | }, |
77 | 77 | ||
@@ -108,16 +108,16 @@ var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.cr | |||
108 | //Adding element once it is loaded | 108 | //Adding element once it is loaded |
109 | element.onload = function () { | 109 | element.onload = function () { |
110 | element.onload = null; | 110 | element.onload = null; |
111 | self.application.ninja.elementMediator.addElements(element, rules, true); | 111 | self.application.ninja.elementMediator.addElements(element, rules, true/*notify*/, false /*callAddDelegate*/); |
112 | }; | 112 | }; |
113 | //Setting rules of element | 113 | //Setting rules of element |
114 | rules = { | 114 | rules = { |
115 | 'position': 'absolute', | 115 | 'position': 'absolute', |
116 | 'top' : '100px', | 116 | 'top' : '0px', |
117 | 'left' : '100px' | 117 | 'left' : '0px' |
118 | }; | 118 | }; |
119 | // | 119 | // |
120 | self.application.ninja.elementMediator.addElements(element, rules, false); | 120 | self.application.ninja.elementMediator.addElements(element, rules, false/*notify*/, false /*callAddDelegate*/); |
121 | } else { | 121 | } else { |
122 | //HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT | 122 | //HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT |
123 | } | 123 | } |
@@ -126,119 +126,59 @@ var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.cr | |||
126 | } | 126 | } |
127 | }, | 127 | }, |
128 | 128 | ||
129 | //paste from external applicaitons | 129 | doPasteHtml:{ |
130 | pasteHtml:{ | ||
131 | value: function(htmlData, textData){ | 130 | value: function(htmlData, textData){ |
132 | var i=0, j=0, | 131 | var divWrapper = null, data = null; |
133 | pasteDataObject=null, | ||
134 | pastedElements = [], | ||
135 | node = null, nodeList = null, | ||
136 | styles = null, | ||
137 | divWrapper = null, | ||
138 | spanWrapper = null, | ||
139 | metaEl = null, | ||
140 | self = this; | ||
141 | 132 | ||
142 | if(htmlData){ | 133 | htmlData = this.sanitize(htmlData); |
134 | textData = this.sanitize(textData); | ||
143 | 135 | ||
144 | //cleanse HTML | 136 | data = htmlData ? htmlData : textData; |
145 | |||
146 | htmlData.replace(/[<script]/g," "); | ||
147 | 137 | ||
138 | if (data && data.length) { | ||
139 | //deselect current selections | ||
148 | this.application.ninja.selectedElements.length = 0; | 140 | this.application.ninja.selectedElements.length = 0; |
149 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} ); | 141 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} ); |
150 | 142 | ||
151 | try{ | 143 | divWrapper = document.application.njUtils.make("div", null, this.application.ninja.currentDocument); |
152 | nodeList = ClipboardUtil.deserializeHtmlString(htmlData);//this removes html and body tags | 144 | this.application.ninja.elementMediator.addElements(divWrapper, {"height": "68px", |
153 | } | 145 | "left": "0px", |
154 | catch(e){ | 146 | "position": "absolute", |
155 | console.log(""+e.stack); | 147 | "top": "0px", |
156 | } | 148 | "width": "161px"}, false); |
157 | |||
158 | for(i=0; i < nodeList.length; i++){ | ||
159 | if(nodeList[i].tagName === "META") { | ||
160 | nodeList[i] = null; | ||
161 | } | ||
162 | else if (nodeList[i].tagName === "CANVAS"){ | ||
163 | //can't paste external canvas for lack of all metadata | ||
164 | nodeList[i] = null; | ||
165 | } | ||
166 | else if((nodeList[i].nodeType === 3) || (nodeList[i].tagName === "A")){ | ||
167 | node = nodeList[i].cloneNode(true); | ||
168 | |||
169 | divWrapper = document.application.njUtils.make("div", null, this.application.ninja.currentDocument); | ||
170 | spanWrapper = document.application.njUtils.make("span", null, this.application.ninja.currentDocument); | ||
171 | spanWrapper.appendChild(node); | ||
172 | divWrapper.appendChild(spanWrapper); | ||
173 | styles = {"position":"absolute", "top":"100px", "left":"100px"}; | ||
174 | 149 | ||
175 | this.pastePositioned(divWrapper, styles); | 150 | divWrapper.innerHTML = data; |
176 | |||
177 | nodeList[i] = null; | ||
178 | pastedElements.push(divWrapper); | ||
179 | |||
180 | }else if(nodeList[i].tagName === "SPAN"){ | ||
181 | node = nodeList[i].cloneNode(true); | ||
182 | |||
183 | divWrapper = document.application.njUtils.make("div", null, this.application.ninja.currentDocument); | ||
184 | divWrapper.appendChild(node); | ||
185 | styles = {"position":"absolute", "top":"100px", "left":"100px"}; | ||
186 | |||
187 | this.pastePositioned(divWrapper, styles); | ||
188 | |||
189 | nodeList[i] = null; | ||
190 | pastedElements.push(divWrapper); | ||
191 | } | ||
192 | else { | ||
193 | node = nodeList[i].cloneNode(true); | ||
194 | |||
195 | //get class string while copying .... generate styles from class | ||
196 | styles = {"position":"absolute", "top":"100px", "left":"100px"}; | ||
197 | |||
198 | this.pastePositioned(node, styles); | ||
199 | |||
200 | nodeList[i] = null; | ||
201 | pastedElements.push(node); | ||
202 | } | ||
203 | 151 | ||
152 | //hack to set the wrapper div's height and width as per the pasted content | ||
153 | var theclass = divWrapper.getAttribute("class"); | ||
154 | //temporarily remove the class to find the computed styles for the pasted content | ||
155 | if(theclass){ | ||
156 | divWrapper.removeAttribute("class"); | ||
204 | } | 157 | } |
158 | var height = divWrapper.ownerDocument.defaultView.getComputedStyle(divWrapper).getPropertyValue("height"); | ||
159 | var width = divWrapper.ownerDocument.defaultView.getComputedStyle(divWrapper).getPropertyValue("width"); | ||
205 | 160 | ||
206 | nodeList = null; | 161 | divWrapper.setAttribute("class", theclass); |
207 | |||
208 | 162 | ||
209 | }else if(textData){ | 163 | this.application.ninja.stylesController.setElementStyle(divWrapper, "height", height); |
210 | node = ClipboardUtil.deserializeHtmlString("<div><span>"+ textData +"</span></div>")[0]; | 164 | this.application.ninja.stylesController.setElementStyle(divWrapper, "width", width); |
211 | styles = {"position":"absolute", "top":"100px", "left":"100px"}; | 165 | //-end hack |
212 | this.pastePositioned(node, styles); | ||
213 | } | ||
214 | 166 | ||
215 | NJevent("elementAdded", pastedElements); | 167 | NJevent("elementAdded", divWrapper); |
216 | this.application.ninja.currentDocument.model.needsSave = true; | ||
217 | 168 | ||
169 | this.application.ninja.currentDocument.model.needsSave = true; | ||
170 | } | ||
218 | } | 171 | } |
219 | }, | 172 | }, |
220 |