Skip to content

Commit

Permalink
Add privateApis to RichText package and export useAnchorWithUpdate hook
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Jan 12, 2024
1 parent 5959090 commit cbcbf41
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 26 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/private-apis/src/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const CORE_MODULES_USING_PRIVATE_APIS = [
'@wordpress/format-library',
'@wordpress/patterns',
'@wordpress/reusable-blocks',
'@wordpress/rich-text',
'@wordpress/router',
'@wordpress/dataviews',
];
Expand Down
4 changes: 4 additions & 0 deletions packages/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ _Returns_

- `RichTextValue`: A new combined value.

### privateApis

Private @wordpress/block-editor APIs.

### registerFormatType

Registers a new format provided a unique name and an object defining its behavior.
Expand Down
1 change: 1 addition & 0 deletions packages/rich-text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@wordpress/escape-html": "file:../escape-html",
"@wordpress/i18n": "file:../i18n",
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/private-apis": "file:../private-apis",
"memize": "^2.1.0",
"rememo": "^4.0.2"
},
Expand Down
55 changes: 29 additions & 26 deletions packages/rich-text/src/component/use-anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@ function createVirtualAnchorElement( range, editableContentElement ) {
};
}

/**
* Get the anchor: a format element if there is a matching one based on the
* tagName and className or a range otherwise.
*
* @param {HTMLElement} editableContentElement The editable wrapper.
* @param {string} tagName The tag name of the format
* element.
* @param {string} className The class name of the format
* element.
*
* @return {HTMLElement|VirtualAnchorElement|undefined} The anchor.
*/
function getAnchor( editableContentElement, tagName, className ) {
if ( ! editableContentElement ) return;

Expand All @@ -124,19 +112,7 @@ function getAnchor( editableContentElement, tagName, className ) {
return createVirtualAnchorElement( range, editableContentElement );
}

/**
* This hook, to be used in a format type's Edit component, returns the active
* element that is formatted, or a virtual element for the selection range if
* no format is active. The returned value is meant to be used for positioning
* UI, e.g. by passing it to the `Popover` component via the `anchor` prop.
*
* @param {Object} $1 Named parameters.
* @param {HTMLElement|null} $1.editableContentElement The element containing
* the editable content.
* @param {WPFormat=} $1.settings The format type's settings.
* @return {Element|VirtualAnchorElement|undefined|null} The active element or selection range.
*/
export function useAnchor( { editableContentElement, settings = {} } ) {
function useAnchorBase( { editableContentElement, settings = {} } ) {
const { tagName, className } = settings;
const [ anchor, setAnchor ] = useState( () =>
getAnchor( editableContentElement, tagName, className )
Expand Down Expand Up @@ -174,6 +150,33 @@ export function useAnchor( { editableContentElement, settings = {} } ) {
};
}, [ editableContentElement, tagName, className, callback ] );

anchor.update = callback;
return {
anchor,
update: callback,
};
}

/**
* This hook, to be used in a format type's Edit component, returns the active
* element that is formatted, or a virtual element for the selection range if
* no format is active. The returned value is meant to be used for positioning
* UI, e.g. by passing it to the `Popover` component via the `anchor` prop.
*
* @param {Object} $1 Named parameters.
* @param {HTMLElement|null} $1.editableContentElement The element containing
* the editable content.
* @param {WPFormat=} $1.settings The format type's settings.
* @return {Element|VirtualAnchorElement|undefined|null} The active element or selection range.
*/
export function useAnchor( { editableContentElement, settings = {} } ) {
const { anchor } = useAnchorBase( { editableContentElement, settings } );

return anchor;
}

export function useAnchorWithUpdate( {
editableContentElement,
settings = {},
} ) {
return useAnchorBase( { editableContentElement, settings } );
}
2 changes: 2 additions & 0 deletions packages/rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export {
* documentation for more information.
*/
export type { RichTextValue } from './types';

export { privateApis } from './private-apis';
10 changes: 10 additions & 0 deletions packages/rich-text/src/lock-unlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* WordPress dependencies
*/
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/rich-text'
);
13 changes: 13 additions & 0 deletions packages/rich-text/src/private-apis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Internal dependencies
*/
import { useAnchorWithUpdate } from './component/use-anchor';
import { lock } from './lock-unlock';

/**
* Private @wordpress/block-editor APIs.
*/
export const privateApis = {};
lock( privateApis, {
useAnchorWithUpdate,
} );

0 comments on commit cbcbf41

Please sign in to comment.