~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/codepress/engines/gecko.js

  • Committer: William Grant
  • Date: 2008-07-07 01:45:11 UTC
  • mfrom: (unknown (missing))
  • Revision ID: wgrant@ugrad.unimelb.edu.au-20080707014511-xyy43p6y8jiqrsh9
Merge killall-editarea branch. We now use CodePress instead, which is
somewhat less slothful.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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();
 
35
                document.changehandlers = Array()
 
36
        },
 
37
 
 
38
        // treat key bindings
 
39
        keyHandler : function(evt) {
 
40
        keyCode = evt.keyCode;  
 
41
                charCode = evt.charCode;
 
42
                fromChar = String.fromCharCode(charCode);
 
43
                for (var handler in this.changehandlers)
 
44
                {
 
45
                        document.changehandlers[handler]();
 
46
                }
 
47
 
 
48
                if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90)  { // shortcuts = ctrl||appleKey+shift+key!=z(undo) 
 
49
                        CodePress.shortcuts(charCode?charCode:keyCode);
 
50
                }
 
51
                else if( (completeEndingChars.indexOf('|'+fromChar+'|')!= -1 || completeChars.indexOf('|'+fromChar+'|')!=-1) && CodePress.autocomplete) { // auto complete
 
52
                        if(!CodePress.completeEnding(fromChar))
 
53
                             CodePress.complete(fromChar);
 
54
                }
 
55
            else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting
 
56
                        top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
 
57
                }
 
58
                else if(keyCode==9 || evt.tabKey) {  // snippets activation (tab)
 
59
                        CodePress.snippets(evt);
 
60
                }
 
61
                else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed
 
62
                        CodePress.actions.history[CodePress.actions.next()] = editor.innerHTML;
 
63
                }
 
64
                else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo
 
65
                        (charCode==121||evt.shiftKey) ? CodePress.actions.redo() :  CodePress.actions.undo(); 
 
66
                        evt.preventDefault();
 
67
                }
 
68
                else if(charCode==118 && evt.ctrlKey)  { // handle paste
 
69
                        top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
 
70
                }
 
71
                else if(charCode==99 && evt.ctrlKey)  { // handle cut
 
72
                        //alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF'));
 
73
                }
 
74
 
 
75
        },
 
76
 
 
77
        // put cursor back to its original position after every parsing
 
78
        findString : function() {
 
79
                if(self.find(cc))
 
80
                        window.getSelection().getRangeAt(0).deleteContents();
 
81
        },
 
82
        
 
83
        // split big files, highlighting parts of it
 
84
        split : function(code,flag) {
 
85
                if(flag=='scroll') {
 
86
                        this.scrolling = true;
 
87
                        return code;
 
88
                }
 
89
                else {
 
90
                        this.scrolling = false;
 
91
                        mid = code.indexOf(cc);
 
92
                        if(mid-2000<0) {ini=0;end=4000;}
 
93
                        else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;}
 
94
                        else {ini=mid-2000;end=mid+2000;}
 
95
                        code = code.substring(ini,end);
 
96
                        return code;
 
97
                }
 
98
        },
 
99
        
 
100
        getEditor : function() {
 
101
                if(!document.getElementsByTagName('pre')[0]) {
 
102
                        body = document.getElementsByTagName('body')[0];
 
103
                        if(!body.innerHTML) return body;
 
104
                        if(body.innerHTML=="<br>") body.innerHTML = "<pre> </pre>";
 
105
                        else body.innerHTML = "<pre>"+body.innerHTML+"</pre>";
 
106
                }
 
107
                return document.getElementsByTagName('pre')[0];
 
108
        },
 
109
        
 
110
        // syntax highlighting parser
 
111
        syntaxHighlight : function(flag) {
 
112
                //if(document.designMode=='off') document.designMode='on'
 
113
                if(flag != 'init') { window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));}
 
114
                editor = CodePress.getEditor();
 
115
                o = editor.innerHTML;
 
116
                o = o.replace(/<br>/g,'\n');
 
117
                o = o.replace(/<.*?>/g,'');
 
118
                x = z = this.split(o,flag);
 
119
                x = x.replace(/\n/g,'<br>');
 
120
 
 
121
                if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]);
 
122
        
 
123
                for(i=0;i<Language.syntax.length;i++) 
 
124
                        x = x.replace(Language.syntax[i].input,Language.syntax[i].output);
 
125
 
 
126
                editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.split(z).join(x);
 
127
                if(flag!='init') this.findString();
 
128
        },
 
129
        
 
130
        getLastWord : function() {
 
131
                var rangeAndCaret = CodePress.getRangeAndCaret();
 
132
                words = rangeAndCaret[0].substring(rangeAndCaret[1]-40,rangeAndCaret[1]);
 
133
                words = words.replace(/[\s\n\r\);\W]/g,'\n').split('\n');
 
134
                return words[words.length-1].replace(/[\W]/gi,'').toLowerCase();
 
135
        },
 
136
        
 
137
        snippets : function(evt) {
 
138
                var snippets = Language.snippets;       
 
139
                var trigger = this.getLastWord();
 
140
                for (var i=0; i<snippets.length; i++) {
 
141
                        if(snippets[i].input == trigger) {
 
142
                                var content = snippets[i].output.replace(/</g,'&lt;');
 
143
                                content = content.replace(/>/g,'&gt;');
 
144
                                if(content.indexOf('$0')<0) content += cc;
 
145
                                else content = content.replace(/\$0/,cc);
 
146
                                content = content.replace(/\n/g,'<br>');
 
147
                                var pattern = new RegExp(trigger+cc,'gi');
 
148
                                evt.preventDefault(); // prevent the tab key from being added
 
149
                                this.syntaxHighlight('snippets',pattern,content);
 
150
                        }
 
151
                }
 
152
        },
 
153
        
 
154
        readOnly : function() {
 
155
                document.designMode = (arguments[0]) ? 'off' : 'on';
 
156
        },
 
157
 
 
158
        complete : function(trigger) {
 
159
                window.getSelection().getRangeAt(0).deleteContents();
 
160
                var complete = Language.complete;
 
161
                for (var i=0; i<complete.length; i++) {
 
162
                        if(complete[i].input == trigger) {
 
163
                                var pattern = new RegExp('\\'+trigger+cc);
 
164
                                var content = complete[i].output.replace(/\$0/g,cc);
 
165
                                parent.setTimeout(function () { CodePress.syntaxHighlight('complete',pattern,content)},0); // wait for char to appear on screen
 
166
                        }
 
167
                }
 
168
        },
 
169
 
 
170
        getCompleteChars : function() {
 
171
                var cChars = '';
 
172
                for(var i=0;i<Language.complete.length;i++)
 
173
                        cChars += '|'+Language.complete[i].input;
 
174
                return cChars+'|';
 
175
        },
 
176
        
 
177
        getCompleteEndingChars : function() {
 
178
                var cChars = '';
 
179
                for(var i=0;i<Language.complete.length;i++)
 
180
                        cChars += '|'+Language.complete[i].output.charAt(Language.complete[i].output.length-1);
 
181
                return cChars+'|';
 
182
        },
 
183
        
 
184
        completeEnding : function(trigger) {
 
185
                var range = window.getSelection().getRangeAt(0);
 
186
                try {
 
187
                        range.setEnd(range.endContainer, range.endOffset+1)
 
188
                }
 
189
                catch(e) {
 
190
                        return false;
 
191
                }
 
192
                var next_character = range.toString()
 
193
                range.setEnd(range.endContainer, range.endOffset-1)
 
194
                if(next_character != trigger) return false;
 
195
                else {
 
196
                        range.setEnd(range.endContainer, range.endOffset+1)
 
197
                        range.deleteContents();
 
198
                        return true;
 
199
                }
 
200
        },
 
201
        
 
202
        shortcuts : function() {
 
203
                var cCode = arguments[0];
 
204
                if(cCode==13) cCode = '[enter]';
 
205
                else if(cCode==32) cCode = '[space]';
 
206
                else cCode = '['+String.fromCharCode(charCode).toLowerCase()+']';
 
207
                for(var i=0;i<Language.shortcuts.length;i++)
 
208
                        if(Language.shortcuts[i].input == cCode)
 
209
                                this.insertCode(Language.shortcuts[i].output,false);
 
210
        },
 
211
        
 
212
        getRangeAndCaret : function() { 
 
213
                var range = window.getSelection().getRangeAt(0);
 
214
                var range2 = range.cloneRange();
 
215
                var node = range.endContainer;                  
 
216
                var caret = range.endOffset;
 
217
                range2.selectNode(node);        
 
218
                return [range2.toString(),caret];
 
219
        },
 
220
        
 
221
        insertCode : function(code,replaceCursorBefore) {
 
222
                var range = window.getSelection().getRangeAt(0);
 
223
                var node = window.document.createTextNode(code);
 
224
                var selct = window.getSelection();
 
225
                var range2 = range.cloneRange();
 
226
                // Insert text at cursor position
 
227
                selct.removeAllRanges();
 
228
                range.deleteContents();
 
229
                range.insertNode(node);
 
230
                // Move the cursor to the end of text
 
231
                range2.selectNode(node);                
 
232
                range2.collapse(replaceCursorBefore);
 
233
                selct.removeAllRanges();
 
234
                selct.addRange(range2);
 
235
        },
 
236
        
 
237
        // get code from editor
 
238
        getCode : function() {
 
239
                if(!document.getElementsByTagName('pre')[0] || editor.innerHTML == '')
 
240
                        editor = CodePress.getEditor();
 
241
                var code = editor.innerHTML;
 
242
                code = code.replace(/<br>/g,'\n');
 
243
                code = code.replace(/\u2009/g,'');
 
244
                code = code.replace(/<.*?>/g,'');
 
245
                code = code.replace(/&lt;/g,'<');
 
246
                code = code.replace(/&gt;/g,'>');
 
247
                code = code.replace(/&amp;/gi,'&');
 
248
                return code;
 
249
        },
 
250
 
 
251
        // put code inside editor
 
252
        setCode : function() {
 
253
                var code = arguments[0];
 
254
                code = code.replace(/\u2009/gi,'');
 
255
                code = code.replace(/&/gi,'&amp;');
 
256
                code = code.replace(/</g,'&lt;');
 
257
                code = code.replace(/>/g,'&gt;');
 
258
                editor.innerHTML = code;
 
259
                if (code == '')
 
260
                        document.getElementsByTagName('body')[0].innerHTML = '';
 
261
        },
 
262
 
 
263
        addChangeHandler : function(handler) {
 
264
                document.changehandlers.push(handler);
 
265
        },
 
266
 
 
267
        // undo and redo methods
 
268
        actions : {
 
269
                pos : -1, // actual history position
 
270
                history : [], // history vector
 
271
                
 
272
                undo : function() {
 
273
                        editor = CodePress.getEditor();
 
274
                        if(editor.innerHTML.indexOf(cc)==-1){
 
275
                                if(editor.innerHTML != " ")
 
276
                                        window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));
 
277
                                this.history[this.pos] = editor.innerHTML;
 
278
                        }
 
279
                        this.pos --;
 
280
                        if(typeof(this.history[this.pos])=='undefined') this.pos ++;
 
281
                        editor.innerHTML = this.history[this.pos];
 
282
                        if(editor.innerHTML.indexOf(cc)>-1) editor.innerHTML+=cc;
 
283
                        CodePress.findString();
 
284
                },
 
285
                
 
286
                redo : function() {
 
287
                        // editor = CodePress.getEditor();
 
288
                        this.pos++;
 
289
                        if(typeof(this.history[this.pos])=='undefined') this.pos--;
 
290
                        editor.innerHTML = this.history[this.pos];
 
291
                        CodePress.findString();
 
292
                },
 
293
                
 
294
                next : function() { // get next vector position and clean old ones
 
295
                        if(this.pos>20) this.history[this.pos-21] = undefined;
 
296
                        return ++this.pos;
 
297
                }
 
298
        }
 
299
}
 
300
 
 
301
Language={};
 
302
window.addEventListener('load', function() { CodePress.initialize('new'); }, true);