Only handle tab key inside of editor

This refactors the controller a bit to insert 4 spaces when
a tab key is inserted inside of an editor, but in all other parts
of the app allow the user to use the tab key normally.

fix
https://github.com/standardnotes/desktop/issues/23
This commit is contained in:
Lev Lazinskiy
2017-01-19 16:20:52 -08:00
parent f904c84827
commit 8d54b916eb

View File

@@ -15,8 +15,12 @@ angular.module('app.frontend')
link:function(scope, elem, attrs, ctrl) { link:function(scope, elem, attrs, ctrl) {
var handler = function(event) { /**
// Handle Tab Key * 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) { if (event.which == 9) {
event.preventDefault(); event.preventDefault();
var start = event.target.selectionStart; var start = event.target.selectionStart;
@@ -25,6 +29,9 @@ angular.module('app.frontend')
event.target.value = event.target.value.substring(0, start) event.target.value = event.target.value.substring(0, start)
+ spaces + event.target.value.substring(end); + spaces + event.target.value.substring(end);
} }
}
var handler = function(event) {
if (event.ctrlKey || event.metaKey) { if (event.ctrlKey || event.metaKey) {
switch (String.fromCharCode(event.which).toLowerCase()) { switch (String.fromCharCode(event.which).toLowerCase()) {
case 's': case 's':
@@ -56,6 +63,9 @@ angular.module('app.frontend')
}; };
window.addEventListener('keydown', handler); window.addEventListener('keydown', handler);
var editor = document.getElementById("note-text-editor");
editor.addEventListener('keydown', handleTab);
scope.$on('$destroy', function(){ scope.$on('$destroy', function(){
window.removeEventListener('keydown', handler); window.removeEventListener('keydown', handler);
@@ -100,8 +110,7 @@ angular.module('app.frontend')
this.focusEditor = function(delay) { this.focusEditor = function(delay) {
setTimeout(function(){ setTimeout(function(){
var element = document.getElementById("note-text-editor"); editor.focus();
element.focus();
}, delay) }, delay)
} }