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

Enable the command palette feature everywhere in WordPress #54515

Open
wants to merge 11 commits into
base: trunk
Choose a base branch
from
11 changes: 8 additions & 3 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,17 @@
break;

case 'wp-edit-post':
array_push( $dependencies, 'media-models', 'media-views', 'postbox' );
array_push( $dependencies, 'media-models', 'media-views', 'postbox', 'wp-core-commands' );
break;

case 'wp-edit-site':
array_push( $dependencies, 'wp-dom-ready' );
array_push( $dependencies, 'wp-dom-ready', 'wp-core-commands' );
break;

case 'wp-edit-widgets':
array_push($dependencies, 'wp-core-commands');

Check failure on line 224 in lib/client-assets.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 224 in lib/client-assets.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found
break;

case 'wp-preferences':
array_push( $dependencies, 'wp-preferences-persistence' );
break;
Expand Down Expand Up @@ -418,7 +423,7 @@
$styles,
'wp-edit-widgets',
gutenberg_url( 'build/edit-widgets/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-patterns', 'wp-reusable-blocks', 'wp-widgets' ),
array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-patterns', 'wp-reusable-blocks', 'wp-widgets', 'wp-commands' ),
$version
);
$styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
Expand Down
52 changes: 52 additions & 0 deletions lib/experimental/commands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Experiment to enable Command Palette everywhere in WordPress.
*
* @package gutenberg
*/

/**
* Enqueue the command palette everywhere in WordPress.
*
* @package gutenberg
*/
function gutenberg_enqueue_commands() {
if ( ! is_user_logged_in() ) {
return;
}

wp_enqueue_style( 'wp-commands' );
wp_enqueue_script( 'wp-core-commands' );

wp_add_inline_script(
'wp-core-commands',
<<<'EOT'
const { useCommands } = wp.coreCommands.unlock(wp.coreCommands.privateApis);
const { RouterProvider } = wp.coreCommands.unlock(wp.router.privateApis);
const mountPoint = document.createElement('div');

mountPoint.id = 'wp-commands';
document.body.appendChild(mountPoint);

const root = wp.element.createRoot(mountPoint);

function CommandMenuWrapper() {
useCommands();

return (
wp.element.createElement(
RouterProvider,
null,
wp.element.createElement(wp.commands.CommandMenu, null)
)
);
}

root.render(
wp.element.createElement(CommandMenuWrapper, null)
);
EOT

Check failure on line 48 in lib/experimental/commands.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Heredoc/nowdoc with an indented closing marker is not supported in PHP 7.2 or earlier.
);
}

add_action( 'wp_print_scripts', 'gutenberg_enqueue_commands', 1 );
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably move this to lib/wordpress-6.4 (or 6.5) depending on when the PR lands.

1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.4/kses.php';

// Experimental features.
require __DIR__ . '/experimental/commands.php';
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/blocks.php';
require __DIR__ . '/experimental/navigation-theme-opt-in.php';
Expand Down
4 changes: 4 additions & 0 deletions packages/core-commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ _This package assumes that your code will run in an **ES2015+** environment. If

Undocumented declaration.

### unlock

Undocumented declaration.

<!-- END TOKEN(Autogenerated API docs) -->

## Contributing to this package
Expand Down
1 change: 1 addition & 0 deletions packages/core-commands/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { unlock } from './lock-unlock';
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately this is not something we should export but it makes "private APIs" public for any third-party plugin and this shouldn't be the case.

Maybe we should just export the CommandWrapper component like we used to do? 🤔

Copy link
Author

Choose a reason for hiding this comment

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

I exported a commands-menu-wrapper component from the core-commands packages and used a small inline script to register it on the page in d381a0d. I honestly would have preferred to this wrapper in the commands package instead but the circular dependency doesn't make this simple.

export { privateApis } from './private-apis';
5 changes: 0 additions & 5 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { useMemo } from '@wordpress/element';
import { SlotFillProvider } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as preferencesStore } from '@wordpress/preferences';
import { CommandMenu } from '@wordpress/commands';
import { privateApis as coreCommandsPrivateApis } from '@wordpress/core-commands';

/**
* Internal dependencies
Expand All @@ -26,10 +24,8 @@ import { unlock } from './lock-unlock';
import useCommonCommands from './hooks/commands/use-common-commands';

const { ExperimentalEditorProvider } = unlock( editorPrivateApis );
const { useCommands } = unlock( coreCommandsPrivateApis );

function Editor( { postId, postType, settings, initialEdits, ...props } ) {
useCommands();
useCommonCommands();
const {
hasFixedToolbar,
Expand Down Expand Up @@ -165,7 +161,6 @@ function Editor( { postId, postType, settings, initialEdits, ...props } ) {
{ ...props }
>
<ErrorBoundary>
<CommandMenu />
<EditorInitialization postId={ postId } />
<Layout />
</ErrorBoundary>
Expand Down
9 changes: 2 additions & 7 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ import { __ } from '@wordpress/i18n';
import { useState, useRef } from '@wordpress/element';
import { NavigableRegion } from '@wordpress/interface';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import {
CommandMenu,
privateApis as commandsPrivateApis,
} from '@wordpress/commands';
import { privateApis as commandsPrivateApis } from '@wordpress/commands';
import { store as preferencesStore } from '@wordpress/preferences';
import {
privateApis as blockEditorPrivateApis,
useBlockCommands,
} from '@wordpress/block-editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
// eslint-disable-next-line no-unused-vars
import { privateApis as coreCommandsPrivateApis } from '@wordpress/core-commands';

/**
Expand All @@ -55,7 +53,6 @@ import { useEditModeCommands } from '../../hooks/commands/use-edit-mode-commands
import PageMain from '../page-main';
import { useIsSiteEditorLoading } from './hooks';

const { useCommands } = unlock( coreCommandsPrivateApis );
const { useCommandContext } = unlock( commandsPrivateApis );
const { useLocation } = unlock( routerPrivateApis );
const { useGlobalStyle } = unlock( blockEditorPrivateApis );
Expand All @@ -66,7 +63,6 @@ export default function Layout() {
// This ensures the edited entity id and type are initialized properly.
useInitEditedEntityFromURL();
useSyncCanvasModeWithURL();
useCommands();
useEditModeCommands();
useCommonCommands();
useBlockCommands();
Expand Down Expand Up @@ -168,7 +164,6 @@ export default function Layout() {

return (
<>
<CommandMenu />
<KeyboardShortcutsRegister />
<KeyboardShortcutsGlobal />
{ fullResizer }
Expand Down
Loading