diff options
author | Florrie <towerofnix@gmail.com> | 2018-09-12 14:20:30 -0300 |
---|---|---|
committer | Florrie <towerofnix@gmail.com> | 2018-09-12 14:22:11 -0300 |
commit | e2f830680c1a6e9f28ad305b105caf5cdf092e63 (patch) | |
tree | 0ce0a3a8134a8864a7d87e4e5d6ea678986d7abd /ui/form | |
parent | 9bffe0da811c4ff445348531f66db1559d80fd2d (diff) |
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.
Diffstat (limited to 'ui/form')
-rw-r--r-- | ui/form/TextInput.js | 76 |
1 files 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) { |