Skip to content

Commit b328877

Browse files
committed
Gutenberg: move Gutenberg extension registration into init
We want to avoid putting any Calypso custom code into edit-post folder. New editor initialization function seems like a better place to do it.
1 parent e3343d2 commit b328877

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

client/gutenberg/editor/api-middleware/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const debugMiddleware = ( options, next ) => {
2929

3030
// Rewrite default API paths to match WP.com equivalents. Note that
3131
// passed apiNamespace will be prepended to the replaced path.
32-
const wpcomPathMappingMiddleware = ( options, next, siteSlug ) => {
32+
export const wpcomPathMappingMiddleware = ( options, next, siteSlug ) => {
3333
// wp/v2 namespace mapping
3434
//
3535
// Path rewrite example:

client/gutenberg/editor/edit-post/editor.js

-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* External dependencies
33
*/
44
import React from 'react';
5-
import { isEnabled } from 'config';
65

76
/**
87
* WordPress dependencies
@@ -16,10 +15,6 @@ import { EditorProvider, ErrorBoundary } from '@wordpress/editor';
1615
import Layout from './components/layout';
1716
import './store';
1817

19-
if ( isEnabled( 'gutenberg/block/jetpack-preset' ) ) {
20-
require( 'gutenberg/extensions/presets/jetpack/editor.js' );
21-
}
22-
2318
function Editor( { settings, hasFixedToolbar, post, overridePost, onError, ...props } ) {
2419
if ( ! post ) {
2520
return null;

client/gutenberg/editor/init.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { once } from 'lodash';
1010
* Internal dependencies
1111
*/
1212
import { applyAPIMiddleware } from './api-middleware';
13+
import { isEnabled } from 'config';
1314
import debugFactory from 'debug';
1415

1516
const debug = debugFactory( 'calypso:gutenberg' );
@@ -20,8 +21,16 @@ const WPCOM_UNSUPPORTED_CORE_BLOCKS = [
2021
'core/file', // see D19851 for more details.
2122
];
2223

23-
// We need to ensure that his function is executed only once to avoid
24-
// registering core blocks and applying middleware twice.
24+
const loadA8CExtensions = () => {
25+
require( '../extensions/classic-block/editor' );
26+
27+
if ( isEnabled( 'gutenberg/block/jetpack-preset' ) ) {
28+
require( 'gutenberg/extensions/presets/jetpack/editor.js' );
29+
}
30+
};
31+
32+
// We need to ensure that his function is executed only once to avoid duplicate
33+
// block registration, API middleware application etc.
2534
export const initGutenberg = once( ( userId, siteSlug ) => {
2635
debug( 'Starting Gutenberg editor initialization...' );
2736

@@ -36,18 +45,14 @@ export const initGutenberg = once( ( userId, siteSlug ) => {
3645

3746
// Avoid using top level imports for this since they will statically
3847
// initialize core-data before required plugins are loaded.
39-
const blockLibrary = require( '@wordpress/block-library' );
40-
const blocks = require( '@wordpress/blocks' );
48+
const { registerCoreBlocks } = require( '@wordpress/block-library' );
49+
const { unregisterBlockType, setFreeformContentHandlerName } = require( '@wordpress/blocks' );
4150

4251
debug( 'Registering core blocks' );
43-
blockLibrary.registerCoreBlocks();
52+
registerCoreBlocks();
4453

4554
debug( 'Removing core blocks that are not yet supported in Calypso' );
46-
WPCOM_UNSUPPORTED_CORE_BLOCKS.forEach( blockName => blocks.unregisterBlockType( blockName ) );
47-
48-
debug( 'Registering Calypso Classic Block handler' );
49-
require( '../extensions/classic-block/editor' );
50-
blocks.setFreeformContentHandlerName( 'a8c/classic' );
55+
WPCOM_UNSUPPORTED_CORE_BLOCKS.forEach( blockName => unregisterBlockType( blockName ) );
5156

5257
// Needed for list block indent/outdent functionality
5358
debug( 'Loading required TinyMCE plugins' );
@@ -59,6 +64,12 @@ export const initGutenberg = once( ( userId, siteSlug ) => {
5964
debug( 'Applying API middleware' );
6065
applyAPIMiddleware( siteSlug );
6166

67+
debug( 'Loading A8C editor extensions' );
68+
loadA8CExtensions();
69+
70+
debug( 'Registering Calypso Classic Block handler' );
71+
setFreeformContentHandlerName( 'a8c/classic' );
72+
6273
debug( 'Gutenberg editor initialization complete.' );
6374

6475
return require( 'gutenberg/editor/main' ).default;

client/gutenberg/editor/main.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import QueryPostTypes from 'components/data/query-post-types';
1717
import { createAutoDraft, requestSitePost, requestGutenbergDemoContent } from 'state/data-getters';
1818
import { getHttpData } from 'state/data-layer/http-data';
1919
import { translate } from 'i18n-calypso';
20-
import './hooks'; // Load hooks that are needed for integrating Calypso's Media Modal
20+
import './hooks'; // Needed for integrating Calypso's media library (and other hooks)
2121

2222
class GutenbergEditor extends Component {
2323
componentDidMount() {

0 commit comments

Comments
 (0)