-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try introducing nested blocks for amp-story support
- Loading branch information
1 parent
014e0a0
commit 56df805
Showing
17 changed files
with
473 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
.editor-block-list__layout { | ||
background-color: #ffdddd; | ||
} | ||
.editor-block-list__layout .editor-block-list__layout { | ||
background-color: white; | ||
} | ||
div[data-type="amp/amp-story-page"] { | ||
background-color: #ddffdd; | ||
} | ||
div[data-type="amp/amp-story-grid-layer"] { | ||
background-color: #ddddff; | ||
} | ||
|
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,84 @@ | ||
const { __ } = wp.i18n; | ||
const { | ||
registerBlockType | ||
} = wp.blocks; | ||
const { | ||
InspectorControls, | ||
InnerBlocks | ||
} = wp.editor; | ||
const { | ||
SelectControl | ||
} = wp.components; | ||
|
||
/** | ||
* Register block. | ||
*/ | ||
export default registerBlockType( | ||
'amp/amp-story-grid-layer', | ||
{ | ||
title: __( 'AMP Story Grid Layer' ), | ||
category: 'layout', | ||
icon: 'grid-view', | ||
|
||
attributes: { | ||
template: { | ||
type: 'string', | ||
source: 'attribute', | ||
selector: 'amp-story-grid-layer', | ||
attribute: 'template', | ||
default: 'fill' | ||
} | ||
}, | ||
|
||
/* | ||
* <amp-story-grid-layer>: | ||
* mandatory_ancestor: "AMP-STORY-PAGE" | ||
* descendant_tag_list: "amp-story-grid-layer-allowed-descendants" | ||
* | ||
* https://github.com/ampproject/amphtml/blob/87fe1d02f902be97b596b36ec3421592c83d241e/extensions/amp-story/validator-amp-story.protoascii#L172-L188 | ||
*/ | ||
|
||
edit( props ) { | ||
const { isSelected, setAttributes } = props; | ||
return [ | ||
isSelected && ( | ||
<InspectorControls key='inspector'> | ||
<SelectControl | ||
key="template" | ||
label={ __( 'Template', 'amp' ) } | ||
value={ props.attributes.template } | ||
options={ [ | ||
{ | ||
value: 'fill', | ||
label: __( 'Fill', 'amp' ) | ||
}, | ||
{ | ||
value: 'horizontal', | ||
label: __( 'Horizontal', 'amp' ) | ||
}, | ||
{ | ||
value: 'thirds', | ||
label: __( 'Thirds', 'amp' ) | ||
}, | ||
{ | ||
value: 'vertical', | ||
label: __( 'Vertical', 'amp' ) | ||
} | ||
] } | ||
onChange={ value => ( setAttributes( { template: value } ) ) } | ||
/> | ||
</InspectorControls> | ||
), | ||
<InnerBlocks key='contents' /> | ||
]; | ||
}, | ||
|
||
save( { attributes } ) { | ||
return ( | ||
<amp-story-grid-layer template={ attributes.template }> | ||
<InnerBlocks.Content /> | ||
</amp-story-grid-layer> | ||
); | ||
} | ||
} | ||
); |
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,76 @@ | ||
const { __ } = wp.i18n; | ||
const { | ||
registerBlockType | ||
} = wp.blocks; | ||
const { | ||
InspectorControls, | ||
InnerBlocks | ||
} = wp.editor; | ||
const { | ||
TextControl, | ||
Notice | ||
} = wp.components; | ||
|
||
/** | ||
* Register block. | ||
*/ | ||
export default registerBlockType( | ||
'amp/amp-story-page', | ||
{ | ||
title: __( 'AMP Story Page', 'amp' ), | ||
category: 'layout', | ||
icon: 'admin-page', | ||
|
||
attributes: { | ||
id: { | ||
type: 'string', | ||
source: 'attribute', | ||
selector: 'amp-story-page', | ||
attribute: 'id' | ||
} | ||
}, | ||
|
||
/* | ||
* <amp-story-page>: | ||
* mandatory_parent: "AMP-STORY" | ||
* mandatory_min_num_child_tags: 1 | ||
* child_tag_name_oneof: "AMP-ANALYTICS" | ||
* child_tag_name_oneof: "AMP-PIXEL" | ||
* child_tag_name_oneof: "AMP-STORY-CTA-LAYER" | ||
* child_tag_name_oneof: "AMP-STORY-GRID-LAYER" | ||
* | ||
* https://github.com/ampproject/amphtml/blob/87fe1d02f902be97b596b36ec3421592c83d241e/extensions/amp-story/validator-amp-story.protoascii#L146-L171 | ||
* */ | ||
|
||
// @todo Show error if no ID is supplied. | ||
edit( props ) { | ||
const { isSelected, setAttributes } = props; | ||
return [ | ||
isSelected && ( | ||
<InspectorControls key='inspector'> | ||
<TextControl | ||
type="text" | ||
className="blocks-amp-story-page__id" | ||
required={ true } | ||
label={ __( 'ID', 'amp' ) } | ||
value={ props.attributes.id } | ||
onChange={ value => ( setAttributes( { id: value } ) ) } | ||
/> | ||
</InspectorControls> | ||
), | ||
! props.attributes.id && ( | ||
<Notice status="error" isDismissible={ false }>{ __( 'You must supply an ID for the page.', 'amp' ) }</Notice> | ||
), | ||
<InnerBlocks key='contents' /> | ||
]; | ||
}, | ||
|
||
save( { attributes } ) { | ||
return ( | ||
<amp-story-page id={ attributes.id }> | ||
<InnerBlocks.Content /> | ||
</amp-story-page> | ||
); | ||
} | ||
} | ||
); |
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,44 @@ | ||
import './amp-story-page'; | ||
import './amp-story-grid-layer'; | ||
|
||
const blockParents = { | ||
'amp/amp-story-grid-layer': 'amp-story-page', | ||
'core/button': 'amp/amp-story-grid-layer', | ||
'core/code': 'amp/amp-story-grid-layer', | ||
'core/embed': 'amp/amp-story-grid-layer', | ||
'core/image': 'amp/amp-story-grid-layer', | ||
'core/list': 'amp/amp-story-grid-layer', | ||
'core/paragraph': 'amp/amp-story-grid-layer', | ||
'core/preformatted': 'amp/amp-story-grid-layer', | ||
'core/pullquote': 'amp/amp-story-grid-layer', | ||
'core/quote': 'amp/amp-story-grid-layer', | ||
'core/table': 'amp/amp-story-grid-layer', | ||
'core/verse': 'amp/amp-story-grid-layer', | ||
'core/video': 'amp/amp-story-grid-layer' | ||
}; | ||
|
||
function setBlockParent( props ) { | ||
if ( blockParents[ props.name ] ) { | ||
return Object.assign( | ||
{}, | ||
props, | ||
{ parent: [ blockParents[ props.name ] ] } | ||
); | ||
} | ||
return props; | ||
} | ||
|
||
wp.hooks.addFilter( | ||
'blocks.registerBlockType', | ||
'amp/set-block-parents', | ||
setBlockParent | ||
); | ||
|
||
// Remove all blocks that are not known to be allowed in AMP Stories (ref. amp-story-cta-layer-allowed-descendants). | ||
window.addEventListener( 'load', () => { // @todo Should be better event. | ||
wp.blocks.getBlockTypes().forEach( function( blockType ) { | ||
if ( 'amp/amp-story-page' !== blockType.name && ! blockParents[ blockType.name ] ) { | ||
wp.blocks.unregisterBlockType( blockType.name ); | ||
} | ||
} ); | ||
} ); |
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 @@ | ||
/** | ||
* Import blocks. | ||
*/ | ||
import './amp-story'; |
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
Oops, something went wrong.