From 4385691a0cd297c101d8877d6ca40c10bfbfb98d Mon Sep 17 00:00:00 2001 From: iseulde Date: Fri, 15 Dec 2017 13:19:11 +0100 Subject: [PATCH] Add subhead block --- blocks/library/index.js | 1 + blocks/library/subhead/editor.scss | 3 + blocks/library/subhead/index.js | 88 +++++++++++++++++++ blocks/test/fixtures/core__subhead.html | 3 + blocks/test/fixtures/core__subhead.json | 18 ++++ .../test/fixtures/core__subhead.parsed.json | 11 +++ .../fixtures/core__subhead.serialized.html | 3 + 7 files changed, 127 insertions(+) create mode 100644 blocks/library/subhead/editor.scss create mode 100644 blocks/library/subhead/index.js create mode 100644 blocks/test/fixtures/core__subhead.html create mode 100644 blocks/test/fixtures/core__subhead.json create mode 100644 blocks/test/fixtures/core__subhead.parsed.json create mode 100644 blocks/test/fixtures/core__subhead.serialized.html diff --git a/blocks/library/index.js b/blocks/library/index.js index 2076db1f9dc5ad..e87daedbc06731 100644 --- a/blocks/library/index.js +++ b/blocks/library/index.js @@ -23,3 +23,4 @@ import './video'; import './audio'; import './block'; import './paragraph'; +import './subhead'; diff --git a/blocks/library/subhead/editor.scss b/blocks/library/subhead/editor.scss new file mode 100644 index 00000000000000..6d359f4819dc86 --- /dev/null +++ b/blocks/library/subhead/editor.scss @@ -0,0 +1,3 @@ +.editor-visual-editor p.wp-block-subhead { + font-size: 1.1em; +} diff --git a/blocks/library/subhead/index.js b/blocks/library/subhead/index.js new file mode 100644 index 00000000000000..ab143b758b8846 --- /dev/null +++ b/blocks/library/subhead/index.js @@ -0,0 +1,88 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import './editor.scss'; +import { registerBlockType, createBlock } from '../../api'; +import Editable from '../../editable'; +import InspectorControls from '../../inspector-controls'; +import BlockDescription from '../../block-description'; + +registerBlockType( 'core/subhead', { + title: __( 'Subhead' ), + + icon: 'text', + + category: 'common', + + attributes: { + content: { + type: 'array', + source: 'children', + selector: 'p', + }, + }, + + transforms: { + from: [ + { + type: 'block', + blocks: [ 'core/paragraph' ], + transform: ( { content } ) => { + return createBlock( 'core/subhead', { + content, + } ); + }, + }, + ], + to: [ + { + type: 'block', + blocks: [ 'core/paragraph' ], + transform: ( { content } ) => { + return createBlock( 'core/paragraph', { + content, + } ); + }, + }, + ], + }, + + edit( { attributes, setAttributes, focus, setFocus, className } ) { + const { content, placeholder } = attributes; + + return [ + focus && ( + + +

{ __( 'Explanatory text under the main heading of an article.' ) }

+
+
+ ), + { + setAttributes( { + content: nextContent, + } ); + } } + focus={ focus } + onFocus={ setFocus } + className={ className } + placeholder={ placeholder || __( 'Write subhead…' ) } + />, + ]; + }, + + save( { attributes, className } ) { + const { content } = attributes; + + return

{ content }

; + }, +} ); diff --git a/blocks/test/fixtures/core__subhead.html b/blocks/test/fixtures/core__subhead.html new file mode 100644 index 00000000000000..61cf93189617ec --- /dev/null +++ b/blocks/test/fixtures/core__subhead.html @@ -0,0 +1,3 @@ + +

This is a subhead.

+ diff --git a/blocks/test/fixtures/core__subhead.json b/blocks/test/fixtures/core__subhead.json new file mode 100644 index 00000000000000..20e5037001563c --- /dev/null +++ b/blocks/test/fixtures/core__subhead.json @@ -0,0 +1,18 @@ +[ + { + "uid": "_uid_0", + "name": "core/subhead", + "isValid": true, + "attributes": { + "content": [ + "This is a ", + { + "type": "em", + "children": "subhead" + }, + "." + ] + }, + "originalContent": "

This is a subhead.

" + } +] diff --git a/blocks/test/fixtures/core__subhead.parsed.json b/blocks/test/fixtures/core__subhead.parsed.json new file mode 100644 index 00000000000000..a36ff7083edec0 --- /dev/null +++ b/blocks/test/fixtures/core__subhead.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/subhead", + "attrs": null,"innerBlocks": [], + "innerHTML": "\n

This is a subhead.

\n" + }, + { + "attrs": {}, + "innerHTML": "\n" + } +] diff --git a/blocks/test/fixtures/core__subhead.serialized.html b/blocks/test/fixtures/core__subhead.serialized.html new file mode 100644 index 00000000000000..23468192081a0c --- /dev/null +++ b/blocks/test/fixtures/core__subhead.serialized.html @@ -0,0 +1,3 @@ + +

This is a subhead.

+