From e2f830680c1a6e9f28ad305b105caf5cdf092e63 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 12 Sep 2018 14:20:30 -0300 Subject: TextInput: Don't bubble backspace or arrow keys Like mentioned before, it's not too complicated to make these not bubble - just some added 'return false's. I wrapped the whole block in a try-finally so that keepCursorInRange could always be called at the end without any significant code structure change, so that means the git diff is a little wonky - best viewed with the -w (ignore whitespace changes) option. --- ui/form/TextInput.js | 76 +++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/ui/form/TextInput.js b/ui/form/TextInput.js index 766f959..5b5fa06 100644 --- a/ui/form/TextInput.js +++ b/ui/form/TextInput.js @@ -48,50 +48,52 @@ module.exports = class TextInput extends FocusElement { } keyPressed(keyBuf) { - if (keyBuf[0] === 127) { - this.value = ( - this.value.slice(0, this.cursorIndex - 1) + - this.value.slice(this.cursorIndex) - ) - this.cursorIndex-- - this.root.cursorMoved() - } else if (keyBuf[0] === 13) { - this.emit('value', this.value) - } else if (keyBuf[0] === 0x1b && keyBuf[1] === 0x5b) { - // Keyboard navigation - if (keyBuf[2] === 0x44) { - this.cursorIndex-- - this.root.cursorMoved() - } else if (keyBuf[2] === 0x43) { - this.cursorIndex++ - this.root.cursorMoved() - } - } else if (telc.isEscape(keyBuf)) { - // ESC is bad and we don't want that in the text input! - // Also emit a "cancel" event, which doesn't necessarily do anything, - // but can be listened to. - this.emit('cancel') - return - } else { - const isTextInput = keyBuf.toString().split('').every(chr => { - const n = chr.charCodeAt(0) - return n > 31 && n < 127 - }) - - if (isTextInput) { + try { + if (keyBuf[0] === 127) { this.value = ( - this.value.slice(0, this.cursorIndex) + keyBuf.toString() + + this.value.slice(0, this.cursorIndex - 1) + this.value.slice(this.cursorIndex) ) - this.cursorIndex += keyBuf.toString().length + this.cursorIndex-- this.root.cursorMoved() - this.keepCursorInRange() - return false + } else if (keyBuf[0] === 13) { + this.emit('value', this.value) + } else if (keyBuf[0] === 0x1b && keyBuf[1] === 0x5b) { + // Keyboard navigation + if (keyBuf[2] === 0x44) { + this.cursorIndex-- + this.root.cursorMoved() + } else if (keyBuf[2] === 0x43) { + this.cursorIndex++ + this.root.cursorMoved() + } + return false + } else if (telc.isEscape(keyBuf)) { + // ESC is bad and we don't want that in the text input! + // Also emit a "cancel" event, which doesn't necessarily do anything, + // but can be listened to. + this.emit('cancel') + } else { + const isTextInput = keyBuf.toString().split('').every(chr => { + const n = chr.charCodeAt(0) + return n > 31 && n < 127 + }) + + if (isTextInput) { + this.value = ( + this.value.slice(0, this.cursorIndex) + keyBuf.toString() + + this.value.slice(this.cursorIndex) + ) + this.cursorIndex += keyBuf.toString().length + this.root.cursorMoved() + + return false + } } + } finally { + this.keepCursorInRange() } - - this.keepCursorInRange() } setValue(value) { -- cgit 1.3.0-6-gf8a5