-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Allow styles to be changed dynamically through editor settings #52767
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/e2e-tests/plugins/iframed-enqueue-block-editor-settings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Gutenberg Test Iframed enqueue block editor settings | ||
* Plugin URI: https://github.com/WordPress/gutenberg | ||
* Author: Gutenberg Team | ||
* | ||
* @package gutenberg-test-iframed-iframed-enqueue-block-editor-settings | ||
*/ | ||
|
||
add_action( | ||
'block_editor_settings_all', | ||
function( $settings ) { | ||
$settings['styles'][] = array( 'css' => 'p { border: 1px solid red }' ); | ||
return $settings; | ||
} | ||
); |
65 changes: 65 additions & 0 deletions
65
packages/e2e-tests/specs/editor/plugins/iframed-enqueue-block-editor-settings.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
activatePlugin, | ||
createNewPost, | ||
deactivatePlugin, | ||
canvas, | ||
activateTheme, | ||
} from '@wordpress/e2e-test-utils'; | ||
|
||
async function getComputedStyle( context, selector, property ) { | ||
return await context.evaluate( | ||
( sel, prop ) => | ||
window.getComputedStyle( document.querySelector( sel ) )[ prop ], | ||
selector, | ||
property | ||
); | ||
} | ||
|
||
describe( 'iframed block editor settings styles', () => { | ||
beforeEach( async () => { | ||
// Activate the empty theme (block based theme), which is iframed. | ||
await activateTheme( 'emptytheme' ); | ||
await activatePlugin( | ||
'gutenberg-test-iframed-enqueue-block-editor-settings' | ||
); | ||
await createNewPost(); | ||
} ); | ||
|
||
afterEach( async () => { | ||
await deactivatePlugin( | ||
'gutenberg-test-iframed-enqueue-block-editor-settings' | ||
); | ||
await activateTheme( 'twentytwentyone' ); | ||
} ); | ||
|
||
it( 'should load styles added through block editor settings', async () => { | ||
await page.waitForSelector( 'iframe[name="editor-canvas"]' ); | ||
// Expect a red border (added in PHP). | ||
expect( await getComputedStyle( canvas(), 'p', 'border-color' ) ).toBe( | ||
'rgb(255, 0, 0)' | ||
); | ||
|
||
await page.evaluate( () => { | ||
const settings = window.wp.data | ||
.select( 'core/editor' ) | ||
.getEditorSettings(); | ||
wp.data.dispatch( 'core/editor' ).updateEditorSettings( { | ||
...settings, | ||
styles: [ | ||
...settings.styles, | ||
{ | ||
css: 'p { border-width: 2px; }', | ||
}, | ||
], | ||
} ); | ||
} ); | ||
|
||
// Expect a 2px border (added in JS). | ||
expect( await getComputedStyle( canvas(), 'p', 'border-width' ) ).toBe( | ||
'2px' | ||
); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
hasThemeStyles
flag corresponds to a user preferences that is changeable from the preferences modal. That means that any change to the "settings" that is done byupdateEditorSettings
call from plugin authors will override any change that this preference might have.Meaning we might want to avoid doing anything here to the "styles" property and instead move the logic of this preference to within the
EditorProvider
component (potentially need to make hasThemeStyles a new setting in editorSettings though).Maybe there are other options, but it does raise the question about whether "styles" was ever meant to be "editable".
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.
You're right, the styles should be filtered after they are passed down to the
EditorProvider
. We can simply move the filtering toLayout
, because that's a component within that provider. I fixed this in 00fde60. I can add an extra e2e test to ensure that this works correctly.