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
Open
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions patches/@wordpress+block-editor+14.11.0.patch
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ index aa2e9a5..f2d8481 100644
expandOnMobile: true,
headerTitle: __('Add a block'),
renderToggle: this.renderToggle,
diff --git a/node_modules/@wordpress/block-editor/build-module/components/provider/index.js b/node_modules/@wordpress/block-editor/build-module/components/provider/index.js
index e89fa49..0f14ae8 100644
--- a/node_modules/@wordpress/block-editor/build-module/components/provider/index.js
+++ b/node_modules/@wordpress/block-editor/build-module/components/provider/index.js
@@ -104,7 +104,7 @@ export const ExperimentalBlockEditorProvider = withRegistryProvider(props => {
export const BlockEditorProvider = props => {
return /*#__PURE__*/_jsx(ExperimentalBlockEditorProvider, {
...props,
- stripExperimentalSettings: true,
+ stripExperimentalSettings: false,
Comment on lines +21 to +22
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.

children: props.children
});
};
20 changes: 0 additions & 20 deletions patches/@wordpress+editor+14.16.0.patch

This file was deleted.

7 changes: 2 additions & 5 deletions patches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ Existing patches should be described and justified here.

### `@wordpress/block-editor`

Expose an `open` prop on the `Inserter` component, allowing toggling the inserter visibility via the quick inserter's "Browse all" button.

### `@wordpress/editor`

Revert https://github.com/WordPress/gutenberg/pull/64892 and export the private `useBlockEditorSettings` API as it is required for GutenbergKit's use of the block editor settings. This can be removed once GutenbergKit resides in the Gutenberg repository, where we can access all the necessary APIs—both public and private.
- Expose an `open` prop on the `Inserter` component, allowing toggling the inserter visibility via the quick inserter's "Browse all" button.
- Disable `stripExperimentalSettings` in the `BlockEditorProvider` component so that the Patterns and Media inserter tabs function.
74 changes: 36 additions & 38 deletions src/components/editor/index.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* WordPress dependencies
*/
import { useEntityBlockEditor } from '@wordpress/core-data';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { store as editorStore, EditorProvider } from '@wordpress/editor';
import { useRef } from '@wordpress/element';

/**
Expand All @@ -17,18 +16,12 @@ import { useHostBridge } from './use-host-bridge';
import { useHostExceptionLogging } from './use-host-exception-logging';
import { useEditorSetup } from './use-editor-setup';
import { useMediaUpload } from './use-media-upload';
import { useGBKitSettings } from './use-gbkit-settings';
import { unlock } from '../../lock-unlock';
import TextEditor from '../text-editor';

/**
* @typedef {import('../utils/bridge').Post} Post
*/

const { ExperimentalBlockEditorProvider: BlockEditorProvider } = unlock(
blockEditorPrivateApis
);

/**
* Entry component rendering the editor and surrounding UI.
*
Expand All @@ -47,42 +40,45 @@ export default function Editor( { post, children, hideTitle } ) {
useEditorSetup( post );
useMediaUpload();

const [ postBlocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
post.type,
{ id: post.id }
);

const settings = useGBKitSettings( post );
const { isReady, mode, isRichEditingEnabled, currentPost } = useSelect(
( select ) => {
const {
__unstableIsEditorReady,
getEditorSettings,
getEditorMode,
} = select( editorStore );
const editorSettings = getEditorSettings();
const { getEntityRecord } = select( coreStore );
const _currentPost = getEntityRecord(
'postType',
post.type,
post.id
);

const { isReady, mode, isRichEditingEnabled } = useSelect( ( select ) => {
const { __unstableIsEditorReady, getEditorSettings, getEditorMode } =
select( editorStore );
const editorSettings = getEditorSettings();

return {
// TODO(Perf): The `__unstableIsEditorReady` selector is insufficient as
// it does not account for post type loading, which is first referenced
// within the post title component render. This results in the post title
// popping in after the editor mounted. The web editor does not experience
// this issue because the post type is loaded for "mode" selection before
// the editor is mounted.
isReady: __unstableIsEditorReady(),
mode: getEditorMode(),
isRichEditingEnabled: editorSettings.richEditingEnabled,
};
}, [] );
return {
// TODO(Perf): The `__unstableIsEditorReady` selector is insufficient as
// it does not account for post type loading, which is first referenced
// within the post title component render. This results in the post title
// popping in after the editor mounted. The web editor does not experience
// this issue because the post type is loaded for "mode" selection before
// the editor is mounted.
isReady: __unstableIsEditorReady(),
mode: getEditorMode(),
isRichEditingEnabled: editorSettings.richEditingEnabled,
currentPost: _currentPost,
};
},
[ post.id, post.type ]
);

if ( ! isReady ) {
return null;
}

return (
<div className="gutenberg-kit-editor" ref={ editorRef }>
<BlockEditorProvider
value={ postBlocks }
onInput={ onInput }
onChange={ onChange }
<EditorProvider
post={ currentPost }
settings={ settings }
useSubRegistry={ false }
>
Expand All @@ -100,7 +96,9 @@ export default function Editor( { post, children, hideTitle } ) {
) }

{ children }
</BlockEditorProvider>
</EditorProvider>
</div>
);
}

const settings = {};
66 changes: 0 additions & 66 deletions src/components/editor/use-gbkit-settings.js

This file was deleted.

6 changes: 5 additions & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ function initializeEditor() {
dispatch( editorStore ).updateEditorSettings( editorSettings );
} );

dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
const preferenceDispatch = dispatch( preferencesStore );
preferenceDispatch.setDefaults( 'core', {
fixedToolbar: true,
} );
Comment on lines +43 to +45
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.

preferenceDispatch.setDefaults( 'core/edit-post', {
themeStyles,
} );

Expand Down
6 changes: 5 additions & 1 deletion src/remote.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ async function initalizeRemoteEditor() {
dispatch( editorStore ).updateEditorSettings( editorSettings );
} );

dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
const preferenceDispatch = dispatch( preferencesStore );
preferenceDispatch.setDefaults( 'core', {
fixedToolbar: true,
} );
preferenceDispatch.setDefaults( 'core/edit-post', {
themeStyles,
} );

Expand Down
7 changes: 5 additions & 2 deletions src/utils/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,21 @@ export function getPost() {
if ( post ) {
return {
id: post.id,
type: post.type || 'post',
status: post.status,
title: { raw: decodeURIComponent( post.title ) },
content: { raw: decodeURIComponent( post.content ) },
type: post.type || 'post',
};
}

// Since we don't use the auto-save functionality, draft posts need to have an ID.
// We assign a temporary ID of -1.
return {
id: -1,
type: 'post',
status: 'auto-draft',
id: -1,
title: { raw: '' },
content: { raw: '' },
Comment on lines +181 to +182
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.

};
}

Expand Down