|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { useEvent } from '@wordpress/compose'; |
| 5 | +import { useEffect, useRef } from '@wordpress/element'; |
| 6 | +import deprecated from '@wordpress/deprecated'; |
| 7 | +import { useDispatch } from '@wordpress/data'; |
| 8 | +import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 9 | + |
| 10 | +/** |
| 11 | + * If a plugin is still using the old textAlign attribute, we need to migrate its value |
| 12 | + * to the new style.typography.textAlign attribute. |
| 13 | + * |
| 14 | + * @param {Object} props Block props. |
| 15 | + */ |
| 16 | +export default function useDeprecatedTextAlign( props ) { |
| 17 | + const { name, attributes, setAttributes } = props; |
| 18 | + const { textAlign, style } = attributes; |
| 19 | + const { __unstableMarkNextChangeAsNotPersistent } = |
| 20 | + useDispatch( blockEditorStore ); |
| 21 | + const updateStyleWithAlign = useEvent( () => { |
| 22 | + deprecated( `textAlign attribute in ${ name }`, { |
| 23 | + alternative: 'style.typography.textAlign', |
| 24 | + since: '7.0', |
| 25 | + } ); |
| 26 | + __unstableMarkNextChangeAsNotPersistent(); |
| 27 | + setAttributes( { |
| 28 | + style: { |
| 29 | + ...style, |
| 30 | + typography: { |
| 31 | + ...style?.typography, |
| 32 | + textAlign, |
| 33 | + }, |
| 34 | + }, |
| 35 | + } ); |
| 36 | + } ); |
| 37 | + const lastUpdatedAlignRef = useRef(); |
| 38 | + useEffect( () => { |
| 39 | + if ( textAlign === lastUpdatedAlignRef.current ) { |
| 40 | + return; |
| 41 | + } |
| 42 | + lastUpdatedAlignRef.current = textAlign; |
| 43 | + updateStyleWithAlign(); |
| 44 | + }, [ textAlign, updateStyleWithAlign ] ); |
| 45 | +} |
0 commit comments