-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Add extensibility to PreviewOptions v2 (#64644)
* add plugin-preview-dropdown-item * Add slot * export PluginPreviewDropdownItem * add registerPlugin example Co-authored-by: Greg Ziółkowski <[email protected]> * example import from @wordpress/editor Co-authored-by: Greg Ziółkowski <[email protected]> * rename to PluginPreviewMenuItem * add tests tests --------- Co-authored-by: Greg Ziółkowski <[email protected]> Co-authored-by: lezama <[email protected]> Co-authored-by: Mamaduka <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: simison <[email protected]> Co-authored-by: fabiankaegy <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: fumikito <[email protected]> Co-authored-by: westonruter <[email protected]>
- Loading branch information
1 parent
9c62555
commit 9d4f918
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
( function () { | ||
const { __ } = wp.i18n; | ||
const { registerPlugin } = wp.plugins; | ||
const PluginPreviewMenuItem = wp.editor.PluginPreviewMenuItem; | ||
const el = wp.element.createElement; | ||
|
||
function CustomPreviewMenuItem() { | ||
return el( PluginPreviewMenuItem, {}, __( 'Custom Preview' ) ); | ||
} | ||
|
||
registerPlugin( 'custom-preview-menu-item', { | ||
render: CustomPreviewMenuItem, | ||
} ); | ||
} )(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/editor/src/components/plugin-preview-menu-item/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { compose } from '@wordpress/compose'; | ||
import { MenuItem } from '@wordpress/components'; | ||
import { withPluginContext } from '@wordpress/plugins'; | ||
import { ActionItem } from '@wordpress/interface'; | ||
|
||
/** | ||
* Renders a menu item in the Preview dropdown, which can be used as a button or link depending on the props provided. | ||
* The text within the component appears as the menu item label. | ||
* | ||
* @param {Object} props Component properties. | ||
* @param {string} [props.href] When `href` is provided, the menu item is rendered as an anchor instead of a button. It corresponds to the `href` attribute of the anchor. | ||
* @param {WPBlockTypeIconRender} [props.icon=inherits from the plugin] The icon to be rendered to the left of the menu item label. Can be a Dashicon slug or an SVG WP element. | ||
* @param {Function} [props.onClick] The callback function to be executed when the user clicks the menu item. | ||
* @param {...*} [props.other] Any additional props are passed through to the underlying MenuItem component. | ||
* | ||
* @example | ||
* ```jsx | ||
* import { __ } from '@wordpress/i18n'; | ||
* import { PluginPreviewMenuItem } from '@wordpress/editor'; | ||
* import { external } from '@wordpress/icons'; | ||
* | ||
* function onPreviewClick() { | ||
* // Handle preview action | ||
* } | ||
* | ||
* const ExternalPreviewMenuItem = () => ( | ||
* <PreviewDropdownMenuItem | ||
* icon={ external } | ||
* onClick={ onPreviewClick } | ||
* > | ||
* { __( 'Preview in new tab' ) } | ||
* </PreviewDropdownMenuItem> | ||
* ); | ||
* registerPlugin( 'external-preview-menu-item', { | ||
* render: ExternalPreviewMenuItem, | ||
* } ); | ||
* ``` | ||
* | ||
* @return {Component} The rendered menu item component. | ||
*/ | ||
export default compose( | ||
withPluginContext( ( context, ownProps ) => { | ||
return { | ||
as: ownProps.as ?? MenuItem, | ||
icon: ownProps.icon || context.icon, | ||
name: 'core/plugin-preview-menu', | ||
}; | ||
} ) | ||
)( ActionItem ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters