Skip to content

Commit 0699639

Browse files
t-hamanoMamadukaadamsilverstein
authored
Block Comment: make blockCommentId as part of the metadata (WordPress#71921)
* Block Comment: make blockCommentId as part of the metadata * Refer to the value as blockCommentId * Remove key prop from Comments component * Revert "Remove key prop from Comments component" This reverts commit 90f58bd. * Use getSelectedBlockClientId and getBlockAttributes inside callback Co-authored-by: t-hamano <[email protected]> Co-authored-by: Mamaduka <[email protected]> Co-authored-by: adamsilverstein <[email protected]>
1 parent d55d5fa commit 0699639

File tree

5 files changed

+41
-40
lines changed

5 files changed

+41
-40
lines changed

packages/block-library/src/utils/get-transformed-metadata.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export function getTransformedMetadata(
4242
transformSupportedProps.push( 'name' );
4343
}
4444

45+
if ( window?.__experimentalEnableBlockComment ) {
46+
transformSupportedProps.push( 'commentId' );
47+
}
48+
4549
// Return early if no supported properties.
4650
if ( ! transformSupportedProps.length ) {
4751
return;

packages/editor/src/components/collab-sidebar/add-comment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function AddComment( {
3636
const selectedBlock = getSelectedBlock();
3737
return {
3838
clientId: selectedBlock?.clientId,
39-
blockCommentId: selectedBlock?.attributes?.blockCommentId,
39+
blockCommentId: selectedBlock?.attributes?.metadata?.commentId,
4040
isEmptyDefaultBlock: selectedBlock
4141
? isUnmodifiedDefaultBlock( selectedBlock )
4242
: false,

packages/editor/src/components/collab-sidebar/comments.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ export function Comments( {
5151
onCommentDelete,
5252
setShowCommentBoard,
5353
} ) {
54-
const { blockCommentId } = useSelect( ( select ) => {
54+
const blockCommentId = useSelect( ( select ) => {
5555
const { getBlockAttributes, getSelectedBlockClientId } =
5656
select( blockEditorStore );
5757
const clientId = getSelectedBlockClientId();
58-
return {
59-
blockCommentId: clientId
60-
? getBlockAttributes( clientId )?.blockCommentId
61-
: null,
62-
};
58+
return clientId
59+
? getBlockAttributes( clientId )?.metadata?.commentId
60+
: null;
6361
}, [] );
6462
const [ focusThread = blockCommentId, setFocusThread ] = useState();
6563

packages/editor/src/components/collab-sidebar/hooks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export function useBlockComments( postId ) {
2626
select( blockEditorStore );
2727

2828
return getClientIdsWithDescendants().reduce( ( results, clientId ) => {
29-
const commentId = getBlockAttributes( clientId )?.blockCommentId;
29+
const commentId =
30+
getBlockAttributes( clientId )?.metadata?.commentId;
3031
if ( commentId ) {
3132
results[ commentId ] = clientId;
3233
}

packages/editor/src/components/collab-sidebar/index.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { useSelect, useDispatch, subscribe } from '@wordpress/data';
66
import { useState } from '@wordpress/element';
77
import { useViewportMatch } from '@wordpress/compose';
88
import { comment as commentIcon } from '@wordpress/icons';
9-
import { addFilter } from '@wordpress/hooks';
109
import { store as noticesStore } from '@wordpress/notices';
1110
import { store as coreStore } from '@wordpress/core-data';
1211
import { store as blockEditorStore } from '@wordpress/block-editor';
@@ -26,26 +25,6 @@ import CommentAvatarIndicator from './comment-indicator-toolbar';
2625
import { useGlobalStylesContext } from '../global-styles-provider';
2726
import { useBlockComments } from './hooks';
2827

29-
const modifyBlockCommentAttributes = ( settings ) => {
30-
if ( ! settings.attributes.blockCommentId ) {
31-
settings.attributes = {
32-
...settings.attributes,
33-
blockCommentId: {
34-
type: 'number',
35-
},
36-
};
37-
}
38-
39-
return settings;
40-
};
41-
42-
// Apply the filter to all core blocks
43-
addFilter(
44-
'blocks.registerBlockType',
45-
'block-comment/modify-core-block-attributes',
46-
modifyBlockCommentAttributes
47-
);
48-
4928
function CollabSidebarContent( {
5029
showCommentBoard,
5130
setShowCommentBoard,
@@ -54,9 +33,18 @@ function CollabSidebarContent( {
5433
} ) {
5534
const { createNotice } = useDispatch( noticesStore );
5635
const { saveEntityRecord, deleteEntityRecord } = useDispatch( coreStore );
57-
const { getCurrentPostId } = useSelect( editorStore );
58-
const { getSelectedBlockClientId } = useSelect( blockEditorStore );
5936
const { updateBlockAttributes } = useDispatch( blockEditorStore );
37+
const { currentPostId, getSelectedBlockClientId, getBlockAttributes } =
38+
useSelect( ( select ) => {
39+
const { getCurrentPostId } = select( editorStore );
40+
return {
41+
getSelectedBlockClientId:
42+
select( blockEditorStore ).getSelectedBlockClientId,
43+
getBlockAttributes:
44+
select( blockEditorStore ).getBlockAttributes,
45+
currentPostId: getCurrentPostId(),
46+
};
47+
}, [] );
6048

6149
const onError = ( error ) => {
6250
const errorMessage =
@@ -75,7 +63,7 @@ function CollabSidebarContent( {
7563
'root',
7664
'comment',
7765
{
78-
post: getCurrentPostId(),
66+
post: currentPostId,
7967
content,
8068
comment_type: 'block_comment',
8169
comment_approved: 0,
@@ -86,8 +74,14 @@ function CollabSidebarContent( {
8674

8775
// If it's a main comment, update the block attributes with the comment id.
8876
if ( ! parent && savedRecord?.id ) {
77+
const metadata = getBlockAttributes(
78+
getSelectedBlockClientId()
79+
)?.metadata;
8980
updateBlockAttributes( getSelectedBlockClientId(), {
90-
blockCommentId: savedRecord.id,
81+
metadata: {
82+
...metadata,
83+
commentId: savedRecord.id,
84+
},
9185
} );
9286
}
9387

@@ -151,8 +145,14 @@ function CollabSidebarContent( {
151145
);
152146

153147
if ( ! comment.parent ) {
148+
const metadata = getBlockAttributes(
149+
getSelectedBlockClientId()
150+
)?.metadata;
154151
updateBlockAttributes( getSelectedBlockClientId(), {
155-
blockCommentId: undefined,
152+
metadata: {
153+
...metadata,
154+
commentId: undefined,
155+
},
156156
} );
157157
}
158158

@@ -201,16 +201,14 @@ export default function CollabSidebar() {
201201
};
202202
}, [] );
203203

204-
const { blockCommentId } = useSelect( ( select ) => {
204+
const blockCommentId = useSelect( ( select ) => {
205205
const { getBlockAttributes, getSelectedBlockClientId } =
206206
select( blockEditorStore );
207207
const _clientId = getSelectedBlockClientId();
208208

209-
return {
210-
blockCommentId: _clientId
211-
? getBlockAttributes( _clientId )?.blockCommentId
212-
: null,
213-
};
209+
return _clientId
210+
? getBlockAttributes( _clientId )?.metadata?.commentId
211+
: null;
214212
}, [] );
215213

216214
const openCollabBoard = () => {

0 commit comments

Comments
 (0)