Improving nano editor (thanks to tragic0mic)

This commit is contained in:
BlackLight 2011-02-09 18:16:44 +01:00
parent fbb1d6ff5b
commit 1f75240b42

View file

@ -68,9 +68,11 @@
shell.fname = arg; shell.fname = arg;
shell.editorkeypressed = this.editorkeypressed; shell.editorkeypressed = this.editorkeypressed;
shell.editorpaste = this.editorpaste;
shell.editorclick = this.editorclick;
shell.confirmkey = this.confirmkey; shell.confirmkey = this.confirmkey;
shell.bufferSave = this.bufferSave; shell.bufferSave = this.bufferSave;
shell.firstKey = true; // Set when no key has been pressed yet shell.firstKey = 1; // Set when no key has been pressed yet
shell.default_editor_status = "[<b>^X</b> Exit] [<b>^O</b> WriteOut] [<b>^F</b> Where Is]"; shell.default_editor_status = "[<b>^X</b> Exit] [<b>^O</b> WriteOut] [<b>^F</b> Where Is]";
@ -105,9 +107,13 @@
shell.editor_head = document.getElementById ( 'editor_head' ); shell.editor_head = document.getElementById ( 'editor_head' );
var editor = document.createElement ( 'textarea' ); var editor = document.createElement ( 'textarea' );
editor.setAttribute ( 'class', 'editor_window' ); editor.setAttribute ( 'class', 'editor_window' );
editor.setAttribute ( 'id', 'editor_window' ); editor.setAttribute ( 'id', 'editor_window' );
editor.setAttribute ( 'onkeypress', 'shell.editorkeypressed ( event )' ); editor.setAttribute ( 'onkeypress', 'shell.editorkeypressed ( event )' );
// editor.setAttribute ( 'onmousedown', 'shell.editorclick ( event )' ); /* This might be useful someday */
editor.setAttribute ( 'onpaste', 'shell.editorpaste ( )' );
editor.setAttribute ( 'oncut', 'shell.editorpaste( )' );
has_content = false; has_content = false;
@ -117,7 +123,7 @@
{ {
if ( file.content.length > 0 ) if ( file.content.length > 0 )
{ {
var content = file.content.replace ( /<br\/?>/g, "\n" ); var content = file.content.replace ( /(<br ?\/?>)/g, "\n" );
content = content.replace ( /&lt;/g, '<' ); content = content.replace ( /&lt;/g, '<' );
content = content.replace ( /&gt;/g, '>' ); content = content.replace ( /&gt;/g, '>' );
editor.value = content; editor.value = content;
@ -146,29 +152,44 @@
container.appendChild ( status ); container.appendChild ( status );
shell.editor_status = document.getElementById ( 'editor_status' ); shell.editor_status = document.getElementById ( 'editor_status' );
shell.hitCount = 0;
return ''; return '';
}, },
"editorkeypressed" : function ( e ) "editorkeypressed" : function ( e )
{ {
shell.hitCount++;
var evt = ( window.event ) ? window.event : e; var evt = ( window.event ) ? window.event : e;
var key = ( evt.charCode ) ? evt.charCode : evt.keyCode; var key = ( evt.charCode ) ? evt.charCode : evt.keyCode;
if ( shell.firstKey && key == 13 ) if ( shell.firstKey == 1 && key == 13 )
{ {
evt.preventDefault(); evt.preventDefault();
shell.firstKey = false; shell.firstKey = -1;
return false; return false;
} }
if(shell.firstKey == -1)
{
shell.file_changed = true;
if(shell.perms.write == false)
shell.editor_status.innerHTML += " [read-only]";
shell.editor_status.innerHTML += " [modified]";
shell.firstKey = 0;
shell.editor_window.value = shell.editor_window.value.replace(/(\n|\r)+$/, '');
}
key = String.fromCharCode ( key ); key = String.fromCharCode ( key );
if (( key == 'x' || key == 'X' ) && evt.ctrlKey ) if (( key == 'x' || key == 'X' ) && evt.ctrlKey )
{ {
evt.preventDefault(); evt.preventDefault();
if ( shell.file_changed ) if(shell.originalContent != shell.editor_window.value)
shell.file_changed = true;
if ( shell.file_changed && shell.hitCount > 2)
{ {
var can_write = false; var can_write = false;
@ -203,6 +224,9 @@
} else if (( key == 'o' || key == 'O' ) && evt.ctrlKey ) { } else if (( key == 'o' || key == 'O' ) && evt.ctrlKey ) {
evt.preventDefault(); evt.preventDefault();
if( shell.originalContent != shell.editor_window.value )
shell.file_changed = true;
if ( shell.file_changed ) if ( shell.file_changed )
{ {
var can_write = false; var can_write = false;
@ -235,7 +259,7 @@
return false; return false;
} }
if ( !shell.file_changed ) if( !shell.file_changed )
{ {
if ( shell.originalContent != shell.editor_window.value ) if ( shell.originalContent != shell.editor_window.value )
{ {
@ -247,11 +271,11 @@
shell.editor_status.innerHTML += ' [read-only]'; shell.editor_status.innerHTML += ' [read-only]';
} }
} }
shell.editor_status.innerHTML += ' [modified]'; shell.editor_status.innerHTML += ' [modified]';
shell.file_changed = true; shell.file_changed = true;
} }
} }
}, },
"confirmkey" : function ( e, arg ) "confirmkey" : function ( e, arg )
@ -397,6 +421,34 @@
http.send ( params ); http.send ( params );
shell.originalContent = shell.editor_window.value; shell.originalContent = shell.editor_window.value;
shell.file_changed = false; shell.file_changed = false;
},
"editorpaste" : function ( )
{
if(shell.perms)
{
if(shell.perms.write == true)
{
if(shell.file_changed == false)
{
shell.file_changed = true;
shell.editor_status.innerHTML += ' [modified]';
}
}
else
if(shell.file_changed == false)
{
shell.file_changed = true;
shell.editor_status.innerHTML += ' [read-only] [modified]';
}
}
return;
},
"editorclick": function ( e )
{
if (e.button == 2); /* Do something with the right button */
return;
} }
} }