| 1 |
458
|
doc
|
/*
|
| 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 |
|
|
* Contributors :
|
| 7 |
|
|
*
|
| 8 |
|
|
* Michael Hurni <michael.hurni@gmail.com>
|
| 9 |
|
|
*
|
| 10 |
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
| 11 |
|
|
* GNU Lesser General Public License as published by the Free Software Foundation.
|
| 12 |
|
|
*
|
| 13 |
|
|
* Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
|
| 14 |
|
|
*/
|
| 15 |
|
|
|
| 16 |
|
|
|
| 17 |
|
|
CodePress = {
|
| 18 |
|
|
scrolling : false,
|
| 19 |
|
|
autocomplete : true,
|
| 20 |
|
|
|
| 21 |
|
|
// set initial vars and start sh
|
| 22 |
|
|
initialize : function() {
|
| 23 |
|
|
if(typeof(editor)=='undefined' && !arguments[0]) return;
|
| 24 |
|
|
chars = '|32|46|62|'; // charcodes that trigger syntax highlighting
|
| 25 |
|
|
cc = '\u2009'; // control char
|
| 26 |
|
|
editor = document.getElementsByTagName('body')[0];
|
| 27 |
|
|
document.designMode = 'on';
|
| 28 |
|
|
document.addEventListener('keypress', this.keyHandler, true);
|
| 29 |
|
|
window.addEventListener('scroll', function() { if(!CodePress.scrolling) CodePress.syntaxHighlight('scroll') }, false);
|
| 30 |
|
|
completeChars = this.getCompleteChars();
|
| 31 |
|
|
// CodePress.syntaxHighlight('init');
|
| 32 |
|
|
},
|
| 33 |
|
|
|
| 34 |
|
|
// treat key bindings
|
| 35 |
|
|
keyHandler : function(evt) {
|
| 36 |
|
|
keyCode = evt.keyCode;
|
| 37 |
|
|
charCode = evt.charCode;
|
| 38 |
|
|
|
| 39 |
|
|
if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo)
|
| 40 |
|
|
CodePress.shortcuts(charCode?charCode:keyCode);
|
| 41 |
|
|
}
|
| 42 |
|
|
else if(completeChars.indexOf('|'+String.fromCharCode(charCode)+'|')!=-1 && CodePress.autocomplete) { // auto complete
|
| 43 |
|
|
CodePress.complete(String.fromCharCode(charCode));
|
| 44 |
|
|
}
|
| 45 |
|
|
else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting
|
| 46 |
|
|
CodePress.syntaxHighlight('generic');
|
| 47 |
|
|
}
|
| 48 |
|
|
else if(keyCode==9 || evt.tabKey) { // snippets activation (tab)
|
| 49 |
|
|
CodePress.snippets(evt);
|
| 50 |
|
|
}
|
| 51 |
|
|
else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed
|
| 52 |
|
|
CodePress.actions.history[CodePress.actions.next()] = editor.innerHTML;
|
| 53 |
|
|
}
|
| 54 |
|
|
else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo
|
| 55 |
|
|
(charCode==121||evt.shiftKey) ? CodePress.actions.redo() : CodePress.actions.undo();
|
| 56 |
|
|
evt.preventDefault();
|
| 57 |
|
|
}
|
| 58 |
|
|
else if(keyCode==86 && evt.ctrlKey) { // paste
|
| 59 |
|
|
// TODO: pasted text should be parsed and highlighted
|
| 60 |
|
|
}
|
| 61 |
|
|
},
|
| 62 |
|
|
|
| 63 |
|
|
// put cursor back to its original position after every parsing
|
| 64 |
|
|
findString : function() {
|
| 65 |
|
|
if(self.find(cc))
|
| 66 |
|
|
window.getSelection().getRangeAt(0).deleteContents();
|
| 67 |
|
|
},
|
| 68 |
|
|
|
| 69 |
|
|
// split big files, highlighting parts of it
|
| 70 |
|
|
split : function(code,flag) {
|
| 71 |
|
|
if(flag=='scroll') {
|
| 72 |
|
|
this.scrolling = true;
|
| 73 |
|
|
return code;
|
| 74 |
|
|
}
|
| 75 |
|
|
else {
|
| 76 |
|
|
this.scrolling = false;
|
| 77 |
|
|
mid = code.indexOf(cc);
|
| 78 |
|
|
if(mid-2000<0) {ini=0;end=4000;}
|
| 79 |
|
|
else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;}
|
| 80 |
|
|
else {ini=mid-2000;end=mid+2000;}
|
| 81 |
|
|
code = code.substring(ini,end);
|
| 82 |
|
|
return code;
|
| 83 |
|
|
}
|
| 84 |
|
|
},
|
| 85 |
|
|
|
| 86 |
|
|
// syntax highlighting parser
|
| 87 |
|
|
syntaxHighlight : function(flag) {
|
| 88 |
|
|
//if(document.designMode=='off') document.designMode='on'
|
| 89 |
|
|
if(flag!='init') window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));
|
| 90 |
|
|
|
| 91 |
|
|
o = editor.innerHTML;
|
| 92 |
|
|
o = o.replace(/<br>/g,'\n');
|
| 93 |
|
|
o = o.replace(/<.*?>/g,'');
|
| 94 |
|
|
x = z = this.split(o,flag);
|
| 95 |
|
|
x = x.replace(/\n/g,'<br>');
|
| 96 |
|
|
|
| 97 |
|
|
if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]);
|
| 98 |
|
|
|
| 99 |
|
|
for(i=0;i<Language.syntax.length;i++)
|
| 100 |
|
|
x = x.replace(Language.syntax[i].input,Language.syntax[i].output);
|
| 101 |
|
|
|
| 102 |
|
|
editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.split(z).join(x);
|
| 103 |
|
|
if(flag!='init') this.findString();
|
| 104 |
|
|
},
|
| 105 |
|
|
|
| 106 |
|
|
getLastWord : function() {
|
| 107 |
|
|
var rangeAndCaret = CodePress.getRangeAndCaret();
|
| 108 |
|
|
var words = rangeAndCaret[0].substring(rangeAndCaret[1]-40,rangeAndCaret[1]).split(/[\s\r\n\);]/);
|
| 109 |
|
|
return words[words.length-1].replace(/_/g,'');
|
| 110 |
|
|
},
|
| 111 |
|
|
|
| 112 |
|
|
snippets : function(evt) {
|
| 113 |
|
|
var snippets = Language.snippets;
|
| 114 |
|
|
var trigger = this.getLastWord();
|
| 115 |
|
|
for (var i=0; i<snippets.length; i++) {
|
| 116 |
|
|
if(snippets[i].input == trigger) {
|
| 117 |
|
|
var content = snippets[i].output.replace(/</g,'<');
|
| 118 |
|
|
content = content.replace(/>/g,'>');
|
| 119 |
|
|
if(content.indexOf('$0')<0) content += cc;
|
| 120 |
|
|
else content = content.replace(/\$0/,cc);
|
| 121 |
|
|
content = content.replace(/\n/g,'<br>');
|
| 122 |
|
|
var pattern = new RegExp(trigger+cc,'g');
|
| 123 |
|
|
evt.preventDefault(); // prevent the tab key from being added
|
| 124 |
|
|
this.syntaxHighlight('snippets',pattern,content);
|
| 125 |
|
|
}
|
| 126 |
|
|
}
|
| 127 |
|
|
},
|
| 128 |
|
|
|
| 129 |
|
|
readOnly : function() {
|
| 130 |
|
|
document.designMode = (arguments[0]) ? 'off' : 'on';
|
| 131 |
|
|
},
|
| 132 |
|
|
|
| 133 |
|
|
complete : function(trigger) {
|
| 134 |
|
|
window.getSelection().getRangeAt(0).deleteContents();
|
| 135 |
|
|
var complete = Language.complete;
|
| 136 |
|
|
for (var i=0; i<complete.length; i++) {
|
| 137 |
|
|
if(complete[i].input == trigger) {
|
| 138 |
|
|
var pattern = new RegExp('\\'+trigger+cc);
|
| 139 |
|
|
var content = complete[i].output.replace(/\$0/g,cc);
|
| 140 |
|
|
parent.setTimeout(function () { CodePress.syntaxHighlight('complete',pattern,content)},0); // wait for char to appear on screen
|
| 141 |
|
|
}
|
| 142 |
|
|
}
|
| 143 |
|
|
},
|
| 144 |
|
|
|
| 145 |
|
|
getCompleteChars : function() {
|
| 146 |
|
|
var cChars = '';
|
| 147 |
|
|
for(var i=0;i<Language.complete.length;i++)
|
| 148 |
|
|
cChars += '|'+Language.complete[i].input;
|
| 149 |
|
|
return cChars+'|';
|
| 150 |
|
|
},
|
| 151 |
|
|
|
| 152 |
|
|
shortcuts : function() {
|
| 153 |
|
|
var cCode = arguments[0];
|
| 154 |
|
|
if(cCode==13) cCode = '[enter]';
|
| 155 |
|
|
else if(cCode==32) cCode = '[space]';
|
| 156 |
|
|
else cCode = '['+String.fromCharCode(charCode).toLowerCase()+']';
|
| 157 |
|
|
for(var i=0;i<Language.shortcuts.length;i++)
|
| 158 |
|
|
if(Language.shortcuts[i].input == cCode)
|
| 159 |
|
|
this.insertCode(Language.shortcuts[i].output,false);
|
| 160 |
|
|
},
|
| 161 |
|
|
|
| 162 |
|
|
getRangeAndCaret : function() {
|
| 163 |
|
|
var range = window.getSelection().getRangeAt(0);
|
| 164 |
|
|
var range2 = range.cloneRange();
|
| 165 |
|
|
var node = range.endContainer;
|
| 166 |
|
|
var caret = range.endOffset;
|
| 167 |
|
|
range2.selectNode(node);
|
| 168 |
|
|
return [range2.toString(),caret];
|
| 169 |
|
|
},
|
| 170 |
|
|
|
| 171 |
|
|
insertCode : function(code,replaceCursorBefore) {
|
| 172 |
|
|
var range = window.getSelection().getRangeAt(0);
|
| 173 |
|
|
var node = window.document.createTextNode(code);
|
| 174 |
|
|
var selct = window.getSelection();
|
| 175 |
|
|
var range2 = range.cloneRange();
|
| 176 |
|
|
// Insert text at cursor position
|
| 177 |
|
|
selct.removeAllRanges();
|
| 178 |
|
|
range.deleteContents();
|
| 179 |
|
|
range.insertNode(node);
|
| 180 |
|
|
// Move the cursor to the end of text
|
| 181 |
|
|
range2.selectNode(node);
|
| 182 |
|
|
range2.collapse(replaceCursorBefore);
|
| 183 |
|
|
selct.removeAllRanges();
|
| 184 |
|
|
selct.addRange(range2);
|
| 185 |
|
|
},
|
| 186 |
|
|
|
| 187 |
|
|
// get code from editor
|
| 188 |
|
|
getCode : function() {
|
| 189 |
|
|
var code = editor.innerHTML;
|
| 190 |
|
|
code = code.replace(/<br>/g,'\n');
|
| 191 |
|
|
code = code.replace(/\u2009/g,'');
|
| 192 |
|
|
code = code.replace(/<.*?>/g,'');
|
| 193 |
|
|
code = code.replace(/</g,'<');
|
| 194 |
|
|
code = code.replace(/>/g,'>');
|
| 195 |
|
|
code = code.replace(/&/gi,'&');
|
| 196 |
|
|
return code;
|
| 197 |
|
|
},
|
| 198 |
|
|
|
| 199 |
|
|
// put code inside editor
|
| 200 |
|
|
setCode : function() {
|
| 201 |
|
|
var code = arguments[0];
|
| 202 |
|
|
code = code.replace(/\u2009/gi,'');
|
| 203 |
|
|
code = code.replace(/&/gi,'&');
|
| 204 |
|
|
code = code.replace(/</g,'<');
|
| 205 |
|
|
code = code.replace(/>/g,'>');
|
| 206 |
|
|
editor.innerHTML = code;
|
| 207 |
|
|
},
|
| 208 |
|
|
|
| 209 |
|
|
// undo and redo methods
|
| 210 |
|
|
actions : {
|
| 211 |
|
|
pos : -1, // actual history position
|
| 212 |
|
|
history : [], // history vector
|
| 213 |
|
|
|
| 214 |
|
|
undo : function() {
|
| 215 |
|
|
if(editor.innerHTML.indexOf(cc)==-1){
|
| 216 |
|
|
window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));
|
| 217 |
|
|
this.history[this.pos] = editor.innerHTML;
|
| 218 |
|
|
}
|
| 219 |
|
|
this.pos--;
|
| 220 |
|
|
if(typeof(this.history[this.pos])=='undefined') this.pos++;
|
| 221 |
|
|
editor.innerHTML = this.history[this.pos];
|
| 222 |
|
|
CodePress.findString();
|
| 223 |
|
|
},
|
| 224 |
|
|
|
| 225 |
|
|
redo : function() {
|
| 226 |
|
|
this.pos++;
|
| 227 |
|
|
if(typeof(this.history[this.pos])=='undefined') this.pos--;
|
| 228 |
|
|
editor.innerHTML = this.history[this.pos];
|
| 229 |
|
|
CodePress.findString();
|
| 230 |
|
|
},
|
| 231 |
|
|
|
| 232 |
|
|
next : function() { // get next vector position and clean old ones
|
| 233 |
|
|
if(this.pos>20) this.history[this.pos-21] = undefined;
|
| 234 |
|
|
return ++this.pos;
|
| 235 |
|
|
}
|
| 236 |
|
|
}
|
| 237 |
|
|
}
|
| 238 |
|
|
|
| 239 |
|
|
Language={};
|
| 240 |
|
|
window.addEventListener('load', function() { CodePress.initialize('new'); }, true);
|