admin管理员组文章数量:1122832
I have a requirement to add up to 8 user selected custom colors to the list of default options when using the Colour Palette.
I have registered a meta that saves custom colours.
Problem:
I want to save custom colours when the "Update" post button is pressed instead of using the onChange
listener, because the listener fires multiple times when the colour selector tool is dragged.
What I am doing at the moment:
const { isSavingPost } = useSelect( select => {
const { isSavingPost } = select( 'core/editor' );
return {
isSavingPost: isSavingPost(),
}
} );
useEffect( () => {
if ( isSavingPost ) {
// Logic to save custom colours.
setAttributes( { customColors } );
}
}, [ isSavingPost ] );
The problem is that when setAttribute()
is called when "Update" is pressed, the state is updated after the post is saved, which Gutenberg considers dirty and a page reload following an "Update" shows this:
I am however able to save it if I press "Update" post twice, but this is not feasible for the end user.
Is there a way I can fire wp.data.dispatch( 'core/editor' ).editPost()
or setAttributes()
when a Post is saved manually by pressing the "Update" button?
I have a requirement to add up to 8 user selected custom colors to the list of default options when using the Colour Palette.
I have registered a meta that saves custom colours.
Problem:
I want to save custom colours when the "Update" post button is pressed instead of using the onChange
listener, because the listener fires multiple times when the colour selector tool is dragged.
What I am doing at the moment:
const { isSavingPost } = useSelect( select => {
const { isSavingPost } = select( 'core/editor' );
return {
isSavingPost: isSavingPost(),
}
} );
useEffect( () => {
if ( isSavingPost ) {
// Logic to save custom colours.
setAttributes( { customColors } );
}
}, [ isSavingPost ] );
The problem is that when setAttribute()
is called when "Update" is pressed, the state is updated after the post is saved, which Gutenberg considers dirty and a page reload following an "Update" shows this:
I am however able to save it if I press "Update" post twice, but this is not feasible for the end user.
Is there a way I can fire wp.data.dispatch( 'core/editor' ).editPost()
or setAttributes()
when a Post is saved manually by pressing the "Update" button?
1 Answer
Reset to default 0You can still use the onChange listener and do setAttributes only when value passed in onChange is different from the one in attributes.customColors
. This is one if
and should not penalize performance much.
本文标签:
版权声明:本文标题:block editor - Gutenberg - Dispatch action or setAttributes() on Post SaveUpdate action instead of onChange listener 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736292949a1929042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论