Skip to content
Merged
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
Prev Previous commit
Next Next commit
Remove wordCount attribute
  • Loading branch information
ntsekouras authored and mcsf committed Jul 15, 2021
commit c71da08655bdf3ec5244cd93c87ee4cf29e34702
4 changes: 0 additions & 4 deletions packages/block-library/src/post-excerpt/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
"textAlign": {
"type": "string"
},
"wordCount": {
"type": "number",
"default": 55
},
"moreText": {
"type": "string"
},
Expand Down
62 changes: 16 additions & 46 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,16 @@ import {
Warning,
useBlockProps,
} from '@wordpress/block-editor';
import {
PanelBody,
RangeControl,
ToggleControl,
Disabled,
} from '@wordpress/components';
import { PanelBody, ToggleControl, Disabled } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { useCanEditEntity } from '../utils/hooks';

function usePostContentExcerpt( wordCount, postId, postType ) {
// Don't destrcuture items from content here, it can be undefined.
const [ , , content ] = useEntityProp(
'postType',
postType,
'content',
postId
);
const renderedPostContent = content?.rendered;
return useMemo( () => {
if ( ! renderedPostContent ) {
return '';
}
const excerptElement = document.createElement( 'div' );
excerptElement.innerHTML = renderedPostContent;
const excerpt =
excerptElement.textContent || excerptElement.innerText || '';
return excerpt.trim().split( ' ', wordCount ).join( ' ' );
}, [ renderedPostContent, wordCount ] );
}

export default function PostExcerptEditor( {
attributes: { textAlign, wordCount, moreText, showMoreOnNewLine },
attributes: { textAlign, moreText, showMoreOnNewLine },
setAttributes,
isSelected,
context: { postId, postType, queryId },
Expand All @@ -64,17 +38,24 @@ export default function PostExcerptEditor( {
setExcerpt,
{ rendered: renderedExcerpt, protected: isProtected } = {},
] = useEntityProp( 'postType', postType, 'excerpt', postId );
const postContentExcerpt = usePostContentExcerpt(
wordCount,
postId,
postType
);
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

/**
* When excerpt is editable, strip the html tags from
* rendered excerpt. This will be used if the entity's
* excerpt has been produced from the content.
*/
const strippedRenderedExcerpt = useMemo( () => {
if ( ! renderedExcerpt ) return '';
const document = new window.DOMParser().parseFromString(
renderedExcerpt,
'text/html'
);
return document.body.innerText || '';
}, [ renderedExcerpt ] );
if ( ! postType || ! postId ) {
return (
<div { ...blockProps }>
Expand Down Expand Up @@ -116,7 +97,7 @@ export default function PostExcerptEditor( {
aria-label={ __( 'Post excerpt text' ) }
value={
rawExcerpt ||
postContentExcerpt ||
strippedRenderedExcerpt ||
( isSelected ? '' : __( 'No post excerpt found' ) )
}
onChange={ setExcerpt }
Expand All @@ -141,17 +122,6 @@ export default function PostExcerptEditor( {
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Post Excerpt Settings' ) }>
{ ! rawExcerpt && (
<RangeControl
label={ __( 'Max words' ) }
value={ wordCount }
onChange={ ( newExcerptLength ) =>
setAttributes( { wordCount: newExcerptLength } )
}
min={ 10 }
max={ 100 }
/>
) }
<ToggleControl
label={ __( 'Show link on new line' ) }
checked={ showMoreOnNewLine }
Expand Down
11 changes: 1 addition & 10 deletions packages/block-library/src/post-excerpt/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) {
* result in showing only one `read more` link at a time.
*/
add_filter( 'excerpt_more', $filter_excerpt_more );

$filter_excerpt_length = function() use ( $attributes ) {
return isset( $attributes['wordCount'] ) ? $attributes['wordCount'] : 55;
};
add_filter( 'excerpt_length', $filter_excerpt_length );

$classes = '';
if ( isset( $attributes['textAlign'] ) ) {
$classes .= 'has-text-align-' . $attributes['textAlign'];
$classes .= "has-text-align-{$attributes['textAlign']}";
}
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );

Expand All @@ -50,10 +44,7 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) {
} else {
$content .= " $more_text</p>";
}

remove_filter( 'excerpt_length', $filter_excerpt_length );
remove_filter( 'excerpt_more', $filter_excerpt_more );

return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"name": "core/post-excerpt",
"isValid": true,
"attributes": {
"wordCount": 55,
"showMoreOnNewLine": true
},
"innerBlocks": [],
Expand Down