I have a text message input using draft-js where I can use emojis. The problem is that if I want to delete a flag emoji, once I press backspace once it deletes one letter of an abbreviature used for that falg emoji and the other letter could be seen instead that way. For example of there is an australian flag emoji which consists of two letters au then after I press backspace the 'a' letter will still remain in the input. What can I do about it?
Assistant
User
this is not reliable. There are lots of flags emojis and with this code I would need to check for each possible combination, not to mention that in any other case backspace should retain its default behaviour
Assistant
I apologize for the confusion. You are right that checking for each possible flag emoji combination would not be feasible.
To retain the default behavior of the Backspace key and only handle deleting flag emojis when necessary, you can use the beforeInput event instead.
To handle the issue of deleting flag emojis, you can create a custom KeyBinding function in draft-js for the backspace key.
In this example, we override the keyBindingFn prop of the Editor component to detect when the Backspace key with a Command modifier is pressed. We then define a new command called 'backspace-emoji'.
When this command is triggered, the handleKeyCommand function is called. We extract the text around the cursor and check if the last two characters match the abbreviation of the flag emoji (in this case, 'au'). If the check is successful, we remove both characters from the text and update the editor state accordingly.
This implementation ensures that when the Backspace key is pressed while the cursor is right after a flag emoji abbreviation, both letters of the abbreviation are deleted together.
In this updated implementation, we handle the flag emoji deletion in the handleBeforeInput event. Instead of checking for the Backspace key, we check if the input matches the flag emoji abbreviation (in this case, 'au'). If it matches, we remove those two characters from the text by updating the blockMap of the content state.
This approach allows the Backspace key to retain its default behavior when it's not deleting a flag emoji abbreviation.