-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Selecting on a comment should scroll to the block #66873
base: trunk
Are you sure you want to change the base?
Selecting on a comment should scroll to the block #66873
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @karthick-murugan! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
if ( block ) { | ||
const blockClientId = block.clientId; | ||
const iframe = document.querySelector( | ||
'iframe[name="editor-canvas"]' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use useBlockElementRef
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellatrix - when I try to use the useBlockElementRef hook, As per my understanding, it’s a part of the @wordpress/block-editor package. However, when I attempt to use it in the @wordpress/editor package, I cannot get it to work as expected.
Could you please confirm if this is an expected limitation? If so, are there any recommended workarounds or alternative approaches to achieve similar functionality (e.g., getting a reference to a block’s DOM element within the editor context)?
Thanks in advance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean that it's not available as an API? Can we export it as a private API so we can use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If just need to focus the respective block when comment board is focused, then I think below code would do the work
dispatch( 'core/block-editor' ).selectBlock( block?.clientId );
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akasunil - thank you and I have updated it and pushed the code.
const clientID = select( blockEditorStore ).getSelectedBlockClientId(); | ||
return ( | ||
const clientBlocks = select( blockEditorStore ).getBlocks(); | ||
setBlocksList( clientBlocks ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't set sate inside useSelect
, return the value instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellatrix - now the value has been returned inside useSelect
if ( block ) { | ||
const blockClientId = block.clientId; | ||
// Select the block in the editor | ||
dispatch( blockEditorStore ).selectBlock( blockClientId ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use useDispatch
instead, so we get the actions from the correct registry based on the context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellatrix - Updated with useDispatch
as per your advice.
Co-authored-by: youknowriad <[email protected]> Co-authored-by: ntsekouras <[email protected]>
…ature/block-comments-scroll
); | ||
|
||
useEffect( () => { | ||
setBlocksList( selectedClientBlocks ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the extra local state here? It's not necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line setBlocksList( selectedClientBlocks );
ensures the local blocksList state stays synchronized with the selected blocks from useSelect, enabling efficient block-related operations. Without it, the component would rely directly on selectedClientBlocks, risking inconsistent data or unnecessary re-renders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without it, the component would rely directly on selectedClientBlocks, risking inconsistent data or unnecessary re-renders.
Can you elaborate more about this or how to reproduce the issue?
As far as I can see, there's no need to keep a copy of selectedClientBlocks
in a state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -64,14 +66,41 @@ export function Comments( { | |||
setIsConfirmDialogOpen( false ); | |||
}; | |||
|
|||
const blockCommentId = useSelect( ( select ) => { | |||
const blockCommentId = useSelect( () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you removed the select on accident here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error 'select' is already declared in the upper scope on line 20 column 34 no-shadow
@ellatrix - I removed the select to fix the above linting issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should remove the select
import above, and then you can pass select as an argument.
Example: useSelect( ( select ) => {}, [] );
@@ -46,6 +46,8 @@ export function Comments( { | |||
} ) { | |||
const [ actionState, setActionState ] = useState( false ); | |||
const [ isConfirmDialogOpen, setIsConfirmDialogOpen ] = useState( false ); | |||
// eslint-disable-next-line no-unused-vars | |||
const [ activeClientId, setActiveClientId ] = useState( null ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is eslint disabled here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellatrix - The above lines no longer needed and removed it.
@@ -35,7 +35,7 @@ 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. | |||
* @return {React.ReactNode} The rendered Comments component. | |||
* @return {JSX.Element} The rendered Comments component. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this need to change? Just curious? What are we using elsewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellatrix - Actually it was updated automatically when running the prettier command. Now replaced it back.
@@ -17,7 +17,7 @@ import { | |||
} from '@wordpress/components'; | |||
import { Icon, check, published, moreVertical } from '@wordpress/icons'; | |||
import { __, _x } from '@wordpress/i18n'; | |||
import { useSelect } from '@wordpress/data'; | |||
import { useSelect, useDispatch, select } from '@wordpress/data'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Core code should avoid using global select
and dispatch
. Use static selector getter instead.
const { getBlocks } = useSelect( blockEditorStore );
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mamaduka - removed the global select and updated as such.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@karthick-murugan, sorry for the confusion. I only suggested getting the getBlocks
selector. You can leave the existing useSelect
for the block comment ID as it was.
Usually, you'll use this method only to get selectors used in event callbacks like handleThreadClick
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Mamaduka for the clarification. Updated as such.
const [ blocks ] = useEntityBlockEditor( 'postType', postType, { | ||
id: postId, | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for switching to useEntityBlockEditor
? This reverts the suggestion from #66873 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mamaduka - used useEntityBlockEditor
for the functionality to work in Site Editor. Before it was not.
} | ||
} | ||
} | ||
return blocks ? getBlockByCommentId( blocks, commentId ) : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this fallback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mamaduka - I have updated the condition now.
…urugan/gutenberg into feature/block-comments-scroll
I agree; I had this in mind because client ID linked to the comment ID is required through out commenting functionality. This would definitely reduce some logic. |
What?
Part of #66377
Why?
Focussing/selecting on a comment should scroll to the block that is being commented on.
How?
Added scroll to the editor section when the corresponding comments is selected.
Testing Instructions
Screenshots or screencast
REC-20241108195829.mp4