
org.wicketstuff.jquery.codepress.engines.gecko.js Maven / Gradle / Ivy
/*
* CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/
*
* Copyright (C) 2007 Fernando M.A.d.S.
*
* Developers:
* Fernando M.A.d.S.
* Michael Hurni
* Contributors:
* Martin D. Kirk
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation.
*
* Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
*/
CodePress = {
scrolling : false,
autocomplete : true,
// set initial vars and start sh
initialize : function() {
if(typeof(editor)=='undefined' && !arguments[0]) return;
body = document.getElementsByTagName('body')[0];
body.innerHTML = body.innerHTML.replace(/\n/g,"");
chars = '|32|46|62|8|'; // charcodes that trigger syntax highlighting
cc = '\u2009'; // carret char
editor = document.getElementsByTagName('pre')[0];
document.designMode = 'on';
document.addEventListener('keypress', this.keyHandler, true);
window.addEventListener('scroll', function() { if(!CodePress.scrolling) CodePress.syntaxHighlight('scroll') }, false);
completeChars = this.getCompleteChars();
completeEndingChars = this.getCompleteEndingChars();
},
// treat key bindings
keyHandler : function(evt) {
keyCode = evt.keyCode;
charCode = evt.charCode;
fromChar = String.fromCharCode(charCode);
if(! (evt.ctrlKey || evt.metaKey || evt.shiftKey) && keyCode == 9) {
if(evt.preventDefault) evt.preventDefault(); // Gecko and Others
evt.returnValue = false; // IE
var code = '';
code = window.getSelection().getRangeAt(0).toString();
CodePress.insertCode('\t'+code);
CodePress.snippets(evt);
}
else if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo)
CodePress.shortcuts(charCode?charCode:keyCode);
}
else if( (completeEndingChars.indexOf('|'+fromChar+'|')!= -1 || completeChars.indexOf('|'+fromChar+'|')!=-1) && CodePress.autocomplete) { // auto complete
if(!CodePress.completeEnding(fromChar))
CodePress.complete(fromChar);
}
else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting
top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
}
else if(keyCode==9 || evt.tabKey) { // snippets activation (tab)
CodePress.snippets(evt);
evt.preventDefault();
}
else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed
CodePress.actions.history[CodePress.actions.next()] = editor.innerHTML;
}
else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo
(charCode==121||evt.shiftKey) ? CodePress.actions.redo() : CodePress.actions.undo();
evt.preventDefault();
}
else if(charCode==118 && evt.ctrlKey) { // handle paste
top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
}
else if(charCode==99 && evt.ctrlKey) { // handle cut
//alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF'));
}
},
// put cursor back to its original position after every parsing
findString : function() {
if(self.find(cc))
window.getSelection().getRangeAt(0).deleteContents();
},
// split big files, highlighting parts of it
split : function(code,flag) {
if(flag=='scroll') {
this.scrolling = true;
return code;
}
else {
this.scrolling = false;
mid = code.indexOf(cc);
if(mid-2000<0) {ini=0;end=4000;}
else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;}
else {ini=mid-2000;end=mid+2000;}
code = code.substring(ini,end);
return code;
}
},
getEditor : function() {
if(!document.getElementsByTagName('pre')[0]) {
body = document.getElementsByTagName('body')[0];
if(!body.innerHTML) return body;
if(body.innerHTML=="
") body.innerHTML = "
";
else body.innerHTML = ""+body.innerHTML+"
";
}
return document.getElementsByTagName('pre')[0];
},
// syntax highlighting parser
syntaxHighlight : function(flag) {
//if(document.designMode=='off') document.designMode='on'
if(flag != 'init') { window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));}
editor = CodePress.getEditor();
o = editor.innerHTML;
o = o.replace(/
/g,'\n');
o = o.replace(/<.*?>/g,'');
x = z = this.split(o,flag);
x = x.replace(/\n/g,'
');
if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]);
for(i=0;i/g,'>');
if(content.indexOf('$0')<0) content += cc;
else content = content.replace(/\$0/,cc);
content = content.replace(/\n/g,'
');
var pattern = new RegExp(trigger+cc,'gi');
evt.preventDefault(); // prevent the tab key from being added
this.syntaxHighlight('snippets',pattern,content);
}
}
},
readOnly : function() {
document.designMode = (arguments[0]) ? 'off' : 'on';
},
complete : function(trigger) {
window.getSelection().getRangeAt(0).deleteContents();
var complete = Language.complete;
for (var i=0; i/g,'\n');
code = code.replace(/\u2009/g,'');
code = code.replace(/<.*?>/g,'');
code = code.replace(/</g,'<');
code = code.replace(/>/g,'>');
code = code.replace(/&/gi,'&');
return code;
},
// put code inside editor
setCode : function() {
var code = arguments[0];
code = code.replace(/\u2009/gi,'');
code = code.replace(/&/gi,'&');
code = code.replace(//g,'>');
editor.innerHTML = code;
if (code == '')
document.getElementsByTagName('body')[0].innerHTML = '';
},
// undo and redo methods
actions : {
pos : -1, // actual history position
history : [], // history vector
undo : function() {
editor = CodePress.getEditor();
if(editor.innerHTML.indexOf(cc)==-1){
if(editor.innerHTML != " ")
window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));
this.history[this.pos] = editor.innerHTML;
}
this.pos --;
if(typeof(this.history[this.pos])=='undefined') this.pos ++;
editor.innerHTML = this.history[this.pos];
if(editor.innerHTML.indexOf(cc)>-1) editor.innerHTML+=cc;
CodePress.findString();
},
redo : function() {
// editor = CodePress.getEditor();
this.pos++;
if(typeof(this.history[this.pos])=='undefined') this.pos--;
editor.innerHTML = this.history[this.pos];
CodePress.findString();
},
next : function() { // get next vector position and clean old ones
if(this.pos>20) this.history[this.pos-21] = undefined;
return ++this.pos;
}
}
}
Language={};
window.addEventListener('load', function() { CodePress.initialize('new'); }, true);
© 2015 - 2025 Weber Informatics LLC | Privacy Policy