-
Notifications
You must be signed in to change notification settings - Fork 385
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 13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.is-amp-fit-text + .blocks-font-size > .components-font-size-picker__buttons, | ||
.is-amp-fit-text + .blocks-font-size > .components-font-size-picker__custom-input { | ||
display: none; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,19 @@ var ampEditorBlocks = ( function() { // eslint-disable-line no-unused-vars | |
'core/image', | ||
'core/video', | ||
'core/audio' | ||
] | ||
], | ||
textBlocks: [ | ||
'core/paragraph', | ||
'core/heading', | ||
'core/code', | ||
'core/quote', | ||
'core/subhead' | ||
], | ||
ampSettingsLabel: __( 'AMP Settings' ), | ||
fontSizes: { | ||
small: 14, | ||
larger: 48 | ||
} | ||
} | ||
}; | ||
|
||
|
@@ -178,6 +190,38 @@ var ampEditorBlocks = ( function() { // eslint-disable-line no-unused-vars | |
}; | ||
} | ||
|
||
// 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', | ||
default: component.data.fontSizes.small, | ||
source: 'attribute', | ||
selector: 'amp-fit-text', | ||
attribute: 'min-font-size' | ||
}; | ||
settings.attributes.maxFont = { | ||
type: 'number', | ||
default: component.data.fontSizes.larger, | ||
source: 'attribute', | ||
selector: 'amp-fit-text', | ||
attribute: 'max-font-size' | ||
}; | ||
settings.attributes.height = { | ||
type: 'number', | ||
default: 50, | ||
source: 'attribute', | ||
selector: 'amp-fit-text', | ||
attribute: 'height' | ||
}; | ||
} | ||
|
||
// Layout settings for embeds and media blocks. | ||
if ( 0 === name.indexOf( 'core-embed' ) || -1 !== component.data.mediaBlocks.indexOf( name ) ) { | ||
if ( ! settings.attributes ) { | ||
|
@@ -222,6 +266,8 @@ var ampEditorBlocks = ( function() { // eslint-disable-line no-unused-vars | |
} | ||
} 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 +360,135 @@ var ampEditorBlocks = ( function() { // eslint-disable-line no-unused-vars | |
); | ||
}; | ||
|
||
/** | ||
* 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.editor.InspectorControls, | ||
TextControl = wp.components.TextControl, | ||
FontSizePicker = wp.components.FontSizePicker, | ||
ToggleControl = wp.components.ToggleControl, | ||
PanelBody = wp.components.PanelBody, | ||
label = __( 'Use AMP Fit Text' ), | ||
FONT_SIZES = [ | ||
{ | ||
name: 'small', | ||
shortName: __( 'S' ), | ||
size: 14 | ||
}, | ||
{ | ||
name: 'regular', | ||
shortName: __( 'M' ), | ||
size: 16 | ||
}, | ||
{ | ||
name: 'large', | ||
shortName: __( 'L' ), | ||
size: 36 | ||
}, | ||
{ | ||
name: 'larger', | ||
shortName: __( 'XL' ), | ||
size: 48 | ||
} | ||
]; | ||
|
||
if ( ampFitText ) { | ||
return isSelected && ( | ||
el( InspectorControls, { key: 'inspector' }, | ||
el( PanelBody, { title: component.data.ampSettingsLabel, className: 'is-amp-fit-text' }, | ||
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. 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' ), | ||
value: height, | ||
type: 'number', | ||
min: 1, | ||
onChange: function( nextHeight ) { | ||
props.setAttributes( { height: nextHeight } ); | ||
} | ||
} ), | ||
maxFont > height && el( | ||
wp.components.Notice, | ||
{ | ||
status: 'error', | ||
isDismissible: false | ||
}, | ||
__( 'The height must be greater than the max font size.' ) | ||
), | ||
el( PanelBody, { title: __( 'Minimum font size' ) }, | ||
el( FontSizePicker, { | ||
fallbackFontSize: 14, | ||
value: minFont, | ||
fontSizes: FONT_SIZES, | ||
onChange: function( nextMinFont ) { | ||
if ( ! nextMinFont ) { | ||
nextMinFont = component.data.fontSizes.small; // @todo Supplying fallbackFontSize should be done automatically by the component? | ||
} | ||
if ( nextMinFont <= maxFont ) { | ||
props.setAttributes( { minFont: nextMinFont } ); | ||
} | ||
} | ||
} ) | ||
), | ||
minFont > maxFont && el( | ||
wp.components.Notice, | ||
{ | ||
status: 'error', | ||
isDismissible: false | ||
}, | ||
__( 'The min font size must less than the max font size.' ) | ||
), | ||
el( PanelBody, { title: __( 'Maximum font size' ) }, | ||
el( FontSizePicker, { | ||
value: maxFont, | ||
fallbackFontSize: 48, | ||
fontSizes: FONT_SIZES, | ||
onChange: function( nextMaxFont ) { | ||
if ( ! nextMaxFont ) { | ||
nextMaxFont = component.data.fontSizes.larger; // @todo Supplying fallbackFontSize should be done automatically by the component? | ||
} | ||
props.setAttributes( { | ||
maxFont: nextMaxFont, | ||
height: Math.max( nextMaxFont, height ) | ||
} ); | ||
} | ||
} ) | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
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. | ||
|
@@ -325,7 +500,7 @@ var ampEditorBlocks = ( function() { // eslint-disable-line no-unused-vars | |
var ampCarousel = props.attributes.ampCarousel, | ||
isSelected = props.isSelected, | ||
el = wp.element.createElement, | ||
InspectorControls = wp.blocks.InspectorControls, | ||
InspectorControls = wp.editor.InspectorControls, | ||
ToggleControl = wp.components.ToggleControl, | ||
PanelBody = wp.components.PanelBody, | ||
toggleControl; | ||
|
@@ -359,7 +534,12 @@ var ampEditorBlocks = ( function() { // eslint-disable-line no-unused-vars | |
* @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 +570,17 @@ var ampEditorBlocks = ( function() { // eslint-disable-line no-unused-vars | |
{}, | ||
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.
Should
ampFitText
be replaced withevent.checked
?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.
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?