22 * WordPress dependencies
33 */
44import { __ } from '@wordpress/i18n' ;
5- import { useAnchor , removeFormat } from '@wordpress/rich-text' ;
5+ import { useAnchor } from '@wordpress/rich-text' ;
66import { Popover , Button , FontSizePicker } from '@wordpress/components' ;
7- import { useSelect } from '@wordpress/data' ;
87
98/**
109 * Internal dependencies
1110 */
12- import { createFormatHelpers } from '../utils ' ;
11+ import { useFontSize } from './hooks ' ;
1312
1413/**
1514 * InlineUI component for handling FontSize text formatting options.
@@ -31,86 +30,27 @@ function InlineUI( {
3130 contentRef,
3231 isActive,
3332} ) {
34- const { replace } = createFormatHelpers ( {
35- value,
36- onChange,
37- formatType : 'blablablocks/font-size' ,
38- activeAttributes,
39- } ) ;
40-
4133 const anchor = useAnchor ( {
4234 editableContentElement : contentRef ,
4335 settings : { isActive } ,
4436 } ) ;
4537
46- const fontSizes = useSelect (
47- ( select ) => select ( 'core/block-editor' ) . getSettings ( ) . fontSizes ,
48- [ ]
49- ) ;
50-
51- const handleFontSizeChange = ( newFontSize ) => {
52- if ( ! newFontSize ) {
53- onChange ( removeFormat ( value , 'blablablocks/font-size' ) ) ;
54- return ;
55- }
56-
57- if ( newFontSize ) {
58- // Find the font size object to get the slug
59- const fontSizeObj = fontSizes ?. find (
60- ( size ) =>
61- size . size === newFontSize || size . slug === newFontSize
62- ) ;
63-
64- if ( fontSizeObj && fontSizeObj . slug ) {
65- // Use CSS class for theme-defined font sizes
66- replace ( {
67- class : `has-${ fontSizeObj . slug } -font-size` ,
68- style : undefined ,
69- } ) ;
70- } else {
71- // Use inline style for custom font sizes
72- replace ( {
73- style : `font-size: ${ newFontSize } ` ,
74- class : undefined ,
75- } ) ;
76- }
77- }
78- } ;
38+ const {
39+ fontSizes,
40+ fontSizeValue,
41+ onFontSizeChange,
42+ onClear,
43+ } = useFontSize ( {
44+ value,
45+ onChange,
46+ activeAttributes,
47+ } ) ;
7948
8049 const handleClear = ( ) => {
81- onChange ( removeFormat ( value , 'blablablocks/font-size' ) ) ;
50+ onClear ( ) ;
8251 onClose ( ) ;
8352 } ;
8453
85- // Extract current font size from class or style attribute
86- const getCurrentFontSize = ( ) => {
87- // Check if there's a class attribute with has-*-font-size pattern
88- const classAttr = activeAttributes . class ;
89- if ( classAttr ) {
90- const match = classAttr . match ( / h a s - ( [ a - z 0 - 9 - ] + ) - f o n t - s i z e / ) ;
91- if ( match ) {
92- const slug = match [ 1 ] ;
93- const fontSizeObj = fontSizes ?. find (
94- ( size ) => size . slug === slug
95- ) ;
96- return fontSizeObj ? fontSizeObj . size : null ;
97- }
98- }
99-
100- // Check if there's a style attribute with font-size
101- const styleAttr = activeAttributes . style ;
102- if ( styleAttr ) {
103- const match = styleAttr . match ( / f o n t - s i z e : \s * ( [ ^ ; ] + ) / ) ;
104- if ( match ) {
105- return match [ 1 ] . trim ( ) ;
106- }
107- }
108-
109- return null ;
110- } ;
111-
112- const fontSizeValue = getCurrentFontSize ( ) ;
113-
11454 return (
11555 < Popover
11656 anchor = { anchor }
@@ -123,7 +63,7 @@ function InlineUI( {
12363 < div style = { { padding : '16px' , minWidth : '220px' } } >
12464 < FontSizePicker
12565 value = { fontSizeValue }
126- onChange = { handleFontSizeChange }
66+ onChange = { onFontSizeChange }
12767 fontSizes = { fontSizes }
12868 __next40pxDefaultSize
12969 />
0 commit comments