From 2908b5d5d7d80feb73bb8be73fafd4206b98d7d7 Mon Sep 17 00:00:00 2001 From: Lev Lazinskiy Date: Mon, 23 Jan 2017 22:13:55 -0800 Subject: [PATCH 1/3] Reset the cursor Once tab key is pressed, place the cursor 4 spaces away from the location where the tab key was pressed. This handles the case of when a tab key is pressed in the middle of an existing note and the cursor was placed at the end of the document. fix https://github.com/standardnotes/web/issues/50 --- .../javascripts/app/frontend/controllers/editor.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/app/frontend/controllers/editor.js b/app/assets/javascripts/app/frontend/controllers/editor.js index 6266db8a7..6c506be8d 100644 --- a/app/assets/javascripts/app/frontend/controllers/editor.js +++ b/app/assets/javascripts/app/frontend/controllers/editor.js @@ -23,11 +23,17 @@ angular.module('app.frontend') var handleTab = function (event) { if (event.which == 9) { event.preventDefault(); - var start = event.target.selectionStart; - var end = event.target.selectionEnd; + var start = this.selectionStart; + var end = this.selectionEnd; var spaces = " "; - event.target.value = event.target.value.substring(0, start) - + spaces + event.target.value.substring(end); + + // Insert 4 spaces + this.value = this.value.substring(0, start) + + spaces + this.value.substring(end); + + // Place cursor 4 spaces away from where + // the tab key was pressed + this.selectionStart = this.selectionEnd = start + 4; } } From 64468909c1edcf15518eb772cc0484096c1eb530 Mon Sep 17 00:00:00 2001 From: Lev Lazinskiy Date: Mon, 23 Jan 2017 22:50:39 -0800 Subject: [PATCH 2/3] Handle Shift Key If Shift + Tab is pressed, this event is not fired and instead the cursor moves to the previous field (i.e default browser behavior) --- app/assets/javascripts/app/frontend/controllers/editor.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/app/frontend/controllers/editor.js b/app/assets/javascripts/app/frontend/controllers/editor.js index 6c506be8d..a7686d0d2 100644 --- a/app/assets/javascripts/app/frontend/controllers/editor.js +++ b/app/assets/javascripts/app/frontend/controllers/editor.js @@ -19,9 +19,11 @@ angular.module('app.frontend') /** * Insert 4 spaces when a tab key is pressed, * only used when inside of the text editor. + * If the shift key is pressed first, this event is + * not fired. */ var handleTab = function (event) { - if (event.which == 9) { + if (!event.shiftKey && event.which == 9) { event.preventDefault(); var start = this.selectionStart; var end = this.selectionEnd; From d6e743a7b21035c74cc328241483963a9e260b47 Mon Sep 17 00:00:00 2001 From: Lev Lazinskiy Date: Mon, 23 Jan 2017 22:55:51 -0800 Subject: [PATCH 3/3] Fix space --- app/assets/javascripts/app/frontend/controllers/editor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/app/frontend/controllers/editor.js b/app/assets/javascripts/app/frontend/controllers/editor.js index a7686d0d2..a12a75394 100644 --- a/app/assets/javascripts/app/frontend/controllers/editor.js +++ b/app/assets/javascripts/app/frontend/controllers/editor.js @@ -23,7 +23,7 @@ angular.module('app.frontend') * not fired. */ var handleTab = function (event) { - if (!event.shiftKey && event.which == 9) { + if (!event.shiftKey && event.which == 9) { event.preventDefault(); var start = this.selectionStart; var end = this.selectionEnd;