Skip to content

Commit

Permalink
Merge pull request #1033 from woocommerce/tweak/editor
Browse files Browse the repository at this point in the history
Dynamically update block editor layout and styling
  • Loading branch information
tiagonoronha authored Jan 9, 2019
2 parents 3cc5d13 + 102d7de commit 7f7e6be
Show file tree
Hide file tree
Showing 7 changed files with 1,337 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

# Minified js
*.min.js
!/assets/js/vendor/*.min.js
/assets/js/vendor/*.min.js
/assets/js/editor.js

# Sass
.sass-cache
Expand Down
42 changes: 30 additions & 12 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,26 +234,31 @@ module.exports = function( grunt ) {
js: {
files: [
// main js
'assets/js/*js',
'!assets/js/*.min.js',
'assets/js/**/*.js',
'!assets/js/**/*.min.js',
'!assets/js/editor.js',

// customizer js
'assets/js/customizer/*js',
'!assets/js/customizer/*.min.js',
'assets/js/customizer/**/*.js',
'!assets/js/customizer/**/*..min.js',

// WooCommerce js
'assets/js/woocommerce/*js',
'!assets/js/woocommerce/*.min.js',
'assets/js/woocommerce/**/*.js',
'!assets/js/woocommerce/**/*.min.js',

// Extensions js
'assets/js/woocommerce/extensions/*js',
'!assets/js/woocommerce/extensions/*.min.js',
'assets/js/woocommerce/extensions/**/*.js',
'!assets/js/woocommerce/extensions/**/*.min.js',

// Welcome screen js
'assets/js/admin/welcome-screen/*js',
'!assets/js/admin/welcome-screen/*.min.js'
'assets/js/admin/welcome-screen/**/*.js',
'!assets/js/admin/welcome-screen/**/*.min.js'
],
tasks: ['jshint', 'uglify']
tasks: [
'babel',
'jshint',
'uglify'
]
}
},

Expand Down Expand Up @@ -328,7 +333,8 @@ module.exports = function( grunt ) {
'!composer.json',
'!assets/css/sass/**',
'!assets/css/**/*.scss',
'!*.scss'
'!*.scss',
'!assets/js/src/**'
],
dest: 'storefront',
expand: true,
Expand Down Expand Up @@ -449,6 +455,16 @@ module.exports = function( grunt ) {
{ src: './storefront/**' }
]
}
},
babel: {
options: {
presets: ['@wordpress/babel-preset-default']
},
dist: {
files: {
'./assets/js/editor.js': './assets/js/src/editor.js'
}
}
}
});

Expand All @@ -465,11 +481,13 @@ module.exports = function( grunt ) {
grunt.loadNpmTasks( 'grunt-postcss' );
grunt.loadNpmTasks( 'grunt-contrib-compress' );
grunt.loadNpmTasks( 'grunt-stylelint' );
grunt.loadNpmTasks( 'grunt-babel' );


// Register tasks
grunt.registerTask( 'default', [
'css',
'babel',
'jshint',
'uglify'
]);
Expand Down
5 changes: 5 additions & 0 deletions assets/css/base/gutenberg-editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ table {
// Main content width
.wp-block {
max-width: $container-width;

.storefront-has-sidebar & {
$span-value: span(9) / 100;
max-width: calc(#{$container-width} * #{$span-value / ( $span-value * 0 + 1 )}); // Unitless hack.
}
}

.wp-block[data-align='wide'] {
Expand Down
152 changes: 152 additions & 0 deletions assets/js/src/editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/**
* Block editor enhancements.
*
* Contains functionality to dynamically update the block editor
* configuration and styling.
*/

( function() {

/**
* Check if the main sidebar is active (has widgets).
*
* This uses a custom property `mainSidebarActive` added via the
* `block_editor_settings` filter.
*
* @return {boolean} Whether sidebar is active.
*/
const sidebarIsActive = () => {
let settings = wp.data.select( 'core/editor' ).getEditorSettings();

if ( settings.hasOwnProperty( 'mainSidebarActive' ) && !! settings.mainSidebarActive ) {
return true;
}

return false;
};

/**
* Get current page template name.
*
* @return {string} The page template name.
*/
const getCurrentPageTemplate = () => {
return wp.data.select( 'core/editor' ).getEditedPostAttribute( 'template' );
};

/**
* Check if the active theme supports a wide layout.
*
* @return {boolean} Whether the theme supports wide layout.
*/
const themeSupportsWide = () => {
let settings = wp.data.select( 'core/editor' ).getEditorSettings();

if ( settings.hasOwnProperty( 'alignWide' ) && !! settings.alignWide ) {
return true;
}

return false;
};

/**
* Update editor wide support.
*
* @param {boolean} alignWide Whether the editor supports
* alignWide support.
*
* @return {void}
*/
const updateWideSupport = ( alignWide ) => {
wp.data.dispatch( 'core/editor' ).updateEditorSettings( { 'alignWide': !! alignWide } );
};

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

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

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

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

if ( blockWrapper ) {
blockWrapper.setAttribute( 'data-align', alignWide ? align : '' );
}
}
} );
};

/**
* Add custom class to editor wrapper if main sidebar is active.
*
* @param {boolean} showSidebar Whether to add custom class.
*
* @return {void}
*/
const toggleCustomSidebarClass = ( showSidebar ) => {
let editorWrapper = document.getElementsByClassName( 'editor-writing-flow' );

if ( ! editorWrapper.length ) {
return;
}

if ( !! showSidebar ) {
editorWrapper[0].classList.add( 'storefront-has-sidebar' );
} else {
editorWrapper[0].classList.remove( 'storefront-has-sidebar' );
}
};

/**
* Update editor and blocks when layout changes.
*
* @return {void}
*/
const maybeUpdateEditor = () => {
if ( 'template-fullwidth.php' === getCurrentPageTemplate() ) {
updateWideSupport( true );
toggleCustomSidebarClass( false );
updateAlignAttribute( true );
} else if ( sidebarIsActive() ) {
updateWideSupport( false );
toggleCustomSidebarClass( true );
updateAlignAttribute( false );
} else {
updateWideSupport( true );
toggleCustomSidebarClass( false );
updateAlignAttribute( true );
}
};

wp.domReady( () => {

// Don't do anything if the theme doesn't declare support for `align-wide`.
if ( ! themeSupportsWide() ) {
return;
}

maybeUpdateEditor();

let pageTemplate = getCurrentPageTemplate();

wp.data.subscribe( () => {
if ( getCurrentPageTemplate() !== pageTemplate ) {
pageTemplate = getCurrentPageTemplate();

maybeUpdateEditor();
}
} );
} );
} )();
28 changes: 27 additions & 1 deletion inc/class-storefront.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct() {
add_filter( 'wp_page_menu_args', array( $this, 'page_menu_args' ) );
add_filter( 'navigation_markup_template', array( $this, 'navigation_markup_template' ) );
add_action( 'enqueue_embed_scripts', array( $this, 'print_embed_styles' ) );
add_filter( 'block_editor_settings', array( $this, 'custom_editor_settings' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -345,15 +346,20 @@ public function google_fonts() {
}

/**
* Enqueue supplemental block editor styles.
* Enqueue supplemental block editor assets.
*
* @since 2.4.0
*/
public function block_editor_assets() {
global $storefront_version;

// Styles.
wp_enqueue_style( 'storefront-editor-block-styles', get_theme_file_uri( '/assets/css/base/gutenberg-blocks.css' ), false, $storefront_version, 'all' );
wp_style_add_data( 'storefront-editor-block-styles', 'rtl', 'replace' );

// JS.
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_script( 'storefront-editor', get_template_directory_uri() . '/assets/js/editor' . $suffix . '.js', array( 'wp-data', 'wp-dom-ready', 'wp-edit-post' ), $storefront_version, true );
}

/**
Expand Down Expand Up @@ -434,6 +440,26 @@ public function body_classes( $classes ) {
return $classes;
}

/**
* Adds a custom parameter to the editor settings that is used
* to track whether the main sidebar has widgets.
*
* @since 2.4.3
* @param array $settings Default editor settings.
* @param WP_Post $post Post being edited.
*
* @return array Filtered block editor settings.
*/
public function custom_editor_settings( $settings, $post ) {
$settings['mainSidebarActive'] = false;

if ( is_active_sidebar( 'sidebar-1' ) ) {
$settings['mainSidebarActive'] = true;
}

return $settings;
}

/**
* Custom navigation markup template hooked into `navigation_markup_template` filter hook.
*/
Expand Down
Loading

0 comments on commit 7f7e6be

Please sign in to comment.