[ Index ] |
PHP Cross Reference of MyBB |
[Summary view] [Print] [Text view]
1 /* 2 * CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/ 3 * 4 * Copyright (C) 2007 Fernando M.A.d.S. <fermads@gmail.com> 5 * 6 * Developers: 7 * Fernando M.A.d.S. <fermads@gmail.com> 8 * Michael Hurni <michael.hurni@gmail.com> 9 * Contributors: 10 * Martin D. Kirk 11 * 12 * This program is free software; you can redistribute it and/or modify it under the terms of the 13 * GNU Lesser General Public License as published by the Free Software Foundation. 14 * 15 * Read the full licence: http://www.opensource.org/licenses/lgpl-license.php 16 */ 17 18 CodePress = { 19 scrolling : false, 20 autocomplete : true, 21 22 // set initial vars and start sh 23 initialize : function() { 24 if(typeof(editor)=='undefined' && !arguments[0]) return; 25 body = document.getElementsByTagName('body')[0]; 26 body.innerHTML = body.innerHTML.replace(/\n/g,""); 27 chars = '|32|46|62|8|'; // charcodes that trigger syntax highlighting 28 cc = '\u2009'; // carret char 29 editor = document.getElementsByTagName('pre')[0]; 30 31 document.designMode = 'on'; 32 33 document.addEventListener('keypress', this.keyHandler, true); 34 window.addEventListener('scroll', function() { if(!CodePress.scrolling) CodePress.syntaxHighlight('scroll') }, false); 35 completeChars = this.getCompleteChars(); 36 completeEndingChars = this.getCompleteEndingChars(); 37 }, 38 39 // treat key bindings 40 keyHandler : function(evt) { 41 keyCode = evt.keyCode; 42 charCode = evt.charCode; 43 fromChar = String.fromCharCode(charCode); 44 45 if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo) 46 CodePress.shortcuts(charCode?charCode:keyCode); 47 } 48 else if( (completeEndingChars.indexOf('|'+fromChar+'|')!= -1 || completeChars.indexOf('|'+fromChar+'|')!=-1) && CodePress.autocomplete) { // auto complete 49 if(!CodePress.completeEnding(fromChar)) 50 CodePress.complete(fromChar); 51 } 52 else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting 53 top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100); 54 } 55 else if(keyCode==9 || evt.tabKey) { // snippets activation (tab) 56 CodePress.snippets(evt); 57 } 58 else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed 59 CodePress.actions.history[CodePress.actions.next()] = editor.innerHTML; 60 } 61 else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo 62 (charCode==121||evt.shiftKey) ? CodePress.actions.redo() : CodePress.actions.undo(); 63 evt.preventDefault(); 64 } 65 else if(charCode==118 && evt.ctrlKey) { // handle paste 66 top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100); 67 } 68 else if(charCode==99 && evt.ctrlKey) { // handle cut 69 //alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF')); 70 } 71 72 }, 73 74 // put cursor back to its original position after every parsing 75 findString : function() { 76 if(self.find(cc)) 77 window.getSelection().getRangeAt(0).deleteContents(); 78 }, 79 80 // split big files, highlighting parts of it 81 split : function(code,flag) { 82 if(flag=='scroll') { 83 this.scrolling = true; 84 return code; 85 } 86 else { 87 this.scrolling = false; 88 mid = code.indexOf(cc); 89 if(mid-2000<0) {ini=0;end=4000;} 90 else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;} 91 else {ini=mid-2000;end=mid+2000;} 92 code = code.substring(ini,end); 93 return code; 94 } 95 }, 96 97 getEditor : function() { 98 if(!document.getElementsByTagName('pre')[0]) { 99 body = document.getElementsByTagName('body')[0]; 100 if(!body.innerHTML) return body; 101 if(body.innerHTML=="<br>") body.innerHTML = "<pre> </pre>"; 102 else body.innerHTML = "<pre>"+body.innerHTML+"</pre>"; 103 } 104 return document.getElementsByTagName('pre')[0]; 105 }, 106 107 // syntax highlighting parser 108 syntaxHighlight : function(flag) { 109 //if(document.designMode=='off') document.designMode='on' 110 if(flag != 'init') { window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));} 111 editor = CodePress.getEditor(); 112 o = editor.innerHTML; 113 o = o.replace(/<br>/g,'\n'); 114 o = o.replace(/<.*?>/g,''); 115 x = z = this.split(o,flag); 116 x = x.replace(/\n/g,'<br>'); 117 118 if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]); 119 120 for(i=0;i<Language.syntax.length;i++) 121 x = x.replace(Language.syntax[i].input,Language.syntax[i].output); 122 123 editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.split(z).join(x); 124 if(flag!='init') this.findString(); 125 }, 126 127 getLastWord : function() { 128 var rangeAndCaret = CodePress.getRangeAndCaret(); 129 words = rangeAndCaret[0].substring(rangeAndCaret[1]-40,rangeAndCaret[1]); 130 words = words.replace(/[\s\n\r\);\W]/g,'\n').split('\n'); 131 return words[words.length-1].replace(/[\W]/gi,'').toLowerCase(); 132 }, 133 134 snippets : function(evt) { 135 var snippets = Language.snippets; 136 var trigger = this.getLastWord(); 137 for (var i=0; i<snippets.length; i++) { 138 if(snippets[i].input == trigger) { 139 var content = snippets[i].output.replace(/</g,'<'); 140 content = content.replace(/>/g,'>'); 141 if(content.indexOf('$0')<0) content += cc; 142 else content = content.replace(/\$0/,cc); 143 content = content.replace(/\n/g,'<br>'); 144 var pattern = new RegExp(trigger+cc,'gi'); 145 evt.preventDefault(); // prevent the tab key from being added 146 this.syntaxHighlight('snippets',pattern,content); 147 } 148 } 149 }, 150 151 readOnly : function() { 152 document.designMode = (arguments[0]) ? 'off' : 'on'; 153 }, 154 155 complete : function(trigger) { 156 window.getSelection().getRangeAt(0).deleteContents(); 157 var complete = Language.complete; 158 for (var i=0; i<complete.length; i++) { 159 if(complete[i].input == trigger) { 160 var pattern = new RegExp('\\'+trigger+cc); 161 var content = complete[i].output.replace(/\$0/g,cc); 162 parent.setTimeout(function () { CodePress.syntaxHighlight('complete',pattern,content)},0); // wait for char to appear on screen 163 } 164 } 165 }, 166 167 getCompleteChars : function() { 168 var cChars = ''; 169 for(var i=0;i<Language.complete.length;i++) 170 cChars += '|'+Language.complete[i].input; 171 return cChars+'|'; 172 }, 173 174 getCompleteEndingChars : function() { 175 var cChars = ''; 176 for(var i=0;i<Language.complete.length;i++) 177 cChars += '|'+Language.complete[i].output.charAt(Language.complete[i].output.length-1); 178 return cChars+'|'; 179 }, 180 181 completeEnding : function(trigger) { 182 var range = window.getSelection().getRangeAt(0); 183 try { 184 range.setEnd(range.endContainer, range.endOffset+1) 185 } 186 catch(e) { 187 return false; 188 } 189 var next_character = range.toString() 190 range.setEnd(range.endContainer, range.endOffset-1) 191 if(next_character != trigger) return false; 192 else { 193 range.setEnd(range.endContainer, range.endOffset+1) 194 range.deleteContents(); 195 return true; 196 } 197 }, 198 199 shortcuts : function() { 200 var cCode = arguments[0]; 201 if(cCode==13) cCode = '[enter]'; 202 else if(cCode==32) cCode = '[space]'; 203 else cCode = '['+String.fromCharCode(charCode).toLowerCase()+']'; 204 for(var i=0;i<Language.shortcuts.length;i++) 205 if(Language.shortcuts[i].input == cCode) 206 this.insertCode(Language.shortcuts[i].output,false); 207 }, 208 209 getRangeAndCaret : function() { 210 var range = window.getSelection().getRangeAt(0); 211 var range2 = range.cloneRange(); 212 var node = range.endContainer; 213 var caret = range.endOffset; 214 range2.selectNode(node); 215 return [range2.toString(),caret]; 216 }, 217 218 insertCode : function(code,replaceCursorBefore) { 219 var range = window.getSelection().getRangeAt(0); 220 var node = window.document.createTextNode(code); 221 var selct = window.getSelection(); 222 var range2 = range.cloneRange(); 223 // Insert text at cursor position 224 selct.removeAllRanges(); 225 range.deleteContents(); 226 range.insertNode(node); 227 // Move the cursor to the end of text 228 range2.selectNode(node); 229 range2.collapse(replaceCursorBefore); 230 selct.removeAllRanges(); 231 selct.addRange(range2); 232 }, 233 234 // get code from editor 235 getCode : function() { 236 if(!document.getElementsByTagName('pre')[0] || editor.innerHTML == '') 237 editor = CodePress.getEditor(); 238 var code = editor.innerHTML; 239 code = code.replace(/<br>/g,'\n'); 240 code = code.replace(/\u2009/g,''); 241 code = code.replace(/<.*?>/g,''); 242 code = code.replace(/</g,'<'); 243 code = code.replace(/>/g,'>'); 244 code = code.replace(/&/gi,'&'); 245 return code; 246 }, 247 248 // put code inside editor 249 setCode : function() { 250 var code = arguments[0]; 251 code = code.replace(/\u2009/gi,''); 252 code = code.replace(/&/gi,'&'); 253 code = code.replace(/</g,'<'); 254 code = code.replace(/>/g,'>'); 255 editor.innerHTML = code; 256 if (code == '') 257 document.getElementsByTagName('body')[0].innerHTML = ''; 258 }, 259 260 // undo and redo methods 261 actions : { 262 pos : -1, // actual history position 263 history : [], // history vector 264 265 undo : function() { 266 editor = CodePress.getEditor(); 267 if(editor.innerHTML.indexOf(cc)==-1){ 268 if(editor.innerHTML != " ") 269 window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc)); 270 this.history[this.pos] = editor.innerHTML; 271 } 272 this.pos --; 273 if(typeof(this.history[this.pos])=='undefined') this.pos ++; 274 editor.innerHTML = this.history[this.pos]; 275 if(editor.innerHTML.indexOf(cc)>-1) editor.innerHTML+=cc; 276 CodePress.findString(); 277 }, 278 279 redo : function() { 280 // editor = CodePress.getEditor(); 281 this.pos++; 282 if(typeof(this.history[this.pos])=='undefined') this.pos--; 283 editor.innerHTML = this.history[this.pos]; 284 CodePress.findString(); 285 }, 286 287 next : function() { // get next vector position and clean old ones 288 if(this.pos>20) this.history[this.pos-21] = undefined; 289 return ++this.pos; 290 } 291 } 292 } 293 294 Language={}; 295 window.addEventListener('load', function() { CodePress.initialize('new'); }, true);
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Oct 8 19:19:50 2013 | Cross-referenced by PHPXref 0.7.1 |