Skip to content
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

Remove block assets enqueuing from editor document #54250

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
$styles,
'wp-edit-post',
gutenberg_url( 'build/edit-post/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-commands' ),
array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-block-library-editor', 'wp-block-library', 'wp-commands' ),
$version
);
$styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
Expand Down Expand Up @@ -369,6 +369,19 @@
);
$styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );

// Essentially the same as wp-edit-blocks, but without dependencies.
// wp-edit-blocks is for use in the editor iframe, while
// wp-block-library-editor is for use outside the iframe. Editor styles are
// expected to be enqueued both inside and outside the iframe.
gutenberg_override_style(
$styles,
'wp-block-library-editor',
gutenberg_url( 'build/block-library/editor.css' ),
array(),
$version
);
$styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );

gutenberg_override_style(
$styles,
'wp-nux',
Expand Down Expand Up @@ -409,7 +422,7 @@
$styles,
'wp-edit-site',
gutenberg_url( 'build/edit-site/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-commands' ),
array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-block-library-editor', 'wp-commands' ),
$version
);
$styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
Expand Down Expand Up @@ -594,3 +607,29 @@
// Enqueue stored styles.
add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' );
add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 );

/*
* This action should be removed in core when backporting. We no longer want to
* enqueue all assets added through enqueue_block_assets, they are added
* separately to the editor through block editor settings.
* See https://github.com/WordPress/wordpress-develop/blob/362624176cba41a2dda57c3e89031aa6c3e4decf/src/wp-includes/default-filters.php#L573.
*/
remove_action( 'admin_enqueue_scripts', 'wp_common_block_scripts_and_styles' );

/*
* We DO want to enqueue EDITOR scripts and styles.
*/
add_action( 'admin_enqueue_scripts', function() {

Check failure on line 622 in lib/client-assets.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Opening parenthesis of a multi-line function call must be the last content on the line

Check failure on line 622 in lib/client-assets.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Only one argument is allowed per line in a multi-line function call

Check failure on line 622 in lib/client-assets.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after FUNCTION keyword; 0 found
$block_registry = WP_Block_Type_Registry::get_instance();
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {

Check warning on line 624 in lib/client-assets.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Unused variable $block_name.
// Editor styles.
foreach ( $block_type->editor_style_handles as $editor_style_handle ) {
wp_enqueue_style( $editor_style_handle );
}

// Editor scripts.
foreach ( $block_type->editor_script_handles as $editor_script_handle ) {
wp_enqueue_script( $editor_script_handle );
}
}
} );

Check failure on line 635 in lib/client-assets.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Closing parenthesis of a multi-line function call must be on a line by itself
36 changes: 36 additions & 0 deletions packages/block-editor/src/components/block-canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* WordPress dependencies
*/
import { useMergeRefs } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -13,6 +15,39 @@ import WritingFlow from '../writing-flow';
import { useMouseMoveTypingReset } from '../observe-typing';
import { useClipboardHandler } from '../copy-handler';
import { useBlockSelectionClearer } from '../block-selection-clearer';
import { store as blockEditorStore } from '../../store';

/**
* This component is for rendering block assets when the editor canvas is not
* iframed. It might include assets that have already been enqueued for the
* editor, so we need to filter them out.
*/
function UnIframedBlockAssets() {
const blockAssets = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings()
.__unstableResolvedAssets;
}, [] );
const { styles: _styles = '', scripts: _scripts = '' } = blockAssets || {};
const assetsHtml = _styles + _scripts;
const filteredAssetsHtml = useMemo( () => {
if ( ! assetsHtml ) return '';
const doc = document.implementation.createHTMLDocument( '' );
doc.body.innerHTML = assetsHtml;
// Remove assets already enqueued in the editor document.
doc.querySelectorAll( '[id]' ).forEach( ( node ) => {
if ( document.getElementById( node.id ) ) {
node.remove();
}
} );
return doc.body.innerHTML;
}, [ assetsHtml ] );

if ( ! filteredAssetsHtml ) return null;

return (
<div dangerouslySetInnerHTML={ { __html: filteredAssetsHtml } }></div>
);
}

export function ExperimentalBlockCanvas( {
shouldIframe = true,
Expand All @@ -34,6 +69,7 @@ export function ExperimentalBlockCanvas( {
if ( ! shouldIframe ) {
return (
<>
<UnIframedBlockAssets />
<EditorStyles
styles={ styles }
scope=".editor-styles-wrapper"
Expand Down
14 changes: 14 additions & 0 deletions packages/block-editor/src/components/block-tools/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@
}
}

// The black plus that shows up on the right side of an empty paragraph block, or the initial appender
// that exists only on empty documents.
.block-editor-block-list__empty-block-inserter.block-editor-block-list__empty-block-inserter,
.block-editor-default-block-appender .block-editor-inserter {
position: absolute;
top: 0;
right: 0;
line-height: 0;

&:disabled {
display: none;
}
}

.block-editor-block-list__insertion-point-inserter .block-editor-inserter__toggle.components-button.has-icon {
background: var(--wp-admin-theme-color);
&:hover {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,6 @@
}
}

// The black plus that shows up on the right side of an empty paragraph block, or the initial appender
// that exists only on empty documents.
.block-editor-block-list__empty-block-inserter.block-editor-block-list__empty-block-inserter,
.block-editor-default-block-appender .block-editor-inserter {
position: absolute;
top: 0;
right: 0;
line-height: 0;

&:disabled {
display: none;
}
}


/**
* Fixed position appender.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export function useCompatibilityStyles() {
return accumulator;
}

// This is the same stylesheet as wp-edit-block but without
// its dependencies, so we don't need to add it.
if ( ownerNode.id === 'wp-block-library-editor-css' ) {
return accumulator;
}

// Don't try to add styles without ID. Styles enqueued via the WP dependency system will always have IDs.
if ( ! ownerNode.id ) {
return accumulator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ function enqueue_inner_blocks_allowed_blocks_script() {
true
);
}
add_action( 'enqueue_block_assets', 'enqueue_inner_blocks_allowed_blocks_script' );
add_action( 'enqueue_block_editor_assets', 'enqueue_inner_blocks_allowed_blocks_script' );
2 changes: 1 addition & 1 deletion packages/e2e-tests/plugins/meta-attribute-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ function enqueue_test_meta_attribute_block() {
true
);
}
add_action( 'enqueue_block_assets', 'enqueue_test_meta_attribute_block' );
add_action( 'enqueue_block_editor_assets', 'enqueue_test_meta_attribute_block' );
Loading