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) 2006 Fernando M.A.d.S. <fermads@gmail.com>
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
7 |
* GNU Lesser General Public License as published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
|
|
10 |
*/
|
|
11 |
||
12 |
CodePress = function(obj) { |
|
13 |
var self = document.createElement('iframe'); |
|
14 |
self.textarea = obj; |
|
15 |
self.textarea.disabled = true; |
|
16 |
self.textarea.style.overflow = 'hidden'; |
|
17 |
self.style.height = self.textarea.clientHeight +'px'; |
|
18 |
self.style.width = self.textarea.clientWidth +'px'; |
|
19 |
self.textarea.style.overflow = 'auto'; |
|
20 |
self.style.border = '1px solid gray'; |
|
21 |
self.frameBorder = 0; // remove IE internal iframe border |
|
22 |
self.style.visibility = 'hidden'; |
|
23 |
self.style.position = 'absolute'; |
|
24 |
self.options = self.textarea.className; |
|
25 |
||
26 |
self.initialize = function() { |
|
27 |
self.editor = self.contentWindow.CodePress; |
|
28 |
self.editor.body = self.contentWindow.document.getElementsByTagName('body')[0]; |
|
29 |
self.editor.setCode(self.textarea.value); |
|
30 |
self.setOptions(); |
|
31 |
self.editor.syntaxHighlight('init'); |
|
32 |
self.textarea.style.display = 'none'; |
|
33 |
self.style.position = 'static'; |
|
34 |
self.style.visibility = 'visible'; |
|
35 |
self.style.display = 'inline'; |
|
36 |
}
|
|
37 |
||
38 |
// obj can by a textarea id or a string (code)
|
|
39 |
self.edit = function(obj,language) { |
|
40 |
if(obj) self.textarea.value = document.getElementById(obj) ? document.getElementById(obj).value : obj; |
|
41 |
if(!self.textarea.disabled) return; |
|
42 |
self.language = language ? language : self.getLanguage(); |
|
43 |
self.src = CodePress.path+'codepress.html?language='+self.language+'&ts='+(new Date).getTime(); |
|
44 |
if(self.attachEvent) self.attachEvent('onload',self.initialize); |
|
45 |
else self.addEventListener('load',self.initialize,false); |
|
46 |
}
|
|
47 |
||
48 |
self.getLanguage = function() { |
|
49 |
for (language in CodePress.languages) |
|
50 |
if(self.options.match('\\b'+language+'\\b')) |
|
51 |
return CodePress.languages[language] ? language : 'generic'; |
|
52 |
}
|
|
53 |
||
54 |
self.setOptions = function() { |
|
55 |
if(self.options.match('autocomplete-off')) self.toggleAutoComplete(); |
|
56 |
if(self.options.match('readonly-on')) self.toggleReadOnly(); |
|
57 |
if(self.options.match('linenumbers-off')) self.toggleLineNumbers(); |
|
58 |
}
|
|
59 |
||
60 |
self.getCode = function() { |
|
61 |
return self.textarea.disabled ? self.editor.getCode() : self.textarea.value; |
|
62 |
}
|
|
63 |
||
64 |
self.setCode = function(code) { |
|
65 |
self.textarea.disabled ? self.editor.setCode(code) : self.textarea.value = code; |
|
66 |
}
|
|
67 |
||
68 |
self.addChangeHandler = function(handler) { |
|
69 |
return self.editor.addChangeHandler ? self.editor.addChangeHandler(handler) : false; |
|
70 |
}
|
|
71 |
||
998
by wagrant
editor: Add a hook to CodePress to catch Ctrl+S. Use that to trigger |
72 |
self.addSaveHandler = function(handler) { |
73 |
return self.editor.addSaveHandler ? self.editor.addSaveHandler(handler) : false; |
|
74 |
}
|
|
75 |
||
809
by William Grant
Merge killall-editarea branch. We now use CodePress instead, which is |
76 |
self.toggleAutoComplete = function() { |
77 |
self.editor.autocomplete = (self.editor.autocomplete) ? false : true; |
|
78 |
}
|
|
79 |
||
80 |
self.toggleReadOnly = function() { |
|
81 |
self.textarea.readOnly = (self.textarea.readOnly) ? false : true; |
|
82 |
if(self.style.display != 'none') // prevent exception on FF + iframe with display:none |
|
83 |
self.editor.readOnly(self.textarea.readOnly ? true : false); |
|
84 |
}
|
|
85 |
||
86 |
self.toggleLineNumbers = function() { |
|
87 |
var cn = self.editor.body.className; |
|
88 |
self.editor.body.className = (cn==''||cn=='show-line-numbers') ? 'hide-line-numbers' : 'show-line-numbers'; |
|
89 |
}
|
|
90 |
||
91 |
self.toggleEditor = function() { |
|
92 |
if(self.textarea.disabled) { |
|
93 |
self.textarea.value = self.getCode(); |
|
94 |
self.textarea.disabled = false; |
|
95 |
self.style.display = 'none'; |
|
96 |
self.textarea.style.display = 'inline'; |
|
97 |
}
|
|
98 |
else { |
|
99 |
self.textarea.disabled = true; |
|
100 |
self.setCode(self.textarea.value); |
|
101 |
self.editor.syntaxHighlight('init'); |
|
102 |
self.style.display = 'inline'; |
|
103 |
self.textarea.style.display = 'none'; |
|
104 |
}
|
|
105 |
}
|
|
106 |
||
107 |
self.edit(); |
|
108 |
return self; |
|
109 |
}
|
|
110 |
||
111 |
CodePress.languages = { |
|
112 |
csharp : 'C#', |
|
113 |
css : 'CSS', |
|
114 |
generic : 'Generic', |
|
115 |
html : 'HTML', |
|
116 |
java : 'Java', |
|
117 |
javascript : 'JavaScript', |
|
118 |
perl : 'Perl', |
|
119 |
ruby : 'Ruby', |
|
120 |
php : 'PHP', |
|
121 |
python : 'Python', |
|
122 |
text : 'Text', |
|
123 |
sql : 'SQL', |
|
124 |
vbscript : 'VBScript' |
|
125 |
}
|
|
126 |
||
127 |
||
128 |
CodePress.run = function() { |
|
129 |
s = document.getElementsByTagName('script'); |
|
130 |
for(var i=0,n=s.length;i<n;i++) { |
|
131 |
if(s[i].src.match('codepress.js')) { |
|
132 |
CodePress.path = s[i].src.replace('codepress.js',''); |
|
133 |
}
|
|
134 |
}
|
|
135 |
t = document.getElementsByTagName('textarea'); |
|
136 |
for(var i=0,n=t.length;i<n;i++) { |
|
137 |
if(t[i].className.match('codepress')) { |
|
138 |
id = t[i].id; |
|
139 |
t[i].id = id+'_cp'; |
|
140 |
eval(id+' = new CodePress(t[i])'); |
|
141 |
t[i].parentNode.insertBefore(eval(id), t[i]); |
|
142 |
}
|
|
143 |
}
|
|
144 |
}
|
|
145 |
||
146 |
if(window.attachEvent) window.attachEvent('onload',CodePress.run); |
|
147 |
else window.addEventListener('DOMContentLoaded',CodePress.run,false); |