]> git.wh0rd.org - tt-rss.git/blob - lib/dijit/_editor/plugins/EnterKeyHandling.js
add dijit/dojo stuff; initial ui mockup
[tt-rss.git] / lib / dijit / _editor / plugins / EnterKeyHandling.js
1 /*
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5 */
6
7
8 if(!dojo._hasResource["dijit._editor.plugins.EnterKeyHandling"]){
9 dojo._hasResource["dijit._editor.plugins.EnterKeyHandling"]=true;
10 dojo.provide("dijit._editor.plugins.EnterKeyHandling");
11 dojo.require("dojo.window");
12 dojo.declare("dijit._editor.plugins.EnterKeyHandling",dijit._editor._Plugin,{blockNodeForEnter:"BR",constructor:function(_1){
13 if(_1){
14 dojo.mixin(this,_1);
15 }
16 },setEditor:function(_2){
17 this.editor=_2;
18 if(this.blockNodeForEnter=="BR"){
19 if(dojo.isIE){
20 _2.contentDomPreFilters.push(dojo.hitch(this,"regularPsToSingleLinePs"));
21 _2.contentDomPostFilters.push(dojo.hitch(this,"singleLinePsToRegularPs"));
22 _2.onLoadDeferred.addCallback(dojo.hitch(this,"_fixNewLineBehaviorForIE"));
23 }else{
24 _2.onLoadDeferred.addCallback(dojo.hitch(this,function(d){
25 try{
26 this.editor.document.execCommand("insertBrOnReturn",false,true);
27 }
28 catch(e){
29 }
30 return d;
31 }));
32 }
33 }else{
34 if(this.blockNodeForEnter){
35 dojo["require"]("dijit._editor.range");
36 var h=dojo.hitch(this,this.handleEnterKey);
37 _2.addKeyHandler(13,0,0,h);
38 _2.addKeyHandler(13,0,1,h);
39 this.connect(this.editor,"onKeyPressed","onKeyPressed");
40 }
41 }
42 },onKeyPressed:function(e){
43 if(this._checkListLater){
44 if(dojo.withGlobal(this.editor.window,"isCollapsed",dijit)){
45 var _3=dojo.withGlobal(this.editor.window,"getAncestorElement",dijit._editor.selection,["LI"]);
46 if(!_3){
47 dijit._editor.RichText.prototype.execCommand.call(this.editor,"formatblock",this.blockNodeForEnter);
48 var _4=dojo.withGlobal(this.editor.window,"getAncestorElement",dijit._editor.selection,[this.blockNodeForEnter]);
49 if(_4){
50 _4.innerHTML=this.bogusHtmlContent;
51 if(dojo.isIE){
52 var r=this.editor.document.selection.createRange();
53 r.move("character",-1);
54 r.select();
55 }
56 }else{
57 console.error("onKeyPressed: Cannot find the new block node");
58 }
59 }else{
60 if(dojo.isMoz){
61 if(_3.parentNode.parentNode.nodeName=="LI"){
62 _3=_3.parentNode.parentNode;
63 }
64 }
65 var fc=_3.firstChild;
66 if(fc&&fc.nodeType==1&&(fc.nodeName=="UL"||fc.nodeName=="OL")){
67 _3.insertBefore(fc.ownerDocument.createTextNode(" "),fc);
68 var _5=dijit.range.create(this.editor.window);
69 _5.setStart(_3.firstChild,0);
70 var _6=dijit.range.getSelection(this.editor.window,true);
71 _6.removeAllRanges();
72 _6.addRange(_5);
73 }
74 }
75 }
76 this._checkListLater=false;
77 }
78 if(this._pressedEnterInBlock){
79 if(this._pressedEnterInBlock.previousSibling){
80 this.removeTrailingBr(this._pressedEnterInBlock.previousSibling);
81 }
82 delete this._pressedEnterInBlock;
83 }
84 },bogusHtmlContent:" ",blockNodes:/^(?:P|H1|H2|H3|H4|H5|H6|LI)$/,handleEnterKey:function(e){
85 var _7,_8,_9,_a=this.editor.document,br;
86 if(e.shiftKey){
87 var _b=dojo.withGlobal(this.editor.window,"getParentElement",dijit._editor.selection);
88 var _c=dijit.range.getAncestor(_b,this.blockNodes);
89 if(_c){
90 if(!e.shiftKey&&_c.tagName=="LI"){
91 return true;
92 }
93 _7=dijit.range.getSelection(this.editor.window);
94 _8=_7.getRangeAt(0);
95 if(!_8.collapsed){
96 _8.deleteContents();
97 _7=dijit.range.getSelection(this.editor.window);
98 _8=_7.getRangeAt(0);
99 }
100 if(dijit.range.atBeginningOfContainer(_c,_8.startContainer,_8.startOffset)){
101 if(e.shiftKey){
102 br=_a.createElement("br");
103 _9=dijit.range.create(this.editor.window);
104 _c.insertBefore(br,_c.firstChild);
105 _9.setStartBefore(br.nextSibling);
106 _7.removeAllRanges();
107 _7.addRange(_9);
108 }else{
109 dojo.place(br,_c,"before");
110 }
111 }else{
112 if(dijit.range.atEndOfContainer(_c,_8.startContainer,_8.startOffset)){
113 _9=dijit.range.create(this.editor.window);
114 br=_a.createElement("br");
115 if(e.shiftKey){
116 _c.appendChild(br);
117 _c.appendChild(_a.createTextNode(" "));
118 _9.setStart(_c.lastChild,0);
119 }else{
120 dojo.place(br,_c,"after");
121 _9.setStartAfter(_c);
122 }
123 _7.removeAllRanges();
124 _7.addRange(_9);
125 }else{
126 return true;
127 }
128 }
129 }else{
130 dijit._editor.RichText.prototype.execCommand.call(this.editor,"inserthtml","<br>");
131 }
132 return false;
133 }
134 var _d=true;
135 _7=dijit.range.getSelection(this.editor.window);
136 _8=_7.getRangeAt(0);
137 if(!_8.collapsed){
138 _8.deleteContents();
139 _7=dijit.range.getSelection(this.editor.window);
140 _8=_7.getRangeAt(0);
141 }
142 var _e=dijit.range.getBlockAncestor(_8.endContainer,null,this.editor.editNode);
143 var _f=_e.blockNode;
144 if((this._checkListLater=(_f&&(_f.nodeName=="LI"||_f.parentNode.nodeName=="LI")))){
145 if(dojo.isMoz){
146 this._pressedEnterInBlock=_f;
147 }
148 if(/^(\s|&nbsp;|\xA0|<span\b[^>]*\bclass=['"]Apple-style-span['"][^>]*>(\s|&nbsp;|\xA0)<\/span>)?(<br>)?$/.test(_f.innerHTML)){
149 _f.innerHTML="";
150 if(dojo.isWebKit){
151 _9=dijit.range.create(this.editor.window);
152 _9.setStart(_f,0);
153 _7.removeAllRanges();
154 _7.addRange(_9);
155 }
156 this._checkListLater=false;
157 }
158 return true;
159 }
160 if(!_e.blockNode||_e.blockNode===this.editor.editNode){
161 try{
162 dijit._editor.RichText.prototype.execCommand.call(this.editor,"formatblock",this.blockNodeForEnter);
163 }
164 catch(e2){
165 }
166 _e={blockNode:dojo.withGlobal(this.editor.window,"getAncestorElement",dijit._editor.selection,[this.blockNodeForEnter]),blockContainer:this.editor.editNode};
167 if(_e.blockNode){
168 if(_e.blockNode!=this.editor.editNode&&(!(_e.blockNode.textContent||_e.blockNode.innerHTML).replace(/^\s+|\s+$/g,"").length)){
169 this.removeTrailingBr(_e.blockNode);
170 return false;
171 }
172 }else{
173 _e.blockNode=this.editor.editNode;
174 }
175 _7=dijit.range.getSelection(this.editor.window);
176 _8=_7.getRangeAt(0);
177 }
178 var _10=_a.createElement(this.blockNodeForEnter);
179 _10.innerHTML=this.bogusHtmlContent;
180 this.removeTrailingBr(_e.blockNode);
181 if(dijit.range.atEndOfContainer(_e.blockNode,_8.endContainer,_8.endOffset)){
182 if(_e.blockNode===_e.blockContainer){
183 _e.blockNode.appendChild(_10);
184 }else{
185 dojo.place(_10,_e.blockNode,"after");
186 }
187 _d=false;
188 _9=dijit.range.create(this.editor.window);
189 _9.setStart(_10,0);
190 _7.removeAllRanges();
191 _7.addRange(_9);
192 if(this.editor.height){
193 dojo.window.scrollIntoView(_10);
194 }
195 }else{
196 if(dijit.range.atBeginningOfContainer(_e.blockNode,_8.startContainer,_8.startOffset)){
197 dojo.place(_10,_e.blockNode,_e.blockNode===_e.blockContainer?"first":"before");
198 if(_10.nextSibling&&this.editor.height){
199 _9=dijit.range.create(this.editor.window);
200 _9.setStart(_10.nextSibling,0);
201 _7.removeAllRanges();
202 _7.addRange(_9);
203 dojo.window.scrollIntoView(_10.nextSibling);
204 }
205 _d=false;
206 }else{
207 if(_e.blockNode===_e.blockContainer){
208 _e.blockNode.appendChild(_10);
209 }else{
210 dojo.place(_10,_e.blockNode,"after");
211 }
212 _d=false;
213 if(_e.blockNode.style){
214 if(_10.style){
215 if(_e.blockNode.style.cssText){
216 _10.style.cssText=_e.blockNode.style.cssText;
217 }
218 }
219 }
220 var rs=_8.startContainer;
221 if(rs&&rs.nodeType==3){
222 var _11,_12;
223 var txt=rs.nodeValue;
224 var _13=_a.createTextNode(txt.substring(0,_8.startOffset));
225 var _14=_a.createTextNode(txt.substring(_8.startOffset,txt.length));
226 dojo.place(_13,rs,"before");
227 dojo.place(_14,rs,"after");
228 dojo.destroy(rs);
229 var _15=_13.parentNode;
230 while(_15!==_e.blockNode){
231 var tg=_15.tagName;
232 var _16=_a.createElement(tg);
233 if(_15.style){
234 if(_16.style){
235 if(_15.style.cssText){
236 _16.style.cssText=_15.style.cssText;
237 }
238 }
239 }
240 _11=_14;
241 while(_11){
242 _12=_11.nextSibling;
243 _16.appendChild(_11);
244 _11=_12;
245 }
246 dojo.place(_16,_15,"after");
247 _13=_15;
248 _14=_16;
249 _15=_15.parentNode;
250 }
251 _11=_14;
252 if(_11.nodeType==1||(_11.nodeType==3&&_11.nodeValue)){
253 _10.innerHTML="";
254 }
255 while(_11){
256 _12=_11.nextSibling;
257 _10.appendChild(_11);
258 _11=_12;
259 }
260 }
261 _9=dijit.range.create(this.editor.window);
262 _9.setStart(_10,0);
263 _7.removeAllRanges();
264 _7.addRange(_9);
265 if(this.editor.height){
266 dijit.scrollIntoView(_10);
267 }
268 if(dojo.isMoz){
269 this._pressedEnterInBlock=_e.blockNode;
270 }
271 }
272 }
273 return _d;
274 },removeTrailingBr:function(_17){
275 var _18=/P|DIV|LI/i.test(_17.tagName)?_17:dijit._editor.selection.getParentOfType(_17,["P","DIV","LI"]);
276 if(!_18){
277 return;
278 }
279 if(_18.lastChild){
280 if((_18.childNodes.length>1&&_18.lastChild.nodeType==3&&/^[\s\xAD]*$/.test(_18.lastChild.nodeValue))||_18.lastChild.tagName=="BR"){
281 dojo.destroy(_18.lastChild);
282 }
283 }
284 if(!_18.childNodes.length){
285 _18.innerHTML=this.bogusHtmlContent;
286 }
287 },_fixNewLineBehaviorForIE:function(d){
288 var doc=this.editor.document;
289 if(doc.__INSERTED_EDITIOR_NEWLINE_CSS===undefined){
290 var _19=dojo.create("style",{type:"text/css"},doc.getElementsByTagName("head")[0]);
291 _19.styleSheet.cssText="p{margin:0;}";
292 this.editor.document.__INSERTED_EDITIOR_NEWLINE_CSS=true;
293 }
294 return d;
295 },regularPsToSingleLinePs:function(_1a,_1b){
296 function _1c(el){
297 function _1d(_1e){
298 var _1f=_1e[0].ownerDocument.createElement("p");
299 _1e[0].parentNode.insertBefore(_1f,_1e[0]);
300 dojo.forEach(_1e,function(_20){
301 _1f.appendChild(_20);
302 });
303 };
304 var _21=0;
305 var _22=[];
306 var _23;
307 while(_21<el.childNodes.length){
308 _23=el.childNodes[_21];
309 if(_23.nodeType==3||(_23.nodeType==1&&_23.nodeName!="BR"&&dojo.style(_23,"display")!="block")){
310 _22.push(_23);
311 }else{
312 var _24=_23.nextSibling;
313 if(_22.length){
314 _1d(_22);
315 _21=(_21+1)-_22.length;
316 if(_23.nodeName=="BR"){
317 dojo.destroy(_23);
318 }
319 }
320 _22=[];
321 }
322 _21++;
323 }
324 if(_22.length){
325 _1d(_22);
326 }
327 };
328 function _25(el){
329 var _26=null;
330 var _27=[];
331 var _28=el.childNodes.length-1;
332 for(var i=_28;i>=0;i--){
333 _26=el.childNodes[i];
334 if(_26.nodeName=="BR"){
335 var _29=_26.ownerDocument.createElement("p");
336 dojo.place(_29,el,"after");
337 if(_27.length==0&&i!=_28){
338 _29.innerHTML="&nbsp;";
339 }
340 dojo.forEach(_27,function(_2a){
341 _29.appendChild(_2a);
342 });
343 dojo.destroy(_26);
344 _27=[];
345 }else{
346 _27.unshift(_26);
347 }
348 }
349 };
350 var _2b=[];
351 var ps=_1a.getElementsByTagName("p");
352 dojo.forEach(ps,function(p){
353 _2b.push(p);
354 });
355 dojo.forEach(_2b,function(p){
356 var _2c=p.previousSibling;
357 if((_2c)&&(_2c.nodeType==1)&&(_2c.nodeName=="P"||dojo.style(_2c,"display")!="block")){
358 var _2d=p.parentNode.insertBefore(this.document.createElement("p"),p);
359 _2d.innerHTML=_1b?"":"&nbsp;";
360 }
361 _25(p);
362 },this.editor);
363 _1c(_1a);
364 return _1a;
365 },singleLinePsToRegularPs:function(_2e){
366 function _2f(_30){
367 var ps=_30.getElementsByTagName("p");
368 var _31=[];
369 for(var i=0;i<ps.length;i++){
370 var p=ps[i];
371 var _32=false;
372 for(var k=0;k<_31.length;k++){
373 if(_31[k]===p.parentNode){
374 _32=true;
375 break;
376 }
377 }
378 if(!_32){
379 _31.push(p.parentNode);
380 }
381 }
382 return _31;
383 };
384 function _33(_34){
385 return (!_34.childNodes.length||_34.innerHTML=="&nbsp;");
386 };
387 var _35=_2f(_2e);
388 for(var i=0;i<_35.length;i++){
389 var _36=_35[i];
390 var _37=null;
391 var _38=_36.firstChild;
392 var _39=null;
393 while(_38){
394 if(_38.nodeType!=1||_38.tagName!="P"||(_38.getAttributeNode("style")||{}).specified){
395 _37=null;
396 }else{
397 if(_33(_38)){
398 _39=_38;
399 _37=null;
400 }else{
401 if(_37==null){
402 _37=_38;
403 }else{
404 if((!_37.lastChild||_37.lastChild.nodeName!="BR")&&(_38.firstChild)&&(_38.firstChild.nodeName!="BR")){
405 _37.appendChild(this.editor.document.createElement("br"));
406 }
407 while(_38.firstChild){
408 _37.appendChild(_38.firstChild);
409 }
410 _39=_38;
411 }
412 }
413 }
414 _38=_38.nextSibling;
415 if(_39){
416 dojo.destroy(_39);
417 _39=null;
418 }
419 }
420 }
421 return _2e;
422 }});
423 }