2
* CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/
4
* Copyright (C) 2007 Fernando M.A.d.S. <fermads@gmail.com>
7
* Fernando M.A.d.S. <fermads@gmail.com>
8
* Michael Hurni <michael.hurni@gmail.com>
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.
15
* Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
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
document.designMode = 'on';
31
document.addEventListener('keypress', this.keyHandler, true);
32
window.addEventListener('scroll', function() { if(!CodePress.scrolling) CodePress.syntaxHighlight('scroll') }, false);
33
completeChars = this.getCompleteChars();
34
completeEndingChars = this.getCompleteEndingChars();
38
keyHandler : function(evt) {
39
keyCode = evt.keyCode; // 42
40
charCode = evt.charCode; // 42
41
fromChar = String.fromCharCode(charCode); // *
43
if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo)
44
CodePress.shortcuts(charCode?charCode:keyCode);
46
else if( (completeEndingChars.indexOf('|'+fromChar+'|')!= -1 || completeChars.indexOf('|'+fromChar+'|')!=-1) && CodePress.autocomplete) { // auto complete
47
if(!CodePress.completeEnding(fromChar))
48
CodePress.complete(fromChar);
50
else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting
51
top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
53
else if(keyCode==9 || evt.tabKey) { // snippets activation (tab)
54
CodePress.snippets(evt);
56
else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed
57
CodePress.actions.history[CodePress.actions.next()] = editor.innerHTML;
59
else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo
60
(charCode==121||evt.shiftKey) ? CodePress.actions.redo() : CodePress.actions.undo();
63
else if(charCode==118 && evt.ctrlKey) { // handle paste
64
top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
66
else if(charCode==99 && evt.ctrlKey) { // handle cut
67
//alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF'));
72
// put cursor back to its original position after every parsing
73
findString : function() {
75
window.getSelection().getRangeAt(0).deleteContents();
78
// split big files, highlighting parts of it
79
split : function(code,flag) {
81
this.scrolling = true;
85
this.scrolling = false;
86
mid = code.indexOf(cc);
87
if(mid-2000<0) {ini=0;end=4000;}
88
else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;}
89
else {ini=mid-2000;end=mid+2000;}
90
code = code.substring(ini,end);
95
getEditor : function() {
96
if(!document.getElementsByTagName('pre')[0]) {
97
body = document.getElementsByTagName('body')[0];
98
if(!body.innerHTML) return body;
99
if(body.innerHTML=="<br>") body.innerHTML = "<pre> </pre>";
100
else body.innerHTML = "<pre>"+body.innerHTML+"</pre>";
102
return document.getElementsByTagName('pre')[0];
105
// syntax highlighting parser
106
syntaxHighlight : function(flag) {
107
//if(document.designMode=='off') document.designMode='on'
108
if(flag != 'init') { window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));}
109
editor = CodePress.getEditor();
110
o = editor.innerHTML;
111
o = o.replace(/<br>/g,'\n');
112
o = o.replace(/<.*?>/g,'');
113
x = z = this.split(o,flag);
114
x = x.replace(/\n/g,'<br>');
116
if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]);
118
for(i=0;i<Language.syntax.length;i++)
119
x = x.replace(Language.syntax[i].input,Language.syntax[i].output);
121
editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.split(z).join(x);
122
if(flag!='init') this.findString();
125
getLastWord : function() {
126
var rangeAndCaret = CodePress.getRangeAndCaret();
127
words = rangeAndCaret[0].substring(rangeAndCaret[1]-40,rangeAndCaret[1]);
128
words = words.replace(/[\s\n\r\);\W]/g,'\n').split('\n');
129
return words[words.length-1].replace(/[\W]/gi,'').toLowerCase();
132
snippets : function(evt) {
133
var snippets = Language.snippets;
134
var trigger = this.getLastWord();
135
var range = window.getSelection().getRangeAt(0);
137
// for the single line
138
range.setEnd(range.startContainer, range.startOffset);
140
var ele = document.createTextNode('\t');
141
range.insertNode(ele);
142
range.setStartAfter(ele);
144
evt.preventDefault();
147
readOnly : function() {
148
document.designMode = (arguments[0]) ? 'off' : 'on';
151
complete : function(trigger) {
152
window.getSelection().getRangeAt(0).deleteContents();
153
var complete = Language.complete;
154
for (var i=0; i<complete.length; i++) {
155
if(complete[i].input == trigger) {
156
var pattern = new RegExp('\\'+trigger+cc);
157
var content = complete[i].output.replace(/\$0/g,cc);
158
parent.setTimeout(function () { CodePress.syntaxHighlight('complete',pattern,content)},0); // wait for char to appear on screen
163
getCompleteChars : function() {
165
for(var i=0;i<Language.complete.length;i++)
166
cChars += '|'+Language.complete[i].input;
170
getCompleteEndingChars : function() {
172
for(var i=0;i<Language.complete.length;i++)
173
cChars += '|'+Language.complete[i].output.charAt(Language.complete[i].output.length-1);
177
completeEnding : function(trigger) {
178
var range = window.getSelection().getRangeAt(0);
180
range.setEnd(range.endContainer, range.endOffset+1)
185
var next_character = range.toString()
186
range.setEnd(range.endContainer, range.endOffset-1)
187
if(next_character != trigger) return false;
189
range.setEnd(range.endContainer, range.endOffset+1)
190
range.deleteContents();
195
shortcuts : function() {
196
var cCode = arguments[0];
197
if(cCode==13) cCode = '[enter]';
198
else if(cCode==32) cCode = '[space]';
199
else cCode = '['+String.fromCharCode(charCode).toLowerCase()+']';
200
for(var i=0;i<Language.shortcuts.length;i++)
201
if(Language.shortcuts[i].input == cCode)
202
this.insertCode(Language.shortcuts[i].output,false);
205
getRangeAndCaret : function() {
206
var range = window.getSelection().getRangeAt(0);
207
var range2 = range.cloneRange();
208
var node = range.endContainer;
209
var caret = range.endOffset;
210
range2.selectNode(node);
211
return [range2.toString(),caret];
214
insertCode : function(code,replaceCursorBefore) {
215
var range = window.getSelection().getRangeAt(0);
216
var node = window.document.createTextNode(code);
217
var selct = window.getSelection();
218
var range2 = range.cloneRange();
219
// Insert text at cursor position
220
selct.removeAllRanges();
221
range.deleteContents();
222
range.insertNode(node);
223
// Move the cursor to the end of text
224
range2.selectNode(node);
225
range2.collapse(replaceCursorBefore);
226
selct.removeAllRanges();
227
selct.addRange(range2);
230
// get code from editor
231
getCode : function() {
232
if(!document.getElementsByTagName('pre')[0] || editor.innerHTML == '')
233
editor = CodePress.getEditor();
234
var code = editor.innerHTML;
235
code = code.replace(/<br>/g,'\n');
236
code = code.replace(/\u2009/g,'');
237
code = code.replace(/<.*?>/g,'');
238
code = code.replace(/</g,'<');
239
code = code.replace(/>/g,'>');
240
code = code.replace(/&/gi,'&');
244
// put code inside editor
245
setCode : function() {
246
var code = arguments[0];
247
code = code.replace(/\u2009/gi,'');
248
code = code.replace(/&/gi,'&');
249
code = code.replace(/</g,'<');
250
code = code.replace(/>/g,'>');
251
editor.innerHTML = code;
253
document.getElementsByTagName('body')[0].innerHTML = '';
256
// undo and redo methods
258
pos : -1, // actual history position
259
history : [], // history vector
262
editor = CodePress.getEditor();
263
if(editor.innerHTML.indexOf(cc)==-1){
264
if(editor.innerHTML != " ")
265
window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));
266
this.history[this.pos] = editor.innerHTML;
269
if(typeof(this.history[this.pos])=='undefined') this.pos ++;
270
editor.innerHTML = this.history[this.pos];
271
if(editor.innerHTML.indexOf(cc)>-1) editor.innerHTML+=cc;
272
CodePress.findString();
276
// editor = CodePress.getEditor();
278
if(typeof(this.history[this.pos])=='undefined') this.pos--;
279
editor.innerHTML = this.history[this.pos];
280
CodePress.findString();
283
next : function() { // get next vector position and clean old ones
284
if(this.pos>20) this.history[this.pos-21] = undefined;
291
window.addEventListener('load', function() { CodePress.initialize('new'); }, true);
b'\\ No newline at end of file'