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
1 change: 1 addition & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
- Convert `AlignmentControl`, `BlockAlignmentMatrixControl`, `HeadingLevelDropdown`, `HTMLElementControl`, `BlockFullHeightAlignmentControl`, `BlockEditVisuallyButton`, `ToolSelector`, `PreviewOptions`, `useBlockOverlayActive`, `UnitControl`, `ResizableBoxPopover` and `HeightControl` to TypeScript ([#84912](https://github.com/WordPress/gutenberg/pull/84912)).
- Convert `InserterListbox`, `BlockPatternsPaging`, `__unstableInserterMenuExtension`, `BlockCard`, `BlockVariationTransforms`, `BlockVariationPicker`, `InspectorPopoverHeader`, `MultiSelectionInspector`, `BlockQuickNavigation` and `TabbedSidebar` to TypeScript ([#84913](https://github.com/WordPress/gutenberg/pull/84913)).
- Convert `BlockEditorProvider`, `ExperimentalBlockEditorProvider`, `withRegistryProvider`, `useBlockSync`, `BlockRefsProvider`, the selection context and `useMediaUploadSettings` to TypeScript ([#84915](https://github.com/WordPress/gutenberg/pull/84915)).
- Convert `WritingFlow` and its hooks to TypeScript ([#84918](https://github.com/WordPress/gutenberg/pull/84918)).

## 18.1.0 (2026-10-07)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useNotifyCopy } from '../../utils/use-notify-copy';
import { focusListItem } from './utils';
import { getPasteBlocks, setClipboardBlocks } from '../writing-flow/utils';

// This hook borrows from useClipboardHandler in ../writing-flow/use-clipboard-handler.js
// This hook borrows from useClipboardHandler in ../writing-flow/use-clipboard-handler.ts
// and adds behaviour for the list view, while skipping partial selection.
export default function useClipboardHandler( { selectBlock } ) {
const registry = useRegistry();
Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/provider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ReactNode } from 'react';
import type { ObservableMap } from '@wordpress/compose';
import type { Block } from '@wordpress/blocks';
import type { DataRegistry } from '@wordpress/data';
import type { BlockEventHandlers } from '../writing-flow/types';
import type {
BlockSelection,
InitialPosition,
Expand All @@ -23,7 +24,7 @@ declare global {
*/
export interface BlockRefsContextValue {
refsMap: ObservableMap< string, Element | null >;
eventHandlers: Map< string, unknown >;
eventHandlers: Map< string, { current?: BlockEventHandlers } >;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { useMergeRefs, useRefEffect } from '@wordpress/compose';
import { forwardRef } from '@wordpress/element';
import type { ForwardedRef, ReactElement, RefCallback } from 'react';
import useEditableRoot from './use-editable-root';
import useHomeEnd from './use-home-end';
import useMultiSelection from './use-multi-selection';
Expand All @@ -19,8 +20,13 @@ import useInput from './use-input';
import useClipboardHandler from './use-clipboard-handler';
import { store as blockEditorStore } from '../../store';
import { useMultiBlockFormatShortcuts } from '../rich-text/multi-block-format-edit';
import type { WritingFlowProps } from './types';

export function useWritingFlow() {
export function useWritingFlow(): [
ReactElement,
RefCallback< HTMLElement >,
ReactElement,
] {
const [ before, ref, after ] = useTabNav();
const hasMultiSelection = useSelect(
( select ) => select( blockEditorStore ).hasMultiSelection(),
Expand Down Expand Up @@ -48,17 +54,19 @@ export function useWritingFlow() {
useSelectAll(),
useArrowNav(),
usePreviewModeNav(),
useRefEffect(
useRefEffect< HTMLElement >(
( node ) => {
node.tabIndex = 0;
node.dataset.hasMultiSelection = hasMultiSelection;
// The DOM stringifies the boolean.
node.dataset.hasMultiSelection =
hasMultiSelection as unknown as string;
// The wrapper is the canvas root: an editing host while
// a selection spans blocks or the selected block
// supports `editableRoot`, explicitly not one otherwise,
// so it can always be found as the outermost editable
// ancestor of a field.
if ( ! node.hasAttribute( 'contenteditable' ) ) {
node.contentEditable = false;
node.contentEditable = false as unknown as string;
}

if ( ! hasMultiSelection ) {
Expand Down Expand Up @@ -94,7 +102,10 @@ export function useWritingFlow() {
];
}

function WritingFlow( { children, ...props }, forwardedRef ) {
function WritingFlow(
{ children, ...props }: WritingFlowProps,
forwardedRef: ForwardedRef< HTMLDivElement >
) {
const [ before, ref, after ] = useWritingFlow();
return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UP, DOWN, LEFT, RIGHT } from '@wordpress/keycodes';
import { isNavigationCandidate } from '../use-arrow-nav';

describe( 'isNavigationCandidate', () => {
let elements;
let elements: Record< string, HTMLElement >;
beforeAll( () => {
elements = {};

Expand All @@ -17,7 +17,8 @@ describe( 'isNavigationCandidate', () => {
elements.inputNumber.setAttribute( 'type', 'number' );

elements.contentEditable = document.createElement( 'p' );
elements.contentEditable.contentEditable = true;
// The DOM stringifies the boolean.
elements.contentEditable.contentEditable = true as unknown as string;
} );

it( 'should return true if vertically navigating input without modifier', () => {
Expand Down
54 changes: 54 additions & 0 deletions packages/block-editor/src/components/writing-flow/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { HTMLAttributes, ReactNode } from 'react';

/**
* The `SyntheticEvent`-like object a block's `wrapperProps` event handler
* receives when the writing flow host calls it under `editableRoot`.
*/
export interface BlockSyntheticEvent {
nativeEvent: Event;
type: string;
target: Element | undefined;
currentTarget: Element | null;
defaultPrevented?: boolean;
preventDefault: () => void;
isDefaultPrevented: () => boolean | undefined;
stopPropagation: () => void;
isPropagationStopped: () => boolean;
persist: () => void;
isPersistent: () => boolean;
[ property: string ]: unknown;
}

/**
* A block's `wrapperProps` event handler.
*/
export type BlockEventHandler = ( event: BlockSyntheticEvent ) => void;

/**
* A block's `wrapperProps` event handlers, keyed by DOM event type.
*/
export type BlockEventHandlers = Record< string, BlockEventHandler >;

/**
* A key down event, or an Enter `beforeinput` event, which writing flow
* handles alike. Rich text flags its legacy split handler on the event.
*/
export type WritingFlowKeyEvent = KeyboardEvent &
Partial< Pick< InputEvent, 'inputType' > > & {
__deprecatedOnSplit?: unknown;
};

/**
* A clipboard event, which rich text flags when it has a legacy split
* handler.
*/
export type WritingFlowClipboardEvent = ClipboardEvent & {
__deprecatedOnSplit?: unknown;
};

/**
* Props of `WritingFlow`, which renders a `div` around the block list.
*/
export type WritingFlowProps = HTMLAttributes< HTMLDivElement > & {
children?: ReactNode;
};
Loading
Loading