Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish rich text build types #49651

Merged
merged 15 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/block-editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
{ "path": "../is-shallow-equal" },
{ "path": "../keycodes" },
{ "path": "../notices" },
{ "path": "../rich-text" },
{ "path": "../style-engine" },
{ "path": "../token-list" },
{ "path": "../url" },
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
{ "path": "../notices" },
{ "path": "../keycodes" },
{ "path": "../primitives" },
{ "path": "../rich-text" },
{ "path": "../url" },
{ "path": "../wordcount" }
],
Expand Down
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Internal

- `NavigableContainer`: Convert to TypeScript ([#49377](https://github.com/WordPress/gutenberg/pull/49377)).
- Move rich-text related types to the rich-text package ([#49651](https://github.com/WordPress/gutenberg/pull/49651)).

### Documentation

Expand Down
2 changes: 0 additions & 2 deletions packages/components/src/autocomplete/autocompleter-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
useEffect,
useState,
} from '@wordpress/element';
// Error expected because `@wordpress/rich-text` is not yet fully typed.
// @ts-expect-error
import { useAnchor } from '@wordpress/rich-text';
import { useMergeRefs, useRefEffect } from '@wordpress/compose';

Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import {
insert,
isCollapsed,
getTextContent,
// Error expected because `@wordpress/rich-text` is not yet fully typed.
// @ts-expect-error
} from '@wordpress/rich-text';
import { speak } from '@wordpress/a11y';

Expand Down Expand Up @@ -241,6 +239,7 @@ export function useAutocomplete( {
if ( isCollapsed( record ) ) {
return getTextContent( slice( record, 0 ) );
}
return '';
}, [ record ] );

useEffect( () => {
Expand Down
22 changes: 3 additions & 19 deletions packages/components/src/autocomplete/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* WordPress dependencies
*/
import type { WPElement } from '@wordpress/element';
import type { RichTextValue } from '@wordpress/rich-text';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -157,24 +159,6 @@ export type CancelablePromise< T = void > = Promise< T > & {
canceled?: boolean;
};

/**
* When `@wordpress/rich-text` is fully typed, the following
* types should be moved to and imported from there
*
* @see /packages/rich-text/src/create.js
*/
type RichTextFormat = {
type: string;
};
type RichTextFormatList = Array< RichTextFormat >;
type RichTextValue = {
text: string;
formats?: Array< RichTextFormatList >;
replacements?: Array< RichTextFormat >;
start: number | undefined;
end: number | undefined;
};

export type UseAutocompleteProps = {
/**
* The rich text value object the autocompleter is being applied to.
Expand All @@ -187,7 +171,7 @@ export type UseAutocompleteProps = {
* A function to be called when an option is selected to insert into the
* existing text.
*/
onChange: ( value: string ) => void;
onChange: ( value: RichTextValue ) => void;
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
/**
* A function to be called when an option is selected to replace the
* existing text.
Expand Down
1 change: 1 addition & 0 deletions packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
{ "path": "../primitives" },
{ "path": "../private-apis" },
{ "path": "../react-i18n" },
{ "path": "../rich-text" },
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
{ "path": "../warning" }
],
"include": [ "src/**/*" ],
Expand Down
7 changes: 7 additions & 0 deletions packages/element/src/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ import {
* @typedef {import('react').SyntheticEvent} WPSyntheticEvent
*/

/**
* Object containing a React synthetic event.
*
* @template T
* @typedef {import('react').RefObject<T>} RefObject<T>
*/

/**
* Object that provides utilities for dealing with React children.
*/
Expand Down
16 changes: 11 additions & 5 deletions packages/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,13 @@ Check if the selection of a Rich Text value is collapsed or not. Collapsed means

_Parameters_

- _value_ `RichTextValue`: The rich text value to check.
- _props_ `RichTextValue`: The rich text value to check.
- _props.start_ `RichTextValue[ 'start' ]`:
- _props.end_ `RichTextValue[ 'end' ]`:

_Returns_

- `boolean|undefined`: True if the selection is collapsed, false if not, undefined if there is no selection.
- `boolean | undefined`: True if the selection is collapsed, false if not, undefined if there is no selection.

### isEmpty

Expand Down Expand Up @@ -356,6 +358,10 @@ _Returns_

- `RichTextValue`: A new value with replacements applied.

### RichTextValue

An object which represents a formatted string. See main `@wordpress/rich-text` documentation for more information.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a ton more detail about this already at the top of the readme


### slice

Slice a Rich Text value from `startIndex` to `endIndex`. Indices are retrieved from the selection if none are provided. This is similar to `String.prototype.slice`.
Expand Down Expand Up @@ -433,7 +439,7 @@ _Parameters_

_Returns_

- `RichTextFormatType|undefined`: The previous format value, if it has been successfully unregistered; otherwise `undefined`.
- `WPFormat|undefined`: The previous format value, if it has been successfully unregistered; otherwise `undefined`.

### useAnchor

Expand All @@ -443,7 +449,7 @@ _Parameters_

- _$1_ `Object`: Named parameters.
- _$1.editableContentElement_ `HTMLElement|null`: The element containing the editable content.
- _$1.settings_ `RichTextFormatType`: The format type's settings.
- _$1.settings_ `WPFormat=`: The format type's settings.

_Returns_

Expand All @@ -458,7 +464,7 @@ _Parameters_
- _$1_ `Object`: Named parameters.
- _$1.ref_ `RefObject<HTMLElement>`: React ref of the element containing the editable content.
- _$1.value_ `RichTextValue`: Value to check for selection.
- _$1.settings_ `RichTextFormatType`: The format type's settings.
- _$1.settings_ `WPFormat`: The format type's settings.

_Returns_

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 @@ -28,6 +28,7 @@
"src/**/*.scss",
"{src,build,build-module}/{index.js,store/index.js}"
],
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/a11y": "file:../a11y",
Expand Down
4 changes: 2 additions & 2 deletions packages/rich-text/src/apply-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import { normaliseFormats } from './normalise-formats';

/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./create').RichTextFormat} RichTextFormat */
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */

function replace( array, index, value ) {
array = array.slice();
Expand Down
11 changes: 7 additions & 4 deletions packages/rich-text/src/component/use-anchor-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import deprecated from '@wordpress/deprecated';
*/
import { getActiveFormat } from '../get-active-format';

/** @typedef {import('@wordpress/element').RefObject} RefObject */
/** @typedef {import('../register-format-type').RichTextFormatType} RichTextFormatType */
/** @typedef {import('../create').RichTextValue} RichTextValue */
/**
* @template T
* @typedef {import('@wordpress/element').RefObject<T>} RefObject<T>
*/
/** @typedef {import('../register-format-type').WPFormat} WPFormat */
/** @typedef {import('../types').RichTextValue} RichTextValue */

/**
* This hook, to be used in a format type's Edit component, returns the active
Expand All @@ -23,7 +26,7 @@ import { getActiveFormat } from '../get-active-format';
* @param {RefObject<HTMLElement>} $1.ref React ref of the element
* containing the editable content.
* @param {RichTextValue} $1.value Value to check for selection.
* @param {RichTextFormatType} $1.settings The format type's settings.
* @param {WPFormat} $1.settings The format type's settings.
*
* @return {Element|Range} The active element or selection range.
*/
Expand Down
16 changes: 8 additions & 8 deletions packages/rich-text/src/component/use-anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
import { useState, useLayoutEffect } from '@wordpress/element';

/** @typedef {import('../register-format-type').RichTextFormatType} RichTextFormatType */
/** @typedef {import('../create').RichTextValue} RichTextValue */
/** @typedef {import('../register-format-type').WPFormat} WPFormat */
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
/** @typedef {import('../types').RichTextValue} RichTextValue */

/**
* Given a range and a format tag name and class name, returns the closest
Expand Down Expand Up @@ -50,8 +50,8 @@ function getFormatElement( range, editableContentElement, tagName, className ) {

/**
* @typedef {Object} VirtualAnchorElement
* @property {Function} getBoundingClientRect A function returning a DOMRect
* @property {Document} ownerDocument The element's ownerDocument
* @property {() => DOMRect} getBoundingClientRect A function returning a DOMRect
* @property {Document} ownerDocument The element's ownerDocument
*/

/**
Expand Down Expand Up @@ -117,10 +117,10 @@ function getAnchor( editableContentElement, tagName, className ) {
* 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 {RichTextFormatType} $1.settings The format type's settings.
* @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.
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
* @return {Element|VirtualAnchorElement|undefined|null} The active element or selection range.
*/
export function useAnchor( { editableContentElement, settings = {} } ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/rich-text/src/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { normaliseFormats } from './normalise-formats';
import { create } from './create';

/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextValue} RichTextValue */

/**
* Concats a pair of rich text values. Not that this mutates `a` and does NOT
Expand Down
20 changes: 1 addition & 19 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,7 @@ import {
ZWNBSP,
} from './special-characters';

/**
* @typedef {Object} RichTextFormat
*
* @property {string} type Format type.
*/

/**
* @typedef {Array<RichTextFormat>} RichTextFormatList
*/

/**
* @typedef {Object} RichTextValue
*
* @property {string} text Text.
* @property {Array<RichTextFormatList>} formats Formats.
* @property {Array<RichTextFormat>} replacements Replacements.
* @property {number|undefined} start Selection start.
* @property {number|undefined} end Selection end.
*/
/** @typedef {import('./types').RichTextValue} RichTextValue */

function createEmptyValue() {
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/rich-text/src/get-active-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
import { getActiveFormats } from './get-active-formats';

/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./create').RichTextFormat} RichTextFormat */
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */

/**
* Gets the format object by type at the start of the selection. This can be
Expand Down
4 changes: 2 additions & 2 deletions packages/rich-text/src/get-active-formats.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./create').RichTextFormatList} RichTextFormatList */
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormatList} RichTextFormatList */

/**
* Internal dependencies
Expand Down
4 changes: 2 additions & 2 deletions packages/rich-text/src/get-active-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';

/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./create').RichTextFormat} RichTextFormat */
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */

/**
* Gets the active object, if there is any.
Expand Down
2 changes: 1 addition & 1 deletion packages/rich-text/src/get-text-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
LINE_SEPARATOR,
} from './special-characters';

/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextValue} RichTextValue */

const pattern = new RegExp(
`[${ OBJECT_REPLACEMENT_CHARACTER }${ LINE_SEPARATOR }]`,
Expand Down
6 changes: 6 additions & 0 deletions packages/rich-text/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ export {
useRichText as __unstableUseRichText,
} from './component';
export { default as __unstableFormatEdit } from './component/format-edit';

/**
* An object which represents a formatted string. See main `@wordpress/rich-text`
* documentation for more information.
*/
export { RichTextValue } from './types';
2 changes: 1 addition & 1 deletion packages/rich-text/src/insert-line-separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { insert } from './insert';
import { LINE_SEPARATOR } from './special-characters';

/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextValue} RichTextValue */

/**
* Insert a line break character into a Rich Text value at the given
Expand Down
4 changes: 2 additions & 2 deletions packages/rich-text/src/insert-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import { insert } from './insert';
import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';

/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./create').RichTextFormat} RichTextFormat */
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */

/**
* Insert a format as an object into a Rich Text value at the given
Expand Down
2 changes: 1 addition & 1 deletion packages/rich-text/src/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { create } from './create';
import { normaliseFormats } from './normalise-formats';

/** @typedef {import('./create').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextValue} RichTextValue */

/**
* Insert a Rich Text value, an HTML string, or a plain text string, into a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
/** @typedef {import('./create').RichTextValue} RichTextValue */
/**
* Internal dependencies
*/
import type { RichTextValue } from './types';

/**
* Check if the selection of a Rich Text value is collapsed or not. Collapsed
* means that no characters are selected, but there is a caret present. If there
* is no selection, `undefined` will be returned. This is similar to
* `window.getSelection().isCollapsed()`.
*
* @param {RichTextValue} value The rich text value to check.
*
* @return {boolean|undefined} True if the selection is collapsed, false if not,
* undefined if there is no selection.
* @param props The rich text value to check.
* @param props.start
* @param props.end
* @return True if the selection is collapsed, false if not, undefined if there is no selection.
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
*/
export function isCollapsed( { start, end } ) {
export function isCollapsed( {
start,
end,
}: RichTextValue ): boolean | undefined {
if ( start === undefined || end === undefined ) {
return;
}
Expand Down
Loading