Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor Write Mode format filtering to use useFormatTypes hook
- Remove withWriteModeFilter HOC approach in favor of centralized filtering
- Add Write Mode detection to RichText component with experiment flag check
- Enhance useFormatTypes hook with disableNoneEssentialFormatting parameter
- Centralize all format filtering logic in one place for better performance
- Eliminate individual store subscriptions per format component
- Maintain same functionality with improved architecture and performance
  • Loading branch information
getdave committed Aug 7, 2025
commit 6fc6b0a9153825091b3b519ccd0580ff191e1ec7
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useContext, useMemo } from '@wordpress/element';
* Internal dependencies
*/
import BlockContext from '../block-context';
import withWriteModeFilter from './with-write-mode-filter';

const DEFAULT_BLOCK_CONTEXT = {};

Expand Down Expand Up @@ -38,17 +37,14 @@ function Edit( { onChange, onFocus, value, forwardedRef, settings } ) {
return null;
}

// Apply the write mode filter HOC
const FilteredEditFunction = withWriteModeFilter( EditFunction, settings );

const activeFormat = getActiveFormat( value, name );
const isActive = activeFormat !== undefined;
const activeObject = getActiveObject( value );
const isObjectActive =
activeObject !== undefined && activeObject.type === name;

return (
<FilteredEditFunction
<EditFunction
key={ name }
isActive={ isActive }
activeAttributes={ isActive ? activeFormat.attributes || {} : {} }
Expand Down
32 changes: 22 additions & 10 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ export function RichTextWrapper(
return { isSelected: false };
}

const { getSelectionStart, getSelectionEnd } =
select( blockEditorStore );
const {
getSelectionStart,
getSelectionEnd,
getBlockEditingMode,
isNavigationMode,
} = select( blockEditorStore );
const selectionStart = getSelectionStart();
const selectionEnd = getSelectionEnd();

Expand All @@ -154,15 +158,22 @@ export function RichTextWrapper(
selectionStart: isSelected ? selectionStart.offset : undefined,
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
isWriteMode:
window?.__experimentalEditorWriteMode &&
isNavigationMode() &&
getBlockEditingMode( clientId ) === 'contentOnly',
};
};
const { selectionStart, selectionEnd, isSelected } = useSelect( selector, [
clientId,
identifier,
instanceId,
originalIsSelected,
isBlockSelected,
] );
const { selectionStart, selectionEnd, isSelected, isWriteMode } = useSelect(
selector,
[
clientId,
identifier,
instanceId,
originalIsSelected,
isBlockSelected,
]
);

const { disableBoundBlock, bindingsPlaceholder, bindingsLabel } = useSelect(
( select ) => {
Expand Down Expand Up @@ -323,8 +334,9 @@ export function RichTextWrapper(
} = useFormatTypes( {
clientId,
identifier,
withoutInteractiveFormatting,
allowedFormats: adjustedAllowedFormats,
withoutInteractiveFormatting,
disableNoneEssentialFormatting: isWriteMode,
} );

function addEditorOnlyFormats( value ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,48 @@ function getPrefixedSelectKeys( selected, prefix ) {
* This hook provides RichText with the `formatTypes` and its derived props from
* experimental format type settings.
*
* @param {Object} $0 Options
* @param {string} $0.clientId Block client ID.
* @param {string} $0.identifier Block attribute.
* @param {boolean} $0.withoutInteractiveFormatting Whether to clean the interactive formatting or not.
* @param {Array} $0.allowedFormats Allowed formats
* @param {Object} options Options
* @param {string} options.clientId Block client ID.
* @param {string} options.identifier Block attribute.
* @param {Array} options.allowedFormats Allowed formats
* @param {boolean} options.withoutInteractiveFormatting Whether to clean the interactive formatting or not.
* @param {boolean} options.disableNoneEssentialFormatting Whether to disable none-essential formatting or not.
*/
export function useFormatTypes( {
clientId,
identifier,
withoutInteractiveFormatting,
allowedFormats,
withoutInteractiveFormatting,
disableNoneEssentialFormatting = false,
} ) {
const allFormatTypes = useSelect( formatTypesSelector, [] );
const formatTypes = useMemo( () => {
return allFormatTypes.filter( ( { name, interactive, tagName } ) => {
if ( allowedFormats && ! allowedFormats.includes( name ) ) {
return false;
}
return allFormatTypes.filter(
( { name, interactive, tagName, __unstableEssential } ) => {
if ( allowedFormats && ! allowedFormats.includes( name ) ) {
return false;
}

if (
withoutInteractiveFormatting &&
( interactive || interactiveContentTags.has( tagName ) )
) {
return false;
}
if ( disableNoneEssentialFormatting && ! __unstableEssential ) {
return false;
}

if (
withoutInteractiveFormatting &&
( interactive || interactiveContentTags.has( tagName ) )
) {
return false;
}

return true;
} );
}, [ allFormatTypes, allowedFormats, withoutInteractiveFormatting ] );
return true;
}
);
}, [
allFormatTypes,
allowedFormats,
disableNoneEssentialFormatting,
withoutInteractiveFormatting,
] );
const keyedSelected = useSelect(
( select ) =>
formatTypes.reduce( ( accumulator, type ) => {
Expand Down

This file was deleted.

Loading