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

Block Library: Add alignment option to the Comment Edit Link block #36033

Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions packages/block-library/src/post-comment-edit/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"linkTarget": {
"type": "string",
"default": "_self"
},
"textAlign": {
"type": "string"
}
},
"supports": {
Expand Down
32 changes: 29 additions & 3 deletions packages/block-library/src/post-comment-edit/edit.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
AlignmentControl,
BlockControls,
InspectorControls,
useBlockProps,
} from '@wordpress/block-editor';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export default function Edit( {
attributes: { className, linkTarget },
attributes: { linkTarget, textAlign },
setAttributes,
} ) {
const blockProps = useBlockProps( { className } );
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

const blockControls = (
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( newAlign ) =>
setAttributes( { textAlign: newAlign } )
}
/>
</BlockControls>
);
const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Link settings' ) }>
Expand All @@ -28,6 +53,7 @@ export default function Edit( {

return (
<>
{ blockControls }
{ inspectorControls }
<div { ...blockProps }>
<a
Expand Down
9 changes: 8 additions & 1 deletion packages/block-library/src/post-comment-edit/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ function render_block_core_post_comment_edit( $attributes, $content, $block ) {
$link_atts .= sprintf( 'target="%s"', esc_attr( $attributes['linkTarget'] ) );
}

$classes = '';
if ( isset( $attributes['textAlign'] ) ) {
$classes .= 'has-text-align-' . esc_attr( $attributes['textAlign'] );
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );

return sprintf(
'<div %1$s><a href="%2$s" %3$s>%4$s</a></div>',
get_block_wrapper_attributes(),
$wrapper_attributes,
esc_url( $edit_comment_link ),
$link_atts,
esc_html__( 'Edit' )
Expand Down