Skip to content

Commit

Permalink
Feedback and site editor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
karthick-murugan committed Dec 5, 2024
1 parent 68568d9 commit 13194c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
34 changes: 12 additions & 22 deletions packages/editor/src/components/collab-sidebar/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import {
} from '@wordpress/components';
import { Icon, check, published, moreVertical } from '@wordpress/icons';
import { __, _x } from '@wordpress/i18n';
import { useSelect, useDispatch, select } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useEntityBlockEditor } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import CommentAuthorInfo from './comment-author-info';
import CommentForm from './comment-form';
import { getBlockByCommentId } from './utils';

/**
* Renders the Comments component.
Expand All @@ -35,6 +37,8 @@ import CommentForm from './comment-form';
* @param {Function} props.onAddReply - The function to add a reply to a comment.
* @param {Function} props.onCommentDelete - The function to delete a comment.
* @param {Function} props.onCommentResolve - The function to mark a comment as resolved.
* @param {string} props.postType - The post type.
* @param {number} props.postId - The post ID.
* @return {React.ReactNode} The rendered Comments component.
*/
export function Comments( {
Expand All @@ -43,6 +47,8 @@ export function Comments( {
onAddReply,
onCommentDelete,
onCommentResolve,
postType, // Ensure postType is passed as a prop
postId, // Ensure postId is passed as a prop
} ) {
const [ actionState, setActionState ] = useState( false );
const [ isConfirmDialogOpen, setIsConfirmDialogOpen ] = useState( false );
Expand All @@ -64,37 +70,21 @@ export function Comments( {
setIsConfirmDialogOpen( false );
};

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

const findBlockByCommentId = ( blocks, commentId ) => {
for ( const block of blocks ) {
if ( block.attributes.blockCommentId === commentId ) {
return block;
}
if ( block.innerBlocks && block.innerBlocks.length > 0 ) {
const foundBlock = findBlockByCommentId(
block.innerBlocks,
commentId
);
if ( foundBlock ) {
return foundBlock;
}
}
}
return null;
};
const [ blocks ] = useEntityBlockEditor( 'postType', postType, {
id: postId,
} );

const { getBlocks } = useSelect( blockEditorStore );
const { selectBlock } = useDispatch( blockEditorStore );
const handleThreadClick = ( thread ) => {
const selClientBlocks = getBlocks();
const block = findBlockByCommentId( selClientBlocks, thread.id );
const block = getBlockByCommentId( blocks, thread.id );
if ( block ) {
selectBlock( block.clientId ); // Use the action to select the block
}
Expand Down
7 changes: 7 additions & 0 deletions packages/editor/src/components/collab-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ function CollabSidebarContent( { showCommentBoard, setShowCommentBoard } ) {
);
};

const postType = useSelect(
( select ) => select( editorStore ).getCurrentPostType(),
[]
);

return (
<div className="editor-collab-sidebar-panel">
<AddComment
Expand All @@ -234,6 +239,8 @@ function CollabSidebarContent( { showCommentBoard, setShowCommentBoard } ) {
onAddReply={ addNewComment }
onCommentDelete={ onCommentDelete }
onCommentResolve={ onCommentResolve }
postId={ postId }
postType={ postType }
/>
</div>
);
Expand Down
18 changes: 18 additions & 0 deletions packages/editor/src/components/collab-sidebar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@
export function sanitizeCommentString( str ) {
return str.trim();
}

export const getBlockByCommentId = ( blocks, commentId ) => {
for ( const block of blocks ) {
if ( block.attributes.blockCommentId === commentId ) {
return block;
}
if ( block.innerBlocks && block.innerBlocks.length > 0 ) {
const foundBlock = getBlockByCommentId(
block.innerBlocks,
commentId
);
if ( foundBlock ) {
return foundBlock;
}
}
}
return blocks ? getBlockByCommentId( blocks, commentId ) : null;
};

0 comments on commit 13194c1

Please sign in to comment.