Skip to content
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

Block Library: Allow changing Site Title block heading level. #20361

Merged
merged 4 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/block-library/src/site-title/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"name": "core/site-title",
"category": "layout"
"category": "layout",
"attributes": {
"level": {
"type": "number",
"default": 1
}
}
}
19 changes: 0 additions & 19 deletions packages/block-library/src/site-title/edit.js

This file was deleted.

38 changes: 38 additions & 0 deletions packages/block-library/src/site-title/edit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { BlockControls, RichText } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import LevelToolbar from './level-toolbar';

export default function SiteTitleEdit( {
attributes: { level },
setAttributes,
} ) {
const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' );
const tagName = level === 0 ? 'p' : `h${ level }`;
return (
<>
<BlockControls>
<LevelToolbar
level={ level }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
</BlockControls>
<RichText
tagName={ tagName }
placeholder={ __( 'Site Title' ) }
value={ title }
onChange={ setTitle }
allowedFormats={ [] }
/>
</>
);
}
27 changes: 27 additions & 0 deletions packages/block-library/src/site-title/edit/level-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* WordPress dependencies
*/
import { SVG, Path } from '@wordpress/components';

export default function LevelIcon( { level, isPressed = false } ) {
const levelToPath = {
0: 'M11 5v7H9.5C7.6 12 6 10.4 6 8.5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8c.2 0 .4 0 .6-.1.2 0 .4-.1.5-.2.1-.1.3-.3.4-.5.1-.2.1-.5.1-.7 0-.4-.1-.8-.4-1.1-.3-.2-.7-.3-1.1-.3-.3 0-.7.1-1 .2-.4.2-.7.4-1 .7 0 .3.1.7.3 1 .1.2.3.4.4.6.2.1.3.3.5.3.2.1.5.2.7.1z',
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we save memory if we moved this const out of the function (and into module level) so it's not re-initialized each time the function is called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not a relevant amount.

return (
<SVG
width="20"
height="20"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
isPressed={ isPressed }
>
<Path d={ levelToPath[ level ] } />
</SVG>
);
}
38 changes: 38 additions & 0 deletions packages/block-library/src/site-title/edit/level-toolbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { ToolbarGroup } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import LevelIcon from './level-icon';

export default function LevelToolbar( { level, onChange } ) {
return (
<ToolbarGroup
isCollapsed
icon={ <LevelIcon level={ level } /> }
controls={ [ 0, 1, 2, 3, 4, 5, 6 ].map( ( currentLevel ) => {
const isActive = currentLevel === level;
return {
icon: (
<LevelIcon
level={ currentLevel }
isPressed={ isActive }
/>
),
title:
currentLevel === 0
? __( 'Paragraph' )
: // translators: %s: heading level e.g: "1", "2", "3"
sprintf( __( 'Heading %d' ), currentLevel ),
isActive,
onClick: () => onChange( currentLevel ),
};
} ) }
label={ __( 'Change heading level' ) }
/>
);
}
10 changes: 8 additions & 2 deletions packages/block-library/src/site-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
/**
* Renders the `core/site-title` block on the server.
*
* @param array $attributes The block attributes.
*
* @return string The render.
*/
function render_block_core_site_title() {
return sprintf( '<h1>%s</h1>', get_bloginfo( 'name' ) );
function render_block_core_site_title( $attributes ) {
$tag_name = 'h1';
if ( isset( $attributes['level'] ) ) {
$tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level'];
}
return sprintf( '<%1$s>%2$s</%1$s>', $tag_name, get_bloginfo( 'name' ) );
}

/**
Expand Down
15 changes: 6 additions & 9 deletions packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,20 @@ export function useEntityId( kind, type ) {
*/
export function useEntityProp( kind, type, prop ) {
const id = useEntityId( kind, type );

const { value, fullValue } = useSelect(
( select ) => {
const { getEntityRecord, getEditedEntityRecord } = select( 'core' );
const entity = getEntityRecord( kind, type, id ); // Trigger resolver.
const editedEntity = getEditedEntityRecord( kind, type, id );
return (
entity &&
editedEntity && {
value: editedEntity[ prop ],
fullValue: entity[ prop ],
}
);
return entity && editedEntity
? {
value: editedEntity[ prop ],
fullValue: entity[ prop ],
}
: {};
},
[ kind, type, id, prop ]
);

const { editEntityRecord } = useDispatch( 'core' );
const setValue = useCallback(
( newValue ) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/e2e-tests/fixtures/blocks/core__site-title.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"clientId": "_clientId_0",
"name": "core/site-title",
"isValid": true,
"attributes": {},
"attributes": {
"level": 1
},
"innerBlocks": [],
"originalContent": ""
}
Expand Down
26 changes: 14 additions & 12 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@ function Editor( { settings: _settings } ) {
return template ? (
<SlotFillProvider>
<DropZoneProvider>
<EntityProvider
kind="postType"
type={ settings.templateType }
id={ settings.templateId }
>
<Context.Provider value={ context }>
<Notices />
<Header />
<Sidebar />
<BlockEditor />
<Popover.Slot />
</Context.Provider>
<EntityProvider kind="root" type="site">
<EntityProvider
kind="postType"
type={ settings.templateType }
id={ settings.templateId }
>
<Context.Provider value={ context }>
<Notices />
<Header />
<Sidebar />
<BlockEditor />
<Popover.Slot />
</Context.Provider>
</EntityProvider>
</EntityProvider>
</DropZoneProvider>
</SlotFillProvider>
Expand Down