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

initializeEditor doesn’t work #49409

Open
htmgarcia opened this issue Mar 28, 2023 · 9 comments
Open

initializeEditor doesn’t work #49409

htmgarcia opened this issue Mar 28, 2023 · 9 comments
Labels
[Package] Edit Post /packages/edit-post [Type] Bug An existing feature does not function as intended

Comments

@htmgarcia
Copy link

Description

reinitializeEditor was deprecated and initializeEditor doesn’t work as used to be.
Can we still use this method to modify editor settings after wp.domReady?

The error I get in console when calling initializeEditor is:
"Warning: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before. Instead, call root.render() on the existing root instead if you want to update it."

wp.editPost.initializeEditor( 'editor', 'post', 99, modified_settings, [] );

Step-by-step reproduction instructions

  1. Use WP 6.2 RC-4
  2. In wp-config.php set define( 'SCRIPT_DEBUG', true );
  3. Open a post edit
  4. Open browser console
  5. Execute the line of code below replacing 99 with the ID of the post you're editing, and modified_settings with the return of wp.data.select('core/editor').getEditorSettings()

wp.editPost.initializeEditor( 'editor', 'post', 99, modified_settings, [] );

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 6.2 RC-4 with and without Gutenberg 15.4

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@Mamaduka
Copy link
Member

Hi, @htmgarcia

Can you provide more details on why you're using the initializeEditor?

Plugins shouldn't use the method; the only reason it's publicly exported is for WP to initialize the post editor.

@Mamaduka Mamaduka added [Status] Needs More Info Follow-up required in order to be actionable. [Package] Edit Post /packages/edit-post labels Mar 29, 2023
@htmgarcia
Copy link
Author

Hi @Mamaduka,
I use initializeEditor to modify allowedBlockTypes. I wonder if there is an alternative method to achieve the same result? Tried updateEditorSettings with no luck.

@github-actions
Copy link

Help us move this issue forward. This issue is being marked stale since it has no activity after 15 days of requesting more information. Please add info requested so we can help move the issue forward. Note: The triage policy is to close stale issues that need more info and no response after 2 weeks.

@github-actions github-actions bot added the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Apr 14, 2023
@Mamaduka Mamaduka removed the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Apr 14, 2023
@Mamaduka
Copy link
Member

@jsnajdr, I remember you debugged the update settings race conditions for React 18 update (#46467 (comment)). Do you have any recommended way for dispatching this action from third-party plugins?

@jsnajdr
Copy link
Member

jsnajdr commented Apr 18, 2023

Tried updateEditorSettings with no luck.

@htmgarcia I think this is a timing issue. When the initializeEditor function is called with a settings parameter, it mounts the editor React UI with the EditorProvider component, and there is an effect in that component that synchronizes the settings to the data store, calling updateEditorSettings:

useEffect( () => {
updateEditorSettings( settings );
}, [ settings ] );

That effect doesn't run synchronously, but a little bit later. If your plugin calls updateEditorSettings too soon, as it probably does, your changes will be overwritten by that effect.

So, the solution is to call updateEditorSettings only after that effect runs. But I don't know at this moment how to do it simply, reliably and elegantly.

@htmgarcia
Copy link
Author

So, the solution is to call updateEditorSettings only after that effect runs. But I don't know at this moment how to do it simply, reliably and elegantly.

Hi @jsnajdr,
thanks for jumping into the conversation. In the meanwhile I wrapped updateEditorSettings with a time delay:

setTimeout( function() {
    wp.data.dispatch('core/editor').updateEditorSettings({ allowedBlockTypes: ['core/paragraph','core/heading'] });
}, 3000 );

@jsnajdr
Copy link
Member

jsnajdr commented Apr 19, 2023

In the meanwhile I wrapped updateEditorSettings with a time delay:

Yes that works, although it's bad that you're forced to do things like this.

I'm exploring a solution where initializeEditor would update the settings immediately, instead of postponing it to an effect.

Not this:

function initializeEditor( settings ) {
  render( <Editor settings={ settings } /> );
}

function Editor( { settings } ) {
  useEffect( () => {
    updateEditorSettings( settings );
  }, [ settings ] );
}

But this:

function initializeEditor( settings ) {
  dispatch( editorStore ).updateEditorSettings( settings );
  render( <Editor /> );
}

Then, when your plugin wants to call updateEditorSettings, initializeEditor has already run and updated the settings. There is no scheduled effect that would overwrite your changes.

In practice it's not so simple and there's some amount of complexity to untangle, but in principle it should work.

@htmgarcia
Copy link
Author

@jsnajdr your proposal sounds promising. I really appreciated!

@github-actions
Copy link

github-actions bot commented May 5, 2023

Help us move this issue forward. This issue is being marked stale since it has no activity after 15 days of requesting more information. Please add info requested so we can help move the issue forward. Note: The triage policy is to close stale issues that need more info and no response after 2 weeks.

@github-actions github-actions bot added the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label May 5, 2023
@Mamaduka Mamaduka removed [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. [Status] Needs More Info Follow-up required in order to be actionable. labels May 5, 2023
@jordesign jordesign added the [Type] Bug An existing feature does not function as intended label Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Edit Post /packages/edit-post [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

4 participants