-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query title block - Archive Title (#29428)
* query title - archive title * add fixtures and polish * Update packages/block-library/src/query-title/index.php Co-authored-by: Carolina Nymark <[email protected]> * add placeholder styles * default to h1 Co-authored-by: Carolina Nymark <[email protected]>
- Loading branch information
1 parent
27cb4f6
commit d4fcfdd
Showing
15 changed files
with
308 additions
and
0 deletions.
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
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
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,72 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "core/query-title", | ||
"category": "design", | ||
"attributes": { | ||
"type": { | ||
"type": "string" | ||
}, | ||
"textAlign": { | ||
"type": "string" | ||
}, | ||
"level": { | ||
"type": "number", | ||
"default": 1 | ||
} | ||
}, | ||
"supports": { | ||
"align": [ "wide", "full" ], | ||
"html": false, | ||
"color": { | ||
"gradients": true | ||
}, | ||
"fontSize": true, | ||
"lineHeight": true, | ||
"__experimentalFontFamily": true, | ||
"__experimentalSelector": { | ||
"core/query-title/h1": { | ||
"title": "h1", | ||
"selector": "h1.wp-block-query-title", | ||
"attributes": { | ||
"level": 1 | ||
} | ||
}, | ||
"core/query-title/h2": { | ||
"title": "h2", | ||
"selector": "h2.wp-block-query-title", | ||
"attributes": { | ||
"level": 2 | ||
} | ||
}, | ||
"core/query-title/h3": { | ||
"title": "h3", | ||
"selector": "h3.wp-block-query-title", | ||
"attributes": { | ||
"level": 3 | ||
} | ||
}, | ||
"core/query-title/h4": { | ||
"title": "h4", | ||
"selector": "h4.wp-block-query-title", | ||
"attributes": { | ||
"level": 4 | ||
} | ||
}, | ||
"core/query-title/h5": { | ||
"title": "h5", | ||
"selector": "h5.wp-block-query-title", | ||
"attributes": { | ||
"level": 5 | ||
} | ||
}, | ||
"core/query-title/h6": { | ||
"title": "h6", | ||
"selector": "h6.wp-block-query-title", | ||
"attributes": { | ||
"level": 6 | ||
} | ||
} | ||
} | ||
}, | ||
"editorStyle": "wp-block-query-title-editor" | ||
} |
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,74 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
// import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
AlignmentToolbar, | ||
BlockControls, | ||
useBlockProps, | ||
Warning, | ||
} from '@wordpress/block-editor'; | ||
import { ToolbarGroup } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import HeadingLevelDropdown from '../heading/heading-level-dropdown'; | ||
|
||
const SUPPORTED_TYPES = [ 'archive' ]; | ||
|
||
export default function QueryTitleEdit( { | ||
attributes: { type, level, textAlign }, | ||
setAttributes, | ||
} ) { | ||
const TagName = `h${ level }`; | ||
const blockProps = useBlockProps( { | ||
className: classnames( { | ||
[ `has-text-align-${ textAlign }` ]: textAlign, | ||
'wp-block-query-title__placeholder': type === 'archive', | ||
} ), | ||
} ); | ||
// The plan is to augment this block with more | ||
// block variations like `Search Title`. | ||
if ( ! SUPPORTED_TYPES.includes( type ) ) { | ||
return ( | ||
<div { ...blockProps }> | ||
<Warning>{ __( 'Provided type is not supported.' ) }</Warning> | ||
</div> | ||
); | ||
} | ||
|
||
let titleElement; | ||
if ( type === 'archive' ) { | ||
titleElement = ( | ||
<TagName { ...blockProps }>{ __( 'Archive title' ) }</TagName> | ||
); | ||
} | ||
return ( | ||
<> | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<HeadingLevelDropdown | ||
selectedLevel={ level } | ||
onChange={ ( newLevel ) => | ||
setAttributes( { level: newLevel } ) | ||
} | ||
/> | ||
</ToolbarGroup> | ||
<AlignmentToolbar | ||
value={ textAlign } | ||
onChange={ ( nextAlign ) => { | ||
setAttributes( { textAlign: nextAlign } ); | ||
} } | ||
/> | ||
</BlockControls> | ||
{ titleElement } | ||
</> | ||
); | ||
} |
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,4 @@ | ||
.wp-block-query-title__placeholder { | ||
padding: 1em 0; | ||
border: 1px dashed; | ||
} |
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,21 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
import variations from './variations'; | ||
|
||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Query Title' ), | ||
description: __( 'Display the query title.' ), | ||
edit, | ||
variations, | ||
}; |
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,51 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/query-title` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/query-title` block on the server. | ||
* For now it only supports Archive title, | ||
* using queried object information | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
* | ||
* @return string Returns the query title based on the queried object. | ||
*/ | ||
function render_block_core_query_title( $attributes, $content, $block ) { | ||
$type = isset( $attributes['type'] ) ? $attributes['type'] : null; | ||
$is_archive = is_archive(); | ||
if ( ! $type || ( 'archive' === $type && ! $is_archive ) ) { | ||
return ''; | ||
} | ||
$title = ''; | ||
if ( $is_archive ) { | ||
$title = get_the_archive_title(); | ||
} | ||
$tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; | ||
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; | ||
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); | ||
return sprintf( | ||
'<%1$s %2$s>%3$s</%1$s>', | ||
$tag_name, | ||
$wrapper_attributes, | ||
$title | ||
); | ||
} | ||
|
||
/** | ||
* Registers the `core/query-title` block on the server. | ||
*/ | ||
function register_block_core_query_title() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/query-title', | ||
array( | ||
'render_callback' => 'render_block_core_query_title', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_query_title' ); |
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,33 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { archiveTitle } from '@wordpress/icons'; | ||
const variations = [ | ||
{ | ||
isDefault: true, | ||
name: 'archive-title', | ||
title: __( 'Archive Title' ), | ||
description: __( | ||
'Display the archive title based on the queried object.' | ||
), | ||
icon: archiveTitle, | ||
attributes: { | ||
type: 'archive', | ||
}, | ||
scope: [ 'inserter' ], | ||
}, | ||
]; | ||
|
||
/** | ||
* Add `isActive` function to all `query-title` variations, if not defined. | ||
* `isActive` function is used to find a variation match from a created | ||
* Block by providing its attributes. | ||
*/ | ||
variations.forEach( ( variation ) => { | ||
if ( variation.isActive ) return; | ||
variation.isActive = ( blockAttributes, variationAttributes ) => | ||
blockAttributes.type === variationAttributes.type; | ||
} ); | ||
|
||
export default variations; |
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 @@ | ||
<!-- wp:query-title /--> |
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,12 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/query-title", | ||
"isValid": true, | ||
"attributes": { | ||
"level": 1 | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
18 changes: 18 additions & 0 deletions
18
packages/e2e-tests/fixtures/blocks/core__query-title.parsed.json
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,18 @@ | ||
[ | ||
{ | ||
"blockName": "core/query-title", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/fixtures/blocks/core__query-title.serialized.html
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 @@ | ||
<!-- wp:query-title /--> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { SVG, Path } from '@wordpress/primitives'; | ||
|
||
const archiveTitle = ( | ||
<SVG viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> | ||
<Path stroke="#1E1E1E" strokeWidth="1.5" d="M4 19.25h9M4 15.25h16" /> | ||
<Path | ||
d="M8.994 10.103H6.08L5.417 12H4l2.846-8h1.383l2.845 8H9.657l-.663-1.897zm-.457-1.28l-.994-2.857-1.006 2.857h2z" | ||
fill="#1E1E1E" | ||
/> | ||
</SVG> | ||
); | ||
|
||
export default archiveTitle; |