Skip to content

Commit 43785b2

Browse files
committed
Use single hook to annotate whole content
1 parent 1ff9d05 commit 43785b2

3 files changed

Lines changed: 40 additions & 16 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { unlock } from '../../lock-unlock';
4141
import CommentAuthorInfo from './comment-author-info';
4242
import CommentForm from './comment-form';
4343
import { focusCommentThread, getCommentExcerpt } from './utils';
44-
import { useFloatingThread, useAnnotateThreadSelections } from './hooks';
44+
import { useFloatingThread } from './hooks';
4545
import { AddComment } from './add-comment';
4646
import { store as editorStore } from '../../store';
4747

@@ -472,7 +472,6 @@ function Thread( {
472472
commentLastUpdated,
473473
} );
474474
const isKeyboardTabbingRef = useRef( false );
475-
useAnnotateThreadSelections( thread );
476475

477476
const onMouseEnter = () => {
478477
debouncedToggleBlockHighlight( thread.blockClientId, true );

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

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -451,31 +451,55 @@ export function useFloatingThread( {
451451
};
452452
}
453453

454-
export function useAnnotateThreadSelections( thread ) {
454+
export function useAnnotateBlocks( threads ) {
455455
const {
456456
__experimentalAddAnnotation,
457457
__experimentalRemoveAnnotationsBySource,
458458
} = useDispatch( annotationsStore );
459459
const selections = useMemo( () => {
460-
const meta = [];
460+
const values = [];
461461

462-
if ( ! thread?.meta ) {
463-
return meta;
462+
if ( ! threads?.length ) {
463+
return values;
464464
}
465465

466-
// Empty object meta data is returned as array.
467-
if ( ! Array.isArray( thread.meta._wp_note_selection ) ) {
468-
meta.push( { id: thread.id, ...thread.meta._wp_note_selection } );
466+
function getSelectionMeta( thread ) {
467+
// Empty object meta data is returned as array.
468+
if (
469+
! thread?.meta?._wp_note_selection ||
470+
Array.isArray( thread.meta._wp_note_selection )
471+
) {
472+
return null;
473+
}
474+
475+
return {
476+
id: thread.id,
477+
...thread.meta._wp_note_selection,
478+
};
469479
}
470480

471-
for ( const reply of thread.reply ) {
472-
if ( ! Array.isArray( reply.meta._wp_note_selection ) ) {
473-
meta.push( { id: reply.id, ...reply.meta._wp_note_selection } );
481+
for ( const thread of threads ) {
482+
const threadSelection = getSelectionMeta( thread );
483+
if ( threadSelection ) {
484+
values.push( {
485+
clientId: thread.blockClientId,
486+
...threadSelection,
487+
} );
488+
}
489+
490+
for ( const reply of thread.reply ) {
491+
const replySelection = getSelectionMeta( reply );
492+
if ( replySelection ) {
493+
values.push( {
494+
clientId: thread.blockClientId,
495+
...replySelection,
496+
} );
497+
}
474498
}
475499
}
476500

477-
return meta;
478-
}, [ thread ] );
501+
return values;
502+
}, [ threads ] );
479503

480504
useEffect( () => {
481505
if ( ! selections.length ) {
@@ -486,7 +510,7 @@ export function useAnnotateThreadSelections( thread ) {
486510
__experimentalAddAnnotation( {
487511
id: selection.id,
488512
source: 'core-note',
489-
blockClientId: thread.blockClientId,
513+
blockClientId: selection.clientId,
490514
richTextIdentifier: selection.attributeKey,
491515
range: {
492516
start: selection.start,
@@ -500,7 +524,6 @@ export function useAnnotateThreadSelections( thread ) {
500524
};
501525
}, [
502526
selections,
503-
thread.blockClientId,
504527
__experimentalAddAnnotation,
505528
__experimentalRemoveAnnotationsBySource,
506529
] );

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
useBlockComments,
3030
useBlockCommentsActions,
3131
useEnableFloatingSidebar,
32+
useAnnotateBlocks,
3233
} from './hooks';
3334
import { focusCommentThread } from './utils';
3435
import PostTypeSupportCheck from '../post-type-support-check';
@@ -121,6 +122,7 @@ function NotesSidebar( { postId } ) {
121122
( unresolvedSortedThreads.length > 0 ||
122123
newNoteFormState !== 'closed' )
123124
);
125+
useAnnotateBlocks( unresolvedSortedThreads );
124126

125127
// Get the global styles to set the background color of the sidebar.
126128
const { merged: GlobalStyles } = useGlobalStylesContext();

0 commit comments

Comments
 (0)