Skip to content
Merged
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
23 changes: 19 additions & 4 deletions packages/block-editor/src/hooks/fit-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,25 @@ function useFitText( { fitText, name, clientId } ) {
const hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY );
const blockElement = useBlockElement( clientId );

// Monitor block attribute changes, and parent changes.
// Monitor block attribute changes, parent changes, and block mode.
// Any attribute or parent change may change the available space.
const { blockAttributes, parentId } = useSelect(
// Block mode is needed to disable fit text when in HTML editing mode.
const { blockAttributes, parentId, blockMode } = useSelect(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use an early return for further performance improvements?

const { blockAttributes, parentId, blockMode } = useSelect(
	( select ) => {
		if ( ! clientId || ! hasFitTextSupport || ! fitText ) {
			return EMPTY_OBJECT;
		}
		const _blockMode =
			select( blockEditorStore ).getBlockMode( clientId );
		if ( _blockMode === 'html' ) {
			return { mode: _blockMode };
		}
		return {
			blockAttributes:
				select( blockEditorStore ).getBlockAttributes( clientId ),
			parentId:
				select( blockEditorStore ).getBlockRootClientId( clientId ),
			blockMode: _blockMode,
		};
	},
	[ clientId, hasFitTextSupport, fitText ]
);

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, good suggestion I will apply it 👍

( select ) => {
if ( ! clientId || ! hasFitTextSupport || ! fitText ) {
return EMPTY_OBJECT;
}
const _blockMode =
select( blockEditorStore ).getBlockMode( clientId );
if ( _blockMode === 'html' ) {
return { blockMode: _blockMode };
}
return {
blockAttributes:
select( blockEditorStore ).getBlockAttributes( clientId ),
parentId:
select( blockEditorStore ).getBlockRootClientId( clientId ),
blockMode: _blockMode,
};
},
[ clientId, hasFitTextSupport, fitText ]
Expand Down Expand Up @@ -118,7 +125,8 @@ function useFitText( { fitText, name, clientId } ) {
! fitText ||
! blockElement ||
! clientId ||
! hasFitTextSupport
! hasFitTextSupport ||
blockMode === 'html'
) {
return;
}
Expand Down Expand Up @@ -189,11 +197,17 @@ function useFitText( { fitText, name, clientId } ) {
applyFitText,
blockElement,
hasFitTextSupport,
blockMode,
] );

// Trigger fit text recalculation when content changes
useEffect( () => {
if ( fitText && blockElement && hasFitTextSupport ) {
if (
fitText &&
blockElement &&
hasFitTextSupport &&
blockMode !== 'html'
) {
// Wait for next frame to ensure DOM has updated after content changes
const frameId = window.requestAnimationFrame( () => {
if ( blockElement ) {
Expand All @@ -209,6 +223,7 @@ function useFitText( { fitText, name, clientId } ) {
applyFitText,
blockElement,
hasFitTextSupport,
blockMode,
] );

return { fontSize };
Expand Down
Loading