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

feat(homepage-articles): rename block and reorganise settings #2000

Merged
merged 10 commits into from
Feb 6, 2025
525 changes: 260 additions & 265 deletions src/blocks/homepage-articles/edit.tsx

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions src/blocks/homepage-articles/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,54 @@
opacity: 0.6;
}
}

&__panel {
.spacing-sizes-control {
.components-range-control__root {
> span {
margin-top: 0;
}
}
}

> *:not(.components-panel__body-title) {
margin-bottom: 16px;
margin-top: 0;

&:last-of-type {
margin-bottom: 8px;
}
}

&.is-content {
.autocomplete-tokenfield + .components-checkbox-control {
margin-top: -8px;
}
}
}

&__button-group {
flex: 0 0 100%;

.components-base-control__label {
width: 100%;
}

.components-button-group {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(40px, 1fr));

button {
justify-content: center;
}
}
}
}

.components-base-control__field:empty {
display: none;

+ .components-base-control__help {
margin: 0;
}
}
20 changes: 16 additions & 4 deletions src/blocks/homepage-articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const { name, attributes, category } = metadata;
// Name must be exported separately.
export { name };

export const title = __( 'Homepage Posts', 'newspack-blocks' );
export const title = __( 'Content Loop', 'newspack-blocks' );

export const icon = (
<SVG xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
Expand All @@ -48,11 +48,23 @@ export const settings = {
__( 'posts', 'newspack-blocks' ),
__( 'articles', 'newspack-blocks' ),
__( 'latest', 'newspack-blocks' ),
__( 'homepage', 'newspack-blocks' ),
__( 'query', 'newspack-blocks' ),
],
description: __( 'A block for displaying homepage posts.', 'newspack-blocks' ),
description: __(
'An advanced block that allows displaying content based on different parameters and visual configurations.',
'newspack-blocks'
),
styles: [
{ name: 'default', label: _x( 'Default', 'block style', 'newspack-blocks' ), isDefault: true },
{ name: 'borders', label: _x( 'Borders', 'block style', 'newspack-blocks' ) },
{
name: 'default',
label: _x('Default', 'block style', 'newspack-blocks'),
isDefault: true,
},
{
name: 'borders',
label: _x('Borders', 'block style', 'newspack-blocks'),
},
],
supports: {
html: false,
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/homepage-articles/templates/article.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"
if ( $attributes['showFullContent'] && ! $attributes['showExcerpt'] ) :
the_content();
endif;
if ( $post_link && ( $attributes['showReadMore'] ) ) :
if ( $post_link && ! $attributes['showFullContent'] && $attributes['showReadMore'] ) :
?>
<a class="more-link" href="<?php echo esc_url( $post_link ); ?>" rel="bookmark">
<?php echo esc_html( $attributes['readMoreLabel'] ); ?>
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/homepage-articles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export const postsBlockDispatch = (
) => {
return {
// Only editor blocks can trigger reflows.
// @ts-ignore It's a string.
// @ts-expect-error It's a string.
triggerReflow: isEditorBlock ? dispatch( STORE_NAMESPACE ).reflow : () => undefined,
};
};
Expand Down
1 change: 1 addition & 0 deletions src/components/autocomplete-tokenfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class AutocompleteTokenField extends Component {
onChange={ tokens => this.handleOnChange( tokens ) }
onInputChange={ input => this.debouncedUpdateSuggestions( input ) }
label={ label }
__next40pxDefaultSize
/>
{ loading && <Spinner /> }
{ help && <p className="autocomplete-tokenfield__help">{ help }</p> }
Expand Down
13 changes: 11 additions & 2 deletions src/components/autocomplete-tokenfield.scss
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
@use "~@wordpress/base-styles/colors" as wp-colors;

.autocomplete-tokenfield {
max-width: 100%;
position: relative;

.components-spinner {
bottom: 12px;
position: absolute;
top: 2em;
right: 0;
}

&:has(.autocomplete-tokenfield__help) .components-spinner {
bottom: calc(36px + 8px + 12px);
}

/* Workaround for hard-coded help text in FormTokenField. */
.components-form-token-field > .components-form-token-field__help {
display: none;
}

.autocomplete-tokenfield__help {
font-style: italic;
color: wp-colors.$gray-700;
font-size: 12px;
margin: 8px 0;
}
}
48 changes: 26 additions & 22 deletions src/components/editor-panels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Spinner, CheckboxControl, PanelBody, PanelRow } from '@wordpress/components';
import { BaseControl, CheckboxControl, PanelBody, Spinner } from '@wordpress/components';
import { useSelect } from '@wordpress/data';

const CheckboxesGroup = ( { options, values, onChange } ) => {
const CheckboxesGroup = ( { options, values, onChange, defaultRequiredSlug = null } ) => {
if ( ! Array.isArray( options ) ) {
return <Spinner />;
}
return options.map( ( { name, slug } ) => (
<PanelRow key={ slug }>
return options.map( ( { name, slug } ) => {
const isDefault = defaultRequiredSlug && slug === defaultRequiredSlug;
const otherOptionsSelected = defaultRequiredSlug && values.some( value => value !== defaultRequiredSlug );
const isDisabled = isDefault && ! otherOptionsSelected;

return (
<CheckboxControl
label={ name }
checked={ values.indexOf( slug ) > -1 }
disabled={ isDisabled }
onChange={ value => {
const cleanPostType = [ ...new Set( values ) ];
if ( value && cleanPostType.indexOf( slug ) === -1 ) {
cleanPostType.push( slug );
} else if ( ! value && cleanPostType.indexOf( slug ) > -1 ) {
cleanPostType.splice( cleanPostType.indexOf( slug ), 1 );
const cleanOptions = [ ...new Set( values ) ];
if ( value && cleanOptions.indexOf( slug ) === -1 ) {
cleanOptions.push( slug );
} else if ( ! value && cleanOptions.indexOf( slug ) > -1 ) {
cleanOptions.splice( cleanOptions.indexOf( slug ), 1 );
}
// If no options would be selected, force the default required one
if ( defaultRequiredSlug && cleanOptions.length === 0 ) {
cleanOptions.push( defaultRequiredSlug );
}
onChange( cleanPostType );
onChange( cleanOptions );
} }
key={ slug }
/>
</PanelRow>
) );
);
} );
};

export const PostTypesPanel = ( { attributes, setAttributes } ) => {
Expand All @@ -50,27 +60,21 @@ export const PostTypesPanel = ( { attributes, setAttributes } ) => {
} );

return (
<PanelBody title={ __( 'Post Types', 'newspack-blocks' ) }>
<PanelBody title={ __( 'Post Types', 'newspack-blocks' ) } initialOpen={ false }>
<CheckboxesGroup
options={ availablePostTypes }
values={ attributes.postType }
onChange={ postType => setAttributes( { postType } ) }
defaultRequiredSlug="post"
/>
</PanelBody>
);
};

export const PostStatusesPanel = ( { attributes, setAttributes } ) => {
return (
<PanelBody title={ __( 'Additional Post Statuses', 'newspack-blocks' ) }>
<PanelRow>
<i>
{ __(
'Selection here has effect only for editors, regular users will only see published posts.',
'newspack-blocks'
) }
</i>
</PanelRow>
<PanelBody title={ __( 'Additional Post Statuses', 'newspack-blocks' ) } initialOpen={ false }>
<BaseControl help={ __( 'Selection here has effect only for editors, regular users will only see published posts.', 'newspack-blocks' ) } />
<CheckboxesGroup
values={ attributes.includedPostStatuses }
options={ [
Expand Down
Loading