Skip to content

Commit

Permalink
Expose API to allow triggering the anchor to be recomputed
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Jan 10, 2024
1 parent 32e820d commit 36a92d3
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/rich-text/src/component/use-anchor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useState, useLayoutEffect } from '@wordpress/element';
import { useState, useLayoutEffect, useCallback } from '@wordpress/element';

/** @typedef {import('../register-format-type').WPFormat} WPFormat */
/** @typedef {import('../types').RichTextValue} RichTextValue */
Expand Down Expand Up @@ -142,17 +142,15 @@ export function useAnchor( { editableContentElement, settings = {} } ) {
getAnchor( editableContentElement, tagName, className )
);

const callback = useCallback( () => {
setAnchor( getAnchor( editableContentElement, tagName, className ) );
}, [ className, editableContentElement, tagName ] );

useLayoutEffect( () => {
if ( ! editableContentElement ) return;

const { ownerDocument } = editableContentElement;

function callback() {
setAnchor(
getAnchor( editableContentElement, tagName, className )
);
}

function attach() {
ownerDocument.addEventListener( 'selectionchange', callback );
}
Expand All @@ -174,7 +172,8 @@ export function useAnchor( { editableContentElement, settings = {} } ) {
editableContentElement.removeEventListener( 'focusin', attach );
editableContentElement.removeEventListener( 'focusout', detach );
};
}, [ editableContentElement, tagName, className ] );
}, [ editableContentElement, tagName, className, callback ] );

anchor.update = callback;
return anchor;
}

0 comments on commit 36a92d3

Please sign in to comment.