feat: add Related Stories editor controls
This commit is contained in:
6 files changed
+503
No files matched your search
@@ -0,0 +1,128 @@
|
||||
( function ( factory ) {
|
||||
if ( typeof module === 'object' && module.exports ) {
|
||||
module.exports = factory();
|
||||
} else {
|
||||
factory().initialize( window.wp );
|
||||
}
|
||||
} )( function () {
|
||||
'use strict';
|
||||
|
||||
const attributes = {
|
||||
limit: { type: 'integer', default: 3, enum: [ 3, 5 ] },
|
||||
layout: {
|
||||
type: 'string',
|
||||
default: 'adaptive',
|
||||
enum: [ 'adaptive', 'grid', 'carousel' ],
|
||||
},
|
||||
headingLevel: { type: 'integer', default: 2, enum: [ 2, 3, 4, 5, 6 ] },
|
||||
};
|
||||
const layouts = [ 'adaptive', 'grid', 'carousel' ];
|
||||
|
||||
function hasRequiredGlobals( wp ) {
|
||||
return Boolean(
|
||||
wp &&
|
||||
wp.blocks &&
|
||||
typeof wp.blocks.registerBlockType === 'function' &&
|
||||
wp.blockEditor &&
|
||||
wp.blockEditor.InspectorControls &&
|
||||
wp.components &&
|
||||
wp.components.PanelBody &&
|
||||
wp.components.SelectControl &&
|
||||
wp.element &&
|
||||
typeof wp.element.createElement === 'function' &&
|
||||
wp.element.Fragment &&
|
||||
wp.i18n &&
|
||||
typeof wp.i18n.__ === 'function' &&
|
||||
wp.serverSideRender,
|
||||
);
|
||||
}
|
||||
|
||||
function initialize( wp ) {
|
||||
if ( ! hasRequiredGlobals( wp ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const __ = wp.i18n.__;
|
||||
|
||||
function Edit( props ) {
|
||||
const currentAttributes = props.attributes || {};
|
||||
const setAttributes =
|
||||
typeof props.setAttributes === 'function' ? props.setAttributes : () => {};
|
||||
|
||||
return wp.element.createElement(
|
||||
wp.element.Fragment,
|
||||
null,
|
||||
wp.element.createElement(
|
||||
wp.blockEditor.InspectorControls,
|
||||
null,
|
||||
wp.element.createElement(
|
||||
wp.components.PanelBody,
|
||||
{
|
||||
title: __( 'Related stories settings', 'chrnx' ),
|
||||
initialOpen: true,
|
||||
},
|
||||
wp.element.createElement( wp.components.SelectControl, {
|
||||
label: __( 'Stories to show', 'chrnx' ),
|
||||
value: String( currentAttributes.limit === 5 ? 5 : 3 ),
|
||||
options: [
|
||||
{ label: '3', value: '3' },
|
||||
{ label: '5', value: '5' },
|
||||
],
|
||||
onChange( value ) {
|
||||
setAttributes( {
|
||||
limit: Number( value ) === 5 ? 5 : 3,
|
||||
} );
|
||||
},
|
||||
} ),
|
||||
wp.element.createElement( wp.components.SelectControl, {
|
||||
label: __( 'Layout', 'chrnx' ),
|
||||
value: layouts.includes( currentAttributes.layout )
|
||||
? currentAttributes.layout
|
||||
: 'adaptive',
|
||||
options: [
|
||||
{
|
||||
label: __( 'Adaptive', 'chrnx' ),
|
||||
value: 'adaptive',
|
||||
},
|
||||
{ label: __( 'Grid', 'chrnx' ), value: 'grid' },
|
||||
{
|
||||
label: __( 'Carousel', 'chrnx' ),
|
||||
value: 'carousel',
|
||||
},
|
||||
],
|
||||
help: __(
|
||||
'Adaptive uses a grid on larger screens and horizontal scrolling on smaller screens.',
|
||||
'chrnx',
|
||||
),
|
||||
onChange( value ) {
|
||||
setAttributes( {
|
||||
layout: layouts.includes( value ) ? value : 'adaptive',
|
||||
} );
|
||||
},
|
||||
} ),
|
||||
),
|
||||
),
|
||||
wp.element.createElement( wp.serverSideRender, {
|
||||
block: 'chrnx/related-posts',
|
||||
attributes: currentAttributes,
|
||||
} ),
|
||||
);
|
||||
}
|
||||
|
||||
wp.blocks.registerBlockType( 'chrnx/related-posts', {
|
||||
apiVersion: 3,
|
||||
title: __( 'Related Posts', 'chrnx' ),
|
||||
category: 'theme',
|
||||
description: __( 'Related stories selected by topic, tags, and recency.', 'chrnx' ),
|
||||
attributes,
|
||||
supports: { html: false, align: [ 'wide', 'full' ] },
|
||||
edit: Edit,
|
||||
save() {
|
||||
return null;
|
||||
},
|
||||
} );
|
||||
return true;
|
||||
}
|
||||
|
||||
return { initialize };
|
||||
} );
|
||||
Reference in new issue
Block a user