Skip to content
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

Set font size slider to 1 as default, em units #3090

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions blocks/api/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ export const children = withKnownSourceFlag( ( selector ) => {
return [];
};
} );
export const childrenFirstMatch = withKnownSourceFlag( ( selectors ) => {
return ( domNode ) => {
let match = [];

selectors.forEach( ( selector ) => {
if ( match.length === 0 ) {
match = children( selector )( domNode );
}
} );

return match;
};
} );
export const node = withKnownSourceFlag( ( selector ) => {
return ( domNode ) => {
let match = domNode;
Expand Down
16 changes: 16 additions & 0 deletions blocks/api/test/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ describe( 'sources', () => {
} );
} );

describe( 'childrenFirstMatch()', () => {
it( 'should return a source function', () => {
const source = sources.childrenFirstMatch();

expect( typeof source ).toBe( 'function' );
} );

it( 'should return HTML equivalent WPElement of the first selector that matches', () => {
const html = '<p><span><b>I am the content.</b></span></p>';
const selectors = [ 'p > div', 'p > span', 'p' ];
const match = parse( html, sources.childrenFirstMatch( selectors ) );

expect( renderToString( match ) ).toBe( '<b>I am the content.</b>' );
} );
} );

describe( 'node()', () => {
it( 'should return a source function', () => {
const source = sources.node();
Expand Down
3 changes: 2 additions & 1 deletion blocks/inspector-controls/range-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { __ } from '@wordpress/i18n';
import BaseControl from './../base-control';
import './style.scss';

function RangeControl( { label, value, instanceId, onChange, beforeIcon, afterIcon, help, allowReset, ...props } ) {
function RangeControl( { label, value, instanceId, onChange, beforeIcon, afterIcon, help, allowReset, units, ...props } ) {
const id = 'inspector-range-control-' + instanceId;
const onChangeValue = ( event ) => onChange( Number( event.target.value ) );

Expand All @@ -33,6 +33,7 @@ function RangeControl( { label, value, instanceId, onChange, beforeIcon, afterIc
value={ value }
{ ...props }
/>
{ units }
{ allowReset &&
<Button onClick={ () => onChange() } disabled={ value === undefined }>
{ __( 'Reset' ) }
Expand Down
35 changes: 24 additions & 11 deletions blocks/library/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ import RangeControl from '../../inspector-controls/range-control';
import ColorPalette from '../../color-palette';
import BlockDescription from '../../block-description';

const { children } = source;
const { childrenFirstMatch } = source;
const MAX_FONT_SIZE = 10;

const wrapFontSize = function( content, fontSize ) {
if ( fontSize && fontSize !== 1 ) {
const styles = {
fontSize: fontSize + 'em',
};
const wrappedContent = <span style={ styles }>{ content }</span>;
return [ wrappedContent ];
}
return content;
};

registerBlockType( 'core/paragraph', {
title: __( 'Paragraph' ),
Expand All @@ -42,7 +54,7 @@ registerBlockType( 'core/paragraph', {
attributes: {
content: {
type: 'array',
source: children( 'p' ),
source: childrenFirstMatch( [ 'p > span', 'p' ] ),
},
align: {
type: 'string',
Expand Down Expand Up @@ -95,10 +107,11 @@ registerBlockType( 'core/paragraph', {
},

edit( { attributes, setAttributes, insertBlocksAfter, focus, setFocus, mergeBlocks, onReplace } ) {
const { align, content, dropCap, placeholder, fontSize, backgroundColor, textColor, width } = attributes;
const { align, content, dropCap, placeholder, backgroundColor, textColor, width } = attributes;
const toggleDropCap = () => setAttributes( { dropCap: ! dropCap } );
const className = dropCap ? 'has-drop-cap' : null;

// if the font size was previously set in pixels, divide by 16 to get em
const fontSize = attributes.fontSize >= MAX_FONT_SIZE ? Math.max( MAX_FONT_SIZE, attributes.fontSize / 16 ) : attributes.fontSize;
return [
focus && (
<BlockControls key="controls">
Expand All @@ -123,11 +136,13 @@ registerBlockType( 'core/paragraph', {
/>
<RangeControl
label={ __( 'Font Size' ) }
value={ fontSize || '' }
value={ fontSize || 1 }
onChange={ ( value ) => setAttributes( { fontSize: value } ) }
min={ 10 }
max={ 200 }
min={ 0.5 }
max={ MAX_FONT_SIZE }
step={ 0.1 }
beforeIcon="editor-textcolor"
units="em"
allowReset
/>
</PanelBody>
Expand Down Expand Up @@ -162,10 +177,9 @@ registerBlockType( 'core/paragraph', {
style={ {
backgroundColor: backgroundColor,
color: textColor,
fontSize: fontSize ? fontSize + 'px' : undefined,
textAlign: align,
} }
value={ content }
value={ wrapFontSize( content, fontSize ) }
onChange={ ( nextContent ) => {
setAttributes( {
content: nextContent,
Expand Down Expand Up @@ -198,11 +212,10 @@ registerBlockType( 'core/paragraph', {
const styles = {
backgroundColor: backgroundColor,
color: textColor,
fontSize: fontSize,
textAlign: align,
};

return <p style={ styles } className={ className ? className : undefined }>{ content }</p>;
return <p style={ styles } className={ className ? className : undefined }>{ wrapFontSize( content, fontSize ) }</p>;
},
} );

Expand Down