Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Visual Editor: Adding the global controls concept
Global controls are controls that can be automatically applied to any block Type
  • Loading branch information
youknowriad committed Apr 20, 2017
commit 8899090fc623eea567939d3aaab3c5e26a3d734e
7 changes: 6 additions & 1 deletion blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ export function parseBlockAttributes( rawContent, blockSettings ) {
* @return {Object} All block attributes
*/
export function getBlockAttributes( blockSettings, rawContent, attributes ) {
// Handle global controls
const contentToParse = attributes.position
? query.parse( rawContent, query.html( 'div' ) )
: rawContent;

// Merge any attributes from comment delimiters with block implementation
attributes = attributes || {};
if ( blockSettings ) {
attributes = {
...attributes,
...parseBlockAttributes( rawContent, blockSettings ),
...parseBlockAttributes( contentToParse, blockSettings ),
};
}

Expand Down
5 changes: 4 additions & 1 deletion blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export default function serialize( blocks ) {
const blockType = block.blockType;
const settings = getBlockSettings( blockType );
const saveContent = getSaveContent( settings.save, block.attributes );
const wrappedSavedContent = block.attributes.position
? `<div classname="align${ block.attributes.position }">${ saveContent }</div>`
: saveContent;

return memo + (
'<!-- wp:' +
Expand All @@ -79,7 +82,7 @@ export default function serialize( blocks ) {
parseBlockAttributes( saveContent, settings )
) +
'-->' +
saveContent +
wrappedSavedContent +
'<!-- /wp:' +
blockType +
' -->'
Expand Down
35 changes: 35 additions & 0 deletions blocks/controls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function applyOrUnset( position ) {
return ( attributes, setAttributes ) => {
const nextPosition = attributes.position === position ? undefined : position;
setAttributes( { position: nextPosition } );
};
}

const controls = [
{
icon: 'align-left',
title: wp.i18n.__( 'Position left' ),
isActive: ( { position } ) => 'left' === position,
onClick: applyOrUnset( 'left' )
},
{
icon: 'align-center',
title: wp.i18n.__( 'Position center' ),
isActive: ( { position } ) => 'center' === position,
onClick: applyOrUnset( 'center' )
},
{
icon: 'align-right',
title: wp.i18n.__( 'Position right' ),
isActive: ( { position } ) => 'right' === position,
onClick: applyOrUnset( 'right' )
},
{
icon: 'align-none',
title: wp.i18n.__( 'No positionning' ),
isActive: ( { position } ) => ! position || 'none' === position,
onClick: applyOrUnset( 'none' )
}
];

export default controls;
1 change: 1 addition & 0 deletions blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import './library';

export * from './api';
export * from './components';
export { default as controls } from './controls';
8 changes: 5 additions & 3 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function VisualEditorBlock( props ) {
const className = classnames( 'editor-visual-editor__block', {
'is-selected': isSelected,
'is-hovered': isHovered
} );
}, block.attributes.position && `align${ block.attributes.position }` );

const { onChange, onSelect, onDeselect, onMouseEnter, onMouseLeave, onInsertAfter } = props;

Expand All @@ -50,6 +50,8 @@ function VisualEditorBlock( props ) {
}
}

const controls = wp.blocks.controls.concat( settings.controls || [] );

// Disable reason: Each block can receive focus but must be able to contain
// block children. Tab keyboard navigation enabled by tabIndex assignment.

Expand All @@ -67,9 +69,9 @@ function VisualEditorBlock( props ) {
{ ( isSelected || isHovered ) && <BlockMover uid={ block.uid } /> }
<div className="editor-visual-editor__block-controls">
{ isSelected && <BlockSwitcher uid={ block.uid } /> }
{ isSelected && settings.controls ? (
{ isSelected && controls ? (
<Toolbar
controls={ settings.controls.map( ( control ) => ( {
controls={ controls.map( ( control ) => ( {
...control,
onClick: () => control.onClick( block.attributes, setAttributes ),
isActive: () => control.isActive( block.attributes )
Expand Down
16 changes: 16 additions & 0 deletions editor/modes/visual-editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@
.editor-visual-editor .editor-inserter {
margin: $item-spacing;
}

.editor-visual-editor .alignright,
.editor-visual-editor .alignleft,
.editor-visual-editor .aligncenter {
z-index: 1;
}

// Temporary styling before having the possibility
// to edit the block width
.editor-visual-editor .alignright,
.editor-visual-editor .alignleft {
max-width: 50%;
img {
max-width: 100%;
}
}
16 changes: 16 additions & 0 deletions languages/gutenberg.pot
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"X-Generator: babel-plugin-wp-i18n\n"

#: blocks/controls/index.js:11
msgid "Position left"
msgstr ""

#: blocks/controls/index.js:17
msgid "Position center"
msgstr ""

#: blocks/controls/index.js:23
msgid "Position right"
msgstr ""

#: blocks/controls/index.js:29
msgid "No positionning"
msgstr ""

#: blocks/library/freeform/index.js:9
msgid "Freeform"
msgstr ""
Expand Down