Skip to content

Commit

Permalink
Merge pull request #71 from rishishah-multidots/try/inline-block-comm…
Browse files Browse the repository at this point in the history
…enting

made changes as per the feedback
  • Loading branch information
poojabhimani12 authored Oct 15, 2024
2 parents ffc7f51 + 9a6556f commit ebc28ca
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/compat/wordpress-6.7/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function update_get_avatar_comment_type( $comment_type ) {
$comment_type[] = 'block_comment';
return $comment_type;
}
add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type', 10, 1 );
add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' );
}

/**
Expand Down
51 changes: 51 additions & 0 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* PHP and WordPress configuration compatibility functions for the Gutenberg
* editor plugin changes related to REST API.
*
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

/**
* Updates the comment type in the REST API for WordPress version 6.7.
*
* This function is used as a filter callback for the 'rest_pre_insert_comment' hook.
* It checks if the 'comment_type' parameter is set to 'block_comment' in the REST API request,
* and if so, updates the 'comment_type' and 'comment_approved' properties of the prepared comment.
*
* @param array $prepared_comment The prepared comment data.
* @param WP_REST_Request $request The REST API request object.
* @return array The updated prepared comment data.
*/
if ( ! function_exists( 'update_comment_type_in_rest_api_6_8' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
function update_comment_type_in_rest_api_6_8( $prepared_comment, $request ) {
if ( ! empty( $request['comment_type'] ) && 'block_comment' === $request['comment_type'] ) {
$prepared_comment['comment_type'] = $request['comment_type'];
$prepared_comment['comment_approved'] = $request['comment_approved'];
}

return $prepared_comment;
}
add_filter( 'rest_pre_insert_comment', 'update_comment_type_in_rest_api_6_8', 10, 2 );
}

/**
* Updates the comment type for avatars in the WordPress REST API.
*
* This function adds the 'block_comment' type to the list of comment types
* for which avatars should be retrieved in the WordPress REST API.
*
* @param array $comment_type The array of comment types.
* @return array The updated array of comment types.
*/
if ( ! function_exists( 'update_get_avatar_comment_type' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) {
function update_get_avatar_comment_type( $comment_type ) {
$comment_type[] = 'block_comment';
return $comment_type;
}
add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' );
}
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.7/class-gutenberg-rest-server.php';
require __DIR__ . '/compat/wordpress-6.7/rest-api.php';

// WordPress 6.8 compat.
require __DIR__ . '/compat/wordpress-6.8/rest-api.php';

// Plugin specific code.
require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php';
require_once __DIR__ . '/class-wp-rest-edit-site-export-controller-gutenberg.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ export function BlockSettingsDropdown( {
</MenuItem>
</>
) }
<__unstableCommentIconFill.Slot
fillProps={ { onClose } }
/>
</MenuGroup>
<__unstableCommentIconFill.Slot
fillProps={ { onClose } }
/>
{ canCopyStyles && ! isContentOnly && (
<MenuGroup>
<CopyMenuItem
Expand Down
10 changes: 2 additions & 8 deletions packages/editor/src/components/collab-sidebar/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ function CommentForm( { onSubmit, onCancel, thread } ) {
? _x( 'Update', 'verb' )
: _x( 'Reply', 'Add reply comment' ) }
</Button>
<Button
__next40pxDefaultSize
onClick={ onCancel }
>
<Button __next40pxDefaultSize onClick={ onCancel }>
{ _x( 'Cancel', 'Cancel comment edit' ) }
</Button>
</HStack>
Expand Down Expand Up @@ -312,10 +309,7 @@ function ConfirmNotice( { confirmMessage, confirmAction, discardAction } ) {
>
{ __( 'Yes' ) }
</Button>
<Button
__next40pxDefaultSize
onClick={ discardAction }
>
<Button __next40pxDefaultSize onClick={ discardAction }>
{ __( 'No' ) }
</Button>
</HStack>
Expand Down
38 changes: 26 additions & 12 deletions packages/editor/src/components/collab-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { collabSidebarName } from './constants';
import { Comments } from './comments';
import { AddComment } from './add-comment';
import { store as editorStore } from '../../store';
import AddCommentButton from './commentButton';
import AddCommentToolbarButton from './commentButtonToolbar';
import AddCommentButton from './comment-button';
import AddCommentToolbarButton from './comment-button-toolbar';

const isBlockCommentExperimentEnabled =
window?.__experimentalEnableBlockComment;
const modifyBlockCommentAttributes = ( settings, name ) => {
if ( name?.includes( 'core/' ) && ! settings.attributes.blockCommentId ) {
if ( ! settings.attributes.blockCommentId ) {
settings.attributes = {
...settings.attributes,
blockCommentId: {
Expand Down Expand Up @@ -66,13 +66,18 @@ export default function CollabSidebar() {

const clientId = useSelect( ( select ) => {
const { getSelectedBlockClientId } = select( blockEditorStore );
setBlockCommentID(
select( blockEditorStore ).getBlock( getSelectedBlockClientId() )
?.attributes.blockCommentId
);
return getSelectedBlockClientId();
}, [] );

const blockDetails = useSelect(
( select ) => {
return clientId
? select( blockEditorStore ).getBlock( clientId )
: null;
},
[ clientId ]
);

// Get the dispatch functions to save the comment and update the block attributes.
const { updateBlockAttributes } = useDispatch( blockEditorStore );

Expand All @@ -90,11 +95,17 @@ export default function CollabSidebar() {
comment_approved: 0,
};

if ( parentCommentId ) {
args.parent = parentCommentId;
}
// Create a new object, conditionally including the parent property
const updatedArgs = {
...args,
...( parentCommentId ? { parent: parentCommentId } : {} ),
};

const savedRecord = await saveEntityRecord( 'root', 'comment', args );
const savedRecord = await saveEntityRecord(
'root',
'comment',
updatedArgs
);

if ( savedRecord ) {
// If it's a main comment, update the block attributes with the comment id.
Expand Down Expand Up @@ -212,8 +223,11 @@ export default function CollabSidebar() {
}, [ postId, getEntityRecords ] );

useEffect( () => {
if ( blockDetails ) {
setBlockCommentID( blockDetails?.attributes.blockCommentId );
}
fetchComments();
}, [ postId, fetchComments ] );
}, [ postId, fetchComments, clientId ] );

const allBlocks = useSelect( ( select ) => {
return select( blockEditorStore ).getBlocks();
Expand Down

0 comments on commit ebc28ca

Please sign in to comment.