809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
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 |
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(); |
|
998
by wagrant
editor: Add a hook to CodePress to catch Ctrl+S. Use that to trigger |
35 |
document.changehandlers = Array(); |
36 |
document.savehandlers = Array(); |
|
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
37 |
},
|
38 |
||
39 |
// treat key bindings
|
|
40 |
keyHandler : function(evt) { |
|
41 |
keyCode = evt.keyCode; |
|
42 |
charCode = evt.charCode; |
|
43 |
fromChar = String.fromCharCode(charCode); |
|
897
by wagrant
CodePress (Gecko engine): Whitelist some movement keys so they don't |
44 |
// Arrow keys, Pg{Up,Dn}, Home, End won't mutate the content.
|
998
by wagrant
editor: Add a hook to CodePress to catch Ctrl+S. Use that to trigger |
45 |
if ([33, 34, 35, 36, 37, 38, 39, 40].indexOf(keyCode) == -1 && (charCode!=115 || !evt.ctrlKey)) |
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
46 |
{
|
897
by wagrant
CodePress (Gecko engine): Whitelist some movement keys so they don't |
47 |
for (var handler in this.changehandlers) |
48 |
{
|
|
49 |
document.changehandlers[handler](); |
|
50 |
}
|
|
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
51 |
}
|
52 |
||
1015
by wagrant
CodePress (Gecko engine): Fix a stupid logic error I made before, which broke |
53 |
if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo) |
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
54 |
CodePress.shortcuts(charCode?charCode:keyCode); |
55 |
}
|
|
56 |
else if( (completeEndingChars.indexOf('|'+fromChar+'|')!= -1 || completeChars.indexOf('|'+fromChar+'|')!=-1) && CodePress.autocomplete) { // auto complete |
|
57 |
if(!CodePress.completeEnding(fromChar)) |
|
58 |
CodePress.complete(fromChar); |
|
59 |
}
|
|
60 |
else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting |
|
61 |
top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100); |
|
62 |
}
|
|
900
by wagrant
CodePress (Gecko engine): Disable snippets, as we now use the tab key |
63 |
else if(keyCode==9 || evt.tabKey) { // Tabbing! Was snippets, but they're bad. |
64 |
// TODO: Allow line indentation
|
|
65 |
evt.preventDefault(); |
|
66 |
CodePress.insertCode(' '); |
|
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
67 |
}
|
68 |
else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed |
|
69 |
CodePress.actions.history[CodePress.actions.next()] = editor.innerHTML; |
|
70 |
}
|
|
71 |
else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo |
|
72 |
(charCode==121||evt.shiftKey) ? CodePress.actions.redo() : CodePress.actions.undo(); |
|
73 |
evt.preventDefault(); |
|
74 |
}
|
|
75 |
else if(charCode==118 && evt.ctrlKey) { // handle paste |
|
76 |
top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100); |
|
77 |
}
|
|
78 |
else if(charCode==99 && evt.ctrlKey) { // handle cut |
|
79 |
//alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF'));
|
|
80 |
}
|
|
998
by wagrant
editor: Add a hook to CodePress to catch Ctrl+S. Use that to trigger |
81 |
else if(charCode==115 && evt.ctrlKey) { // handle save |
82 |
evt.preventDefault(); |
|
83 |
for (var handler in this.savehandlers) |
|
84 |
{
|
|
85 |
document.savehandlers[handler](); |
|
86 |
}
|
|
87 |
}
|
|
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
88 |
},
|
89 |
||
90 |
// put cursor back to its original position after every parsing
|
|
91 |
findString : function() { |
|
92 |
if(self.find(cc)) |
|
93 |
window.getSelection().getRangeAt(0).deleteContents(); |
|
94 |
},
|
|
95 |
||
96 |
// split big files, highlighting parts of it
|
|
97 |
split : function(code,flag) { |
|
98 |
if(flag=='scroll') { |
|
99 |
this.scrolling = true; |
|
100 |
return code; |
|
101 |
}
|
|
102 |
else { |
|
103 |
this.scrolling = false; |
|
104 |
mid = code.indexOf(cc); |
|
105 |
if(mid-2000<0) {ini=0;end=4000;} |
|
106 |
else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;} |
|
107 |
else {ini=mid-2000;end=mid+2000;} |
|
108 |
code = code.substring(ini,end); |
|
109 |
return code; |
|
110 |
}
|
|
111 |
},
|
|
112 |
||
113 |
getEditor : function() { |
|
114 |
if(!document.getElementsByTagName('pre')[0]) { |
|
115 |
body = document.getElementsByTagName('body')[0]; |
|
116 |
if(!body.innerHTML) return body; |
|
117 |
if(body.innerHTML=="<br>") body.innerHTML = "<pre> </pre>"; |
|
118 |
else body.innerHTML = "<pre>"+body.innerHTML+"</pre>"; |
|
119 |
}
|
|
1061
by wagrant
Make CodePress' Gecko engine merge any <pre>s in the editor widget if there is |
120 |
/* For some reason we'll sometimes get multiple <pre>s when
|
121 |
* something is pasted from within Firefox (particularly when
|
|
122 |
* there are blank lines). Here we check if multiple <p>s exist
|
|
123 |
* and merge any that do. */
|
|
124 |
var pres = document.getElementsByTagName('pre'); |
|
125 |
if(pres.length > 1) { |
|
126 |
tomerge = pres.length - 1; // As we kill them, this will change, so we store it here. |
|
127 |
for(i = 1; i <= tomerge; i++) { |
|
128 |
pres[0].innerHTML += "<br />" + pres[1].innerHTML; |
|
129 |
pres[1].parentNode.removeChild(pres[1]); |
|
130 |
}
|
|
131 |
}
|
|
132 |
return pres[0]; |
|
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
133 |
},
|
134 |
||
135 |
// syntax highlighting parser
|
|
136 |
syntaxHighlight : function(flag) { |
|
137 |
//if(document.designMode=='off') document.designMode='on'
|
|
138 |
if(flag != 'init') { window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));} |
|
139 |
editor = CodePress.getEditor(); |
|
140 |
o = editor.innerHTML; |
|
992
by wagrant
CodePress (Gecko engine): Replace all <br> elements with a linefeed, not just |
141 |
|
142 |
// The ? in the following two regexps makes them less hungry. Do not remove!
|
|
143 |
o = o.replace(/<br(\/?| .*?)>/g,'\n'); |
|
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
144 |
o = o.replace(/<.*?>/g,''); |
145 |
x = z = this.split(o,flag); |
|
146 |
x = x.replace(/\n/g,'<br>'); |
|
147 |
||
148 |
if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]); |
|
149 |
||
150 |
for(i=0;i<Language.syntax.length;i++) |
|
151 |
x = x.replace(Language.syntax[i].input,Language.syntax[i].output); |
|
152 |
||
153 |
editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.split(z).join(x); |
|
154 |
if(flag!='init') this.findString(); |
|
155 |
},
|
|
156 |
||
157 |
getLastWord : function() { |
|
158 |
var rangeAndCaret = CodePress.getRangeAndCaret(); |
|
159 |
words = rangeAndCaret[0].substring(rangeAndCaret[1]-40,rangeAndCaret[1]); |
|
160 |
words = words.replace(/[\s\n\r\);\W]/g,'\n').split('\n'); |
|
161 |
return words[words.length-1].replace(/[\W]/gi,'').toLowerCase(); |
|
162 |
},
|
|
163 |
||
164 |
snippets : function(evt) { |
|
165 |
var snippets = Language.snippets; |
|
166 |
var trigger = this.getLastWord(); |
|
167 |
for (var i=0; i<snippets.length; i++) { |
|
168 |
if(snippets[i].input == trigger) { |
|
169 |
var content = snippets[i].output.replace(/</g,'<'); |
|
170 |
content = content.replace(/>/g,'>'); |
|
171 |
if(content.indexOf('$0')<0) content += cc; |
|
172 |
else content = content.replace(/\$0/,cc); |
|
173 |
content = content.replace(/\n/g,'<br>'); |
|
174 |
var pattern = new RegExp(trigger+cc,'gi'); |
|
175 |
evt.preventDefault(); // prevent the tab key from being added |
|
176 |
this.syntaxHighlight('snippets',pattern,content); |
|
177 |
}
|
|
178 |
}
|
|
179 |
},
|
|
180 |
||
181 |
readOnly : function() { |
|
182 |
document.designMode = (arguments[0]) ? 'off' : 'on'; |
|
183 |
},
|
|
184 |
||
185 |
complete : function(trigger) { |
|
186 |
window.getSelection().getRangeAt(0).deleteContents(); |
|
187 |
var complete = Language.complete; |
|
188 |
for (var i=0; i<complete.length; i++) { |
|
189 |
if(complete[i].input == trigger) { |
|
190 |
var pattern = new RegExp('\\'+trigger+cc); |
|
191 |
var content = complete[i].output.replace(/\$0/g,cc); |
|
192 |
parent.setTimeout(function () { CodePress.syntaxHighlight('complete',pattern,content)},0); // wait for char to appear on screen |
|
193 |
}
|
|
194 |
}
|
|
195 |
},
|
|
196 |
||
197 |
getCompleteChars : function() { |
|
198 |
var cChars = ''; |
|
199 |
for(var i=0;i<Language.complete.length;i++) |
|
200 |
cChars += '|'+Language.complete[i].input; |
|
201 |
return cChars+'|'; |
|
202 |
},
|
|
203 |
||
204 |
getCompleteEndingChars : function() { |
|
205 |
var cChars = ''; |
|
206 |
for(var i=0;i<Language.complete.length;i++) |
|
207 |
cChars += '|'+Language.complete[i].output.charAt(Language.complete[i].output.length-1); |
|
208 |
return cChars+'|'; |
|
209 |
},
|
|
210 |
||
211 |
completeEnding : function(trigger) { |
|
212 |
var range = window.getSelection().getRangeAt(0); |
|
213 |
try { |
|
214 |
range.setEnd(range.endContainer, range.endOffset+1) |
|
215 |
}
|
|
216 |
catch(e) { |
|
217 |
return false; |
|
218 |
}
|
|
219 |
var next_character = range.toString() |
|
220 |
range.setEnd(range.endContainer, range.endOffset-1) |
|
221 |
if(next_character != trigger) return false; |
|
222 |
else { |
|
223 |
range.setEnd(range.endContainer, range.endOffset+1) |
|
224 |
range.deleteContents(); |
|
225 |
return true; |
|
226 |
}
|
|
227 |
},
|
|
228 |
||
229 |
shortcuts : function() { |
|
230 |
var cCode = arguments[0]; |
|
231 |
if(cCode==13) cCode = '[enter]'; |
|
232 |
else if(cCode==32) cCode = '[space]'; |
|
233 |
else cCode = '['+String.fromCharCode(charCode).toLowerCase()+']'; |
|
234 |
for(var i=0;i<Language.shortcuts.length;i++) |
|
235 |
if(Language.shortcuts[i].input == cCode) |
|
236 |
this.insertCode(Language.shortcuts[i].output,false); |
|
237 |
},
|
|
238 |
||
239 |
getRangeAndCaret : function() { |
|
240 |
var range = window.getSelection().getRangeAt(0); |
|
241 |
var range2 = range.cloneRange(); |
|
242 |
var node = range.endContainer; |
|
243 |
var caret = range.endOffset; |
|
244 |
range2.selectNode(node); |
|
245 |
return [range2.toString(),caret]; |
|
246 |
},
|
|
247 |
||
248 |
insertCode : function(code,replaceCursorBefore) { |
|
249 |
var range = window.getSelection().getRangeAt(0); |
|
250 |
var node = window.document.createTextNode(code); |
|
251 |
var selct = window.getSelection(); |
|
252 |
var range2 = range.cloneRange(); |
|
253 |
// Insert text at cursor position
|
|
254 |
selct.removeAllRanges(); |
|
255 |
range.deleteContents(); |
|
256 |
range.insertNode(node); |
|
257 |
// Move the cursor to the end of text
|
|
258 |
range2.selectNode(node); |
|
259 |
range2.collapse(replaceCursorBefore); |
|
260 |
selct.removeAllRanges(); |
|
261 |
selct.addRange(range2); |
|
262 |
},
|
|
263 |
||
264 |
// get code from editor
|
|
265 |
getCode : function() { |
|
266 |
if(!document.getElementsByTagName('pre')[0] || editor.innerHTML == '') |
|
267 |
editor = CodePress.getEditor(); |
|
268 |
var code = editor.innerHTML; |
|
269 |
code = code.replace(/<br>/g,'\n'); |
|
270 |
code = code.replace(/\u2009/g,''); |
|
271 |
code = code.replace(/<.*?>/g,''); |
|
272 |
code = code.replace(/</g,'<'); |
|
273 |
code = code.replace(/>/g,'>'); |
|
274 |
code = code.replace(/&/gi,'&'); |
|
275 |
return code; |
|
276 |
},
|
|
277 |
||
278 |
// put code inside editor
|
|
279 |
setCode : function() { |
|
280 |
var code = arguments[0]; |
|
281 |
code = code.replace(/\u2009/gi,''); |
|
282 |
code = code.replace(/&/gi,'&'); |
|
283 |
code = code.replace(/</g,'<'); |
|
284 |
code = code.replace(/>/g,'>'); |
|
285 |
editor.innerHTML = code; |
|
286 |
if (code == '') |
|
287 |
document.getElementsByTagName('body')[0].innerHTML = ''; |
|
288 |
},
|
|
289 |
||
290 |
addChangeHandler : function(handler) { |
|
291 |
document.changehandlers.push(handler); |
|
292 |
},
|
|
293 |
||
998
by wagrant
editor: Add a hook to CodePress to catch Ctrl+S. Use that to trigger |
294 |
addSaveHandler : function(handler) { |
295 |
document.savehandlers.push(handler); |
|
296 |
},
|
|
297 |
||
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
298 |
// undo and redo methods
|
299 |
actions : { |
|
300 |
pos : -1, // actual history position |
|
301 |
history : [], // history vector |
|
302 |
||
303 |
undo : function() { |
|
304 |
editor = CodePress.getEditor(); |
|
305 |
if(editor.innerHTML.indexOf(cc)==-1){ |
|
306 |
if(editor.innerHTML != " ") |
|
307 |
window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc)); |
|
308 |
this.history[this.pos] = editor.innerHTML; |
|
309 |
}
|
|
310 |
this.pos --; |
|
311 |
if(typeof(this.history[this.pos])=='undefined') this.pos ++; |
|
312 |
editor.innerHTML = this.history[this.pos]; |
|
313 |
if(editor.innerHTML.indexOf(cc)>-1) editor.innerHTML+=cc; |
|
314 |
CodePress.findString(); |
|
315 |
},
|
|
316 |
||
317 |
redo : function() { |
|
318 |
// editor = CodePress.getEditor();
|
|
319 |
this.pos++; |
|
320 |
if(typeof(this.history[this.pos])=='undefined') this.pos--; |
|
321 |
editor.innerHTML = this.history[this.pos]; |
|
322 |
CodePress.findString(); |
|
323 |
},
|
|
324 |
||
325 |
next : function() { // get next vector position and clean old ones |
|
326 |
if(this.pos>20) this.history[this.pos-21] = undefined; |
|
327 |
return ++this.pos; |
|
328 |
}
|
|
329 |
}
|
|
330 |
}
|
|
331 |
||
332 |
Language={}; |
|
333 |
window.addEventListener('load', function() { CodePress.initialize('new'); }, true); |