块编辑器开发文档

💡 云策文档标注

概述

本文档介绍了 WordPress 核心编辑站点(core/edit-site)命名空间下的选择器和操作,用于管理站点编辑器的状态和功能,包括用户权限、模板操作、编辑器模式等。

关键要点

  • 选择器:提供状态查询功能,如 getCanUserCreateMedia 检查用户媒体创建权限,getEditorMode 获取当前编辑模式,isListViewOpened 检查列表视图是否打开。
  • 操作:包括状态修改功能,如 closeGeneralSidebar 关闭侧边栏,revertTemplate 恢复模板到主题原始文件,setIsSaveViewOpened 设置保存面板的打开状态。
  • 已弃用函数:多个函数(如 getCurrentTemplateNavigationPanelSubMenu)已标记为弃用,建议开发者使用替代方法。

代码示例

// 示例:使用 getCanUserCreateMedia 选择器
const canCreateMedia = getCanUserCreateMedia(state);
console.log(canCreateMedia); // 输出布尔值

// 示例:使用 revertTemplate 操作
revertTemplate(template, { allowUndo: true });

注意事项

  • 注意已弃用函数,避免在代码中使用,以保持兼容性和最佳实践。
  • 操作函数通常返回动作对象,用于在 Redux 状态管理中分发。

📄 原文内容

Namespace: core/edit-site.

Selectors

getCanUserCreateMedia

Returns whether the current user can create media or not.

Parameters

  • state Object: Global application state.

Returns

  • Object: Whether the current user can create media or not.

getCurrentTemplateNavigationPanelSubMenu

Deprecated

getCurrentTemplateTemplateParts

Deprecated

Returns the template parts and their blocks for the current edited template.

Parameters

  • state Object: Global application state.

Returns

  • Array: Template parts and their blocks in an array.

getEditedPostContext

Deprecated

Returns the edited post’s context object.

Parameters

  • state Object: Global application state.

Returns

  • Object: Page.

getEditedPostId

Deprecated

Returns the ID of the currently edited template or template part.

Parameters

  • state Object: Global application state.

Returns

  • ?string: Post ID.

getEditedPostType

Deprecated

Returns the current edited post type (wp_template or wp_template_part).

Parameters

  • state Object: Global application state.

Returns

  • ?TemplateType: Template type.

getEditorMode

Returns the current editing mode.

Parameters

  • state Object: Global application state.

Returns

  • string: Editing mode.

getHomeTemplateId

Deprecated

getNavigationPanelActiveMenu

Deprecated

getPage

Deprecated

Returns the current page object.

Parameters

  • state Object: Global application state.

Returns

  • Object: Page.

getReusableBlocks

Returns any available Reusable blocks.

Parameters

  • state Object: Global application state.

Returns

  • Array: The available reusable blocks.

getSettings

Returns the site editor settings.

Parameters

  • state Object: Global application state.

Returns

  • Object: Settings.

hasPageContentFocus

Deprecated

Whether or not the editor allows only page content to be edited.

Returns

  • boolean: Whether or not focus is on editing page content.

isFeatureActive

Deprecated

Returns whether the given feature is enabled or not.

Parameters

  • state Object: Global application state.
  • featureName string: Feature slug.

Returns

  • boolean: Is active.

isInserterOpened

Deprecated

Returns true if the inserter is opened.

Parameters

  • state Object: Global application state.

Returns

  • boolean: Whether the inserter is opened.

isListViewOpened

Returns true if the list view is opened.

Parameters

  • state Object: Global application state.

Returns

  • boolean: Whether the list view is opened.

isNavigationOpened

Deprecated

isPage

Deprecated

Whether or not the editor has a page loaded into it.

Related

  • setPage

Parameters

  • state Object: Global application state.

Returns

  • boolean: Whether or not the editor has a page loaded into it.

isSaveViewOpened

Returns the current opened/closed state of the save panel.

Parameters

  • state Object: Global application state.

Returns

  • boolean: True if the save panel should be open; false if closed.

Actions

addTemplate

Deprecated

Action that adds a new template and sets it as the current template.

Parameters

  • template Object: The template.

Returns

  • Object: Action object used to set the current template.

closeGeneralSidebar

Action that closes the sidebar.

openGeneralSidebar

Action that opens an editor sidebar.

Parameters

  • name ?string: Sidebar name to be opened.

openNavigationPanelToMenu

Deprecated

Opens the navigation panel and sets its active menu at the same time.

removeTemplate

Action that removes a template.

Parameters

  • template Object: The template object.

revertTemplate

Reverts a template to its original theme-provided file.

Parameters

  • template Object: The template to revert.
  • options [Object]:
  • options.allowUndo [boolean]: Whether to allow the user to undo reverting the template. Default true.

setEditedEntity

Deprecated

Action that sets an edited entity.

Parameters

  • postType string: The entity’s post type.
  • postId string: The entity’s ID.
  • context Object: The entity’s context.

Returns

  • Object: Action object.

setEditedPostContext

Deprecated

Set’s the current block editor context.

Parameters

  • context Object: The context object.

Returns

  • Object: Action object.

setHasPageContentFocus

Sets whether or not the editor allows only page content to be edited.

Parameters

  • hasPageContentFocus boolean: True to allow only page content to be edited, false to allow template to be edited.

setHomeTemplateId

Deprecated

setIsInserterOpened

Deprecated

Returns an action object used to open/close the inserter.

Parameters

  • value boolean|Object: Whether the inserter should be opened (true) or closed (false).

setIsListViewOpened

Deprecated

Returns an action object used to open/close the list view.

Parameters

  • isOpen boolean: A boolean representing whether the list view should be opened or closed.

setIsNavigationPanelOpened

Deprecated

Sets whether the navigation panel should be open.

setIsSaveViewOpened

Sets whether the save view panel should be open.

Parameters

  • isOpen boolean: If true, opens the save view. If false, closes it. It does not toggle the state, but sets it directly.

setNavigationMenu

Deprecated

Action that sets a navigation menu.

Parameters

  • navigationMenuId string: The Navigation Menu Post ID.

Returns

  • Object: Action object.

setNavigationPanelActiveMenu

Deprecated

Action that sets the active navigation panel menu.

Returns

  • Object: Action object.

setPage

Deprecated

Resolves the template for a page and displays both. If no path is given, attempts to use the postId to generate a path like ?p=${ postId }.

Returns

  • Object: Action object.

setTemplate

Action that sets a template, optionally fetching it from REST API.

Returns

  • Object: Action object.

setTemplatePart

Deprecated

Action that sets a template part.

Parameters

  • templatePartId string: The template part ID.

Returns

  • Object: Action object.

switchEditorMode

Deprecated

Triggers an action used to switch editor mode.

Parameters

  • mode string: The editor mode.

toggleDistractionFree

Deprecated

Action that toggles Distraction free mode. Distraction free mode expects there are no sidebars, as due to the z-index values set, you can’t close sidebars.

toggleFeature

Dispatches an action that toggles a feature flag.

Parameters

  • featureName string: Feature name.

updateSettings

Returns an action object used to update the settings.

Parameters

  • settings Object: New settings.

Returns

  • Object: Action object.