Skip to content

Commit

Permalink
Create a SiteDescription block similar to SiteTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
Copons committed Nov 1, 2019
1 parent c000551 commit 7531800
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/base-styles/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ $z-layers: (
".wp-block-cover.has-background-dim::before": 1, // Overlay area inside block cover need to be higher than the video background.
".wp-block-cover__video-background": 0, // Video background inside cover block.
".wp-block-site-title__save-button": 1,
".wp-block-site-description__save-button": 1,

// Active pill button
".components-button.is-button {:focus or .is-primary}": 1,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@import "./verse/editor.scss";
@import "./video/editor.scss";
@import "./site-title/editor.scss";
@import "./site-description/editor.scss";

/**
* Import styles from internal editor components used by the blocks.
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import * as socialLink from './social-link';

// Full Site Editing Blocks
import * as siteTitle from './site-title';
import * as siteDescription from './site-description';

/**
* Function to register an individual block.
Expand Down Expand Up @@ -182,7 +183,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
...socialLink.sites,

// Register Full Site Editing Blocks.
...( __experimentalEnableFullSiteEditing ? [ siteTitle ] : [] ),
...( __experimentalEnableFullSiteEditing ? [ siteTitle, siteDescription ] : [] ),
].forEach( registerBlock );
} :
undefined;
39 changes: 39 additions & 0 deletions packages/block-library/src/site-description/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "core/site-description",
"category": "layout",
"supports": {
"align": [ "wide", "full" ],
"html": false,
"multiple": false,
"reusable": false
},
"attributes": {
"align": {
"type": "string",
"default": "wide"
},
"textAlign": {
"type": "string",
"default": "center"
},
"textColor": {
"type": "string"
},
"customTextColor": {
"type": "string"
},
"backgroundColor": {
"type": "string"
},
"customBackgroundColor": {
"type": "string"
},
"fontSize": {
"type": "string",
"default": "small"
},
"customFontSize": {
"type": "number"
}
}
}
65 changes: 65 additions & 0 deletions packages/block-library/src/site-description/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* WordPress dependencies
*/
import {
useEntityProp,
__experimentalUseEntitySaving,
} from '@wordpress/core-data';
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { PlainText } from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { ENTER } from '@wordpress/keycodes';

function SiteDescriptionEdit( { insertDefaultBlock } ) {
const [ description, setDescription ] = useEntityProp( 'root', 'site', 'description' );
const [ isDirty, isSaving, save ] = __experimentalUseEntitySaving(
'root',
'site',
'description'
);

const preventNewlines = ( event ) => {
if ( event.keyCode === ENTER ) {
event.preventDefault();
insertDefaultBlock();
}
};

return (
<>
<Button
isPrimary
className="wp-block-site-description__save-button"
disabled={ ! isDirty || ! description }
isBusy={ isSaving }
onClick={ save }
>
{ __( 'Update' ) }
</Button>
<PlainText
placeholder={ __( 'Site Description' ) }
value={ description }
onChange={ setDescription }
isStylable
onKeyDown={ preventNewlines }
/>
</>
);
}

export default compose( [
withSelect( ( select, { clientId } ) => {
const { getBlockIndex, getBlockRootClientId } = select( 'core/block-editor' );
const rootClientId = getBlockRootClientId( clientId );
return {
blockIndex: getBlockIndex( clientId, rootClientId ),
rootClientId,
};
} ),
withDispatch( ( dispatch, { blockIndex, rootClientId } ) => ( {
insertDefaultBlock: () =>
dispatch( 'core/block-editor' ).insertDefaultBlock( undefined, rootClientId, blockIndex + 1 ),
} ) ),
] )( SiteDescriptionEdit );
6 changes: 6 additions & 0 deletions packages/block-library/src/site-description/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.wp-block-site-description__save-button {
position: absolute;
right: 0;
top: 0;
z-index: z-index(".wp-block-site-description__save-button");
}
11 changes: 11 additions & 0 deletions packages/block-library/src/site-description/icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { SVG, Path } from '@wordpress/components';

export default (
<SVG xmlns="http://www.w3.org/2000/svg" width="24" height="24">
<Path fill="none" d="M0 0h24v24H0z" />
<Path d="M4 9h16v2H4V9zm0 4h10v2H4v-2z" />
</SVG>
);
20 changes: 20 additions & 0 deletions packages/block-library/src/site-description/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import metadata from './block.json';
import icon from './icon';
import edit from './edit';

const { name } = metadata;
export { metadata, name };

export const settings = {
title: __( 'Site Description' ),
icon,
edit,
};

0 comments on commit 7531800

Please sign in to comment.