diff --git a/app/assets/javascripts/app/frontend/controllers/editor.js b/app/assets/javascripts/app/frontend/controllers/editor.js index 358a24aa5..e567a3ad8 100644 --- a/app/assets/javascripts/app/frontend/controllers/editor.js +++ b/app/assets/javascripts/app/frontend/controllers/editor.js @@ -15,16 +15,23 @@ angular.module('app.frontend') link:function(scope, elem, attrs, ctrl) { + /** + * Insert 4 spaces when a tab key is pressed, + * only used when inside of the text editor. + */ + var handleTab = function (event ) { + + if (event.which == 9) { + event.preventDefault(); + var start = event.target.selectionStart; + var end = event.target.selectionEnd; + var spaces = " "; + event.target.value = event.target.value.substring(0, start) + + spaces + event.target.value.substring(end); + } + } + var handler = function(event) { - // Handle Tab Key - if (event.which == 9) { - event.preventDefault(); - var start = event.target.selectionStart; - var end = event.target.selectionEnd; - var spaces = " "; - event.target.value = event.target.value.substring(0, start) - + spaces + event.target.value.substring(end); - } if (event.ctrlKey || event.metaKey) { switch (String.fromCharCode(event.which).toLowerCase()) { case 's': @@ -56,6 +63,9 @@ angular.module('app.frontend') }; window.addEventListener('keydown', handler); + var editor = document.getElementById("note-text-editor"); + editor.addEventListener('keydown', handleTab); + scope.$on('$destroy', function(){ window.removeEventListener('keydown', handler); @@ -100,8 +110,7 @@ angular.module('app.frontend') this.focusEditor = function(delay) { setTimeout(function(){ - var element = document.getElementById("note-text-editor"); - element.focus(); + editor.focus(); }, delay) }