Skip to content

Commit

Permalink
ensure each line is in its own div on paste
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieroberts committed Nov 26, 2024
1 parent 3e0639f commit 0faad01
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions bitty.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,18 @@ let bitty = window.bitty = {
})

observer.observe(el, { childList: true })

// when pasting into a blank line, the result
// creates a div inside of a div. To avoid this,
// we remove nodes that are blank lines or only line breaks
// before pasting

const noDivsInDivs = function() {
for( let n of Array.from( bitty.el.childNodes ) ) {
const lines = n.querySelectorAll('div')
if( lines.length > 1 ) {
for( let l of lines ) {
bitty.el.insertBefore( l,n )
}
}
}
}

el.addEventListener("paste", function(e) {
var text = (e.clipboardData || window.clipboardData).getData('text/plain');
if( text.split('\n').length === 1 ) return
Expand All @@ -258,8 +265,10 @@ let bitty = window.bitty = {
|| e.target.innerText === '\n'
|| e.target.innerText === '<br>') e.target.remove()

setTimeout( noDivsInDivs, 0 )
// now paste continues as usual, no blocking the default event...
});


el.addEventListener('keydown', e => {
// handle tab key
Expand Down

0 comments on commit 0faad01

Please sign in to comment.