-
Notifications
You must be signed in to change notification settings - Fork 81
Block editor: ensure meta data is saved before publishing. #1763
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
Changes from all commits
ddb3d66
d974380
1258cb2
df411f0
0e23a29
00d337d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: fixed | ||
|
||
Block editor: avoid conflicts with other plugins that save meta data. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => '1bd009e5ff8d85217279'); | ||
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => 'fc80a746c1daa1dc0de8'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { PluginDocumentSettingPanel, PluginPreviewMenuItem } from '@wordpress/editor'; | ||
import { PluginDocumentSettingPanel, PluginPreviewMenuItem, store as editorStore } from '@wordpress/editor'; | ||
import { registerPlugin } from '@wordpress/plugins'; | ||
import { TextControl, RadioControl, RangeControl, __experimentalText as Text, Tooltip } from '@wordpress/components'; | ||
import { Icon, globe, people, external } from '@wordpress/icons'; | ||
|
@@ -21,20 +21,53 @@ const notAllowed = ( | |
</SVG> | ||
); | ||
|
||
/** | ||
* Custom hook to update metadata in the post editor. | ||
* | ||
* @param {string} metaKey The key of the metadata to update. | ||
* @param {string} postType The type of post to update the metadata for. | ||
* @returns {[string, (value: string) => void]} The current value of the metadata and a function to update it. | ||
*/ | ||
function useSetMeta( metaKey, postType ) { | ||
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' ); | ||
|
||
const setValue = ( value ) => { | ||
setMeta( { ...meta, [ metaKey ]: value } ); | ||
}; | ||
|
||
return [ meta?.[ metaKey ], setValue ]; | ||
} | ||
|
||
/** | ||
* Editor plugin for ActivityPub settings in the block editor. | ||
* | ||
* @returns {JSX.Element|null} The settings panel for ActivityPub or null for sync blocks. | ||
*/ | ||
const EditorPlugin = () => { | ||
const postType = useSelect( ( select ) => select( 'core/editor' ).getCurrentPostType(), [] ); | ||
const [ meta, setMeta ] = useEntityProp( 'postType', postType || 'default', 'meta' ); | ||
|
||
const postType = useSelect( ( select ) => select( editorStore ).getCurrentPostType(), [] ); | ||
// Don't show when editing sync blocks. | ||
if ( 'wp_block' === postType ) { | ||
return null; | ||
} | ||
const { maxImageAttachments = 4 } = useOptions(); | ||
|
||
const [ contentWarning, setContentWarning ] = useSetMeta( 'activitypub_content_warning', postType ); | ||
const [ maxImageAttachments, setMaxImageAttachments ] = useSetMeta( 'activitypub_max_image_attachments', postType ); | ||
const [ contentVisibility, setContentVisibility ] = useSetMeta( 'activitypub_content_visibility', postType ); | ||
|
||
const handleContentWarningChange = ( value ) => { | ||
setContentWarning( value ); | ||
}; | ||
|
||
const handleMaxImageAttachmentsChange = ( value ) => { | ||
setMaxImageAttachments( value ); | ||
}; | ||
|
||
const handleVisibilityChange = ( value ) => { | ||
setContentVisibility( value ); | ||
}; | ||
|
||
Comment on lines
+57
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the setter functions in the onChange handlers directly? I'm not sure the wrappers are needed? The setters' function names are also less ambiguous. |
||
const { maxImageAttachments: defaultMaxImageAttachments = 4 } = useOptions(); | ||
|
||
const labelStyling = { | ||
verticalAlign: 'middle', | ||
gap: '4px', | ||
|
@@ -69,10 +102,8 @@ const EditorPlugin = () => { | |
> | ||
<TextControl | ||
label={ __( 'Content Warning', 'activitypub' ) } | ||
value={ meta?.activitypub_content_warning } | ||
onChange={ ( value ) => { | ||
setMeta( { ...meta, activitypub_content_warning: value } ); | ||
} } | ||
value={ contentWarning ?? '' } | ||
onChange={ handleContentWarningChange } | ||
placeholder={ __( 'Optional content warning', 'activitypub' ) } | ||
help={ __( | ||
'Content warnings do not change the content on your site, only in the fediverse.', | ||
|
@@ -84,10 +115,8 @@ const EditorPlugin = () => { | |
|
||
<RangeControl | ||
label={ __( 'Maximum Image Attachments', 'activitypub' ) } | ||
value={ meta?.activitypub_max_image_attachments ?? maxImageAttachments } | ||
onChange={ ( value ) => { | ||
setMeta( { ...meta, activitypub_max_image_attachments: value } ); | ||
} } | ||
value={ maxImageAttachments ?? defaultMaxImageAttachments } | ||
onChange={ handleMaxImageAttachmentsChange } | ||
min={ 0 } | ||
max={ 10 } | ||
help={ __( | ||
|
@@ -104,7 +133,7 @@ const EditorPlugin = () => { | |
"This adjusts the visibility of a post in the fediverse, but note that it won't affect how the post appears on the blog.", | ||
'activitypub' | ||
) } | ||
selected={ meta?.activitypub_content_visibility || 'public' } | ||
selected={ contentVisibility || 'public' } | ||
options={ [ | ||
{ | ||
label: enhancedLabel( | ||
|
@@ -134,9 +163,7 @@ const EditorPlugin = () => { | |
value: 'local', | ||
}, | ||
] } | ||
onChange={ ( value ) => { | ||
setMeta( { ...meta, activitypub_content_visibility: value } ); | ||
} } | ||
onChange={ handleVisibilityChange } | ||
className="activitypub-visibility" | ||
/> | ||
</PluginDocumentSettingPanel> | ||
|
@@ -147,7 +174,7 @@ const EditorPlugin = () => { | |
* Opens the Fediverse preview for the current post in a new tab. | ||
*/ | ||
function onActivityPubPreview() { | ||
const previewLink = select( 'core/editor' ).getEditedPostPreviewLink(); | ||
const previewLink = select( editorStore ).getEditedPostPreviewLink(); | ||
const fediversePreviewLink = addQueryArgs( previewLink, { activitypub: 'true' } ); | ||
|
||
window.open( fediversePreviewLink, '_blank' ); | ||
|
@@ -160,7 +187,7 @@ function onActivityPubPreview() { | |
*/ | ||
const EditorPreview = () => { | ||
// check if post was saved | ||
const post_status = useSelect( ( select ) => select( 'core/editor' ).getCurrentPost().status ); | ||
const post_status = useSelect( ( select ) => select( editorStore ).getCurrentPost().status ); | ||
|
||
return ( | ||
<> | ||
|
Uh oh!
There was an error while loading. Please reload this page.