diff options
author | Ananya Sen | 2012-07-16 15:34:07 -0700 |
---|---|---|
committer | Ananya Sen | 2012-07-16 15:34:07 -0700 |
commit | 5a34cac9594d710f6e5675bee34dc16be1b4d8a0 (patch) | |
tree | 1078009cd6443ac9cabfeb2cbdc5110b5e195b38 | |
parent | 5146f224258929415adf4a8022e492454b4e2476 (diff) | |
download | ninja-5a34cac9594d710f6e5675bee34dc16be1b4d8a0.tar.gz |
Fixed copy paste from external applications
Signed-off-by: Ananya Sen <Ananya.Sen@motorola.com>
-rw-r--r-- | js/clipboard/external-apps-clipboard-agent.js | 138 | ||||
-rw-r--r-- | js/clipboard/internal-ops/elements-clipboard-agent.js | 24 | ||||
-rwxr-xr-x | js/mediators/element-mediator.js | 6 |
3 files changed, 42 insertions, 126 deletions
diff --git a/js/clipboard/external-apps-clipboard-agent.js b/js/clipboard/external-apps-clipboard-agent.js index 6b9d8090..e78950df 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, |
@@ -57,16 +63,14 @@ var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.cr | |||
57 | }catch(e){ | 63 | }catch(e){ |
58 | console.log(""+e.stack); | 64 | console.log(""+e.stack); |
59 | } | 65 | } |
60 | this.application.ninja.selectionController.selectElements(imageElement); | ||
61 | this.application.ninja.currentDocument.model.needsSave = true; | 66 | this.application.ninja.currentDocument.model.needsSave = true; |
62 | |||
63 | } | 67 | } |
64 | } | 68 | } |
65 | } | 69 | } |
66 | 70 | ||
67 | try{ | 71 | try{ |
68 | if(!!htmlData || !!textData){ | 72 | if(!!htmlData || !!textData){ |
69 | this.pasteHtml(htmlData, textData); | 73 | this.doPasteHtml(htmlData, textData); |
70 | } | 74 | } |
71 | }catch(e){ | 75 | }catch(e){ |
72 | console.log(""+e.stack); | 76 | console.log(""+e.stack); |
@@ -108,16 +112,16 @@ var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.cr | |||
108 | //Adding element once it is loaded | 112 | //Adding element once it is loaded |
109 | element.onload = function () { | 113 | element.onload = function () { |
110 | element.onload = null; | 114 | element.onload = null; |
111 | self.application.ninja.elementMediator.addElements(element, rules, true); | 115 | self.application.ninja.elementMediator.addElements(element, rules, true/*notify*/, false /*callAddDelegate*/); |
112 | }; | 116 | }; |
113 | //Setting rules of element | 117 | //Setting rules of element |
114 | rules = { | 118 | rules = { |
115 | 'position': 'absolute', | 119 | 'position': 'absolute', |
116 | 'top' : '100px', | 120 | 'top' : '0px', |
117 | 'left' : '100px' | 121 | 'left' : '0px' |
118 | }; | 122 | }; |
119 | // | 123 | // |
120 | self.application.ninja.elementMediator.addElements(element, rules, false); | 124 | self.application.ninja.elementMediator.addElements(element, rules, false/*notify*/, false /*callAddDelegate*/); |
121 | } else { | 125 | } else { |
122 | //HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT | 126 | //HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT |
123 | } | 127 | } |
@@ -126,119 +130,37 @@ var ExternalAppsClipboardAgent = exports.ExternalAppsClipboardAgent = Montage.cr | |||
126 | } | 130 | } |
127 | }, | 131 | }, |
128 | 132 | ||
129 | //paste from external applicaitons | 133 | doPasteHtml:{ |
130 | pasteHtml:{ | ||
131 | value: function(htmlData, textData){ | 134 | value: function(htmlData, textData){ |
132 | var i=0, j=0, | 135 | 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 | 136 | ||
142 | if(htmlData){ | 137 | if(htmlData){ |
143 | |||
144 | //cleanse HTML | 138 | //cleanse HTML |
145 | 139 | htmlData = htmlData.replace(/\<meta [^>]+>/gi, ""); // Remove the meta tag. | |
146 | htmlData.replace(/[<script]/g," "); | 140 | htmlData = htmlData.replace(/\<script [^>]+>/g," "); // Remove the script tag. |
147 | |||
148 | this.application.ninja.selectedElements.length = 0; | ||
149 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} ); | ||
150 | |||
151 | try{ | ||
152 | nodeList = ClipboardUtil.deserializeHtmlString(htmlData);//this removes html and body tags | ||
153 | } | ||
154 | catch(e){ | ||
155 | console.log(""+e.stack); | ||
156 | } | ||
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 | |||
175 | this.pastePositioned(divWrapper, styles); | ||
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 | |||
204 | } | ||
205 | |||
206 | nodeList = null; | ||
207 | |||
208 | |||
209 | }else if(textData){ | ||
210 | node = ClipboardUtil.deserializeHtmlString("<div><span>"+ textData +"</span></div>")[0]; | ||
211 | styles = {"position":"absolute", "top":"100px", "left":"100px"}; | ||
212 | this.pastePositioned(node, styles); | ||
213 | } | 141 | } |
214 | 142 | ||
215 | NJevent("elementAdded", pastedElements); | 143 | textData = textData.replace(/\<script [^>]+>/g," "); // Remove any script tag. |
216 | this.application.ninja.currentDocument.model.needsSave = true; | ||
217 | 144 | ||
218 | } | ||
219 | }, | ||
220 | 145 | ||
221 | pastePositioned:{ | 146 | data = htmlData ? htmlData : textData; |
222 | value: function(element, styles, fromCopy){// for now can wok for both in-place and centered paste | ||
223 | var modObject = [], x,y, newX, newY, counter; | ||
224 | 147 | ||
225 | if((typeof fromCopy === "undefined") || (fromCopy && fromCopy === true)){ | 148 | if (data && data.length) { |
226 | counter = this.pasteCounter; | 149 | //deselect current selections |
227 | }else{ | 150 | this.application.ninja.selectedElements.length = 0; |
228 | counter = this.pasteCounter - 1; | 151 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": true} ); |
229 | } | ||
230 | 152 | ||
231 | x = styles ? ("" + styles.left + "px") : "100px"; | 153 | divWrapper = document.application.njUtils.make("div", null, this.application.ninja.currentDocument); |
232 | y = styles ? ("" + styles.top + "px") : "100px"; | 154 | this.application.ninja.elementMediator.addElements(divWrapper, {"height": "68px", |
233 | newX = styles ? ("" + (styles.left + (25 * counter)) + "px") : "100px"; | 155 | "left": "0px", |
234 | newY = styles ? ("" + (styles.top + (25 * counter)) + "px") : "100px"; | 156 | "position": "absolute", |
157 | "top": "0px", | ||
158 | "width": "161px"}); | ||
235 | 159 | ||
236 | if(!styles || (styles && !styles.position)){ | 160 | divWrapper.innerHTML = data; |
237 | this.application.ninja.elementMediator.addElements(element, null, false); | 161 | |
238 | }else if(styles && (styles.position === "absolute")){ | 162 | this.application.ninja.currentDocument.model.needsSave = true; |
239 | this.application.ninja.elementMediator.addElements(element, {"top" : newY, "left" : newX}, false);//displace | ||
240 | } | 163 | } |
241 | } | 164 | } |
242 | } | 165 | } |
243 | |||
244 | }); | 166 | }); |
diff --git a/js/clipboard/internal-ops/elements-clipboard-agent.js b/js/clipboard/internal-ops/elements-clipboard-agent.js index a6bce2ba..cfc75916 100644 --- a/js/clipboard/internal-ops/elements-clipboard-agent.js +++ b/js/clipboard/internal-ops/elements-clipboard-agent.js | |||
@@ -152,8 +152,6 @@ var ElementsClipboardAgent = exports.ElementsClipboardAgent = Montage.create(Com | |||