From 88de9e40503833f76e79e8ac722025ceafd15c4b Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 11 Nov 2011 20:46:06 +0000 Subject: [PATCH] Keyboard shortcuts for saving. Refined code. --- apps/files_texteditor/js/editor.js | 59 +++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/apps/files_texteditor/js/editor.js b/apps/files_texteditor/js/editor.js index fd93ac5a7a8..828839cbc92 100644 --- a/apps/files_texteditor/js/editor.js +++ b/apps/files_texteditor/js/editor.js @@ -77,8 +77,10 @@ function updateSessionFileHash(path){ "json"); } +var editor_is_saving = false; function doFileSave(){ if(is_editor_shown){ + editor_is_saving = true; $('#editor_save').after(''); var filecontents = window.aceEditor.getSession().getValue(); var dir = $('#editor').attr('data-dir'); @@ -93,7 +95,10 @@ function doFileSave(){ $('#saving_icon').remove(); $('#editor_save').after('

Saved!

') setTimeout(function() { - $('#save_result').remove(); + $('#save_result').fadeOut('slow',function(){ + $(this).remove(); + editor_is_saving = false; + }); }, 2000); } else { @@ -101,7 +106,10 @@ function doFileSave(){ $('#saving_icon').remove(); $('#editor_save').after('

Failed!

'); setTimeout(function() { - $('#save_result').fadeOut('slow',function(){ $(this).remove(); }); + $('#save_result').fadeOut('slow',function(){ + $(this).remove(); + editor_is_saving = false; + }); }, 2000); } }, 'json'); @@ -112,7 +120,10 @@ function doFileSave(){ // Temporary measure until we get a tick icon $('#editor_save').after('

Saved!

'); setTimeout(function() { - $('#save_result').fadeOut('slow',function(){ $(this).remove(); }); + $('#save_result').fadeOut('slow',function(){ + $(this).remove(); + editor_is_saving = false; + }); }, 2000); } } @@ -122,21 +133,17 @@ function doFileSave(){ // Temporary measure until we get a tick icon $('#editor_save').after('

Saved!

'); setTimeout(function() { - $('#save_result').fadeOut('slow',function(){ $(this).remove(); }); + $('#save_result').fadeOut('slow',function(){ + $(this).remove(); + editor_is_saving = false; + }); }, 2000); } }, 'json'); - giveEditorFocus(); - } else { - $('#editor').data('editor','false'); - return; + window.aceEditor.focus(); } }; -function giveEditorFocus(){ - window.aceEditor.focus(); -}; - function showFileEditor(dir,filename){ if(!is_editor_shown){ // Loads the file editor and display it. @@ -190,9 +197,31 @@ function hideFileEditor(){ } } +// Keyboard Shortcuts +var ctrlBtn = false; + +function checkForCtrlKey(e){ + if(e.which == 17 || e.which == 91) ctrlBtn=false; + } + +function checkForSaveKeyPress(e){ + if(!editor_is_saving){ + if(e.which == 17 || e.which == 91) ctrlBtn=true; + if(e.which == 83 && ctrlBtn == true) { + e.preventDefault(); + doFileSave(); + return false; + } + } else { + e.preventDefault(); + } +} + +// Sets the correct size of the editor window $(window).resize(function() { setEditorSize(); }); + var is_editor_shown = false; $(document).ready(function(){ @@ -220,4 +249,8 @@ $(document).ready(function(){ } // Binds the file save and close editor events to the buttons bindControlEvents(); -}); + + // Binds the save keyboard shortcut events + $(document).unbind('keyup').bind('keyup',checkForCtrlKey).unbind('keydown').bind('keydown',checkForSaveKeyPress); + + });