-
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 experimental flag and enable widgets screen in customizer #28618
Add experimental flag and enable widgets screen in customizer #28618
Conversation
Size Change: -949 B (0%) Total Size: 1.37 MB
ℹ️ View Unchanged
|
fc3403b
to
76aa918
Compare
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.
I didn't test this locally but I looked through the diff and it's a great start! 🏅
The big problem I see with it right now is that it looks like the block editor embedded into the Customizer uses the /wp/widgets
endpoints to load and save widgets.
Instead we should be using the wp.customize
API to load and save widgets from the setting that we're adding here. This lets us use the same draft/publish flow that already exists in the Customizer.
Or, as a start, we could embed an empty block editor in the Customizer and then follow-up with further PRs which hook it up to wp.customize
.
packages/edit-widgets/src/blocks/legacy-widget/edit/widget-preview.js
Outdated
Show resolved
Hide resolved
66049cf
to
ab226e2
Compare
|
||
this.subscribers = new Set(); | ||
|
||
this._handleSettingChange = this._handleSettingChange.bind( this ); |
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.
Maybe we should bite the bullet and add an observable library lol. It's come up a few times in some PRs I've been a part of. Not right now though.
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.
I think maybe we can further simplify this a lot to the point that we don't need to introduce an observable library though 🤔 . Let's see how it goes 😆 .
import { serialize, parse, createBlock } from '@wordpress/blocks'; | ||
import { useState, useEffect, useCallback, useRef } from '@wordpress/element'; | ||
|
||
function blockToWidget( block, existingWidget = null ) { |
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.
These functions already exist in edit-widgets
. Noting that we'll want to do some DRYIng up eventually. Again, not right now though.
const widget = sidebar.getWidget( widgetId ); | ||
const block = widgetToBlock( widget ); | ||
state.blocks.push( block ); | ||
state.widgetIds[ block.clientId ] = widgetId; |
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.
Can probably simplify how this hook works using __internalWidgetId
similar to #28379. Not right now though.
/** | ||
* Initializes the widgets block editor in the customizer. | ||
*/ | ||
export function initialize() { |
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.
We'll eventually want to DRY a lot of this up too. Some of it already exists in edit-widgets
. Not right now though.
@noisysocks Good catch! Seems like we need to delay the initializing until the DOM is ready. Fixed in 59d36e5. |
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.
Obviously still a lot of work to do but this is a great start. I tested a few different themes and I see the block editor loading for each of the widget areas. I also confirmed that nothing changes if the experiment is off.
Thanks @kevin940726! Let's get iterating! 💪
Below comment is also updated in the PR description. Why are blocks now just JSON-stringified strings?It's because that we can't handle blocks serialization correctly yet. We are using the customizer API to get the contents of the widget, which looks something like: {
"id": "block-11",
"idBase": "block",
"number": 11,
"instance": {
"encoded_serialized_instance": "YToxOntzOjc6ImNvbnRlbnQiO3M6NTQ6IjwhLS0gd3A6cGFyYWdyYXBoIC0tPgo8cD5wMjwvcD4KPCEtLSAvd3A6cGFyYWdyYXBoIC0tPiI7fQ==",
"title": "",
"is_widget_customizer_js_value": true,
"instance_hash_key": "19e32b0073d55e11b26ab957abcf9386"
}
} Notice that the content of the widget is a base64-encoded serialized string, which can be unserialize via |
I think what we should do is update Legacy Widget to use |
Description
First stab to bring the widget screen to the customizer, and add a experimental flag defaults to false. Based on #27668 and the widgets-customize branch.
Note that this is a POC, the customizer is not going to be working properly.
Why are blocks now just JSON-stringified strings?
It's because that we can't handle blocks serialization correctly yet. We are using the customizer API to get the contents of the widget, which looks something like:
Notice that the content of the widget is a base64-encoded serialized string, which can be unserialize via
sanitize_widget_instance
. However, there's no equivalent of that in JS. We need to think of another way to do this. Hence, as a fallback, we're just serializing it to string for now.How has this been tested?
Screenshots
Types of changes
New feature
Checklist: