Created
February 14, 2019 08:00
-
-
Save janwirth/a3140100b7115d6270da1d5222fbb040 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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