@@ -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 ] ) ;
0 commit comments