1
- import { PluginDocumentSettingPanel , PluginPreviewMenuItem } from '@wordpress/editor' ;
1
+ import { PluginDocumentSettingPanel , PluginPreviewMenuItem , store as editorStore } from '@wordpress/editor' ;
2
2
import { registerPlugin } from '@wordpress/plugins' ;
3
3
import { TextControl , RadioControl , RangeControl , __experimentalText as Text , Tooltip } from '@wordpress/components' ;
4
- import { Icon , globe , people , external } from '@wordpress/icons' ;
4
+ import { Icon , globe , people , external , notAllowed } from '@wordpress/icons' ;
5
5
import { useSelect , select } from '@wordpress/data' ;
6
6
import { useEntityProp } from '@wordpress/core-data' ;
7
7
import { addQueryArgs } from '@wordpress/url' ;
8
8
import { __ } from '@wordpress/i18n' ;
9
- import { SVG , Path } from '@wordpress/primitives' ;
10
9
import { useOptions } from '../shared/use-options' ;
11
10
12
- // Defining our own because it's too new in @wordpress/icons
13
- // https://github.com/WordPress/gutenberg/blob/trunk/packages/icons/src/library/not-allowed.js
14
- const notAllowed = (
15
- < SVG xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 24 24" >
16
- < Path
17
- fillRule = "evenodd"
18
- clipRule = "evenodd"
19
- d = "M12 18.5A6.5 6.5 0 0 1 6.93 7.931l9.139 9.138A6.473 6.473 0 0 1 12 18.5Zm5.123-2.498a6.5 6.5 0 0 0-9.124-9.124l9.124 9.124ZM4 12a8 8 0 1 1 16 0 8 8 0 0 1-16 0Z"
20
- />
21
- </ SVG >
22
- ) ;
11
+ /**
12
+ * Custom hook to update metadata in the post editor.
13
+ *
14
+ * @param {string } metaKey The key of the metadata to update.
15
+ * @param {string } postType The type of post to update the metadata for.
16
+ * @returns {[string, (value: string) => void] } The current value of the metadata and a function to update it.
17
+ */
18
+ function useSetMeta ( metaKey , postType ) {
19
+ const [ meta , setMeta ] = useEntityProp ( 'postType' , postType || 'default' , 'meta' ) ;
20
+
21
+ const setValue = ( value ) => {
22
+ setMeta ( { ...meta , [ metaKey ] : value } ) ;
23
+ } ;
24
+
25
+ return [ meta ?. [ metaKey ] , setValue ] ;
26
+ }
23
27
24
28
/**
25
29
* Editor plugin for ActivityPub settings in the block editor.
26
30
*
27
31
* @returns {JSX.Element|null } The settings panel for ActivityPub or null for sync blocks.
28
32
*/
29
33
const EditorPlugin = ( ) => {
30
- const postType = useSelect ( ( select ) => select ( 'core/editor' ) . getCurrentPostType ( ) , [ ] ) ;
31
- const [ meta , setMeta ] = useEntityProp ( 'postType' , postType || 'default' , 'meta' ) ;
34
+ const postType = useSelect ( ( select ) => select ( editorStore ) . getCurrentPostType ( ) , [ ] ) ;
35
+
36
+ const [ contentWarning , setContentWarning ] = useSetMeta ( 'activitypub_content_warning' , postType ) ;
37
+ const [ maxImageAttachments , setMaxImageAttachments ] = useSetMeta ( 'activitypub_max_image_attachments' , postType ) ;
38
+ const [ contentVisibility , setContentVisibility ] = useSetMeta ( 'activitypub_content_visibility' , postType ) ;
39
+
40
+ const handleContentWarningChange = ( value ) => {
41
+ setContentWarning ( value ) ;
42
+ } ;
43
+
44
+ const handleMaxImageAttachmentsChange = ( value ) => {
45
+ setMaxImageAttachments ( value ) ;
46
+ } ;
47
+
48
+ const handleVisibilityChange = ( value ) => {
49
+ setContentVisibility ( value ) ;
50
+ } ;
32
51
33
52
// Don't show when editing sync blocks.
34
53
if ( 'wp_block' === postType ) {
35
54
return null ;
36
55
}
37
- const { maxImageAttachments = 4 } = useOptions ( ) ;
56
+
57
+ const { maxImageAttachments : defaultMaxImageAttachments = 4 } = useOptions ( ) ;
58
+
38
59
const labelStyling = {
39
60
verticalAlign : 'middle' ,
40
61
gap : '4px' ,
@@ -69,10 +90,8 @@ const EditorPlugin = () => {
69
90
>
70
91
< TextControl
71
92
label = { __ ( 'Content Warning' , 'activitypub' ) }
72
- value = { meta ?. activitypub_content_warning }
73
- onChange = { ( value ) => {
74
- setMeta ( { ...meta , activitypub_content_warning : value } ) ;
75
- } }
93
+ value = { contentWarning ?? '' }
94
+ onChange = { handleContentWarningChange }
76
95
placeholder = { __ ( 'Optional content warning' , 'activitypub' ) }
77
96
help = { __ (
78
97
'Content warnings do not change the content on your site, only in the fediverse.' ,
@@ -84,10 +103,8 @@ const EditorPlugin = () => {
84
103
85
104
< RangeControl
86
105
label = { __ ( 'Maximum Image Attachments' , 'activitypub' ) }
87
- value = { meta ?. activitypub_max_image_attachments ?? maxImageAttachments }
88
- onChange = { ( value ) => {
89
- setMeta ( { ...meta , activitypub_max_image_attachments : value } ) ;
90
- } }
106
+ value = { maxImageAttachments ?? defaultMaxImageAttachments }
107
+ onChange = { handleMaxImageAttachmentsChange }
91
108
min = { 0 }
92
109
max = { 10 }
93
110
help = { __ (
@@ -104,7 +121,7 @@ const EditorPlugin = () => {
104
121
"This adjusts the visibility of a post in the fediverse, but note that it won't affect how the post appears on the blog." ,
105
122
'activitypub'
106
123
) }
107
- selected = { meta ?. activitypub_content_visibility || 'public' }
124
+ selected = { contentVisibility || 'public' }
108
125
options = { [
109
126
{
110
127
label : enhancedLabel (
@@ -134,9 +151,7 @@ const EditorPlugin = () => {
134
151
value : 'local' ,
135
152
} ,
136
153
] }
137
- onChange = { ( value ) => {
138
- setMeta ( { ...meta , activitypub_content_visibility : value } ) ;
139
- } }
154
+ onChange = { handleVisibilityChange }
140
155
className = "activitypub-visibility"
141
156
/>
142
157
</ PluginDocumentSettingPanel >
@@ -147,7 +162,7 @@ const EditorPlugin = () => {
147
162
* Opens the Fediverse preview for the current post in a new tab.
148
163
*/
149
164
function onActivityPubPreview ( ) {
150
- const previewLink = select ( 'core/editor' ) . getEditedPostPreviewLink ( ) ;
165
+ const previewLink = select ( editorStore ) . getEditedPostPreviewLink ( ) ;
151
166
const fediversePreviewLink = addQueryArgs ( previewLink , { activitypub : 'true' } ) ;
152
167
153
168
window . open ( fediversePreviewLink , '_blank' ) ;
@@ -160,7 +175,7 @@ function onActivityPubPreview() {
160
175
*/
161
176
const EditorPreview = ( ) => {
162
177
// check if post was saved
163
- const post_status = useSelect ( ( select ) => select ( 'core/editor' ) . getCurrentPost ( ) . status ) ;
178
+ const post_status = useSelect ( ( select ) => select ( editorStore ) . getCurrentPost ( ) . status ) ;
164
179
165
180
return (
166
181
< >
0 commit comments