Skip to content

Commit 94d00d1

Browse files
committed
chore: core refactor and UI polish
1 parent 58cf56e commit 94d00d1

File tree

3 files changed

+118
-74
lines changed

3 files changed

+118
-74
lines changed

src/font-size/editor.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.block-editor-format-toolbar__blablablocks-font-size-popover {
66
.components-popover__content {
77
min-width: 20.625rem;
8+
padding: 5px;
89
}
910

1011
.reset-button {

src/font-size/hooks.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { useSelect } from '@wordpress/data';
5+
import { removeFormat } from '@wordpress/rich-text';
6+
7+
/**
8+
* Internal dependencies
9+
*/
10+
import { createFormatHelpers } from '../utils';
11+
12+
/**
13+
* Custom hook for handling font size formatting.
14+
*
15+
* @param {Object} props - The hook properties.
16+
* @param {string} props.value - The current rich text value.
17+
* @param {Function} props.onChange - Callback to update the format.
18+
* @param {Object} props.activeAttributes - The currently active format attributes.
19+
* @return {Object} The font size state and handlers.
20+
*/
21+
export const useFontSize = ( { value, onChange, activeAttributes } ) => {
22+
const { replace } = createFormatHelpers( {
23+
value,
24+
onChange,
25+
formatType: 'blablablocks/font-size',
26+
activeAttributes,
27+
} );
28+
29+
const fontSizes = useSelect(
30+
( select ) => select( 'core/block-editor' ).getSettings().fontSizes,
31+
[]
32+
);
33+
34+
/**
35+
* Extract current font size from class or style attribute.
36+
*
37+
* @return {string|null} Current font size.
38+
*/
39+
const getCurrentFontSize = () => {
40+
const { class: classAttr, style: styleAttr } = activeAttributes;
41+
42+
if ( classAttr ) {
43+
const match = classAttr.match( /has-([a-z0-9-]+)-font-size/ );
44+
if ( match ) {
45+
const slug = match[ 1 ];
46+
const fontSizeObj = fontSizes?.find(
47+
( size ) => size.slug === slug
48+
);
49+
return fontSizeObj ? fontSizeObj.size : null;
50+
}
51+
}
52+
53+
if ( styleAttr ) {
54+
const match = styleAttr.match( /font-size:\s*([^;]+)/ );
55+
if ( match ) {
56+
return match[ 1 ].trim();
57+
}
58+
}
59+
60+
return null;
61+
};
62+
63+
/**
64+
* Handle font size changes.
65+
*
66+
* @param {string} newFontSize The new font size value.
67+
*/
68+
const onFontSizeChange = ( newFontSize ) => {
69+
if ( ! newFontSize ) {
70+
onChange( removeFormat( value, 'blablablocks/font-size' ) );
71+
return;
72+
}
73+
74+
const fontSizeObj = fontSizes?.find(
75+
( size ) =>
76+
size.size === newFontSize || size.slug === newFontSize
77+
);
78+
79+
if ( fontSizeObj && fontSizeObj.slug ) {
80+
replace( {
81+
class: `has-${ fontSizeObj.slug }-font-size`,
82+
} );
83+
} else {
84+
replace( {
85+
style: `font-size: ${ newFontSize }`,
86+
} );
87+
}
88+
};
89+
90+
/**
91+
* Clear the font size format.
92+
*/
93+
const onClear = () => {
94+
onChange( removeFormat( value, 'blablablocks/font-size' ) );
95+
};
96+
97+
return {
98+
fontSizes,
99+
fontSizeValue: getCurrentFontSize(),
100+
onFontSizeChange,
101+
onClear,
102+
};
103+
};

src/font-size/inline-ui.js

Lines changed: 14 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
* WordPress dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5-
import { useAnchor, removeFormat } from '@wordpress/rich-text';
5+
import { useAnchor } from '@wordpress/rich-text';
66
import { 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( /has-([a-z0-9-]+)-font-size/ );
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( /font-size:\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

Comments
 (0)