Skip to content

Instantly share code, notes, and snippets.

@janwirth
Created February 14, 2019 08:00
Show Gist options
  • Save janwirth/a3140100b7115d6270da1d5222fbb040 to your computer and use it in GitHub Desktop.
Save janwirth/a3140100b7115d6270da1d5222fbb040 to your computer and use it in GitHub Desktop.
function makeElmCompatible () {
// Prevent content editable from deleting the text node that elm renders to
document.addEventListener('keydown', event => {
const zeroWidthSpace = '​'
// only for content editable
if (event.target.contentEditable !== 'true') {
return
}
// prevent deletion of last character and replace with zero width space instead
if (event.key === 'Backspace') {
if (event.target.innerText.length == 1) {
event.preventDefault()
event.target.innerText = zeroWidthSpace
}
} else {
if (event.target.innerText.length == 1 && event.target.innerText[0] == zeroWidthSpace) {
event.target.innerText = ""
}
}
})
}
export { makeElmCompatible }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment