From 2908b5d5d7d80feb73bb8be73fafd4206b98d7d7 Mon Sep 17 00:00:00 2001 From: Lev Lazinskiy Date: Mon, 23 Jan 2017 22:13:55 -0800 Subject: [PATCH] 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; } }