-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Allow the Preview Menu to be extendible by plugins #25430
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f238cfe
Allow Preview menu to be extendible by plugins: WIP
mreishus fcc3670
PluginPreview{MenuItem,}: Change external interface "deviceName" -> "…
mreishus 0d9cbe2
Merge branch 'master' into add/preview-menu-slot-3
mreishus 0fa2be2
Merge branch 'master' into add/preview-menu-slot-3
mreishus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 37 additions & 0 deletions
37
docs/designers-developers/developers/slotfills/plugin-preview-menu-item.md
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,37 @@ | ||
# PluginPreviewMenuItem | ||
|
||
This is designed to be used alongside the PluginPreview. | ||
|
||
- PluginPreviewMenuItem: Adds a menu item to the "Preview" menu. | ||
- PluginPreview: Renders the main content area when that menu item is chosen. | ||
|
||
Each of these takes 2 props, `previewId`, and `children`. | ||
|
||
- `previewId` - A string that serves as an internal identifier for your | ||
preview. Must match across PluginPreviewMenuItem and PluginPreview. | ||
- `children` - What gets rendered in that spot. | ||
|
||
## Example | ||
|
||
```js | ||
import { registerPlugin } from '@wordpress/plugins'; | ||
import { PluginPreview, PluginPreviewMenuItem } from '@wordpress/block-editor'; | ||
import { Fragment } from '@wordpress/element'; | ||
|
||
const PluginPreviewTest = () => ( | ||
<Fragment> | ||
<PluginPreviewMenuItem previewId="preview-custom-1"> | ||
Custom Preview 1 Menu Text | ||
</PluginPreviewMenuItem> | ||
<PluginPreview previewId="preview-custom-1"> | ||
<div> | ||
<h4>Custom Preview 1 Content</h4> | ||
</div> | ||
</PluginPreview> | ||
</Fragment> | ||
); | ||
|
||
registerPlugin( 'plugin-preview-test', { | ||
render: PluginPreviewTest, | ||
} ); | ||
``` |
37 changes: 37 additions & 0 deletions
37
docs/designers-developers/developers/slotfills/plugin-preview.md
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,37 @@ | ||
# PluginPreview | ||
|
||
This is designed to be used alongside the PluginPreviewMenuItem. | ||
|
||
- PluginPreviewMenuItem: Adds a menu item to the "Preview" menu. | ||
- PluginPreview: Renders the main content area when that menu item is chosen. | ||
|
||
Each of these takes 2 props, `previewId`, and `children`. | ||
|
||
- `previewId` - A string that serves as an internal identifier for your | ||
preview. Must match across PluginPreviewMenuItem and PluginPreview. | ||
- `children` - What gets rendered in that spot. | ||
|
||
## Example | ||
|
||
```js | ||
import { registerPlugin } from '@wordpress/plugins'; | ||
import { PluginPreview, PluginPreviewMenuItem } from '@wordpress/block-editor'; | ||
import { Fragment } from '@wordpress/element'; | ||
|
||
const PluginPreviewTest = () => ( | ||
<Fragment> | ||
<PluginPreviewMenuItem previewId="preview-custom-1"> | ||
Custom Preview 1 Menu Text | ||
</PluginPreviewMenuItem> | ||
<PluginPreview previewId="preview-custom-1"> | ||
<div> | ||
<h4>Custom Preview 1 Content</h4> | ||
</div> | ||
</PluginPreview> | ||
</Fragment> | ||
); | ||
|
||
registerPlugin( 'plugin-preview-test', { | ||
render: PluginPreviewTest, | ||
} ); | ||
``` |
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
54 changes: 54 additions & 0 deletions
54
packages/block-editor/src/components/preview-options/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,54 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Fill, MenuItem } from '@wordpress/components'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { check } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { coreDeviceTypes } from '../index'; | ||
|
||
/** | ||
* Component used by a plugin to define the contents of a menu item that | ||
* selects a "custom preview". The children of this component will be displayed | ||
* inside the preview menu. Typically a single string is good enough. | ||
* | ||
* @param {Object} props Component properties. | ||
* @param {string} props.previewId The internal name of this custom preview. Must match the _previewId_ given to `PluginPreview`. | ||
* @param {WPElement} props.children Children to be rendered. | ||
*/ | ||
export default function PluginPreviewMenuItem( { | ||
children, | ||
previewId, | ||
...props | ||
} ) { | ||
const { | ||
__experimentalSetPreviewDeviceType: setPreviewDeviceType, | ||
} = useDispatch( 'core/edit-post' ); | ||
|
||
const { deviceType } = useSelect( | ||
( select ) => ( { | ||
deviceType: select( | ||
'core/edit-post' | ||
).__experimentalGetPreviewDeviceType(), | ||
} ), | ||
[] | ||
); | ||
|
||
if ( coreDeviceTypes.includes( previewId ) ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Fill name="core/block-editor/plugin-preview-menu" { ...props }> | ||
<MenuItem | ||
onClick={ () => setPreviewDeviceType( previewId ) } | ||
icon={ deviceType === previewId && check } | ||
> | ||
{ children } | ||
</MenuItem> | ||
</Fill> | ||
); | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/block-editor/src/components/preview-options/plugin-preview/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,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Fill } from '@wordpress/components'; | ||
|
||
/** | ||
* Component used by a plugin to define the contents of a "custom | ||
* preview". The children of this component will be displayed in the main editor | ||
* screen when this "custom preview" is chosen from the preview menu. | ||
* | ||
* @param {Object} props Component properties. | ||
* @param {string} props.previewId The internal name of this custom preview. Must match the _previewId_ given to `PluginPreviewMenuItem`. | ||
* @param {WPElement} props.children Children to be rendered. | ||
*/ | ||
export default function PluginPreview( { children, previewId, ...props } ) { | ||
return ( | ||
<Fill | ||
name={ 'core/block-editor/plugin-preview/' + previewId } | ||
{ ...props } | ||
> | ||
{ children } | ||
</Fill> | ||
); | ||
} |
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
25 changes: 25 additions & 0 deletions
25
packages/edit-post/src/components/visual-editor/visual-editor-or-plugin-preview.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,25 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { Slot } from '@wordpress/components'; | ||
import { coreDeviceTypes } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import VisualEditor from './index'; | ||
|
||
function VisualEditorOrPluginPreview() { | ||
const deviceType = useSelect( ( select ) => { | ||
return select( 'core/edit-post' ).__experimentalGetPreviewDeviceType(); | ||
}, [] ); | ||
|
||
if ( ! coreDeviceTypes.includes( deviceType ) ) { | ||
return ( | ||
<Slot name={ 'core/block-editor/plugin-preview/' + deviceType } /> | ||
); | ||
} | ||
return <VisualEditor />; | ||
} | ||
export default VisualEditorOrPluginPreview; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The block editor is a generic package that may be used outside WordPress, while the core/edit-post is a package to edit regular WordPress posts. I think we should avoid having the block-editor depending or querying data from core/edit-post.
I guess things will also not work as expected for edit-site scree where we don't have a core/edit-post module?
The device type is passed to the PreviewOptions as a prop. Could we rely on that prop instead?
Not directly related to this PR, but I think it makes sense to move the preview functionality under the WordPress/interface package. It is common to edit-post and edit-site. It has functionality like the singleEnableItems that can be used to store which preview is active. I guess the slot / fill here could also be abstracted as action item packages/interface/src/components/action-item/README.md.
Regarding the API, we are exposing PluginPreview, PluginPreviewMenuItem on the @wordpress/block-editor' package. As far as I know, the bock-editor does not expose slot & fills besides the ones used by blocks. All the plugin related API's are exposed under edit-post/edit-site. I guess for this case it may make sense too to make PluginPreview, PluginPreviewMenuItem under @wordpress/edit-site/edit-post. I guess we could implement the slot fills under WordPress/interface, and then on edit-site and edit-post, we would simply have wrappers that pass a specific scope. Similar to what we do for the sidebar and menu item slots.
The PreviewOptions component would have a prop for additional preview options that edit-site and edit-post use.
cc: @gziolo and @youknowriad In case you have a different option.