Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/components/editor/filters/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,33 @@ function BlockCommentsControls( { clientId } ) {
getSelectedComment,
selectionStart,
selectionEnd,
} = useSelect(
( select ) => ( {
selectedText,
} = useSelect( ( select ) => {
const _selectionEnd = select( 'core/block-editor' ).getSelectionEnd();
const _selectionStart = select(
'core/block-editor'
).getSelectionStart();

let _selectedText = null;
const { offset: startOffset, attributeKey } = _selectionStart;
const { offset: endOffset } = _selectionEnd;

// we only support storing text now.
if ( attributeKey === 'content' ) {
const block = select( 'core/block-editor' ).getBlock( clientId );
_selectedText = block.attributes[ attributeKey ].slice(
startOffset,
endOffset
);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the attributeKey for the block and the attributeKey for the RichText are not always the same thing, so this will break in some blocks. For instance in a Table block, there's multiple RichTexts but a single attribute.

}
return {
getEditedProperty: select( 'asblocks' ).getEditedProperty,
getSelectedComment: select( 'asblocks' ).getSelectedComment,
selectionEnd: select( 'core/block-editor' ).getSelectionEnd(),
selectionStart: select( 'core/block-editor' ).getSelectionStart(),
} ),
[]
);
selectionEnd: _selectionEnd,
selectionStart: _selectionStart,
selectedText: _selectedText,
};
}, [] );

const { addComment, selectComment } = useDispatch( 'asblocks' );
const [ authorId ] = useAuthorId();
Expand All @@ -31,6 +49,7 @@ function BlockCommentsControls( { clientId } ) {
addComment( {
start: selectionStart,
end: selectionEnd,
context: selectedText,
type: 'block',
_id: uuidv4(),
content: '',
Expand Down