2
2
* WordPress dependencies
3
3
*/
4
4
import { __ } from "@wordpress/i18n" ;
5
- import { useSelect , withSelect } from "@wordpress/data" ;
5
+ import { useSelect } from "@wordpress/data" ;
6
6
import { store as coreStore } from "@wordpress/core-data" ;
7
- import { renderToString } from "@wordpress/element" ;
7
+ import { renderToString , useState } from "@wordpress/element" ;
8
8
import { addQueryArgs } from "@wordpress/url" ;
9
9
import { registerPlugin } from "@wordpress/plugins" ;
10
10
11
- const AddNewPostButton = ( { postType, newPost } ) => {
11
+ const AddNewPostButton = ( ) => {
12
+ const { postType } = useSelect ( ( select ) => {
13
+ return {
14
+ postType : select ( "core/editor" ) . getCurrentPostType ( ) ,
15
+ } ;
16
+ } ) ;
17
+
12
18
if ( ! postType ) {
13
19
return null ;
14
20
}
15
- if ( newPost ) {
16
- return null ;
17
- }
21
+ const { newState , setNewState } = useState ( true ) ;
22
+ const { newPost } = useSelect ( ( select ) => {
23
+ const newPost = select ( "core/editor" ) . isCleanNewPost ( ) ;
18
24
25
+ return {
26
+ newPost : newPost ,
27
+ } ;
28
+ } ) ;
19
29
const { singleLabel, addNewLabel } = useSelect ( ( select ) => {
20
30
const { getPostTypes } = select ( coreStore ) ;
21
31
const includedPostType = [ postType ] ;
@@ -34,7 +44,9 @@ const AddNewPostButton = ({ postType, newPost }) => {
34
44
singleLabel : undefined ,
35
45
} ;
36
46
} ) ;
47
+ // console.log("post is " + newPost);
37
48
if ( undefined !== addNewLabel ) {
49
+ console . log ( "post is " + newPost ) ;
38
50
const AddButton = (
39
51
< a
40
52
class = "components-button is-secondary"
@@ -57,29 +69,55 @@ const AddNewPostButton = ({ postType, newPost }) => {
57
69
</ span >
58
70
</ a >
59
71
) ;
60
- requestAnimationFrame ( ( ) => {
72
+ const DisabledButton = (
73
+ < button
74
+ class = "components-button is-secondary"
75
+ id = "createwithrani-add-new-button"
76
+ style = { {
77
+ textTransform : "capitalize" ,
78
+ margin : "0 1em" ,
79
+ } }
80
+ aria-disabled = { true }
81
+ >
82
+ < span >
83
+ { sprintf (
84
+ /* translators: %1$s: the phrase "Add New", %2$s: Name of current post type. */
85
+ __ ( "%1$s %2$s" , "createwithrani-add-new-post" ) ,
86
+ addNewLabel ,
87
+ singleLabel
88
+ ) }
89
+ </ span >
90
+ </ button >
91
+ ) ;
92
+ const paintbutton = ( ) => {
61
93
if ( ! document . querySelector ( ".edit-post-header-toolbar__left" ) ) {
62
94
return ;
63
95
}
64
- // Redundant extra check added because of a bug where the above check wasn't working, credit: Extendify plugin
65
96
if ( document . getElementById ( "createwithrani-add-new-button" ) ) {
66
- return ;
97
+ var existingButton = document . getElementById (
98
+ "createwithrani-add-new-button"
99
+ ) ;
100
+ existingButton . remove ( ) ;
67
101
}
68
- document
69
- . querySelector ( ".edit-post-header-toolbar__left" )
70
- . insertAdjacentHTML ( "beforeend" , renderToString ( AddButton ) ) ;
71
- } ) ;
72
- }
102
+ if ( newPost ) {
103
+ document
104
+ . querySelector ( ".edit-post-header-toolbar__left" )
105
+ . insertAdjacentHTML (
106
+ "beforeend" ,
107
+ renderToString ( DisabledButton )
108
+ ) ;
109
+ } else {
110
+ document
111
+ . querySelector ( ".edit-post-header-toolbar__left" )
112
+ . insertAdjacentHTML ( "beforeend" , renderToString ( AddButton ) ) ;
113
+ }
114
+ } ;
73
115
116
+ requestAnimationFrame ( paintbutton ) ;
117
+ }
74
118
return null ;
75
119
} ;
76
- const AddNewPostButtonWrapped = withSelect ( ( select ) => {
77
- return {
78
- postType : select ( "core/editor" ) . getCurrentPostType ( ) ,
79
- newPost : select ( "core/editor" ) . isCleanNewPost ( ) ,
80
- } ;
81
- } ) ( AddNewPostButton ) ;
82
120
83
121
registerPlugin ( "createwithrani-add-new-post" , {
84
- render : AddNewPostButtonWrapped ,
122
+ render : AddNewPostButton ,
85
123
} ) ;
0 commit comments