@wordpress/block-library
Version:
Block library for the WordPress editor.
40 lines (39 loc) • 1.41 kB
JavaScript
import { useEvent } from "@wordpress/compose";
import { useEffect, useRef } from "@wordpress/element";
import deprecated from "@wordpress/deprecated";
import { useDispatch } from "@wordpress/data";
import { store } from "@wordpress/block-editor";
// packages/block-library/src/utils/deprecated-text-align-attributes.js
/**
* If a plugin is still using the old textAlign attribute, we need to migrate its value
* to the new style.typography.textAlign attribute.
*
* @param {Object} props Block props.
*/
function useDeprecatedTextAlign(props) {
const { name, attributes, setAttributes } = props;
const { textAlign } = attributes;
const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(store);
const updateStyleWithAlign = useEvent(() => {
deprecated(`textAlign attribute in ${name}`, {
alternative: "style.typography.textAlign",
since: "7.0"
});
__unstableMarkNextChangeAsNotPersistent();
setAttributes((currentAttr) => ({ style: {
...currentAttr.style,
typography: {
...currentAttr.style?.typography,
textAlign
}
} }));
});
const lastUpdatedAlignRef = useRef();
useEffect(() => {
if (textAlign === lastUpdatedAlignRef.current) return;
lastUpdatedAlignRef.current = textAlign;
updateStyleWithAlign();
}, [textAlign, updateStyleWithAlign]);
}
export { useDeprecatedTextAlign as default };
//# sourceMappingURL=deprecated-text-align-attributes.mjs.map