Skip to content

Commit

Permalink
Add: Option to select the style that is automatically applied (#16465)
Browse files Browse the repository at this point in the history
Squashed commits:
[ffd16b5] Add unit tests
[51dd9dc] Refactor & changed the design
[3b9ac24] Add: Option to allow user to select the style that is automatically applied.
  • Loading branch information
jorgefilipecosta authored and gziolo committed Aug 29, 2019
1 parent c8ed262 commit 7bc32cc
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 30 deletions.
13 changes: 13 additions & 0 deletions docs/designers-developers/developers/data/data-core-edit-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,17 @@ _Returns_

- `Object`: Action object

<a name="updatePreferredStyleVariations" href="#updatePreferredStyleVariations">#</a> **updatePreferredStyleVariations**

Returns an action object used in signaling that a style should be auto-applied when a block is created.

_Parameters_

- _blockName_ `string`: Name of the block.
- _blockStyle_ `?string`: Name of the style that should be auto applied. If undefined, the "auto apply" setting of the block is removed.

_Returns_

- `Object`: Action object.

<!-- END TOKEN(Autogenerated actions) -->
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import InspectorControls from '../inspector-controls';
import InspectorAdvancedControls from '../inspector-advanced-controls';
import BlockStyles from '../block-styles';
import MultiSelectionInspector from '../multi-selection-inspector';
import DefaultStylePicker from '../default-style-picker';
const BlockInspector = ( {
blockType,
count,
Expand Down Expand Up @@ -61,6 +62,7 @@ const BlockInspector = ( {
<BlockStyles
clientId={ selectedBlockClientId }
/>
<DefaultStylePicker blockName={ blockType.name } />
</PanelBody>
</div>
) }
Expand Down
60 changes: 60 additions & 0 deletions packages/block-editor/src/components/default-style-picker/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
import { useMemo, useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { SelectControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';

export default function DefaultStylePicker( { blockName } ) {
const {
preferredStyle,
onUpdatePreferredStyleVariations,
styles,
} = useSelect(
( select ) => {
const settings = select( 'core/block-editor' ).getSettings();
const preferredStyleVariations = settings.__experimentalPreferredStyleVariations;
return {
preferredStyle: get(
preferredStyleVariations,
[ 'value', blockName ]
),
onUpdatePreferredStyleVariations: get(
preferredStyleVariations,
[ 'onChange' ],
null
),
styles: select( 'core/blocks' ).getBlockStyles( blockName ),
};
},
[ blockName ]
);
const selectOptions = useMemo(
() => ( [
{ label: __( 'Not set' ), value: '' },
...styles.map( ( { label, name } ) => ( { label, value: name } ) ),
] ),
[ styles ],
);
const selectOnChange = useCallback(
( blockStyle ) => {
onUpdatePreferredStyleVariations( blockName, blockStyle );
},
[ blockName, onUpdatePreferredStyleVariations ]
);

return onUpdatePreferredStyleVariations && (
<SelectControl
options={ selectOptions }
value={ preferredStyle || '' }
label={ __( 'Default Style' ) }
onChange={ selectOnChange }
/>
);
}
45 changes: 42 additions & 3 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { castArray, first } from 'lodash';
import { castArray, first, get, includes } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -218,6 +218,33 @@ export function toggleSelection( isSelectionEnabled = true ) {
};
}

function getBlocksWithDefaultStylesApplied( blocks, blockEditorSettings ) {
const preferredStyleVariations = get(
blockEditorSettings,
[ '__experimentalPreferredStyleVariations', 'value' ],
{}
);
return blocks.map( ( block ) => {
const blockName = block.name;
if ( ! preferredStyleVariations[ blockName ] ) {
return block;
}
const className = get( block, [ 'attributes', 'className' ] );
if ( includes( className, 'is-style-' ) ) {
return block;
}
const { attributes = {} } = block;
const blockStyle = preferredStyleVariations[ blockName ];
return {
...block,
attributes: {
...attributes,
className: `${ ( className || '' ) } is-style-${ blockStyle }`.trim(),
},
};
} );
}

/**
* Returns an action object signalling that a blocks should be replaced with
* one or more replacement blocks.
Expand All @@ -231,7 +258,13 @@ export function toggleSelection( isSelectionEnabled = true ) {
*/
export function* replaceBlocks( clientIds, blocks, indexToSelect ) {
clientIds = castArray( clientIds );
blocks = castArray( blocks );
blocks = getBlocksWithDefaultStylesApplied(
castArray( blocks ),
yield select(
'core/block-editor',
'getSettings',
)
);
const rootClientId = yield select(
'core/block-editor',
'getBlockRootClientId',
Expand Down Expand Up @@ -398,7 +431,13 @@ export function* insertBlocks(
rootClientId,
updateSelection = true
) {
blocks = castArray( blocks );
blocks = getBlocksWithDefaultStylesApplied(
castArray( blocks ),
yield select(
'core/block-editor',
'getSettings',
)
);
const allowedBlocks = [];
for ( const block of blocks ) {
const isValid = yield select(
Expand Down
Loading

0 comments on commit 7bc32cc

Please sign in to comment.