Skip to content

Commit

Permalink
Create Block: Add a way to provide a default value for attributes and…
Browse files Browse the repository at this point in the history
… supports
  • Loading branch information
gziolo committed Feb 10, 2021
1 parent c23fe11 commit 5b6d8a1
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 42 deletions.
10 changes: 10 additions & 0 deletions packages/create-block-tutorial-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ module.exports = {
description:
'A Gutenberg block to show your pride! This block enables you to type text and style it with the color font Gilbert from Type with Pride.',
dashicon: 'flag',
attributes: {
message: {
type: 'string',
source: 'text',
selector: 'div',
},
},
supports: {
html: false,
},
npmDependencies: [
'@wordpress/block-editor',
'@wordpress/blocks',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,6 @@ import save from './save';
* @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
*/
registerBlockType( '{{namespace}}/{{slug}}', {
/**
* Attributes are the way a block stores data, they define how a block is parsed
* to extract data from the saved content. When the block loads it will look
* at the saved content for the block, look for the `div` tag, take the text portion,
* and store the content in an `attributes.message` variable.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/block-attributes/
*/
attributes: {
message: {
type: 'string',
source: 'text',
selector: 'div',
},
},

/**
* Optional block extended support features.
*/
supports: {
// Removes support for an HTML mode.
html: false,
},

/**
* Used to construct a preview for the block to be shown in the block inserter.
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Features

- Add a way to provide a default value in the template for `attributes` and `supports` Block API fields ([#28883](https://github.com/WordPress/gutenberg/pull/28883)).

### Enhancement

- Block scaffolded with `esnext` template is now registered from `block.json` with the `register_block_type_from_metadata` helper ([#28883](https://github.com/WordPress/gutenberg/pull/28883)).
Expand Down
12 changes: 7 additions & 5 deletions packages/create-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ module.exports = {

The following configurable variables are used with the template files. Template authors can change default values to use when users don't provide their data:

- `apiVersion` (default: `2`)
- `apiVersion` (default: `2`) - see https://make.wordpress.org/core/2020/11/18/block-api-version-2/.
- `slug` (no default)
- `namespace` (default: `'create-block'`)
- `title` (no default)
- `description` (no default)
- `dashicon` (no default)
- `category` (default: `'widgets'`)
- `title` (no default) - a display title for your block.
- `description` (no default) - a short description for your block.
- `dashicon` (no default) - an icon property thats makes it easier to identify a block, see https://developer.wordpress.org/resource/dashicons/.
- `category` (default: `'widgets'`) - blocks are grouped into categories to help users browse and discover them. The categories provided by core are `text`, `media`, `design`, `widgets`, and `embed`.
- `attributes` (no default) - see https://developer.wordpress.org/block-editor/developers/block-api/block-attributes/.
- `supports` (no default) - optional block extended support features, see https://developer.wordpress.org/block-editor/developers/block-api/block-supports/.
- `author` (default: `'The WordPress Contributors'`)
- `license` (default: `'GPL-2.0-or-later'`)
- `licenseURI` (default: `'https://www.gnu.org/licenses/gpl-2.0.html'`)
Expand Down
11 changes: 6 additions & 5 deletions packages/create-block/lib/init-block-json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
const { omitBy } = require( 'lodash' );
const { isEmpty, omitBy } = require( 'lodash' );
const { join } = require( 'path' );
const { writeFile } = require( 'fs' ).promises;

Expand All @@ -17,6 +17,8 @@ module.exports = async ( {
title,
description,
category,
attributes,
supports,
dashicon,
textdomain,
editorScript,
Expand All @@ -37,15 +39,14 @@ module.exports = async ( {
category,
icon: dashicon,
description,
attributes,
supports,
textdomain,
supports: {
html: false,
},
editorScript,
editorStyle,
style,
},
( value ) => ! value
isEmpty
),
null,
'\t'
Expand Down
4 changes: 4 additions & 0 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module.exports = async (
description,
dashicon,
category,
attributes,
supports,
author,
license,
licenseURI,
Expand Down Expand Up @@ -55,6 +57,8 @@ module.exports = async (
description,
dashicon,
category,
attributes,
supports,
version,
author,
license,
Expand Down
3 changes: 3 additions & 0 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const predefinedBlockTemplates = {
description:
'Example block written with ESNext standard and JSX support – build step required.',
dashicon: 'smiley',
supports: {
html: false,
},
npmDependencies: [
'@wordpress/block-editor',
'@wordpress/blocks',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ import save from './save';
* @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
*/
registerBlockType( '{{namespace}}/{{slug}}', {
/**
* Optional block extended support features.
*/
supports: {
// Removes support for an HTML mode.
html: false,
},

/**
* @see ./edit.js
*/
Expand Down

0 comments on commit 5b6d8a1

Please sign in to comment.