Skip to content

Commit

Permalink
Merge pull request #2 from cumhuronat/main
Browse files Browse the repository at this point in the history
fix handling of enter key
  • Loading branch information
charlieroberts authored Dec 13, 2024
2 parents 78ae34a + 291be12 commit 61c1abd
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bitty.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ let bitty = window.bitty = {

editor( instance, el, tab = ' ') {
const bitty = instance
let lastKeyDownCode = 0
const caret = () => {
const range = window.getSelection().getRangeAt(0)
const prefix = range.cloneRange()
Expand Down Expand Up @@ -438,6 +439,10 @@ let bitty = window.bitty = {


el.addEventListener( 'keydown', e => {
// register last key down code if not a control character
if(e.key.length === 1 || e.key === 'Tab' || e.key === 'Enter'){
lastKeyDownCode = e.keyCode
}
// handle tab key
if(e.keyCode === 9) {
const pos = caret() + tab.length
Expand Down Expand Up @@ -481,9 +486,9 @@ let bitty = window.bitty = {

// handle all other non-control keys
el.addEventListener('keyup', e => {
// do not refocus if ctrl key is pressed
// do not refocus if ctrl key is pressed or if the last key down is enter
// this stops refocusing for ctrl+a, or ctrl+enter etc.
if ( !e.ctrlKey && e.keyCode >= 0x30 || e.keyCode === 0x20) {
if (lastKeyDownCode != 13 && !e.ctrlKey && e.keyCode >= 0x30 || e.keyCode === 0x20) {
const pos = caret()
bitty.process()
setCaret( pos )
Expand Down

0 comments on commit 61c1abd

Please sign in to comment.