From 1b1362c5957a4c9af9b138616b675f936d49c688 Mon Sep 17 00:00:00 2001 From: Seghir Nadir Date: Sat, 20 Jun 2020 21:25:17 +0100 Subject: [PATCH] add comment context --- src/components/editor/filters/comment.js | 33 +++++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/components/editor/filters/comment.js b/src/components/editor/filters/comment.js index 01a810a..b3bf429 100644 --- a/src/components/editor/filters/comment.js +++ b/src/components/editor/filters/comment.js @@ -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 + ); + } + 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(); @@ -31,6 +49,7 @@ function BlockCommentsControls( { clientId } ) { addComment( { start: selectionStart, end: selectionEnd, + context: selectedText, type: 'block', _id: uuidv4(), content: '',