Created
May 9, 2020 02:29
-
-
Save ShanonJackson/9a4ca13fcea4281ce094505505237671 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Check if a code unit forms part of a surrogate pair. Surrogate pairs are rendered as one character but stored | |
* as two characters in a JS string. | |
* https://dmitripavlutin.com/what-every-javascript-developer-should-know-about-unicode/#24-surrogate-pairs | |
*/ | |
const isSurrogate = (code: number): boolean => 0xd800 <= code && code <= 0xdfff; | |
/* Modify our onKeyDown Backspace Handler slightly... */ | |
if (e.keyCode === Keycodes.BACKSPACE) { | |
if (start === 0) return; | |
const charsToRemove = isSurrogate(value.charCodeAt(start - 1)) ? 2 : 1; | |
return insertString("", { start: start - charsToRemove, end: start }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment