块编辑器开发文档

PluginDocumentSettingPanel

💡 云策文档标注

概述

PluginDocumentSettingPanel 是一个 SlotFill,允许注册用于编辑文档设置的 UI 面板。它提供了核心和自定义面板的创建、访问和移除功能。

关键要点

  • PluginDocumentSettingPanel 是一个 SlotFill,用于在文档设置侧边栏添加自定义面板。
  • 可用属性包括 name、className、title 和 icon,用于定义面板的标识、样式和显示。
  • 核心面板(如 Summary、Categories 等)有预定义的名称,自定义面板名称需包含插件命名空间。
  • 可以通过 toggleEditorPanelOpened 和 toggleEditorPanelEnabled 函数以编程方式切换面板的打开或启用状态。
  • 可以使用 removeEditorPanel 函数从管理界面移除面板。

代码示例

import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/editor';

const PluginDocumentSettingPanelDemo = () => (
    <PluginDocumentSettingPanel
        name="custom-panel"
        title="Custom Panel"
        className="custom-panel"
    >
        Custom Panel Contents
    </PluginDocumentSettingPanel>
);

registerPlugin( 'plugin-document-setting-panel-demo', {
    render: PluginDocumentSettingPanelDemo,
    icon: 'palmtree',
} );

注意事项

  • 自定义面板的名称必须包含插件命名空间,例如 'plugin-name/panel-name',以便正确访问和操作。
  • 使用 toggleEditorPanelOpened 或 removeEditorPanel 时,需确保传递完整的面板名称,包括命名空间。

📄 原文内容

This SlotFill allows registering a UI to edit Document settings.

Available Props

  • name string: A string identifying the panel.
  • className string: An optional class name added to the sidebar body.
  • title string: Title displayed at the top of the sidebar.
  • icon (string|Element): The Dashicon icon slug string, or an SVG WP element.

Example

import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/editor';

const PluginDocumentSettingPanelDemo = () => (
    <PluginDocumentSettingPanel
        name="custom-panel"
        title="Custom Panel"
        className="custom-panel"
    >
        Custom Panel Contents
    </PluginDocumentSettingPanel>
);

registerPlugin( 'plugin-document-setting-panel-demo', {
    render: PluginDocumentSettingPanelDemo,
    icon: 'palmtree',
} );

Accessing a panel programmatically

Core and custom panels can be accessed programmatically using their panel name. The core panel names are:

  • Summary Panel: post-status
  • Categories Panel: taxonomy-panel-category
  • Tags Panel: taxonomy-panel-post_tag
  • Featured Image Panel: featured-image
  • Excerpt Panel: post-excerpt
  • DiscussionPanel: discussion-panel

Custom panels are namespaced with the plugin name that was passed to registerPlugin.
In order to access the panels using function such as toggleEditorPanelOpened or toggleEditorPanelEnabled be sure to prepend the namespace.

To programmatically toggle panels, use the following:

import { useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';

const Example = () => {
    const { toggleEditorPanelOpened } = useDispatch( editorStore );
    return (
        <Button
            variant="primary"
            onClick={ () => {
                // Toggle the Summary panel
                toggleEditorPanelOpened( 'post-status' );

                // Toggle the Custom Panel introduced in the example above.
                toggleEditorPanelOpened(
                    'plugin-document-setting-panel-demo/custom-panel'
                );
            } }
        >
            Toggle Panels
        </Button>
    );
};

It is also possible to remove panels from the admin using the removeEditorPanel function by passing the name of the registered panel.

import { useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';

const Example = () => {
    const { removeEditorPanel } = useDispatch( editorStore );
    return (
        <Button
            variant="primary"
            onClick={ () => {
                // Remove the Featured Image panel.
                removeEditorPanel( 'featured-image' );

                // Remove the Custom Panel introduced in the example above.
                removeEditorPanel(
                    'plugin-document-setting-panel-demo/custom-panel'
                );
            } }
        >
            Toggle Panels
        </Button>
    );
};