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

Dynamically update block editor layout and styling #1033

Merged
merged 9 commits into from
Jan 9, 2019
Prev Previous commit
Next Next commit
Switch from updating block data to only visual tweaks
  • Loading branch information
tiagonoronha committed Jan 9, 2019
commit 2cb6d0e31e30b4e808af68470b89023031376950
28 changes: 18 additions & 10 deletions assets/js/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
/**
* Update editor wide support.
*
* @param {boolean} alignWide Whether to enable or disable
* @param {boolean} alignWide Whether the editor supports
* alignWide support.
*
* @return {void}
Expand All @@ -62,22 +62,28 @@
};

/**
* Update blocks to remove alignWide support.
* Update `data-align` attribute on each block.
*
* @param {boolean} alignWide Whether alignWide is supported.
*
* @return {void}
*/
const removeWideAlignFromBlocks = () => {
const updateAlignAttribute = ( alignWide ) => {
let blocks = wp.data.select( 'core/editor' ).getBlocks();

blocks.forEach( ( block ) => {
let align = '';

if ( block.attributes.hasOwnProperty( 'align' ) ) {
align = block.attributes.align;
}
let align = block.attributes.align;

if ( ! [ 'full', 'wide' ].includes( align ) ) {
return;
}

let blockWrapper = document.getElementById( 'block-' + block.clientId );

if ( 'full' === align || 'wide' === align ) {
wp.data.dispatch( 'core/editor' ).updateBlockAttributes( block.clientId, { 'align': '' } );
if ( blockWrapper ) {
blockWrapper.setAttribute( 'data-align', alignWide ? align : '' );
}
}
} );
};
Expand Down Expand Up @@ -112,13 +118,15 @@
if ( 'template-fullwidth.php' === getCurrentPageTemplate() ) {
updateWideSupport( true );
toggleCustomSidebarClass( false );
updateAlignAttribute( true );
} else if ( sidebarIsActive() ) {
updateWideSupport( false );
removeWideAlignFromBlocks();
toggleCustomSidebarClass( true );
updateAlignAttribute( false );
} else {
updateWideSupport( true );
toggleCustomSidebarClass( false );
updateAlignAttribute( true );
}
};

Expand Down