-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add keycodes file #940
Add keycodes file #940
Conversation
webpack.config.js
Outdated
@@ -13,6 +13,7 @@ const config = { | |||
blocks: './blocks/index.js', | |||
editor: './editor/index.js', | |||
element: './element/index.js', | |||
keycodes: './keycodes/index.js', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want a separate module for keycodes
? This raises the question of whether we'd need a generic module: "utils", "common" or something like that.
General +1, but I don't think this should be its own Webpack entry point. Maybe ok for See also #929 (introduced conflicts here, but should simplify the needed |
Sorry, I don't know how web pack is configured etc. I just added it everywhere so that it works somehow. :) The point is that the codes are in one file. Where should the file be placed? cc @aduth |
This branch will now fail. @nylen I don't know how to do add a new nodule now. |
If not |
There is a way :) https://github.com/webpack-contrib/expose-loader/blob/master/index.js#L16 |
Extending this["wp"] = this["wp"] || {}; this["wp"]["utils"] =
/******/ (function(modules) { // webpackBootstrap
// ... To demonstrate that this works well, load Gutenberg in the browser and inspect diff --git a/index.php b/index.php
index fcca4ad..c5bfa68 100644
--- a/index.php
+++ b/index.php
@@ -491,7 +491,7 @@ function gutenberg_scripts_and_styles( $hook ) {
wp_enqueue_script(
'wp-editor',
plugins_url( 'editor/build/index.js', __FILE__ ),
- array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils' ),
+ array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils', 'word-count' ),
filemtime( plugin_dir_path( __FILE__ ) . 'editor/build/index.js' ),
true // enqueue in the footer.
); |
The one thing I still don't like about how this PR is built is that importing from diff --git a/blocks/editable/index.js b/blocks/editable/index.js
index 6d2a16a..7d91c2c 100644
--- a/blocks/editable/index.js
+++ b/blocks/editable/index.js
@@ -11,7 +11,7 @@ import 'element-closest';
* WordPress dependencies
*/
import { Toolbar } from 'components';
-import { BACKSPACE, DELETE } from 'utils/keycodes';
+import { keycodes } from 'utils';
/**
* Internal dependencies
@@ -219,11 +219,11 @@ export default class Editable extends wp.element.Component {
onKeyDown( event ) {
if (
this.props.onMerge && (
- ( event.keyCode === BACKSPACE && this.isStartOfEditor() ) ||
- ( event.keyCode === DELETE && this.isEndOfEditor() )
+ ( event.keyCode === keycodes.BACKSPACE && this.isStartOfEditor() ) ||
+ ( event.keyCode === keycodes.DELETE && this.isEndOfEditor() )
)
) {
- const forward = event.keyCode === DELETE;
+ const forward = event.keyCode === keycodes.DELETE;
this.onChange();
this.props.onMerge( forward );
event.preventDefault();
@@ -232,7 +232,7 @@ export default class Editable extends wp.element.Component {
}
onKeyUp( { keyCode } ) {
- if ( keyCode === BACKSPACE ) {
+ if ( keyCode === keycodes.BACKSPACE ) {
this.onSelectionChange();
}
} I hope we can find a way to preserve the |
I don't really mind any way we can avoid it. If it has to be from import { keycodes } from 'utils';
/* ... */
const { BACKSPACE, DELETE } = keycodes; |
But yeah, I think it would be good to find a way to keep importing form |
Or simply: /**
* WordPress dependencies
*/
const { BACKSPACE, DELETE } = wp.utils.keycodes; ... and wait for a future where WP has less global variables. 😭 |
@nylen Just noticed: it doesn't do |
Ah right, good catch! So this means that the webpack version needs to be loaded first. We can accomplish this by adding
Edit: or maybe this is reversed, I am getting confused again :( In any case this is an example of something that needs a simple integration test to make sure it works both ways. See #939. |
Yeah, either that, or exporting each thing we add to utils to |
@nylen I was thinking about this more last night, and I'm not sure I get what you're saying. They're not really global right? They're namespaced under |
Even worse, in addition to plugins that can conflict with it, there's also an entire npm repository, and more. :) I think we should always export to something like |
I dislike the name "utils" because it's way too tempting a dumping ground for miscellany in lieu of better organization or naming. I'm speaking generally more to this specific case. This one is tricky, as is often the case. If I were to suggest anything, maybe something on the basis of events, keyboard, formatting, or constants (last being my pick). Or maybe we don't need it at all. I don't feel particularly strongly about this, just thinking through the precedent we set. Maybe it's inevitable/preferable we have this folder than stress to be overly imaginative with our groupings (example). To modularity point, I agree we'll need something to distinguish WordPress dependencies from others. |
I chose |
Proposal to add key codes file