fix: Prevent Keydown event being triggered twice for CJK languages #47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
In Chrome-based browsers, entering Non-Latin-based language such as Chinese, Japanese and Korean (CJK) triggers Keydown event 2 times. Hence, the LAST letter is added to the tag list as well.
(chrome)
(safari)
Causes
The root cause of the problem has to do with Chrome, IME and Keydown Event. Typing CJK languages need the help of IME. When IME is used, Chrome triggers Keydown event twice.
While each letter in an English word is simply an alphabet character, each letter in CJK is composed of 2 or more characters. Keydown event has a boolean property 'isComposing’ to track if the user is composing a letter.
When a user types in some CJK word, isComposing property returns true - since the last letter is still considered being 'in the composition process’. When pressing the 'separator' key, Chrome’s bug triggers Keydown event twice, firstly for the whole word and secondly for the last letter which still remains in the input box because it was considered being composed when the first Keydown event was triggered. This results in both the word and the last letter to be added the tag list.
For the record, for any alphabet character, ‘isComposing’ property always returns ‘false’ value. Hence, the fix won't affect other languages.
Solution
Since alphabet characters always return falsy isComposing property, we only need to track if 'isComposing' is true. By doing so, the first Keydown event is ignored and ends composition process, and the second Keydown event correctly submits the whole word.
Reference
https://groups.google.com/g/mozilla.dev.platform/c/oZEz5JH9ZK8
vuejs/vue#10277 (comment)
https://core.trac.wordpress.org/ticket/45371
https://stackoverflow.com/a/25509350