Index: trunk/extensions/UsabilityInitiative/js/plugins/codepress/engines/engine.js |
— | — | @@ -0,0 +1,574 @@ |
| 2 | + |
| 3 | +gecko = false; |
| 4 | +if($.browser.safari || $.browser.mozilla ){ |
| 5 | + gecko = true; |
| 6 | +} |
| 7 | + |
| 8 | +WikiEditorEngine = { |
| 9 | + scrolling : false, |
| 10 | + autocomplete : true, |
| 11 | + |
| 12 | + initialize : function() { |
| 13 | + if(typeof(editor)=='undefined' && !arguments[0]) return; |
| 14 | + |
| 15 | + if(gecko){ |
| 16 | + body = document.getElementsByTagName('body')[0]; |
| 17 | + body.innerHTML = body.innerHTML.replace(/\n/g,""); |
| 18 | + } |
| 19 | + |
| 20 | + chars = '|32|46|62|'; // charcodes that trigger syntax highlighting |
| 21 | + |
| 22 | + if(gecko){ |
| 23 | + chars += '8|'; |
| 24 | + } |
| 25 | + |
| 26 | + cc = '\u2009'; // carret char |
| 27 | + editorTag = 'pre'; |
| 28 | + if($.browser.opera){ |
| 29 | + editorTag = 'body'; |
| 30 | + } |
| 31 | + editor = document.getElementsByTagName(editorTag)[0]; |
| 32 | + |
| 33 | + if($.browser.msie){ |
| 34 | + editor.contentEditable = 'true'; |
| 35 | + document.getElementsByTagName('body')[0].onfocus = function() {editor.focus();} |
| 36 | + document.attachEvent('onkeydown', this.metaHandler); |
| 37 | + document.attachEvent('onkeypress', this.keyHandler); |
| 38 | + window.attachEvent('onscroll', function() { if(!WikiEditorEngine.scrolling) setTimeout(function(){WikiEditorEngine.syntaxHighlight('scroll')},1)}); |
| 39 | + } |
| 40 | + else{ //!$.browser.msie |
| 41 | + document.designMode = 'on'; |
| 42 | + if(gecko){ |
| 43 | + document.addEventListener('keypress', this.keyHandler, true); |
| 44 | + } |
| 45 | + if($.browser.opera){ |
| 46 | + document.addEventListener('keyup', this.keyHandler, true); |
| 47 | + } |
| 48 | + window.addEventListener('scroll', function() { if(!WikiEditorEngine.scrolling) WikiEditorEngine.syntaxHighlight('scroll') }, false); |
| 49 | + } |
| 50 | + |
| 51 | + completeChars = this.getCompleteChars(); |
| 52 | + if(!$.browser.opera){ |
| 53 | + completeEndingChars = this.getCompleteEndingChars(); |
| 54 | + } |
| 55 | + if($.browser.msie){ |
| 56 | + setTimeout(function() { window.scroll(0,0) },50); // scroll IE to top |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + keyHandler : function(evt) { |
| 61 | + |
| 62 | + if(gecko){ |
| 63 | + keyCode = evt.keyCode; |
| 64 | + charCode = evt.charCode; |
| 65 | + fromChar = String.fromCharCode(charCode); |
| 66 | + |
| 67 | + if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo) |
| 68 | + WikiEditorEngine.shortcuts(charCode?charCode:keyCode); |
| 69 | + } |
| 70 | + else if( (completeEndingChars.indexOf('|'+fromChar+'|')!= -1 || completeChars.indexOf('|'+fromChar+'|')!=-1) && WikiEditorEngine.autocomplete) { // auto complete |
| 71 | + if(!WikiEditorEngine.completeEnding(fromChar)) |
| 72 | + WikiEditorEngine.complete(fromChar); |
| 73 | + } |
| 74 | + else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting |
| 75 | + top.setTimeout(function(){WikiEditorEngine.syntaxHighlight('generic');},100); |
| 76 | + } |
| 77 | + else if(keyCode==9 || evt.tabKey) { // snippets activation (tab) |
| 78 | + WikiEditorEngine.snippets(evt); |
| 79 | + } |
| 80 | + else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed |
| 81 | + WikiEditorEngine.actions.history[WikiEditorEngine.actions.next()] = editor.innerHTML; |
| 82 | + } |
| 83 | + else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo |
| 84 | + (charCode==121||evt.shiftKey) ? WikiEditorEngine.actions.redo() : WikiEditorEngine.actions.undo(); |
| 85 | + evt.preventDefault(); |
| 86 | + } |
| 87 | + else if(charCode==118 && evt.ctrlKey) { // handle paste |
| 88 | + top.setTimeout(function(){WikiEditorEngine.syntaxHighlight('generic');},100); |
| 89 | + } |
| 90 | + else if(charCode==99 && evt.ctrlKey) { // handle cut |
| 91 | + //alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF')); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + else if($.browser.opera){ |
| 96 | + keyCode = evt.keyCode; |
| 97 | + charCode = evt.charCode; |
| 98 | + |
| 99 | + if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo) |
| 100 | + WikiEditorEngine.shortcuts(charCode?charCode:keyCode); |
| 101 | + } |
| 102 | + else if(completeChars.indexOf('|'+String.fromCharCode(charCode)+'|')!=-1 && WikiEditorEngine.autocomplete) { // auto complete |
| 103 | + WikiEditorEngine.complete(String.fromCharCode(charCode)); |
| 104 | + } |
| 105 | + else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting |
| 106 | + WikiEditorEngine.syntaxHighlight('generic'); |
| 107 | + } |
| 108 | + else if(keyCode==9 || evt.tabKey) { // snippets activation (tab) |
| 109 | + WikiEditorEngine.snippets(evt); |
| 110 | + } |
| 111 | + else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed |
| 112 | + WikiEditorEngine.actions.history[WikiEditorEngine.actions.next()] = editor.innerHTML; |
| 113 | + } |
| 114 | + else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo |
| 115 | + (charCode==121||evt.shiftKey) ? WikiEditorEngine.actions.redo() : WikiEditorEngine.actions.undo(); |
| 116 | + evt.preventDefault(); |
| 117 | + } |
| 118 | + else if(keyCode==86 && evt.ctrlKey) { // paste |
| 119 | + // TODO: pasted text should be parsed and highlighted |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + else if($.browser.msie){ |
| 124 | + charCode = evt.keyCode; |
| 125 | + fromChar = String.fromCharCode(charCode); |
| 126 | + |
| 127 | + if( (completeEndingChars.indexOf('|'+fromChar+'|')!= -1 || completeChars.indexOf('|'+fromChar+'|')!=-1 )&& WikiEditorEngine.autocomplete) { // auto complete |
| 128 | + if(!WikiEditorEngine.completeEnding(fromChar)) |
| 129 | + WikiEditorEngine.complete(fromChar); |
| 130 | + } |
| 131 | + else if(chars.indexOf('|'+charCode+'|')!=-1||charCode==13) { // syntax highlighting |
| 132 | + WikiEditorEngine.syntaxHighlight('generic'); |
| 133 | + } |
| 134 | + } |
| 135 | + }, |
| 136 | + |
| 137 | + //IE SPECIFIC FN |
| 138 | + metaHandler : function(evt) { |
| 139 | + keyCode = evt.keyCode; |
| 140 | + |
| 141 | + if(keyCode==9 || evt.tabKey) { |
| 142 | + WikiEditorEngine.snippets(); |
| 143 | + } |
| 144 | + else if((keyCode==122||keyCode==121||keyCode==90) && evt.ctrlKey) { // undo and redo |
| 145 | + (keyCode==121||evt.shiftKey) ? WikiEditorEngine.actions.redo() : WikiEditorEngine.actions.undo(); |
| 146 | + evt.returnValue = false; |
| 147 | + } |
| 148 | + else if(keyCode==34||keyCode==33) { // handle page up/down for IE |
| 149 | + self.scrollBy(0, (keyCode==34) ? 200 : -200); |
| 150 | + evt.returnValue = false; |
| 151 | + } |
| 152 | + else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed |
| 153 | + WikiEditorEngine.actions.history[WikiEditorEngine.actions.next()] = editor.innerHTML; |
| 154 | + } |
| 155 | + else if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && keyCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo) |
| 156 | + WikiEditorEngine.shortcuts(keyCode); |
| 157 | + evt.returnValue = false; |
| 158 | + } |
| 159 | + else if(keyCode==86 && evt.ctrlKey) { // handle paste |
| 160 | + window.clipboardData.setData('Text',window.clipboardData.getData('Text').replace(/\t/g,'\u2008')); |
| 161 | + top.setTimeout(function(){WikiEditorEngine.syntaxHighlight('paste');},10); |
| 162 | + } |
| 163 | + else if(keyCode==67 && evt.ctrlKey) { // handle cut |
| 164 | + // window.clipboardData.setData('Text',x[0]); |
| 165 | + // code = window.clipboardData.getData('Text'); |
| 166 | + } |
| 167 | + }, |
| 168 | + |
| 169 | + // put cursor back to its original position after every parsing |
| 170 | + findString : function() { |
| 171 | + if(gecko){ |
| 172 | + if(self.find(cc)) |
| 173 | + window.getSelection().getRangeAt(0).deleteContents(); |
| 174 | + } |
| 175 | + else if($.browser.opera) { |
| 176 | + var sel = window.getSelection(); |
| 177 | + var range = window.document.createRange(); |
| 178 | + var span = window.document.getElementsByTagName('span')[0]; |
| 179 | + |
| 180 | + range.selectNode(span); |
| 181 | + sel.removeAllRanges(); |
| 182 | + sel.addRange(range); |
| 183 | + span.parentNode.removeChild(span); |
| 184 | + //if(self.find(cc)) |
| 185 | + //window.getSelection().getRangeAt(0).deleteContents(); |
| 186 | + } |
| 187 | + else if($.browser.msie){ |
| 188 | + range = self.document.body.createTextRange(); |
| 189 | + if(range.findText(cc)){ |
| 190 | + range.select(); |
| 191 | + range.text = ''; |
| 192 | + } |
| 193 | + } |
| 194 | + }, |
| 195 | + |
| 196 | + // split big files, highlighting parts of it |
| 197 | + split : function(code,flag) { |
| 198 | + if(gecko){ |
| 199 | + if(flag=='scroll') { |
| 200 | + this.scrolling = true; |
| 201 | + return code; |
| 202 | + } |
| 203 | + else { |
| 204 | + this.scrolling = false; |
| 205 | + mid = code.indexOf(cc); |
| 206 | + if(mid-2000<0) {ini=0;end=4000;} |
| 207 | + else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;} |
| 208 | + else {ini=mid-2000;end=mid+2000;} |
| 209 | + code = code.substring(ini,end); |
| 210 | + return code; |
| 211 | + } |
| 212 | + } |
| 213 | + else if($.browser.opera){ |
| 214 | + if(flag=='scroll') { |
| 215 | + this.scrolling = true; |
| 216 | + return code; |
| 217 | + } |
| 218 | + else { |
| 219 | + this.scrolling = false; |
| 220 | + mid = code.indexOf('<SPAN>'); |
| 221 | + if(mid-2000<0) {ini=0;end=4000;} |
| 222 | + else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;} |
| 223 | + else {ini=mid-2000;end=mid+2000;} |
| 224 | + code = code.substring(ini,end); |
| 225 | + return code; |
| 226 | + } |
| 227 | + } |
| 228 | + else if($.browser.msie){ |
| 229 | + if(flag=='scroll') { |
| 230 | + this.scrolling = true; |
| 231 | + return code; |
| 232 | + } |
| 233 | + else { |
| 234 | + this.scrolling = false; |
| 235 | + mid = code.indexOf(cc); |
| 236 | + if(mid-2000<0) {ini=0;end=4000;} |
| 237 | + else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;} |
| 238 | + else {ini=mid-2000;end=mid+2000;} |
| 239 | + code = code.substring(ini,end); |
| 240 | + return code.substring(code.indexOf('<P>'),code.lastIndexOf('</P>')+4); |
| 241 | + } |
| 242 | + } |
| 243 | + }, |
| 244 | + |
| 245 | +//GECKO SPECIFIC |
| 246 | + getEditor : function() { |
| 247 | + if(!document.getElementsByTagName('pre')[0]) { |
| 248 | + body = document.getElementsByTagName('body')[0]; |
| 249 | + if(!body.innerHTML) return body; |
| 250 | + if(body.innerHTML=="<br>") body.innerHTML = "<pre> </pre>"; |
| 251 | + else body.innerHTML = "<pre>"+body.innerHTML+"</pre>"; |
| 252 | + } |
| 253 | + return document.getElementsByTagName('pre')[0]; |
| 254 | + }, |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | + // syntax highlighting parser |
| 259 | + syntaxHighlight : function(flag) { |
| 260 | + if(gecko){ |
| 261 | + if(flag != 'init') { window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));} |
| 262 | + editor = WikiEditorEngine.getEditor(); |
| 263 | + o = editor.innerHTML; |
| 264 | + o = o.replace(/<br>/g,'\n'); |
| 265 | + o = o.replace(/<.*?>/g,''); |
| 266 | + x = z = this.split(o,flag); |
| 267 | + x = x.replace(/\n/g,'<br>'); |
| 268 | + |
| 269 | + if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]); |
| 270 | + |
| 271 | + for(i=0;i<Language.syntax.length;i++) |
| 272 | + x = x.replace(Language.syntax[i].input,Language.syntax[i].output); |
| 273 | + |
| 274 | + editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.split(z).join(x); |
| 275 | + if(flag!='init') this.findString(); |
| 276 | + } |
| 277 | + else if($.browser.opera){ |
| 278 | + if(flag!='init') { |
| 279 | + var span = document.createElement('span'); |
| 280 | + window.getSelection().getRangeAt(0).insertNode(span); |
| 281 | + } |
| 282 | + |
| 283 | + o = editor.innerHTML; |
| 284 | + |
| 285 | + o = o.replace(/<(?!span|\/span|br).*?>/gi,''); |
| 286 | + x = z = this.split(o,flag); |
| 287 | + x = x.replace(/\t/g, ' '); |
| 288 | + |
| 289 | + if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]); |
| 290 | + |
| 291 | + for(i=0;i<Language.syntax.length;i++) |
| 292 | + x = x.replace(Language.syntax[i].input,Language.syntax[i].output); |
| 293 | + |
| 294 | + editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.split(z).join(x); |
| 295 | + |
| 296 | + if(flag!='init') this.findString(); |
| 297 | + } |
| 298 | + else if($.browser.msie){ |
| 299 | + if(flag!='init') document.selection.createRange().text = cc; |
| 300 | + o = editor.innerHTML; |
| 301 | + if(flag=='paste') { // fix pasted text |
| 302 | + o = o.replace(/<BR>/g,'\r\n'); |
| 303 | + o = o.replace(/\u2008/g,'\t'); |
| 304 | + } |
| 305 | + o = o.replace(/<P>/g,'\n'); |
| 306 | + o = o.replace(/<\/P>/g,'\r'); |
| 307 | + o = o.replace(/<.*?>/g,''); |
| 308 | + o = o.replace(/ /g,''); |
| 309 | + o = '<PRE><P>'+o+'</P></PRE>'; |
| 310 | + o = o.replace(/\n\r/g,'<P></P>'); |
| 311 | + o = o.replace(/\n/g,'<P>'); |
| 312 | + o = o.replace(/\r/g,'<\/P>'); |
| 313 | + o = o.replace(/<P>(<P>)+/,'<P>'); |
| 314 | + o = o.replace(/<\/P>(<\/P>)+/,'</P>'); |
| 315 | + o = o.replace(/<P><\/P>/g,'<P><BR/></P>'); |
| 316 | + x = z = this.split(o,flag); |
| 317 | + |
| 318 | + if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]); |
| 319 | + |
| 320 | + for(i=0;i<Language.syntax.length;i++) |
| 321 | + x = x.replace(Language.syntax[i].input,Language.syntax[i].output); |
| 322 | + |
| 323 | + editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.replace(z,x); |
| 324 | + if(flag!='init') this.findString(); |
| 325 | + } |
| 326 | + }, |
| 327 | + |
| 328 | + getLastWord : function() { |
| 329 | + var rangeAndCaret = WikiEditorEngine.getRangeAndCaret(); |
| 330 | + words = rangeAndCaret[0].substring(rangeAndCaret[1]-40,rangeAndCaret[1]); |
| 331 | + words = words.replace(/[\s\n\r\);\W]/g,'\n').split('\n'); |
| 332 | + return words[words.length-1].replace(/[\W]/gi,'').toLowerCase(); |
| 333 | + }, |
| 334 | + |
| 335 | + |
| 336 | + snippets : function(evt) { |
| 337 | + var snippets = Language.snippets; |
| 338 | + var trigger = this.getLastWord(); |
| 339 | + for (var i=0; i<snippets.length; i++) { |
| 340 | + if(snippets[i].input == trigger) { |
| 341 | + var content = snippets[i].output.replace(/</g,'<'); |
| 342 | + content = content.replace(/>/g,'>'); |
| 343 | + if(content.indexOf('$0')<0) content += cc; |
| 344 | + else content = content.replace(/\$0/,cc); |
| 345 | + content = content.replace(/\n/g,'<br>'); |
| 346 | + var pattern = new RegExp(trigger+cc,'gi'); |
| 347 | + if(!$.browser.msie){ |
| 348 | + evt.preventDefault(); // prevent the tab key from being added |
| 349 | + } |
| 350 | + this.syntaxHighlight('snippets',pattern,content); |
| 351 | + } |
| 352 | + } |
| 353 | + }, |
| 354 | + |
| 355 | + readOnly : function() { |
| 356 | + if($.browser.msie){ |
| 357 | + editor.contentEditable = (arguments[0]) ? 'false' : 'true'; |
| 358 | + } |
| 359 | + else if (gecko || $.browser.opera){ |
| 360 | + document.designMode = (arguments[0]) ? 'off' : 'on'; |
| 361 | + } |
| 362 | + }, |
| 363 | + |
| 364 | + |
| 365 | + complete : function(trigger) { |
| 366 | + window.getSelection().getRangeAt(0).deleteContents(); |
| 367 | + var complete = Language.complete; |
| 368 | + for (var i=0; i<complete.length; i++) { |
| 369 | + if(complete[i].input == trigger) { |
| 370 | + var pattern = new RegExp('\\'+trigger+cc); |
| 371 | + var content = complete[i].output.replace(/\$0/g,cc); |
| 372 | + parent.setTimeout(function () { WikiEditorEngine.syntaxHighlight('complete',pattern,content)},0); // wait for char to appear on screen |
| 373 | + } |
| 374 | + } |
| 375 | + }, |
| 376 | + |
| 377 | + getCompleteChars : function() { |
| 378 | + var cChars = ''; |
| 379 | + for(var i=0;i<Language.complete.length;i++) |
| 380 | + cChars += '|'+Language.complete[i].input; |
| 381 | + return cChars+'|'; |
| 382 | + }, |
| 383 | + |
| 384 | + //not in Opera |
| 385 | + getCompleteEndingChars : function() { |
| 386 | + var cChars = ''; |
| 387 | + for(var i=0;i<Language.complete.length;i++) |
| 388 | + cChars += '|'+Language.complete[i].output.charAt(Language.complete[i].output.length-1); |
| 389 | + return cChars+'|'; |
| 390 | + }, |
| 391 | + |
| 392 | + |
| 393 | +//also not in opera |
| 394 | + completeEnding : function(trigger) { |
| 395 | + if(gecko){ |
| 396 | + var range = window.getSelection().getRangeAt(0); |
| 397 | + try { |
| 398 | + range.setEnd(range.endContainer, range.endOffset+1) |
| 399 | + } |
| 400 | + catch(e) { |
| 401 | + return false; |
| 402 | + } |
| 403 | + var next_character = range.toString() |
| 404 | + range.setEnd(range.endContainer, range.endOffset-1) |
| 405 | + if(next_character != trigger) return false; |
| 406 | + else { |
| 407 | + range.setEnd(range.endContainer, range.endOffset+1) |
| 408 | + range.deleteContents(); |
| 409 | + return true; |
| 410 | + } |
| 411 | + } |
| 412 | + else if($.browser.msie){ |
| 413 | + var range = document.selection.createRange(); |
| 414 | + try { |
| 415 | + range.moveEnd('character', 1) |
| 416 | + } |
| 417 | + catch(e) { |
| 418 | + return false; |
| 419 | + } |
| 420 | + var next_character = range.text |
| 421 | + range.moveEnd('character', -1) |
| 422 | + if(next_character != trigger ) return false; |
| 423 | + else { |
| 424 | + range.moveEnd('character', 1) |
| 425 | + range.text='' |
| 426 | + return true; |
| 427 | + } |
| 428 | + } |
| 429 | + }, |
| 430 | + |
| 431 | + shortcuts : function() { |
| 432 | + var cCode = arguments[0]; |
| 433 | + if(cCode==13) cCode = '[enter]'; |
| 434 | + else if(cCode==32) cCode = '[space]'; |
| 435 | + else cCode = '['+String.fromCharCode(charCode).toLowerCase()+']'; |
| 436 | + for(var i=0;i<Language.shortcuts.length;i++) |
| 437 | + if(Language.shortcuts[i].input == cCode) |
| 438 | + this.insertCode(Language.shortcuts[i].output,false); |
| 439 | + }, |
| 440 | + |
| 441 | + getRangeAndCaret : function() { |
| 442 | + if($.browser.msie){ |
| 443 | + var range = document.selection.createRange(); |
| 444 | + var caret = Math.abs(range.moveStart('character', -1000000)+1); |
| 445 | + range = this.getCode(); |
| 446 | + range = range.replace(/\n\r/gi,' '); |
| 447 | + range = range.replace(/\n/gi,''); |
| 448 | + return [range.toString(),caret]; |
| 449 | + } |
| 450 | + else if(!$.browser.msie){ |
| 451 | + var range = window.getSelection().getRangeAt(0); |
| 452 | + var range2 = range.cloneRange(); |
| 453 | + var node = range.endContainer; |
| 454 | + var caret = range.endOffset; |
| 455 | + range2.selectNode(node); |
| 456 | + return [range2.toString(),caret]; |
| 457 | + } |
| 458 | + }, |
| 459 | + |
| 460 | + insertCode : function(code,replaceCursorBefore) { |
| 461 | + if($.browser.msie){ |
| 462 | + var repdeb = ''; |
| 463 | + var repfin = ''; |
| 464 | + |
| 465 | + if(replaceCursorBefore) { repfin = code; } |
| 466 | + else { repdeb = code; } |
| 467 | + |
| 468 | + if(typeof document.selection != 'undefined') { |
| 469 | + var range = document.selection.createRange(); |
| 470 | + range.text = repdeb + repfin; |
| 471 | + range = document.selection.createRange(); |
| 472 | + range.move('character', -repfin.length); |
| 473 | + range.select(); |
| 474 | + } |
| 475 | + } |
| 476 | + else if(!$.browser.msie){ |
| 477 | + var range = window.getSelection().getRangeAt(0); |
| 478 | + var node = window.document.createTextNode(code); |
| 479 | + var selct = window.getSelection(); |
| 480 | + var range2 = range.cloneRange(); |
| 481 | + // Insert text at cursor position |
| 482 | + selct.removeAllRanges(); |
| 483 | + range.deleteContents(); |
| 484 | + range.insertNode(node); |
| 485 | + // Move the cursor to the end of text |
| 486 | + range2.selectNode(node); |
| 487 | + range2.collapse(replaceCursorBefore); |
| 488 | + selct.removeAllRanges(); |
| 489 | + selct.addRange(range2); |
| 490 | + } |
| 491 | + }, |
| 492 | + |
| 493 | + getCode : function() { |
| 494 | + if(gecko && !document.getElementsByTagName('pre')[0] || editor.innerHTML == '') |
| 495 | + editor = WikiEditorEngine.getEditor(); |
| 496 | + var code = editor.innerHTML; |
| 497 | + code = code.replace(/<br>/g,'\n'); |
| 498 | + code = code.replace(/\u2009/g,''); |
| 499 | + code = code.replace(/<.*?>/g,''); |
| 500 | + code = code.replace(/</g,'<'); |
| 501 | + code = code.replace(/>/g,'>'); |
| 502 | + code = code.replace(/&/gi,'&'); |
| 503 | + |
| 504 | + if($.browser.msie){ |
| 505 | + code = code.replace(/<\/p>/gi,'\r'); |
| 506 | + code = code.replace(/<p>/i,''); // IE first line fix |
| 507 | + code = code.replace(/<p>/gi,'\n'); |
| 508 | + code = code.replace(/ /gi,''); |
| 509 | + } |
| 510 | + return code; |
| 511 | + }, |
| 512 | + |
| 513 | + setCode : function() { |
| 514 | + var code = arguments[0]; |
| 515 | + code = code.replace(/\u2009/gi,''); |
| 516 | + code = code.replace(/&/gi,'&'); |
| 517 | + code = code.replace(/</g,'<'); |
| 518 | + code = code.replace(/>/g,'>'); |
| 519 | + if($.browser.msie){ |
| 520 | + editor.innerHTML = '<pre>'+code+'</pre>';} |
| 521 | + else if (!$.browser.msie){ |
| 522 | + editor.innerHTML = code; |
| 523 | + } |
| 524 | + if (gecko && code == ''){ |
| 525 | + document.getElementsByTagName('body')[0].innerHTML = ''; |
| 526 | + } |
| 527 | + }, |
| 528 | + |
| 529 | +//g |
| 530 | + actions : { |
| 531 | + pos : -1, // actual history position |
| 532 | + history : [], // history vector |
| 533 | + |
| 534 | + undo : function() { |
| 535 | + if(gecko){ |
| 536 | + editor = WikiEditorEngine.getEditor(); |
| 537 | + } |
| 538 | + if(editor.innerHTML.indexOf(cc)==-1){ |
| 539 | + if(opera || (gecko && editor.innerHTML != " ")) |
| 540 | + window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc)); |
| 541 | + if($.browser.msie){ |
| 542 | + document.selection.createRange().text = cc; |
| 543 | + } |
| 544 | + this.history[this.pos] = editor.innerHTML; |
| 545 | + } |
| 546 | + this.pos --; |
| 547 | + if(typeof(this.history[this.pos])=='undefined') this.pos++; //in gecko it was pos ++ with a space. necessary? |
| 548 | + editor.innerHTML = this.history[this.pos]; |
| 549 | + if(gecko && editor.innerHTML.indexOf(cc)>-1) editor.innerHTML+=cc; |
| 550 | + WikiEditorEngine.findString(); |
| 551 | + }, |
| 552 | + |
| 553 | + redo : function() { |
| 554 | + // editor = WikiEditorEngine.getEditor(); |
| 555 | + this.pos++; |
| 556 | + if(typeof(this.history[this.pos])=='undefined') this.pos--; |
| 557 | + editor.innerHTML = this.history[this.pos]; |
| 558 | + WikiEditorEngine.findString(); |
| 559 | + }, |
| 560 | + |
| 561 | + next : function() { // get next vector position and clean old ones |
| 562 | + if(this.pos>20) this.history[this.pos-21] = undefined; |
| 563 | + return ++this.pos; |
| 564 | + } |
| 565 | + } |
| 566 | + |
| 567 | +}//end WikiEditorEngine obj |
| 568 | + |
| 569 | +Language={}; |
| 570 | +if($.browser.msie){ |
| 571 | + window.attachEvent('onload', function() { WikiEditorEngine.initialize('new');}); |
| 572 | +} |
| 573 | +else if(!$.browser.msie){ |
| 574 | + window.addEventListener('load', function() { WikiEditorEngine.initialize('new'); }, true); |
| 575 | +} |
Property changes on: trunk/extensions/UsabilityInitiative/js/plugins/codepress/engines/engine.js |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 576 | + native |