From 2ad0ba8506d9a64bf389264dca2adee66e185836 Mon Sep 17 00:00:00 2001 From: Francisco Hodge Date: Sat, 6 Oct 2018 02:22:02 -0400 Subject: [PATCH] Cursor position support --- src/lib/components/Keyboard.js | 27 +++++++++++++++++++++++++++ src/lib/services/Utilities.js | 16 ++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/lib/components/Keyboard.js b/src/lib/components/Keyboard.js index 0c750725..46f449a7 100644 --- a/src/lib/components/Keyboard.js +++ b/src/lib/components/Keyboard.js @@ -167,6 +167,8 @@ class SimpleKeyboard { clear = () => { this.keyboardDOM.innerHTML = ''; this.keyboardDOM.className = this.keyboardDOMClass; + this.buttonElements = {}; + this.caretPosition = null; } dispatch = (callback) => { @@ -196,6 +198,31 @@ class SimpleKeyboard { return output; } + handleCaret = () => { + if(this.options.debug){ + console.log("Caret handling started"); + } + + let handler = (event) => { + let targetTagName = event.target.tagName.toLowerCase(); + + if( + targetTagName === "textarea" || + targetTagName === "input" + ){ + this.caretPosition = event.target.selectionStart; + + if(this.options.debug){ + console.log('Caret at: ', event.target.selectionStart, event.target.tagName.toLowerCase()); + } + } + }; + + document.addEventListener("keyup", handler); + document.addEventListener("mouseup", handler); + document.addEventListener("touchend", handler); + } + render = () => { /** * Clear keyboard diff --git a/src/lib/services/Utilities.js b/src/lib/services/Utilities.js index 49d642dc..297f52ea 100644 --- a/src/lib/services/Utilities.js +++ b/src/lib/services/Utilities.js @@ -1,5 +1,9 @@ class Utilities { - static normalizeString(string){ + constructor(simpleKeyboardInstance){ + this.simpleKeyboardInstance = simpleKeyboardInstance; + } + + normalizeString(string){ let output; if(string === "@") @@ -166,7 +170,15 @@ class Utilities { else if(button === "{numpaddecimal}") output = output + '.'; else if(!button.includes("{") && !button.includes("}")) - output = output + button; + + updateCaretPos = (length, minus) => { + if(minus){ + this.simpleKeyboardInstance.caretPosition = this.simpleKeyboardInstance.caretPosition - length + } else { + this.simpleKeyboardInstance.caretPosition = this.simpleKeyboardInstance.caretPosition + length; + } + } + return output; }