@wordpress/block-library 7.3.0 → 7.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/build/audio/edit.js +1 -1
  2. package/build/audio/edit.js.map +1 -1
  3. package/build/comment-template/edit.js +3 -1
  4. package/build/comment-template/edit.js.map +1 -1
  5. package/build/comment-template/hooks.js +14 -4
  6. package/build/comment-template/hooks.js.map +1 -1
  7. package/build/comments-pagination/edit.js +18 -1
  8. package/build/comments-pagination/edit.js.map +1 -1
  9. package/build/comments-query-loop/edit.js +2 -2
  10. package/build/comments-query-loop/edit.js.map +1 -1
  11. package/build/comments-title/edit.js +149 -0
  12. package/build/comments-title/edit.js.map +1 -0
  13. package/build/comments-title/index.js +101 -0
  14. package/build/comments-title/index.js.map +1 -0
  15. package/build/cover/transforms.js +33 -32
  16. package/build/cover/transforms.js.map +1 -1
  17. package/build/index.js +6 -4
  18. package/build/index.js.map +1 -1
  19. package/build/list/index.js +13 -2
  20. package/build/list/index.js.map +1 -1
  21. package/build/navigation/edit/index.js +17 -1
  22. package/build/navigation/edit/index.js.map +1 -1
  23. package/build/navigation/edit/navigation-menu-selector.js +7 -2
  24. package/build/navigation/edit/navigation-menu-selector.js.map +1 -1
  25. package/build/post-comments/edit.js +141 -35
  26. package/build/post-comments/edit.js.map +1 -1
  27. package/build/post-comments/index.js +6 -4
  28. package/build/post-comments/index.js.map +1 -1
  29. package/build/post-comments-form/edit.js +22 -1
  30. package/build/post-comments-form/edit.js.map +1 -1
  31. package/build/post-comments-form/index.js +1 -0
  32. package/build/post-comments-form/index.js.map +1 -1
  33. package/build/post-featured-image/edit.js +19 -7
  34. package/build/post-featured-image/edit.js.map +1 -1
  35. package/build/query/edit/index.js +84 -22
  36. package/build/query/edit/index.js.map +1 -1
  37. package/build/query/edit/query-placeholder.js +7 -14
  38. package/build/query/edit/query-placeholder.js.map +1 -1
  39. package/build/query/edit/query-toolbar.js +6 -1
  40. package/build/query/edit/query-toolbar.js.map +1 -1
  41. package/build/query-no-results/edit.js +1 -1
  42. package/build/query-no-results/edit.js.map +1 -1
  43. package/build/quote/index.js +13 -2
  44. package/build/quote/index.js.map +1 -1
  45. package/build-module/audio/edit.js +1 -1
  46. package/build-module/audio/edit.js.map +1 -1
  47. package/build-module/comment-template/edit.js +3 -1
  48. package/build-module/comment-template/edit.js.map +1 -1
  49. package/build-module/comment-template/hooks.js +14 -4
  50. package/build-module/comment-template/hooks.js.map +1 -1
  51. package/build-module/comments-pagination/edit.js +19 -2
  52. package/build-module/comments-pagination/edit.js.map +1 -1
  53. package/build-module/comments-query-loop/edit.js +2 -2
  54. package/build-module/comments-query-loop/edit.js.map +1 -1
  55. package/build-module/comments-title/edit.js +133 -0
  56. package/build-module/comments-title/edit.js.map +1 -0
  57. package/build-module/comments-title/index.js +88 -0
  58. package/build-module/comments-title/index.js.map +1 -0
  59. package/build-module/cover/transforms.js +33 -32
  60. package/build-module/cover/transforms.js.map +1 -1
  61. package/build-module/index.js +5 -4
  62. package/build-module/index.js.map +1 -1
  63. package/build-module/list/index.js +9 -3
  64. package/build-module/list/index.js.map +1 -1
  65. package/build-module/navigation/edit/index.js +17 -1
  66. package/build-module/navigation/edit/index.js.map +1 -1
  67. package/build-module/navigation/edit/navigation-menu-selector.js +6 -2
  68. package/build-module/navigation/edit/navigation-menu-selector.js.map +1 -1
  69. package/build-module/post-comments/edit.js +143 -38
  70. package/build-module/post-comments/edit.js.map +1 -1
  71. package/build-module/post-comments/index.js +6 -4
  72. package/build-module/post-comments/index.js.map +1 -1
  73. package/build-module/post-comments-form/edit.js +21 -1
  74. package/build-module/post-comments-form/edit.js.map +1 -1
  75. package/build-module/post-comments-form/index.js +1 -0
  76. package/build-module/post-comments-form/index.js.map +1 -1
  77. package/build-module/post-featured-image/edit.js +19 -7
  78. package/build-module/post-featured-image/edit.js.map +1 -1
  79. package/build-module/query/edit/index.js +87 -26
  80. package/build-module/query/edit/index.js.map +1 -1
  81. package/build-module/query/edit/query-placeholder.js +8 -15
  82. package/build-module/query/edit/query-placeholder.js.map +1 -1
  83. package/build-module/query/edit/query-toolbar.js +6 -1
  84. package/build-module/query/edit/query-toolbar.js.map +1 -1
  85. package/build-module/query-no-results/edit.js +1 -1
  86. package/build-module/query-no-results/edit.js.map +1 -1
  87. package/build-module/quote/index.js +9 -3
  88. package/build-module/quote/index.js.map +1 -1
  89. package/build-style/comments-title/editor-rtl.css +79 -0
  90. package/build-style/comments-title/editor.css +79 -0
  91. package/build-style/editor-rtl.css +51 -0
  92. package/build-style/editor.css +51 -0
  93. package/build-style/post-comments/editor-rtl.css +79 -0
  94. package/build-style/post-comments/editor.css +79 -0
  95. package/build-style/post-comments/style-rtl.css +1 -3
  96. package/build-style/post-comments/style.css +1 -3
  97. package/build-style/post-comments-form/editor-rtl.css +79 -0
  98. package/build-style/post-comments-form/editor.css +79 -0
  99. package/build-style/post-comments-form/style-rtl.css +9 -0
  100. package/build-style/post-comments-form/style.css +9 -0
  101. package/build-style/query/editor-rtl.css +39 -0
  102. package/build-style/query/editor.css +39 -0
  103. package/build-style/style-rtl.css +10 -3
  104. package/build-style/style.css +10 -3
  105. package/package.json +28 -28
  106. package/src/audio/edit.js +1 -1
  107. package/src/comment-edit-link/index.php +1 -4
  108. package/src/comment-reply-link/index.php +1 -4
  109. package/src/comment-template/edit.js +5 -2
  110. package/src/comment-template/hooks.js +15 -2
  111. package/src/comment-template/index.php +23 -2
  112. package/src/comments-pagination/edit.js +23 -0
  113. package/src/comments-query-loop/edit.js +2 -0
  114. package/src/comments-title/block.json +70 -0
  115. package/src/comments-title/edit.js +197 -0
  116. package/src/comments-title/editor.scss +4 -0
  117. package/src/comments-title/index.js +18 -0
  118. package/src/comments-title/index.php +68 -0
  119. package/src/cover/transforms.js +51 -28
  120. package/src/editor.scss +3 -0
  121. package/src/index.js +8 -2
  122. package/src/list/index.js +7 -3
  123. package/src/navigation/edit/index.js +24 -0
  124. package/src/navigation/edit/navigation-menu-selector.js +15 -9
  125. package/src/post-comments/block.json +6 -4
  126. package/src/post-comments/edit.js +204 -44
  127. package/src/post-comments/editor.scss +3 -0
  128. package/src/post-comments/style.scss +1 -5
  129. package/src/post-comments-form/block.json +1 -0
  130. package/src/post-comments-form/edit.js +39 -2
  131. package/src/post-comments-form/editor.scss +3 -0
  132. package/src/post-comments-form/index.php +12 -4
  133. package/src/post-comments-form/style.scss +11 -0
  134. package/src/post-featured-image/edit.js +24 -12
  135. package/src/query/edit/index.js +125 -26
  136. package/src/query/edit/query-placeholder.js +3 -13
  137. package/src/query/edit/query-toolbar.js +6 -0
  138. package/src/query/editor.scss +38 -0
  139. package/src/query-no-results/edit.js +1 -1
  140. package/src/quote/index.js +7 -3
@@ -6,65 +6,89 @@ import classnames from 'classnames';
6
6
  /**
7
7
  * WordPress dependencies
8
8
  */
9
- import { useSelect } from '@wordpress/data';
10
9
  import {
11
10
  AlignmentControl,
12
11
  BlockControls,
13
12
  Warning,
14
13
  useBlockProps,
14
+ store as blockEditorStore,
15
15
  } from '@wordpress/block-editor';
16
- import { __ } from '@wordpress/i18n';
17
- import { RawHTML } from '@wordpress/element';
18
- import { store as coreStore } from '@wordpress/core-data';
19
-
20
- function PostCommentsDisplay( { postId } ) {
21
- return useSelect(
22
- ( select ) => {
23
- const comments = select( coreStore ).getEntityRecords(
24
- 'root',
25
- 'comment',
26
- {
27
- post: postId,
28
- }
29
- );
30
- // TODO: "No Comments" placeholder should be editable.
31
- return comments && comments.length
32
- ? comments.map( ( comment ) => (
33
- <RawHTML
34
- className="wp-block-post-comments__comment"
35
- key={ comment.id }
36
- >
37
- { comment.content.rendered }
38
- </RawHTML>
39
- ) )
40
- : __( 'No comments.' );
41
- },
42
- [ postId ]
43
- );
44
- }
16
+ import { __, sprintf } from '@wordpress/i18n';
17
+ import { useSelect } from '@wordpress/data';
18
+ import { useEntityProp, store as coreStore } from '@wordpress/core-data';
19
+ import {
20
+ __experimentalUseDisabled as useDisabled,
21
+ useInstanceId,
22
+ } from '@wordpress/compose';
45
23
 
46
24
  export default function PostCommentsEdit( {
47
- attributes,
25
+ attributes: { textAlign },
48
26
  setAttributes,
49
- context,
27
+ context: { postType, postId },
50
28
  } ) {
51
- const { postType, postId } = context;
52
- const { textAlign } = attributes;
29
+ let [ postTitle ] = useEntityProp( 'postType', postType, 'title', postId );
30
+ postTitle = postTitle || __( 'Post Title' );
31
+
32
+ const [ commentStatus ] = useEntityProp(
33
+ 'postType',
34
+ postType,
35
+ 'comment_status',
36
+ postId
37
+ );
38
+
39
+ const { avatarURL, defaultCommentStatus } = useSelect(
40
+ ( select ) =>
41
+ select( blockEditorStore ).getSettings()
42
+ .__experimentalDiscussionSettings
43
+ );
44
+
45
+ const isSiteEditor = postType === undefined || postId === undefined;
46
+
47
+ const postTypeSupportsComments = useSelect( ( select ) =>
48
+ postType
49
+ ? !! select( coreStore ).getPostType( postType )?.supports.comments
50
+ : false
51
+ );
52
+
53
+ let warning = __(
54
+ 'Post Comments block: This is just a placeholder, not a real comment. The final styling may differ because it also depends on the current theme. For better compatibility with the Block Editor, please consider replacing this block with the "Comments Query Loop" block.'
55
+ );
56
+ let showPlacholder = true;
57
+
58
+ if ( ! isSiteEditor && 'open' !== commentStatus ) {
59
+ if ( 'closed' === commentStatus ) {
60
+ warning = sprintf(
61
+ /* translators: 1: Post type (i.e. "post", "page") */
62
+ __(
63
+ 'Post Comments block: Comments to this %s are not allowed.'
64
+ ),
65
+ postType
66
+ );
67
+ showPlacholder = false;
68
+ } else if ( ! postTypeSupportsComments ) {
69
+ warning = sprintf(
70
+ /* translators: 1: Post type (i.e. "post", "page") */
71
+ __(
72
+ 'Post Comments block: Comments for this post type (%s) are not enabled.'
73
+ ),
74
+ postType
75
+ );
76
+ showPlacholder = false;
77
+ } else if ( 'open' !== defaultCommentStatus ) {
78
+ warning = __( 'Post Comments block: Comments are not enabled.' );
79
+ showPlacholder = false;
80
+ }
81
+ }
82
+
53
83
  const blockProps = useBlockProps( {
54
84
  className: classnames( {
55
85
  [ `has-text-align-${ textAlign }` ]: textAlign,
56
86
  } ),
57
87
  } );
58
88
 
59
- if ( ! postType || ! postId ) {
60
- return (
61
- <div { ...blockProps }>
62
- <Warning>
63
- { __( 'Post comments block: no post found.' ) }
64
- </Warning>
65
- </div>
66
- );
67
- }
89
+ const disabledRef = useDisabled();
90
+
91
+ const textareaId = useInstanceId( PostCommentsEdit );
68
92
 
69
93
  return (
70
94
  <>
@@ -78,7 +102,143 @@ export default function PostCommentsEdit( {
78
102
  </BlockControls>
79
103
 
80
104
  <div { ...blockProps }>
81
- <PostCommentsDisplay postId={ postId } />
105
+ <Warning>{ warning }</Warning>
106
+
107
+ { showPlacholder && (
108
+ <div
109
+ className="wp-block-post-comments__placeholder"
110
+ ref={ disabledRef }
111
+ >
112
+ <h3>
113
+ { __( 'One response to' ) } “{ postTitle }”
114
+ </h3>
115
+
116
+ <div className="navigation">
117
+ <div className="alignleft">
118
+ <a href="#top">« { __( 'Older Comments' ) }</a>
119
+ </div>
120
+ <div className="alignright">
121
+ <a href="#top">{ __( 'Newer Comments' ) } »</a>
122
+ </div>
123
+ </div>
124
+
125
+ <ol className="commentlist">
126
+ <li className="comment even thread-even depth-1">
127
+ <article className="comment-body">
128
+ <footer className="comment-meta">
129
+ <div className="comment-author vcard">
130
+ <img
131
+ alt="Commenter Avatar"
132
+ src={ avatarURL }
133
+ className="avatar avatar-32 photo"
134
+ height="32"
135
+ width="32"
136
+ loading="lazy"
137
+ />
138
+ <b className="fn">
139
+ <a href="#top" className="url">
140
+ { __(
141
+ 'A WordPress Commenter'
142
+ ) }
143
+ </a>
144
+ </b>{ ' ' }
145
+ <span className="says">
146
+ { __( 'says' ) }:
147
+ </span>
148
+ </div>
149
+
150
+ <div className="comment-metadata">
151
+ <a href="#top">
152
+ <time dateTime="2000-01-01T00:00:00+00:00">
153
+ { __(
154
+ 'January 1, 2000 at 00:00 am'
155
+ ) }
156
+ </time>
157
+ </a>{ ' ' }
158
+ <span className="edit-link">
159
+ <a
160
+ className="comment-edit-link"
161
+ href="#top"
162
+ >
163
+ { __( 'Edit' ) }
164
+ </a>
165
+ </span>
166
+ </div>
167
+ </footer>
168
+
169
+ <div className="comment-content">
170
+ <p>
171
+ { __( 'Hi, this is a comment.' ) }
172
+ <br />
173
+ { __(
174
+ 'To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.'
175
+ ) }
176
+ <br />
177
+ { __(
178
+ 'Commenter avatars come from'
179
+ ) }{ ' ' }
180
+ <a href="https://gravatar.com/">
181
+ Gravatar
182
+ </a>
183
+ .
184
+ </p>
185
+ </div>
186
+
187
+ <div className="reply">
188
+ <a
189
+ className="comment-reply-link"
190
+ href="#top"
191
+ aria-label="Reply to A WordPress Commenter"
192
+ >
193
+ { __( 'Reply' ) }
194
+ </a>
195
+ </div>
196
+ </article>
197
+ </li>
198
+ </ol>
199
+
200
+ <div className="navigation">
201
+ <div className="alignleft">
202
+ <a href="#top">« { __( 'Older Comments' ) }</a>
203
+ </div>
204
+ <div className="alignright">
205
+ <a href="#top">{ __( 'Newer Comments' ) } »</a>
206
+ </div>
207
+ </div>
208
+
209
+ <div className="comment-respond">
210
+ <h3 className="comment-reply-title">
211
+ { __( 'Leave a Reply' ) }
212
+ </h3>
213
+
214
+ <form className="comment-form" noValidate>
215
+ <p className="comment-form-comment">
216
+ <label
217
+ htmlFor={ `comment-${ textareaId }` }
218
+ >
219
+ { __( 'Comment' ) }{ ' ' }
220
+ <span className="required">*</span>
221
+ </label>
222
+ <textarea
223
+ id={ `comment-${ textareaId }` }
224
+ name="comment"
225
+ cols="45"
226
+ rows="8"
227
+ required
228
+ />
229
+ </p>
230
+ <p className="form-submit wp-block-button">
231
+ <input
232
+ name="submit"
233
+ type="submit"
234
+ className="submit wp-block-button__link"
235
+ value={ __( 'Post Comment' ) }
236
+ />
237
+ </p>
238
+ </form>
239
+ </div>
240
+ </div>
241
+ ) }
82
242
  </div>
83
243
  </>
84
244
  );
@@ -0,0 +1,3 @@
1
+ .wp-block-post-comments__placeholder * {
2
+ pointer-events: none;
3
+ }
@@ -1,10 +1,6 @@
1
1
  .wp-block-post-comments {
2
- // Remove extraneous top padding added to the first heading of the block.
3
- > h3:first-of-type {
4
- margin-top: 0;
5
- }
6
-
7
2
  .commentlist {
3
+ clear: both;
8
4
  list-style: none;
9
5
  margin: 0;
10
6
  padding: 0;
@@ -34,6 +34,7 @@
34
34
  }
35
35
  }
36
36
  },
37
+ "editorStyle": "wp-block-post-comments-form-editor",
37
38
  "style": [
38
39
  "wp-block-post-comments-form",
39
40
  "wp-block-buttons",
@@ -14,6 +14,10 @@ import {
14
14
  } from '@wordpress/block-editor';
15
15
  import { useEntityProp } from '@wordpress/core-data';
16
16
  import { __, sprintf } from '@wordpress/i18n';
17
+ import {
18
+ __experimentalUseDisabled as useDisabled,
19
+ useInstanceId,
20
+ } from '@wordpress/compose';
17
21
 
18
22
  export default function PostCommentsFormEdit( {
19
23
  attributes,
@@ -36,6 +40,10 @@ export default function PostCommentsFormEdit( {
36
40
 
37
41
  const isInSiteEditor = postType === undefined || postId === undefined;
38
42
 
43
+ const disabledFormRef = useDisabled();
44
+
45
+ const instanceId = useInstanceId( PostCommentsFormEdit );
46
+
39
47
  return (
40
48
  <>
41
49
  <BlockControls group="block">
@@ -67,8 +75,37 @@ export default function PostCommentsFormEdit( {
67
75
  </Warning>
68
76
  ) }
69
77
 
70
- { ( 'open' === commentStatus || isInSiteEditor ) &&
71
- __( 'Post Comments Form' ) }
78
+ { ( 'open' === commentStatus || isInSiteEditor ) && (
79
+ <div>
80
+ <h3>{ __( 'Leave a Reply' ) }</h3>
81
+ <form
82
+ noValidate
83
+ className="comment-form"
84
+ ref={ disabledFormRef }
85
+ >
86
+ <p>
87
+ <label htmlFor={ `comment-${ instanceId }` }>
88
+ { __( 'Comment' ) }
89
+ </label>
90
+ <textarea
91
+ id={ `comment-${ instanceId }` }
92
+ name="comment"
93
+ cols="45"
94
+ rows="8"
95
+ />
96
+ </p>
97
+ <p>
98
+ <input
99
+ name="submit"
100
+ className="submit wp-block-button__link"
101
+ label={ __( 'Post Comment' ) }
102
+ value={ __( 'Post Comment' ) }
103
+ readOnly
104
+ />
105
+ </p>
106
+ </form>
107
+ </div>
108
+ ) }
72
109
  </div>
73
110
  </>
74
111
  );
@@ -0,0 +1,3 @@
1
+ .wp-block-post-comments-form * {
2
+ pointer-events: none;
3
+ }
@@ -18,20 +18,28 @@ function render_block_core_post_comments_form( $attributes, $content, $block ) {
18
18
  return '';
19
19
  }
20
20
 
21
- $classes = '';
21
+ $classes = 'comment-respond'; // See comment further below.
22
22
  if ( isset( $attributes['textAlign'] ) ) {
23
23
  $classes .= 'has-text-align-' . $attributes['textAlign'];
24
24
  }
25
25
 
26
+ $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
27
+
26
28
  ob_start();
27
29
  comment_form( array(), $block->context['postId'] );
28
- $form = ob_get_clean();
29
- $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
30
+ $form = ob_get_clean();
31
+
32
+ // We use the outermost wrapping `<div />` returned by `comment_form()`
33
+ // which is identified by its default classname `comment-respond` to inject
34
+ // our wrapper attributes. This way, it is guaranteed that all styling applied
35
+ // to the block is carried along when the comment form is moved to the location
36
+ // of the 'Reply' link that the user clicked by Core's `comment-reply.js` script.
37
+ $form = str_replace( 'class="comment-respond"', $wrapper_attributes, $form );
30
38
 
31
39
  // Enqueue the comment-reply script.
32
40
  wp_enqueue_script( 'comment-reply' );
33
41
 
34
- return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $form );
42
+ return $form;
35
43
  }
36
44
 
37
45
  /**
@@ -67,4 +67,15 @@
67
67
  margin-top: 0.35em;
68
68
  }
69
69
  }
70
+
71
+ .comment-reply-title {
72
+ align-items: baseline;
73
+ display: flex;
74
+ justify-content: space-between;
75
+ margin-bottom: 0;
76
+
77
+ :where(small) {
78
+ font-size: var(--wp--preset--font-size--medium, smaller);
79
+ }
80
+ }
70
81
  }
@@ -57,22 +57,30 @@ function PostFeaturedImageDisplay( {
57
57
  clientId,
58
58
  attributes,
59
59
  setAttributes,
60
- context: { postId, postType, queryId },
60
+ context: { postId, postType: postTypeSlug, queryId },
61
61
  } ) {
62
62
  const isDescendentOfQueryLoop = Number.isFinite( queryId );
63
63
  const { isLink, height, width, scale, sizeSlug } = attributes;
64
64
  const [ featuredImage, setFeaturedImage ] = useEntityProp(
65
65
  'postType',
66
- postType,
66
+ postTypeSlug,
67
67
  'featured_media',
68
68
  postId
69
69
  );
70
70
 
71
- const media = useSelect(
72
- ( select ) =>
73
- featuredImage &&
74
- select( coreStore ).getMedia( featuredImage, { context: 'view' } ),
75
- [ featuredImage ]
71
+ const { media, postType } = useSelect(
72
+ ( select ) => {
73
+ const { getMedia, getPostType } = select( coreStore );
74
+ return {
75
+ media:
76
+ featuredImage &&
77
+ getMedia( featuredImage, {
78
+ context: 'view',
79
+ } ),
80
+ postType: postTypeSlug && getPostType( postTypeSlug ),
81
+ };
82
+ },
83
+ [ featuredImage, postTypeSlug ]
76
84
  );
77
85
  const mediaUrl = getMediaSourceUrlBySizeSlug( media, sizeSlug );
78
86
 
@@ -124,11 +132,15 @@ function PostFeaturedImageDisplay( {
124
132
  <InspectorControls>
125
133
  <PanelBody title={ __( 'Link settings' ) }>
126
134
  <ToggleControl
127
- label={ sprintf(
128
- // translators: %s: Name of the post type e.g: "post".
129
- __( 'Link to %s' ),
130
- postType
131
- ) }
135
+ label={
136
+ postType?.labels.singular_name
137
+ ? sprintf(
138
+ // translators: %s: Name of the post type e.g: "post".
139
+ __( 'Link to %s' ),
140
+ postType.labels.singular_name.toLowerCase()
141
+ )
142
+ : __( 'Link to post' )
143
+ }
132
144
  onChange={ () => setAttributes( { isLink: ! isLink } ) }
133
145
  checked={ isLink }
134
146
  />
@@ -2,9 +2,9 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { useSelect, useDispatch } from '@wordpress/data';
5
- import { cloneBlock } from '@wordpress/blocks';
5
+ import { store as blocksStore, cloneBlock } from '@wordpress/blocks';
6
6
  import { useInstanceId } from '@wordpress/compose';
7
- import { useEffect } from '@wordpress/element';
7
+ import { useState, useEffect } from '@wordpress/element';
8
8
  import {
9
9
  BlockControls,
10
10
  InspectorControls,
@@ -12,9 +12,15 @@ import {
12
12
  useSetting,
13
13
  store as blockEditorStore,
14
14
  useInnerBlocksProps,
15
+ __experimentalGetMatchingVariation as getMatchingVariation,
15
16
  __experimentalBlockPatternSetup as BlockPatternSetup,
16
17
  } from '@wordpress/block-editor';
17
- import { SelectControl } from '@wordpress/components';
18
+ import {
19
+ Button,
20
+ SelectControl,
21
+ Placeholder,
22
+ Modal,
23
+ } from '@wordpress/components';
18
24
  import { __ } from '@wordpress/i18n';
19
25
 
20
26
  /**
@@ -27,7 +33,11 @@ import { DEFAULTS_POSTS_PER_PAGE } from '../constants';
27
33
  import { getFirstQueryClientIdFromBlocks } from '../utils';
28
34
 
29
35
  const TEMPLATE = [ [ 'core/post-template' ] ];
30
- export function QueryContent( { attributes, setAttributes } ) {
36
+ export function QueryContent( {
37
+ attributes,
38
+ setAttributes,
39
+ openPatternSelectionModal,
40
+ } ) {
31
41
  const {
32
42
  queryId,
33
43
  query,
@@ -102,6 +112,7 @@ export function QueryContent( { attributes, setAttributes } ) {
102
112
  attributes={ attributes }
103
113
  setQuery={ updateQuery }
104
114
  setDisplayLayout={ updateDisplayLayout }
115
+ openPatternSelectionModal={ openPatternSelectionModal }
105
116
  />
106
117
  </BlockControls>
107
118
  <InspectorControls __experimentalGroup="advanced">
@@ -124,43 +135,131 @@ export function QueryContent( { attributes, setAttributes } ) {
124
135
  );
125
136
  }
126
137
 
127
- function QueryPatternSetup( props ) {
128
- const { clientId, name: blockName } = props;
138
+ function QueryPatternSetup( {
139
+ attributes,
140
+ clientId,
141
+ name,
142
+ openPatternSelectionModal,
143
+ setAttributes,
144
+ } ) {
145
+ const [ isStartingBlank, setIsStartingBlank ] = useState( false );
129
146
  const blockProps = useBlockProps();
130
- const { replaceBlock, selectBlock } = useDispatch( blockEditorStore );
131
- const onBlockPatternSelect = ( blocks ) => {
132
- const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
133
- const firstQueryClientId = getFirstQueryClientIdFromBlocks(
134
- clonedBlocks
147
+
148
+ const { blockType, allVariations, hasPatterns } = useSelect(
149
+ ( select ) => {
150
+ const { getBlockVariations, getBlockType } = select( blocksStore );
151
+ const {
152
+ getBlockRootClientId,
153
+ __experimentalGetPatternsByBlockTypes,
154
+ } = select( blockEditorStore );
155
+ const rootClientId = getBlockRootClientId( clientId );
156
+
157
+ return {
158
+ blockType: getBlockType( name ),
159
+ allVariations: getBlockVariations( name ),
160
+ hasPatterns: !! __experimentalGetPatternsByBlockTypes(
161
+ name,
162
+ rootClientId
163
+ ).length,
164
+ };
165
+ },
166
+ [ name, clientId ]
167
+ );
168
+
169
+ const matchingVariation = getMatchingVariation( attributes, allVariations );
170
+ const icon = matchingVariation?.icon || blockType?.icon?.src;
171
+ const label = matchingVariation?.title || blockType?.title;
172
+ if ( isStartingBlank ) {
173
+ return (
174
+ <QueryPlaceholder
175
+ clientId={ clientId }
176
+ name={ name }
177
+ setAttributes={ setAttributes }
178
+ icon={ icon }
179
+ label={ label }
180
+ />
135
181
  );
136
- replaceBlock( clientId, clonedBlocks );
137
- if ( firstQueryClientId ) {
138
- selectBlock( firstQueryClientId );
139
- }
140
- };
141
- // `startBlankComponent` is what to render when clicking `Start blank`
142
- // or if no matched patterns are found.
182
+ }
143
183
  return (
144
184
  <div { ...blockProps }>
145
- <BlockPatternSetup
146
- blockName={ blockName }
147
- clientId={ clientId }
148
- startBlankComponent={ <QueryPlaceholder { ...props } /> }
149
- onBlockPatternSelect={ onBlockPatternSelect }
150
- />
185
+ <Placeholder
186
+ icon={ icon }
187
+ label={ label }
188
+ instructions={ __(
189
+ 'Choose a pattern for the query loop or start blank.'
190
+ ) }
191
+ >
192
+ { !! hasPatterns && (
193
+ <Button
194
+ variant="primary"
195
+ onClick={ openPatternSelectionModal }
196
+ >
197
+ { __( 'Choose' ) }
198
+ </Button>
199
+ ) }
200
+
201
+ <Button
202
+ variant="secondary"
203
+ onClick={ () => {
204
+ setIsStartingBlank( true );
205
+ } }
206
+ >
207
+ { __( 'Start blank' ) }
208
+ </Button>
209
+ </Placeholder>
151
210
  </div>
152
211
  );
153
212
  }
154
213
 
155
214
  const QueryEdit = ( props ) => {
156
- const { clientId } = props;
215
+ const { clientId, name } = props;
216
+ const [
217
+ isPatternSelectionModalOpen,
218
+ setIsPatternSelectionModalOpen,
219
+ ] = useState( false );
220
+ const { replaceBlock, selectBlock } = useDispatch( blockEditorStore );
157
221
  const hasInnerBlocks = useSelect(
158
222
  ( select ) =>
159
223
  !! select( blockEditorStore ).getBlocks( clientId ).length,
160
224
  [ clientId ]
161
225
  );
162
226
  const Component = hasInnerBlocks ? QueryContent : QueryPatternSetup;
163
- return <Component { ...props } />;
227
+ const onBlockPatternSelect = ( blocks ) => {
228
+ const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
229
+ const firstQueryClientId = getFirstQueryClientIdFromBlocks(
230
+ clonedBlocks
231
+ );
232
+ replaceBlock( clientId, clonedBlocks );
233
+ if ( firstQueryClientId ) {
234
+ selectBlock( firstQueryClientId );
235
+ }
236
+ };
237
+ return (
238
+ <>
239
+ <Component
240
+ { ...props }
241
+ openPatternSelectionModal={ () =>
242
+ setIsPatternSelectionModalOpen( true )
243
+ }
244
+ />
245
+ { isPatternSelectionModalOpen && (
246
+ <Modal
247
+ className="block-editor-query-pattern__selection-modal"
248
+ title={ __( 'Choose a pattern' ) }
249
+ closeLabel={ __( 'Cancel' ) }
250
+ onRequestClose={ () =>
251
+ setIsPatternSelectionModalOpen( false )
252
+ }
253
+ >
254
+ <BlockPatternSetup
255
+ blockName={ name }
256
+ clientId={ clientId }
257
+ onBlockPatternSelect={ onBlockPatternSelect }
258
+ />
259
+ </Modal>
260
+ ) }
261
+ </>
262
+ );
164
263
  };
165
264
 
166
265
  export default QueryEdit;