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

refactor: Avoid private useBlockEditorSettings API #108

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

dcalhoun
Copy link
Member

@dcalhoun dcalhoun commented Mar 5, 2025

What?

Remove use of @wordpress/block-editor's private, non-exported useBlockEditorSettings function. This reintroduces changes originally implemented in #24, but extended to address a disappearing block toolbar and empty Patterns/Media inserter tabs—effectively reverting #50.

Why?

First, using private APIs is inadvisable and not a sound, long-term solution.

Second, we were actually patching @wordpress/block-editor to export useBlockEditorSettings as a private API. While patching "works" for the editor bundled with the app, we cannot patch the "remote" editor—i.e., one that relies upon WordPress packages sourced from a user's site.

How?

First, remove the @wordpress/block-editor patch.

Second, we now rely upon @wordpress/editor's EditorProvider rather than @wordpress/block-editor's BlockEditorProvider. The positive is that this approach negates our need to access useBlockEditorSettings as its usage is now managed by the EditorProvider. The negative is that the EditorProvider strips other experimental features—e.g., populating content for the Patterns/Media inserter tabs—by default, which leads to the final change...

Lastly, we introduce a @wordpress/block-editor patch disabling stripExperimentalSettings. This means the Patterns/Media tabs now function as desired in the bundled editor. As noted previously, this patch cannot be applied to the remote editor, meaning the tabs do not populate in that environment.

We are trading one patch for another, but the outcome is the remote editor, at least, functions, albeit without a few features requiring patches.

Testing Instructions

1 - Formatting options remain present within the block toolbar

  1. Start the development server.
  2. Open the editor in your desktop browser.
  3. Insert a Paragraph block.
  4. Type a couple of words into the block.
  5. Move your mouse cursor.
  6. Verify the block toolbar remains populated with rich text formatting
    options—e.g., bold, italic, etc.

2 - The Patterns/Media tab populate with content from the current site

  1. Open the editor within the WordPress mobile app.
  2. Open the block inserter.
  3. Tap the Patterns/Media tab.
  4. Verify the tab is populated with content from the current site.

Accessibility Testing Instructions

N/A, no user-facing changes.

Screenshots or screencast

N/A, no user-facing changes.

dcalhoun added 4 commits March 5, 2025 16:11
The `useBlockEditorSettings` function is not exported, even as a private
API. Using it is brittle, at best, and the approach is not possible when
sourcing WordPress package scripts from remote sites.

These changes still patch the `@wordpress/block-editor` package to (1)
allow the "quick inserter" to open the primary inserter and (2) enable
the Media tab in the inserter. Long-term, we should remove these patches
and replace them with changes to core.
When a post object is unavailable, we must ensure the raw title and
content fields have fallback values. Otherwise, the host bridge logic
throws while referencing values on an undefined object.
Now that we rely upon `EditorProvider` over `BlockEditorProvider`, we
can no longer configure the former with the `hasFixedToolbar` prop.
Instead, we must rely upon the preference store configuration.
We now render `EditorProvider` rather than `BlockEditorProvider`, so we
no longer configure the latter with explicit settings. We also do not
have explicit settings we need to pass to the former, so we pass an
empty object to avoid exceptions from referencing values on an undefined
object.
@dcalhoun dcalhoun added the [Type] Task Issues or PRs that have been broken down into an individual action to take label Mar 5, 2025
Comment on lines +21 to +22
- stripExperimentalSettings: true,
+ stripExperimentalSettings: false,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patch manually enables populating the Patterns and Media tabs within the inserter. As outlined in the PR description, this is a workaround that works for the local editor, but not the remote editor.

Ideally, we seek the Gutenberg project exposing the APIs we need to avoid this patch.

Comment on lines +43 to +45
preferenceDispatch.setDefaults( 'core', {
fixedToolbar: true,
} );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we can no longer set hasFixedToolbar on the BlockEditorProvider, we must rely upon the preferences store.

Comment on lines +181 to +182
title: { raw: '' },
content: { raw: '' },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensures draft posts do not throw exceptions when referencing values on undefined objects—this only occurred in development environments.

@dcalhoun dcalhoun marked this pull request as ready for review March 5, 2025 23:59
@dcalhoun dcalhoun requested a review from kean March 5, 2025 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Task Issues or PRs that have been broken down into an individual action to take
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant