Skip to content
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
4 changes: 4 additions & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
- Fix pasting multi-line text into an empty Preformatted or Poetry block, which replaced the block with Paragraph blocks instead of keeping the text inside it ([#83552](https://github.com/WordPress/gutenberg/pull/83552)).


### Internal

- Convert the remaining `utils` modules and the `state-utils` and `useCachedTruthy` hook helpers to TypeScript ([#84629](https://github.com/WordPress/gutenberg/pull/84629)).

## 18.1.0 (2026-10-07)

### New Features
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { BlockType } from '@wordpress/blocks';
import { getBlockDefaultClassName, getBlockType } from '@wordpress/blocks';
import { splitSelectorList } from '@wordpress/global-styles-engine';

type BlockSelector = NonNullable< BlockType[ 'selectors' ] >[ string ];

/**
* Given a block's `selectors.root` value, returns the part of the selector
* that is relative to the block wrapper — i.e., everything after the first
Expand All @@ -11,10 +14,10 @@ import { splitSelectorList } from '@wordpress/global-styles-engine';
* ".wp-block-foo > .inner" → "> .inner"
* ".wp-block-foo" → null (no descendant)
*
* @param {string} rootSelector The block's `selectors.root` value.
* @return {string|null} Relative selector, or null if rootSelector targets the wrapper itself.
* @param rootSelector The block's `selectors.root` value.
* @return Relative selector, or null if rootSelector targets the wrapper itself.
*/
export function getRelativeRootSelector( rootSelector ) {
export function getRelativeRootSelector( rootSelector: string ): string | null {
// Match everything after the first compound selector (up to the first
// whitespace or combinator character).
// Require at least one combinator character (space, >, +, ~) between the
Expand All @@ -33,11 +36,11 @@ export function getRelativeRootSelector( rootSelector ) {
* a different block, e.g. `.wp-block-list > ` in List Item's `.wp-block-list > li`.
* Mirror of `gutenberg_get_state_selector_ancestor()` in PHP.
*
* @param {string} selector A single block or feature selector.
* @param {string=} name Name of the block being scoped.
* @return {string} The leading compound and its combinator, or an empty string.
* @param selector A single block or feature selector.
* @param name Name of the block being scoped.
* @return The leading compound and its combinator, or an empty string.
*/
function getStateSelectorAncestor( selector, name ) {
function getStateSelectorAncestor( selector: string, name?: string ): string {
const match = name && selector.match( /^\.(wp-block-[-_a-zA-Z0-9]+)/ );
if ( ! match || match[ 1 ] === getBlockDefaultClassName( name ) ) {
return '';
Expand All @@ -59,7 +62,7 @@ function getStateSelectorAncestor( selector, name ) {
break;
}
ancestorLength =
i + selector.slice( i ).match( /^[\s>+~]*/ )[ 0 ].length;
i + selector.slice( i ).match( /^[\s>+~]*/ )![ 0 ].length;
i = ancestorLength - 1;
} else if ( depth === 0 && ancestorLength ) {
nextCompound += char;
Expand All @@ -81,18 +84,18 @@ function getStateSelectorAncestor( selector, name ) {
* When the leading compound names a different block (an ancestor outside this
* block's markup), it is kept and the next compound is scoped instead.
*
* @param {string} baseSelector The block-instance scoping selector.
* @param {string} blockSelector The block or feature selector from block metadata.
* @param {string} suffix Optional selector suffix, e.g. ":hover".
* @param {string=} name Optional name of the block being scoped.
* @return {string} The scoped CSS selector.
* @param baseSelector The block-instance scoping selector.
* @param blockSelector The block or feature selector from block metadata.
* @param suffix Optional selector suffix, e.g. ":hover".
* @param name Optional name of the block being scoped.
* @return The scoped CSS selector.
*/
export function buildScopedBlockSelector(
baseSelector,
blockSelector,
suffix = '',
name
) {
baseSelector: string,
blockSelector: BlockSelector | undefined,
suffix: string = '',
name?: string
): string {
if ( typeof blockSelector !== 'string' || ! blockSelector ) {
return splitSelectorList( baseSelector )
.map( ( selector ) => `${ selector.trim() }${ suffix }` )
Expand Down Expand Up @@ -152,11 +155,14 @@ export function buildScopedBlockSelector(
* scoped under `baseSelector`. If no descendant is present, falls back to the
* base selector.
*
* @param {string} baseSelector The block-instance scoping class selector.
* @param {string} name The block name, used to look up selectors.
* @return {string} The fully-scoped CSS selector for root state styles.
* @param baseSelector The block-instance scoping class selector.
* @param name The block name, used to look up selectors.
* @return The fully-scoped CSS selector for root state styles.
*/
export function buildRootStyleStateSelector( baseSelector, name ) {
export function buildRootStyleStateSelector(
baseSelector: string,
name: string
): string {
const rootSelector = getBlockType( name )?.selectors?.root;
return buildScopedBlockSelector( baseSelector, rootSelector );
}
Expand All @@ -170,16 +176,24 @@ export function buildRootStyleStateSelector( baseSelector, name ) {
* portion (".wp-block-button__link") is scoped under `baseSelector`. If no
* descendant is present, falls back to appending the state to `baseSelector`.
*
* @param {string} baseSelector The block-instance scoping class selector.
* @param {string} name The block name, used to look up selectors.
* @param {string} state The pseudo-class string, e.g. ":hover".
* @return {string} The fully-scoped CSS selector for this state.
* @param baseSelector The block-instance scoping class selector.
* @param name The block name, used to look up selectors.
* @param state The pseudo-class string, e.g. ":hover".
* @return The fully-scoped CSS selector for this state.
*/
export function buildPseudoStyleStateSelector( baseSelector, name, state ) {
export function buildPseudoStyleStateSelector(
baseSelector: string,
name: string,
state: string
): string {
return `${ buildRootStyleStateSelector( baseSelector, name ) }${ state }`;
}

export function buildStateSelector( baseSelector, name, state ) {
export function buildStateSelector(
baseSelector: string,
name: string,
state: string
): string {
const rootSelector = getBlockType( name )?.selectors?.root;
return buildScopedBlockSelector( baseSelector, rootSelector, state );
}
Expand All @@ -193,11 +207,14 @@ export function buildStateSelector( baseSelector, name, state ) {
* that descendant inside the block wrapper. Otherwise it targets the wrapper
* itself.
*
* @param {string} clientId The block's clientId.
* @param {string} name The block name, used to look up selectors.
* @return {string} CSS selector scoped to this block instance.
* @param clientId The block's clientId.
* @param name The block name, used to look up selectors.
* @return CSS selector scoped to this block instance.
*/
export function buildCanvasStateSelector( clientId, name ) {
export function buildCanvasStateSelector(
clientId: string,
name: string
): string {
const rootSelector = getBlockType( name )?.selectors?.root;
return buildScopedBlockSelector(
`[data-block="${ clientId }"]`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useState, useEffect } from '@wordpress/element';
* @param {any} value
* @return {any} value
*/
export function useCachedTruthy( value ) {
export function useCachedTruthy< T >( value: T ): T {
const [ cachedValue, setCachedValue ] = useState( value );
useEffect( () => {
if ( value ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import type {
Block,
BlockBlockTransform,
NormalizedBlockTransform,
} from '@wordpress/blocks';
import {
findTransform,
getBlockTransforms,
Expand All @@ -11,19 +16,22 @@ import {
* block's own transform changes what the block is (a quote's transform to
* group dissolves the quote into its inner blocks).
*
* @param {WPBlock[]} blocks Blocks to group.
* @param {string} groupingBlockName Name of the grouping block.
* @param blocks Blocks to group.
* @param groupingBlockName Name of the grouping block.
*
* @return {?WPBlock[]} The grouped blocks, or null.
* @return The grouped blocks, or null.
*/
export function groupBlocks( blocks, groupingBlockName ) {
export function groupBlocks(
blocks: Block[],
groupingBlockName: string
): Block[] | null {
const groupingBlockTransform = findTransform(
getBlockTransforms( 'from', groupingBlockName ),
( transform ) =>
transform.type === 'block' &&
transform.isMultiBlock &&
!! transform.isMultiBlock &&
transform.blocks.includes( '*' )
);
) as NormalizedBlockTransform< BlockBlockTransform > | null;

if ( groupingBlockTransform?.__experimentalConvert ) {
const result = groupingBlockTransform.__experimentalConvert( blocks );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { useRefEffect } from '@wordpress/compose';
* HTML editing field, a search field), registered through `useNativeUndo`.
* The editor history must not act on events originating within them.
*/
const nativeUndoNodes = new WeakSet();
const nativeUndoNodes = new WeakSet< Element >();

/**
* Returns a ref that marks an element (and its descendants) as relying on
* the browser's own undo, so the editor history leaves the undo and redo
* shortcuts alone within it.
*
* @return {Function} Ref callback.
* @return Ref callback.
*/
export function useNativeUndo() {
return useRefEffect( ( node ) => {
return useRefEffect< Element >( ( node ) => {
nativeUndoNodes.add( node );
return () => {
nativeUndoNodes.delete( node );
Expand All @@ -31,15 +31,15 @@ export function useNativeUndo() {
* element, so the real target is resolved through the frame's focused
* element.
*
* @param {KeyboardEvent} event Keyboard event.
* @param event Keyboard event.
*
* @return {boolean} Whether the element relies on the browser's own undo.
* @return Whether the element relies on the browser's own undo.
*/
export function usesNativeUndo( event ) {
let { target } = event;
export function usesNativeUndo( event: KeyboardEvent ): boolean {
let target = event.target as Element | null | undefined;

if ( target?.nodeName === 'IFRAME' ) {
target = target.contentDocument?.activeElement;
target = ( target as HTMLIFrameElement ).contentDocument?.activeElement;
}

while ( target ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { getBlockAttributesNamesByRole } from '@wordpress/blocks';
* (attributes with the `content` role) and its metadata.
* A styled sibling thus yields an equally styled, empty new block.
*
* @param {string} blockName The block name.
* @param {?Object} attributes The adjacent block's attributes.
* @param blockName The block name.
* @param attributes The adjacent block's attributes.
*
* @return {Object} The attributes for the new block.
* @return The attributes for the new block.
*/
export function getSiblingBlockAttributes( blockName, attributes ) {
export function getSiblingBlockAttributes(
blockName: string,
attributes?: Record< string, unknown > | null
): Record< string, unknown > {
if ( ! attributes ) {
return {};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ export function useNotifyCopy() {
const { createSuccessNotice } = useDispatch( noticesStore );

return useCallback(
( eventType, selectedBlockClientIds ) => {
( eventType: string, selectedBlockClientIds: string[] ) => {
let notice = '';

if ( eventType === 'copyStyles' ) {
notice = __( 'Styles copied to clipboard.' );
} else if ( selectedBlockClientIds.length === 1 ) {
const clientId = selectedBlockClientIds[ 0 ];
const title = getBlockType( getBlockName( clientId ) )?.title;
// A selected block always has a registered type.
const title = getBlockType( getBlockName( clientId ) )?.title!;

if ( eventType === 'copy' ) {
notice = sprintf(
Expand Down
Loading