refactor: Migrate Blockly.onKeyDown out of blockly.js #5476
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.
The basics
goog_modulegoog_moduleconversion guide
npm test.The details
Resolves
Part of #5026 and #5208
Proposed Changes
Moves
Blockly.onKeyDownto a local method oninject.js. This method was@packageand only called fromBlockly.injectto handle all keydown events on the page. This probably isn't something external developers would have been calling since we register it for them (and they shouldn't be since it's package) so I did not create an alias for it. Also, this is not marked as a breaking change since this should not affect code outside of Blockly.Since the function is now not exported from its module, it is not accessible outside of this file. For the unit tests, I changed the tests to dispatch an event on the document instead of calling the event handler directly.
Additional Information
In order to get the unit tests to work with
dispatchEvent, I had to create an actualKeyboardEventobject instead of the fake implementation previously used in the test_helpers. You can't just change the target type in the real event like this, so I removed that part. The only time it was ever used was for the "Not called when focus is an HTML input test" but that test was actually already broken. The event was created by passing inthis.eventas the first argument, which should be a keycode. So the keycode was notESCsohideChaffwas not called for that reason, and not because of the input. I've fixed this by dispatching the event on an actual html input, which correctly does not call the keyboard shortcut.I also had to change the shortcut_registry tests to use an actual keycode instead of a random string, otherwise the event would not be created correctly.