-
Notifications
You must be signed in to change notification settings - Fork 383
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
[Gutenberg] Add amp-fit-text support to text blocks #1151
Changes from 4 commits
40e9982
bba71a9
92eeca2
4f814c3
91448b0
d1c2065
ed79521
78ff7aa
8e0f249
d76a23e
5a45e80
c3d9915
937035f
03ebbf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"boss": true, | ||
"curly": true, | ||
"eqeqeq": true, | ||
"eqnull": true, | ||
"es3": true, | ||
"esversion": 6, | ||
"expr": true, | ||
"immed": true, | ||
"noarg": true, | ||
"nonbsp": true, | ||
"onevar": true, | ||
"quotmark": "single", | ||
"trailing": true, | ||
"undef": true, | ||
"unused": true, | ||
|
||
"browser": true, | ||
|
||
"globals": { | ||
"_": false, | ||
"Backbone": false, | ||
"jQuery": false, | ||
"JSON": false, | ||
"wp": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,15 @@ var ampEditorBlocks = ( function() { | |
'core/image', | ||
'core/video', | ||
'core/audio' | ||
] | ||
], | ||
textBlocks: [ | ||
'core/paragraph', | ||
'core/heading', | ||
'core/code', | ||
'core/quote', | ||
'core/subhead' | ||
], | ||
ampSettingsLabel: __( 'AMP Settings' ) | ||
} | ||
}; | ||
|
||
|
@@ -178,6 +186,27 @@ var ampEditorBlocks = ( function() { | |
}; | ||
} | ||
|
||
// Fit-text for text blocks. | ||
if ( -1 !== component.data.textBlocks.indexOf( name ) ) { | ||
if ( ! settings.attributes ) { | ||
settings.attributes = {}; | ||
} | ||
settings.attributes.ampFitText = { | ||
type: 'boolean', | ||
default: false | ||
}; | ||
settings.attributes.minFont = { | ||
type: 'number' | ||
}; | ||
settings.attributes.maxFont = { | ||
type: 'number' | ||
}; | ||
settings.attributes.height = { | ||
type: 'number', | ||
default: 50 | ||
}; | ||
} | ||
|
||
// Layout settings for embeds and media blocks. | ||
if ( 0 === name.indexOf( 'core-embed' ) || -1 !== component.data.mediaBlocks.indexOf( name ) ) { | ||
if ( ! settings.attributes ) { | ||
|
@@ -222,6 +251,8 @@ var ampEditorBlocks = ( function() { | |
} | ||
} else if ( -1 !== component.data.mediaBlocks.indexOf( name ) || 0 === name.indexOf( 'core-embed/' ) ) { | ||
inspectorControls = component.setUpInspectorControls( props ); | ||
} else if ( -1 !== component.data.textBlocks.indexOf( name ) ) { | ||
inspectorControls = component.setUpTextBlocksInspectorControls( props ); | ||
} | ||
|
||
// Return just inspector controls in case of 'nodisplay'. | ||
|
@@ -314,6 +345,77 @@ var ampEditorBlocks = ( function() { | |
); | ||
}; | ||
|
||
/** | ||
* Setup inspector controls for text blocks. | ||
* | ||
* @param {Object} props Props. | ||
* @return {Object|Element|*|{$$typeof, type, key, ref, props, _owner}} Inspector Controls. | ||
*/ | ||
component.setUpTextBlocksInspectorControls = function setUpInspectorControls( props ) { | ||
var ampFitText = props.attributes.ampFitText, | ||
minFont = props.attributes.minFont, | ||
maxFont = props.attributes.maxFont, | ||
height = props.attributes.height, | ||
isSelected = props.isSelected, | ||
el = wp.element.createElement, | ||
InspectorControls = wp.blocks.InspectorControls, | ||
TextControl = wp.components.TextControl, | ||
ToggleControl = wp.components.ToggleControl, | ||
PanelBody = wp.components.PanelBody, | ||
label = 'Use AMP Fit Text'; | ||
|
||
if ( ampFitText ) { | ||
return isSelected && ( | ||
el( InspectorControls, { key: 'inspector' }, | ||
el( PanelBody, { title: component.data.ampSettingsLabel }, | ||
el( ToggleControl, { | ||
label: label, | ||
checked: ampFitText, | ||
onChange: function() { | ||
props.setAttributes( { ampFitText: ! ampFitText } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the core blocks seem to use negating the current value for toggle controls (as far as I've seen), thought of staying consistent with the core blocks when using that. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this also result in the |
||
} | ||
} ), | ||
el( TextControl, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the |
||
label: __( 'Height (px)' ), | ||
value: height, | ||
onChange: function( nextHeight ) { | ||
props.setAttributes( { height: nextHeight } ); | ||
} | ||
} ), | ||
el( TextControl, { | ||
label: __( 'Min font (px)' ), | ||
value: minFont, | ||
onChange: function( nextMinFont ) { | ||
props.setAttributes( { minFont: nextMinFont } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abort if |
||
} | ||
} ), | ||
el( TextControl, { | ||
label: __( 'Max font (px)' ), | ||
value: maxFont, | ||
onChange: function( nextMaxFont ) { | ||
props.setAttributes( { maxFont: nextMaxFont } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure the |
||
} | ||
} ) | ||
) | ||
) | ||
); | ||
} | ||
|
||
return isSelected && ( | ||
el( InspectorControls, { key: 'inspector' }, | ||
el( PanelBody, { title: component.data.ampSettingsLabel }, | ||
el( ToggleControl, { | ||
label: label, | ||
checked: ampFitText, | ||
onChange: function() { | ||
props.setAttributes( { ampFitText: ! ampFitText } ); | ||
} | ||
} ) | ||
) | ||
) | ||
); | ||
}; | ||
|
||
/** | ||
* Set up inspector controls for shortcode block. | ||
* Adds ampCarousel attribute in case of gallery shortcode. | ||
|
@@ -359,7 +461,12 @@ var ampEditorBlocks = ( function() { | |
* @return {*} Output element. | ||
*/ | ||
component.filterBlocksSave = function filterBlocksSave( element, blockType, attributes ) { | ||
var text; | ||
var text, | ||
fitTextProps = { | ||
layout: 'fixed-height', | ||
children: element | ||
}; | ||
|
||
if ( 'core/shortcode' === blockType.name && component.isGalleryShortcode( attributes ) ) { | ||
if ( attributes.ampCarousel ) { | ||
// If the text contains amp-carousel, lets remove it. | ||
|
@@ -390,6 +497,17 @@ var ampEditorBlocks = ( function() { | |
{}, | ||
text | ||
); | ||
} else if ( -1 !== component.data.textBlocks.indexOf( blockType.name ) && attributes.ampFitText ) { | ||
if ( attributes.minFont ) { | ||
fitTextProps[ 'min-font-size' ] = attributes.minFont; | ||
} | ||
if ( attributes.maxFont ) { | ||
fitTextProps[ 'max-font-size' ] = attributes.maxFont; | ||
} | ||
if ( attributes.height ) { | ||
fitTextProps.height = attributes.height; | ||
} | ||
return wp.element.createElement( 'amp-fit-text', fitTextProps ); | ||
} | ||
return element; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs translation