本文档介绍了 WordPress 核心编辑小工具模块的命名空间、选择器和操作,用于管理小工具区域的块插入、状态控制和保存功能。
import { __ } from '@wordpress/i18n';
import { store as widgetStore } from '@wordpress/edit-widgets';
import { useSelect } from '@wordpress/data';
const ExampleComponent = () => {
const isSavingLocked = useSelect(
( select ) => select( widgetStore ).isWidgetSavingLocked(),
[]
);
return isSavingLocked ? (
<p>{ __( 'Widget saving is locked' ) }</p>
) : (
<p>{ __( 'Widget saving is not locked' ) }</p>
);
}; Namespace: core/edit-widgets.
Returns true if a block can be inserted into a widget area.
Parameters
Array: The open state of the widget areas.string: The name of the block being inserted.Returns
boolean: True if the block can be inserted in a widget area.Returns all edited widget area entity records.
Returns
Object[]: List of edited widget area entity records.Gets whether the widget area is opened.
Parameters
Array: The open state of the widget areas.string: The clientId of the widget area.Returns
boolean: True if the widget area is open.Given a child client id, returns the parent widget area block.
Parameters
string: The client id of a block in a widget area.Returns
WPBlock: The widget area block.Returns all blocks representing reference widgets.
Parameters
string: Optional. If given, only reference widgets with this name will be returned.Returns
Array: List of all blocks representing reference widgetsReturns API widget data for a particular widget ID.
Parameters
number: Widget ID.Returns
Object: API widget data for a particular widget ID.Returns widgetArea containing a block identify by given widgetId
Parameters
string: The ID of the widget.Returns
Object: Containing widget area.Returns all API widget areas.
Returns
Object[]: API List of widget areas.Returns all API widgets.
Returns
Object[]: API List of widgets.Returns true if the inserter is opened.
Parameters
Object: Global application state.Returns
boolean: Whether the inserter is opened.Returns true if the list view is opened.
Parameters
Object: Global application state.Returns
boolean: Whether the list view is opened.Returns true if any widget area is currently being saved.
Returns
boolean: True if any widget area is currently being saved. False otherwise.Returns whether widget saving is locked.
Usage
import { __ } from '@wordpress/i18n';
import { store as widgetStore } from '@wordpress/edit-widgets';
import { useSelect } from '@wordpress/data';
const ExampleComponent = () => {
const isSavingLocked = useSelect(
( select ) => select( widgetStore ).isWidgetSavingLocked(),
[]
);
return isSavingLocked ? (
<p>{ __( 'Widget saving is locked' ) }</p>
) : (
<p>{ __( 'Widget saving is not locked' ) }</p>
);
};
Parameters
Object: Global application state.Returns
boolean: Is locked.Returns an action object signalling that the user closed the sidebar.
Returns
Object: Action creator.Returns an action object used to signal that widget saving is locked.
Usage
import { store as widgetStore } from '@wordpress/edit-widgets';
import { useDispatch } from '@wordpress/data';
const ExampleComponent = () => {
const { lockWidgetSaving } = useDispatch( widgetStore );
return (
<Button onClick={ () => lockWidgetSaving( 'lockName' ) }>
{ __( 'Lock Widget Saving' ) }
</Button>
);
};
Parameters
string: The lock name.Returns
Object: Action objectAction that handles moving a block between widget areas
Parameters
string: The clientId of the block to move.string: The id of the widget area to move the block to.Persists a stub post with given ID to core data store. The post is meant to be in-memory only and shouldn’t be saved via the API.
Parameters
string: Post ID.Array: Blocks the post should consist of.Returns
Object: The post object.Converts all the blocks from edited widget areas into widgets, and submits a batch request to save everything at once.
Creates a snackbar notice on either success or error.
Returns
Function: An action creator.Converts all the blocks from a widget area specified by ID into widgets, and submits a batch request to save everything at once.
Parameters
string: ID of the widget area to process.Returns
Function: An action creator.Converts all the blocks from specified widget areas into widgets, and submits a batch request to save everything at once.
Parameters
Object[]: Widget areas to save.Returns
Function: An action creator.Returns an action object used to open/close the inserter.
Parameters
boolean|Object: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object.string: The root client ID to insert at.number: The index to insert at.Returns
Object: Action object.Returns an action object used to open/close the list view.
Parameters
boolean: A boolean representing whether the list view should be opened or closed.Returns
Object: Action object.Sets the open state of the widget area.
Parameters
string: The clientId of the widget area.boolean: Whether the widget area should be opened.Returns
Object: Action.Sets the open state of all the widget areas.
Parameters
Object: The open states of all the widget areas.Returns
Object: Action.Sets the clientId stored for a particular widgetId.
Parameters
number: Client id.number: Widget id.Returns
Object: Action.Returns an action object used to signal that widget saving is unlocked.
Usage
import { store as widgetStore } from '@wordpress/edit-widgets';
import { useDispatch } from '@wordpress/data';
const ExampleComponent = () => {
const { unlockWidgetSaving } = useDispatch( widgetStore );
return (
<Button onClick={ () => unlockWidgetSaving( 'lockName' ) }>
{ __( 'Unlock Widget Saving' ) }
</Button>
);
};
Parameters
string: The lock name.Returns
Object: Action object