diff --git a/packages/editor/src/components/collab-sidebar/comment-form.js b/packages/editor/src/components/collab-sidebar/comment-form.js index 052fd3cdd2656..bd139a682f5cb 100644 --- a/packages/editor/src/components/collab-sidebar/comment-form.js +++ b/packages/editor/src/components/collab-sidebar/comment-form.js @@ -42,7 +42,10 @@ function CommentForm( { onSubmit, onCancel, thread, submitButtonText } ) { __next40pxDefaultSize accessibleWhenDisabled variant="primary" - onClick={ () => onSubmit( inputComment ) } + onClick={ () => { + onSubmit( inputComment ); + setInputComment( '' ); + } } disabled={ 0 === sanitizeCommentString( inputComment ).length } diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 7a03068787c81..f5d95621cb250 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -6,7 +6,7 @@ import clsx from 'clsx'; /** * WordPress dependencies */ -import { useState, RawHTML } from '@wordpress/element'; +import { useState, RawHTML, useEffect } from '@wordpress/element'; import { __experimentalHStack as HStack, __experimentalVStack as VStack, @@ -16,7 +16,7 @@ import { Tooltip, } from '@wordpress/components'; import { Icon, check, published, moreVertical } from '@wordpress/icons'; -import { __, _x } from '@wordpress/i18n'; +import { __, _x, sprintf } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; import { store as blockEditorStore } from '@wordpress/block-editor'; @@ -44,25 +44,7 @@ export function Comments( { onCommentDelete, onCommentResolve, } ) { - const [ actionState, setActionState ] = useState( false ); - const [ isConfirmDialogOpen, setIsConfirmDialogOpen ] = useState( false ); - - const handleConfirmDelete = () => { - onCommentDelete( actionState.id ); - setActionState( false ); - setIsConfirmDialogOpen( false ); - }; - - const handleConfirmResolve = () => { - onCommentResolve( actionState.id ); - setActionState( false ); - setIsConfirmDialogOpen( false ); - }; - - const handleCancelDelete = () => { - setActionState( false ); - setIsConfirmDialogOpen( false ); - }; + const [ focusThread, setFocusThread ] = useState( null ); const blockCommentId = useSelect( ( select ) => { const clientID = select( blockEditorStore ).getSelectedBlockClientId(); @@ -72,101 +54,11 @@ export function Comments( { ); }, [] ); - const CommentBoard = ( { thread, parentThread } ) => { - return ( - <> - { - setActionState( { - action: 'resolve', - id: parentThread?.id ?? thread.id, - } ); - setIsConfirmDialogOpen( true ); - } } - onEdit={ () => - setActionState( { action: 'edit', id: thread.id } ) - } - onDelete={ () => { - setActionState( { action: 'delete', id: thread.id } ); - setIsConfirmDialogOpen( true ); - } } - onReply={ - ! parentThread - ? () => - setActionState( { - action: 'reply', - id: thread.id, - } ) - : undefined - } - status={ parentThread?.status ?? thread.status } - /> - - - { 'edit' === actionState?.action && - thread.id === actionState?.id && ( - { - onEditComment( thread.id, value ); - setActionState( false ); - } } - onCancel={ () => setActionState( false ) } - thread={ thread } - submitButtonText={ _x( 'Update', 'verb' ) } - /> - ) } - { ( ! actionState || - 'edit' !== actionState?.action ) && ( - { thread?.content?.raw } - ) } - - - { 'resolve' === actionState?.action && - thread.id === actionState?.id && ( - - { - // translators: message displayed when confirming an action - __( - 'Are you sure you want to mark this comment as resolved?' - ) - } - - ) } - { 'delete' === actionState?.action && - thread.id === actionState?.id && ( - - { - // translators: message displayed when confirming an action - __( - 'Are you sure you want to delete this comment?' - ) - } - - ) } - - ); - }; + useEffect( () => { + if ( blockCommentId ) { + setFocusThread( blockCommentId ); + } + }, [ blockCommentId ] ); return ( <> @@ -186,7 +78,6 @@ export function Comments( { ) } - { Array.isArray( threads ) && threads.length > 0 && threads.map( ( thread ) => ( @@ -196,98 +87,157 @@ export function Comments( { 'editor-collab-sidebar-panel__thread', { 'editor-collab-sidebar-panel__active-thread': - blockCommentId && - blockCommentId === thread.id, + focusThread && focusThread === thread.id, } ) } id={ thread.id } spacing="3" + onClick={ () => setFocusThread( thread.id ) } > - - { 0 < thread?.reply?.length && - thread.reply.map( ( reply ) => ( - - - - ) ) } - { 'reply' === actionState?.action && - thread.id === actionState?.id && ( - - - - - - { - onAddReply( - inputComment, - thread.id - ); - setActionState( false ); - } } - onCancel={ () => - setActionState( false ) - } - submitButtonText={ _x( - 'Reply', - 'Add reply comment' - ) } - /> - - - ) } + ) ) } ); } -/** - * Renders the header of a comment in the collaboration sidebar. - * - * @param {Object} props - The component props. - * @param {Object} props.thread - The comment thread object. - * @param {Function} props.onResolve - The function to resolve the comment. - * @param {Function} props.onEdit - The function to edit the comment. - * @param {Function} props.onDelete - The function to delete the comment. - * @param {Function} props.onReply - The function to reply to the comment. - * @param {string} props.status - The status of the comment. - * @return {React.ReactNode} The rendered comment header. - */ -function CommentHeader( { +function Thread( { + thread, + onEditComment, + onAddReply, + onCommentDelete, + onCommentResolve, +} ) { + const [ showMoreReply, setShowMoreReply ] = useState( false ); + const [ showReplyInput, setShowReplyInput ] = useState( false ); + + const handleReply = () => { + setShowReplyInput( true ); + }; + + return ( + <> + + { 0 < thread?.reply?.length && ( + <> + { ! showMoreReply && ( + + ) } + + { showMoreReply && + thread.reply.map( ( reply ) => ( + + + + ) ) } + + ) } + { 'approved' !== thread.status && showReplyInput && ( + + + + + + { + onAddReply( inputComment, thread.id ); + } } + onCancel={ () => setShowReplyInput( false ) } + submitButtonText={ _x( + 'Reply', + 'Add reply comment' + ) } + /> + + + ) } + + ); +} + +const CommentBoard = ( { thread, onResolve, onEdit, onDelete, onReply, status, -} ) { +} ) => { + const [ actionState, setActionState ] = useState( false ); + const [ showConfirmDialog, setShowConfirmDialog ] = useState( false ); + + const handleConfirmDelete = () => { + onDelete( thread.id ); + setActionState( false ); + setShowConfirmDialog( false ); + }; + + const handleConfirmResolve = () => { + onResolve( thread.id ); + setActionState( false ); + setShowConfirmDialog( false ); + }; + + const handleCancel = () => { + setActionState( false ); + setShowConfirmDialog( false ); + }; + const actions = [ { title: _x( 'Edit', 'Edit comment' ), - onClick: onEdit, + onClick: () => { + setActionState( 'edit' ); + }, }, { title: _x( 'Delete', 'Delete comment' ), - onClick: onDelete, + onClick: () => { + setActionState( 'delete' ); + setShowConfirmDialog( true ); + }, }, { title: _x( 'Reply', 'Reply on a comment' ), @@ -298,45 +248,110 @@ function CommentHeader( { const moreActions = actions.filter( ( item ) => item.onClick ); return ( - - - - { status !== 'approved' && ( - - { 0 === thread?.parent && onResolve && ( -